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