Added new SCSI_ASENSE_NOT_READY_TO_READY_CHANGE constant to the Mass Storage class...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / MIDI.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_HOST)
34
35 #define __INCLUDE_FROM_MIDI_CLASS_HOST_C
36 #define __INCLUDE_FROM_MIDI_DRIVER
37 #include "MIDI.h"
38
39 uint8_t MIDI_Host_ConfigurePipes(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
40 uint16_t ConfigDescriptorSize,
41 void* ConfigDescriptorData)
42 {
43 uint8_t FoundEndpoints = 0;
44
45 memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));
46
47 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
48 return MIDI_ENUMERROR_InvalidConfigDescriptor;
49
50 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
51 DCOMP_MIDI_Host_NextMIDIStreamingInterface) != DESCRIPTOR_SEARCH_COMP_Found)
52 {
53 return MIDI_ENUMERROR_NoStreamingInterfaceFound;
54 }
55
56 while (FoundEndpoints != (MIDI_FOUND_DATAPIPE_IN | MIDI_FOUND_DATAPIPE_OUT))
57 {
58 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
59 DCOMP_MIDI_Host_NextMIDIStreamingDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
60 {
61 return MIDI_ENUMERROR_EndpointsNotFound;
62 }
63
64 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
65
66 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
67 {
68 Pipe_ConfigurePipe(MIDIInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_IN,
69 EndpointData->EndpointAddress, EndpointData->EndpointSize,
70 MIDIInterfaceInfo->Config.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
71 MIDIInterfaceInfo->State.DataINPipeSize = EndpointData->EndpointSize;
72
73 FoundEndpoints |= MIDI_FOUND_DATAPIPE_IN;
74 }
75 else
76 {
77 Pipe_ConfigurePipe(MIDIInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_OUT,
78 EndpointData->EndpointAddress, EndpointData->EndpointSize,
79 MIDIInterfaceInfo->Config.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
80 MIDIInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;
81
82 FoundEndpoints |= MIDI_FOUND_DATAPIPE_OUT;
83 }
84 }
85
86 MIDIInterfaceInfo->State.IsActive = true;
87 return MIDI_ENUMERROR_NoError;
88 }
89
90 static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingInterface(void* const CurrentDescriptor)
91 {
92 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
93 {
94 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
95 USB_Descriptor_Interface_t);
96
97 if ((CurrentInterface->Class == MIDI_STREAMING_CLASS) &&
98 (CurrentInterface->SubClass == MIDI_STREAMING_SUBCLASS) &&
99 (CurrentInterface->Protocol == MIDI_STREAMING_PROTOCOL))
100 {
101 return DESCRIPTOR_SEARCH_Found;
102 }
103 }
104
105 return DESCRIPTOR_SEARCH_NotFound;
106 }
107
108 static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingDataEndpoint(void* const CurrentDescriptor)
109 {
110 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
111 {
112 USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,
113 USB_Descriptor_Endpoint_t);
114
115 uint8_t EndpointType = (CurrentEndpoint->Attributes & EP_TYPE_MASK);
116
117 if ((EndpointType == EP_TYPE_BULK) && !(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress)))
118 return DESCRIPTOR_SEARCH_Found;
119 }
120 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
121 {
122 return DESCRIPTOR_SEARCH_Fail;
123 }
124
125 return DESCRIPTOR_SEARCH_NotFound;
126 }
127
128 uint8_t MIDI_Host_Flush(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo)
129 {
130 if (USB_HostState != HOST_STATE_Configured)
131 return PIPE_RWSTREAM_DeviceDisconnected;
132
133 uint8_t ErrorCode;
134
135 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataOUTPipeNumber);
136
137 if (Pipe_BytesInPipe())
138 {
139 Pipe_ClearOUT();
140
141 if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
142 return ErrorCode;
143 }
144
145 return PIPE_READYWAIT_NoError;
146 }
147
148 uint8_t MIDI_Host_SendEventPacket(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
149 MIDI_EventPacket_t* const Event)
150 {
151 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
152 return HOST_SENDCONTROL_DeviceDisconnected;
153
154 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataOUTPipeNumber);
155
156 if (Pipe_IsReadWriteAllowed())
157 {
158 uint8_t ErrorCode;
159
160 if ((ErrorCode = Pipe_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
161 return ErrorCode;
162
163 if (!(Pipe_IsReadWriteAllowed()))
164 Pipe_ClearOUT();
165 }
166
167 return PIPE_RWSTREAM_NoError;
168 }
169
170 bool MIDI_Host_ReceiveEventPacket(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
171 MIDI_EventPacket_t* const Event)
172 {
173 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
174 return HOST_SENDCONTROL_DeviceDisconnected;
175
176 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataINPipeNumber);
177
178 if (!(Pipe_IsReadWriteAllowed()))
179 return false;
180
181 Pipe_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK);
182
183 if (!(Pipe_IsReadWriteAllowed()))
184 Pipe_ClearIN();
185
186 return true;
187 }
188
189 #endif