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