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