3 Copyright (C) Dean Camera, 2012.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2012 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_AOA_DRIVER
37 #define __INCLUDE_FROM_ANDROIDACCESSORY_HOST_C
38 #include "AndroidAccessoryClassHost.h"
40 bool AOA_Host_ValidateAccessoryDevice(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
,
41 const USB_Descriptor_Device_t
* const DeviceDescriptor
,
42 bool* const NeedModeSwitch
)
44 if (DeviceDescriptor
->Header
.Type
!= DTYPE_Device
)
47 *NeedModeSwitch
= ((DeviceDescriptor
->ProductID
!= ANDROID_ACCESSORY_PRODUCT_ID
) &&
48 (DeviceDescriptor
->ProductID
!= ANDROID_ACCESSORY_ADB_PRODUCT_ID
));
53 uint8_t AOA_Host_ConfigurePipes(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
,
54 uint16_t ConfigDescriptorSize
,
55 void* ConfigDescriptorData
)
57 USB_Descriptor_Endpoint_t
* DataINEndpoint
= NULL
;
58 USB_Descriptor_Endpoint_t
* DataOUTEndpoint
= NULL
;
59 USB_Descriptor_Interface_t
* AOAInterface
= NULL
;
61 memset(&AOAInterfaceInfo
->State
, 0x00, sizeof(AOAInterfaceInfo
->State
));
63 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
64 return AOA_ENUMERROR_InvalidConfigDescriptor
;
66 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
67 DCOMP_AOA_Host_NextAndroidAccessoryInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
69 return AOA_ENUMERROR_NoCompatibleInterfaceFound
;
72 AOAInterface
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
);
74 while (!(DataINEndpoint
) || !(DataOUTEndpoint
))
76 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
77 DCOMP_AOA_Host_NextInterfaceBulkEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
79 return AOA_ENUMERROR_NoCompatibleInterfaceFound
;
82 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
84 if ((EndpointData
->EndpointAddress
& ENDPOINT_DIR_MASK
) == ENDPOINT_DIR_IN
)
85 DataINEndpoint
= EndpointData
;
87 DataOUTEndpoint
= EndpointData
;
90 for (uint8_t PipeNum
= 1; PipeNum
< PIPE_TOTAL_PIPES
; PipeNum
++)
95 uint8_t EndpointAddress
;
98 if (PipeNum
== AOAInterfaceInfo
->Config
.DataINPipeNumber
)
100 Size
= le16_to_cpu(DataINEndpoint
->EndpointSize
);
101 EndpointAddress
= DataINEndpoint
->EndpointAddress
;
102 Token
= PIPE_TOKEN_IN
;
104 DoubleBanked
= AOAInterfaceInfo
->Config
.DataINPipeDoubleBank
;
106 AOAInterfaceInfo
->State
.DataINPipeSize
= DataINEndpoint
->EndpointSize
;
108 else if (PipeNum
== AOAInterfaceInfo
->Config
.DataOUTPipeNumber
)
110 Size
= le16_to_cpu(DataOUTEndpoint
->EndpointSize
);
111 EndpointAddress
= DataOUTEndpoint
->EndpointAddress
;
112 Token
= PIPE_TOKEN_OUT
;
114 DoubleBanked
= AOAInterfaceInfo
->Config
.DataOUTPipeDoubleBank
;
116 AOAInterfaceInfo
->State
.DataOUTPipeSize
= DataOUTEndpoint
->EndpointSize
;
123 if (!(Pipe_ConfigurePipe(PipeNum
, Type
, Token
, EndpointAddress
, Size
,
124 DoubleBanked ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
)))
126 return AOA_ENUMERROR_PipeConfigurationFailed
;
130 AOAInterfaceInfo
->State
.IsActive
= true;
131 AOAInterfaceInfo
->State
.InterfaceNumber
= AOAInterface
->InterfaceNumber
;
133 return AOA_ENUMERROR_NoError
;
136 static uint8_t DCOMP_AOA_Host_NextAndroidAccessoryInterface(void* const CurrentDescriptor
)
138 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
140 if (Header
->Type
== DTYPE_Interface
)
142 USB_Descriptor_Interface_t
* Interface
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
);
144 if ((Interface
->Class
== AOA_CSCP_AOADataClass
) &&
145 (Interface
->SubClass
== AOA_CSCP_AOADataSubclass
) &&
146 (Interface
->Protocol
== AOA_CSCP_AOADataProtocol
))
148 return DESCRIPTOR_SEARCH_Found
;
152 return DESCRIPTOR_SEARCH_NotFound
;
155 static uint8_t DCOMP_AOA_Host_NextInterfaceBulkEndpoint(void* const CurrentDescriptor
)
157 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
159 if (Header
->Type
== DTYPE_Endpoint
)
161 USB_Descriptor_Endpoint_t
* Endpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
);
163 uint8_t EndpointType
= (Endpoint
->Attributes
& EP_TYPE_MASK
);
165 if ((EndpointType
== EP_TYPE_BULK
) && (!(Pipe_IsEndpointBound(Endpoint
->EndpointAddress
))))
166 return DESCRIPTOR_SEARCH_Found
;
168 else if (Header
->Type
== DTYPE_Interface
)
170 return DESCRIPTOR_SEARCH_Fail
;
173 return DESCRIPTOR_SEARCH_NotFound
;
176 void AOA_Host_USBTask(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
)
178 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AOAInterfaceInfo
->State
.IsActive
))
181 #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
182 AOA_Host_Flush(AOAInterfaceInfo
);
186 uint8_t AOA_Host_StartAccessoryMode(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
)
190 uint16_t AccessoryProtocol
;
191 if ((ErrorCode
= AOA_Host_GetAccessoryProtocol(&AccessoryProtocol
)) != HOST_WAITERROR_Successful
)
194 if (AccessoryProtocol
!= CPU_TO_LE16(AOA_PROTOCOL_AccessoryV1
))
195 return AOA_ERROR_LOGICAL_CMD_FAILED
;
197 for (uint8_t PropertyIndex
= 0; PropertyIndex
< AOA_STRING_TOTAL_STRINGS
; PropertyIndex
++)
199 if ((ErrorCode
= AOA_Host_SendPropertyString(AOAInterfaceInfo
, PropertyIndex
)) != HOST_WAITERROR_Successful
)
203 USB_ControlRequest
= (USB_Request_Header_t
)
205 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_VENDOR
| REQREC_DEVICE
),
206 .bRequest
= AOA_REQ_StartAccessoryMode
,
212 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
213 return USB_Host_SendControlRequest(NULL
);
216 static uint8_t AOA_Host_GetAccessoryProtocol(uint16_t* const Protocol
)
218 USB_ControlRequest
= (USB_Request_Header_t
)
220 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_VENDOR
| REQREC_DEVICE
),
221 .bRequest
= AOA_REQ_GetAccessoryProtocol
,
224 .wLength
= sizeof(uint16_t),
227 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
228 return USB_Host_SendControlRequest(Protocol
);
231 static uint8_t AOA_Host_SendPropertyString(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
,
232 const uint8_t StringIndex
)
234 const char* String
= ((char**)&AOAInterfaceInfo
->Config
.PropertyStrings
)[StringIndex
];
239 USB_ControlRequest
= (USB_Request_Header_t
)
241 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_VENDOR
| REQREC_DEVICE
),
242 .bRequest
= AOA_REQ_SendString
,
244 .wIndex
= StringIndex
,
245 .wLength
= (strlen(String
) + 1),
248 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
249 return USB_Host_SendControlRequest((char*)String
);
252 uint8_t AOA_Host_SendData(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
,
253 const uint8_t* const Buffer
,
254 const uint16_t Length
)
256 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AOAInterfaceInfo
->State
.IsActive
))
257 return PIPE_READYWAIT_DeviceDisconnected
;
261 Pipe_SelectPipe(AOAInterfaceInfo
->Config
.DataOUTPipeNumber
);
264 ErrorCode
= Pipe_Write_Stream_LE(Buffer
, Length
, NULL
);
270 uint8_t AOA_Host_SendString(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
,
271 const char* const String
)
273 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AOAInterfaceInfo
->State
.IsActive
))
274 return PIPE_READYWAIT_DeviceDisconnected
;
278 Pipe_SelectPipe(AOAInterfaceInfo
->Config
.DataOUTPipeNumber
);
281 ErrorCode
= Pipe_Write_Stream_LE(String
, strlen(String
), NULL
);
287 uint8_t AOA_Host_SendByte(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
,
290 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AOAInterfaceInfo
->State
.IsActive
))
291 return PIPE_READYWAIT_DeviceDisconnected
;
295 Pipe_SelectPipe(AOAInterfaceInfo
->Config
.DataOUTPipeNumber
);
298 if (!(Pipe_IsReadWriteAllowed()))
302 if ((ErrorCode
= Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError
)
309 return PIPE_READYWAIT_NoError
;
312 uint16_t AOA_Host_BytesReceived(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
)
314 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AOAInterfaceInfo
->State
.IsActive
))
317 Pipe_SelectPipe(AOAInterfaceInfo
->Config
.DataINPipeNumber
);
320 if (Pipe_IsINReceived())
322 if (!(Pipe_BytesInPipe()))
331 return Pipe_BytesInPipe();
342 int16_t AOA_Host_ReceiveByte(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
)
344 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AOAInterfaceInfo
->State
.IsActive
))
347 int16_t ReceivedByte
= -1;
349 Pipe_SelectPipe(AOAInterfaceInfo
->Config
.DataINPipeNumber
);
352 if (Pipe_IsINReceived())
354 if (Pipe_BytesInPipe())
355 ReceivedByte
= Pipe_Read_8();
357 if (!(Pipe_BytesInPipe()))
366 uint8_t AOA_Host_Flush(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
)
368 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AOAInterfaceInfo
->State
.IsActive
))
369 return PIPE_READYWAIT_DeviceDisconnected
;
373 Pipe_SelectPipe(AOAInterfaceInfo
->Config
.DataOUTPipeNumber
);
376 if (!(Pipe_BytesInPipe()))
377 return PIPE_READYWAIT_NoError
;
379 bool BankFull
= !(Pipe_IsReadWriteAllowed());
385 if ((ErrorCode
= Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError
)
393 return PIPE_READYWAIT_NoError
;
396 #if defined(FDEV_SETUP_STREAM)
397 void AOA_Host_CreateStream(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
,
400 *Stream
= (FILE)FDEV_SETUP_STREAM(AOA_Host_putchar
, AOA_Host_getchar
, _FDEV_SETUP_RW
);
401 fdev_set_udata(Stream
, AOAInterfaceInfo
);
404 void AOA_Host_CreateBlockingStream(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
,
407 *Stream
= (FILE)FDEV_SETUP_STREAM(AOA_Host_putchar
, AOA_Host_getchar_Blocking
, _FDEV_SETUP_RW
);
408 fdev_set_udata(Stream
, AOAInterfaceInfo
);
411 static int AOA_Host_putchar(char c
,
414 return AOA_Host_SendByte((USB_ClassInfo_AOA_Host_t
*)fdev_get_udata(Stream
), c
) ? _FDEV_ERR
: 0;
417 static int AOA_Host_getchar(FILE* Stream
)
419 int16_t ReceivedByte
= AOA_Host_ReceiveByte((USB_ClassInfo_AOA_Host_t
*)fdev_get_udata(Stream
));
421 if (ReceivedByte
< 0)
427 static int AOA_Host_getchar_Blocking(FILE* Stream
)
429 int16_t ReceivedByte
;
431 while ((ReceivedByte
= AOA_Host_ReceiveByte((USB_ClassInfo_AOA_Host_t
*)fdev_get_udata(Stream
))) < 0)
433 if (USB_HostState
== HOST_STATE_Unattached
)
436 AOA_Host_USBTask((USB_ClassInfo_AOA_Host_t
*)fdev_get_udata(Stream
));