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