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