1d790f043c7038960b7c319e8f763f8033bf4ee2
[tmk_keyboard.git] / usb_device.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/pgmspace.h>
25 #include <avr/interrupt.h>
26 #include "usb_device.h"
27 #include "usb_keyboard.h"
28 #include "usb_debug.h"
29
30
31 /**************************************************************************
32 *
33 * Configurable Options
34 *
35 **************************************************************************/
36
37 // You can change these to give your code its own name.
38 #define STR_MANUFACTURER L"t.m.k."
39 #define STR_PRODUCT L"t.m.k. keyboard"
40
41
42 // Mac OS-X and Linux automatically load the correct drivers. On
43 // Windows, even though the driver is supplied by Microsoft, an
44 // INF file is needed to load the driver. These numbers need to
45 // match the INF file.
46 #define VENDOR_ID 0xFEED
47 #define PRODUCT_ID 0xBABE
48
49
50 // USB devices are supposed to implment a halt feature, which is
51 // rarely (if ever) used. If you comment this line out, the halt
52 // code will be removed, saving 102 bytes of space (gcc 4.3.0).
53 // This is not strictly USB compliant, but works with all major
54 // operating systems.
55 #define SUPPORT_ENDPOINT_HALT
56
57
58
59 /**************************************************************************
60 *
61 * Endpoint Buffer Configuration
62 *
63 **************************************************************************/
64
65 #define ENDPOINT0_SIZE 32
66
67 static const uint8_t PROGMEM endpoint_config_table[] = {
68 0,
69 0,
70 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KEYBOARD_SIZE) | KEYBOARD_BUFFER,
71 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER
72 };
73
74
75 /**************************************************************************
76 *
77 * Descriptor Data
78 *
79 **************************************************************************/
80
81 // Descriptors are the data that your computer reads when it auto-detects
82 // this USB device (called "enumeration" in USB lingo). The most commonly
83 // changed items are editable at the top of this file. Changing things
84 // in here should only be done by those who've read chapter 9 of the USB
85 // spec and relevant portions of any USB class specifications!
86
87
88 static uint8_t PROGMEM device_descriptor[] = {
89 18, // bLength
90 1, // bDescriptorType
91 0x00, 0x02, // bcdUSB
92 0, // bDeviceClass
93 0, // bDeviceSubClass
94 0, // bDeviceProtocol
95 ENDPOINT0_SIZE, // bMaxPacketSize0
96 LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
97 LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
98 0x00, 0x01, // bcdDevice
99 1, // iManufacturer
100 2, // iProduct
101 0, // iSerialNumber
102 1 // bNumConfigurations
103 };
104
105 // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
106 static uint8_t PROGMEM keyboard_hid_report_desc[] = {
107 0x05, 0x01, // Usage Page (Generic Desktop),
108 0x09, 0x06, // Usage (Keyboard),
109 0xA1, 0x01, // Collection (Application),
110 0x75, 0x01, // Report Size (1),
111 0x95, 0x08, // Report Count (8),
112 0x05, 0x07, // Usage Page (Key Codes),
113 0x19, 0xE0, // Usage Minimum (224),
114 0x29, 0xE7, // Usage Maximum (231),
115 0x15, 0x00, // Logical Minimum (0),
116 0x25, 0x01, // Logical Maximum (1),
117 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
118 0x95, 0x01, // Report Count (1),
119 0x75, 0x08, // Report Size (8),
120 0x81, 0x03, // Input (Constant), ;Reserved byte
121 0x95, 0x05, // Report Count (5),
122 0x75, 0x01, // Report Size (1),
123 0x05, 0x08, // Usage Page (LEDs),
124 0x19, 0x01, // Usage Minimum (1),
125 0x29, 0x05, // Usage Maximum (5),
126 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
127 0x95, 0x01, // Report Count (1),
128 0x75, 0x03, // Report Size (3),
129 0x91, 0x03, // Output (Constant), ;LED report padding
130 0x95, 0x06, // Report Count (6),
131 0x75, 0x08, // Report Size (8),
132 0x15, 0x00, // Logical Minimum (0),
133 0x25, 0x68, // Logical Maximum(104),
134 0x05, 0x07, // Usage Page (Key Codes),
135 0x19, 0x00, // Usage Minimum (0),
136 0x29, 0x68, // Usage Maximum (104),
137 0x81, 0x00, // Input (Data, Array),
138 0xc0 // End Collection
139 };
140
141 static uint8_t PROGMEM debug_hid_report_desc[] = {
142 0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined)
143 0x09, 0x74, // Usage 0x74
144 0xA1, 0x53, // Collection 0x53
145 0x75, 0x08, // report size = 8 bits
146 0x15, 0x00, // logical minimum = 0
147 0x26, 0xFF, 0x00, // logical maximum = 255
148 0x95, DEBUG_TX_SIZE, // report count
149 0x09, 0x75, // usage
150 0x81, 0x02, // Input (array)
151 0xC0 // end collection
152 };
153
154 #define CONFIG1_DESC_SIZE (9+9+9+7+9+9+7)
155 #define KEYBOARD_HID_DESC_OFFSET (9+9)
156 #define DEBUG_HID_DESC_OFFSET (9+9+9+7+9)
157 static uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
158 // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
159 9, // bLength;
160 2, // bDescriptorType;
161 LSB(CONFIG1_DESC_SIZE), // wTotalLength
162 MSB(CONFIG1_DESC_SIZE),
163 2, // bNumInterfaces
164 1, // bConfigurationValue
165 0, // iConfiguration
166 0xC0, // bmAttributes
167 50, // bMaxPower
168 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
169 9, // bLength
170 4, // bDescriptorType
171 KEYBOARD_INTERFACE, // bInterfaceNumber
172 0, // bAlternateSetting
173 1, // bNumEndpoints
174 0x03, // bInterfaceClass (0x03 = HID)
175 0x01, // bInterfaceSubClass (0x01 = Boot)
176 0x01, // bInterfaceProtocol (0x01 = Keyboard)
177 0, // iInterface
178 // HID interface descriptor, HID 1.11 spec, section 6.2.1
179 9, // bLength
180 0x21, // bDescriptorType
181 0x11, 0x01, // bcdHID
182 0, // bCountryCode
183 1, // bNumDescriptors
184 0x22, // bDescriptorType
185 sizeof(keyboard_hid_report_desc), // wDescriptorLength
186 0,
187 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
188 7, // bLength
189 5, // bDescriptorType
190 KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
191 0x03, // bmAttributes (0x03=intr)
192 KEYBOARD_SIZE, 0, // wMaxPacketSize
193 1, // bInterval
194 // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
195 9, // bLength
196 4, // bDescriptorType
197 DEBUG_INTERFACE, // bInterfaceNumber
198 0, // bAlternateSetting
199 1, // bNumEndpoints
200 0x03, // bInterfaceClass (0x03 = HID)
201 0x00, // bInterfaceSubClass
202 0x00, // bInterfaceProtocol
203 0, // iInterface
204 // HID interface descriptor, HID 1.11 spec, section 6.2.1
205 9, // bLength
206 0x21, // bDescriptorType
207 0x11, 0x01, // bcdHID
208 0, // bCountryCode
209 1, // bNumDescriptors
210 0x22, // bDescriptorType
211 sizeof(debug_hid_report_desc), // wDescriptorLength
212 0,
213 // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
214 7, // bLength
215 5, // bDescriptorType
216 DEBUG_TX_ENDPOINT | 0x80, // bEndpointAddress
217 0x03, // bmAttributes (0x03=intr)
218 DEBUG_TX_SIZE, 0, // wMaxPacketSize
219 1 // bInterval
220 };
221
222 // If you're desperate for a little extra code memory, these strings
223 // can be completely removed if iManufacturer, iProduct, iSerialNumber
224 // in the device desciptor are changed to zeros.
225 struct usb_string_descriptor_struct {
226 uint8_t bLength;
227 uint8_t bDescriptorType;
228 int16_t wString[];
229 };
230 static struct usb_string_descriptor_struct PROGMEM string0 = {
231 4,
232 3,
233 {0x0409}
234 };
235 static struct usb_string_descriptor_struct PROGMEM string1 = {
236 sizeof(STR_MANUFACTURER),
237 3,
238 STR_MANUFACTURER
239 };
240 static struct usb_string_descriptor_struct PROGMEM string2 = {
241 sizeof(STR_PRODUCT),
242 3,
243 STR_PRODUCT
244 };
245
246 // This table defines which descriptor data is sent for each specific
247 // request from the host (in wValue and wIndex).
248 static struct descriptor_list_struct {
249 uint16_t wValue; // descriptor type
250 uint16_t wIndex;
251 const uint8_t *addr;
252 uint8_t length;
253 } PROGMEM descriptor_list[] = {
254 // DEVICE descriptor
255 {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
256 // CONFIGURATION descriptor
257 {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
258 // HID REPORT
259 {0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
260 {0x2100, KEYBOARD_INTERFACE, config1_descriptor+KEYBOARD_HID_DESC_OFFSET, 9},
261 // HID REPORT
262 {0x2200, DEBUG_INTERFACE, debug_hid_report_desc, sizeof(debug_hid_report_desc)},
263 {0x2100, DEBUG_INTERFACE, config1_descriptor+DEBUG_HID_DESC_OFFSET, 9},
264 // STRING descriptor
265 {0x0300, 0x0000, (const uint8_t *)&string0, 4},
266 {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
267 {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)}
268 };
269 #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
270
271
272 /**************************************************************************
273 *
274 * Variables - these are the only non-stack RAM usage
275 *
276 **************************************************************************/
277
278 // zero when we are not configured, non-zero when enumerated
279 static volatile uint8_t usb_configuration=0;
280
281
282 /**************************************************************************
283 *
284 * Public Functions - these are the API intended for the user
285 *
286 **************************************************************************/
287
288
289 // initialize USB
290 void usb_init(void)
291 {
292 HW_CONFIG();
293 USB_FREEZE(); // enable USB
294 PLL_CONFIG(); // config PLL
295 while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
296 USB_CONFIG(); // start USB clock
297 UDCON = 0; // enable attach resistor
298 usb_configuration = 0;
299 UDIEN = (1<<EORSTE)|(1<<SOFE);
300 sei();
301 }
302
303 // return 0 if the USB is not configured, or the configuration
304 // number selected by the HOST
305 uint8_t usb_configured(void)
306 {
307 return usb_configuration;
308 }
309
310
311
312 /**************************************************************************
313 *
314 * Private Functions - not intended for general user consumption....
315 *
316 **************************************************************************/
317
318
319
320 // USB Device Interrupt - handle all device-level events
321 // the transmit buffer flushing is triggered by the start of frame
322 //
323 ISR(USB_GEN_vect)
324 {
325 uint8_t intbits, t, i;
326 static uint8_t div4=0;
327
328 intbits = UDINT;
329 UDINT = 0;
330 if (intbits & (1<<EORSTI)) {
331 UENUM = 0;
332 UECONX = 1;
333 UECFG0X = EP_TYPE_CONTROL;
334 UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
335 UEIENX = (1<<RXSTPE);
336 usb_configuration = 0;
337 }
338 if ((intbits & (1<<SOFI)) && usb_configuration) {
339 t = debug_flush_timer;
340 if (t) {
341 debug_flush_timer = -- t;
342 if (!t) {
343 UENUM = DEBUG_TX_ENDPOINT;
344 while ((UEINTX & (1<<RWAL))) {
345 UEDATX = 0;
346 }
347 UEINTX = 0x3A;
348 }
349 }
350 if (keyboard_idle_config && (++div4 & 3) == 0) {
351 UENUM = KEYBOARD_ENDPOINT;
352 if (UEINTX & (1<<RWAL)) {
353 keyboard_idle_count++;
354 if (keyboard_idle_count == keyboard_idle_config) {
355 keyboard_idle_count = 0;
356 UEDATX = keyboard_modifier_keys;
357 UEDATX = 0;
358 for (i=0; i<6; i++) {
359 UEDATX = keyboard_keys[i];
360 }
361 UEINTX = 0x3A;
362 }
363 }
364 }
365 }
366 }
367
368
369
370 // Misc functions to wait for ready and send/receive packets
371 static inline void usb_wait_in_ready(void)
372 {
373 while (!(UEINTX & (1<<TXINI))) ;
374 }
375 static inline void usb_send_in(void)
376 {
377 UEINTX = ~(1<<TXINI);
378 }
379 static inline void usb_wait_receive_out(void)
380 {
381 while (!(UEINTX & (1<<RXOUTI))) ;
382 }
383 static inline void usb_ack_out(void)
384 {
385 UEINTX = ~(1<<RXOUTI);
386 }
387
388
389
390 // USB Endpoint Interrupt - endpoint 0 is handled here. The
391 // other endpoints are manipulated by the user-callable
392 // functions, and the start-of-frame interrupt.
393 //
394 ISR(USB_COM_vect)
395 {
396 uint8_t intbits;
397 const uint8_t *list;
398 const uint8_t *cfg;
399 uint8_t i, n, len, en;
400 uint8_t bmRequestType;
401 uint8_t bRequest;
402 uint16_t wValue;
403 uint16_t wIndex;
404 uint16_t wLength;
405 uint16_t desc_val;
406 const uint8_t *desc_addr;
407 uint8_t desc_length;
408
409 UENUM = 0;
410 intbits = UEINTX;
411 if (intbits & (1<<RXSTPI)) {
412 bmRequestType = UEDATX;
413 bRequest = UEDATX;
414 wValue = UEDATX;
415 wValue |= (UEDATX << 8);
416 wIndex = UEDATX;
417 wIndex |= (UEDATX << 8);
418 wLength = UEDATX;
419 wLength |= (UEDATX << 8);
420 UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
421 if (bRequest == GET_DESCRIPTOR) {
422 list = (const uint8_t *)descriptor_list;
423 for (i=0; ; i++) {
424 if (i >= NUM_DESC_LIST) {
425 UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
426 return;
427 }
428 desc_val = pgm_read_word(list);
429 if (desc_val != wValue) {
430 list += sizeof(struct descriptor_list_struct);
431 continue;
432 }
433 list += 2;
434 desc_val = pgm_read_word(list);
435 if (desc_val != wIndex) {
436 list += sizeof(struct descriptor_list_struct)-2;
437 continue;
438 }
439 list += 2;
440 desc_addr = (const uint8_t *)pgm_read_word(list);
441 list += 2;
442 desc_length = pgm_read_byte(list);
443 break;
444 }
445 len = (wLength < 256) ? wLength : 255;
446 if (len > desc_length) len = desc_length;
447 do {
448 // wait for host ready for IN packet
449 do {
450 i = UEINTX;
451 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
452 if (i & (1<<RXOUTI)) return; // abort
453 // send IN packet
454 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
455 for (i = n; i; i--) {
456 UEDATX = pgm_read_byte(desc_addr++);
457 }
458 len -= n;
459 usb_send_in();
460 } while (len || n == ENDPOINT0_SIZE);
461 return;
462 }
463 if (bRequest == SET_ADDRESS) {
464 usb_send_in();
465 usb_wait_in_ready();
466 UDADDR = wValue | (1<<ADDEN);
467 return;
468 }
469 if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
470 usb_configuration = wValue;
471 usb_send_in();
472 cfg = endpoint_config_table;
473 for (i=1; i<5; i++) {
474 UENUM = i;
475 en = pgm_read_byte(cfg++);
476 UECONX = en;
477 if (en) {
478 UECFG0X = pgm_read_byte(cfg++);
479 UECFG1X = pgm_read_byte(cfg++);
480 }
481 }
482 UERST = 0x1E;
483 UERST = 0;
484 return;
485 }
486 if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
487 usb_wait_in_ready();
488 UEDATX = usb_configuration;
489 usb_send_in();
490 return;
491 }
492
493 if (bRequest == GET_STATUS) {
494 usb_wait_in_ready();
495 i = 0;
496 #ifdef SUPPORT_ENDPOINT_HALT
497 if (bmRequestType == 0x82) {
498 UENUM = wIndex;
499 if (UECONX & (1<<STALLRQ)) i = 1;
500 UENUM = 0;
501 }
502 #endif
503 UEDATX = i;
504 UEDATX = 0;
505 usb_send_in();
506 return;
507 }
508 #ifdef SUPPORT_ENDPOINT_HALT
509 if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
510 && bmRequestType == 0x02 && wValue == 0) {
511 i = wIndex & 0x7F;
512 if (i >= 1 && i <= MAX_ENDPOINT) {
513 usb_send_in();
514 UENUM = i;
515 if (bRequest == SET_FEATURE) {
516 UECONX = (1<<STALLRQ)|(1<<EPEN);
517 } else {
518 UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
519 UERST = (1 << i);
520 UERST = 0;
521 }
522 return;
523 }
524 }
525 #endif
526 if (wIndex == KEYBOARD_INTERFACE) {
527 if (bmRequestType == 0xA1) {
528 if (bRequest == HID_GET_REPORT) {
529 usb_wait_in_ready();
530 UEDATX = keyboard_modifier_keys;
531 UEDATX = 0;
532 for (i=0; i<6; i++) {
533 UEDATX = keyboard_keys[i];
534 }
535 usb_send_in();
536 return;
537 }
538 if (bRequest == HID_GET_IDLE) {
539 usb_wait_in_ready();
540 UEDATX = keyboard_idle_config;
541 usb_send_in();
542 return;
543 }
544 if (bRequest == HID_GET_PROTOCOL) {
545 usb_wait_in_ready();
546 UEDATX = keyboard_protocol;
547 usb_send_in();
548 return;
549 }
550 }
551 if (bmRequestType == 0x21) {
552 if (bRequest == HID_SET_REPORT) {
553 usb_wait_receive_out();
554 keyboard_leds = UEDATX;
555 usb_ack_out();
556 usb_send_in();
557 return;
558 }
559 if (bRequest == HID_SET_IDLE) {
560 keyboard_idle_config = (wValue >> 8);
561 keyboard_idle_count = 0;
562 //usb_wait_in_ready();
563 usb_send_in();
564 return;
565 }
566 if (bRequest == HID_SET_PROTOCOL) {
567 keyboard_protocol = wValue;
568 //usb_wait_in_ready();
569 usb_send_in();
570 return;
571 }
572 }
573 }
574 if (wIndex == DEBUG_INTERFACE) {
575 if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) {
576 len = wLength;
577 do {
578 // wait for host ready for IN packet
579 do {
580 i = UEINTX;
581 } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
582 if (i & (1<<RXOUTI)) return; // abort
583 // send IN packet
584 n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
585 for (i = n; i; i--) {
586 UEDATX = 0;
587 }
588 len -= n;
589 usb_send_in();
590 } while (len || n == ENDPOINT0_SIZE);
591 return;
592 }
593 }
594 }
595 UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
596 }
597
598
Imprint / Impressum