Kiibohd Controller
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

usb_keyboard_debug.c 21KB

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