2 #include <avr/interrupt.h>
6 #include "usb_keycodes.h"
12 static report_keyboard_t report0
;
13 static report_keyboard_t report1
;
14 report_keyboard_t
*keyboard_report
= &report0
;
15 report_keyboard_t
*keyboard_report_prev
= &report1
;
17 static uint8_t keyboard_leds
= 0;
18 static uchar idleRate
= 0;
20 uint8_t host_keyboard_leds(void)
26 /*------------------------------------------------------------------*
27 * Keyboard report operations
28 *------------------------------------------------------------------*/
29 void host_add_key(uint8_t code
)
33 for (; i
< REPORT_KEYS
; i
++) {
34 if (keyboard_report_prev
->keys
[i
] == code
) {
35 keyboard_report
->keys
[i
] = code
;
38 if (empty
== -1 && keyboard_report_prev
->keys
[i
] == KB_NO
&& keyboard_report
->keys
[i
] == KB_NO
) {
42 if (i
== REPORT_KEYS
&& empty
!= -1) {
43 keyboard_report
->keys
[empty
] = code
;
47 void host_add_mod_bit(uint8_t mod
)
49 keyboard_report
->mods
|= mod
;
52 void host_set_mods(uint8_t mods
)
54 keyboard_report
->mods
= mods
;
57 void host_add_code(uint8_t code
)
60 host_add_mod_bit(MOD_BIT(code
));
66 void host_swap_keyboard_report(void)
70 report_keyboard_t
*tmp
= keyboard_report_prev
;
71 keyboard_report_prev
= keyboard_report
;
72 keyboard_report
= tmp
;
76 void host_clear_keyboard_report(void)
78 keyboard_report
->mods
= 0;
79 for (int8_t i
= 0; i
< REPORT_KEYS
; i
++) {
80 keyboard_report
->keys
[i
] = 0;
84 uint8_t host_has_anykey(void)
87 for (int i
= 0; i
< REPORT_KEYS
; i
++) {
88 if (keyboard_report
->keys
[i
])
94 uint8_t host_get_first_key(void)
96 #ifdef USB_NKRO_ENABLE
99 for (; i
< REPORT_KEYS
&& !keyboard_report
->keys
[i
]; i
++)
101 return i
<<3 | biton(keyboard_report
->keys
[i
]);
104 return keyboard_report
->keys
[0];
108 /*------------------------------------------------------------------*
109 * Keyboard report send buffer
110 *------------------------------------------------------------------*/
112 static report_keyboard_t kbuf
[KBUF_SIZE
];
113 static uint8_t kbuf_head
= 0;
114 static uint8_t kbuf_tail
= 0;
116 void host_vusb_keyboard_send(void)
118 if (usbInterruptIsReady() && kbuf_head
!= kbuf_tail
) {
119 usbSetInterrupt((void *)&kbuf
[kbuf_tail
], sizeof(report_keyboard_t
));
120 kbuf_tail
= (kbuf_tail
+ 1) % KBUF_SIZE
;
124 void host_send_keyboard_report(void)
126 uint8_t next
= (kbuf_head
+ 1) % KBUF_SIZE
;
127 if (next
!= kbuf_tail
) {
128 kbuf
[kbuf_head
] = *keyboard_report
;
131 debug("kbuf: full\n");
136 #if defined(MOUSEKEY_ENABLE) || defined(PS2_MOUSE_ENABLE)
137 void host_mouse_send(report_mouse_t
*report
)
139 report
->report_id
= REPORT_ID_MOUSE
;
140 if (usbInterruptIsReady3()) {
141 usbSetInterrupt3((void *)report
, sizeof(*report
));
143 debug("Int3 not ready\n");
148 #ifdef USB_EXTRA_ENABLE
149 void host_system_send(uint16_t data
)
152 static uint8_t report
[] = { REPORT_ID_SYSTEM
, 0, 0 };
153 report
[1] = data
&0xFF;
154 report
[2] = (data
>>8)&0xFF;
155 if (usbInterruptIsReady3()) {
156 usbSetInterrupt3((void *)&report
, sizeof(report
));
158 debug("Int3 not ready\n");
162 void host_consumer_send(uint16_t data
)
164 static uint16_t last_data
= 0;
165 if (data
== last_data
) return;
169 static uint8_t report
[] = { REPORT_ID_CONSUMER
, 0, 0 };
170 report
[1] = data
&0xFF;
171 report
[2] = (data
>>8)&0xFF;
172 if (usbInterruptIsReady3()) {
173 usbSetInterrupt3((void *)&report
, sizeof(report
));
175 debug("Int3 not ready\n");
182 /*------------------------------------------------------------------*
183 * Request from host *
184 *------------------------------------------------------------------*/
193 usbMsgLen_t
usbFunctionSetup(uchar data
[8])
195 usbRequest_t
*rq
= (void *)data
;
197 if((rq
->bmRequestType
& USBRQ_TYPE_MASK
) == USBRQ_TYPE_CLASS
){ /* class request type */
198 if(rq
->bRequest
== USBRQ_HID_GET_REPORT
){
199 debug(" GET_REPORT");
200 /* we only have one report type, so don't look at wValue */
201 usbMsgPtr
= (void *)keyboard_report_prev
;
202 return sizeof(*keyboard_report_prev
);
203 }else if(rq
->bRequest
== USBRQ_HID_GET_IDLE
){
204 debug(" GET_IDLE: ");
206 usbMsgPtr
= &idleRate
;
208 }else if(rq
->bRequest
== USBRQ_HID_SET_IDLE
){
209 idleRate
= rq
->wValue
.bytes
[1];
210 debug(" SET_IDLE: ");
212 }else if(rq
->bRequest
== USBRQ_HID_SET_REPORT
){
213 //debug(" SET_REPORT: ");
214 if (rq
->wValue
.word
== 0x0200 && rq
->wIndex
.word
== 0) {
215 last_req
.kind
= SET_LED
;
216 last_req
.len
= rq
->wLength
.word
;
218 return USB_NO_MSG
; // to get data in usbFunctionWrite
223 /* no vendor specific requests implemented */
225 return 0; /* default for not implemented requests: return no data back to host */
228 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
230 if (last_req
.len
== 0) {
233 switch (last_req
.kind
) {
235 //debug("SET_LED\n");
236 keyboard_leds
= data
[0];
250 /*------------------------------------------------------------------*
252 *------------------------------------------------------------------*/
255 * Report Descriptor for keyboard
257 * from an example in HID spec appendix
259 PROGMEM uchar keyboard_hid_report
[] = {
260 0x05, 0x01, // Usage Page (Generic Desktop),
261 0x09, 0x06, // Usage (Keyboard),
262 0xA1, 0x01, // Collection (Application),
263 0x75, 0x01, // Report Size (1),
264 0x95, 0x08, // Report Count (8),
265 0x05, 0x07, // Usage Page (Key Codes),
266 0x19, 0xE0, // Usage Minimum (224),
267 0x29, 0xE7, // Usage Maximum (231),
268 0x15, 0x00, // Logical Minimum (0),
269 0x25, 0x01, // Logical Maximum (1),
270 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
271 0x95, 0x01, // Report Count (1),
272 0x75, 0x08, // Report Size (8),
273 0x81, 0x03, // Input (Constant), ;Reserved byte
274 0x95, 0x05, // Report Count (5),
275 0x75, 0x01, // Report Size (1),
276 0x05, 0x08, // Usage Page (LEDs),
277 0x19, 0x01, // Usage Minimum (1),
278 0x29, 0x05, // Usage Maximum (5),
279 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
280 0x95, 0x01, // Report Count (1),
281 0x75, 0x03, // Report Size (3),
282 0x91, 0x03, // Output (Constant), ;LED report padding
283 0x95, 0x06, // Report Count (6),
284 0x75, 0x08, // Report Size (8),
285 0x15, 0x00, // Logical Minimum (0),
286 0x25, 0xFF, // Logical Maximum(255),
287 0x05, 0x07, // Usage Page (Key Codes),
288 0x19, 0x00, // Usage Minimum (0),
289 0x29, 0xFF, // Usage Maximum (255),
290 0x81, 0x00, // Input (Data, Array),
291 0xc0 // End Collection
295 * Report Descriptor for mouse
297 * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
298 * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
299 * http://www.keil.com/forum/15671/
300 * http://www.microsoft.com/whdc/device/input/wheel.mspx
302 PROGMEM uchar mouse_hid_report
[] = {
304 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
305 0x09, 0x02, // USAGE (Mouse)
306 0xa1, 0x01, // COLLECTION (Application)
307 0x85, REPORT_ID_MOUSE
, // REPORT_ID (1)
308 0x09, 0x01, // USAGE (Pointer)
309 0xa1, 0x00, // COLLECTION (Physical)
310 // ---------------------------- Buttons
311 0x05, 0x09, // USAGE_PAGE (Button)
312 0x19, 0x01, // USAGE_MINIMUM (Button 1)
313 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
314 0x15, 0x00, // LOGICAL_MINIMUM (0)
315 0x25, 0x01, // LOGICAL_MAXIMUM (1)
316 0x75, 0x01, // REPORT_SIZE (1)
317 0x95, 0x05, // REPORT_COUNT (5)
318 0x81, 0x02, // INPUT (Data,Var,Abs)
319 0x75, 0x03, // REPORT_SIZE (3)
320 0x95, 0x01, // REPORT_COUNT (1)
321 0x81, 0x03, // INPUT (Cnst,Var,Abs)
322 // ---------------------------- X,Y position
323 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
324 0x09, 0x30, // USAGE (X)
325 0x09, 0x31, // USAGE (Y)
326 0x15, 0x81, // LOGICAL_MINIMUM (-127)
327 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
328 0x75, 0x08, // REPORT_SIZE (8)
329 0x95, 0x02, // REPORT_COUNT (2)
330 0x81, 0x06, // INPUT (Data,Var,Rel)
331 // ---------------------------- Vertical wheel
332 0x09, 0x38, // USAGE (Wheel)
333 0x15, 0x81, // LOGICAL_MINIMUM (-127)
334 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
335 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
336 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
337 0x75, 0x08, // REPORT_SIZE (8)
338 0x95, 0x01, // REPORT_COUNT (1)
339 0x81, 0x06, // INPUT (Data,Var,Rel)
340 // ---------------------------- Horizontal wheel
341 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
342 0x0a, 0x38, 0x02, // USAGE (AC Pan)
343 0x15, 0x81, // LOGICAL_MINIMUM (-127)
344 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
345 0x75, 0x08, // REPORT_SIZE (8)
346 0x95, 0x01, // REPORT_COUNT (1)
347 0x81, 0x06, // INPUT (Data,Var,Rel)
348 0xc0, // END_COLLECTION
349 0xc0, // END_COLLECTION
351 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
352 0x09, 0x80, // USAGE (System Control)
353 0xa1, 0x01, // COLLECTION (Application)
354 0x85, REPORT_ID_SYSTEM
, // REPORT_ID (2)
355 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
356 0x25, 0xb7, // LOGICAL_MAXIMUM (0xb7)
357 0x19, 0x01, // USAGE_MINIMUM (0x1)
358 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
359 0x75, 0x10, // REPORT_SIZE (16)
360 0x95, 0x01, // REPORT_COUNT (1)
361 0x81, 0x00, // INPUT (Data,Array,Abs)
362 0xc0, // END_COLLECTION
364 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
365 0x09, 0x01, // USAGE (Consumer Control)
366 0xa1, 0x01, // COLLECTION (Application)
367 0x85, REPORT_ID_CONSUMER
, // REPORT_ID (3)
368 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
369 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
370 0x19, 0x01, // USAGE_MINIMUM (0x1)
371 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
372 0x75, 0x10, // REPORT_SIZE (16)
373 0x95, 0x01, // REPORT_COUNT (1)
374 0x81, 0x00, // INPUT (Data,Array,Abs)
375 0xc0, // END_COLLECTION
380 * Descriptor for compite device: Keyboard + Mouse
382 * contains: device, interface, HID and endpoint descriptors
384 #if USB_CFG_DESCR_PROPS_CONFIGURATION
385 PROGMEM
char usbDescriptorConfiguration
[] = { /* USB configuration descriptor */
386 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
387 USBDESCR_CONFIG
, /* descriptor type */
388 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
389 //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
390 /* total length of data returned (including inlined descriptors) */
391 2, /* number of interfaces in this configuration */
392 1, /* index of this configuration */
393 0, /* configuration name string index */
394 #if USB_CFG_IS_SELF_POWERED
395 (1 << 7) | USBATTR_SELFPOWER
, /* attributes */
397 (1 << 7), /* attributes */
399 USB_CFG_MAX_BUS_POWER
/2, /* max USB current in 2mA units */
404 /* Interface descriptor */
405 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
406 USBDESCR_INTERFACE
, /* descriptor type */
407 0, /* index of this interface */
408 0, /* alternate setting for this interface */
409 USB_CFG_HAVE_INTRIN_ENDPOINT
, /* endpoints excl 0: number of endpoint descriptors to follow */
410 USB_CFG_INTERFACE_CLASS
,
411 USB_CFG_INTERFACE_SUBCLASS
,
412 USB_CFG_INTERFACE_PROTOCOL
,
413 0, /* string index for interface */
415 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
416 USBDESCR_HID
, /* descriptor type: HID */
417 0x01, 0x01, /* BCD representation of HID version */
418 0x00, /* target country code */
419 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
420 0x22, /* descriptor type: report */
421 sizeof(keyboard_hid_report
), 0, /* total length of report descriptor */
422 /* Endpoint descriptor */
423 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
424 7, /* sizeof(usbDescrEndpoint) */
425 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
426 (char)0x81, /* IN endpoint number 1 */
427 0x03, /* attrib: Interrupt endpoint */
428 8, 0, /* maximum packet size */
429 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
435 /* Interface descriptor */
436 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
437 USBDESCR_INTERFACE
, /* descriptor type */
438 1, /* index of this interface */
439 0, /* alternate setting for this interface */
440 USB_CFG_HAVE_INTRIN_ENDPOINT3
, /* endpoints excl 0: number of endpoint descriptors to follow */
441 0x03, /* CLASS: HID */
442 0, /* SUBCLASS: none */
443 0, /* PROTOCOL: none */
444 0, /* string index for interface */
446 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
447 USBDESCR_HID
, /* descriptor type: HID */
448 0x01, 0x01, /* BCD representation of HID version */
449 0x00, /* target country code */
450 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
451 0x22, /* descriptor type: report */
452 sizeof(mouse_hid_report
), 0, /* total length of report descriptor */
453 #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
454 /* Endpoint descriptor */
455 7, /* sizeof(usbDescrEndpoint) */
456 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
457 (char)(0x80 | USB_CFG_EP3_NUMBER
), /* IN endpoint number 3 */
458 0x03, /* attrib: Interrupt endpoint */
459 8, 0, /* maximum packet size */
460 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
466 USB_PUBLIC usbMsgLen_t
usbFunctionDescriptor(struct usbRequest
*rq
)
470 debug("usbFunctionDescriptor: ");
471 debug_hex(rq
->bmRequestType
); debug(" ");
472 debug_hex(rq
->bRequest
); debug(" ");
473 debug_hex16(rq
->wValue
.word
); debug(" ");
474 debug_hex16(rq
->wIndex
.word
); debug(" ");
475 debug_hex16(rq
->wLength
.word
); debug("\n");
477 switch (rq
->wValue
.bytes
[1]) {
478 #if USB_CFG_DESCR_PROPS_CONFIGURATION
479 case USBDESCR_CONFIG
:
480 usbMsgPtr
= (unsigned char *)usbDescriptorConfiguration
;
481 len
= sizeof(usbDescriptorConfiguration
);
485 usbMsgPtr
= (unsigned char *)(usbDescriptorConfiguration
+ 18);
488 case USBDESCR_HID_REPORT
:
489 /* interface index */
490 switch (rq
->wIndex
.word
) {
492 usbMsgPtr
= keyboard_hid_report
;
493 len
= sizeof(keyboard_hid_report
);
496 usbMsgPtr
= mouse_hid_report
;
497 len
= sizeof(mouse_hid_report
);
502 debug("desc len: "); debug_hex(len
); debug("\n");