]> git.gir.st - tmk_keyboard.git/blob - usb.c
add audio controls from consumer page(HID)
[tmk_keyboard.git] / 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 <avr/io.h>
25 #include <avr/pgmspace.h>
26 #include <avr/interrupt.h>
27 #include "usb.h"
28 #include "usb_keyboard.h"
29 #include "usb_mouse.h"
30 #include "usb_debug.h"
31 #include "usb_extra.h"
32 #include "print.h"
33 #include "util.h"
34
35
36 /**************************************************************************
37 *
38 * Configurable Options
39 *
40 **************************************************************************/
41
42 // You can change these to give your code its own name.
43 #ifndef MANUFACTURER
44 # define STR_MANUFACTURER L"t.m.k."
45 #else
46 # define STR_MANUFACTURER LSTR(MANUFACTURER)
47 #endif
48 #ifndef PRODUCT
49 # define STR_PRODUCT L"t.m.k. keyboard"
50 #else
51 # define STR_PRODUCT LSTR(PRODUCT)
52 #endif
53
54
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.
59 #ifndef VENDOR_ID
60 # define VENDOR_ID 0xFEED
61 #endif
62
63 #ifndef PRODUCT_ID
64 # define PRODUCT_ID 0xBABE
65 #endif
66
67
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
72 // operating systems.
73 #define SUPPORT_ENDPOINT_HALT
74
75
76
77 /**************************************************************************
78 *
79 * Endpoint Buffer Configuration
80 *
81 **************************************************************************/
82
83 #define ENDPOINT0_SIZE 32
84
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
92 0, // 5
93 0, // 6
94 };
95
96
97 /**************************************************************************
98 *
99 * Descriptor Data
100 *
101 **************************************************************************/
102
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!
108
109
110 static uint8_t PROGMEM device_descriptor[] = {
111 18, // bLength
112 1, // bDescriptorType
113 0x00, 0x02, // bcdUSB
114 0, // bDeviceClass
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
121 1, // iManufacturer
122 2, // iProduct
123 0, // iSerialNumber
124 1 // bNumConfigurations
125 };
126
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
161 };
162
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)
206 0xa4, // PUSH
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)
220 0xb4, // POP
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
238 };
239
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
248 0x09, 0x75, // usage
249 0x81, 0x02, // Input (array)
250 0xC0 // end collection
251 };
252
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
276 };
277
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
285 9, // bLength;
286 2, // bDescriptorType;
287 LSB(CONFIG1_DESC_SIZE), // wTotalLength
288 MSB(CONFIG1_DESC_SIZE),
289 4, // bNumInterfaces
290 1, // bConfigurationValue
291 0, // iConfiguration
292 0xC0, // bmAttributes
293 50, // bMaxPower
294
295 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
296 9, // bLength
297 4, // bDescriptorType
298 KEYBOARD_INTERFACE, // bInterfaceNumber
299 0, // bAlternateSetting
300 1, // bNumEndpoints
301 0x03, // bInterfaceClass (0x03 = HID)
302 0x01, // bInterfaceSubClass (0x01 = Boot)
303 0x01, // bInterfaceProtocol (0x01 = Keyboard)
304 0, // iInterface
305 // HID descriptor, HID 1.11 spec, section 6.2.1
306 9, // bLength
307 0x21, // bDescriptorType
308 0x11, 0x01, // bcdHID
309 0, // bCountryCode
310 1, // bNumDescriptors
311 0x22, // bDescriptorType
312 sizeof(keyboard_hid_report_desc), // wDescriptorLength
313 0,
314 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
315 7, // bLength
316 5, // bDescriptorType
317 KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
318 0x03, // bmAttributes (0x03=intr)
319 KEYBOARD_SIZE, 0, // wMaxPacketSize
320 1, // bInterval
321
322 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
323 9, // bLength
324 4, // bDescriptorType
325 MOUSE_INTERFACE, // bInterfaceNumber
326 0, // bAlternateSetting
327 1, // bNumEndpoints
328 0x03, // bInterfaceClass (0x03 = HID)
329 0x01, // bInterfaceSubClass (0x01 = Boot)
330 0x02, // bInterfaceProtocol (0x02 = Mouse)
331 0, // iInterface
332 // HID descriptor, HID 1.11 spec, section 6.2.1
333 9, // bLength
334 0x21, // bDescriptorType
335 0x11, 0x01, // bcdHID
336 0, // bCountryCode
337 1, // bNumDescriptors
338 0x22, // bDescriptorType
339 sizeof(mouse_hid_report_desc), // wDescriptorLength
340 0,
341 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
342 7, // bLength
343 5, // bDescriptorType
344 MOUSE_ENDPOINT | 0x80, // bEndpointAddress
345 0x03, // bmAttributes (0x03=intr)
346 MOUSE_SIZE, 0, // wMaxPacketSize
347 1, // bInterval
348
349 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
350 9, // bLength
351 4, // bDescriptorType
352 DEBUG_INTERFACE, // bInterfaceNumber
353 0, // bAlternateSetting
354 1, // bNumEndpoints
355 0x03, // bInterfaceClass (0x03 = HID)
356 0x00, // bInterfaceSubClass
357 0x00, // bInterfaceProtocol
358 0, // iInterface
359 // HID descriptor, HID 1.11 spec, section 6.2.1
360 9, // bLength
361 0x21, // bDescriptorType
362 0x11, 0x01, // bcdHID
363 0, // bCountryCode
364 1, // bNumDescriptors
365 0x22, // bDescriptorType
366 sizeof(debug_hid_report_desc), // wDescriptorLength
367 0,
368 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
369 7, // bLength
370 5, // bDescriptorType
371 DEBUG_TX_ENDPOINT | 0x80, // bEndpointAddress
372 0x03, // bmAttributes (0x03=intr)
373 DEBUG_TX_SIZE, 0, // wMaxPacketSize
374 1, // bInterval
375
376 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
377 9, // bLength
378 4, // bDescriptorType
379 EXTRA_INTERFACE, // bInterfaceNumber
380 0, // bAlternateSetting
381 1, // bNumEndpoints
382 0x03, // bInterfaceClass (0x03 = HID)
383 0x00, // bInterfaceSubClass
384 0x00, // bInterfaceProtocol
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(extra_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 EXTRA_ENDPOINT | 0x80, // bEndpointAddress
399 0x03, // bmAttributes (0x03=intr)
400 EXTRA_SIZE, 0, // wMaxPacketSize
401 10, // bInterval
402 };
403
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 {
408 uint8_t bLength;
409 uint8_t bDescriptorType;
410 int16_t wString[];
411 };
412 static struct usb_string_descriptor_struct PROGMEM string0 = {
413 4,
414 3,
415 {0x0409}
416 };
417 static struct usb_string_descriptor_struct PROGMEM string1 = {
418 sizeof(STR_MANUFACTURER),
419 3,
420 STR_MANUFACTURER
421 };
422 static struct usb_string_descriptor_struct PROGMEM string2 = {
423 sizeof(STR_PRODUCT),
424 3,
425 STR_PRODUCT
426 };
427
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
432 uint16_t wIndex;
433 const uint8_t *addr;
434 uint8_t length;
435 } PROGMEM descriptor_list[] = {
436 // DEVICE descriptor
437 {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
438 // CONFIGURATION descriptor
439 {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
440 // HID REPORT
441 {0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
442 {0x2100, KEYBOARD_INTERFACE, config1_descriptor+KEYBOARD_HID_DESC_OFFSET, 9},
443 // HID REPORT
444 {0x2200, MOUSE_INTERFACE, mouse_hid_report_desc, sizeof(mouse_hid_report_desc)},
445 {0x2100, MOUSE_INTERFACE, config1_descriptor+MOUSE_HID_DESC_OFFSET, 9},
446 // HID REPORT
447 {0x2200, DEBUG_INTERFACE, debug_hid_report_desc, sizeof(debug_hid_report_desc)},
448 {0x2100, DEBUG_INTERFACE, config1_descriptor+DEBUG_HID_DESC_OFFSET, 9},
449 // HID REPORT
450 {0x2200, EXTRA_INTERFACE, extra_hid_report_desc, sizeof(extra_hid_report_desc)},
451 {0x2100, EXTRA_INTERFACE, config1_descriptor+EXTRA_HID_DESC_OFFSET, 9},
452 // STRING descriptor
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)}
456 };
457 #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
458
459
460 /**************************************************************************
461 *
462 * Variables - these are the only non-stack RAM usage
463 *
464 **************************************************************************/
465
466 // zero when we are not configured, non-zero when enumerated
467 static volatile uint8_t usb_configuration=0;
468
469
470 /**************************************************************************
471 *
472 * Public Functions - these are the API intended for the user
473 *
474 **************************************************************************/
475
476
477 // initialize USB
478 void usb_init(void)
479 {
480 HW_CONFIG();
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);
488 sei();
489 }
490
491 // return 0 if the USB is not configured, or the configuration
492 // number selected by the HOST
493 uint8_t usb_configured(void)
494 {
495 return usb_configuration;
496 }
497
498
499
500 /**************************************************************************
501 *
502 * Private Functions - not intended for general user consumption....
503 *
504 **************************************************************************/
505
506
507
508 // USB Device Interrupt - handle all device-level events
509 // the transmit buffer flushing is triggered by the start of frame
510 //
511 ISR(USB_GEN_vect)
512 {
513 uint8_t intbits, t, i;
514 static uint8_t div4=0;
515
516 intbits = UDINT;
517 UDINT = 0;
518 if (intbits & (1<<EORSTI)) {
519 UENUM = 0;
520 UECONX = 1;
521 UECFG0X = EP_TYPE_CONTROL;
522 UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
523 UEIENX = (1<<RXSTPE);
524 usb_configuration = 0;
525 }
526 if ((intbits & (1<<SOFI)) && usb_configuration) {
527 t = debug_flush_timer;
528 if (t) {
529 debug_flush_timer = -- t;
530 if (!t) {
531 UENUM = DEBUG_TX_ENDPOINT;
532 while ((UEINTX & (1<<RWAL))) {
533 UEDATX = 0;
534 }
535 UEINTX = 0x3A;
536 }
537 }
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;
545 UEDATX = 0;
546 for (i=0; i<6; i++) {
547 UEDATX = usb_keyboard_keys[i];
548 }
549 UEINTX = 0x3A;
550 }
551 }
552 }
553 }
554 }
555
556
557
558 // Misc functions to wait for ready and send/receive packets
559 static inline void usb_wait_in_ready(void)
560 {
561 while (!(UEINTX & (1<<TXINI))) ;
562 }
563 static inline void usb_send_in(void)
564 {
565 UEINTX = ~(1<<TXINI);
566 }
567 static inline void usb_wait_receive_out(void)
568 {
569 while (!(UEINTX & (1<<RXOUTI))) ;
570 }
571 static inline void usb_ack_out(void)
572 {
573 UEINTX = ~(1<<RXOUTI);
574 }
575
576
577
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.
581 //
582 ISR(USB_COM_vect)
583 {
584 uint8_t intbits;
585 const uint8_t *list;
586 const uint8_t *cfg;
587 uint8_t i, n, len, en;
588 uint8_t bmRequestType;
589 uint8_t bRequest;
590 uint16_t wValue;
591 uint16_t wIndex;
592 uint16_t wLength;
593 uint16_t desc_val;
594 const uint8_t *desc_addr;
595 uint8_t desc_length;
596
597 UENUM = 0;
598 intbits = UEINTX;
599 if (intbits & (1<<RXSTPI)) {
600 bmRequestType = UEDATX;
601 bRequest = UEDATX;
602 wValue = UEDATX;
603 wValue |= (UEDATX << 8);
604 wIndex = UEDATX;
605 wIndex |= (UEDATX << 8);
606 wLength = UEDATX;
607 wLength |= (UEDATX << 8);
608 UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
609 if (bRequest == GET_DESCRIPTOR) {
610 list = (const uint8_t *)descriptor_list;
611 for (i=0; ; i++) {
612 if (i >= NUM_DESC_LIST) {
613 UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
614 return;
615 }
616 desc_val = pgm_read_word(list);
617 if (desc_val != wValue) {
618 list += sizeof(struct descriptor_list_struct);
619 continue;
620 }
621 list += 2;
622 desc_val = pgm_read_word(list);
623 if (desc_val != wIndex) {
624 list += sizeof(struct descriptor_list_struct)-2;
625 continue;
626 }
627 list += 2;
628 desc_addr = (const uint8_t *)pgm_read_word(list);
629 list += 2;
630 desc_length = pgm_read_byte(list);
631 break;
632 }
633 len = (wLength < 256) ? wLength : 255;
634 if (len > desc_length) len = desc_length;
635 do {
636 // wait for host ready for IN packet
637 do {
638 i = UEINTX;
639 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
640 if (i & (1<<RXOUTI)) return; // abort
641 // send IN packet
642 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
643 for (i = n; i; i--) {
644 UEDATX = pgm_read_byte(desc_addr++);
645 }
646 len -= n;
647 usb_send_in();
648 } while (len || n == ENDPOINT0_SIZE);
649 return;
650 }
651 if (bRequest == SET_ADDRESS) {
652 usb_send_in();
653 usb_wait_in_ready();
654 UDADDR = wValue | (1<<ADDEN);
655 return;
656 }
657 if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
658 usb_configuration = wValue;
659 usb_send_in();
660 cfg = endpoint_config_table;
661 for (i=1; i<=6; i++) {
662 UENUM = i;
663 en = pgm_read_byte(cfg++);
664 UECONX = en;
665 if (en) {
666 UECFG0X = pgm_read_byte(cfg++);
667 UECFG1X = pgm_read_byte(cfg++);
668 }
669 }
670 UERST = 0x7E;
671 UERST = 0;
672 return;
673 }
674 if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
675 usb_wait_in_ready();
676 UEDATX = usb_configuration;
677 usb_send_in();
678 return;
679 }
680
681 if (bRequest == GET_STATUS) {
682 usb_wait_in_ready();
683 i = 0;
684 #ifdef SUPPORT_ENDPOINT_HALT
685 if (bmRequestType == 0x82) {
686 UENUM = wIndex;
687 if (UECONX & (1<<STALLRQ)) i = 1;
688 UENUM = 0;
689 }
690 #endif
691 UEDATX = i;
692 UEDATX = 0;
693 usb_send_in();
694 return;
695 }
696 #ifdef SUPPORT_ENDPOINT_HALT
697 if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
698 && bmRequestType == 0x02 && wValue == 0) {
699 i = wIndex & 0x7F;
700 if (i >= 1 && i <= MAX_ENDPOINT) {
701 usb_send_in();
702 UENUM = i;
703 if (bRequest == SET_FEATURE) {
704 UECONX = (1<<STALLRQ)|(1<<EPEN);
705 } else {
706 UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
707 UERST = (1 << i);
708 UERST = 0;
709 }
710 return;
711 }
712 }
713 #endif
714 if (wIndex == KEYBOARD_INTERFACE) {
715 if (bmRequestType == 0xA1) {
716 if (bRequest == HID_GET_REPORT) {
717 usb_wait_in_ready();
718 UEDATX = usb_keyboard_mods;
719 UEDATX = 0;
720 for (i=0; i<6; i++) {
721 UEDATX = usb_keyboard_keys[i];
722 }
723 usb_send_in();
724 return;
725 }
726 if (bRequest == HID_GET_IDLE) {
727 usb_wait_in_ready();
728 UEDATX = usb_keyboard_idle_config;
729 usb_send_in();
730 return;
731 }
732 if (bRequest == HID_GET_PROTOCOL) {
733 usb_wait_in_ready();
734 UEDATX = usb_keyboard_protocol;
735 usb_send_in();
736 return;
737 }
738 }
739 if (bmRequestType == 0x21) {
740 if (bRequest == HID_SET_REPORT) {
741 usb_wait_receive_out();
742 usb_keyboard_leds = UEDATX;
743 usb_ack_out();
744 usb_send_in();
745 return;
746 }
747 if (bRequest == HID_SET_IDLE) {
748 usb_keyboard_idle_config = (wValue >> 8);
749 usb_keyboard_idle_count = 0;
750 //usb_wait_in_ready();
751 usb_send_in();
752 return;
753 }
754 if (bRequest == HID_SET_PROTOCOL) {
755 usb_keyboard_protocol = wValue;
756 //usb_wait_in_ready();
757 usb_send_in();
758 return;
759 }
760 }
761 }
762 if (wIndex == MOUSE_INTERFACE) {
763 if (bmRequestType == 0xA1) {
764 if (bRequest == HID_GET_REPORT) {
765 if (wValue == HID_REPORT_INPUT) {
766 usb_wait_in_ready();
767 UEDATX = mouse_buttons;
768 UEDATX = 0;
769 UEDATX = 0;
770 UEDATX = 0;
771 usb_send_in();
772 return;
773 }
774 if (wValue == HID_REPORT_FEATURE) {
775 usb_wait_in_ready();
776 UEDATX = 0x05;
777 usb_send_in();
778 return;
779 }
780 }
781 if (bRequest == HID_GET_PROTOCOL) {
782 usb_wait_in_ready();
783 UEDATX = mouse_protocol;
784 usb_send_in();
785 return;
786 }
787 }
788 if (bmRequestType == 0x21) {
789 if (bRequest == HID_SET_PROTOCOL) {
790 mouse_protocol = wValue;
791 usb_send_in();
792 return;
793 }
794 }
795 }
796 if (wIndex == DEBUG_INTERFACE) {
797 if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) {
798 len = wLength;
799 do {
800 // wait for host ready for IN packet
801 do {
802 i = UEINTX;
803 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
804 if (i & (1<<RXOUTI)) return; // abort
805 // send IN packet
806 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
807 for (i = n; i; i--) {
808 UEDATX = 0;
809 }
810 len -= n;
811 usb_send_in();
812 } while (len || n == ENDPOINT0_SIZE);
813 return;
814 }
815 }
816 }
817 UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
818 }
Imprint / Impressum