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_ProcessControlRequest(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
)
38 if (!(Endpoint_IsSETUPReceived()))
41 if (USB_ControlRequest
.wIndex
!= HIDInterfaceInfo
->Config
.InterfaceNumber
)
44 switch (USB_ControlRequest
.bRequest
)
47 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
49 Endpoint_ClearSETUP();
51 uint16_t ReportINSize
= 0;
52 uint8_t ReportID
= (USB_ControlRequest
.wValue
& 0xFF);
53 uint8_t ReportType
= (USB_ControlRequest
.wValue
>> 8) - 1;
55 memset(HIDInterfaceInfo
->Config
.PrevReportINBuffer
, 0, HIDInterfaceInfo
->Config
.PrevReportINBufferSize
);
57 CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo
, &ReportID
, ReportType
,
58 HIDInterfaceInfo
->Config
.PrevReportINBuffer
, &ReportINSize
);
60 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP
);
61 Endpoint_Write_Control_Stream_LE(HIDInterfaceInfo
->Config
.PrevReportINBuffer
, ReportINSize
);
67 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
69 Endpoint_ClearSETUP();
71 uint16_t ReportOUTSize
= USB_ControlRequest
.wLength
;
72 uint8_t ReportOUTData
[ReportOUTSize
];
73 uint8_t ReportID
= (USB_ControlRequest
.wValue
& 0xFF);
75 Endpoint_Read_Control_Stream_LE(ReportOUTData
, ReportOUTSize
);
78 CALLBACK_HID_Device_ProcessHIDReport(HIDInterfaceInfo
, ReportID
, ReportOUTData
, ReportOUTSize
);
83 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
85 Endpoint_ClearSETUP();
87 Endpoint_Write_Byte(HIDInterfaceInfo
->State
.UsingReportProtocol
);
90 Endpoint_ClearStatusStage();
95 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
97 Endpoint_ClearSETUP();
99 HIDInterfaceInfo
->State
.UsingReportProtocol
= ((USB_ControlRequest
.wValue
& 0xFF) != 0x00);
101 Endpoint_ClearStatusStage();
106 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
108 Endpoint_ClearSETUP();
110 HIDInterfaceInfo
->State
.IdleCount
= ((USB_ControlRequest
.wValue
& 0xFF00) >> 6);
112 Endpoint_ClearStatusStage();
117 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
119 Endpoint_ClearSETUP();
121 Endpoint_Write_Byte(HIDInterfaceInfo
->State
.IdleCount
>> 2);
124 Endpoint_ClearStatusStage();
131 bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
)
133 memset(&HIDInterfaceInfo
->State
, 0x00, sizeof(HIDInterfaceInfo
->State
));
134 HIDInterfaceInfo
->State
.UsingReportProtocol
= true;
135 HIDInterfaceInfo
->State
.IdleCount
= 500;
137 if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo
->Config
.ReportINEndpointNumber
, EP_TYPE_INTERRUPT
,
138 ENDPOINT_DIR_IN
, HIDInterfaceInfo
->Config
.ReportINEndpointSize
,
139 HIDInterfaceInfo
->Config
.ReportINEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE
: ENDPOINT_BANK_SINGLE
)))
147 void HID_Device_USBTask(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
)
149 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
152 Endpoint_SelectEndpoint(HIDInterfaceInfo
->Config
.ReportINEndpointNumber
);
154 if (Endpoint_IsReadWriteAllowed())
156 uint8_t ReportINData
[HIDInterfaceInfo
->Config
.PrevReportINBufferSize
];
157 uint8_t ReportID
= 0;
158 uint16_t ReportINSize
= 0;
160 memset(ReportINData
, 0, sizeof(ReportINData
));
162 bool ForceSend
= CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo
, &ReportID
, REPORT_ITEM_TYPE_In
,
163 ReportINData
, &ReportINSize
);
164 bool StatesChanged
= false;
165 bool IdlePeriodElapsed
= (HIDInterfaceInfo
->State
.IdleCount
&& !(HIDInterfaceInfo
->State
.IdleMSRemaining
));
167 if (HIDInterfaceInfo
->Config
.PrevReportINBuffer
!= NULL
)
169 StatesChanged
= (memcmp(ReportINData
, HIDInterfaceInfo
->Config
.PrevReportINBuffer
, ReportINSize
) != 0);
170 memcpy(HIDInterfaceInfo
->Config
.PrevReportINBuffer
, ReportINData
, ReportINSize
);
173 if (ReportINSize
&& (ForceSend
|| StatesChanged
|| IdlePeriodElapsed
))
175 HIDInterfaceInfo
->State
.IdleMSRemaining
= HIDInterfaceInfo
->State
.IdleCount
;
177 Endpoint_SelectEndpoint(HIDInterfaceInfo
->Config
.ReportINEndpointNumber
);
180 Endpoint_Write_Byte(ReportID
);
182 Endpoint_Write_Stream_LE(ReportINData
, ReportINSize
, NO_STREAM_CALLBACK
);
189 void HID_Device_MillisecondElapsed(USB_ClassInfo_HID_Device_t
* HIDInterfaceInfo
)
191 if (HIDInterfaceInfo
->State
.IdleMSRemaining
)
192 HIDInterfaceInfo
->State
.IdleMSRemaining
--;