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