2 Copyright 2011 Jun Wako <wakojun@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <avr/interrupt.h>
21 #include "usbconfig.h"
23 #include "usb_keycodes.h"
25 #include "host_vusb.h"
29 static report_keyboard_t report0
;
30 static report_keyboard_t report1
;
31 report_keyboard_t
*keyboard_report
= &report0
;
32 report_keyboard_t
*keyboard_report_prev
= &report1
;
34 static uint8_t keyboard_leds
= 0;
35 static uchar idleRate
= 0;
37 uint8_t host_keyboard_leds(void)
43 /*------------------------------------------------------------------*
44 * Keyboard report operations
45 *------------------------------------------------------------------*/
46 void host_add_key(uint8_t code
)
50 for (; i
< REPORT_KEYS
; i
++) {
51 if (keyboard_report_prev
->keys
[i
] == code
) {
52 keyboard_report
->keys
[i
] = code
;
55 if (empty
== -1 && keyboard_report_prev
->keys
[i
] == KB_NO
&& keyboard_report
->keys
[i
] == KB_NO
) {
59 if (i
== REPORT_KEYS
&& empty
!= -1) {
60 keyboard_report
->keys
[empty
] = code
;
64 void host_add_mod_bit(uint8_t mod
)
66 keyboard_report
->mods
|= mod
;
69 void host_set_mods(uint8_t mods
)
71 keyboard_report
->mods
= mods
;
74 void host_add_code(uint8_t code
)
77 host_add_mod_bit(MOD_BIT(code
));
83 void host_swap_keyboard_report(void)
87 report_keyboard_t
*tmp
= keyboard_report_prev
;
88 keyboard_report_prev
= keyboard_report
;
89 keyboard_report
= tmp
;
93 void host_clear_keyboard_report(void)
95 keyboard_report
->mods
= 0;
96 for (int8_t i
= 0; i
< REPORT_KEYS
; i
++) {
97 keyboard_report
->keys
[i
] = 0;
101 uint8_t host_has_anykey(void)
104 for (int i
= 0; i
< REPORT_KEYS
; i
++) {
105 if (keyboard_report
->keys
[i
])
111 uint8_t host_get_first_key(void)
113 #ifdef USB_NKRO_ENABLE
116 for (; i
< REPORT_KEYS
&& !keyboard_report
->keys
[i
]; i
++)
118 return i
<<3 | biton(keyboard_report
->keys
[i
]);
121 return keyboard_report
->keys
[0];
125 /*------------------------------------------------------------------*
126 * Keyboard report send buffer
127 *------------------------------------------------------------------*/
129 static report_keyboard_t kbuf
[KBUF_SIZE
];
130 static uint8_t kbuf_head
= 0;
131 static uint8_t kbuf_tail
= 0;
133 void host_vusb_keyboard_send(void)
135 if (usbInterruptIsReady() && kbuf_head
!= kbuf_tail
) {
136 usbSetInterrupt((void *)&kbuf
[kbuf_tail
], sizeof(report_keyboard_t
));
137 kbuf_tail
= (kbuf_tail
+ 1) % KBUF_SIZE
;
141 void host_send_keyboard_report(void)
143 uint8_t next
= (kbuf_head
+ 1) % KBUF_SIZE
;
144 if (next
!= kbuf_tail
) {
145 kbuf
[kbuf_head
] = *keyboard_report
;
148 debug("kbuf: full\n");
153 #if defined(MOUSEKEY_ENABLE) || defined(PS2_MOUSE_ENABLE)
154 void host_mouse_send(report_mouse_t
*report
)
156 report
->report_id
= REPORT_ID_MOUSE
;
157 if (usbInterruptIsReady3()) {
158 usbSetInterrupt3((void *)report
, sizeof(*report
));
160 debug("Int3 not ready\n");
165 #ifdef USB_EXTRA_ENABLE
166 void host_system_send(uint16_t data
)
169 static uint8_t report
[] = { REPORT_ID_SYSTEM
, 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");
179 void host_consumer_send(uint16_t data
)
181 static uint16_t last_data
= 0;
182 if (data
== last_data
) return;
186 static uint8_t report
[] = { REPORT_ID_CONSUMER
, 0, 0 };
187 report
[1] = data
&0xFF;
188 report
[2] = (data
>>8)&0xFF;
189 if (usbInterruptIsReady3()) {
190 usbSetInterrupt3((void *)&report
, sizeof(report
));
192 debug("Int3 not ready\n");
199 /*------------------------------------------------------------------*
200 * Request from host *
201 *------------------------------------------------------------------*/
210 usbMsgLen_t
usbFunctionSetup(uchar data
[8])
212 usbRequest_t
*rq
= (void *)data
;
214 if((rq
->bmRequestType
& USBRQ_TYPE_MASK
) == USBRQ_TYPE_CLASS
){ /* class request type */
215 if(rq
->bRequest
== USBRQ_HID_GET_REPORT
){
216 debug(" GET_REPORT");
217 /* we only have one report type, so don't look at wValue */
218 usbMsgPtr
= (void *)keyboard_report_prev
;
219 return sizeof(*keyboard_report_prev
);
220 }else if(rq
->bRequest
== USBRQ_HID_GET_IDLE
){
221 debug(" GET_IDLE: ");
223 usbMsgPtr
= &idleRate
;
225 }else if(rq
->bRequest
== USBRQ_HID_SET_IDLE
){
226 idleRate
= rq
->wValue
.bytes
[1];
227 debug(" SET_IDLE: ");
229 }else if(rq
->bRequest
== USBRQ_HID_SET_REPORT
){
230 //debug(" SET_REPORT: ");
231 if (rq
->wValue
.word
== 0x0200 && rq
->wIndex
.word
== 0) {
232 last_req
.kind
= SET_LED
;
233 last_req
.len
= rq
->wLength
.word
;
235 return USB_NO_MSG
; // to get data in usbFunctionWrite
240 /* no vendor specific requests implemented */
242 return 0; /* default for not implemented requests: return no data back to host */
245 uchar
usbFunctionWrite(uchar
*data
, uchar len
)
247 if (last_req
.len
== 0) {
250 switch (last_req
.kind
) {
252 //debug("SET_LED\n");
253 keyboard_leds
= data
[0];
267 /*------------------------------------------------------------------*
269 *------------------------------------------------------------------*/
272 * Report Descriptor for keyboard
274 * from an example in HID spec appendix
276 PROGMEM uchar keyboard_hid_report
[] = {
277 0x05, 0x01, // Usage Page (Generic Desktop),
278 0x09, 0x06, // Usage (Keyboard),
279 0xA1, 0x01, // Collection (Application),
280 0x75, 0x01, // Report Size (1),
281 0x95, 0x08, // Report Count (8),
282 0x05, 0x07, // Usage Page (Key Codes),
283 0x19, 0xE0, // Usage Minimum (224),
284 0x29, 0xE7, // Usage Maximum (231),
285 0x15, 0x00, // Logical Minimum (0),
286 0x25, 0x01, // Logical Maximum (1),
287 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
288 0x95, 0x01, // Report Count (1),
289 0x75, 0x08, // Report Size (8),
290 0x81, 0x03, // Input (Constant), ;Reserved byte
291 0x95, 0x05, // Report Count (5),
292 0x75, 0x01, // Report Size (1),
293 0x05, 0x08, // Usage Page (LEDs),
294 0x19, 0x01, // Usage Minimum (1),
295 0x29, 0x05, // Usage Maximum (5),
296 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
297 0x95, 0x01, // Report Count (1),
298 0x75, 0x03, // Report Size (3),
299 0x91, 0x03, // Output (Constant), ;LED report padding
300 0x95, 0x06, // Report Count (6),
301 0x75, 0x08, // Report Size (8),
302 0x15, 0x00, // Logical Minimum (0),
303 0x25, 0xFF, // Logical Maximum(255),
304 0x05, 0x07, // Usage Page (Key Codes),
305 0x19, 0x00, // Usage Minimum (0),
306 0x29, 0xFF, // Usage Maximum (255),
307 0x81, 0x00, // Input (Data, Array),
308 0xc0 // End Collection
312 * Report Descriptor for mouse
314 * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
315 * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
316 * http://www.keil.com/forum/15671/
317 * http://www.microsoft.com/whdc/device/input/wheel.mspx
319 PROGMEM uchar mouse_hid_report
[] = {
321 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
322 0x09, 0x02, // USAGE (Mouse)
323 0xa1, 0x01, // COLLECTION (Application)
324 0x85, REPORT_ID_MOUSE
, // REPORT_ID (1)
325 0x09, 0x01, // USAGE (Pointer)
326 0xa1, 0x00, // COLLECTION (Physical)
327 // ---------------------------- Buttons
328 0x05, 0x09, // USAGE_PAGE (Button)
329 0x19, 0x01, // USAGE_MINIMUM (Button 1)
330 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
331 0x15, 0x00, // LOGICAL_MINIMUM (0)
332 0x25, 0x01, // LOGICAL_MAXIMUM (1)
333 0x75, 0x01, // REPORT_SIZE (1)
334 0x95, 0x05, // REPORT_COUNT (5)
335 0x81, 0x02, // INPUT (Data,Var,Abs)
336 0x75, 0x03, // REPORT_SIZE (3)
337 0x95, 0x01, // REPORT_COUNT (1)
338 0x81, 0x03, // INPUT (Cnst,Var,Abs)
339 // ---------------------------- X,Y position
340 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
341 0x09, 0x30, // USAGE (X)
342 0x09, 0x31, // USAGE (Y)
343 0x15, 0x81, // LOGICAL_MINIMUM (-127)
344 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
345 0x75, 0x08, // REPORT_SIZE (8)
346 0x95, 0x02, // REPORT_COUNT (2)
347 0x81, 0x06, // INPUT (Data,Var,Rel)
348 // ---------------------------- Vertical wheel
349 0x09, 0x38, // USAGE (Wheel)
350 0x15, 0x81, // LOGICAL_MINIMUM (-127)
351 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
352 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
353 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
354 0x75, 0x08, // REPORT_SIZE (8)
355 0x95, 0x01, // REPORT_COUNT (1)
356 0x81, 0x06, // INPUT (Data,Var,Rel)
357 // ---------------------------- Horizontal wheel
358 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
359 0x0a, 0x38, 0x02, // USAGE (AC Pan)
360 0x15, 0x81, // LOGICAL_MINIMUM (-127)
361 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
362 0x75, 0x08, // REPORT_SIZE (8)
363 0x95, 0x01, // REPORT_COUNT (1)
364 0x81, 0x06, // INPUT (Data,Var,Rel)
365 0xc0, // END_COLLECTION
366 0xc0, // END_COLLECTION
368 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
369 0x09, 0x80, // USAGE (System Control)
370 0xa1, 0x01, // COLLECTION (Application)
371 0x85, REPORT_ID_SYSTEM
, // REPORT_ID (2)
372 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
373 0x25, 0xb7, // LOGICAL_MAXIMUM (0xb7)
374 0x19, 0x01, // USAGE_MINIMUM (0x1)
375 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
376 0x75, 0x10, // REPORT_SIZE (16)
377 0x95, 0x01, // REPORT_COUNT (1)
378 0x81, 0x00, // INPUT (Data,Array,Abs)
379 0xc0, // END_COLLECTION
381 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
382 0x09, 0x01, // USAGE (Consumer Control)
383 0xa1, 0x01, // COLLECTION (Application)
384 0x85, REPORT_ID_CONSUMER
, // REPORT_ID (3)
385 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
386 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
387 0x19, 0x01, // USAGE_MINIMUM (0x1)
388 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
389 0x75, 0x10, // REPORT_SIZE (16)
390 0x95, 0x01, // REPORT_COUNT (1)
391 0x81, 0x00, // INPUT (Data,Array,Abs)
392 0xc0, // END_COLLECTION
397 * Descriptor for compite device: Keyboard + Mouse
399 * contains: device, interface, HID and endpoint descriptors
401 #if USB_CFG_DESCR_PROPS_CONFIGURATION
402 PROGMEM
char usbDescriptorConfiguration
[] = { /* USB configuration descriptor */
403 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
404 USBDESCR_CONFIG
, /* descriptor type */
405 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
406 //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
407 /* total length of data returned (including inlined descriptors) */
408 2, /* number of interfaces in this configuration */
409 1, /* index of this configuration */
410 0, /* configuration name string index */
411 #if USB_CFG_IS_SELF_POWERED
412 (1 << 7) | USBATTR_SELFPOWER
, /* attributes */
414 (1 << 7), /* attributes */
416 USB_CFG_MAX_BUS_POWER
/2, /* max USB current in 2mA units */
421 /* Interface descriptor */
422 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
423 USBDESCR_INTERFACE
, /* descriptor type */
424 0, /* index of this interface */
425 0, /* alternate setting for this interface */
426 USB_CFG_HAVE_INTRIN_ENDPOINT
, /* endpoints excl 0: number of endpoint descriptors to follow */
427 USB_CFG_INTERFACE_CLASS
,
428 USB_CFG_INTERFACE_SUBCLASS
,
429 USB_CFG_INTERFACE_PROTOCOL
,
430 0, /* string index for interface */
432 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
433 USBDESCR_HID
, /* descriptor type: HID */
434 0x01, 0x01, /* BCD representation of HID version */
435 0x00, /* target country code */
436 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
437 0x22, /* descriptor type: report */
438 sizeof(keyboard_hid_report
), 0, /* total length of report descriptor */
439 /* Endpoint descriptor */
440 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
441 7, /* sizeof(usbDescrEndpoint) */
442 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
443 (char)0x81, /* IN endpoint number 1 */
444 0x03, /* attrib: Interrupt endpoint */
445 8, 0, /* maximum packet size */
446 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
452 /* Interface descriptor */
453 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
454 USBDESCR_INTERFACE
, /* descriptor type */
455 1, /* index of this interface */
456 0, /* alternate setting for this interface */
457 USB_CFG_HAVE_INTRIN_ENDPOINT3
, /* endpoints excl 0: number of endpoint descriptors to follow */
458 0x03, /* CLASS: HID */
459 0, /* SUBCLASS: none */
460 0, /* PROTOCOL: none */
461 0, /* string index for interface */
463 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
464 USBDESCR_HID
, /* descriptor type: HID */
465 0x01, 0x01, /* BCD representation of HID version */
466 0x00, /* target country code */
467 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
468 0x22, /* descriptor type: report */
469 sizeof(mouse_hid_report
), 0, /* total length of report descriptor */
470 #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
471 /* Endpoint descriptor */
472 7, /* sizeof(usbDescrEndpoint) */
473 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
474 (char)(0x80 | USB_CFG_EP3_NUMBER
), /* IN endpoint number 3 */
475 0x03, /* attrib: Interrupt endpoint */
476 8, 0, /* maximum packet size */
477 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
483 USB_PUBLIC usbMsgLen_t
usbFunctionDescriptor(struct usbRequest
*rq
)
487 debug("usbFunctionDescriptor: ");
488 debug_hex(rq
->bmRequestType
); debug(" ");
489 debug_hex(rq
->bRequest
); debug(" ");
490 debug_hex16(rq
->wValue
.word
); debug(" ");
491 debug_hex16(rq
->wIndex
.word
); debug(" ");
492 debug_hex16(rq
->wLength
.word
); debug("\n");
494 switch (rq
->wValue
.bytes
[1]) {
495 #if USB_CFG_DESCR_PROPS_CONFIGURATION
496 case USBDESCR_CONFIG
:
497 usbMsgPtr
= (unsigned char *)usbDescriptorConfiguration
;
498 len
= sizeof(usbDescriptorConfiguration
);
502 usbMsgPtr
= (unsigned char *)(usbDescriptorConfiguration
+ 18);
505 case USBDESCR_HID_REPORT
:
506 /* interface index */
507 switch (rq
->wIndex
.word
) {
509 usbMsgPtr
= keyboard_hid_report
;
510 len
= sizeof(keyboard_hid_report
);
513 usbMsgPtr
= mouse_hid_report
;
514 len
= sizeof(mouse_hid_report
);
519 debug("desc len: "); debug_hex(len
); debug("\n");