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/>.
18 #include <avr/eeprom.h>
22 #include "usbconfig.h"
27 #include "host_driver.h"
29 #include "bootloader.h"
30 #include <util/delay.h>
33 static uint8_t vusb_keyboard_leds = 0;
34 static uint8_t vusb_idle_rate = 0;
36 /* Keyboard report send buffer */
38 static report_keyboard_t kbuf[KBUF_SIZE];
39 static uint8_t kbuf_head = 0;
40 static uint8_t kbuf_tail = 0;
48 static keyboard_report_t keyboard_report; // sent to PC
50 #define VUSB_TRANSFER_KEYBOARD_MAX_TRIES 10
52 /* transfer keyboard report from buffer */
53 void vusb_transfer_keyboard(void)
55 for (int i = 0; i < VUSB_TRANSFER_KEYBOARD_MAX_TRIES; i++) {
56 if (usbInterruptIsReady()) {
57 if (kbuf_head != kbuf_tail) {
58 usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
59 kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
61 print("V-USB: kbuf["); pdec(kbuf_tail); print("->"); pdec(kbuf_head); print("](");
62 phex((kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail));
74 /*------------------------------------------------------------------*
76 *------------------------------------------------------------------*/
77 static uint8_t keyboard_leds(void);
78 static void send_keyboard(report_keyboard_t *report);
79 static void send_mouse(report_mouse_t *report);
80 static void send_system(uint16_t data);
81 static void send_consumer(uint16_t data);
83 static host_driver_t driver = {
91 host_driver_t *vusb_driver(void)
96 static uint8_t keyboard_leds(void) {
97 return vusb_keyboard_leds;
100 static void send_keyboard(report_keyboard_t *report)
102 uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
103 if (next != kbuf_tail) {
104 kbuf[kbuf_head] = *report;
107 debug("kbuf: full\n");
110 // NOTE: send key strokes of Macro
112 vusb_transfer_keyboard();
118 report_mouse_t report;
119 } __attribute__ ((packed)) vusb_mouse_report_t;
121 static void send_mouse(report_mouse_t *report)
123 vusb_mouse_report_t r = {
124 .report_id = REPORT_ID_MOUSE,
127 if (usbInterruptIsReady3()) {
128 usbSetInterrupt3((void *)&r, sizeof(vusb_mouse_report_t));
136 } __attribute__ ((packed)) report_extra_t;
138 static void send_system(uint16_t data)
140 static uint16_t last_data = 0;
141 if (data == last_data) return;
144 report_extra_t report = {
145 .report_id = REPORT_ID_SYSTEM,
148 if (usbInterruptIsReady3()) {
149 usbSetInterrupt3((void *)&report, sizeof(report));
153 static void send_consumer(uint16_t data)
155 static uint16_t last_data = 0;
156 if (data == last_data) return;
159 report_extra_t report = {
160 .report_id = REPORT_ID_CONSUMER,
163 if (usbInterruptIsReady3()) {
164 usbSetInterrupt3((void *)&report, sizeof(report));
170 /*------------------------------------------------------------------*
171 * Request from host *
172 *------------------------------------------------------------------*/
182 usbMsgLen_t usbFunctionSetup(uchar data[8])
184 usbRequest_t *rq = (void *)data;
186 if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
187 if(rq->bRequest == USBRQ_HID_GET_REPORT){
188 debug("GET_REPORT:");
189 /* we only have one report type, so don't look at wValue */
190 usbMsgPtr = (void *)&keyboard_report;
191 return sizeof(keyboard_report);
192 }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
194 //debug_hex(vusb_idle_rate);
195 usbMsgPtr = &vusb_idle_rate;
197 }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
198 vusb_idle_rate = rq->wValue.bytes[1];
200 debug_hex(vusb_idle_rate);
201 }else if(rq->bRequest == USBRQ_HID_SET_REPORT){
202 debug("SET_REPORT: ");
203 // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
204 if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
206 last_req.kind = SET_LED;
207 last_req.len = rq->wLength.word;
208 #ifdef BOOTLOADER_SIZE
209 } else if(rq->wValue.word == 0x0301) {
210 last_req.kind = BOOTLOADER;
211 last_req.len = rq->wLength.word;
214 return USB_NO_MSG; // to get data in usbFunctionWrite
220 /* no vendor specific requests implemented */
223 return 0; /* default for not implemented requests: return no data back to host */
226 uchar usbFunctionWrite(uchar *data, uchar len)
228 if (last_req.len == 0) {
231 switch (last_req.kind) {
236 vusb_keyboard_leds = data[0];
241 usbDeviceDisconnect();
255 /*------------------------------------------------------------------*
257 *------------------------------------------------------------------*/
260 * Report Descriptor for keyboard
262 * from an example in HID spec appendix
264 const PROGMEM uchar keyboard_hid_report[] = {
265 0x05, 0x01, // Usage Page (Generic Desktop),
266 0x09, 0x06, // Usage (Keyboard),
267 0xA1, 0x01, // Collection (Application),
268 0x75, 0x01, // Report Size (1),
269 0x95, 0x08, // Report Count (8),
270 0x05, 0x07, // Usage Page (Key Codes),
271 0x19, 0xE0, // Usage Minimum (224),
272 0x29, 0xE7, // Usage Maximum (231),
273 0x15, 0x00, // Logical Minimum (0),
274 0x25, 0x01, // Logical Maximum (1),
275 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
276 0x95, 0x01, // Report Count (1),
277 0x75, 0x08, // Report Size (8),
278 0x81, 0x03, // Input (Constant), ;Reserved byte
279 0x95, 0x05, // Report Count (5),
280 0x75, 0x01, // Report Size (1),
281 0x05, 0x08, // Usage Page (LEDs),
282 0x19, 0x01, // Usage Minimum (1),
283 0x29, 0x05, // Usage Maximum (5),
284 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
285 0x95, 0x01, // Report Count (1),
286 0x75, 0x03, // Report Size (3),
287 0x91, 0x03, // Output (Constant), ;LED report padding
288 0x95, 0x06, // Report Count (6),
289 0x75, 0x08, // Report Size (8),
290 0x15, 0x00, // Logical Minimum (0),
291 0x26, 0xFF, 0x00, // Logical Maximum(255),
292 0x05, 0x07, // Usage Page (Key Codes),
293 0x19, 0x00, // Usage Minimum (0),
294 0x29, 0xFF, // Usage Maximum (255),
295 0x81, 0x00, // Input (Data, Array),
296 0xc0 // End Collection
300 * Report Descriptor for mouse
302 * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
303 * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
304 * http://www.keil.com/forum/15671/
305 * http://www.microsoft.com/whdc/device/input/wheel.mspx
307 const PROGMEM uchar mouse_hid_report[] = {
309 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
310 0x09, 0x02, // USAGE (Mouse)
311 0xa1, 0x01, // COLLECTION (Application)
312 0x85, REPORT_ID_MOUSE, // REPORT_ID (1)
313 0x09, 0x01, // USAGE (Pointer)
314 0xa1, 0x00, // COLLECTION (Physical)
315 // ---------------------------- Buttons
316 0x05, 0x09, // USAGE_PAGE (Button)
317 0x19, 0x01, // USAGE_MINIMUM (Button 1)
318 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
319 0x15, 0x00, // LOGICAL_MINIMUM (0)
320 0x25, 0x01, // LOGICAL_MAXIMUM (1)
321 0x75, 0x01, // REPORT_SIZE (1)
322 0x95, 0x05, // REPORT_COUNT (5)
323 0x81, 0x02, // INPUT (Data,Var,Abs)
324 0x75, 0x03, // REPORT_SIZE (3)
325 0x95, 0x01, // REPORT_COUNT (1)
326 0x81, 0x03, // INPUT (Cnst,Var,Abs)
327 // ---------------------------- X,Y position
328 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
329 0x09, 0x30, // USAGE (X)
330 0x09, 0x31, // USAGE (Y)
331 0x15, 0x81, // LOGICAL_MINIMUM (-127)
332 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
333 0x75, 0x08, // REPORT_SIZE (8)
334 0x95, 0x02, // REPORT_COUNT (2)
335 0x81, 0x06, // INPUT (Data,Var,Rel)
336 // ---------------------------- Vertical wheel
337 0x09, 0x38, // USAGE (Wheel)
338 0x15, 0x81, // LOGICAL_MINIMUM (-127)
339 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
340 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
341 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
342 0x75, 0x08, // REPORT_SIZE (8)
343 0x95, 0x01, // REPORT_COUNT (1)
344 0x81, 0x06, // INPUT (Data,Var,Rel)
345 // ---------------------------- Horizontal wheel
346 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
347 0x0a, 0x38, 0x02, // USAGE (AC Pan)
348 0x15, 0x81, // LOGICAL_MINIMUM (-127)
349 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
350 0x75, 0x08, // REPORT_SIZE (8)
351 0x95, 0x01, // REPORT_COUNT (1)
352 0x81, 0x06, // INPUT (Data,Var,Rel)
353 0xc0, // END_COLLECTION
354 0xc0, // END_COLLECTION
356 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
357 0x09, 0x80, // USAGE (System Control)
358 0xa1, 0x01, // COLLECTION (Application)
359 0x85, REPORT_ID_SYSTEM, // REPORT_ID (2)
360 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
361 0x26, 0xb7, 0x00, // LOGICAL_MAXIMUM (0xb7)
362 0x19, 0x01, // USAGE_MINIMUM (0x1)
363 0x29, 0xb7, // USAGE_MAXIMUM (0xb7)
364 0x75, 0x10, // REPORT_SIZE (16)
365 0x95, 0x01, // REPORT_COUNT (1)
366 0x81, 0x00, // INPUT (Data,Array,Abs)
367 0xc0, // END_COLLECTION
369 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
370 0x09, 0x01, // USAGE (Consumer Control)
371 0xa1, 0x01, // COLLECTION (Application)
372 0x85, REPORT_ID_CONSUMER, // REPORT_ID (3)
373 0x15, 0x01, // LOGICAL_MINIMUM (0x1)
374 0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
375 0x19, 0x01, // USAGE_MINIMUM (0x1)
376 0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
377 0x75, 0x10, // REPORT_SIZE (16)
378 0x95, 0x01, // REPORT_COUNT (1)
379 0x81, 0x00, // INPUT (Data,Array,Abs)
380 0xc0, // END_COLLECTION
385 * Descriptor for compite device: Keyboard + Mouse
387 * contains: device, interface, HID and endpoint descriptors
389 #if USB_CFG_DESCR_PROPS_CONFIGURATION
390 const PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */
391 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
392 USBDESCR_CONFIG, /* descriptor type */
393 9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
394 //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
395 /* total length of data returned (including inlined descriptors) */
396 2, /* number of interfaces in this configuration */
397 1, /* index of this configuration */
398 0, /* configuration name string index */
399 #if USB_CFG_IS_SELF_POWERED
400 (1 << 7) | USBATTR_SELFPOWER, /* attributes */
402 (1 << 7), /* attributes */
404 USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */
409 /* Interface descriptor */
410 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
411 USBDESCR_INTERFACE, /* descriptor type */
412 0, /* index of this interface */
413 0, /* alternate setting for this interface */
414 USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
415 USB_CFG_INTERFACE_CLASS,
416 USB_CFG_INTERFACE_SUBCLASS,
417 USB_CFG_INTERFACE_PROTOCOL,
418 0, /* string index for interface */
420 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
421 USBDESCR_HID, /* descriptor type: HID */
422 0x01, 0x01, /* BCD representation of HID version */
423 0x00, /* target country code */
424 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
425 0x22, /* descriptor type: report */
426 sizeof(keyboard_hid_report), 0, /* total length of report descriptor */
427 /* Endpoint descriptor */
428 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
429 7, /* sizeof(usbDescrEndpoint) */
430 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
431 (char)0x81, /* IN endpoint number 1 */
432 0x03, /* attrib: Interrupt endpoint */
433 8, 0, /* maximum packet size */
434 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
440 /* Interface descriptor */
441 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
442 USBDESCR_INTERFACE, /* descriptor type */
443 1, /* index of this interface */
444 0, /* alternate setting for this interface */
445 USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
446 0x03, /* CLASS: HID */
447 0, /* SUBCLASS: none */
448 0, /* PROTOCOL: none */
449 0, /* string index for interface */
451 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
452 USBDESCR_HID, /* descriptor type: HID */
453 0x01, 0x01, /* BCD representation of HID version */
454 0x00, /* target country code */
455 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
456 0x22, /* descriptor type: report */
457 sizeof(mouse_hid_report), 0, /* total length of report descriptor */
458 #if USB_CFG_HAVE_INTRIN_ENDPOINT3 /* endpoint descriptor for endpoint 3 */
459 /* Endpoint descriptor */
460 7, /* sizeof(usbDescrEndpoint) */
461 USBDESCR_ENDPOINT, /* descriptor type = endpoint */
462 (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
463 0x03, /* attrib: Interrupt endpoint */
464 8, 0, /* maximum packet size */
465 USB_CFG_INTR_POLL_INTERVAL, /* in ms */
471 USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
476 debug("usbFunctionDescriptor: ");
477 debug_hex(rq->bmRequestType); debug(" ");
478 debug_hex(rq->bRequest); debug(" ");
479 debug_hex16(rq->wValue.word); debug(" ");
480 debug_hex16(rq->wIndex.word); debug(" ");
481 debug_hex16(rq->wLength.word); debug("\n");
483 switch (rq->wValue.bytes[1]) {
484 #if USB_CFG_DESCR_PROPS_CONFIGURATION
485 case USBDESCR_CONFIG:
486 usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
487 len = sizeof(usbDescriptorConfiguration);
491 switch (rq->wValue.bytes[0]) {
493 usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + 9);
497 usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + (9 + 9 + 7) + 9);
502 case USBDESCR_HID_REPORT:
503 /* interface index */
504 switch (rq->wIndex.word) {
506 usbMsgPtr = (unsigned char *)keyboard_hid_report;
507 len = sizeof(keyboard_hid_report);
510 usbMsgPtr = (unsigned char *)mouse_hid_report;
511 len = sizeof(mouse_hid_report);
516 //debug("desc len: "); debug_hex(len); debug("\n");