3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../Core/USBMode.h"
34 #if defined(USB_CAN_BE_HOST)
36 #define __INCLUDE_FROM_MIDI_DRIVER
37 #define __INCLUDE_FROM_MIDI_HOST_C
38 #include "MIDIClassHost.h"
40 uint8_t MIDI_Host_ConfigurePipes(USB_ClassInfo_MIDI_Host_t
* const MIDIInterfaceInfo
,
41 uint16_t ConfigDescriptorSize
,
42 void* ConfigDescriptorData
)
44 USB_Descriptor_Endpoint_t
* DataINEndpoint
= NULL
;
45 USB_Descriptor_Endpoint_t
* DataOUTEndpoint
= NULL
;
46 USB_Descriptor_Interface_t
* MIDIInterface
= NULL
;
48 memset(&MIDIInterfaceInfo
->State
, 0x00, sizeof(MIDIInterfaceInfo
->State
));
50 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
51 return MIDI_ENUMERROR_InvalidConfigDescriptor
;
53 while (!(DataINEndpoint
) || !(DataOUTEndpoint
))
55 if (!(MIDIInterface
) ||
56 USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
57 DCOMP_MIDI_Host_NextMIDIStreamingDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
59 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
60 DCOMP_MIDI_Host_NextMIDIStreamingInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
62 return MIDI_ENUMERROR_NoCompatibleInterfaceFound
;
65 MIDIInterface
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
);
67 DataINEndpoint
= NULL
;
68 DataOUTEndpoint
= NULL
;
73 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
75 if ((EndpointData
->EndpointAddress
& ENDPOINT_DIR_MASK
) == ENDPOINT_DIR_IN
)
76 DataINEndpoint
= EndpointData
;
78 DataOUTEndpoint
= EndpointData
;
81 for (uint8_t PipeNum
= 1; PipeNum
< PIPE_TOTAL_PIPES
; PipeNum
++)
86 uint8_t EndpointAddress
;
89 if (PipeNum
== MIDIInterfaceInfo
->Config
.DataINPipeNumber
)
91 Size
= le16_to_cpu(DataINEndpoint
->EndpointSize
);
92 EndpointAddress
= DataINEndpoint
->EndpointAddress
;
93 Token
= PIPE_TOKEN_IN
;
95 DoubleBanked
= MIDIInterfaceInfo
->Config
.DataINPipeDoubleBank
;
97 MIDIInterfaceInfo
->State
.DataINPipeSize
= DataINEndpoint
->EndpointSize
;
99 else if (PipeNum
== MIDIInterfaceInfo
->Config
.DataOUTPipeNumber
)
101 Size
= le16_to_cpu(DataOUTEndpoint
->EndpointSize
);
102 EndpointAddress
= DataOUTEndpoint
->EndpointAddress
;
103 Token
= PIPE_TOKEN_OUT
;
105 DoubleBanked
= MIDIInterfaceInfo
->Config
.DataOUTPipeDoubleBank
;
107 MIDIInterfaceInfo
->State
.DataOUTPipeSize
= DataOUTEndpoint
->EndpointSize
;
114 if (!(Pipe_ConfigurePipe(PipeNum
, Type
, Token
, EndpointAddress
, Size
,
115 DoubleBanked ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
)))
117 return MIDI_ENUMERROR_PipeConfigurationFailed
;
121 MIDIInterfaceInfo
->State
.InterfaceNumber
= MIDIInterface
->InterfaceNumber
;
122 MIDIInterfaceInfo
->State
.IsActive
= true;
124 return MIDI_ENUMERROR_NoError
;
127 static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingInterface(void* const CurrentDescriptor
)
129 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
131 if (Header
->Type
== DTYPE_Interface
)
133 USB_Descriptor_Interface_t
* Interface
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
);
135 if ((Interface
->Class
== AUDIO_CSCP_AudioClass
) &&
136 (Interface
->SubClass
== AUDIO_CSCP_MIDIStreamingSubclass
) &&
137 (Interface
->Protocol
== AUDIO_CSCP_StreamingProtocol
))
139 return DESCRIPTOR_SEARCH_Found
;
143 return DESCRIPTOR_SEARCH_NotFound
;
146 static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingDataEndpoint(void* const CurrentDescriptor
)
148 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
150 if (Header
->Type
== DTYPE_Endpoint
)
152 USB_Descriptor_Endpoint_t
* Endpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
);
154 uint8_t EndpointType
= (Endpoint
->Attributes
& EP_TYPE_MASK
);
156 if ((EndpointType
== EP_TYPE_BULK
) && !(Pipe_IsEndpointBound(Endpoint
->EndpointAddress
)))
157 return DESCRIPTOR_SEARCH_Found
;
159 else if (Header
->Type
== DTYPE_Interface
)
161 return DESCRIPTOR_SEARCH_Fail
;
164 return DESCRIPTOR_SEARCH_NotFound
;
167 void MIDI_Host_USBTask(USB_ClassInfo_MIDI_Host_t
* const MIDIInterfaceInfo
)
169 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MIDIInterfaceInfo
->State
.IsActive
))
172 #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
173 MIDI_Host_Flush(MIDIInterfaceInfo
);
177 uint8_t MIDI_Host_Flush(USB_ClassInfo_MIDI_Host_t
* const MIDIInterfaceInfo
)
179 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MIDIInterfaceInfo
->State
.IsActive
))
180 return PIPE_RWSTREAM_DeviceDisconnected
;
184 Pipe_SelectPipe(MIDIInterfaceInfo
->Config
.DataOUTPipeNumber
);
186 if (Pipe_BytesInPipe())
190 if ((ErrorCode
= Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError
)
194 return PIPE_READYWAIT_NoError
;
197 uint8_t MIDI_Host_SendEventPacket(USB_ClassInfo_MIDI_Host_t
* const MIDIInterfaceInfo
,
198 MIDI_EventPacket_t
* const Event
)
200 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MIDIInterfaceInfo
->State
.IsActive
))
201 return HOST_SENDCONTROL_DeviceDisconnected
;
205 Pipe_SelectPipe(MIDIInterfaceInfo
->Config
.DataOUTPipeNumber
);
207 if ((ErrorCode
= Pipe_Write_Stream_LE(Event
, sizeof(MIDI_EventPacket_t
), NULL
)) != PIPE_RWSTREAM_NoError
)
210 if (!(Pipe_IsReadWriteAllowed()))
213 return PIPE_RWSTREAM_NoError
;
216 bool MIDI_Host_ReceiveEventPacket(USB_ClassInfo_MIDI_Host_t
* const MIDIInterfaceInfo
,
217 MIDI_EventPacket_t
* const Event
)
219 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MIDIInterfaceInfo
->State
.IsActive
))
220 return HOST_SENDCONTROL_DeviceDisconnected
;
222 Pipe_SelectPipe(MIDIInterfaceInfo
->Config
.DataINPipeNumber
);
224 if (!(Pipe_IsReadWriteAllowed()))
227 Pipe_Read_Stream_LE(Event
, sizeof(MIDI_EventPacket_t
), NULL
);
229 if (!(Pipe_IsReadWriteAllowed()))