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 while (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(uint8_t data
)
151 static uint8_t report
[] = { REPORT_ID_SYSTEM
, 0 };
153 if (usbInterruptIsReady3()) {
154 usbSetInterrupt3((void *)&report
, sizeof(report
));
156 debug("Int3 not ready\n");
160 void host_audio_send(uint8_t data
)
162 static uint8_t report
[] = { REPORT_ID_AUDIO
, 0 };
164 if (usbInterruptIsReady3()) {
165 usbSetInterrupt3((void *)&report
, sizeof(report
));
167 debug("Int3 not ready\n");
174 /*------------------------------------------------------------------*
175 * Request from host *
176 *------------------------------------------------------------------*/
185 usbMsgLen_t
usbFunctionSetup(uchar data
[8])
187 usbRequest_t
*rq
= (void *)data
;
189 if((rq
->bmRequestType
& USBRQ_TYPE_MASK
) == USBRQ_TYPE_CLASS
){ /* class request type */
190 if(rq
->bRequest
== USBRQ_HID_GET_REPORT
){
191 debug(" GET_REPORT");
192 /* we only have one report type, so don't look at wValue */
193 usbMsgPtr
= (void *)keyboard_report_prev
;
194 return sizeof(*keyboard_report_prev
);
195 }else if(rq
->bRequest
== USBRQ_HID_GET_IDLE
){
196 debug(" GET_IDLE: ");
198 usbMsgPtr
= &idleRate
;
200 }else if(rq
->bRequest
== USBRQ_HID_SET_IDLE
){
201 idleRate
= rq
->wValue
.bytes
[1];
202 debug(" SET_IDLE: ");
204 }else if(rq
->bRequest
== USBRQ_HID_SET_REPORT
){
205 //debug(" SET_REPORT: ");
206 if (rq
->wValue
.word
== 0x0200 && rq
->wIndex
.word
== 0) {
207 last_req
.kind
= SET_LED
;
208 last_req
.len
= rq
->wLength
.word
;
210 return USB_NO_MSG
; // to get data in usbFunctionWrite
215 /* no vendor specific requests implemented */
217 return 0; /* default for not implemented requests: return no data back to host */
220 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
222 if (last_req
.len
== 0) {
225 switch (last_req
.kind
) {
227 //debug("SET_LED\n");
228 keyboard_leds
= data
[0];
242 /*------------------------------------------------------------------*
244 *------------------------------------------------------------------*/
247 * Report Descriptor for keyboard
249 * from an example in HID spec appendix
251 PROGMEM uchar keyboard_hid_report
[] = {
252 0x05, 0x01, // Usage Page (Generic Desktop),
253 0x09, 0x06, // Usage (Keyboard),
254 0xA1, 0x01, // Collection (Application),
255 0x75, 0x01, // Report Size (1),
256 0x95, 0x08, // Report Count (8),
257 0x05, 0x07, // Usage Page (Key Codes),
258 0x19, 0xE0, // Usage Minimum (224),
259 0x29, 0xE7, // Usage Maximum (231),
260 0x15, 0x00, // Logical Minimum (0),
261 0x25, 0x01, // Logical Maximum (1),
262 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
263 0x95, 0x01, // Report Count (1),
264 0x75, 0x08, // Report Size (8),
265 0x81, 0x03, // Input (Constant), ;Reserved byte
266 0x95, 0x05, // Report Count (5),
267 0x75, 0x01, // Report Size (1),
268 0x05, 0x08, // Usage Page (LEDs),
269 0x19, 0x01, // Usage Minimum (1),
270 0x29, 0x05, // Usage Maximum (5),
271 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
272 0x95, 0x01, // Report Count (1),
273 0x75, 0x03, // Report Size (3),
274 0x91, 0x03, // Output (Constant), ;LED report padding
275 0x95, 0x06, // Report Count (6),
276 0x75, 0x08, // Report Size (8),
277 0x15, 0x00, // Logical Minimum (0),
278 0x25, 0xFF, // Logical Maximum(255),
279 0x05, 0x07, // Usage Page (Key Codes),
280 0x19, 0x00, // Usage Minimum (0),
281 0x29, 0xFF, // Usage Maximum (255),
282 0x81, 0x00, // Input (Data, Array),
283 0xc0 // End Collection
287 * Report Descriptor for mouse
289 * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
290 * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
291 * http://www.keil.com/forum/15671/
292 * http://www.microsoft.com/whdc/device/input/wheel.mspx
294 PROGMEM uchar mouse_hid_report
[] = {
296 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
297 0x09, 0x02, // USAGE (Mouse)
298 0xa1, 0x01, // COLLECTION (Application)
299 0x85, 0x01, // REPORT_ID (1)
300 0x09, 0x01, // USAGE (Pointer)
301 0xa1, 0x00, // COLLECTION (Physical)
302 // ---------------------------- Buttons
303 0x05, 0x09, // USAGE_PAGE (Button)
304 0x19, 0x01, // USAGE_MINIMUM (Button 1)
305 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
306 0x15, 0x00, // LOGICAL_MINIMUM (0)
307 0x25, 0x01, // LOGICAL_MAXIMUM (1)
308 0x75, 0x01, // REPORT_SIZE (1)
309 0x95, 0x05, // REPORT_COUNT (5)
310 0x81, 0x02, // INPUT (Data,Var,Abs)
311 // ---------------------------- Padding
312 0x75, 0x03, // REPORT_SIZE (3)
313 0x95, 0x01, // REPORT_COUNT (1)
314 0x81, 0x03, // INPUT (Cnst,Var,Abs)
315 // ---------------------------- X,Y position
316 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
317 0x09, 0x30, // USAGE (X)
318 0x09, 0x31, // USAGE (Y)
319 0x15, 0x81, // LOGICAL_MINIMUM (-127)
320 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
321 0x75, 0x08, // REPORT_SIZE (8)
322 0x95, 0x02, // REPORT_COUNT (2)
323 0x81, 0x06, // INPUT (Data,Var,Rel)
324 // ---------------------------- Vertical wheel
325 0x09, 0x38, // USAGE (Wheel)
326 0x15, 0x81, // LOGICAL_MINIMUM (-127)
327 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
328 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
329 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
330 0x75, 0x08, // REPORT_SIZE (8)
331 0x95, 0x01, // REPORT_COUNT (1)
332 0x81, 0x06, // INPUT (Data,Var,Rel)
333 // ---------------------------- Horizontal wheel
334 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
335 0x0a, 0x38, 0x02, // USAGE (AC Pan)
336 0x15, 0x81, // LOGICAL_MINIMUM (-127)
337 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
338 0x75, 0x08, // REPORT_SIZE (8)
339 0x95, 0x01, // REPORT_COUNT (1)
340 0x81, 0x06, // INPUT (Data,Var,Rel)
341 0xc0, // END_COLLECTION
342 0xc0, // END_COLLECTION
344 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
345 0x09, 0x80, // USAGE (System Control)
346 0xa1, 0x01, // COLLECTION (Application)
347 0x85, 0x02, // REPORT_ID (2)
348 0x19, 0x81, // USAGE_MINIMUM (System Power Down)
349 0x29, 0x83, // USAGE_MAXIMUM (System Wake Up)
350 0x75, 0x01, // REPORT_SIZE (1)
351 0x95, 0x03, // REPORT_COUNT (3)
352 0x81, 0x06, // INPUT (Data,Var,Rel)
353 0x95, 0x05, // REPORT_COUNT (5)
354 0x81, 0x07, // INPUT (Cnst,Var,Rel)
355 0xc0, // END_COLLECTION
357 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
358 0x09, 0x01, // USAGE (Consumer Control)
359 0xa1, 0x01, // COLLECTION (Application)
360 0x85, 0x03, // REPORT_ID (3)
361 0x09, 0xe9, // USAGE (Volume Up)
362 0x09, 0xea, // USAGE (Volume Down)
363 0x15, 0x00, // LOGICAL_MINIMUM (0)
364 0x25, 0x01, // LOGICAL_MAXIMUM (1)
365 0x75, 0x01, // REPORT_SIZE (1)
366 0x95, 0x02, // REPORT_COUNT (2)
367 0x81, 0x02, // INPUT (Data,Var,Abs)
368 0x09, 0xe2, // USAGE (Mute)
369 0x15, 0x00, // LOGICAL_MINIMUM (0)
370 0x25, 0x01, // LOGICAL_MAXIMUM (1)
371 0x95, 0x01, // REPORT_COUNT (1)
372 0x81, 0x06, // INPUT (Data,Var,Rel)
373 0x95, 0x05, // REPORT_COUNT (5)
374 0x81, 0x07, // INPUT (Cnst,Var,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");