You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

970 lines
37 KiB

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