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
25 #include <avr/pgmspace.h>
26 #include <avr/interrupt.h>
28 #include "usb_keyboard.h"
29 #include "usb_mouse.h"
30 #include "usb_debug.h"
31 #include "usb_extra.h"
36 /**************************************************************************
38 * Configurable Options
40 **************************************************************************/
42 // You can change these to give your code its own name.
44 # define STR_MANUFACTURER L"t.m.k."
46 # define STR_MANUFACTURER LSTR(MANUFACTURER)
49 # define STR_PRODUCT L"t.m.k. keyboard"
51 # define STR_PRODUCT LSTR(PRODUCT)
55 // Mac OS-X and Linux automatically load the correct drivers. On
56 // Windows, even though the driver is supplied by Microsoft, an
57 // INF file is needed to load the driver. These numbers need to
58 // match the INF file.
60 # define VENDOR_ID 0xFEED
64 # define PRODUCT_ID 0xBABE
68 // USB devices are supposed to implment a halt feature, which is
69 // rarely (if ever) used. If you comment this line out, the halt
70 // code will be removed, saving 102 bytes of space (gcc 4.3.0).
71 // This is not strictly USB compliant, but works with all major
73 #define SUPPORT_ENDPOINT_HALT
77 /**************************************************************************
79 * Endpoint Buffer Configuration
81 **************************************************************************/
83 #define ENDPOINT0_SIZE 32
85 // 0:control endpoint is enabled automatically by controller.
86 static const uint8_t PROGMEM endpoint_config_table[] = {
87 // enable, UECFG0X(type, direction), UECFG1X(size, bank, allocation)
88 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KEYBOARD_SIZE) | KEYBOARD_BUFFER, // 1
89 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(MOUSE_SIZE) | MOUSE_BUFFER, // 2
90 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER, // 3
91 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(EXTRA_SIZE) | EXTRA_BUFFER, // 4
97 /**************************************************************************
101 **************************************************************************/
103 // Descriptors are the data that your computer reads when it auto-detects
104 // this USB device (called "enumeration" in USB lingo). The most commonly
105 // changed items are editable at the top of this file. Changing things
106 // in here should only be done by those who've read chapter 9 of the USB
107 // spec and relevant portions of any USB class specifications!
110 static uint8_t PROGMEM device_descriptor[] = {
112 1, // bDescriptorType
113 0x00, 0x02, // bcdUSB
115 0, // bDeviceSubClass
116 0, // bDeviceProtocol
117 ENDPOINT0_SIZE, // bMaxPacketSize0
118 LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
119 LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
120 0x00, 0x01, // bcdDevice
124 1 // bNumConfigurations
127 // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
128 static uint8_t PROGMEM keyboard_hid_report_desc[] = {
129 0x05, 0x01, // Usage Page (Generic Desktop),
130 0x09, 0x06, // Usage (Keyboard),
131 0xA1, 0x01, // Collection (Application),
132 0x75, 0x01, // Report Size (1),
133 0x95, 0x08, // Report Count (8),
134 0x05, 0x07, // Usage Page (Key Codes),
135 0x19, 0xE0, // Usage Minimum (224),
136 0x29, 0xE7, // Usage Maximum (231),
137 0x15, 0x00, // Logical Minimum (0),
138 0x25, 0x01, // Logical Maximum (1),
139 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
140 0x95, 0x01, // Report Count (1),
141 0x75, 0x08, // Report Size (8),
142 0x81, 0x03, // Input (Constant), ;Reserved byte
143 0x95, 0x05, // Report Count (5),
144 0x75, 0x01, // Report Size (1),
145 0x05, 0x08, // Usage Page (LEDs),
146 0x19, 0x01, // Usage Minimum (1),
147 0x29, 0x05, // Usage Maximum (5),
148 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
149 0x95, 0x01, // Report Count (1),
150 0x75, 0x03, // Report Size (3),
151 0x91, 0x03, // Output (Constant), ;LED report padding
152 0x95, 0x06, // Report Count (6),
153 0x75, 0x08, // Report Size (8),
154 0x15, 0x00, // Logical Minimum (0),
155 0x25, 0x68, // Logical Maximum(104),
156 0x05, 0x07, // Usage Page (Key Codes),
157 0x19, 0x00, // Usage Minimum (0),
158 0x29, 0x68, // Usage Maximum (104),
159 0x81, 0x00, // Input (Data, Array),
160 0xc0 // End Collection
163 // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
164 // http://www.microchip.com/forums/tm.aspx?high=&m=391435&mpage=1#391521
165 // http://www.keil.com/forum/15671/
166 // http://www.microsoft.com/whdc/device/input/wheel.mspx
167 static uint8_t PROGMEM mouse_hid_report_desc[] = {
168 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
169 0x09, 0x02, // USAGE (Mouse)
170 0xa1, 0x01, // COLLECTION (Application)
171 0x09, 0x02, // USAGE (Mouse)
172 0xa1, 0x02, // COLLECTION (Logical)
173 0x09, 0x01, // USAGE (Pointer)
174 0xa1, 0x00, // COLLECTION (Physical)
175 // ------------------------------ Buttons
176 0x05, 0x09, // USAGE_PAGE (Button)
177 0x19, 0x01, // USAGE_MINIMUM (Button 1)
178 0x29, 0x05, // USAGE_MAXIMUM (Button 5)
179 0x15, 0x00, // LOGICAL_MINIMUM (0)
180 0x25, 0x01, // LOGICAL_MAXIMUM (1)
181 0x75, 0x01, // REPORT_SIZE (1)
182 0x95, 0x05, // REPORT_COUNT (5)
183 0x81, 0x02, // INPUT (Data,Var,Abs)
184 // ------------------------------ Padding
185 0x75, 0x03, // REPORT_SIZE (3)
186 0x95, 0x01, // REPORT_COUNT (1)
187 0x81, 0x03, // INPUT (Cnst,Var,Abs)
188 // ------------------------------ X,Y position
189 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
190 0x09, 0x30, // USAGE (X)
191 0x09, 0x31, // USAGE (Y)
192 0x15, 0x81, // LOGICAL_MINIMUM (-127)
193 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
194 0x75, 0x08, // REPORT_SIZE (8)
195 0x95, 0x02, // REPORT_COUNT (2)
196 0x81, 0x06, // INPUT (Data,Var,Rel)
197 0xa1, 0x02, // COLLECTION (Logical)
198 // ------------------------------ Vertical wheel res multiplier
199 0x09, 0x48, // USAGE (Resolution Multiplier)
200 0x15, 0x00, // LOGICAL_MINIMUM (0)
201 0x25, 0x01, // LOGICAL_MAXIMUM (1)
202 0x35, 0x01, // PHYSICAL_MINIMUM (1)
203 0x45, 0x04, // PHYSICAL_MAXIMUM (4)
204 0x75, 0x02, // REPORT_SIZE (2)
205 0x95, 0x01, // REPORT_COUNT (1)
207 0xb1, 0x02, // FEATURE (Data,Var,Abs)
208 // ------------------------------ Vertical wheel
209 0x09, 0x38, // USAGE (Wheel)
210 0x15, 0x81, // LOGICAL_MINIMUM (-127)
211 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
212 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
213 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
214 0x75, 0x08, // REPORT_SIZE (8)
215 0x81, 0x06, // INPUT (Data,Var,Rel)
216 0xc0, // END_COLLECTION
217 0xa1, 0x02, // COLLECTION (Logical)
218 // ------------------------------ Horizontal wheel res multiplier
219 0x09, 0x48, // USAGE (Resolution Multiplier)
221 0xb1, 0x02, // FEATURE (Data,Var,Abs)
222 // ------------------------------ Padding for Feature report
223 0x35, 0x00, // PHYSICAL_MINIMUM (0) - reset physical
224 0x45, 0x00, // PHYSICAL_MAXIMUM (0)
225 0x75, 0x04, // REPORT_SIZE (4)
226 0xb1, 0x03, // FEATURE (Cnst,Var,Abs)
227 // ------------------------------ Horizontal wheel
228 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
229 0x0a, 0x38, 0x02, // USAGE (AC Pan)
230 0x15, 0x81, // LOGICAL_MINIMUM (-127)
231 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
232 0x75, 0x08, // REPORT_SIZE (8)
233 0x81, 0x06, // INPUT (Data,Var,Rel)
234 0xc0, // END_COLLECTION
235 0xc0, // END_COLLECTION
236 0xc0, // END_COLLECTION
237 0xc0 // END_COLLECTION
240 static uint8_t PROGMEM debug_hid_report_desc[] = {
241 0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined)
242 0x09, 0x74, // Usage 0x74
243 0xA1, 0x53, // Collection 0x53
244 0x75, 0x08, // report size = 8 bits
245 0x15, 0x00, // logical minimum = 0
246 0x26, 0xFF, 0x00, // logical maximum = 255
247 0x95, DEBUG_TX_SIZE, // report count
249 0x81, 0x02, // Input (array)
250 0xC0 // end collection
253 // audio controls(consumer page)
254 // http://www.microsoft.com/whdc/archive/w2kbd.mspx
255 static uint8_t PROGMEM extra_hid_report_desc[] = {
256 0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
257 0x09, 0x01, // USAGE (Consumer Control)
258 0xa1, 0x01, // COLLECTION (Application)
259 0x85, 0x01, // REPORT_ID (1)
260 0x09, 0xe9, // USAGE (Volume Up)
261 0x09, 0xea, // USAGE (Volume Down)
262 0x15, 0x00, // LOGICAL_MINIMUM (0)
263 0x25, 0x01, // LOGICAL_MAXIMUM (1)
264 0x75, 0x01, // REPORT_SIZE (1)
265 0x95, 0x02, // REPORT_COUNT (2)
266 0x81, 0x02, // INPUT (Data,Var,Abs)
267 0x09, 0xe2, // USAGE (Mute)
268 0x15, 0x00, // LOGICAL_MINIMUM (0)
269 0x25, 0x01, // LOGICAL_MAXIMUM (1)
270 0x75, 0x01, // REPORT_SIZE (1)
271 0x95, 0x01, // REPORT_COUNT (1)
272 0x81, 0x06, // INPUT (Data,Var,Rel)
273 0x95, 0x05, // REPORT_COUNT (5)
274 0x81, 0x07, // INPUT (Cnst,Var,Abs)
275 0xc0 // END_COLLECTION
278 #define CONFIG1_DESC_SIZE (9+(9+9+7)*4)
279 #define KEYBOARD_HID_DESC_OFFSET (9+(9+9+7)*0+9)
280 #define MOUSE_HID_DESC_OFFSET (9+(9+9+7)*1+9)
281 #define DEBUG_HID_DESC_OFFSET (9+(9+9+7)*2+9)
282 #define EXTRA_HID_DESC_OFFSET (9+(9+9+7)*3+9)
283 static uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
284 // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
286 2, // bDescriptorType;
287 LSB(CONFIG1_DESC_SIZE), // wTotalLength
288 MSB(CONFIG1_DESC_SIZE),
290 1, // bConfigurationValue
292 0xC0, // bmAttributes
295 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
297 4, // bDescriptorType
298 KEYBOARD_INTERFACE, // bInterfaceNumber
299 0, // bAlternateSetting
301 0x03, // bInterfaceClass (0x03 = HID)
302 0x01, // bInterfaceSubClass (0x01 = Boot)
303 0x01, // bInterfaceProtocol (0x01 = Keyboard)
305 // HID descriptor, HID 1.11 spec, section 6.2.1
307 0x21, // bDescriptorType
308 0x11, 0x01, // bcdHID
310 1, // bNumDescriptors
311 0x22, // bDescriptorType
312 sizeof(keyboard_hid_report_desc), // wDescriptorLength
314 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
316 5, // bDescriptorType
317 KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
318 0x03, // bmAttributes (0x03=intr)
319 KEYBOARD_SIZE, 0, // wMaxPacketSize
322 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
324 4, // bDescriptorType
325 MOUSE_INTERFACE, // bInterfaceNumber
326 0, // bAlternateSetting
328 0x03, // bInterfaceClass (0x03 = HID)
329 0x01, // bInterfaceSubClass (0x01 = Boot)
330 0x02, // bInterfaceProtocol (0x02 = Mouse)
332 // HID descriptor, HID 1.11 spec, section 6.2.1
334 0x21, // bDescriptorType
335 0x11, 0x01, // bcdHID
337 1, // bNumDescriptors
338 0x22, // bDescriptorType
339 sizeof(mouse_hid_report_desc), // wDescriptorLength
341 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
343 5, // bDescriptorType
344 MOUSE_ENDPOINT | 0x80, // bEndpointAddress
345 0x03, // bmAttributes (0x03=intr)
346 MOUSE_SIZE, 0, // wMaxPacketSize
349 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
351 4, // bDescriptorType
352 DEBUG_INTERFACE, // bInterfaceNumber
353 0, // bAlternateSetting
355 0x03, // bInterfaceClass (0x03 = HID)
356 0x00, // bInterfaceSubClass
357 0x00, // bInterfaceProtocol
359 // HID descriptor, HID 1.11 spec, section 6.2.1
361 0x21, // bDescriptorType
362 0x11, 0x01, // bcdHID
364 1, // bNumDescriptors
365 0x22, // bDescriptorType
366 sizeof(debug_hid_report_desc), // wDescriptorLength
368 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
370 5, // bDescriptorType
371 DEBUG_TX_ENDPOINT | 0x80, // bEndpointAddress
372 0x03, // bmAttributes (0x03=intr)
373 DEBUG_TX_SIZE, 0, // wMaxPacketSize
376 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
378 4, // bDescriptorType
379 EXTRA_INTERFACE, // bInterfaceNumber
380 0, // bAlternateSetting
382 0x03, // bInterfaceClass (0x03 = HID)
383 0x00, // bInterfaceSubClass
384 0x00, // bInterfaceProtocol
386 // HID descriptor, HID 1.11 spec, section 6.2.1
388 0x21, // bDescriptorType
389 0x11, 0x01, // bcdHID
391 1, // bNumDescriptors
392 0x22, // bDescriptorType
393 sizeof(extra_hid_report_desc), // wDescriptorLength
395 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
397 5, // bDescriptorType
398 EXTRA_ENDPOINT | 0x80, // bEndpointAddress
399 0x03, // bmAttributes (0x03=intr)
400 EXTRA_SIZE, 0, // wMaxPacketSize
404 // If you're desperate for a little extra code memory, these strings
405 // can be completely removed if iManufacturer, iProduct, iSerialNumber
406 // in the device desciptor are changed to zeros.
407 struct usb_string_descriptor_struct {
409 uint8_t bDescriptorType;
412 static struct usb_string_descriptor_struct PROGMEM string0 = {
417 static struct usb_string_descriptor_struct PROGMEM string1 = {
418 sizeof(STR_MANUFACTURER),
422 static struct usb_string_descriptor_struct PROGMEM string2 = {
428 // This table defines which descriptor data is sent for each specific
429 // request from the host (in wValue and wIndex).
430 static struct descriptor_list_struct {
431 uint16_t wValue; // descriptor type
435 } PROGMEM descriptor_list[] = {
437 {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
438 // CONFIGURATION descriptor
439 {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
441 {0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
442 {0x2100, KEYBOARD_INTERFACE, config1_descriptor+KEYBOARD_HID_DESC_OFFSET, 9},
444 {0x2200, MOUSE_INTERFACE, mouse_hid_report_desc, sizeof(mouse_hid_report_desc)},
445 {0x2100, MOUSE_INTERFACE, config1_descriptor+MOUSE_HID_DESC_OFFSET, 9},
447 {0x2200, DEBUG_INTERFACE, debug_hid_report_desc, sizeof(debug_hid_report_desc)},
448 {0x2100, DEBUG_INTERFACE, config1_descriptor+DEBUG_HID_DESC_OFFSET, 9},
450 {0x2200, EXTRA_INTERFACE, extra_hid_report_desc, sizeof(extra_hid_report_desc)},
451 {0x2100, EXTRA_INTERFACE, config1_descriptor+EXTRA_HID_DESC_OFFSET, 9},
453 {0x0300, 0x0000, (const uint8_t *)&string0, 4},
454 {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
455 {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)}
457 #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
460 /**************************************************************************
462 * Variables - these are the only non-stack RAM usage
464 **************************************************************************/
466 // zero when we are not configured, non-zero when enumerated
467 static volatile uint8_t usb_configuration=0;
470 /**************************************************************************
472 * Public Functions - these are the API intended for the user
474 **************************************************************************/
481 USB_FREEZE(); // enable USB
482 PLL_CONFIG(); // config PLL
483 while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
484 USB_CONFIG(); // start USB clock
485 UDCON = 0; // enable attach resistor
486 usb_configuration = 0;
487 UDIEN = (1<<EORSTE)|(1<<SOFE);
491 // return 0 if the USB is not configured, or the configuration
492 // number selected by the HOST
493 uint8_t usb_configured(void)
495 return usb_configuration;
500 /**************************************************************************
502 * Private Functions - not intended for general user consumption....
504 **************************************************************************/
508 // USB Device Interrupt - handle all device-level events
509 // the transmit buffer flushing is triggered by the start of frame
513 uint8_t intbits, t, i;
514 static uint8_t div4=0;
518 if (intbits & (1<<EORSTI)) {
521 UECFG0X = EP_TYPE_CONTROL;
522 UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
523 UEIENX = (1<<RXSTPE);
524 usb_configuration = 0;
526 if ((intbits & (1<<SOFI)) && usb_configuration) {
527 t = debug_flush_timer;
529 debug_flush_timer = -- t;
531 UENUM = DEBUG_TX_ENDPOINT;
532 while ((UEINTX & (1<<RWAL))) {
538 if (usb_keyboard_idle_config && (++div4 & 3) == 0) {
539 UENUM = KEYBOARD_ENDPOINT;
540 if (UEINTX & (1<<RWAL)) {
541 usb_keyboard_idle_count++;
542 if (usb_keyboard_idle_count == usb_keyboard_idle_config) {
543 usb_keyboard_idle_count = 0;
544 UEDATX = usb_keyboard_mods;
546 for (i=0; i<6; i++) {
547 UEDATX = usb_keyboard_keys[i];
558 // Misc functions to wait for ready and send/receive packets
559 static inline void usb_wait_in_ready(void)
561 while (!(UEINTX & (1<<TXINI))) ;
563 static inline void usb_send_in(void)
565 UEINTX = ~(1<<TXINI);
567 static inline void usb_wait_receive_out(void)
569 while (!(UEINTX & (1<<RXOUTI))) ;
571 static inline void usb_ack_out(void)
573 UEINTX = ~(1<<RXOUTI);
578 // USB Endpoint Interrupt - endpoint 0 is handled here. The
579 // other endpoints are manipulated by the user-callable
580 // functions, and the start-of-frame interrupt.
587 uint8_t i, n, len, en;
588 uint8_t bmRequestType;
594 const uint8_t *desc_addr;
599 if (intbits & (1<<RXSTPI)) {
600 bmRequestType = UEDATX;
603 wValue |= (UEDATX << 8);
605 wIndex |= (UEDATX << 8);
607 wLength |= (UEDATX << 8);
608 UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
609 if (bRequest == GET_DESCRIPTOR) {
610 list = (const uint8_t *)descriptor_list;
612 if (i >= NUM_DESC_LIST) {
613 UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
616 desc_val = pgm_read_word(list);
617 if (desc_val != wValue) {
618 list += sizeof(struct descriptor_list_struct);
622 desc_val = pgm_read_word(list);
623 if (desc_val != wIndex) {
624 list += sizeof(struct descriptor_list_struct)-2;
628 desc_addr = (const uint8_t *)pgm_read_word(list);
630 desc_length = pgm_read_byte(list);
633 len = (wLength < 256) ? wLength : 255;
634 if (len > desc_length) len = desc_length;
636 // wait for host ready for IN packet
639 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
640 if (i & (1<<RXOUTI)) return; // abort
642 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
643 for (i = n; i; i--) {
644 UEDATX = pgm_read_byte(desc_addr++);
648 } while (len || n == ENDPOINT0_SIZE);
651 if (bRequest == SET_ADDRESS) {
654 UDADDR = wValue | (1<<ADDEN);
657 if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
658 usb_configuration = wValue;
660 cfg = endpoint_config_table;
661 for (i=1; i<=6; i++) {
663 en = pgm_read_byte(cfg++);
666 UECFG0X = pgm_read_byte(cfg++);
667 UECFG1X = pgm_read_byte(cfg++);
674 if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
676 UEDATX = usb_configuration;
681 if (bRequest == GET_STATUS) {
684 #ifdef SUPPORT_ENDPOINT_HALT
685 if (bmRequestType == 0x82) {
687 if (UECONX & (1<<STALLRQ)) i = 1;
696 #ifdef SUPPORT_ENDPOINT_HALT
697 if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
698 && bmRequestType == 0x02 && wValue == 0) {
700 if (i >= 1 && i <= MAX_ENDPOINT) {
703 if (bRequest == SET_FEATURE) {
704 UECONX = (1<<STALLRQ)|(1<<EPEN);
706 UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
714 if (wIndex == KEYBOARD_INTERFACE) {
715 if (bmRequestType == 0xA1) {
716 if (bRequest == HID_GET_REPORT) {
718 UEDATX = usb_keyboard_mods;
720 for (i=0; i<6; i++) {
721 UEDATX = usb_keyboard_keys[i];
726 if (bRequest == HID_GET_IDLE) {
728 UEDATX = usb_keyboard_idle_config;
732 if (bRequest == HID_GET_PROTOCOL) {
734 UEDATX = usb_keyboard_protocol;
739 if (bmRequestType == 0x21) {
740 if (bRequest == HID_SET_REPORT) {
741 usb_wait_receive_out();
742 usb_keyboard_leds = UEDATX;
747 if (bRequest == HID_SET_IDLE) {
748 usb_keyboard_idle_config = (wValue >> 8);
749 usb_keyboard_idle_count = 0;
750 //usb_wait_in_ready();
754 if (bRequest == HID_SET_PROTOCOL) {
755 usb_keyboard_protocol = wValue;
756 //usb_wait_in_ready();
762 if (wIndex == MOUSE_INTERFACE) {
763 if (bmRequestType == 0xA1) {
764 if (bRequest == HID_GET_REPORT) {
765 if (wValue == HID_REPORT_INPUT) {
767 UEDATX = mouse_buttons;
774 if (wValue == HID_REPORT_FEATURE) {
781 if (bRequest == HID_GET_PROTOCOL) {
783 UEDATX = mouse_protocol;
788 if (bmRequestType == 0x21) {
789 if (bRequest == HID_SET_PROTOCOL) {
790 mouse_protocol = wValue;
796 if (wIndex == DEBUG_INTERFACE) {
797 if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) {
800 // wait for host ready for IN packet
803 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
804 if (i & (1<<RXOUTI)) return; // abort
806 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
807 for (i = n; i; i--) {
812 } while (len || n == ENDPOINT0_SIZE);
817 UECONX = (1<<STALLRQ) | (1<<EPEN); // stall