4 #include "usb_keycodes.h"
10 static report_keyboard_t report0
;
11 static report_keyboard_t report1
;
12 report_keyboard_t
*keyboard_report
= &report0
;
13 report_keyboard_t
*keyboard_report_prev
= &report1
;
15 static uint8_t keyboard_leds
= 0;
16 static uchar idleRate
= 0;
18 uint8_t host_keyboard_leds(void)
24 /*------------------------------------------------------------------*
25 * Keyboard report operations
26 *------------------------------------------------------------------*/
27 void host_add_key(uint8_t code
)
31 for (; i
< REPORT_KEYS
; i
++) {
32 if (keyboard_report_prev
->keys
[i
] == code
) {
33 keyboard_report
->keys
[i
] = code
;
36 if (empty
== -1 && keyboard_report_prev
->keys
[i
] == KB_NO
&& keyboard_report
->keys
[i
] == KB_NO
) {
40 if (i
== REPORT_KEYS
&& empty
!= -1) {
41 keyboard_report
->keys
[empty
] = code
;
45 void host_add_mod_bit(uint8_t mod
)
47 keyboard_report
->mods
|= mod
;
50 void host_set_mods(uint8_t mods
)
52 keyboard_report
->mods
= mods
;
55 void host_add_code(uint8_t code
)
58 host_add_mod_bit(MOD_BIT(code
));
64 void host_swap_keyboard_report(void)
66 report_keyboard_t
*tmp
= keyboard_report_prev
;
67 keyboard_report_prev
= keyboard_report
;
68 keyboard_report
= tmp
;
71 void host_clear_keyboard_report(void)
73 keyboard_report
->mods
= 0;
74 for (int8_t i
= 0; i
< REPORT_KEYS
; i
++) {
75 keyboard_report
->keys
[i
] = 0;
79 uint8_t host_has_anykey(void)
82 for (int i
= 0; i
< REPORT_KEYS
; i
++) {
83 if (keyboard_report
->keys
[i
])
89 uint8_t host_get_first_key(void)
91 #ifdef USB_NKRO_ENABLE
94 for (; i
< REPORT_KEYS
&& !keyboard_report
->keys
[i
]; i
++)
96 return i
<<3 | biton(keyboard_report
->keys
[i
]);
99 return keyboard_report
->keys
[0];
103 /*------------------------------------------------------------------*
104 * Keyboard report send buffer
105 *------------------------------------------------------------------*/
107 static report_keyboard_t kbuf
[KBUF_SIZE
];
108 static uint8_t kbuf_head
= 0;
109 static uint8_t kbuf_tail
= 0;
111 void host_vusb_keyboard_send(void)
113 while (usbInterruptIsReady() && kbuf_head
!= kbuf_tail
) {
114 usbSetInterrupt((void *)&kbuf
[kbuf_tail
], sizeof(report_keyboard_t
));
115 kbuf_tail
= (kbuf_tail
+ 1) % KBUF_SIZE
;
119 void host_send_keyboard_report(void)
121 uint8_t next
= (kbuf_head
+ 1) % KBUF_SIZE
;
122 if (next
!= kbuf_tail
) {
123 kbuf
[kbuf_head
] = *keyboard_report
;
126 debug("kbuf: full\n");
131 void host_mouse_send(report_mouse_t
*report
)
133 if (usbInterruptIsReady3()) {
134 usbSetInterrupt3((void *)report
, sizeof(*report
));
136 debug("Int3 not ready\n");
142 /*------------------------------------------------------------------*
143 * Request from host *
144 *------------------------------------------------------------------*/
153 usbMsgLen_t
usbFunctionSetup(uchar data
[8])
155 usbRequest_t
*rq
= (void *)data
;
157 if((rq
->bmRequestType
& USBRQ_TYPE_MASK
) == USBRQ_TYPE_CLASS
){ /* class request type */
158 if(rq
->bRequest
== USBRQ_HID_GET_REPORT
){
159 debug(" GET_REPORT");
160 /* we only have one report type, so don't look at wValue */
161 usbMsgPtr
= (void *)keyboard_report
;
162 return sizeof(*keyboard_report
);
163 }else if(rq
->bRequest
== USBRQ_HID_GET_IDLE
){
164 debug(" GET_IDLE: ");
166 usbMsgPtr
= &idleRate
;
168 }else if(rq
->bRequest
== USBRQ_HID_SET_IDLE
){
169 idleRate
= rq
->wValue
.bytes
[1];
170 debug(" SET_IDLE: ");
172 }else if(rq
->bRequest
== USBRQ_HID_SET_REPORT
){
173 //debug(" SET_REPORT: ");
174 if (rq
->wValue
.word
== 0x0200 && rq
->wIndex
.word
== 0) {
175 last_req
.kind
= SET_LED
;
176 last_req
.len
= rq
->wLength
.word
;
178 return USB_NO_MSG
; // to get data in usbFunctionWrite
183 /* no vendor specific requests implemented */
185 return 0; /* default for not implemented requests: return no data back to host */
188 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
190 if (last_req
.len
== 0) {
193 switch (last_req
.kind
) {
195 //debug("SET_LED\n");
196 keyboard_leds
= data
[0];
210 /*------------------------------------------------------------------*
212 *------------------------------------------------------------------*/
215 * Report Descriptor for keyboard
217 * from an example in HID spec appendix
219 PROGMEM uchar keyboard_hid_report
[] = {
220 0x05, 0x01, // Usage Page (Generic Desktop),
221 0x09, 0x06, // Usage (Keyboard),
222 0xA1, 0x01, // Collection (Application),
223 0x75, 0x01, // Report Size (1),
224 0x95, 0x08, // Report Count (8),
225 0x05, 0x07, // Usage Page (Key Codes),
226 0x19, 0xE0, // Usage Minimum (224),
227 0x29, 0xE7, // Usage Maximum (231),
228 0x15, 0x00, // Logical Minimum (0),
229 0x25, 0x01, // Logical Maximum (1),
230 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
231 0x95, 0x01, // Report Count (1),
232 0x75, 0x08, // Report Size (8),
233 0x81, 0x03, // Input (Constant), ;Reserved byte
234 0x95, 0x05, // Report Count (5),
235 0x75, 0x01, // Report Size (1),
236 0x05, 0x08, // Usage Page (LEDs),
237 0x19, 0x01, // Usage Minimum (1),
238 0x29, 0x05, // Usage Maximum (5),
239 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
240 0x95, 0x01, // Report Count (1),
241 0x75, 0x03, // Report Size (3),
242 0x91, 0x03, // Output (Constant), ;LED report padding
243 0x95, 0x06, // Report Count (6),
244 0x75, 0x08, // Report Size (8),
245 0x15, 0x00, // Logical Minimum (0),
246 0x25, 0xFF, // Logical Maximum(255),
247 0x05, 0x07, // Usage Page (Key Codes),
248 0x19, 0x00, // Usage Minimum (0),
249 0x29, 0xFF, // Usage Maximum (255),
250 0x81, 0x00, // Input (Data, Array),
251 0xc0 // End Collection
255 * Report Descriptor for mouse
257 * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
258 * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
259 * http://www.keil.com/forum/15671/
260 * http://www.microsoft.com/whdc/device/input/wheel.mspx
262 PROGMEM uchar mouse_hid_report
[] = {
263 /* from HID 1.11 spec example */
264 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
265 0x09, 0x02, // USAGE (Mouse)
266 0xa1, 0x01, // COLLECTION (Application)
267 0x09, 0x02, // USAGE (Mouse)
268 0xa1, 0x02, // COLLECTION (Logical)
269 0x09, 0x01, // USAGE (Pointer)
270 0xa1, 0x00, // COLLECTION (Physical)
271 // ------------------------------ Buttons
272 0x05, 0x09, // USAGE_PAGE (Button)
273 0x19, 0x01, // USAGE_MINIMUM (Button 1)
274 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
275 0x15, 0x00, // LOGICAL_MINIMUM (0)
276 0x25, 0x01, // LOGICAL_MAXIMUM (1)
277 0x75, 0x01, // REPORT_SIZE (1)
278 0x95, 0x05, // REPORT_COUNT (5)
279 0x81, 0x02, // INPUT (Data,Var,Abs)
280 // ------------------------------ Padding
281 0x75, 0x03, // REPORT_SIZE (3)
282 0x95, 0x01, // REPORT_COUNT (1)
283 0x81, 0x03, // INPUT (Cnst,Var,Abs)
284 // ------------------------------ X,Y position
285 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
286 0x09, 0x30, // USAGE (X)
287 0x09, 0x31, // USAGE (Y)
288 0x15, 0x81, // LOGICAL_MINIMUM (-127)
289 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
290 0x75, 0x08, // REPORT_SIZE (8)
291 0x95, 0x02, // REPORT_COUNT (2)
292 0x81, 0x06, // INPUT (Data,Var,Rel)
293 0xa1, 0x02, // COLLECTION (Logical)
294 // ------------------------------ Vertical wheel res multiplier
295 0x09, 0x48, // USAGE (Resolution Multiplier)
296 0x15, 0x00, // LOGICAL_MINIMUM (0)
297 0x25, 0x01, // LOGICAL_MAXIMUM (1)
298 0x35, 0x01, // PHYSICAL_MINIMUM (1)
299 0x45, 0x04, // PHYSICAL_MAXIMUM (4)
300 0x75, 0x02, // REPORT_SIZE (2)
301 0x95, 0x01, // REPORT_COUNT (1)
303 0xb1, 0x02, // FEATURE (Data,Var,Abs)
304 // ------------------------------ Vertical wheel
305 0x09, 0x38, // USAGE (Wheel)
306 0x15, 0x81, // LOGICAL_MINIMUM (-127)
307 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
308 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
309 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
310 0x75, 0x08, // REPORT_SIZE (8)
311 0x81, 0x06, // INPUT (Data,Var,Rel)
312 0xc0, // END_COLLECTION
313 0xa1, 0x02, // COLLECTION (Logical)
314 // ------------------------------ Horizontal wheel res multiplier
315 0x09, 0x48, // USAGE (Resolution Multiplier)
317 0xb1, 0x02, // FEATURE (Data,Var,Abs)
318 // ------------------------------ Padding for Feature report
319 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
320 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
321 0x75, 0x04, // REPORT_SIZE (4)
322 0xb1, 0x03, // FEATURE (Cnst,Var,Abs)
323 // ------------------------------ Horizontal wheel
324 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
325 0x0a, 0x38, 0x02, // USAGE (AC Pan)
326 0x15, 0x81, // LOGICAL_MINIMUM (-127)
327 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
328 0x75, 0x08, // REPORT_SIZE (8)
329 0x81, 0x06, // INPUT (Data,Var,Rel)
330 0xc0, // END_COLLECTION
331 0xc0, // END_COLLECTION
332 0xc0, // END_COLLECTION
333 0xc0 // END_COLLECTION
338 * Descriptor for compite device: Keyboard + Mouse
340 * contains: device, interface, HID and endpoint descriptors
342 #if USB_CFG_DESCR_PROPS_CONFIGURATION
343 PROGMEM
char usbDescriptorConfiguration
[] = { /* USB configuration descriptor */
344 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
345 USBDESCR_CONFIG
, /* descriptor type */
346 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
347 //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
348 /* total length of data returned (including inlined descriptors) */
349 2, /* number of interfaces in this configuration */
350 1, /* index of this configuration */
351 0, /* configuration name string index */
352 #if USB_CFG_IS_SELF_POWERED
353 (1 << 7) | USBATTR_SELFPOWER
, /* attributes */
355 (1 << 7), /* attributes */
357 USB_CFG_MAX_BUS_POWER
/2, /* max USB current in 2mA units */
362 /* Interface descriptor */
363 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
364 USBDESCR_INTERFACE
, /* descriptor type */
365 0, /* index of this interface */
366 0, /* alternate setting for this interface */
367 USB_CFG_HAVE_INTRIN_ENDPOINT
, /* endpoints excl 0: number of endpoint descriptors to follow */
368 USB_CFG_INTERFACE_CLASS
,
369 USB_CFG_INTERFACE_SUBCLASS
,
370 USB_CFG_INTERFACE_PROTOCOL
,
371 0, /* string index for interface */
373 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
374 USBDESCR_HID
, /* descriptor type: HID */
375 0x01, 0x01, /* BCD representation of HID version */
376 0x00, /* target country code */
377 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
378 0x22, /* descriptor type: report */
379 sizeof(keyboard_hid_report
), 0, /* total length of report descriptor */
380 /* Endpoint descriptor */
381 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
382 7, /* sizeof(usbDescrEndpoint) */
383 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
384 (char)0x81, /* IN endpoint number 1 */
385 0x03, /* attrib: Interrupt endpoint */
386 8, 0, /* maximum packet size */
387 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
393 /* Interface descriptor */
394 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
395 USBDESCR_INTERFACE
, /* descriptor type */
396 1, /* index of this interface */
397 0, /* alternate setting for this interface */
398 USB_CFG_HAVE_INTRIN_ENDPOINT3
, /* endpoints excl 0: number of endpoint descriptors to follow */
399 0x03, /* CLASS: HID */
400 0, /* SUBCLASS: none */
401 0, /* PROTOCOL: none */
402 0, /* string index for interface */
404 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
405 USBDESCR_HID
, /* descriptor type: HID */
406 0x01, 0x01, /* BCD representation of HID version */
407 0x00, /* target country code */
408 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
409 0x22, /* descriptor type: report */
410 sizeof(mouse_hid_report
), 0, /* total length of report descriptor */
411 #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
412 /* Endpoint descriptor */
413 7, /* sizeof(usbDescrEndpoint) */
414 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
415 (char)(0x80 | USB_CFG_EP3_NUMBER
), /* IN endpoint number 3 */
416 0x03, /* attrib: Interrupt endpoint */
417 8, 0, /* maximum packet size */
418 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
424 USB_PUBLIC usbMsgLen_t
usbFunctionDescriptor(struct usbRequest
*rq
)
428 debug("usbFunctionDescriptor: ");
429 debug_hex(rq
->bmRequestType
); debug(" ");
430 debug_hex(rq
->bRequest
); debug(" ");
431 debug_hex16(rq
->wValue
.word
); debug(" ");
432 debug_hex16(rq
->wIndex
.word
); debug(" ");
433 debug_hex16(rq
->wLength
.word
); debug("\n");
435 switch (rq
->wValue
.bytes
[1]) {
436 #if USB_CFG_DESCR_PROPS_CONFIGURATION
437 case USBDESCR_CONFIG
:
438 usbMsgPtr
= (unsigned char *)usbDescriptorConfiguration
;
439 len
= sizeof(usbDescriptorConfiguration
);
443 usbMsgPtr
= (unsigned char *)(usbDescriptorConfiguration
+ 18);
446 case USBDESCR_HID_REPORT
:
447 /* interface index */
448 switch (rq
->wIndex
.word
) {
450 usbMsgPtr
= keyboard_hid_report
;
451 len
= sizeof(keyboard_hid_report
);
454 usbMsgPtr
= mouse_hid_report
;
455 len
= sizeof(mouse_hid_report
);
460 debug("desc len: "); debug_hex(len
); debug("\n");