3 Copyright (C) Dean Camera, 2014.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2014 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 disclaims 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 MIDIInterfaceInfo->Config.DataINPipe.Size = le16_to_cpu(DataINEndpoint->EndpointSize);
82 MIDIInterfaceInfo->Config.DataINPipe.EndpointAddress = DataINEndpoint->EndpointAddress;
83 MIDIInterfaceInfo->Config.DataINPipe.Type = EP_TYPE_BULK;
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;
89 if (!(Pipe_ConfigurePipeTable(&MIDIInterfaceInfo->Config.DataINPipe, 1)))
90 return MIDI_ENUMERROR_PipeConfigurationFailed;
92 if (!(Pipe_ConfigurePipeTable(&MIDIInterfaceInfo->Config.DataOUTPipe, 1)))
93 return MIDI_ENUMERROR_PipeConfigurationFailed;
95 MIDIInterfaceInfo->State.InterfaceNumber = MIDIInterface->InterfaceNumber;
96 MIDIInterfaceInfo->State.IsActive = true;
98 return MIDI_ENUMERROR_NoError;
101 static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingInterface(void* const CurrentDescriptor)
103 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
105 if (Header->Type == DTYPE_Interface)
107 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
109 if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
110 (Interface->SubClass == AUDIO_CSCP_MIDIStreamingSubclass) &&
111 (Interface->Protocol == AUDIO_CSCP_StreamingProtocol))
113 return DESCRIPTOR_SEARCH_Found;
117 return DESCRIPTOR_SEARCH_NotFound;
120 static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingDataEndpoint(void* const CurrentDescriptor)
122 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
124 if (Header->Type == DTYPE_Endpoint)
126 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
128 uint8_t EndpointType = (Endpoint->Attributes & EP_TYPE_MASK);
130 if ((EndpointType == EP_TYPE_BULK) && !(Pipe_IsEndpointBound(Endpoint->EndpointAddress)))
131 return DESCRIPTOR_SEARCH_Found;
133 else if (Header->Type == DTYPE_Interface)
135 return DESCRIPTOR_SEARCH_Fail;
138 return DESCRIPTOR_SEARCH_NotFound;
141 void MIDI_Host_USBTask(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo)
143 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
146 #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
147 MIDI_Host_Flush(MIDIInterfaceInfo);
151 uint8_t MIDI_Host_Flush(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo)
153 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
154 return PIPE_RWSTREAM_DeviceDisconnected;
158 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataOUTPipe.Address);
161 if (Pipe_BytesInPipe())
165 if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
174 return PIPE_READYWAIT_NoError;
177 uint8_t MIDI_Host_SendEventPacket(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
178 MIDI_EventPacket_t* const Event)
180 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
181 return HOST_SENDCONTROL_DeviceDisconnected;
185 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataOUTPipe.Address);
188 if ((ErrorCode = Pipe_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL)) != PIPE_RWSTREAM_NoError)
194 if (!(Pipe_IsReadWriteAllowed()))
199 return PIPE_RWSTREAM_NoError;
202 bool MIDI_Host_ReceiveEventPacket(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
203 MIDI_EventPacket_t* const Event)
205 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
206 return HOST_SENDCONTROL_DeviceDisconnected;
208 bool DataReady = false;
210 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataINPipe.Address);
213 if (Pipe_IsINReceived())
215 if (Pipe_BytesInPipe())
217 Pipe_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL);
221 if (!(Pipe_BytesInPipe()))