Keyboard firmwares for Atmel AVR and Cortex-M
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

usb.c 29KB

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