6aed766c8cf4eb0de2fc157e4caa7001c7f19f29
[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.lufa-lib.org
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_DRIVER
36 #define __INCLUDE_FROM_MIDI_HOST_C
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 USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
44 USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
45 USB_Descriptor_Interface_t* MIDIInterface = NULL;
46
47 memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));
48
49 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
50 return MIDI_ENUMERROR_InvalidConfigDescriptor;
51
52 while (!(DataINEndpoint) || !(DataOUTEndpoint))
53 {
54 if (!(MIDIInterface) ||
55 USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
56 DCOMP_MIDI_Host_NextMIDIStreamingDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
57 {
58 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
59 DCOMP_MIDI_Host_NextMIDIStreamingInterface) != DESCRIPTOR_SEARCH_COMP_Found)
60 {
61 return MIDI_ENUMERROR_NoCompatibleInterfaceFound;
62 }
63
64 MIDIInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
65
66 DataINEndpoint = NULL;
67 DataOUTEndpoint = NULL;
68
69 continue;
70 }
71
72 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
73
74 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
75 DataINEndpoint = EndpointData;
76 else
77 DataOUTEndpoint = EndpointData;
78 }
79
80 for (uint8_t PipeNum = 1; PipeNum < PIPE_TOTAL_PIPES; PipeNum++)
81 {
82 if (PipeNum == MIDIInterfaceInfo->Config.DataINPipeNumber)
83 {
84 Pipe_ConfigurePipe(PipeNum, EP_TYPE_BULK, PIPE_TOKEN_IN,
85 DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize,
86 MIDIInterfaceInfo->Config.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
87
88 MIDIInterfaceInfo->State.DataINPipeSize = DataINEndpoint->EndpointSize;
89 }
90 else if (PipeNum == MIDIInterfaceInfo->Config.DataOUTPipeNumber)
91 {
92 Pipe_ConfigurePipe(PipeNum, EP_TYPE_BULK, PIPE_TOKEN_OUT,
93 DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize,
94 MIDIInterfaceInfo->Config.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
95
96 MIDIInterfaceInfo->State.DataOUTPipeSize = DataOUTEndpoint->EndpointSize;
97 }
98 }
99
100 MIDIInterfaceInfo->State.InterfaceNumber = MIDIInterface->InterfaceNumber;
101 MIDIInterfaceInfo->State.IsActive = true;
102
103 return MIDI_ENUMERROR_NoError;
104 }
105
106 static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingInterface(void* const CurrentDescriptor)
107 {
108 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
109 {
110 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
111 USB_Descriptor_Interface_t);
112
113 if ((CurrentInterface->Class == AUDIO_CSCP_AudioClass) &&
114 (CurrentInterface->SubClass == AUDIO_CSCP_MIDIStreamingSubclass) &&
115 (CurrentInterface->Protocol == AUDIO_CSCP_StreamingProtocol))
116 {
117 return DESCRIPTOR_SEARCH_Found;
118 }
119 }
120
121 return DESCRIPTOR_SEARCH_NotFound;
122 }
123
124 static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingDataEndpoint(void* const CurrentDescriptor)
125 {
126 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
127 {
128 USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,
129 USB_Descriptor_Endpoint_t);
130
131 uint8_t EndpointType = (CurrentEndpoint->Attributes & EP_TYPE_MASK);
132
133 if ((EndpointType == EP_TYPE_BULK) && !(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress)))
134 return DESCRIPTOR_SEARCH_Found;
135 }
136 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
137 {
138 return DESCRIPTOR_SEARCH_Fail;
139 }
140
141 return DESCRIPTOR_SEARCH_NotFound;
142 }
143
144 void MIDI_Host_USBTask(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo)
145 {
146 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
147 return;
148
149 #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
150 MIDI_Host_Flush(MIDIInterfaceInfo);
151 #endif
152 }
153
154 uint8_t MIDI_Host_Flush(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo)
155 {
156 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
157 return PIPE_RWSTREAM_DeviceDisconnected;
158
159 uint8_t ErrorCode;
160
161 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataOUTPipeNumber);
162
163 if (Pipe_BytesInPipe())
164 {
165 Pipe_ClearOUT();
166
167 if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
168 return ErrorCode;
169 }
170
171 return PIPE_READYWAIT_NoError;
172 }
173
174 uint8_t MIDI_Host_SendEventPacket(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
175 MIDI_EventPacket_t* const Event)
176 {
177 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
178 return HOST_SENDCONTROL_DeviceDisconnected;
179
180 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataOUTPipeNumber);
181
182 if (Pipe_IsReadWriteAllowed())
183 {
184 uint8_t ErrorCode;
185
186 if ((ErrorCode = Pipe_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
187 return ErrorCode;
188
189 if (!(Pipe_IsReadWriteAllowed()))
190 Pipe_ClearOUT();
191 }
192
193 return PIPE_RWSTREAM_NoError;
194 }
195
196 bool MIDI_Host_ReceiveEventPacket(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
197 MIDI_EventPacket_t* const Event)
198 {
199 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
200 return HOST_SENDCONTROL_DeviceDisconnected;
201
202 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataINPipeNumber);
203
204 if (!(Pipe_IsReadWriteAllowed()))
205 return false;
206
207 Pipe_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NO_STREAM_CALLBACK);
208
209 if (!(Pipe_IsReadWriteAllowed()))
210 Pipe_ClearIN();
211
212 return true;
213 }
214
215 #endif
216