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
33 void USB_HID_ProcessControlPacket(USB_ClassInfo_HID_t
* HIDInterfaceInfo
)
35 if (!(Endpoint_IsSETUPReceived()))
38 if (USB_ControlRequest
.wIndex
!= HIDInterfaceInfo
->InterfaceNumber
)
41 switch (USB_ControlRequest
.bRequest
)
44 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
46 Endpoint_ClearSETUP();
48 uint8_t ReportINData
[HIDInterfaceInfo
->ReportBufferSize
];
49 uint16_t ReportINSize
;
51 memset(ReportINData
, 0, sizeof(ReportINData
));
53 ReportINSize
= CALLBACK_USB_HID_CreateNextHIDReport(HIDInterfaceInfo
, ReportINData
);
55 Endpoint_Write_Control_Stream_LE(ReportINData
, ReportINSize
);
61 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
63 Endpoint_ClearSETUP();
65 uint16_t ReportOUTSize
= USB_ControlRequest
.wLength
;
66 uint8_t ReportOUTData
[ReportOUTSize
];
68 Endpoint_Read_Control_Stream_LE(ReportOUTData
, ReportOUTSize
);
71 CALLBACK_USB_HID_ProcessReceivedHIDReport(HIDInterfaceInfo
, ReportOUTData
, ReportOUTSize
);
76 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
78 Endpoint_ClearSETUP();
80 Endpoint_Write_Byte(HIDInterfaceInfo
->UsingReportProtocol
);
83 while (!(Endpoint_IsOUTReceived()));
89 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
91 Endpoint_ClearSETUP();
93 HIDInterfaceInfo
->UsingReportProtocol
= (USB_ControlRequest
.wValue
!= 0x0000);
95 while (!(Endpoint_IsINReady()));
101 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
103 Endpoint_ClearSETUP();
105 HIDInterfaceInfo
->IdleCount
= ((USB_ControlRequest
.wValue
>> 8) << 2);
107 while (!(Endpoint_IsINReady()));
113 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
115 Endpoint_ClearSETUP();
117 Endpoint_Write_Byte(HIDInterfaceInfo
->IdleCount
>> 2);
120 while (!(Endpoint_IsOUTReceived()));
128 bool USB_HID_ConfigureEndpoints(USB_ClassInfo_HID_t
* HIDInterfaceInfo
)
130 HIDInterfaceInfo
->UsingReportProtocol
= true;
132 if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo
->ReportINEndpointNumber
, EP_TYPE_INTERRUPT
,
133 ENDPOINT_DIR_IN
, HIDInterfaceInfo
->ReportINEndpointSize
, ENDPOINT_BANK_SINGLE
)))
138 if (HIDInterfaceInfo
->ReportOUTEndpointNumber
)
140 if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo
->ReportOUTEndpointNumber
, EP_TYPE_INTERRUPT
,
141 ENDPOINT_DIR_OUT
, HIDInterfaceInfo
->ReportOUTEndpointSize
, ENDPOINT_BANK_SINGLE
)))
150 void USB_HID_RegisterStartOfFrame(USB_ClassInfo_HID_t
* HIDInterfaceInfo
)
152 if (HIDInterfaceInfo
->IdleMSRemaining
)
153 HIDInterfaceInfo
->IdleMSRemaining
--;
156 void USB_HID_USBTask(USB_ClassInfo_HID_t
* HIDInterfaceInfo
)
158 if (!(USB_IsConnected
))
161 Endpoint_SelectEndpoint(HIDInterfaceInfo
->ReportINEndpointNumber
);
163 if (Endpoint_IsReadWriteAllowed() &&
164 !(HIDInterfaceInfo
->IdleCount
&& HIDInterfaceInfo
->IdleMSRemaining
))
166 if (HIDInterfaceInfo
->IdleCount
&& !(HIDInterfaceInfo
->IdleMSRemaining
))
167 HIDInterfaceInfo
->IdleMSRemaining
= HIDInterfaceInfo
->IdleCount
;
169 uint8_t ReportINData
[HIDInterfaceInfo
->ReportBufferSize
];
170 uint16_t ReportINSize
;
172 memset(ReportINData
, 0, sizeof(ReportINData
));
174 ReportINSize
= CALLBACK_USB_HID_CreateNextHIDReport(HIDInterfaceInfo
, ReportINData
);
178 Endpoint_Write_Stream_LE(ReportINData
, ReportINSize
179 #if !defined(NO_STREAM_CALLBACKS)
188 if (HIDInterfaceInfo
->ReportOUTEndpointNumber
)
190 Endpoint_SelectEndpoint(HIDInterfaceInfo
->ReportOUTEndpointNumber
);
192 if (Endpoint_IsOUTReceived())
194 uint16_t ReportOUTSize
= Endpoint_BytesInEndpoint();
195 uint8_t ReportOUTData
[ReportOUTSize
];
199 Endpoint_Read_Stream_LE(ReportOUTData
, ReportOUTSize
200 #if !defined(NO_STREAM_CALLBACKS)
206 CALLBACK_USB_HID_ProcessReceivedHIDReport(HIDInterfaceInfo
, ReportOUTData
, ReportOUTSize
);