]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/vusb/vusb.c
Got ps2avrGB to work with the V-USB protocol
[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
31
32 static uint8_t vusb_keyboard_leds = 0;
33 static uint8_t vusb_idle_rate = 0;
34
35 /* Keyboard report send buffer */
36 #define KBUF_SIZE 16
37 static report_keyboard_t kbuf[KBUF_SIZE];
38 static uint8_t kbuf_head = 0;
39 static uint8_t kbuf_tail = 0;
40
41 typedef struct {
42         uint8_t modifier;
43         uint8_t reserved;
44         uint8_t keycode[6];
45 } keyboard_report_t;
46
47 static keyboard_report_t keyboard_report; // sent to PC
48
49 /* transfer keyboard report from buffer */
50 void vusb_transfer_keyboard(void)
51 {
52     if (usbInterruptIsReady()) {
53         if (kbuf_head != kbuf_tail) {
54             usbSetInterrupt((void *)&kbuf[kbuf_tail], sizeof(report_keyboard_t));
55             kbuf_tail = (kbuf_tail + 1) % KBUF_SIZE;
56             if (debug_keyboard) {
57                 print("V-USB: kbuf["); pdec(kbuf_tail); print("->"); pdec(kbuf_head); print("](");
58                 phex((kbuf_head < kbuf_tail) ? (KBUF_SIZE - kbuf_tail + kbuf_head) : (kbuf_head - kbuf_tail));
59                 print(")\n");
60             }
61         }
62     }
63 }
64
65
66 /*------------------------------------------------------------------*
67  * Host driver
68  *------------------------------------------------------------------*/
69 static uint8_t keyboard_leds(void);
70 static void send_keyboard(report_keyboard_t *report);
71 static void send_mouse(report_mouse_t *report);
72 static void send_system(uint16_t data);
73 static void send_consumer(uint16_t data);
74
75 static host_driver_t driver = {
76         keyboard_leds,
77         send_keyboard,
78         send_mouse,
79         send_system,
80         send_consumer
81 };
82
83 host_driver_t *vusb_driver(void)
84 {
85     return &driver;
86 }
87
88 static uint8_t keyboard_leds(void) {
89     return vusb_keyboard_leds;
90 }
91
92 static void send_keyboard(report_keyboard_t *report)
93 {
94     uint8_t next = (kbuf_head + 1) % KBUF_SIZE;
95     if (next != kbuf_tail) {
96         kbuf[kbuf_head] = *report;
97         kbuf_head = next;
98     } else {
99         debug("kbuf: full\n");
100     }
101
102     // NOTE: send key strokes of Macro
103     usbPoll();
104     vusb_transfer_keyboard();
105 }
106
107
108 typedef struct {
109     uint8_t report_id;
110     report_mouse_t report;
111 } __attribute__ ((packed)) vusb_mouse_report_t;
112
113 static void send_mouse(report_mouse_t *report)
114 {
115     vusb_mouse_report_t r = {
116         .report_id = REPORT_ID_MOUSE,
117         .report = *report
118     };
119     if (usbInterruptIsReady3()) {
120         usbSetInterrupt3((void *)&r, sizeof(vusb_mouse_report_t));
121     }
122 }
123
124
125 typedef struct {
126     uint8_t  report_id;
127     uint16_t usage;
128 } __attribute__ ((packed)) report_extra_t;
129
130 static void send_system(uint16_t data)
131 {
132     static uint16_t last_data = 0;
133     if (data == last_data) return;
134     last_data = data;
135
136     report_extra_t report = {
137         .report_id = REPORT_ID_SYSTEM,
138         .usage = data
139     };
140     if (usbInterruptIsReady3()) {
141         usbSetInterrupt3((void *)&report, sizeof(report));
142     }
143 }
144
145 static void send_consumer(uint16_t data)
146 {
147     static uint16_t last_data = 0;
148     if (data == last_data) return;
149     last_data = data;
150
151     report_extra_t report = {
152         .report_id = REPORT_ID_CONSUMER,
153         .usage = data
154     };
155     if (usbInterruptIsReady3()) {
156         usbSetInterrupt3((void *)&report, sizeof(report));
157     }
158 }
159
160
161
162 /*------------------------------------------------------------------*
163  * Request from host                                                *
164  *------------------------------------------------------------------*/
165 static struct {
166     uint16_t        len;
167     enum {
168         NONE,
169         BOOTLOADER,
170         SET_LED
171     }               kind;
172 } last_req;
173
174 usbMsgLen_t usbFunctionSetup(uchar data[8])
175 {
176 usbRequest_t    *rq = (void *)data;
177
178     if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){    /* class request type */
179         if(rq->bRequest == USBRQ_HID_GET_REPORT){
180             debug("GET_REPORT:");
181             /* we only have one report type, so don't look at wValue */
182             usbMsgPtr = (void *)&keyboard_report;
183             return sizeof(keyboard_report);
184         }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
185             debug("GET_IDLE: ");
186             //debug_hex(vusb_idle_rate);
187             usbMsgPtr = &vusb_idle_rate;
188             return 1;
189         }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
190             vusb_idle_rate = rq->wValue.bytes[1];
191             debug("SET_IDLE: ");
192             debug_hex(vusb_idle_rate);
193         }else if(rq->bRequest == USBRQ_HID_SET_REPORT){
194             debug("SET_REPORT: ");
195             // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
196             if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
197                 debug("SET_LED: ");
198                 last_req.kind = SET_LED;
199                 last_req.len = rq->wLength.word;
200 #ifdef BOOTLOADER_SIZE
201             } else if(rq->wValue.word == 0x0301) {
202                 last_req.kind = BOOTLOADER;
203                 last_req.len = rq->wLength.word;
204 #endif
205             }
206             return USB_NO_MSG; // to get data in usbFunctionWrite
207         } else {
208             debug("UNKNOWN:");
209         }
210     }else{
211         debug("VENDOR:");
212         /* no vendor specific requests implemented */
213     }
214     debug("\n");
215     return 0;   /* default for not implemented requests: return no data back to host */
216 }
217
218 uchar usbFunctionWrite(uchar *data, uchar len)
219 {
220     if (last_req.len == 0) {
221         return -1;
222     }
223     switch (last_req.kind) {
224         case SET_LED:
225             debug("SET_LED: ");
226             debug_hex(data[0]);
227             debug("\n");
228             vusb_keyboard_leds = data[0];
229             last_req.len = 0;
230             return 1;
231             break;
232         case BOOTLOADER:
233             usbDeviceDisconnect();
234             bootloader_jump();
235             return 1;
236             break;
237         case NONE:
238         default:
239             return -1;
240             break;
241     }
242     return 1;
243 }
244
245
246
247 /*------------------------------------------------------------------*
248  * Descriptors                                                      *
249  *------------------------------------------------------------------*/
250
251 /*
252  * Report Descriptor for keyboard
253  *
254  * from an example in HID spec appendix
255  */
256 const PROGMEM uchar keyboard_hid_report[] = {
257     0x05, 0x01,          // Usage Page (Generic Desktop),
258     0x09, 0x06,          // Usage (Keyboard),
259     0xA1, 0x01,          // Collection (Application),
260     0x75, 0x01,          //   Report Size (1),
261     0x95, 0x08,          //   Report Count (8),
262     0x05, 0x07,          //   Usage Page (Key Codes),
263     0x19, 0xE0,          //   Usage Minimum (224),
264     0x29, 0xE7,          //   Usage Maximum (231),
265     0x15, 0x00,          //   Logical Minimum (0),
266     0x25, 0x01,          //   Logical Maximum (1),
267     0x81, 0x02,          //   Input (Data, Variable, Absolute), ;Modifier byte
268     0x95, 0x01,          //   Report Count (1),
269     0x75, 0x08,          //   Report Size (8),
270     0x81, 0x03,          //   Input (Constant),                 ;Reserved byte
271     0x95, 0x05,          //   Report Count (5),
272     0x75, 0x01,          //   Report Size (1),
273     0x05, 0x08,          //   Usage Page (LEDs),
274     0x19, 0x01,          //   Usage Minimum (1),
275     0x29, 0x05,          //   Usage Maximum (5),
276     0x91, 0x02,          //   Output (Data, Variable, Absolute), ;LED report
277     0x95, 0x01,          //   Report Count (1),
278     0x75, 0x03,          //   Report Size (3),
279     0x91, 0x03,          //   Output (Constant),                 ;LED report padding
280     0x95, 0x06,          //   Report Count (6),
281     0x75, 0x08,          //   Report Size (8),
282     0x15, 0x00,          //   Logical Minimum (0),
283     0x25, 0xFF, 0x00,    //   Logical Maximum(255),
284     0x05, 0x07,          //   Usage Page (Key Codes),
285     0x19, 0x00,          //   Usage Minimum (0),
286     0x29, 0xFF,          //   Usage Maximum (255),
287     0x81, 0x00,          //   Input (Data, Array),
288     0xc0                 // End Collection
289 };
290
291 /*
292  * Report Descriptor for mouse
293  *
294  * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
295  * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
296  * http://www.keil.com/forum/15671/
297  * http://www.microsoft.com/whdc/device/input/wheel.mspx
298  */
299 const PROGMEM uchar mouse_hid_report[] = {
300     /* mouse */
301     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
302     0x09, 0x02,                    // USAGE (Mouse)
303     0xa1, 0x01,                    // COLLECTION (Application)
304     0x85, REPORT_ID_MOUSE,         //   REPORT_ID (1)
305     0x09, 0x01,                    //   USAGE (Pointer)
306     0xa1, 0x00,                    //   COLLECTION (Physical)
307                                    // ----------------------------  Buttons
308     0x05, 0x09,                    //     USAGE_PAGE (Button)
309     0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
310     0x29, 0x05,                    //     USAGE_MAXIMUM (Button 5)
311     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
312     0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
313     0x75, 0x01,                    //     REPORT_SIZE (1)
314     0x95, 0x05,                    //     REPORT_COUNT (5)
315     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
316     0x75, 0x03,                    //     REPORT_SIZE (3)
317     0x95, 0x01,                    //     REPORT_COUNT (1)
318     0x81, 0x03,                    //     INPUT (Cnst,Var,Abs)
319                                    // ----------------------------  X,Y position
320     0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
321     0x09, 0x30,                    //     USAGE (X)
322     0x09, 0x31,                    //     USAGE (Y)
323     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
324     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
325     0x75, 0x08,                    //     REPORT_SIZE (8)
326     0x95, 0x02,                    //     REPORT_COUNT (2)
327     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
328                                    // ----------------------------  Vertical wheel
329     0x09, 0x38,                    //     USAGE (Wheel)
330     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
331     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
332     0x35, 0x00,                    //     PHYSICAL_MINIMUM (0)        - reset physical
333     0x45, 0x00,                    //     PHYSICAL_MAXIMUM (0)
334     0x75, 0x08,                    //     REPORT_SIZE (8)
335     0x95, 0x01,                    //     REPORT_COUNT (1)
336     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
337                                    // ----------------------------  Horizontal wheel
338     0x05, 0x0c,                    //     USAGE_PAGE (Consumer Devices)
339     0x0a, 0x38, 0x02,              //     USAGE (AC Pan)
340     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
341     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
342     0x75, 0x08,                    //     REPORT_SIZE (8)
343     0x95, 0x01,                    //     REPORT_COUNT (1)
344     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
345     0xc0,                          //   END_COLLECTION
346     0xc0,                          // END_COLLECTION
347     /* system control */
348     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
349     0x09, 0x80,                    // USAGE (System Control)
350     0xa1, 0x01,                    // COLLECTION (Application)
351     0x85, REPORT_ID_SYSTEM,        //   REPORT_ID (2)
352     0x15, 0x01,                    //   LOGICAL_MINIMUM (0x1)
353     0x25, 0xb7, 0x00,              //   LOGICAL_MAXIMUM (0xb7)
354     0x19, 0x01,                    //   USAGE_MINIMUM (0x1)
355     0x29, 0xb7,                    //   USAGE_MAXIMUM (0xb7)
356     0x75, 0x10,                    //   REPORT_SIZE (16)
357     0x95, 0x01,                    //   REPORT_COUNT (1)
358     0x81, 0x00,                    //   INPUT (Data,Array,Abs)
359     0xc0,                          // END_COLLECTION
360     /* consumer */
361     0x05, 0x0c,                    // USAGE_PAGE (Consumer Devices)
362     0x09, 0x01,                    // USAGE (Consumer Control)
363     0xa1, 0x01,                    // COLLECTION (Application)
364     0x85, REPORT_ID_CONSUMER,      //   REPORT_ID (3)
365     0x15, 0x01,                    //   LOGICAL_MINIMUM (0x1)
366     0x26, 0x9c, 0x02,              //   LOGICAL_MAXIMUM (0x29c)
367     0x19, 0x01,                    //   USAGE_MINIMUM (0x1)
368     0x2a, 0x9c, 0x02,              //   USAGE_MAXIMUM (0x29c)
369     0x75, 0x10,                    //   REPORT_SIZE (16)
370     0x95, 0x01,                    //   REPORT_COUNT (1)
371     0x81, 0x00,                    //   INPUT (Data,Array,Abs)
372     0xc0,                          // END_COLLECTION
373 };
374
375
376 /* 
377  * Descriptor for compite device: Keyboard + Mouse
378  * 
379  * contains: device, interface, HID and endpoint descriptors
380  */
381 #if USB_CFG_DESCR_PROPS_CONFIGURATION
382 const PROGMEM char usbDescriptorConfiguration[] = {    /* USB configuration descriptor */
383     9,          /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
384     USBDESCR_CONFIG,    /* descriptor type */
385     9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
386     //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
387                 /* total length of data returned (including inlined descriptors) */
388     2,          /* number of interfaces in this configuration */
389     1,          /* index of this configuration */
390     0,          /* configuration name string index */
391 #if USB_CFG_IS_SELF_POWERED
392     (1 << 7) | USBATTR_SELFPOWER,       /* attributes */
393 #else
394     (1 << 7),                           /* attributes */
395 #endif
396     USB_CFG_MAX_BUS_POWER/2,            /* max USB current in 2mA units */
397
398     /*
399      * Keyboard interface
400      */
401     /* Interface descriptor */
402     9,          /* sizeof(usbDescrInterface): length of descriptor in bytes */
403     USBDESCR_INTERFACE, /* descriptor type */
404     0,          /* index of this interface */
405     0,          /* alternate setting for this interface */
406     USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
407     USB_CFG_INTERFACE_CLASS,
408     USB_CFG_INTERFACE_SUBCLASS,
409     USB_CFG_INTERFACE_PROTOCOL,
410     0,          /* string index for interface */
411     /* HID descriptor */
412     9,          /* sizeof(usbDescrHID): length of descriptor in bytes */
413     USBDESCR_HID,   /* descriptor type: HID */
414     0x01, 0x01, /* BCD representation of HID version */
415     0x00,       /* target country code */
416     0x01,       /* number of HID Report (or other HID class) Descriptor infos to follow */
417     0x22,       /* descriptor type: report */
418     sizeof(keyboard_hid_report), 0,  /* total length of report descriptor */
419     /* Endpoint descriptor */
420 #if USB_CFG_HAVE_INTRIN_ENDPOINT    /* endpoint descriptor for endpoint 1 */
421     7,          /* sizeof(usbDescrEndpoint) */
422     USBDESCR_ENDPOINT,  /* descriptor type = endpoint */
423     (char)0x81, /* IN endpoint number 1 */
424     0x03,       /* attrib: Interrupt endpoint */
425     8, 0,       /* maximum packet size */
426     USB_CFG_INTR_POLL_INTERVAL, /* in ms */
427 #endif
428
429     /*
430      * Mouse interface
431      */
432     /* Interface descriptor */
433     9,          /* sizeof(usbDescrInterface): length of descriptor in bytes */
434     USBDESCR_INTERFACE, /* descriptor type */
435     1,          /* index of this interface */
436     0,          /* alternate setting for this interface */
437     USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
438     0x03,       /* CLASS: HID */
439     0,          /* SUBCLASS: none */
440     0,          /* PROTOCOL: none */
441     0,          /* string index for interface */
442     /* HID descriptor */
443     9,          /* sizeof(usbDescrHID): length of descriptor in bytes */
444     USBDESCR_HID,   /* descriptor type: HID */
445     0x01, 0x01, /* BCD representation of HID version */
446     0x00,       /* target country code */
447     0x01,       /* number of HID Report (or other HID class) Descriptor infos to follow */
448     0x22,       /* descriptor type: report */
449     sizeof(mouse_hid_report), 0,  /* total length of report descriptor */
450 #if USB_CFG_HAVE_INTRIN_ENDPOINT3   /* endpoint descriptor for endpoint 3 */
451     /* Endpoint descriptor */
452     7,          /* sizeof(usbDescrEndpoint) */
453     USBDESCR_ENDPOINT,  /* descriptor type = endpoint */
454     (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
455     0x03,       /* attrib: Interrupt endpoint */
456     8, 0,       /* maximum packet size */
457     USB_CFG_INTR_POLL_INTERVAL, /* in ms */
458 #endif
459 };
460 #endif
461
462
463 USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
464 {
465     usbMsgLen_t len = 0;
466
467 /*
468     debug("usbFunctionDescriptor: ");
469     debug_hex(rq->bmRequestType); debug(" ");
470     debug_hex(rq->bRequest); debug(" ");
471     debug_hex16(rq->wValue.word); debug(" ");
472     debug_hex16(rq->wIndex.word); debug(" ");
473     debug_hex16(rq->wLength.word); debug("\n");
474 */
475     switch (rq->wValue.bytes[1]) {
476 #if USB_CFG_DESCR_PROPS_CONFIGURATION
477         case USBDESCR_CONFIG:
478             usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
479             len = sizeof(usbDescriptorConfiguration);
480             break;
481 #endif
482         case USBDESCR_HID:
483             switch (rq->wValue.bytes[0]) {
484                 case 0:
485                     usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + 9);
486                     len = 9;
487                     break;
488                 case 1:
489                     usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + (9 + 9 + 7) + 9);
490                     len = 9;
491                     break;
492             }
493             break;
494         case USBDESCR_HID_REPORT:
495             /* interface index */
496             switch (rq->wIndex.word) {
497                 case 0:
498                     usbMsgPtr = (unsigned char *)keyboard_hid_report;
499                     len = sizeof(keyboard_hid_report);
500                     break;
501                 case 1:
502                     usbMsgPtr = (unsigned char *)mouse_hid_report;
503                     len = sizeof(mouse_hid_report);
504                     break;
505             }
506             break;
507     }
508     //debug("desc len: "); debug_hex(len); debug("\n");
509     return len;
510 }