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
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
27 #include <avr/pgmspace.h>
28 #include <avr/interrupt.h>
30 #include "usb_keyboard.h"
31 #include "usb_mouse.h"
32 #include "usb_debug.h"
33 #include "usb_extra.h"
37 #ifdef SLEEP_LED_ENABLE
38 #include "sleep_led.h"
42 #include "action_util.h"
45 /**************************************************************************
47 * Configurable Options
49 **************************************************************************/
51 // You can change these to give your code its own name.
53 # define STR_MANUFACTURER L"t.m.k."
55 # define STR_MANUFACTURER LSTR(MANUFACTURER)
58 # define STR_PRODUCT L"t.m.k. keyboard"
60 # define STR_PRODUCT LSTR(PRODUCT)
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.
69 # define VENDOR_ID 0xFEED
73 # define PRODUCT_ID 0xBABE
77 # define DEVICE_VER 0x0100
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
86 #define SUPPORT_ENDPOINT_HALT
90 /**************************************************************************
92 * Endpoint Buffer Configuration
94 **************************************************************************/
96 #define ENDPOINT0_SIZE 32
98 bool remote_wakeup = false;
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
106 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(MOUSE_SIZE) | MOUSE_BUFFER, // 2
110 #ifdef CONSOLE_ENABLE
111 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER, // 3
115 #ifdef EXTRAKEY_ENABLE
116 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(EXTRA_SIZE) | EXTRA_BUFFER, // 4
121 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KBD2_SIZE) | KBD2_BUFFER, // 5
129 /**************************************************************************
133 **************************************************************************/
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!
142 static const uint8_t PROGMEM device_descriptor[] = {
144 1, // bDescriptorType
145 0x00, 0x02, // bcdUSB
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
156 1 // bNumConfigurations
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
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
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),
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
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[] = {
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
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
296 0x81, 0x02, // Input (array)
297 0xC0 // end collection
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[] = {
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
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
333 #define KBD_HID_DESC_NUM 0
334 #define KBD_HID_DESC_OFFSET (9+(9+9+7)*KBD_HID_DESC_NUM+9)
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)
340 # define MOUSE_HID_DESC_NUM (KBD_HID_DESC_NUM + 0)
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)
347 # define DEBUG_HID_DESC_NUM (MOUSE_HID_DESC_NUM + 0)
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)
354 # define EXTRA_HID_DESC_NUM (DEBUG_HID_DESC_NUM + 0)
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)
361 # define KBD2_HID_DESC_NUM (EXTRA_HID_DESC_NUM + 0)
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
369 2, // bDescriptorType;
370 LSB(CONFIG1_DESC_SIZE), // wTotalLength
371 MSB(CONFIG1_DESC_SIZE),
372 NUM_INTERFACES, // bNumInterfaces
373 1, // bConfigurationValue
375 0xA0, // bmAttributes
378 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
380 4, // bDescriptorType
381 KBD_INTERFACE, // bInterfaceNumber
382 0, // bAlternateSetting
384 0x03, // bInterfaceClass (0x03 = HID)
385 0x01, // bInterfaceSubClass (0x01 = Boot)
386 0x01, // bInterfaceProtocol (0x01 = Keyboard)
388 // HID descriptor, HID 1.11 spec, section 6.2.1
390 0x21, // bDescriptorType
391 0x11, 0x01, // bcdHID
393 1, // bNumDescriptors
394 0x22, // bDescriptorType
395 sizeof(keyboard_hid_report_desc), // wDescriptorLength
397 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
399 5, // bDescriptorType
400 KBD_ENDPOINT | 0x80, // bEndpointAddress
401 0x03, // bmAttributes (0x03=intr)
402 KBD_SIZE, 0, // wMaxPacketSize
406 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
408 4, // bDescriptorType
409 MOUSE_INTERFACE, // bInterfaceNumber
410 0, // bAlternateSetting
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)
417 0x01, // bInterfaceSubClass (0x01 = Boot)
418 0x02, // bInterfaceProtocol (0x02 = Mouse)
421 // HID descriptor, HID 1.11 spec, section 6.2.1
423 0x21, // bDescriptorType
424 0x11, 0x01, // bcdHID
426 1, // bNumDescriptors
427 0x22, // bDescriptorType
428 sizeof(mouse_hid_report_desc), // wDescriptorLength
430 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
432 5, // bDescriptorType
433 MOUSE_ENDPOINT | 0x80, // bEndpointAddress
434 0x03, // bmAttributes (0x03=intr)
435 MOUSE_SIZE, 0, // wMaxPacketSize
439 #ifdef CONSOLE_ENABLE
440 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
442 4, // bDescriptorType
443 DEBUG_INTERFACE, // bInterfaceNumber
444 0, // bAlternateSetting
446 0x03, // bInterfaceClass (0x03 = HID)
447 0x00, // bInterfaceSubClass
448 0x00, // bInterfaceProtocol
450 // HID descriptor, HID 1.11 spec, section 6.2.1
452 0x21, // bDescriptorType
453 0x11, 0x01, // bcdHID
455 1, // bNumDescriptors
456 0x22, // bDescriptorType
457 sizeof(debug_hid_report_desc), // wDescriptorLength
459 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
461 5, // bDescriptorType
462 DEBUG_TX_ENDPOINT | 0x80, // bEndpointAddress
463 0x03, // bmAttributes (0x03=intr)
464 DEBUG_TX_SIZE, 0, // wMaxPacketSize
468 #ifdef EXTRAKEY_ENABLE
469 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
471 4, // bDescriptorType
472 EXTRA_INTERFACE, // bInterfaceNumber
473 0, // bAlternateSetting
475 0x03, // bInterfaceClass (0x03 = HID)
476 0x00, // bInterfaceSubClass
477 0x00, // bInterfaceProtocol
479 // HID descriptor, HID 1.11 spec, section 6.2.1
481 0x21, // bDescriptorType
482 0x11, 0x01, // bcdHID
484 1, // bNumDescriptors
485 0x22, // bDescriptorType
486 sizeof(extra_hid_report_desc), // wDescriptorLength
488 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
490 5, // bDescriptorType
491 EXTRA_ENDPOINT | 0x80, // bEndpointAddress
492 0x03, // bmAttributes (0x03=intr)
493 EXTRA_SIZE, 0, // wMaxPacketSize
498 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
500 4, // bDescriptorType
501 KBD2_INTERFACE, // bInterfaceNumber
502 0, // bAlternateSetting
504 0x03, // bInterfaceClass (0x03 = HID)
505 0x00, // bInterfaceSubClass (0x01 = Boot)
506 0x00, // bInterfaceProtocol (0x01 = Keyboard)
508 // HID descriptor, HID 1.11 spec, section 6.2.1
510 0x21, // bDescriptorType
511 0x11, 0x01, // bcdHID
513 1, // bNumDescriptors
514 0x22, // bDescriptorType
515 sizeof(keyboard2_hid_report_desc), // wDescriptorLength
517 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
519 5, // bDescriptorType
520 KBD2_ENDPOINT | 0x80, // bEndpointAddress
521 0x03, // bmAttributes (0x03=intr)
522 KBD2_SIZE, 0, // wMaxPacketSize
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 {
532 uint8_t bDescriptorType;
535 static const struct usb_string_descriptor_struct PROGMEM string0 = {
540 static const struct usb_string_descriptor_struct PROGMEM string1 = {
541 sizeof(STR_MANUFACTURER),
545 static const struct usb_string_descriptor_struct PROGMEM string2 = {
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
558 } PROGMEM descriptor_list[] = {
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)},
567 {0x2100, MOUSE_INTERFACE, config1_descriptor+MOUSE_HID_DESC_OFFSET, 9},
568 {0x2200, MOUSE_INTERFACE, mouse_hid_report_desc, sizeof(mouse_hid_report_desc)},
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)},
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)},
579 {0x2100, KBD2_INTERFACE, config1_descriptor+KBD2_HID_DESC_OFFSET, 9},
580 {0x2200, KBD2_INTERFACE, keyboard2_hid_report_desc, sizeof(keyboard2_hid_report_desc)},
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)}
587 #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
590 /**************************************************************************
592 * Variables - these are the only non-stack RAM usage
594 **************************************************************************/
596 // zero when we are not configured, non-zero when enumerated
597 static volatile uint8_t usb_configuration=0;
600 /**************************************************************************
602 * Public Functions - these are the API intended for the user
604 **************************************************************************/
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;
618 UDIEN = (1<<EORSTE)|(1<<SOFE)|(1<<SUSPE)|(1<<WAKEUPE);
622 // return 0 if the USB is not configured, or the configuration
623 // number selected by the HOST
624 uint8_t usb_configured(void)
626 return usb_configuration && !suspend;
629 void usb_remote_wakeup(void)
631 UDCON |= (1<<RMWKUP);
632 while (UDCON & (1<<RMWKUP));
637 /**************************************************************************
639 * Private Functions - not intended for general user consumption....
641 **************************************************************************/
645 // USB Device Interrupt - handle all device-level events
646 // the transmit buffer flushing is triggered by the start of frame
651 static uint8_t div4=0;
655 if ((intbits & (1<<SUSPI)) && (UDIEN & (1<<SUSPE)) && usb_configuration) {
656 #ifdef SLEEP_LED_ENABLE
659 UDIEN &= ~(1<<SUSPE);
660 UDIEN |= (1<<WAKEUPE);
663 if ((intbits & (1<<WAKEUPI)) && (UDIEN & (1<<WAKEUPE)) && usb_configuration) {
664 suspend_wakeup_init();
665 #ifdef SLEEP_LED_ENABLE
667 // NOTE: converters may not accept this
668 led_set(host_keyboard_leds());
672 UDIEN &= ~(1<<WAKEUPE);
675 if (intbits & (1<<EORSTI)) {
678 UECFG0X = EP_TYPE_CONTROL;
679 UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
680 UEIENX = (1<<RXSTPE);
681 usb_configuration = 0;
683 if ((intbits & (1<<SOFI)) && usb_configuration) {
684 t = debug_flush_timer;
686 debug_flush_timer = -- t;
688 UENUM = DEBUG_TX_ENDPOINT;
689 while ((UEINTX & (1<<RWAL))) {
695 /* TODO: should keep IDLE rate on each keyboard interface */
697 if (!keyboard_nkro && keyboard_idle && (++div4 & 3) == 0) {
699 if (keyboard_idle && (++div4 & 3) == 0) {
701 UENUM = KBD_ENDPOINT;
702 if (UEINTX & (1<<RWAL)) {
703 usb_keyboard_idle_count++;
704 if (usb_keyboard_idle_count == keyboard_idle) {
705 usb_keyboard_idle_count = 0;
706 /* TODO: fix keyboard_report inconsistency */
707 /* To avoid Mac SET_IDLE behaviour.
708 UEDATX = keyboard_report_prev->mods;
710 uint8_t keys = keyboard_protocol ? KBD_REPORT_KEYS : 6;
711 for (uint8_t i=0; i<keys; i++) {
712 UEDATX = keyboard_report_prev->keys[i];
724 // Misc functions to wait for ready and send/receive packets
725 static inline void usb_wait_in_ready(void)
727 while (!(UEINTX & (1<<TXINI))) ;
729 static inline void usb_send_in(void)
731 UEINTX = ~(1<<TXINI);
733 static inline void usb_wait_receive_out(void)
735 while (!(UEINTX & (1<<RXOUTI))) ;
737 static inline void usb_ack_out(void)
739 UEINTX = ~(1<<RXOUTI);
744 // USB Endpoint Interrupt - endpoint 0 is handled here. The
745 // other endpoints are manipulated by the user-callable
746 // functions, and the start-of-frame interrupt.
753 uint8_t i, n, len, en;
754 uint8_t bmRequestType;
760 const uint8_t *desc_addr;
765 if (intbits & (1<<RXSTPI)) {
766 bmRequestType = UEDATX;
769 wValue |= (UEDATX << 8);
771 wIndex |= (UEDATX << 8);
773 wLength |= (UEDATX << 8);
774 UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
775 if (bRequest == GET_DESCRIPTOR) {
776 list = (const uint8_t *)descriptor_list;
778 if (i >= NUM_DESC_LIST) {
779 UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
782 desc_val = pgm_read_word(list);
783 if (desc_val != wValue) {
784 list += sizeof(struct descriptor_list_struct);
788 desc_val = pgm_read_word(list);
789 if (desc_val != wIndex) {
790 list += sizeof(struct descriptor_list_struct)-2;
794 desc_addr = (const uint8_t *)pgm_read_word(list);
796 desc_length = pgm_read_byte(list);
799 len = (wLength < 256) ? wLength : 255;
800 if (len > desc_length) len = desc_length;
802 // wait for host ready for IN packet
805 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
806 if (i & (1<<RXOUTI)) return; // abort
808 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
809 for (i = n; i; i--) {
810 UEDATX = pgm_read_byte(desc_addr++);
814 } while (len || n == ENDPOINT0_SIZE);
817 if (bRequest == SET_ADDRESS) {
820 UDADDR = wValue | (1<<ADDEN);
823 if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
824 usb_configuration = wValue;
826 cfg = endpoint_config_table;
827 for (i=1; i<=MAX_ENDPOINT; i++) {
829 en = pgm_read_byte(cfg++);
832 UECFG0X = pgm_read_byte(cfg++);
833 UECFG1X = pgm_read_byte(cfg++);
842 if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
844 UEDATX = usb_configuration;
849 if (bRequest == GET_STATUS) {
852 #ifdef SUPPORT_ENDPOINT_HALT
853 if (bmRequestType == 0x82) {
855 if (UECONX & (1<<STALLRQ)) i = 1;
864 if (bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE) {
865 #ifdef SUPPORT_ENDPOINT_HALT
866 if (bmRequestType == 0x02 && wValue == ENDPOINT_HALT) {
868 if (i >= 1 && i <= MAX_ENDPOINT) {
871 if (bRequest == SET_FEATURE) {
872 UECONX = (1<<STALLRQ)|(1<<EPEN);
874 UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
882 if (bmRequestType == 0x00 && wValue == DEVICE_REMOTE_WAKEUP) {
883 if (bRequest == SET_FEATURE) {
884 remote_wakeup = true;
886 remote_wakeup = false;
892 if (wIndex == KBD_INTERFACE) {
893 if (bmRequestType == 0xA1) {
894 if (bRequest == HID_GET_REPORT) {
896 UEDATX = keyboard_report->mods;
898 for (i=0; i<6; i++) {
899 UEDATX = keyboard_report->keys[i];
904 if (bRequest == HID_GET_IDLE) {
906 UEDATX = keyboard_idle;
910 if (bRequest == HID_GET_PROTOCOL) {
912 UEDATX = keyboard_protocol;
917 if (bmRequestType == 0x21) {
918 if (bRequest == HID_SET_REPORT) {
919 usb_wait_receive_out();
920 usb_keyboard_leds = UEDATX;
925 if (bRequest == HID_SET_IDLE) {
926 keyboard_idle = (wValue >> 8);
927 usb_keyboard_idle_count = 0;
928 //usb_wait_in_ready();
932 if (bRequest == HID_SET_PROTOCOL) {
933 keyboard_protocol = wValue;
935 keyboard_nkro = !!keyboard_protocol;
938 //usb_wait_in_ready();
945 if (wIndex == MOUSE_INTERFACE) {
946 if (bmRequestType == 0xA1) {
947 if (bRequest == HID_GET_REPORT) {
948 if (wValue == HID_REPORT_INPUT) {
957 if (wValue == HID_REPORT_FEATURE) {
964 if (bRequest == HID_GET_PROTOCOL) {
966 UEDATX = usb_mouse_protocol;
971 if (bmRequestType == 0x21) {
972 if (bRequest == HID_SET_PROTOCOL) {
973 usb_mouse_protocol = wValue;
980 if (wIndex == DEBUG_INTERFACE) {
981 if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) {
984 // wait for host ready for IN packet
987 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
988 if (i & (1<<RXOUTI)) return; // abort
990 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
991 for (i = n; i; i--) {
996 } while (len || n == ENDPOINT0_SIZE);
1001 UECONX = (1<<STALLRQ) | (1<<EPEN); // stall