]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Class/Host/MIDIClassHost.c
Squashed 'tmk_core/' changes from b9e0ea0..caca2c0
[tmk_keyboard.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Class / Host / MIDIClassHost.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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 "../../Core/USBMode.h"
33
34 #if defined(USB_CAN_BE_HOST)
35
36 #define __INCLUDE_FROM_MIDI_DRIVER
37 #define __INCLUDE_FROM_MIDI_HOST_C
38 #include "MIDIClassHost.h"
39
40 uint8_t MIDI_Host_ConfigurePipes(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
41 uint16_t ConfigDescriptorSize,
42 void* ConfigDescriptorData)
43 {
44 USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
45 USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
46 USB_Descriptor_Interface_t* MIDIInterface = NULL;
47
48 memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));
49
50 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
51 return MIDI_ENUMERROR_InvalidConfigDescriptor;
52
53 while (!(DataINEndpoint) || !(DataOUTEndpoint))
54 {
55 if (!(MIDIInterface) ||
56 USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
57 DCOMP_MIDI_Host_NextMIDIStreamingDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
58 {
59 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
60 DCOMP_MIDI_Host_NextMIDIStreamingInterface) != DESCRIPTOR_SEARCH_COMP_Found)
61 {
62 return MIDI_ENUMERROR_NoCompatibleInterfaceFound;
63 }
64
65 MIDIInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
66
67 DataINEndpoint = NULL;
68 DataOUTEndpoint = NULL;
69
70 continue;
71 }
72
73 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
74
75 if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
76 DataINEndpoint = EndpointData;
77 else
78 DataOUTEndpoint = EndpointData;
79 }
80
81 MIDIInterfaceInfo->Config.DataINPipe.Size = le16_to_cpu(DataINEndpoint->EndpointSize);
82 MIDIInterfaceInfo->Config.DataINPipe.EndpointAddress = DataINEndpoint->EndpointAddress;
83 MIDIInterfaceInfo->Config.DataINPipe.Type = EP_TYPE_BULK;
84
85 MIDIInterfaceInfo->Config.DataOUTPipe.Size = le16_to_cpu(DataOUTEndpoint->EndpointSize);
86 MIDIInterfaceInfo->Config.DataOUTPipe.EndpointAddress = DataOUTEndpoint->EndpointAddress;
87 MIDIInterfaceInfo->Config.DataOUTPipe.Type = EP_TYPE_BULK;
88
89 if (!(Pipe_ConfigurePipeTable(&MIDIInterfaceInfo->Config.DataINPipe, 1)))
90 return false;
91
92 if (!(Pipe_ConfigurePipeTable(&MIDIInterfaceInfo->Config.DataOUTPipe, 1)))
93 return false;
94
95 MIDIInterfaceInfo->State.InterfaceNumber = MIDIInterface->InterfaceNumber;
96 MIDIInterfaceInfo->State.IsActive = true;
97
98 return MIDI_ENUMERROR_NoError;
99 }
100
101 static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingInterface(void* const CurrentDescriptor)
102 {
103 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
104
105 if (Header->Type == DTYPE_Interface)
106 {
107 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
108
109 if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
110 (Interface->SubClass == AUDIO_CSCP_MIDIStreamingSubclass) &&
111 (Interface->Protocol == AUDIO_CSCP_StreamingProtocol))
112 {
113 return DESCRIPTOR_SEARCH_Found;
114 }
115 }
116
117 return DESCRIPTOR_SEARCH_NotFound;
118 }
119
120 static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingDataEndpoint(void* const CurrentDescriptor)
121 {
122 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
123
124 if (Header->Type == DTYPE_Endpoint)
125 {
126 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
127
128 uint8_t EndpointType = (Endpoint->Attributes & EP_TYPE_MASK);
129
130 if ((EndpointType == EP_TYPE_BULK) && !(Pipe_IsEndpointBound(Endpoint->EndpointAddress)))
131 return DESCRIPTOR_SEARCH_Found;
132 }
133 else if (Header->Type == DTYPE_Interface)
134 {
135 return DESCRIPTOR_SEARCH_Fail;
136 }
137
138 return DESCRIPTOR_SEARCH_NotFound;
139 }
140
141 void MIDI_Host_USBTask(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo)
142 {
143 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
144 return;
145
146 #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
147 MIDI_Host_Flush(MIDIInterfaceInfo);
148 #endif
149 }
150
151 uint8_t MIDI_Host_Flush(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo)
152 {
153 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
154 return PIPE_RWSTREAM_DeviceDisconnected;
155
156 uint8_t ErrorCode;
157
158 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataOUTPipe.Address);
159 Pipe_Unfreeze();
160
161 if (Pipe_BytesInPipe())
162 {
163 Pipe_ClearOUT();
164
165 if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
166 {
167 Pipe_Freeze();
168 return ErrorCode;
169 }
170 }
171
172 Pipe_Freeze();
173
174 return PIPE_READYWAIT_NoError;
175 }
176
177 uint8_t MIDI_Host_SendEventPacket(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
178 MIDI_EventPacket_t* const Event)
179 {
180 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
181 return HOST_SENDCONTROL_DeviceDisconnected;
182
183 uint8_t ErrorCode;
184
185 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataOUTPipe.Address);
186 Pipe_Unfreeze();
187
188 if ((ErrorCode = Pipe_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL)) != PIPE_RWSTREAM_NoError)
189 {
190 Pipe_Freeze();
191 return ErrorCode;
192 }
193
194 if (!(Pipe_IsReadWriteAllowed()))
195 Pipe_ClearOUT();
196
197 Pipe_Freeze();
198
199 return PIPE_RWSTREAM_NoError;
200 }
201
202 bool MIDI_Host_ReceiveEventPacket(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
203 MIDI_EventPacket_t* const Event)
204 {
205 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
206 return HOST_SENDCONTROL_DeviceDisconnected;
207
208 bool DataReady = false;
209
210 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataINPipe.Address);
211 Pipe_Unfreeze();
212
213 if (Pipe_IsINReceived())
214 {
215 if (Pipe_BytesInPipe())
216 {
217 Pipe_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL);
218 DataReady = true;
219 }
220
221 if (!(Pipe_BytesInPipe()))
222 Pipe_ClearIN();
223 }
224
225 Pipe_Freeze();
226
227 return DataReady;
228 }
229
230 #endif
231
Imprint / Impressum