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 uint8_t FoundEndpoints
= 0;
45 memset(&HIDInterfaceInfo
->State
, 0x00, sizeof(HIDInterfaceInfo
->State
));
47 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
48 return HID_ENUMERROR_InvalidConfigDescriptor
;
50 USB_Descriptor_Interface_t
* CurrentHIDInterface
;
54 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
55 DCOMP_HID_Host_NextHIDInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
57 return HID_ENUMERROR_NoHIDInterfaceFound
;
60 CurrentHIDInterface
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
);
61 } while (HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
&&
62 (CurrentHIDInterface
->Protocol
!= HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
));
64 HIDInterfaceInfo
->State
.InterfaceNumber
= CurrentHIDInterface
->InterfaceNumber
;
65 HIDInterfaceInfo
->State
.SupportsBootProtocol
= (CurrentHIDInterface
->SubClass
!= HID_NON_BOOT_PROTOCOL
);
67 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, DCOMP_HID_NextHID
) != DESCRIPTOR_SEARCH_COMP_Found
)
69 return HID_ENUMERROR_NoHIDDescriptorFound
;
72 HIDInterfaceInfo
->State
.HIDReportSize
= DESCRIPTOR_CAST(ConfigDescriptorData
, USB_HID_Descriptor_t
).HIDReportLength
;
74 while (FoundEndpoints
!= (HID_FOUND_DATAPIPE_IN
| HID_FOUND_DATAPIPE_OUT
))
76 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
77 DCOMP_HID_Host_NextHIDInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
79 if (FoundEndpoints
& HID_FOUND_DATAPIPE_IN
)
82 return HID_ENUMERROR_EndpointsNotFound
;
85 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
87 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
89 Pipe_ConfigurePipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
90 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
91 HIDInterfaceInfo
->Config
.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
92 HIDInterfaceInfo
->State
.DataINPipeSize
= EndpointData
->EndpointSize
;
94 FoundEndpoints
|= HID_FOUND_DATAPIPE_IN
;
98 Pipe_ConfigurePipe(HIDInterfaceInfo
->Config
.DataOUTPipeNumber
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_OUT
,
99 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
100 HIDInterfaceInfo
->Config
.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
101 HIDInterfaceInfo
->State
.DataOUTPipeSize
= EndpointData
->EndpointSize
;
103 HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
= true;
105 FoundEndpoints
|= HID_FOUND_DATAPIPE_OUT
;
109 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
110 HIDInterfaceInfo
->State
.IsActive
= true;
111 return HID_ENUMERROR_NoError
;
114 static uint8_t DCOMP_HID_Host_NextHIDInterface(void* const CurrentDescriptor
)
116 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
118 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
119 USB_Descriptor_Interface_t
);
121 if (CurrentInterface
->Class
== HID_INTERFACE_CLASS
)
122 return DESCRIPTOR_SEARCH_Found
;
125 return DESCRIPTOR_SEARCH_NotFound
;
128 static uint8_t DCOMP_HID_NextHID(void* const CurrentDescriptor
)
130 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_HID
)
131 return DESCRIPTOR_SEARCH_Found
;
132 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
133 return DESCRIPTOR_SEARCH_Fail
;
135 return DESCRIPTOR_SEARCH_NotFound
;
138 static uint8_t DCOMP_HID_Host_NextHIDInterfaceEndpoint(void* const CurrentDescriptor
)
140 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
142 USB_Descriptor_Endpoint_t
* CurrentEndpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
,
143 USB_Descriptor_Endpoint_t
);
145 if (!(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
)))
146 return DESCRIPTOR_SEARCH_Found
;
148 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
150 return DESCRIPTOR_SEARCH_Fail
;
153 return DESCRIPTOR_SEARCH_NotFound
;
156 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
157 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
158 const uint8_t ReportID
,
161 USB_ControlRequest
= (USB_Request_Header_t
)
163 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
164 .bRequest
= REQ_SetReport
,
165 .wValue
= ((REPORT_ITEM_TYPE_In
+ 1) << 8) | ReportID
,
166 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
167 .wLength
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, REPORT_ITEM_TYPE_In
),
170 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
172 return USB_Host_SendControlRequest(Buffer
);
176 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
179 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
180 return PIPE_READYWAIT_DeviceDisconnected
;
184 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
188 uint8_t* BufferPos
= Buffer
;
190 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
191 if (!(HIDInterfaceInfo
->State
.UsingBootProtocol
))
193 uint8_t ReportID
= 0;
195 if (HIDInterfaceInfo
->Config
.HIDParserData
->UsingReportIDs
)
197 ReportID
= Pipe_Read_Byte();
198 *(BufferPos
++) = ReportID
;
201 ReportSize
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, REPORT_ITEM_TYPE_In
);
206 ReportSize
= Pipe_BytesInPipe();
209 if ((ErrorCode
= Pipe_Read_Stream_LE(BufferPos
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
215 return PIPE_RWSTREAM_NoError
;
218 uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
219 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
220 const uint8_t ReportID
,
222 const uint8_t ReportType
,
224 const uint16_t ReportSize
)
226 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
227 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
230 if (HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
&& (ReportType
== REPORT_ITEM_TYPE_Out
))
234 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataOUTPipeNumber
);
238 Pipe_Write_Stream_LE(&ReportID
, sizeof(ReportID
), NO_STREAM_CALLBACK
);
240 if ((ErrorCode
= Pipe_Write_Stream_LE(Buffer
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
246 return PIPE_RWSTREAM_NoError
;
251 USB_ControlRequest
= (USB_Request_Header_t
)
253 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
254 .bRequest
= REQ_SetReport
,
255 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
256 .wValue
= ((ReportType
+ 1) << 8) | ReportID
,
258 .wValue
= ((ReportType
+ 1) << 8),
260 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
261 .wLength
= ReportSize
,
264 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
266 return USB_Host_SendControlRequest(Buffer
);
270 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
272 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
277 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
280 ReportReceived
= Pipe_IsINReceived();
284 return ReportReceived
;
287 uint8_t HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
291 USB_ControlRequest
= (USB_Request_Header_t
)
293 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
294 .bRequest
= REQ_SetProtocol
,
296 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
300 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
302 if (!(HIDInterfaceInfo
->State
.SupportsBootProtocol
))
303 return HID_ERROR_LOGICAL
;
305 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
308 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
309 HIDInterfaceInfo
->State
.UsingBootProtocol
= true;
311 return HOST_SENDCONTROL_Successful
;
314 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
315 uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
319 uint8_t HIDReportData
[HIDInterfaceInfo
->State
.HIDReportSize
];
321 USB_ControlRequest
= (USB_Request_Header_t
)
323 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_INTERFACE
),
324 .bRequest
= REQ_GetDescriptor
,
325 .wValue
= (DTYPE_Report
<< 8),
326 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
327 .wLength
= HIDInterfaceInfo
->State
.HIDReportSize
,
330 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
332 if ((ErrorCode
= USB_Host_SendControlRequest(HIDReportData
)) != HOST_SENDCONTROL_Successful
)
335 if (HIDInterfaceInfo
->State
.UsingBootProtocol
)
337 USB_ControlRequest
= (USB_Request_Header_t
)
339 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
340 .bRequest
= REQ_SetProtocol
,
342 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
346 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
349 HIDInterfaceInfo
->State
.UsingBootProtocol
= false;
352 if (HIDInterfaceInfo
->Config
.HIDParserData
== NULL
)
353 return HID_ERROR_LOGICAL
;
355 if ((ErrorCode
= USB_ProcessHIDReport(HIDReportData
, HIDInterfaceInfo
->State
.HIDReportSize
,
356 HIDInterfaceInfo
->Config
.HIDParserData
)) != HID_PARSE_Successful
)
358 return HID_ERROR_LOGICAL
| ErrorCode
;
361 uint8_t LargestReportSizeBits
= HIDInterfaceInfo
->Config
.HIDParserData
->LargestReportSizeBits
;
362 HIDInterfaceInfo
->State
.LargestReportSize
= (LargestReportSizeBits
>> 3) + ((LargestReportSizeBits
& 0x07) != 0);