Keyboard firmwares for Atmel AVR and Cortex-M
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.

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