]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/vusb/vusb.c
vusb: Fix keyboard_protocol and keyboard_idle #547
[max/tmk_keyboard.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 <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 /* host.h */
30 uint8_t keyboard_protocol=1;
31 uint8_t keyboard_idle = 0;
32
33 static uint8_t vusb_keyboard_leds = 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         SET_LED
170     }               kind;
171 } last_req;
172
173 usbMsgLen_t usbFunctionSetup(uchar data[8])
174 {
175 usbRequest_t    *rq = (void *)data;
176
177     if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){    /* class request type */
178         if(rq->bRequest == USBRQ_HID_GET_REPORT){
179             debug("GET_REPORT:");
180             /* we only have one report type, so don't look at wValue */
181             usbMsgPtr = (void *)&keyboard_report;
182             return sizeof(keyboard_report);
183         }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
184             debug("GET_IDLE: ");
185             //debug_hex(keyboard_idle);
186             usbMsgPtr = &keyboard_idle;
187             return 1;
188         }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
189             keyboard_idle = rq->wValue.bytes[1];
190             debug("SET_IDLE: ");
191             debug_hex(keyboard_idle);
192         }else if(rq->bRequest == USBRQ_HID_SET_REPORT){
193             debug("SET_REPORT: ");
194             // Report Type: 0x02(Out)/ReportID: 0x00(none) && Interface: 0(keyboard)
195             if (rq->wValue.word == 0x0200 && rq->wIndex.word == 0) {
196                 debug("SET_LED: ");
197                 last_req.kind = SET_LED;
198                 last_req.len = rq->wLength.word;
199             }
200             return USB_NO_MSG; // to get data in usbFunctionWrite
201         } else {
202             debug("UNKNOWN:");
203         }
204     }else{
205         debug("VENDOR:");
206         /* no vendor specific requests implemented */
207     }
208     debug("\n");
209     return 0;   /* default for not implemented requests: return no data back to host */
210 }
211
212 uchar usbFunctionWrite(uchar *data, uchar len)
213 {
214     if (last_req.len == 0) {
215         return -1;
216     }
217     switch (last_req.kind) {
218         case SET_LED:
219             debug("SET_LED: ");
220             debug_hex(data[0]);
221             debug("\n");
222             vusb_keyboard_leds = data[0];
223             last_req.len = 0;
224             return 1;
225             break;
226         case NONE:
227         default:
228             return -1;
229             break;
230     }
231     return 1;
232 }
233
234
235
236 /*------------------------------------------------------------------*
237  * Descriptors                                                      *
238  *------------------------------------------------------------------*/
239
240 /*
241  * Report Descriptor for keyboard
242  *
243  * from an example in HID spec appendix
244  */
245 const PROGMEM uchar keyboard_hid_report[] = {
246     0x05, 0x01,          // Usage Page (Generic Desktop),
247     0x09, 0x06,          // Usage (Keyboard),
248     0xA1, 0x01,          // Collection (Application),
249     0x75, 0x01,          //   Report Size (1),
250     0x95, 0x08,          //   Report Count (8),
251     0x05, 0x07,          //   Usage Page (Key Codes),
252     0x19, 0xE0,          //   Usage Minimum (224),
253     0x29, 0xE7,          //   Usage Maximum (231),
254     0x15, 0x00,          //   Logical Minimum (0),
255     0x25, 0x01,          //   Logical Maximum (1),
256     0x81, 0x02,          //   Input (Data, Variable, Absolute), ;Modifier byte
257     0x95, 0x01,          //   Report Count (1),
258     0x75, 0x08,          //   Report Size (8),
259     0x81, 0x03,          //   Input (Constant),                 ;Reserved byte
260     0x95, 0x05,          //   Report Count (5),
261     0x75, 0x01,          //   Report Size (1),
262     0x05, 0x08,          //   Usage Page (LEDs),
263     0x19, 0x01,          //   Usage Minimum (1),
264     0x29, 0x05,          //   Usage Maximum (5),
265     0x91, 0x02,          //   Output (Data, Variable, Absolute), ;LED report
266     0x95, 0x01,          //   Report Count (1),
267     0x75, 0x03,          //   Report Size (3),
268     0x91, 0x03,          //   Output (Constant),                 ;LED report padding
269     0x95, 0x06,          //   Report Count (6),
270     0x75, 0x08,          //   Report Size (8),
271     0x15, 0x00,          //   Logical Minimum (0),
272     0x26, 0xFF, 0x00,    //   Logical Maximum(255),
273     0x05, 0x07,          //   Usage Page (Key Codes),
274     0x19, 0x00,          //   Usage Minimum (0),
275     0x29, 0xFF,          //   Usage Maximum (255),
276     0x81, 0x00,          //   Input (Data, Array),
277     0xc0                 // End Collection
278 };
279
280 /*
281  * Report Descriptor for mouse
282  *
283  * Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
284  * http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
285  * http://www.keil.com/forum/15671/
286  * http://www.microsoft.com/whdc/device/input/wheel.mspx
287  */
288 const PROGMEM uchar mouse_hid_report[] = {
289     /* mouse */
290     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
291     0x09, 0x02,                    // USAGE (Mouse)
292     0xa1, 0x01,                    // COLLECTION (Application)
293     0x85, REPORT_ID_MOUSE,         //   REPORT_ID (1)
294     0x09, 0x01,                    //   USAGE (Pointer)
295     0xa1, 0x00,                    //   COLLECTION (Physical)
296                                    // ----------------------------  Buttons
297     0x05, 0x09,                    //     USAGE_PAGE (Button)
298     0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
299     0x29, 0x05,                    //     USAGE_MAXIMUM (Button 5)
300     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
301     0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
302     0x75, 0x01,                    //     REPORT_SIZE (1)
303     0x95, 0x05,                    //     REPORT_COUNT (5)
304     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
305     0x75, 0x03,                    //     REPORT_SIZE (3)
306     0x95, 0x01,                    //     REPORT_COUNT (1)
307     0x81, 0x03,                    //     INPUT (Cnst,Var,Abs)
308                                    // ----------------------------  X,Y position
309     0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
310     0x09, 0x30,                    //     USAGE (X)
311     0x09, 0x31,                    //     USAGE (Y)
312     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
313     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
314     0x75, 0x08,                    //     REPORT_SIZE (8)
315     0x95, 0x02,                    //     REPORT_COUNT (2)
316     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
317                                    // ----------------------------  Vertical wheel
318     0x09, 0x38,                    //     USAGE (Wheel)
319     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
320     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
321     0x35, 0x00,                    //     PHYSICAL_MINIMUM (0)        - reset physical
322     0x45, 0x00,                    //     PHYSICAL_MAXIMUM (0)
323     0x75, 0x08,                    //     REPORT_SIZE (8)
324     0x95, 0x01,                    //     REPORT_COUNT (1)
325     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
326                                    // ----------------------------  Horizontal wheel
327     0x05, 0x0c,                    //     USAGE_PAGE (Consumer Devices)
328     0x0a, 0x38, 0x02,              //     USAGE (AC Pan)
329     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
330     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
331     0x75, 0x08,                    //     REPORT_SIZE (8)
332     0x95, 0x01,                    //     REPORT_COUNT (1)
333     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
334     0xc0,                          //   END_COLLECTION
335     0xc0,                          // END_COLLECTION
336     /* system control */
337     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
338     0x09, 0x80,                    // USAGE (System Control)
339     0xa1, 0x01,                    // COLLECTION (Application)
340     0x85, REPORT_ID_SYSTEM,        //   REPORT_ID (2)
341     0x15, 0x01,                    //   LOGICAL_MINIMUM (0x1)
342     0x26, 0xb7, 0x00,              //   LOGICAL_MAXIMUM (0xb7)
343     0x19, 0x01,                    //   USAGE_MINIMUM (0x1)
344     0x29, 0xb7,                    //   USAGE_MAXIMUM (0xb7)
345     0x75, 0x10,                    //   REPORT_SIZE (16)
346     0x95, 0x01,                    //   REPORT_COUNT (1)
347     0x81, 0x00,                    //   INPUT (Data,Array,Abs)
348     0xc0,                          // END_COLLECTION
349     /* consumer */
350     0x05, 0x0c,                    // USAGE_PAGE (Consumer Devices)
351     0x09, 0x01,                    // USAGE (Consumer Control)
352     0xa1, 0x01,                    // COLLECTION (Application)
353     0x85, REPORT_ID_CONSUMER,      //   REPORT_ID (3)
354     0x15, 0x01,                    //   LOGICAL_MINIMUM (0x1)
355     0x26, 0x9c, 0x02,              //   LOGICAL_MAXIMUM (0x29c)
356     0x19, 0x01,                    //   USAGE_MINIMUM (0x1)
357     0x2a, 0x9c, 0x02,              //   USAGE_MAXIMUM (0x29c)
358     0x75, 0x10,                    //   REPORT_SIZE (16)
359     0x95, 0x01,                    //   REPORT_COUNT (1)
360     0x81, 0x00,                    //   INPUT (Data,Array,Abs)
361     0xc0,                          // END_COLLECTION
362 };
363
364
365 /* 
366  * Descriptor for compite device: Keyboard + Mouse
367  * 
368  * contains: device, interface, HID and endpoint descriptors
369  */
370 #if USB_CFG_DESCR_PROPS_CONFIGURATION
371 const PROGMEM char usbDescriptorConfiguration[] = {    /* USB configuration descriptor */
372     9,          /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
373     USBDESCR_CONFIG,    /* descriptor type */
374     9 + (9 + 9 + 7) + (9 + 9 + 7), 0,
375     //18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT3 + 9, 0,
376                 /* total length of data returned (including inlined descriptors) */
377     2,          /* number of interfaces in this configuration */
378     1,          /* index of this configuration */
379     0,          /* configuration name string index */
380 #if USB_CFG_IS_SELF_POWERED
381     (1 << 7) | USBATTR_SELFPOWER,       /* attributes */
382 #else
383     (1 << 7),                           /* attributes */
384 #endif
385     USB_CFG_MAX_BUS_POWER/2,            /* max USB current in 2mA units */
386
387     /*
388      * Keyboard interface
389      */
390     /* Interface descriptor */
391     9,          /* sizeof(usbDescrInterface): length of descriptor in bytes */
392     USBDESCR_INTERFACE, /* descriptor type */
393     0,          /* index of this interface */
394     0,          /* alternate setting for this interface */
395     USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */
396     USB_CFG_INTERFACE_CLASS,
397     USB_CFG_INTERFACE_SUBCLASS,
398     USB_CFG_INTERFACE_PROTOCOL,
399     0,          /* string index for interface */
400     /* HID descriptor */
401     9,          /* sizeof(usbDescrHID): length of descriptor in bytes */
402     USBDESCR_HID,   /* descriptor type: HID */
403     0x01, 0x01, /* BCD representation of HID version */
404     0x00,       /* target country code */
405     0x01,       /* number of HID Report (or other HID class) Descriptor infos to follow */
406     0x22,       /* descriptor type: report */
407     sizeof(keyboard_hid_report), 0,  /* total length of report descriptor */
408     /* Endpoint descriptor */
409 #if USB_CFG_HAVE_INTRIN_ENDPOINT    /* endpoint descriptor for endpoint 1 */
410     7,          /* sizeof(usbDescrEndpoint) */
411     USBDESCR_ENDPOINT,  /* descriptor type = endpoint */
412     (char)0x81, /* IN endpoint number 1 */
413     0x03,       /* attrib: Interrupt endpoint */
414     8, 0,       /* maximum packet size */
415     USB_CFG_INTR_POLL_INTERVAL, /* in ms */
416 #endif
417
418     /*
419      * Mouse interface
420      */
421     /* Interface descriptor */
422     9,          /* sizeof(usbDescrInterface): length of descriptor in bytes */
423     USBDESCR_INTERFACE, /* descriptor type */
424     1,          /* index of this interface */
425     0,          /* alternate setting for this interface */
426     USB_CFG_HAVE_INTRIN_ENDPOINT3, /* endpoints excl 0: number of endpoint descriptors to follow */
427     0x03,       /* CLASS: HID */
428     0,          /* SUBCLASS: none */
429     0,          /* PROTOCOL: none */
430     0,          /* string index for interface */
431     /* HID descriptor */
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(mouse_hid_report), 0,  /* total length of report descriptor */
439 #if USB_CFG_HAVE_INTRIN_ENDPOINT3   /* endpoint descriptor for endpoint 3 */
440     /* Endpoint descriptor */
441     7,          /* sizeof(usbDescrEndpoint) */
442     USBDESCR_ENDPOINT,  /* descriptor type = endpoint */
443     (char)(0x80 | USB_CFG_EP3_NUMBER), /* IN endpoint number 3 */
444     0x03,       /* attrib: Interrupt endpoint */
445     8, 0,       /* maximum packet size */
446     USB_CFG_INTR_POLL_INTERVAL, /* in ms */
447 #endif
448 };
449 #endif
450
451
452 USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq)
453 {
454     usbMsgLen_t len = 0;
455
456 /*
457     debug("usbFunctionDescriptor: ");
458     debug_hex(rq->bmRequestType); debug(" ");
459     debug_hex(rq->bRequest); debug(" ");
460     debug_hex16(rq->wValue.word); debug(" ");
461     debug_hex16(rq->wIndex.word); debug(" ");
462     debug_hex16(rq->wLength.word); debug("\n");
463 */
464     switch (rq->wValue.bytes[1]) {
465 #if USB_CFG_DESCR_PROPS_CONFIGURATION
466         case USBDESCR_CONFIG:
467             usbMsgPtr = (unsigned char *)usbDescriptorConfiguration;
468             len = sizeof(usbDescriptorConfiguration);
469             break;
470 #endif
471         case USBDESCR_HID:
472             switch (rq->wValue.bytes[0]) {
473                 case 0:
474                     usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + 9);
475                     len = 9;
476                     break;
477                 case 1:
478                     usbMsgPtr = (unsigned char *)(usbDescriptorConfiguration + 9 + (9 + 9 + 7) + 9);
479                     len = 9;
480                     break;
481             }
482             break;
483         case USBDESCR_HID_REPORT:
484             /* interface index */
485             switch (rq->wIndex.word) {
486                 case 0:
487                     usbMsgPtr = keyboard_hid_report;
488                     len = sizeof(keyboard_hid_report);
489                     break;
490                 case 1:
491                     usbMsgPtr = mouse_hid_report;
492                     len = sizeof(mouse_hid_report);
493                     break;
494             }
495             break;
496     }
497     //debug("desc len: "); debug_hex(len); debug("\n");
498     return len;
499 }