Keyboard firmwares for Atmel AVR and Cortex-M
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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