3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 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 "../../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_HOST)
35 #define __INCLUDE_FROM_HID_CLASS_HOST_C
36 #define __INCLUDE_FROM_HID_DRIVER
39 uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
40 uint16_t ConfigDescriptorSize
,
41 void* ConfigDescriptorData
)
43 USB_Descriptor_Endpoint_t
* DataINEndpoint
= NULL
;
44 USB_Descriptor_Endpoint_t
* DataOUTEndpoint
= NULL
;
45 USB_Descriptor_Interface_t
* HIDInterface
= NULL
;
46 USB_HID_Descriptor_HID_t
* HIDDescriptor
= NULL
;
48 memset(&HIDInterfaceInfo
->State
, 0x00, sizeof(HIDInterfaceInfo
->State
));
50 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
51 return HID_ENUMERROR_InvalidConfigDescriptor
;
53 while (!(DataINEndpoint
) || !(DataOUTEndpoint
))
55 if (!(HIDInterface
) ||
56 USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
57 DCOMP_HID_Host_NextHIDInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
59 if (DataINEndpoint
|| DataOUTEndpoint
)
64 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
65 DCOMP_HID_Host_NextHIDInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
67 return HID_ENUMERROR_NoCompatibleInterfaceFound
;
70 HIDInterface
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
);
71 } while (HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
&&
72 (HIDInterface
->Protocol
!= HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
));
74 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
75 DCOMP_HID_Host_NextHID
) != DESCRIPTOR_SEARCH_COMP_Found
)
77 return HID_ENUMERROR_NoCompatibleInterfaceFound
;
80 HIDDescriptor
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_HID_Descriptor_HID_t
);
82 DataINEndpoint
= NULL
;
83 DataOUTEndpoint
= NULL
;
88 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
90 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
91 DataINEndpoint
= EndpointData
;
93 DataOUTEndpoint
= EndpointData
;
96 for (uint8_t PipeNum
= 1; PipeNum
< PIPE_TOTAL_PIPES
; PipeNum
++)
98 if (PipeNum
== HIDInterfaceInfo
->Config
.DataINPipeNumber
)
100 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
101 DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
,
102 HIDInterfaceInfo
->Config
.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
103 Pipe_SetInterruptPeriod(DataINEndpoint
->PollingIntervalMS
);
105 HIDInterfaceInfo
->State
.DataINPipeSize
= DataINEndpoint
->EndpointSize
;
107 else if (PipeNum
== HIDInterfaceInfo
->Config
.DataOUTPipeNumber
)
109 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_OUT
,
110 DataOUTEndpoint
->EndpointAddress
, DataOUTEndpoint
->EndpointSize
,
111 HIDInterfaceInfo
->Config
.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
112 Pipe_SetInterruptPeriod(DataOUTEndpoint
->PollingIntervalMS
);
114 HIDInterfaceInfo
->State
.DataOUTPipeSize
= DataOUTEndpoint
->EndpointSize
;
115 HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
= true;
119 HIDInterfaceInfo
->State
.InterfaceNumber
= HIDInterface
->InterfaceNumber
;
120 HIDInterfaceInfo
->State
.HIDReportSize
= HIDDescriptor
->HIDReportLength
;
121 HIDInterfaceInfo
->State
.SupportsBootProtocol
= (HIDInterface
->SubClass
!= HID_BOOTP_NonBootProtocol
);
122 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
123 HIDInterfaceInfo
->State
.IsActive
= true;
125 return HID_ENUMERROR_NoError
;
128 static uint8_t DCOMP_HID_Host_NextHIDInterface(void* const CurrentDescriptor
)
130 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
132 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
133 USB_Descriptor_Interface_t
);
135 if (CurrentInterface
->Class
== HID_INTERFACE_CLASS
)
136 return DESCRIPTOR_SEARCH_Found
;
139 return DESCRIPTOR_SEARCH_NotFound
;
142 static uint8_t DCOMP_HID_Host_NextHID(void* const CurrentDescriptor
)
144 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == HID_DTYPE_HID
)
145 return DESCRIPTOR_SEARCH_Found
;
146 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
147 return DESCRIPTOR_SEARCH_Fail
;
149 return DESCRIPTOR_SEARCH_NotFound
;
152 static uint8_t DCOMP_HID_Host_NextHIDInterfaceEndpoint(void* const CurrentDescriptor
)
154 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
156 USB_Descriptor_Endpoint_t
* CurrentEndpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
,
157 USB_Descriptor_Endpoint_t
);
159 if (!(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
)))
160 return DESCRIPTOR_SEARCH_Found
;
162 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
164 return DESCRIPTOR_SEARCH_Fail
;
167 return DESCRIPTOR_SEARCH_NotFound
;
170 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
171 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
172 const uint8_t ReportID
,
175 USB_ControlRequest
= (USB_Request_Header_t
)
177 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
178 .bRequest
= HID_REQ_SetReport
,
179 .wValue
= ((HID_REPORT_ITEM_In
+ 1) << 8) | ReportID
,
180 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
181 .wLength
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, HID_REPORT_ITEM_In
),
184 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
186 return USB_Host_SendControlRequest(Buffer
);
190 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
193 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
194 return PIPE_READYWAIT_DeviceDisconnected
;
198 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
202 uint8_t* BufferPos
= Buffer
;
204 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
205 if (!(HIDInterfaceInfo
->State
.UsingBootProtocol
))
207 uint8_t ReportID
= 0;
209 if (HIDInterfaceInfo
->Config
.HIDParserData
->UsingReportIDs
)
211 ReportID
= Pipe_Read_Byte();
212 *(BufferPos
++) = ReportID
;
215 ReportSize
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, HID_REPORT_ITEM_In
);
220 ReportSize
= Pipe_BytesInPipe();
223 if ((ErrorCode
= Pipe_Read_Stream_LE(BufferPos
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
229 return PIPE_RWSTREAM_NoError
;
232 uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
233 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
234 const uint8_t ReportID
,
236 const uint8_t ReportType
,
238 const uint16_t ReportSize
)
240 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
241 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
244 if (HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
&& (ReportType
== HID_REPORT_ITEM_Out
))
248 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataOUTPipeNumber
);
252 Pipe_Write_Stream_LE(&ReportID
, sizeof(ReportID
), NO_STREAM_CALLBACK
);
254 if ((ErrorCode
= Pipe_Write_Stream_LE(Buffer
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
260 return PIPE_RWSTREAM_NoError
;
265 USB_ControlRequest
= (USB_Request_Header_t
)
267 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
268 .bRequest
= HID_REQ_SetReport
,
269 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
270 .wValue
= ((ReportType
+ 1) << 8) | ReportID
,
272 .wValue
= ((ReportType
+ 1) << 8),
274 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
275 .wLength
= ReportSize
,
278 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
280 return USB_Host_SendControlRequest(Buffer
);
284 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
286 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
291 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
294 ReportReceived
= Pipe_IsINReceived();
298 return ReportReceived
;
301 uint8_t HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
305 USB_ControlRequest
= (USB_Request_Header_t
)
307 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
308 .bRequest
= HID_REQ_SetProtocol
,
310 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
314 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
316 if (!(HIDInterfaceInfo
->State
.SupportsBootProtocol
))
317 return HID_ERROR_LOGICAL
;
319 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
322 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
323 HIDInterfaceInfo
->State
.UsingBootProtocol
= true;
325 return HOST_SENDCONTROL_Successful
;
328 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
329 uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
333 uint8_t HIDReportData
[HIDInterfaceInfo
->State
.HIDReportSize
];
335 USB_ControlRequest
= (USB_Request_Header_t
)
337 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_INTERFACE
),
338 .bRequest
= REQ_GetDescriptor
,
339 .wValue
= (HID_DTYPE_Report
<< 8),
340 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
341 .wLength
= HIDInterfaceInfo
->State
.HIDReportSize
,
344 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
346 if ((ErrorCode
= USB_Host_SendControlRequest(HIDReportData
)) != HOST_SENDCONTROL_Successful
)
349 if (HIDInterfaceInfo
->State
.UsingBootProtocol
)
351 USB_ControlRequest
= (USB_Request_Header_t
)
353 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
354 .bRequest
= HID_REQ_SetProtocol
,
356 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
360 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
363 HIDInterfaceInfo
->State
.UsingBootProtocol
= false;
366 if (HIDInterfaceInfo
->Config
.HIDParserData
== NULL
)
367 return HID_ERROR_LOGICAL
;
369 if ((ErrorCode
= USB_ProcessHIDReport(HIDReportData
, HIDInterfaceInfo
->State
.HIDReportSize
,
370 HIDInterfaceInfo
->Config
.HIDParserData
)) != HID_PARSE_Successful
)
372 return HID_ERROR_LOGICAL
| ErrorCode
;
375 uint8_t LargestReportSizeBits
= HIDInterfaceInfo
->Config
.HIDParserData
->LargestReportSizeBits
;
376 HIDInterfaceInfo
->State
.LargestReportSize
= (LargestReportSizeBits
>> 3) + ((LargestReportSizeBits
& 0x07) != 0);