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 (void)AOAInterfaceInfo
;
46 if (DeviceDescriptor
->Header
.Type
!= DTYPE_Device
)
49 *NeedModeSwitch
= ((DeviceDescriptor
->ProductID
!= ANDROID_ACCESSORY_PRODUCT_ID
) &&
50 (DeviceDescriptor
->ProductID
!= ANDROID_ACCESSORY_ADB_PRODUCT_ID
));
55 uint8_t AOA_Host_ConfigurePipes(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
,
56 uint16_t ConfigDescriptorSize
,
57 void* ConfigDescriptorData
)
59 USB_Descriptor_Endpoint_t
* DataINEndpoint
= NULL
;
60 USB_Descriptor_Endpoint_t
* DataOUTEndpoint
= NULL
;
61 USB_Descriptor_Interface_t
* AOAInterface
= NULL
;
63 memset(&AOAInterfaceInfo
->State
, 0x00, sizeof(AOAInterfaceInfo
->State
));
65 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
66 return AOA_ENUMERROR_InvalidConfigDescriptor
;
68 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
69 DCOMP_AOA_Host_NextAndroidAccessoryInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
71 return AOA_ENUMERROR_NoCompatibleInterfaceFound
;
74 AOAInterface
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
);
76 while (!(DataINEndpoint
) || !(DataOUTEndpoint
))
78 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
79 DCOMP_AOA_Host_NextInterfaceBulkEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
81 return AOA_ENUMERROR_NoCompatibleInterfaceFound
;
84 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
86 if ((EndpointData
->EndpointAddress
& ENDPOINT_DIR_MASK
) == ENDPOINT_DIR_IN
)
87 DataINEndpoint
= EndpointData
;
89 DataOUTEndpoint
= EndpointData
;
92 for (uint8_t PipeNum
= 1; PipeNum
< PIPE_TOTAL_PIPES
; PipeNum
++)
97 uint8_t EndpointAddress
;
100 if (PipeNum
== AOAInterfaceInfo
->Config
.DataINPipeNumber
)
102 Size
= le16_to_cpu(DataINEndpoint
->EndpointSize
);
103 EndpointAddress
= DataINEndpoint
->EndpointAddress
;
104 Token
= PIPE_TOKEN_IN
;
106 DoubleBanked
= AOAInterfaceInfo
->Config
.DataINPipeDoubleBank
;
108 AOAInterfaceInfo
->State
.DataINPipeSize
= DataINEndpoint
->EndpointSize
;
110 else if (PipeNum
== AOAInterfaceInfo
->Config
.DataOUTPipeNumber
)
112 Size
= le16_to_cpu(DataOUTEndpoint
->EndpointSize
);
113 EndpointAddress
= DataOUTEndpoint
->EndpointAddress
;
114 Token
= PIPE_TOKEN_OUT
;
116 DoubleBanked
= AOAInterfaceInfo
->Config
.DataOUTPipeDoubleBank
;
118 AOAInterfaceInfo
->State
.DataOUTPipeSize
= DataOUTEndpoint
->EndpointSize
;
125 if (!(Pipe_ConfigurePipe(PipeNum
, Type
, Token
, EndpointAddress
, Size
,
126 DoubleBanked ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
)))
128 return AOA_ENUMERROR_PipeConfigurationFailed
;
132 AOAInterfaceInfo
->State
.IsActive
= true;
133 AOAInterfaceInfo
->State
.InterfaceNumber
= AOAInterface
->InterfaceNumber
;
135 return AOA_ENUMERROR_NoError
;
138 static uint8_t DCOMP_AOA_Host_NextAndroidAccessoryInterface(void* const CurrentDescriptor
)
140 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
142 if (Header
->Type
== DTYPE_Interface
)
144 USB_Descriptor_Interface_t
* Interface
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
);
146 if ((Interface
->Class
== AOA_CSCP_AOADataClass
) &&
147 (Interface
->SubClass
== AOA_CSCP_AOADataSubclass
) &&
148 (Interface
->Protocol
== AOA_CSCP_AOADataProtocol
))
150 return DESCRIPTOR_SEARCH_Found
;
154 return DESCRIPTOR_SEARCH_NotFound
;
157 static uint8_t DCOMP_AOA_Host_NextInterfaceBulkEndpoint(void* const CurrentDescriptor
)
159 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
161 if (Header
->Type
== DTYPE_Endpoint
)
163 USB_Descriptor_Endpoint_t
* Endpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
);
165 uint8_t EndpointType
= (Endpoint
->Attributes
& EP_TYPE_MASK
);
167 if ((EndpointType
== EP_TYPE_BULK
) && (!(Pipe_IsEndpointBound(Endpoint
->EndpointAddress
))))
168 return DESCRIPTOR_SEARCH_Found
;
170 else if (Header
->Type
== DTYPE_Interface
)
172 return DESCRIPTOR_SEARCH_Fail
;
175 return DESCRIPTOR_SEARCH_NotFound
;
178 void AOA_Host_USBTask(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
)
180 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AOAInterfaceInfo
->State
.IsActive
))
183 #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
184 AOA_Host_Flush(AOAInterfaceInfo
);
188 uint8_t AOA_Host_StartAccessoryMode(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
)
192 uint16_t AccessoryProtocol
;
193 if ((ErrorCode
= AOA_Host_GetAccessoryProtocol(&AccessoryProtocol
)) != HOST_WAITERROR_Successful
)
196 if (AccessoryProtocol
!= CPU_TO_LE16(AOA_PROTOCOL_AccessoryV1
))
197 return AOA_ERROR_LOGICAL_CMD_FAILED
;
199 for (uint8_t PropertyIndex
= 0; PropertyIndex
< AOA_STRING_TOTAL_STRINGS
; PropertyIndex
++)
201 if ((ErrorCode
= AOA_Host_SendPropertyString(AOAInterfaceInfo
, PropertyIndex
)) != HOST_WAITERROR_Successful
)
205 USB_ControlRequest
= (USB_Request_Header_t
)
207 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_VENDOR
| REQREC_DEVICE
),
208 .bRequest
= AOA_REQ_StartAccessoryMode
,
214 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
215 return USB_Host_SendControlRequest(NULL
);
218 static uint8_t AOA_Host_GetAccessoryProtocol(uint16_t* const Protocol
)
220 USB_ControlRequest
= (USB_Request_Header_t
)
222 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_VENDOR
| REQREC_DEVICE
),
223 .bRequest
= AOA_REQ_GetAccessoryProtocol
,
226 .wLength
= sizeof(uint16_t),
229 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
230 return USB_Host_SendControlRequest(Protocol
);
233 static uint8_t AOA_Host_SendPropertyString(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
,
234 const uint8_t StringIndex
)
236 const char* String
= ((char**)&AOAInterfaceInfo
->Config
.PropertyStrings
)[StringIndex
];
241 USB_ControlRequest
= (USB_Request_Header_t
)
243 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_VENDOR
| REQREC_DEVICE
),
244 .bRequest
= AOA_REQ_SendString
,
246 .wIndex
= StringIndex
,
247 .wLength
= (strlen(String
) + 1),
250 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
251 return USB_Host_SendControlRequest((char*)String
);
254 uint8_t AOA_Host_SendData(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
,
255 const uint8_t* const Buffer
,
256 const uint16_t Length
)
258 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AOAInterfaceInfo
->State
.IsActive
))
259 return PIPE_READYWAIT_DeviceDisconnected
;
263 Pipe_SelectPipe(AOAInterfaceInfo
->Config
.DataOUTPipeNumber
);
266 ErrorCode
= Pipe_Write_Stream_LE(Buffer
, Length
, NULL
);
272 uint8_t AOA_Host_SendString(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
,
273 const char* const String
)
275 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AOAInterfaceInfo
->State
.IsActive
))
276 return PIPE_READYWAIT_DeviceDisconnected
;
280 Pipe_SelectPipe(AOAInterfaceInfo
->Config
.DataOUTPipeNumber
);
283 ErrorCode
= Pipe_Write_Stream_LE(String
, strlen(String
), NULL
);
289 uint8_t AOA_Host_SendByte(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
,
292 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AOAInterfaceInfo
->State
.IsActive
))
293 return PIPE_READYWAIT_DeviceDisconnected
;
297 Pipe_SelectPipe(AOAInterfaceInfo
->Config
.DataOUTPipeNumber
);
300 if (!(Pipe_IsReadWriteAllowed()))
304 if ((ErrorCode
= Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError
)
311 return PIPE_READYWAIT_NoError
;
314 uint16_t AOA_Host_BytesReceived(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
)
316 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AOAInterfaceInfo
->State
.IsActive
))
319 Pipe_SelectPipe(AOAInterfaceInfo
->Config
.DataINPipeNumber
);
322 if (Pipe_IsINReceived())
324 if (!(Pipe_BytesInPipe()))
333 return Pipe_BytesInPipe();
344 int16_t AOA_Host_ReceiveByte(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
)
346 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AOAInterfaceInfo
->State
.IsActive
))
349 int16_t ReceivedByte
= -1;
351 Pipe_SelectPipe(AOAInterfaceInfo
->Config
.DataINPipeNumber
);
354 if (Pipe_IsINReceived())
356 if (Pipe_BytesInPipe())
357 ReceivedByte
= Pipe_Read_8();
359 if (!(Pipe_BytesInPipe()))
368 uint8_t AOA_Host_Flush(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
)
370 if ((USB_HostState
!= HOST_STATE_Configured
) || !(AOAInterfaceInfo
->State
.IsActive
))
371 return PIPE_READYWAIT_DeviceDisconnected
;
375 Pipe_SelectPipe(AOAInterfaceInfo
->Config
.DataOUTPipeNumber
);
378 if (!(Pipe_BytesInPipe()))
379 return PIPE_READYWAIT_NoError
;
381 bool BankFull
= !(Pipe_IsReadWriteAllowed());
387 if ((ErrorCode
= Pipe_WaitUntilReady()) != PIPE_READYWAIT_NoError
)
395 return PIPE_READYWAIT_NoError
;
398 #if defined(FDEV_SETUP_STREAM)
399 void AOA_Host_CreateStream(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
,
402 *Stream
= (FILE)FDEV_SETUP_STREAM(AOA_Host_putchar
, AOA_Host_getchar
, _FDEV_SETUP_RW
);
403 fdev_set_udata(Stream
, AOAInterfaceInfo
);
406 void AOA_Host_CreateBlockingStream(USB_ClassInfo_AOA_Host_t
* const AOAInterfaceInfo
,
409 *Stream
= (FILE)FDEV_SETUP_STREAM(AOA_Host_putchar
, AOA_Host_getchar_Blocking
, _FDEV_SETUP_RW
);
410 fdev_set_udata(Stream
, AOAInterfaceInfo
);
413 static int AOA_Host_putchar(char c
,
416 return AOA_Host_SendByte((USB_ClassInfo_AOA_Host_t
*)fdev_get_udata(Stream
), c
) ? _FDEV_ERR
: 0;
419 static int AOA_Host_getchar(FILE* Stream
)
421 int16_t ReceivedByte
= AOA_Host_ReceiveByte((USB_ClassInfo_AOA_Host_t
*)fdev_get_udata(Stream
));
423 if (ReceivedByte
< 0)
429 static int AOA_Host_getchar_Blocking(FILE* Stream
)
431 int16_t ReceivedByte
;
433 while ((ReceivedByte
= AOA_Host_ReceiveByte((USB_ClassInfo_AOA_Host_t
*)fdev_get_udata(Stream
))) < 0)
435 if (USB_HostState
== HOST_STATE_Unattached
)
438 AOA_Host_USBTask((USB_ClassInfo_AOA_Host_t
*)fdev_get_udata(Stream
));