]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/vusb/vusb.c
Configure Vagrant to use qmk_base_container (#6194)
[qmk_firmware.git] / tmk_core / protocol / vusb / vusb.c
1 /*
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17
18 #include <avr/eeprom.h>
19 #include <avr/wdt.h>
20 #include <stdint.h>
21 #include "usbdrv.h"
22 #include "usbconfig.h"
23 #include "host.h"
24 #include "report.h"
25 #include "print.h"
26 #include "debug.h"
27 #include "host_driver.h"
28 #include "vusb.h"
29 #include "bootloader.h"
30 #include <util/delay.h>
31
32
33 static uint8_t vusb_keyboard_leds = 0;
34 static uint8_t vusb_idle_rate = 0;
35
36 /* Keyboard report send buffer */
37 #define KBUF_SIZE 16
38 static report_keyboard_t kbuf[KBUF_SIZE];
39 static uint8_t kbuf_head = 0;
40 static uint8_t kbuf_tail = 0;
41
42 typedef struct {
43         uint8_t modifier;
44         uint8_t reserved;
45         uint8_t keycode[6];
46 } keyboard_report_t;
47
48 static keyboard_report_t keyboard_report; // sent to PC
49
50 #define VUSB_TRANSFER_KEYBOARD_MAX_TRIES 10
51
52 /* transfer keyboard report from buffer */
53 void vusb_transfer_keyboard(void)
54 {
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;
60                 if (debug_keyboard) {
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));
63                     print(")\n");
64                 }
65             }
66             break;
67         }
68         usbPoll();
69         _delay_ms(1);
70     }
71 }
72
73
74 /*------------------------------------------------------------------*
75  * Host driver
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);
82
83 static host_driver_t driver = {
84         keyboard_leds,
85         send_keyboard,
86         send_mouse,
87         send_system,
88         send_consumer
89 };
90
91 host_driver_t *vusb_driver(void)
92 {
93     return &driver;
94 }
95
96 static uint8_t keyboard_leds(void) {
97     return vusb_keyboard_leds;
98 }
99
100 static void send_keyboard(report_keyboard_t *report)
101 {
102     uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
103     if (next != kbuf_tail) {
104         kbuf[kbuf_head] = *report;
105         kbuf_head = next;
106     } else {
107         debug("kbuf: full\n");
108     }
109
110     // NOTE: send key strokes of Macro
111     usbPoll();
112     vusb_transfer_keyboard();
113 }
114
115
116 typedef struct {
117     uint8_t report_id;
118     report_mouse_t report;
119 } __attribute__ ((packed)) vusb_mouse_report_t;
120
121 static void send_mouse(report_mouse_t *report)
122 {
123     vusb_mouse_report_t r = {
124         .report_id = REPORT_ID_MOUSE,
125         .report = *report
126     };
127     if (usbInterruptIsReady3()) {
128         usbSetInterrupt3((void *)&r, sizeof(vusb_mouse_report_t));
129     }
130 }
131
132
133 typedef struct {
134     uint8_t  report_id;
135     uint16_t usage;
136 } __attribute__ ((packed)) report_extra_t;
137
138 static void send_system(uint16_t data)
139 {
140     static uint16_t last_data = 0;
141     if (data == last_data) return;
142     last_data = data;
143
144     report_extra_t report = {
145         .report_id = REPORT_ID_SYSTEM,
146         .usage = data
147     };
148     if (usbInterruptIsReady3()) {
149         usbSetInterrupt3((void *)&report, sizeof(report));
150     }
151 }
152
153 static void send_consumer(uint16_t data)
154 {
155     static uint16_t last_data = 0;
156     if (data == last_data) return;
157     last_data = data;
158
159     report_extra_t report = {
160         .report_id = REPORT_ID_CONSUMER,
161         .usage = data
162     };
163     if (usbInterruptIsReady3()) {
164         usbSetInterrupt3((void *)&report, sizeof(report));
165     }
166 }
167
168
169
170 /*------------------------------------------------------------------*
171  * Request from host                                                *
172  *------------------------------------------------------------------*/
173 static struct {
174     uint16_t        len;
175     enum {
176         NONE,
177         BOOTLOADER,
178         SET_LED
179     }               kind;
180 } last_req;
181
182 usbMsgLen_t usbFunctionSetup(uchar data[8])
183 {
184 usbRequest_t    *rq = (void *)data;
185
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){
193             debug("GET_IDLE: ");
194             //debug_hex(vusb_idle_rate);
195             usbMsgPtr = &vusb_idle_rate;
196             return 1;
197         }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
198             vusb_idle_rate = rq->wValue.bytes[1];
199             debug("SET_IDLE: ");
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) {
205                 debug("SET_LED: ");
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;
212 #endif
213             }
214             return USB_NO_MSG; // to get data in usbFunctionWrite
215         } else {
216             debug("UNKNOWN:");
217         }
218     }else{
219         debug("VENDOR:");
220         /* no vendor specific requests implemented */
221     }
222     debug("\n");
223     return 0;   /* default for not implemented requests: return no data back to host */
224 }
225
226 uchar usbFunctionWrite(uchar *data, uchar len)
227 {
228     if (last_req.len == 0) {
229         return -1;
230     }
231     switch (last_req.kind) {
232         case SET_LED:
233             debug("SET_LED: ");
234             debug_hex(data[0]);
235             debug("\n");
236             vusb_keyboard_leds = data[0];
237             last_req.len = 0;
238             return 1;
239             break;
240         case BOOTLOADER:
241             usbDeviceDisconnect();
242             bootloader_jump();
243             return 1;
244             break;
245         case NONE:
246         default:
247             return -1;
248             break;
249     }
250     return 1;
251 }
252
253
254
255 /*------------------------------------------------------------------*
256  * Descriptors                                                      *
257  *------------------------------------------------------------------*/
258
259 /*
260  * Report Descriptor for keyboard
261  *
262  * from an example in HID spec appendix
263  */
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
297 };
298
299 /*
300  * Report Descriptor for mouse
301  *
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
306  */
307 const PROGMEM uchar mouse_hid_report[] = {
308     /* mouse */
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
355     /* system control */
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
368     /* consumer */
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
381 };
382
383
384 /* 
385  * Descriptor for compite device: Keyboard + Mouse
386  * 
387  * contains: device, interface, HID and endpoint descriptors
388  */
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 */
401 #else
402     (1 << 7),                           /* attributes */
403 #endif
404     USB_CFG_MAX_BUS_POWER/2,            /* max USB current in 2mA units */
405
406     /*
407      * Keyboard interface
408      */
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 */
419     /* HID descriptor */
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 */
435 #endif
436
437     /*
438      * Mouse interface
439      */
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 */
450     /* HID descriptor */
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 */
466 #endif
467 };
468 #endif
469
470
471 USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
472 {
473     usbMsgLen_t len = 0;
474
475 /*
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");
482 */
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);
488             break;
489 #endif
490         case USBDESCR_HID:
491             switch (rq->wValue.bytes[0]) {
492                 case 0:
493                     usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + 9);
494                     len = 9;
495                     break;
496                 case 1:
497                     usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + (9 + 9 + 7) + 9);
498                     len = 9;
499                     break;
500             }
501             break;
502         case USBDESCR_HID_REPORT:
503             /* interface index */
504             switch (rq->wIndex.word) {
505                 case 0:
506                     usbMsgPtr = (unsigned char *)keyboard_hid_report;
507                     len = sizeof(keyboard_hid_report);
508                     break;
509                 case 1:
510                     usbMsgPtr = (unsigned char *)mouse_hid_report;
511                     len = sizeof(mouse_hid_report);
512                     break;
513             }
514             break;
515     }
516     //debug("desc len: "); debug_hex(len); debug("\n");
517     return len;
518 }