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_DEVICE)
36 void HID_Device_ProcessControlPacket(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
)
38 if (!(Endpoint_IsSETUPReceived()))
41 if ((USB_ControlRequest
.wIndex
!= HIDInterfaceInfo
->Config
.InterfaceNumber
) &&
42 (USB_ControlRequest
.bRequest
!= REQ_SetIdle
))
47 switch (USB_ControlRequest
.bRequest
)
50 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
52 Endpoint_ClearSETUP();
54 uint8_t ReportINData
[HIDInterfaceInfo
->Config
.ReportINBufferSize
];
55 uint16_t ReportINSize
;
56 uint8_t ReportID
= (USB_ControlRequest
.wValue
& 0xFF);
58 memset(ReportINData
, 0, sizeof(ReportINData
));
60 ReportINSize
= CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo
, &ReportID
, ReportINData
);
62 Endpoint_Write_Control_Stream_LE(ReportINData
, ReportINSize
);
68 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
70 Endpoint_ClearSETUP();
72 uint16_t ReportOUTSize
= USB_ControlRequest
.wLength
;
73 uint8_t ReportOUTData
[ReportOUTSize
];
74 uint8_t ReportID
= (USB_ControlRequest
.wValue
& 0xFF);
76 Endpoint_Read_Control_Stream_LE(ReportOUTData
, ReportOUTSize
);
79 CALLBACK_HID_Device_ProcessHIDReport(HIDInterfaceInfo
, ReportID
, ReportOUTData
, ReportOUTSize
);
84 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
86 Endpoint_ClearSETUP();
88 Endpoint_Write_Byte(HIDInterfaceInfo
->State
.UsingReportProtocol
);
91 while (!(Endpoint_IsOUTReceived()));
97 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
99 Endpoint_ClearSETUP();
101 HIDInterfaceInfo
->State
.UsingReportProtocol
= (USB_ControlRequest
.wValue
!= 0x0000);
103 while (!(Endpoint_IsINReady()));
109 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
111 if ((USB_ControlRequest
.wIndex
== HIDInterfaceInfo
->Config
.InterfaceNumber
) ||
112 (USB_ControlRequest
.wValue
& 0xFF) == 0)
114 Endpoint_ClearSETUP();
116 HIDInterfaceInfo
->State
.IdleCount
= ((USB_ControlRequest
.wValue
& 0xFF00) >> 6);
118 while (!(Endpoint_IsINReady()));
125 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
127 Endpoint_ClearSETUP();
129 Endpoint_Write_Byte(HIDInterfaceInfo
->State
.IdleCount
>> 2);
132 while (!(Endpoint_IsOUTReceived()));
140 bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
)
142 HIDInterfaceInfo
->State
.UsingReportProtocol
= true;
144 if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo
->Config
.ReportINEndpointNumber
, EP_TYPE_INTERRUPT
,
145 ENDPOINT_DIR_IN
, HIDInterfaceInfo
->Config
.ReportINEndpointSize
, ENDPOINT_BANK_SINGLE
)))
153 void HID_Device_USBTask(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
)
155 if (!(USB_IsConnected
) || !(USB_ConfigurationNumber
))
158 Endpoint_SelectEndpoint(HIDInterfaceInfo
->Config
.ReportINEndpointNumber
);
160 if (Endpoint_IsReadWriteAllowed() &&
161 !(HIDInterfaceInfo
->State
.IdleCount
&& HIDInterfaceInfo
->State
.IdleMSRemaining
))
163 if (HIDInterfaceInfo
->State
.IdleCount
&& !(HIDInterfaceInfo
->State
.IdleMSRemaining
))
164 HIDInterfaceInfo
->State
.IdleMSRemaining
= HIDInterfaceInfo
->State
.IdleCount
;
166 uint8_t ReportINData
[HIDInterfaceInfo
->Config
.ReportINBufferSize
];
167 uint16_t ReportINSize
;
168 uint8_t ReportID
= 0;
170 memset(ReportINData
, 0, sizeof(ReportINData
));
172 ReportINSize
= CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo
, &ReportID
, ReportINData
);
177 Endpoint_Write_Stream_LE(&ReportID
, sizeof(ReportID
), NO_STREAM_CALLBACK
);
179 Endpoint_Write_Stream_LE(ReportINData
, ReportINSize
, NO_STREAM_CALLBACK
);