3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_HOST)
34 #define INCLUDE_FROM_HID_CLASS_HOST_C
37 uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
, uint16_t ConfigDescriptorSize
,
38 uint8_t* ConfigDescriptorData
)
40 uint8_t FoundEndpoints
= 0;
42 memset(&HIDInterfaceInfo
->State
, 0x00, sizeof(HIDInterfaceInfo
->State
));
44 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
45 return HID_ENUMERROR_InvalidConfigDescriptor
;
47 USB_Descriptor_Interface_t
* CurrentHIDInterface
;
51 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
52 DComp_HID_Host_NextHIDInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
54 return HID_ENUMERROR_NoHIDInterfaceFound
;
57 CurrentHIDInterface
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
);
58 } while (HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
&&
59 (CurrentHIDInterface
->Protocol
!= HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
));
61 HIDInterfaceInfo
->State
.InterfaceNumber
= CurrentHIDInterface
->InterfaceNumber
;
62 HIDInterfaceInfo
->State
.SupportsBootProtocol
= (CurrentHIDInterface
->SubClass
!= HID_NON_BOOT_PROTOCOL
);
64 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, DComp_NextHID
) != DESCRIPTOR_SEARCH_COMP_Found
)
66 return HID_ENUMERROR_NoHIDDescriptorFound
;
69 HIDInterfaceInfo
->State
.HIDReportSize
= DESCRIPTOR_CAST(ConfigDescriptorData
, USB_HID_Descriptor_t
).HIDReportLength
;
71 while (FoundEndpoints
!= (HID_FOUND_DATAPIPE_IN
| HID_FOUND_DATAPIPE_OUT
))
73 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
74 DComp_HID_Host_NextHIDInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
76 if (FoundEndpoints
& HID_FOUND_DATAPIPE_IN
)
79 return HID_ENUMERROR_EndpointsNotFound
;
82 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
84 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
86 Pipe_ConfigurePipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
,
87 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
);
88 HIDInterfaceInfo
->State
.DataINPipeSize
= EndpointData
->EndpointSize
;
90 FoundEndpoints
|= HID_FOUND_DATAPIPE_IN
;
94 Pipe_ConfigurePipe(HIDInterfaceInfo
->Config
.DataOUTPipeNumber
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_OUT
,
95 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
);
96 HIDInterfaceInfo
->State
.DataOUTPipeSize
= EndpointData
->EndpointSize
;
98 HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
= true;
100 FoundEndpoints
|= HID_FOUND_DATAPIPE_OUT
;
104 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
105 HIDInterfaceInfo
->State
.IsActive
= true;
106 return HID_ENUMERROR_NoError
;
109 static uint8_t DComp_HID_Host_NextHIDInterface(void* CurrentDescriptor
)
111 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
113 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
114 USB_Descriptor_Interface_t
);
116 if (CurrentInterface
->Class
== HID_INTERFACE_CLASS
)
117 return DESCRIPTOR_SEARCH_Found
;
120 return DESCRIPTOR_SEARCH_NotFound
;
123 static uint8_t DComp_NextHID(void* CurrentDescriptor
)
125 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_HID
)
126 return DESCRIPTOR_SEARCH_Found
;
127 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
128 return DESCRIPTOR_SEARCH_Fail
;
130 return DESCRIPTOR_SEARCH_NotFound
;
133 static uint8_t DComp_HID_Host_NextHIDInterfaceEndpoint(void* CurrentDescriptor
)
135 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
137 USB_Descriptor_Endpoint_t
* CurrentEndpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
,
138 USB_Descriptor_Endpoint_t
);
140 if (!(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
)))
141 return DESCRIPTOR_SEARCH_Found
;
143 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
145 return DESCRIPTOR_SEARCH_Fail
;
148 return DESCRIPTOR_SEARCH_NotFound
;
151 void HID_Host_USBTask(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
156 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
, const uint8_t ReportID
, void* Buffer
)
158 USB_ControlRequest
= (USB_Request_Header_t
)
160 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
161 .bRequest
= REQ_SetReport
,
163 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
164 .wLength
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, REPORT_ITEM_TYPE_In
),
167 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
169 return USB_Host_SendControlRequest(Buffer
);
172 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
, void* Buffer
)
174 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
179 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
184 if (HIDInterfaceInfo
->State
.UsingBootProtocol
)
186 ReportSize
= Pipe_BytesInPipe();
190 uint8_t ReportID
= 0;
192 if (HIDInterfaceInfo
->Config
.HIDParserData
->UsingReportIDs
)
194 ReportID
= Pipe_Read_Byte();
195 *((uint8_t*)Buffer
++) = ReportID
;
198 ReportSize
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, REPORT_ITEM_TYPE_In
);
201 if ((ErrorCode
= Pipe_Read_Stream_LE(Buffer
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
207 return PIPE_RWSTREAM_NoError
;
210 uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
, uint8_t ReportID
, void* Buffer
,
211 const uint16_t ReportSize
)
213 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
216 if (HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
)
218 USB_ControlRequest
= (USB_Request_Header_t
)
220 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
221 .bRequest
= REQ_SetReport
,
223 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
224 .wLength
= ReportSize
,
227 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
229 return USB_Host_SendControlRequest(Buffer
);
235 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataOUTPipeNumber
);
239 Pipe_Write_Stream_LE(&ReportID
, sizeof(ReportID
), NO_STREAM_CALLBACK
);
241 if ((ErrorCode
= Pipe_Write_Stream_LE(Buffer
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
247 return PIPE_RWSTREAM_NoError
;
251 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
253 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
258 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
261 ReportReceived
= Pipe_IsINReceived();
265 return ReportReceived
;
268 uint8_t HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
270 if (HIDInterfaceInfo
->State
.UsingBootProtocol
)
271 return HOST_SENDCONTROL_Successful
;
275 USB_ControlRequest
= (USB_Request_Header_t
)
277 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
278 .bRequest
= REQ_SetProtocol
,
280 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
284 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
286 if (!(HIDInterfaceInfo
->State
.SupportsBootProtocol
))
287 return HID_ERROR_LOGICAL
;
289 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
292 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
293 HIDInterfaceInfo
->State
.UsingBootProtocol
= true;
295 return HOST_SENDCONTROL_Successful
;
298 uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
302 uint8_t HIDReportData
[HIDInterfaceInfo
->State
.HIDReportSize
];
304 USB_ControlRequest
= (USB_Request_Header_t
)
306 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_INTERFACE
),
307 .bRequest
= REQ_GetDescriptor
,
308 .wValue
= (DTYPE_Report
<< 8),
309 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
310 .wLength
= HIDInterfaceInfo
->State
.HIDReportSize
,
313 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
315 if ((ErrorCode
= USB_Host_SendControlRequest(HIDReportData
)) != HOST_SENDCONTROL_Successful
)
318 if (HIDInterfaceInfo
->State
.UsingBootProtocol
)
320 USB_ControlRequest
= (USB_Request_Header_t
)
322 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
323 .bRequest
= REQ_SetProtocol
,
325 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
329 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
332 HIDInterfaceInfo
->State
.UsingBootProtocol
= false;
335 if (HIDInterfaceInfo
->Config
.HIDParserData
== NULL
)
336 return HID_ERROR_LOGICAL
;
338 if ((ErrorCode
= USB_ProcessHIDReport(HIDReportData
, HIDInterfaceInfo
->State
.HIDReportSize
,
339 HIDInterfaceInfo
->Config
.HIDParserData
)) != HID_PARSE_Successful
)
341 return HID_ERROR_LOGICAL
| ErrorCode
;
344 uint8_t LargestReportSizeBits
= HIDInterfaceInfo
->Config
.HIDParserData
->LargestReportSizeBits
;
345 HIDInterfaceInfo
->State
.LargestReportSize
= (LargestReportSizeBits
>> 3) + ((LargestReportSizeBits
& 0x07) != 0);