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