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