Fix nasty bug in USBInterrupt.c which would cause a lockup if the control endpoint...
[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, uint16_t ConfigDescriptorSize,
40 void* ConfigDescriptorData)
41 {
42 uint8_t FoundEndpoints = 0;
43
44 memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));
45
46 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
47 return MIDI_ENUMERROR_InvalidConfigDescriptor;
48
49 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
50 DCOMP_MIDI_Host_NextMIDIStreamingInterface) != DESCRIPTOR_SEARCH_COMP_Found)
51 {
52 return MIDI_ENUMERROR_NoStreamingInterfaceFound;
53 }
54
55 while (FoundEndpoints != (MIDI_FOUND_DATAPIPE_IN | MIDI_FOUND_DATAPIPE_OUT))
56 {
57 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
58 DCOMP_MIDI_Host_NextMIDIStreamingDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
59 {
60 return MIDI_ENUMERROR_EndpointsNotFound;
61 }
62
63 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
64
65 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
66 {
67 Pipe_ConfigurePipe(MIDIInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_IN,
68 EndpointData->EndpointAddress, EndpointData->EndpointSize,
69 MIDIInterfaceInfo->Config.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
70 MIDIInterfaceInfo->State.DataINPipeSize = EndpointData->EndpointSize;
71
72 FoundEndpoints |= MIDI_FOUND_DATAPIPE_IN;
73 }
74 else
75 {
76 Pipe_ConfigurePipe(MIDIInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_OUT,
77 EndpointData->EndpointAddress, EndpointData->EndpointSize,
78 MIDIInterfaceInfo->Config.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
79 MIDIInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;
80
81 FoundEndpoints |= MIDI_FOUND_DATAPIPE_OUT;
82 }
83 }
84
85 MIDIInterfaceInfo->State.IsActive = true;
86 return MIDI_ENUMERROR_NoError;
87 }
88
89 static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingInterface(void* const CurrentDescriptor)
90 {
91 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
92 {
93 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
94 USB_Descriptor_Interface_t);
95
96 if ((CurrentInterface->Class == MIDI_STREAMING_CLASS) &&
97 (CurrentInterface->SubClass == MIDI_STREAMING_SUBCLASS) &&
98 (CurrentInterface->Protocol == MIDI_STREAMING_PROTOCOL))
99 {
100 return DESCRIPTOR_SEARCH_Found;
101 }
102 }
103
104 return DESCRIPTOR_SEARCH_NotFound;
105 }
106
107 static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingDataEndpoint(void* const CurrentDescriptor)
108 {
109 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
110 {
111 USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,
112 USB_Descriptor_Endpoint_t);
113
114 uint8_t EndpointType = (CurrentEndpoint->Attributes & EP_TYPE_MASK);
115
116 if ((EndpointType == EP_TYPE_BULK) && !(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress)))
117 return DESCRIPTOR_SEARCH_Found;
118 }
119 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
120 {
121 return DESCRIPTOR_SEARCH_Fail;
122 }
123
124 return DESCRIPTOR_SEARCH_NotFound;
125 }
126
127 uint8_t MIDI_Host_Flush(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo)
128 {
129 if (USB_HostState != HOST_STATE_Configured)
130 return PIPE_RWSTREAM_DeviceDisconnected;
131
132 uint8_t ErrorCode;
133
134 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataOUTPipeNumber);
135
136 if (Pipe_BytesInPipe())
137 {
138 Pipe_ClearOUT();
139
140 if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
141 return ErrorCode;
142 }
143
144 return PIPE_READYWAIT_NoError;
145 }
146
147 uint8_t MIDI_Host_SendEventPacket(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo, MIDI_EventPacket_t* const Event)
148 {
149 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
150 return HOST_SENDCONTROL_DeviceDisconnected;
151
152 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataOUTPipeNumber);
153
154 if (Pipe_IsReadWriteAllowed())
155 {
156 uint8_t ErrorCode;
157
158 if ((ErrorCode = Pipe_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
159 return ErrorCode;
160
161 if (!(Pipe_IsReadWriteAllowed()))
162 Pipe_ClearOUT();
163 }
164
165 return PIPE_RWSTREAM_NoError;
166 }
167
168 bool MIDI_Host_ReceiveEventPacket(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo, MIDI_EventPacket_t* const Event)
169 {
170 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
171 return HOST_SENDCONTROL_DeviceDisconnected;
172
173 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataINPipeNumber);
174
175 if (!(Pipe_IsReadWriteAllowed()))
176 return false;
177
178 Pipe_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK);
179
180 if (!(Pipe_IsReadWriteAllowed()))
181 Pipe_ClearIN();
182
183 return true;
184 }
185
186 #endif