]> git.donarmstrong.com Git - tmk_firmware.git/blob - protocol/pjrc/usb.c
Merge branch 'hid_liber'
[tmk_firmware.git] / protocol / pjrc / usb.c
1 /* USB Keyboard Plus Debug Channel Example for Teensy USB Development Board
2  * http://www.pjrc.com/teensy/usb_keyboard.html
3  * Copyright (c) 2009 PJRC.COM, LLC
4  * 
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  * 
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  * 
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  * THE SOFTWARE.
22  */
23
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include <avr/io.h>
27 #include <avr/pgmspace.h>
28 #include <avr/interrupt.h>
29 #include "usb.h"
30 #include "usb_keyboard.h"
31 #include "usb_mouse.h"
32 #include "usb_debug.h"
33 #include "usb_extra.h"
34 #include "print.h"
35 #include "util.h"
36
37
38 /**************************************************************************
39  *
40  *  Configurable Options
41  *
42  **************************************************************************/
43
44 // You can change these to give your code its own name.
45 #ifndef MANUFACTURER
46 #   define STR_MANUFACTURER     L"t.m.k."
47 #else
48 #   define STR_MANUFACTURER     LSTR(MANUFACTURER)
49 #endif
50 #ifndef PRODUCT
51 #   define STR_PRODUCT          L"t.m.k. keyboard"
52 #else
53 #   define STR_PRODUCT          LSTR(PRODUCT)
54 #endif
55
56
57 // Mac OS-X and Linux automatically load the correct drivers.  On
58 // Windows, even though the driver is supplied by Microsoft, an
59 // INF file is needed to load the driver.  These numbers need to
60 // match the INF file.
61 #ifndef VENDOR_ID
62 #   define VENDOR_ID            0xFEED
63 #endif
64
65 #ifndef PRODUCT_ID
66 #   define PRODUCT_ID           0xBABE
67 #endif
68
69 #ifndef DEVICE_VER
70 #   define DEVICE_VER           0x0100
71 #endif
72
73
74 // USB devices are supposed to implment a halt feature, which is
75 // rarely (if ever) used.  If you comment this line out, the halt
76 // code will be removed, saving 102 bytes of space (gcc 4.3.0).
77 // This is not strictly USB compliant, but works with all major
78 // operating systems.
79 #define SUPPORT_ENDPOINT_HALT
80
81
82
83 /**************************************************************************
84  *
85  *  Endpoint Buffer Configuration
86  *
87  **************************************************************************/
88
89 #define ENDPOINT0_SIZE          32
90
91 bool remote_wakeup = false;
92 bool suspend = false;
93
94 // 0:control endpoint is enabled automatically by controller.
95 static const uint8_t PROGMEM endpoint_config_table[] = {
96         // enable, UECFG0X(type, direction), UECFG1X(size, bank, allocation)
97         1, EP_TYPE_INTERRUPT_IN,  EP_SIZE(KBD_SIZE)      | KBD_BUFFER,      // 1
98 #ifdef MOUSE_ENABLE
99         1, EP_TYPE_INTERRUPT_IN,  EP_SIZE(MOUSE_SIZE)    | MOUSE_BUFFER,    // 2
100 #else
101         0,                                                                  // 2
102 #endif
103         1, EP_TYPE_INTERRUPT_IN,  EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER, // 3
104 #ifdef EXTRAKEY_ENABLE
105         1, EP_TYPE_INTERRUPT_IN,  EP_SIZE(EXTRA_SIZE)    | EXTRA_BUFFER,    // 4
106 #else
107         0,                                                                  // 4
108 #endif
109 #ifdef NKRO_ENABLE
110         1, EP_TYPE_INTERRUPT_IN,  EP_SIZE(KBD2_SIZE)     | KBD2_BUFFER,     // 5
111 #else
112         0,                                                                  // 5
113 #endif
114         0,                                                                  // 6
115 };
116
117
118 /**************************************************************************
119  *
120  *  Descriptor Data
121  *
122  **************************************************************************/
123
124 // Descriptors are the data that your computer reads when it auto-detects
125 // this USB device (called "enumeration" in USB lingo).  The most commonly
126 // changed items are editable at the top of this file.  Changing things
127 // in here should only be done by those who've read chapter 9 of the USB
128 // spec and relevant portions of any USB class specifications!
129
130
131 static const uint8_t PROGMEM device_descriptor[] = {
132         18,                                     // bLength
133         1,                                      // bDescriptorType
134         0x00, 0x02,                             // bcdUSB
135         0,                                      // bDeviceClass
136         0,                                      // bDeviceSubClass
137         0,                                      // bDeviceProtocol
138         ENDPOINT0_SIZE,                         // bMaxPacketSize0
139         LSB(VENDOR_ID), MSB(VENDOR_ID),         // idVendor
140         LSB(PRODUCT_ID), MSB(PRODUCT_ID),       // idProduct
141         LSB(DEVICE_VER), MSB(DEVICE_VER),       // bcdDevice
142         1,                                      // iManufacturer
143         2,                                      // iProduct
144         0,                                      // iSerialNumber
145         1                                       // bNumConfigurations
146 };
147
148 // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
149 static const uint8_t PROGMEM keyboard_hid_report_desc[] = {
150         0x05, 0x01,          // Usage Page (Generic Desktop),
151         0x09, 0x06,          // Usage (Keyboard),
152         0xA1, 0x01,          // Collection (Application),
153         0x75, 0x01,          //   Report Size (1),
154         0x95, 0x08,          //   Report Count (8),
155         0x05, 0x07,          //   Usage Page (Key Codes),
156         0x19, 0xE0,          //   Usage Minimum (224),
157         0x29, 0xE7,          //   Usage Maximum (231),
158         0x15, 0x00,          //   Logical Minimum (0),
159         0x25, 0x01,          //   Logical Maximum (1),
160         0x81, 0x02,          //   Input (Data, Variable, Absolute), ;Modifier byte
161         0x95, 0x01,          //   Report Count (1),
162         0x75, 0x08,          //   Report Size (8),
163         0x81, 0x03,          //   Input (Constant),                 ;Reserved byte
164         0x95, 0x05,          //   Report Count (5),
165         0x75, 0x01,          //   Report Size (1),
166         0x05, 0x08,          //   Usage Page (LEDs),
167         0x19, 0x01,          //   Usage Minimum (1),
168         0x29, 0x05,          //   Usage Maximum (5),
169         0x91, 0x02,          //   Output (Data, Variable, Absolute), ;LED report
170         0x95, 0x01,          //   Report Count (1),
171         0x75, 0x03,          //   Report Size (3),
172         0x91, 0x03,          //   Output (Constant),                 ;LED report padding
173         0x95, KBD_REPORT_KEYS,    //   Report Count (),
174         0x75, 0x08,          //   Report Size (8),
175         0x15, 0x00,          //   Logical Minimum (0),
176         0x25, 0xFF,          //   Logical Maximum(255),
177         0x05, 0x07,          //   Usage Page (Key Codes),
178         0x19, 0x00,          //   Usage Minimum (0),
179         0x29, 0xFF,          //   Usage Maximum (255),
180         0x81, 0x00,          //   Input (Data, Array),
181         0xc0                 // End Collection
182 };
183 #ifdef NKRO_ENABLE
184 static const uint8_t PROGMEM keyboard2_hid_report_desc[] = {
185         0x05, 0x01,                     // Usage Page (Generic Desktop),
186         0x09, 0x06,                     // Usage (Keyboard),
187         0xA1, 0x01,                     // Collection (Application),
188         // bitmap of modifiers
189         0x75, 0x01,                     //   Report Size (1),
190         0x95, 0x08,                     //   Report Count (8),
191         0x05, 0x07,                     //   Usage Page (Key Codes),
192         0x19, 0xE0,                     //   Usage Minimum (224),
193         0x29, 0xE7,                     //   Usage Maximum (231),
194         0x15, 0x00,                     //   Logical Minimum (0),
195         0x25, 0x01,                     //   Logical Maximum (1),
196         0x81, 0x02,                     //   Input (Data, Variable, Absolute), ;Modifier byte
197         // LED output report
198         0x95, 0x05,                     //   Report Count (5),
199         0x75, 0x01,                     //   Report Size (1),
200         0x05, 0x08,                     //   Usage Page (LEDs),
201         0x19, 0x01,                     //   Usage Minimum (1),
202         0x29, 0x05,                     //   Usage Maximum (5),
203         0x91, 0x02,                     //   Output (Data, Variable, Absolute),
204         0x95, 0x01,                     //   Report Count (1),
205         0x75, 0x03,                     //   Report Size (3),
206         0x91, 0x03,                     //   Output (Constant),
207         // bitmap of keys
208         0x95, KBD2_REPORT_KEYS*8,       //   Report Count (),
209         0x75, 0x01,                     //   Report Size (1),
210         0x15, 0x00,                     //   Logical Minimum (0),
211         0x25, 0x01,                     //   Logical Maximum(1),
212         0x05, 0x07,                     //   Usage Page (Key Codes),
213         0x19, 0x00,                     //   Usage Minimum (0),
214         0x29, KBD2_REPORT_KEYS*8-1,     //   Usage Maximum (),
215         0x81, 0x02,                     //   Input (Data, Variable, Absolute),
216         0xc0                            // End Collection
217 };
218 #endif
219
220 #ifdef MOUSE_ENABLE
221 // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
222 // http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
223 // http://www.keil.com/forum/15671/
224 // http://www.microsoft.com/whdc/device/input/wheel.mspx
225 static const uint8_t PROGMEM mouse_hid_report_desc[] = {
226     /* mouse */
227     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
228     0x09, 0x02,                    // USAGE (Mouse)
229     0xa1, 0x01,                    // COLLECTION (Application)
230     //0x85, REPORT_ID_MOUSE,         //   REPORT_ID (1)
231     0x09, 0x01,                    //   USAGE (Pointer)
232     0xa1, 0x00,                    //   COLLECTION (Physical)
233                                    // ----------------------------  Buttons
234     0x05, 0x09,                    //     USAGE_PAGE (Button)
235     0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
236     0x29, 0x05,                    //     USAGE_MAXIMUM (Button 5)
237     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
238     0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
239     0x75, 0x01,                    //     REPORT_SIZE (1)
240     0x95, 0x05,                    //     REPORT_COUNT (5)
241     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
242     0x75, 0x03,                    //     REPORT_SIZE (3)
243     0x95, 0x01,                    //     REPORT_COUNT (1)
244     0x81, 0x03,                    //     INPUT (Cnst,Var,Abs)
245                                    // ----------------------------  X,Y position
246     0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
247     0x09, 0x30,                    //     USAGE (X)
248     0x09, 0x31,                    //     USAGE (Y)
249     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
250     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
251     0x75, 0x08,                    //     REPORT_SIZE (8)
252     0x95, 0x02,                    //     REPORT_COUNT (2)
253     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
254                                    // ----------------------------  Vertical wheel
255     0x09, 0x38,                    //     USAGE (Wheel)
256     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
257     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
258     0x35, 0x00,                    //     PHYSICAL_MINIMUM (0)        - reset physical
259     0x45, 0x00,                    //     PHYSICAL_MAXIMUM (0)
260     0x75, 0x08,                    //     REPORT_SIZE (8)
261     0x95, 0x01,                    //     REPORT_COUNT (1)
262     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
263                                    // ----------------------------  Horizontal wheel
264     0x05, 0x0c,                    //     USAGE_PAGE (Consumer Devices)
265     0x0a, 0x38, 0x02,              //     USAGE (AC Pan)
266     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
267     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
268     0x75, 0x08,                    //     REPORT_SIZE (8)
269     0x95, 0x01,                    //     REPORT_COUNT (1)
270     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
271     0xc0,                          //   END_COLLECTION
272     0xc0,                          // END_COLLECTION
273 };
274 #endif
275
276 static const uint8_t PROGMEM debug_hid_report_desc[] = {
277         0x06, 0x31, 0xFF,                       // Usage Page 0xFF31 (vendor defined)
278         0x09, 0x74,                             // Usage 0x74
279         0xA1, 0x53,                             // Collection 0x53
280         0x75, 0x08,                             // report size = 8 bits
281         0x15, 0x00,                             // logical minimum = 0
282         0x26, 0xFF, 0x00,                       // logical maximum = 255
283         0x95, DEBUG_TX_SIZE,                    // report count
284         0x09, 0x75,                             // usage
285         0x81, 0x02,                             // Input (array)
286         0xC0                                    // end collection
287 };
288
289 #ifdef EXTRAKEY_ENABLE
290 // audio controls & system controls
291 // http://www.microsoft.com/whdc/archive/w2kbd.mspx
292 static const uint8_t PROGMEM extra_hid_report_desc[] = {
293     /* system control */
294     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
295     0x09, 0x80,                    // USAGE (System Control)
296     0xa1, 0x01,                    // COLLECTION (Application)
297     0x85, REPORT_ID_SYSTEM,        //   REPORT_ID (2)
298     0x15, 0x01,                    //   LOGICAL_MINIMUM (0x1)
299     0x25, 0xb7,                    //   LOGICAL_MAXIMUM (0xb7)
300     0x19, 0x01,                    //   USAGE_MINIMUM (0x1)
301     0x29, 0xb7,                    //   USAGE_MAXIMUM (0xb7)
302     0x75, 0x10,                    //   REPORT_SIZE (16)
303     0x95, 0x01,                    //   REPORT_COUNT (1)
304     0x81, 0x00,                    //   INPUT (Data,Array,Abs)
305     0xc0,                          // END_COLLECTION
306     /* consumer */
307     0x05, 0x0c,                    // USAGE_PAGE (Consumer Devices)
308     0x09, 0x01,                    // USAGE (Consumer Control)
309     0xa1, 0x01,                    // COLLECTION (Application)
310     0x85, REPORT_ID_CONSUMER,      //   REPORT_ID (3)
311     0x15, 0x01,                    //   LOGICAL_MINIMUM (0x1)
312     0x26, 0x9c, 0x02,              //   LOGICAL_MAXIMUM (0x29c)
313     0x19, 0x01,                    //   USAGE_MINIMUM (0x1)
314     0x2a, 0x9c, 0x02,              //   USAGE_MAXIMUM (0x29c)
315     0x75, 0x10,                    //   REPORT_SIZE (16)
316     0x95, 0x01,                    //   REPORT_COUNT (1)
317     0x81, 0x00,                    //   INPUT (Data,Array,Abs)
318     0xc0,                          // END_COLLECTION
319 };
320 #endif
321
322 #define KBD_HID_DESC_NUM                0
323 #define KBD_HID_DESC_OFFSET             (9+(9+9+7)*KBD_HID_DESC_NUM+9)
324
325 #ifdef MOUSE_ENABLE
326 #   define MOUSE_HID_DESC_NUM           (KBD_HID_DESC_NUM + 1)
327 #   define MOUSE_HID_DESC_OFFSET        (9+(9+9+7)*MOUSE_HID_DESC_NUM+9)
328 #else
329 #   define MOUSE_HID_DESC_NUM           (KBD_HID_DESC_NUM + 0)
330 #endif
331
332 #define DEBUG_HID_DESC_NUM              (MOUSE_HID_DESC_NUM + 1)
333 #define DEBUG_HID_DESC_OFFSET           (9+(9+9+7)*DEBUG_HID_DESC_NUM+9)
334
335 #ifdef EXTRAKEY_ENABLE
336 #   define EXTRA_HID_DESC_NUM           (DEBUG_HID_DESC_NUM + 1)
337 #   define EXTRA_HID_DESC_OFFSET        (9+(9+9+7)*EXTRA_HID_DESC_NUM+9)
338 #else
339 #   define EXTRA_HID_DESC_NUM           (DEBUG_HID_DESC_NUM + 0)
340 #endif
341
342 #ifdef NKRO_ENABLE
343 #   define KBD2_HID_DESC_NUM            (EXTRA_HID_DESC_NUM + 1)
344 #   define KBD2_HID_DESC_OFFSET         (9+(9+9+7)*EXTRA_HID_DESC_NUM+9)
345 #else
346 #   define KBD2_HID_DESC_NUM            (EXTRA_HID_DESC_NUM + 0)
347 #endif
348
349 #define NUM_INTERFACES                  (KBD2_HID_DESC_NUM + 1)
350 #define CONFIG1_DESC_SIZE               (9+(9+9+7)*NUM_INTERFACES)
351 static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
352         // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
353         9,                                      // bLength;
354         2,                                      // bDescriptorType;
355         LSB(CONFIG1_DESC_SIZE),                 // wTotalLength
356         MSB(CONFIG1_DESC_SIZE),
357         NUM_INTERFACES,                         // bNumInterfaces
358         1,                                      // bConfigurationValue
359         0,                                      // iConfiguration
360         0xA0,                                   // bmAttributes
361         50,                                     // bMaxPower
362
363         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
364         9,                                      // bLength
365         4,                                      // bDescriptorType
366         KBD_INTERFACE,                          // bInterfaceNumber
367         0,                                      // bAlternateSetting
368         1,                                      // bNumEndpoints
369         0x03,                                   // bInterfaceClass (0x03 = HID)
370         0x01,                                   // bInterfaceSubClass (0x01 = Boot)
371         0x01,                                   // bInterfaceProtocol (0x01 = Keyboard)
372         0,                                      // iInterface
373         // HID descriptor, HID 1.11 spec, section 6.2.1
374         9,                                      // bLength
375         0x21,                                   // bDescriptorType
376         0x11, 0x01,                             // bcdHID
377         0,                                      // bCountryCode
378         1,                                      // bNumDescriptors
379         0x22,                                   // bDescriptorType
380         sizeof(keyboard_hid_report_desc),       // wDescriptorLength
381         0,
382         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
383         7,                                      // bLength
384         5,                                      // bDescriptorType
385         KBD_ENDPOINT | 0x80,                    // bEndpointAddress
386         0x03,                                   // bmAttributes (0x03=intr)
387         KBD_SIZE, 0,                            // wMaxPacketSize
388         10,                                     // bInterval
389
390 #ifdef MOUSE_ENABLE
391         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
392         9,                                      // bLength
393         4,                                      // bDescriptorType
394         MOUSE_INTERFACE,                        // bInterfaceNumber
395         0,                                      // bAlternateSetting
396         1,                                      // bNumEndpoints
397         0x03,                                   // bInterfaceClass (0x03 = HID)
398         // ThinkPad T23 BIOS doesn't work with boot mouse.
399         0x00,                                   // bInterfaceSubClass (0x01 = Boot)
400         0x00,                                   // bInterfaceProtocol (0x02 = Mouse)
401 /*
402         0x01,                                   // bInterfaceSubClass (0x01 = Boot)
403         0x02,                                   // bInterfaceProtocol (0x02 = Mouse)
404 */
405         0,                                      // iInterface
406         // HID descriptor, HID 1.11 spec, section 6.2.1
407         9,                                      // bLength
408         0x21,                                   // bDescriptorType
409         0x11, 0x01,                             // bcdHID
410         0,                                      // bCountryCode
411         1,                                      // bNumDescriptors
412         0x22,                                   // bDescriptorType
413         sizeof(mouse_hid_report_desc),          // wDescriptorLength
414         0,
415         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
416         7,                                      // bLength
417         5,                                      // bDescriptorType
418         MOUSE_ENDPOINT | 0x80,                  // bEndpointAddress
419         0x03,                                   // bmAttributes (0x03=intr)
420         MOUSE_SIZE, 0,                          // wMaxPacketSize
421         1,                                      // bInterval
422 #endif
423
424         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
425         9,                                      // bLength
426         4,                                      // bDescriptorType
427         DEBUG_INTERFACE,                        // bInterfaceNumber
428         0,                                      // bAlternateSetting
429         1,                                      // bNumEndpoints
430         0x03,                                   // bInterfaceClass (0x03 = HID)
431         0x00,                                   // bInterfaceSubClass
432         0x00,                                   // bInterfaceProtocol
433         0,                                      // iInterface
434         // HID descriptor, HID 1.11 spec, section 6.2.1
435         9,                                      // bLength
436         0x21,                                   // bDescriptorType
437         0x11, 0x01,                             // bcdHID
438         0,                                      // bCountryCode
439         1,                                      // bNumDescriptors
440         0x22,                                   // bDescriptorType
441         sizeof(debug_hid_report_desc),          // wDescriptorLength
442         0,
443         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
444         7,                                      // bLength
445         5,                                      // bDescriptorType
446         DEBUG_TX_ENDPOINT | 0x80,               // bEndpointAddress
447         0x03,                                   // bmAttributes (0x03=intr)
448         DEBUG_TX_SIZE, 0,                       // wMaxPacketSize
449         1,                                      // bInterval
450
451 #ifdef EXTRAKEY_ENABLE
452         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
453         9,                                      // bLength
454         4,                                      // bDescriptorType
455         EXTRA_INTERFACE,                        // bInterfaceNumber
456         0,                                      // bAlternateSetting
457         1,                                      // bNumEndpoints
458         0x03,                                   // bInterfaceClass (0x03 = HID)
459         0x00,                                   // bInterfaceSubClass
460         0x00,                                   // bInterfaceProtocol
461         0,                                      // iInterface
462         // HID descriptor, HID 1.11 spec, section 6.2.1
463         9,                                      // bLength
464         0x21,                                   // bDescriptorType
465         0x11, 0x01,                             // bcdHID
466         0,                                      // bCountryCode
467         1,                                      // bNumDescriptors
468         0x22,                                   // bDescriptorType
469         sizeof(extra_hid_report_desc),          // wDescriptorLength
470         0,
471         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
472         7,                                      // bLength
473         5,                                      // bDescriptorType
474         EXTRA_ENDPOINT | 0x80,                  // bEndpointAddress
475         0x03,                                   // bmAttributes (0x03=intr)
476         EXTRA_SIZE, 0,                          // wMaxPacketSize
477         10,                                     // bInterval
478 #endif
479
480 #ifdef NKRO_ENABLE
481         // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
482         9,                                      // bLength
483         4,                                      // bDescriptorType
484         KBD2_INTERFACE,                         // bInterfaceNumber
485         0,                                      // bAlternateSetting
486         1,                                      // bNumEndpoints
487         0x03,                                   // bInterfaceClass (0x03 = HID)
488         0x00,                                   // bInterfaceSubClass (0x01 = Boot)
489         0x00,                                   // bInterfaceProtocol (0x01 = Keyboard)
490         0,                                      // iInterface
491         // HID descriptor, HID 1.11 spec, section 6.2.1
492         9,                                      // bLength
493         0x21,                                   // bDescriptorType
494         0x11, 0x01,                             // bcdHID
495         0,                                      // bCountryCode
496         1,                                      // bNumDescriptors
497         0x22,                                   // bDescriptorType
498         sizeof(keyboard2_hid_report_desc),      // wDescriptorLength
499         0,
500         // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
501         7,                                      // bLength
502         5,                                      // bDescriptorType
503         KBD2_ENDPOINT | 0x80,                   // bEndpointAddress
504         0x03,                                   // bmAttributes (0x03=intr)
505         KBD2_SIZE, 0,                           // wMaxPacketSize
506         1,                                      // bInterval
507 #endif
508 };
509
510 // If you're desperate for a little extra code memory, these strings
511 // can be completely removed if iManufacturer, iProduct, iSerialNumber
512 // in the device desciptor are changed to zeros.
513 struct usb_string_descriptor_struct {
514         uint8_t bLength;
515         uint8_t bDescriptorType;
516         int16_t wString[];
517 };
518 static const struct usb_string_descriptor_struct PROGMEM string0 = {
519         4,
520         3,
521         {0x0409}
522 };
523 static const struct usb_string_descriptor_struct PROGMEM string1 = {
524         sizeof(STR_MANUFACTURER),
525         3,
526         STR_MANUFACTURER
527 };
528 static const struct usb_string_descriptor_struct PROGMEM string2 = {
529         sizeof(STR_PRODUCT),
530         3,
531         STR_PRODUCT
532 };
533
534 // This table defines which descriptor data is sent for each specific
535 // request from the host (in wValue and wIndex).
536 static const struct descriptor_list_struct {
537         uint16_t        wValue;     // descriptor type
538         uint16_t        wIndex;
539         const uint8_t   *addr;
540         uint8_t         length;
541 } PROGMEM descriptor_list[] = {
542         // DEVICE descriptor
543         {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
544         // CONFIGURATION descriptor
545         {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
546         // HID/REPORT descriptors
547         {0x2100, KBD_INTERFACE, config1_descriptor+KBD_HID_DESC_OFFSET, 9},
548         {0x2200, KBD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
549 #ifdef MOUSE_ENABLE
550         {0x2100, MOUSE_INTERFACE, config1_descriptor+MOUSE_HID_DESC_OFFSET, 9},
551         {0x2200, MOUSE_INTERFACE, mouse_hid_report_desc, sizeof(mouse_hid_report_desc)},
552 #endif
553         {0x2100, DEBUG_INTERFACE, config1_descriptor+DEBUG_HID_DESC_OFFSET, 9},
554         {0x2200, DEBUG_INTERFACE, debug_hid_report_desc, sizeof(debug_hid_report_desc)},
555 #ifdef EXTRAKEY_ENABLE
556         {0x2100, EXTRA_INTERFACE, config1_descriptor+EXTRA_HID_DESC_OFFSET, 9},
557         {0x2200, EXTRA_INTERFACE, extra_hid_report_desc, sizeof(extra_hid_report_desc)},
558 #endif
559 #ifdef NKRO_ENABLE
560         {0x2100, KBD2_INTERFACE, config1_descriptor+KBD2_HID_DESC_OFFSET, 9},
561         {0x2200, KBD2_INTERFACE, keyboard2_hid_report_desc, sizeof(keyboard2_hid_report_desc)},
562 #endif
563         // STRING descriptors
564         {0x0300, 0x0000, (const uint8_t *)&string0, 4},
565         {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
566         {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)}
567 };
568 #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
569
570
571 /**************************************************************************
572  *
573  *  Variables - these are the only non-stack RAM usage
574  *
575  **************************************************************************/
576
577 // zero when we are not configured, non-zero when enumerated
578 static volatile uint8_t usb_configuration=0;
579
580
581 /**************************************************************************
582  *
583  *  Public Functions - these are the API intended for the user
584  *
585  **************************************************************************/
586
587
588 // initialize USB
589 void usb_init(void)
590 {
591         HW_CONFIG();
592         USB_FREEZE();                           // enable USB
593         PLL_CONFIG();                           // config PLL
594         while (!(PLLCSR & (1<<PLOCK))) ;        // wait for PLL lock
595         USB_CONFIG();                           // start USB clock
596         UDCON = 0;                              // enable attach resistor
597         usb_configuration = 0;
598         UDIEN = (1<<EORSTE)|(1<<SOFE)|(1<<SUSPE);
599         sei();
600 }
601
602 // return 0 if the USB is not configured, or the configuration
603 // number selected by the HOST
604 uint8_t usb_configured(void)
605 {
606         return usb_configuration && !suspend;
607 }
608
609 void usb_remote_wakeup(void)
610 {
611     UDCON |= (1<<RMWKUP);
612 }
613
614
615
616 /**************************************************************************
617  *
618  *  Private Functions - not intended for general user consumption....
619  *
620  **************************************************************************/
621
622
623
624 // USB Device Interrupt - handle all device-level events
625 // the transmit buffer flushing is triggered by the start of frame
626 //
627 ISR(USB_GEN_vect)
628 {
629         uint8_t intbits, t;
630         static uint8_t div4=0;
631
632         intbits = UDINT;
633         UDINT = 0;
634         if (intbits & (1<<SUSPI)) {
635             suspend = true;
636         } else {
637             suspend = false;
638         }
639         if (intbits & (1<<EORSTI)) {
640                 UENUM = 0;
641                 UECONX = 1;
642                 UECFG0X = EP_TYPE_CONTROL;
643                 UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
644                 UEIENX = (1<<RXSTPE);
645                 usb_configuration = 0;
646         }
647         if ((intbits & (1<<SOFI)) && usb_configuration) {
648                 t = debug_flush_timer;
649                 if (t) {
650                         debug_flush_timer = -- t;
651                         if (!t) {
652                                 UENUM = DEBUG_TX_ENDPOINT;
653                                 while ((UEINTX & (1<<RWAL))) {
654                                         UEDATX = 0;
655                                 }
656                                 UEINTX = 0x3A;
657                         }
658                 }
659                 /* TODO: should keep IDLE rate on each keyboard interface */
660 #ifdef NKRO_ENABLE
661                 if (!keyboard_nkro && usb_keyboard_idle_config && (++div4 & 3) == 0) {
662 #else
663                 if (usb_keyboard_idle_config && (++div4 & 3) == 0) {
664 #endif
665                         UENUM = KBD_ENDPOINT;
666                         if (UEINTX & (1<<RWAL)) {
667                                 usb_keyboard_idle_count++;
668                                 if (usb_keyboard_idle_count == usb_keyboard_idle_config) {
669                                         usb_keyboard_idle_count = 0;
670                                         /* TODO: fix keyboard_report inconsistency */
671 /* To avoid Mac SET_IDLE behaviour.
672                                         UEDATX = keyboard_report_prev->mods;
673                                         UEDATX = 0;
674                                         uint8_t keys = usb_keyboard_protocol ? KBD_REPORT_KEYS : 6;
675                                         for (uint8_t i=0; i<keys; i++) {
676                                                 UEDATX = keyboard_report_prev->keys[i];
677                                         }
678                                         UEINTX = 0x3A;
679 */
680                                 }
681                         }
682                 }
683         }
684 }
685
686
687
688 // Misc functions to wait for ready and send/receive packets
689 static inline void usb_wait_in_ready(void)
690 {
691         while (!(UEINTX & (1<<TXINI))) ;
692 }
693 static inline void usb_send_in(void)
694 {
695         UEINTX = ~(1<<TXINI);
696 }
697 static inline void usb_wait_receive_out(void)
698 {
699         while (!(UEINTX & (1<<RXOUTI))) ;
700 }
701 static inline void usb_ack_out(void)
702 {
703         UEINTX = ~(1<<RXOUTI);
704 }
705
706
707
708 // USB Endpoint Interrupt - endpoint 0 is handled here.  The
709 // other endpoints are manipulated by the user-callable
710 // functions, and the start-of-frame interrupt.
711 //
712 ISR(USB_COM_vect)
713 {
714         uint8_t intbits;
715         const uint8_t *list;
716         const uint8_t *cfg;
717         uint8_t i, n, len, en;
718         uint8_t bmRequestType;
719         uint8_t bRequest;
720         uint16_t wValue;
721         uint16_t wIndex;
722         uint16_t wLength;
723         uint16_t desc_val;
724         const uint8_t *desc_addr;
725         uint8_t desc_length;
726
727         UENUM = 0;
728         intbits = UEINTX;
729         if (intbits & (1<<RXSTPI)) {
730                 bmRequestType = UEDATX;
731                 bRequest = UEDATX;
732                 wValue = UEDATX;
733                 wValue |= (UEDATX << 8);
734                 wIndex = UEDATX;
735                 wIndex |= (UEDATX << 8);
736                 wLength = UEDATX;
737                 wLength |= (UEDATX << 8);
738                 UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
739                 if (bRequest == GET_DESCRIPTOR) {
740                         list = (const uint8_t *)descriptor_list;
741                         for (i=0; ; i++) {
742                                 if (i >= NUM_DESC_LIST) {
743                                         UECONX = (1<<STALLRQ)|(1<<EPEN);  //stall
744                                         return;
745                                 }
746                                 desc_val = pgm_read_word(list);
747                                 if (desc_val != wValue) {
748                                         list += sizeof(struct descriptor_list_struct);
749                                         continue;
750                                 }
751                                 list += 2;
752                                 desc_val = pgm_read_word(list);
753                                 if (desc_val != wIndex) {
754                                         list += sizeof(struct descriptor_list_struct)-2;
755                                         continue;
756                                 }
757                                 list += 2;
758                                 desc_addr = (const uint8_t *)pgm_read_word(list);
759                                 list += 2;
760                                 desc_length = pgm_read_byte(list);
761                                 break;
762                         }
763                         len = (wLength < 256) ? wLength : 255;
764                         if (len > desc_length) len = desc_length;
765                         do {
766                                 // wait for host ready for IN packet
767                                 do {
768                                         i = UEINTX;
769                                 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
770                                 if (i & (1<<RXOUTI)) return;    // abort
771                                 // send IN packet
772                                 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
773                                 for (i = n; i; i--) {
774                                         UEDATX = pgm_read_byte(desc_addr++);
775                                 }
776                                 len -= n;
777                                 usb_send_in();
778                         } while (len || n == ENDPOINT0_SIZE);
779                         return;
780                 }
781                 if (bRequest == SET_ADDRESS) {
782                         usb_send_in();
783                         usb_wait_in_ready();
784                         UDADDR = wValue | (1<<ADDEN);
785                         return;
786                 }
787                 if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
788                         usb_configuration = wValue;
789                         usb_send_in();
790                         cfg = endpoint_config_table;
791                         for (i=1; i<=MAX_ENDPOINT; i++) {
792                                 UENUM = i;
793                                 en = pgm_read_byte(cfg++);
794                                 if (en) {
795                                     UECONX = (1<<EPEN);
796                                     UECFG0X = pgm_read_byte(cfg++);
797                                     UECFG1X = pgm_read_byte(cfg++);
798                                 } else {
799                                     UECONX = 0;
800                                 }
801                         }
802                         UERST = UERST_MASK;
803                         UERST = 0;
804                         return;
805                 }
806                 if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
807                         usb_wait_in_ready();
808                         UEDATX = usb_configuration;
809                         usb_send_in();
810                         return;
811                 }
812
813                 if (bRequest == GET_STATUS) {
814                         usb_wait_in_ready();
815                         i = 0;
816                         #ifdef SUPPORT_ENDPOINT_HALT
817                         if (bmRequestType == 0x82) {
818                                 UENUM = wIndex;
819                                 if (UECONX & (1<<STALLRQ)) i = 1;
820                                 UENUM = 0;
821                         }
822                         #endif
823                         UEDATX = i;
824                         UEDATX = 0;
825                         usb_send_in();
826                         return;
827                 }
828                 if (bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE) {
829 #ifdef SUPPORT_ENDPOINT_HALT
830                     if (bmRequestType == 0x02 && wValue == ENDPOINT_HALT) {
831                         i = wIndex & 0x7F;
832                         if (i >= 1 && i <= MAX_ENDPOINT) {
833                                 usb_send_in();
834                                 UENUM = i;
835                                 if (bRequest == SET_FEATURE) {
836                                         UECONX = (1<<STALLRQ)|(1<<EPEN);
837                                 } else {
838                                         UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
839                                         UERST = (1 << i);
840                                         UERST = 0;
841                                 }
842                                 return;
843                         }
844                     }
845 #endif
846                     if (bmRequestType == 0x00 && wValue == DEVICE_REMOTE_WAKEUP) {
847                         if (bRequest == SET_FEATURE) {
848                             remote_wakeup = true;   
849                         } else {
850                             remote_wakeup = false;
851                         }
852                         usb_send_in();
853                         return;
854                     }
855                 }
856                 if (wIndex == KBD_INTERFACE) {
857                         if (bmRequestType == 0xA1) {
858                                 if (bRequest == HID_GET_REPORT) {
859                                         usb_wait_in_ready();
860                                         UEDATX = keyboard_report->mods;
861                                         UEDATX = 0;
862                                         for (i=0; i<6; i++) {
863                                                 UEDATX = keyboard_report->keys[i];
864                                         }
865                                         usb_send_in();
866                                         return;
867                                 }
868                                 if (bRequest == HID_GET_IDLE) {
869                                         usb_wait_in_ready();
870                                         UEDATX = usb_keyboard_idle_config;
871                                         usb_send_in();
872                                         return;
873                                 }
874                                 if (bRequest == HID_GET_PROTOCOL) {
875                                         usb_wait_in_ready();
876                                         UEDATX = usb_keyboard_protocol;
877                                         usb_send_in();
878                                         return;
879                                 }
880                         }
881                         if (bmRequestType == 0x21) {
882                                 if (bRequest == HID_SET_REPORT) {
883                                         usb_wait_receive_out();
884                                         usb_keyboard_leds = UEDATX;
885                                         usb_ack_out();
886                                         usb_send_in();
887                                         return;
888                                 }
889                                 if (bRequest == HID_SET_IDLE) {
890                                         usb_keyboard_idle_config = (wValue >> 8);
891                                         usb_keyboard_idle_count = 0;
892                                         //usb_wait_in_ready();
893                                         usb_send_in();
894                                         return;
895                                 }
896                                 if (bRequest == HID_SET_PROTOCOL) {
897                                         usb_keyboard_protocol = wValue;
898                                         //usb_wait_in_ready();
899                                         usb_send_in();
900                                         return;
901                                 }
902                         }
903                 }
904 #ifdef MOUSE_ENABLE
905                 if (wIndex == MOUSE_INTERFACE) {
906                         if (bmRequestType == 0xA1) {
907                                 if (bRequest == HID_GET_REPORT) {
908                                     if (wValue == HID_REPORT_INPUT) {
909                                         usb_wait_in_ready();
910                                         UEDATX = 0;
911                                         UEDATX = 0;
912                                         UEDATX = 0;
913                                         UEDATX = 0;
914                                         usb_send_in();
915                                         return;
916                                     }
917                                     if (wValue == HID_REPORT_FEATURE) {
918                                         usb_wait_in_ready();
919                                         UEDATX = 0x05;
920                                         usb_send_in();
921                                         return;
922                                     }
923                                 }
924                                 if (bRequest == HID_GET_PROTOCOL) {
925                                         usb_wait_in_ready();
926                                         UEDATX = usb_mouse_protocol;
927                                         usb_send_in();
928                                         return;
929                                 }
930                         }
931                         if (bmRequestType == 0x21) {
932                                 if (bRequest == HID_SET_PROTOCOL) {
933                                         usb_mouse_protocol = wValue;
934                                         usb_send_in();
935                                         return;
936                                 }
937                         }
938                 }
939 #endif
940                 if (wIndex == DEBUG_INTERFACE) {
941                         if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) {
942                                 len = wLength;
943                                 do {
944                                         // wait for host ready for IN packet
945                                         do {
946                                                 i = UEINTX;
947                                         } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
948                                         if (i & (1<<RXOUTI)) return;    // abort
949                                         // send IN packet
950                                         n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
951                                         for (i = n; i; i--) {
952                                                 UEDATX = 0;
953                                         }
954                                         len -= n;
955                                         usb_send_in();
956                                 } while (len || n == ENDPOINT0_SIZE);
957                                 return;
958                         }
959                 }
960         }
961         UECONX = (1<<STALLRQ) | (1<<EPEN);      // stall
962 }