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