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