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_BOOTP_NonBootProtocol
);
67 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, DCOMP_HID_NextHID
) != DESCRIPTOR_SEARCH_COMP_Found
)
68 return HID_ENUMERROR_NoHIDDescriptorFound
;
70 HIDInterfaceInfo
->State
.HIDReportSize
= DESCRIPTOR_CAST(ConfigDescriptorData
, USB_HID_Descriptor_HID_t
).HIDReportLength
;
72 while (FoundEndpoints
!= (HID_FOUND_DATAPIPE_IN
| HID_FOUND_DATAPIPE_OUT
))
74 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
75 DCOMP_HID_Host_NextHIDInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
77 if (FoundEndpoints
& HID_FOUND_DATAPIPE_IN
)
80 return HID_ENUMERROR_EndpointsNotFound
;
83 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
85 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
87 Pipe_ConfigurePipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
88 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
89 HIDInterfaceInfo
->Config
.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
90 HIDInterfaceInfo
->State
.DataINPipeSize
= EndpointData
->EndpointSize
;
92 FoundEndpoints
|= HID_FOUND_DATAPIPE_IN
;
96 Pipe_ConfigurePipe(HIDInterfaceInfo
->Config
.DataOUTPipeNumber
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_OUT
,
97 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
98 HIDInterfaceInfo
->Config
.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
99 HIDInterfaceInfo
->State
.DataOUTPipeSize
= EndpointData
->EndpointSize
;
101 HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
= true;
103 FoundEndpoints
|= HID_FOUND_DATAPIPE_OUT
;
107 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
108 HIDInterfaceInfo
->State
.IsActive
= true;
109 return HID_ENUMERROR_NoError
;
112 static uint8_t DCOMP_HID_Host_NextHIDInterface(void* const CurrentDescriptor
)
114 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
116 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
117 USB_Descriptor_Interface_t
);
119 if (CurrentInterface
->Class
== HID_INTERFACE_CLASS
)
120 return DESCRIPTOR_SEARCH_Found
;
123 return DESCRIPTOR_SEARCH_NotFound
;
126 static uint8_t DCOMP_HID_NextHID(void* const CurrentDescriptor
)
128 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == HID_DTYPE_HID
)
129 return DESCRIPTOR_SEARCH_Found
;
130 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
131 return DESCRIPTOR_SEARCH_Fail
;
133 return DESCRIPTOR_SEARCH_NotFound
;
136 static uint8_t DCOMP_HID_Host_NextHIDInterfaceEndpoint(void* const CurrentDescriptor
)
138 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
140 USB_Descriptor_Endpoint_t
* CurrentEndpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
,
141 USB_Descriptor_Endpoint_t
);
143 if (!(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
)))
144 return DESCRIPTOR_SEARCH_Found
;
146 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
148 return DESCRIPTOR_SEARCH_Fail
;
151 return DESCRIPTOR_SEARCH_NotFound
;
154 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
155 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
156 const uint8_t ReportID
,
159 USB_ControlRequest
= (USB_Request_Header_t
)
161 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
162 .bRequest
= HID_REQ_SetReport
,
163 .wValue
= ((HID_REPORT_ITEM_In
+ 1) << 8) | ReportID
,
164 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
165 .wLength
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, HID_REPORT_ITEM_In
),
168 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
170 return USB_Host_SendControlRequest(Buffer
);
174 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
177 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
178 return PIPE_READYWAIT_DeviceDisconnected
;
182 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
186 uint8_t* BufferPos
= Buffer
;
188 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
189 if (!(HIDInterfaceInfo
->State
.UsingBootProtocol
))
191 uint8_t ReportID
= 0;
193 if (HIDInterfaceInfo
->Config
.HIDParserData
->UsingReportIDs
)
195 ReportID
= Pipe_Read_Byte();
196 *(BufferPos
++) = ReportID
;
199 ReportSize
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, HID_REPORT_ITEM_In
);
204 ReportSize
= Pipe_BytesInPipe();
207 if ((ErrorCode
= Pipe_Read_Stream_LE(BufferPos
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
213 return PIPE_RWSTREAM_NoError
;
216 uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
217 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
218 const uint8_t ReportID
,
220 const uint8_t ReportType
,
222 const uint16_t ReportSize
)
224 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
225 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
228 if (HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
&& (ReportType
== HID_REPORT_ITEM_Out
))
232 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataOUTPipeNumber
);
236 Pipe_Write_Stream_LE(&ReportID
, sizeof(ReportID
), NO_STREAM_CALLBACK
);
238 if ((ErrorCode
= Pipe_Write_Stream_LE(Buffer
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
244 return PIPE_RWSTREAM_NoError
;
249 USB_ControlRequest
= (USB_Request_Header_t
)
251 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
252 .bRequest
= HID_REQ_SetReport
,
253 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
254 .wValue
= ((ReportType
+ 1) << 8) | ReportID
,
256 .wValue
= ((ReportType
+ 1) << 8),
258 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
259 .wLength
= ReportSize
,
262 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
264 return USB_Host_SendControlRequest(Buffer
);
268 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
270 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
275 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
278 ReportReceived
= Pipe_IsINReceived();
282 return ReportReceived
;
285 uint8_t HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
289 USB_ControlRequest
= (USB_Request_Header_t
)
291 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
292 .bRequest
= HID_REQ_SetProtocol
,
294 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
298 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
300 if (!(HIDInterfaceInfo
->State
.SupportsBootProtocol
))
301 return HID_ERROR_LOGICAL
;
303 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
306 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
307 HIDInterfaceInfo
->State
.UsingBootProtocol
= true;
309 return HOST_SENDCONTROL_Successful
;
312 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
313 uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
317 uint8_t HIDReportData
[HIDInterfaceInfo
->State
.HIDReportSize
];
319 USB_ControlRequest
= (USB_Request_Header_t
)
321 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_INTERFACE
),
322 .bRequest
= REQ_GetDescriptor
,
323 .wValue
= (HID_DTYPE_Report
<< 8),
324 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
325 .wLength
= HIDInterfaceInfo
->State
.HIDReportSize
,
328 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
330 if ((ErrorCode
= USB_Host_SendControlRequest(HIDReportData
)) != HOST_SENDCONTROL_Successful
)
333 if (HIDInterfaceInfo
->State
.UsingBootProtocol
)
335 USB_ControlRequest
= (USB_Request_Header_t
)
337 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
338 .bRequest
= HID_REQ_SetProtocol
,
340 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
344 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
347 HIDInterfaceInfo
->State
.UsingBootProtocol
= false;
350 if (HIDInterfaceInfo
->Config
.HIDParserData
== NULL
)
351 return HID_ERROR_LOGICAL
;
353 if ((ErrorCode
= USB_ProcessHIDReport(HIDReportData
, HIDInterfaceInfo
->State
.HIDReportSize
,
354 HIDInterfaceInfo
->Config
.HIDParserData
)) != HID_PARSE_Successful
)
356 return HID_ERROR_LOGICAL
| ErrorCode
;
359 uint8_t LargestReportSizeBits
= HIDInterfaceInfo
->Config
.HIDParserData
->LargestReportSizeBits
;
360 HIDInterfaceInfo
->State
.LargestReportSize
= (LargestReportSizeBits
>> 3) + ((LargestReportSizeBits
& 0x07) != 0);