Keyboard firmwares for Atmel AVR and Cortex-M
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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