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