Finished basic documentation of all device mode class drivers.
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / HID.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 #include "HID.h"
32
33 void USB_HID_ProcessControlPacket(USB_ClassInfo_HID_t* HIDInterfaceInfo)
34 {
35 if (!(Endpoint_IsSETUPReceived()))
36 return;
37
38 if (USB_ControlRequest.wIndex != HIDInterfaceInfo->InterfaceNumber)
39 return;
40
41 switch (USB_ControlRequest.bRequest)
42 {
43 case REQ_GetReport:
44 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
45 {
46 Endpoint_ClearSETUP();
47
48 uint8_t ReportINData[HIDInterfaceInfo->ReportINBufferSize];
49 uint16_t ReportINSize;
50 uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF);
51
52 memset(ReportINData, 0, sizeof(ReportINData));
53
54 ReportINSize = CALLBACK_USB_HID_CreateNextHIDReport(HIDInterfaceInfo, &ReportID, ReportINData);
55
56 Endpoint_Write_Control_Stream_LE(ReportINData, ReportINSize);
57 Endpoint_ClearOUT();
58 }
59
60 break;
61 case REQ_SetReport:
62 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
63 {
64 Endpoint_ClearSETUP();
65
66 uint16_t ReportOUTSize = USB_ControlRequest.wLength;
67 uint8_t ReportOUTData[ReportOUTSize];
68 uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF);
69
70 Endpoint_Read_Control_Stream_LE(ReportOUTData, ReportOUTSize);
71 Endpoint_ClearIN();
72
73 CALLBACK_USB_HID_ProcessReceivedHIDReport(HIDInterfaceInfo, ReportID, ReportOUTData, ReportOUTSize);
74 }
75
76 break;
77 case REQ_GetProtocol:
78 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
79 {
80 Endpoint_ClearSETUP();
81
82 Endpoint_Write_Byte(HIDInterfaceInfo->UsingReportProtocol);
83 Endpoint_ClearIN();
84
85 while (!(Endpoint_IsOUTReceived()));
86 Endpoint_ClearOUT();
87 }
88
89 break;
90 case REQ_SetProtocol:
91 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
92 {
93 Endpoint_ClearSETUP();
94
95 HIDInterfaceInfo->UsingReportProtocol = (USB_ControlRequest.wValue != 0x0000);
96
97 while (!(Endpoint_IsINReady()));
98 Endpoint_ClearIN();
99 }
100
101 break;
102 case REQ_SetIdle:
103 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
104 {
105 Endpoint_ClearSETUP();
106
107 HIDInterfaceInfo->IdleCount = ((USB_ControlRequest.wValue >> 8) << 2);
108
109 while (!(Endpoint_IsINReady()));
110 Endpoint_ClearIN();
111 }
112
113 break;
114 case REQ_GetIdle:
115 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
116 {
117 Endpoint_ClearSETUP();
118
119 Endpoint_Write_Byte(HIDInterfaceInfo->IdleCount >> 2);
120 Endpoint_ClearIN();
121
122 while (!(Endpoint_IsOUTReceived()));
123 Endpoint_ClearOUT();
124 }
125
126 break;
127 }
128 }
129
130 bool USB_HID_ConfigureEndpoints(USB_ClassInfo_HID_t* HIDInterfaceInfo)
131 {
132 HIDInterfaceInfo->UsingReportProtocol = true;
133
134 if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo->ReportINEndpointNumber, EP_TYPE_INTERRUPT,
135 ENDPOINT_DIR_IN, HIDInterfaceInfo->ReportINEndpointSize, ENDPOINT_BANK_SINGLE)))
136 {
137 return false;
138 }
139
140 return true;
141 }
142
143 void USB_HID_USBTask(USB_ClassInfo_HID_t* HIDInterfaceInfo)
144 {
145 if (!(USB_IsConnected))
146 return;
147
148 Endpoint_SelectEndpoint(HIDInterfaceInfo->ReportINEndpointNumber);
149
150 if (Endpoint_IsReadWriteAllowed() &&
151 !(HIDInterfaceInfo->IdleCount && HIDInterfaceInfo->IdleMSRemaining))
152 {
153 if (HIDInterfaceInfo->IdleCount && !(HIDInterfaceInfo->IdleMSRemaining))
154 HIDInterfaceInfo->IdleMSRemaining = HIDInterfaceInfo->IdleCount;
155
156 uint8_t ReportINData[HIDInterfaceInfo->ReportINBufferSize];
157 uint16_t ReportINSize;
158 uint8_t ReportID = 0;
159
160 memset(ReportINData, 0, sizeof(ReportINData));
161
162 ReportINSize = CALLBACK_USB_HID_CreateNextHIDReport(HIDInterfaceInfo, &ReportID, ReportINData);
163
164 if (ReportINSize)
165 {
166 if (ReportID)
167 Endpoint_Write_Stream_LE(&ReportID, sizeof(ReportID), NO_STREAM_CALLBACK);
168
169 Endpoint_Write_Stream_LE(ReportINData, ReportINSize, NO_STREAM_CALLBACK);
170 }
171
172 Endpoint_ClearIN();
173 }
174 }