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