3 Copyright (C) Dean Camera, 2012.
\r
5 dean [at] fourwalledcubicle [dot] com
\r
10 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
\r
12 Permission to use, copy, modify, distribute, and sell this
\r
13 software and its documentation for any purpose is hereby granted
\r
14 without fee, provided that the above copyright notice appear in
\r
15 all copies and that both that the copyright notice and this
\r
16 permission notice and warranty disclaimer appear in supporting
\r
17 documentation, and that the name of the author not be used in
\r
18 advertising or publicity pertaining to distribution of the
\r
19 software without specific, written prior permission.
\r
21 The author disclaim all warranties with regard to this
\r
22 software, including all implied warranties of merchantability
\r
23 and fitness. In no event shall the author be liable for any
\r
24 special, indirect or consequential damages or any damages
\r
25 whatsoever resulting from loss of use, data or profits, whether
\r
26 in an action of contract, negligence or other tortious action,
\r
27 arising out of or in connection with the use or performance of
\r
31 #define __INCLUDE_FROM_USB_DRIVER
\r
32 #include "../../Core/USBMode.h"
\r
34 #if defined(USB_CAN_BE_HOST)
\r
36 #define __INCLUDE_FROM_MIDI_DRIVER
\r
37 #define __INCLUDE_FROM_MIDI_HOST_C
\r
38 #include "MIDIClassHost.h"
\r
40 uint8_t MIDI_Host_ConfigurePipes(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
\r
41 uint16_t ConfigDescriptorSize,
\r
42 void* ConfigDescriptorData)
\r
44 USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
\r
45 USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
\r
46 USB_Descriptor_Interface_t* MIDIInterface = NULL;
\r
48 memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));
\r
50 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
\r
51 return MIDI_ENUMERROR_InvalidConfigDescriptor;
\r
53 while (!(DataINEndpoint) || !(DataOUTEndpoint))
\r
55 if (!(MIDIInterface) ||
\r
56 USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
\r
57 DCOMP_MIDI_Host_NextMIDIStreamingDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
\r
59 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
\r
60 DCOMP_MIDI_Host_NextMIDIStreamingInterface) != DESCRIPTOR_SEARCH_COMP_Found)
\r
62 return MIDI_ENUMERROR_NoCompatibleInterfaceFound;
\r
65 MIDIInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
\r
67 DataINEndpoint = NULL;
\r
68 DataOUTEndpoint = NULL;
\r
73 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
\r
75 if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
\r
76 DataINEndpoint = EndpointData;
\r
78 DataOUTEndpoint = EndpointData;
\r
81 MIDIInterfaceInfo->Config.DataINPipe.Size = le16_to_cpu(DataINEndpoint->EndpointSize);
\r
82 MIDIInterfaceInfo->Config.DataINPipe.EndpointAddress = DataINEndpoint->EndpointAddress;
\r
83 MIDIInterfaceInfo->Config.DataINPipe.Type = EP_TYPE_BULK;
\r
85 MIDIInterfaceInfo->Config.DataOUTPipe.Size = le16_to_cpu(DataOUTEndpoint->EndpointSize);
\r
86 MIDIInterfaceInfo->Config.DataOUTPipe.EndpointAddress = DataOUTEndpoint->EndpointAddress;
\r
87 MIDIInterfaceInfo->Config.DataOUTPipe.Type = EP_TYPE_BULK;
\r
89 if (!(Pipe_ConfigurePipeTable(&MIDIInterfaceInfo->Config.DataINPipe, 1)))
\r
92 if (!(Pipe_ConfigurePipeTable(&MIDIInterfaceInfo->Config.DataOUTPipe, 1)))
\r
95 MIDIInterfaceInfo->State.InterfaceNumber = MIDIInterface->InterfaceNumber;
\r
96 MIDIInterfaceInfo->State.IsActive = true;
\r
98 return MIDI_ENUMERROR_NoError;
\r
101 static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingInterface(void* const CurrentDescriptor)
\r
103 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
\r
105 if (Header->Type == DTYPE_Interface)
\r
107 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
\r
109 if ((Interface->Class == AUDIO_CSCP_AudioClass) &&
\r
110 (Interface->SubClass == AUDIO_CSCP_MIDIStreamingSubclass) &&
\r
111 (Interface->Protocol == AUDIO_CSCP_StreamingProtocol))
\r
113 return DESCRIPTOR_SEARCH_Found;
\r
117 return DESCRIPTOR_SEARCH_NotFound;
\r
120 static uint8_t DCOMP_MIDI_Host_NextMIDIStreamingDataEndpoint(void* const CurrentDescriptor)
\r
122 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
\r
124 if (Header->Type == DTYPE_Endpoint)
\r
126 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
\r
128 uint8_t EndpointType = (Endpoint->Attributes & EP_TYPE_MASK);
\r
130 if ((EndpointType == EP_TYPE_BULK) && !(Pipe_IsEndpointBound(Endpoint->EndpointAddress)))
\r
131 return DESCRIPTOR_SEARCH_Found;
\r
133 else if (Header->Type == DTYPE_Interface)
\r
135 return DESCRIPTOR_SEARCH_Fail;
\r
138 return DESCRIPTOR_SEARCH_NotFound;
\r
141 void MIDI_Host_USBTask(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo)
\r
143 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
\r
146 #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
\r
147 MIDI_Host_Flush(MIDIInterfaceInfo);
\r
151 uint8_t MIDI_Host_Flush(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo)
\r
153 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
\r
154 return PIPE_RWSTREAM_DeviceDisconnected;
\r
158 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataOUTPipe.Address);
\r
161 if (Pipe_BytesInPipe())
\r
165 if ((ErrorCode = Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError)
\r
174 return PIPE_READYWAIT_NoError;
\r
177 uint8_t MIDI_Host_SendEventPacket(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
\r
178 MIDI_EventPacket_t* const Event)
\r
180 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
\r
181 return HOST_SENDCONTROL_DeviceDisconnected;
\r
185 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataOUTPipe.Address);
\r
188 if ((ErrorCode = Pipe_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL)) != PIPE_RWSTREAM_NoError)
\r
194 if (!(Pipe_IsReadWriteAllowed()))
\r
199 return PIPE_RWSTREAM_NoError;
\r
202 bool MIDI_Host_ReceiveEventPacket(USB_ClassInfo_MIDI_Host_t* const MIDIInterfaceInfo,
\r
203 MIDI_EventPacket_t* const Event)
\r
205 if ((USB_HostState != HOST_STATE_Configured) || !(MIDIInterfaceInfo->State.IsActive))
\r
206 return HOST_SENDCONTROL_DeviceDisconnected;
\r
208 bool DataReady = false;
\r
210 Pipe_SelectPipe(MIDIInterfaceInfo->Config.DataINPipe.Address);
\r
213 if (Pipe_IsINReceived())
\r
215 if (Pipe_BytesInPipe())
\r
217 Pipe_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL);
\r
221 if (!(Pipe_BytesInPipe()))
\r