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_Interface_t
* CurrentHIDInterface
;
45 USB_Descriptor_Endpoint_t
* DataINEndpoint
= NULL
;
46 USB_Descriptor_Endpoint_t
* DataOUTEndpoint
= NULL
;
48 memset(&HIDInterfaceInfo
->State
, 0x00, sizeof(HIDInterfaceInfo
->State
));
50 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
51 return HID_ENUMERROR_InvalidConfigDescriptor
;
55 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
56 DCOMP_HID_Host_NextHIDInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
58 return HID_ENUMERROR_NoCompatibleInterfaceFound
;
61 CurrentHIDInterface
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
);
62 } while (HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
&&
63 (CurrentHIDInterface
->Protocol
!= HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
));
65 HIDInterfaceInfo
->State
.InterfaceNumber
= CurrentHIDInterface
->InterfaceNumber
;
66 HIDInterfaceInfo
->State
.SupportsBootProtocol
= (CurrentHIDInterface
->SubClass
!= HID_BOOTP_NonBootProtocol
);
68 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, DCOMP_HID_NextHID
) != DESCRIPTOR_SEARCH_COMP_Found
)
69 return HID_ENUMERROR_NoCompatibleInterfaceFound
;
71 HIDInterfaceInfo
->State
.HIDReportSize
= DESCRIPTOR_PCAST(ConfigDescriptorData
,
72 USB_HID_Descriptor_HID_t
)->HIDReportLength
;
74 while (!(DataINEndpoint
) || !(DataOUTEndpoint
))
76 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
77 DCOMP_HID_Host_NextHIDInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
79 if (DataINEndpoint
|| DataOUTEndpoint
)
84 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
85 DCOMP_HID_Host_NextHIDInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
87 return HID_ENUMERROR_NoCompatibleInterfaceFound
;
90 CurrentHIDInterface
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
);
91 } while (HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
&&
92 (CurrentHIDInterface
->Protocol
!= HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
));
94 HIDInterfaceInfo
->State
.InterfaceNumber
= CurrentHIDInterface
->InterfaceNumber
;
95 HIDInterfaceInfo
->State
.SupportsBootProtocol
= (CurrentHIDInterface
->SubClass
!= HID_BOOTP_NonBootProtocol
);
97 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, DCOMP_HID_NextHID
) != DESCRIPTOR_SEARCH_COMP_Found
)
98 return HID_ENUMERROR_NoCompatibleInterfaceFound
;
100 HIDInterfaceInfo
->State
.HIDReportSize
= DESCRIPTOR_PCAST(ConfigDescriptorData
,
101 USB_HID_Descriptor_HID_t
)->HIDReportLength
;
103 DataINEndpoint
= NULL
;
104 DataOUTEndpoint
= NULL
;
109 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
111 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
112 DataINEndpoint
= EndpointData
;
114 DataOUTEndpoint
= EndpointData
;
117 for (uint8_t PipeNum
= 1; PipeNum
< PIPE_TOTAL_PIPES
; PipeNum
++)
119 if (PipeNum
== HIDInterfaceInfo
->Config
.DataINPipeNumber
)
121 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
122 DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
,
123 HIDInterfaceInfo
->Config
.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
124 Pipe_SetInterruptPeriod(DataINEndpoint
->PollingIntervalMS
);
126 HIDInterfaceInfo
->State
.DataINPipeSize
= DataINEndpoint
->EndpointSize
;
128 else if (PipeNum
== HIDInterfaceInfo
->Config
.DataOUTPipeNumber
)
130 Pipe_ConfigurePipe(PipeNum
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_OUT
,
131 DataOUTEndpoint
->EndpointAddress
, DataOUTEndpoint
->EndpointSize
,
132 HIDInterfaceInfo
->Config
.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
133 Pipe_SetInterruptPeriod(DataOUTEndpoint
->PollingIntervalMS
);
135 HIDInterfaceInfo
->State
.DataOUTPipeSize
= DataOUTEndpoint
->EndpointSize
;
136 HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
= true;
140 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
141 HIDInterfaceInfo
->State
.IsActive
= true;
143 return HID_ENUMERROR_NoError
;
146 static uint8_t DCOMP_HID_Host_NextHIDInterface(void* const CurrentDescriptor
)
148 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
150 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
151 USB_Descriptor_Interface_t
);
153 if (CurrentInterface
->Class
== HID_INTERFACE_CLASS
)
154 return DESCRIPTOR_SEARCH_Found
;
157 return DESCRIPTOR_SEARCH_NotFound
;
160 static uint8_t DCOMP_HID_NextHID(void* const CurrentDescriptor
)
162 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == HID_DTYPE_HID
)
163 return DESCRIPTOR_SEARCH_Found
;
164 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
165 return DESCRIPTOR_SEARCH_Fail
;
167 return DESCRIPTOR_SEARCH_NotFound
;
170 static uint8_t DCOMP_HID_Host_NextHIDInterfaceEndpoint(void* const CurrentDescriptor
)
172 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
174 USB_Descriptor_Endpoint_t
* CurrentEndpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
,
175 USB_Descriptor_Endpoint_t
);
177 if (!(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
)))
178 return DESCRIPTOR_SEARCH_Found
;
180 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
182 return DESCRIPTOR_SEARCH_Fail
;
185 return DESCRIPTOR_SEARCH_NotFound
;
188 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
189 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
190 const uint8_t ReportID
,
193 USB_ControlRequest
= (USB_Request_Header_t
)
195 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
196 .bRequest
= HID_REQ_SetReport
,
197 .wValue
= ((HID_REPORT_ITEM_In
+ 1) << 8) | ReportID
,
198 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
199 .wLength
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, HID_REPORT_ITEM_In
),
202 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
204 return USB_Host_SendControlRequest(Buffer
);
208 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
211 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
212 return PIPE_READYWAIT_DeviceDisconnected
;
216 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
220 uint8_t* BufferPos
= Buffer
;
222 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
223 if (!(HIDInterfaceInfo
->State
.UsingBootProtocol
))
225 uint8_t ReportID
= 0;
227 if (HIDInterfaceInfo
->Config
.HIDParserData
->UsingReportIDs
)
229 ReportID
= Pipe_Read_Byte();
230 *(BufferPos
++) = ReportID
;
233 ReportSize
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, HID_REPORT_ITEM_In
);
238 ReportSize
= Pipe_BytesInPipe();
241 if ((ErrorCode
= Pipe_Read_Stream_LE(BufferPos
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
247 return PIPE_RWSTREAM_NoError
;
250 uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
251 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
252 const uint8_t ReportID
,
254 const uint8_t ReportType
,
256 const uint16_t ReportSize
)
258 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
259 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
262 if (HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
&& (ReportType
== HID_REPORT_ITEM_Out
))
266 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataOUTPipeNumber
);
270 Pipe_Write_Stream_LE(&ReportID
, sizeof(ReportID
), NO_STREAM_CALLBACK
);
272 if ((ErrorCode
= Pipe_Write_Stream_LE(Buffer
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
278 return PIPE_RWSTREAM_NoError
;
283 USB_ControlRequest
= (USB_Request_Header_t
)
285 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
286 .bRequest
= HID_REQ_SetReport
,
287 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
288 .wValue
= ((ReportType
+ 1) << 8) | ReportID
,
290 .wValue
= ((ReportType
+ 1) << 8),
292 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
293 .wLength
= ReportSize
,
296 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
298 return USB_Host_SendControlRequest(Buffer
);
302 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
304 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
309 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
312 ReportReceived
= Pipe_IsINReceived();
316 return ReportReceived
;
319 uint8_t HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
323 USB_ControlRequest
= (USB_Request_Header_t
)
325 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
326 .bRequest
= HID_REQ_SetProtocol
,
328 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
332 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
334 if (!(HIDInterfaceInfo
->State
.SupportsBootProtocol
))
335 return HID_ERROR_LOGICAL
;
337 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
340 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
341 HIDInterfaceInfo
->State
.UsingBootProtocol
= true;
343 return HOST_SENDCONTROL_Successful
;
346 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
347 uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
351 uint8_t HIDReportData
[HIDInterfaceInfo
->State
.HIDReportSize
];
353 USB_ControlRequest
= (USB_Request_Header_t
)
355 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_INTERFACE
),
356 .bRequest
= REQ_GetDescriptor
,
357 .wValue
= (HID_DTYPE_Report
<< 8),
358 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
359 .wLength
= HIDInterfaceInfo
->State
.HIDReportSize
,
362 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
364 if ((ErrorCode
= USB_Host_SendControlRequest(HIDReportData
)) != HOST_SENDCONTROL_Successful
)
367 if (HIDInterfaceInfo
->State
.UsingBootProtocol
)
369 USB_ControlRequest
= (USB_Request_Header_t
)
371 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
372 .bRequest
= HID_REQ_SetProtocol
,
374 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
378 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
381 HIDInterfaceInfo
->State
.UsingBootProtocol
= false;
384 if (HIDInterfaceInfo
->Config
.HIDParserData
== NULL
)
385 return HID_ERROR_LOGICAL
;
387 if ((ErrorCode
= USB_ProcessHIDReport(HIDReportData
, HIDInterfaceInfo
->State
.HIDReportSize
,
388 HIDInterfaceInfo
->Config
.HIDParserData
)) != HID_PARSE_Successful
)
390 return HID_ERROR_LOGICAL
| ErrorCode
;
393 uint8_t LargestReportSizeBits
= HIDInterfaceInfo
->Config
.HIDParserData
->LargestReportSizeBits
;
394 HIDInterfaceInfo
->State
.LargestReportSize
= (LargestReportSizeBits
>> 3) + ((LargestReportSizeBits
& 0x07) != 0);