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