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