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 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. // send the contents of USBKeys_Array and USBKeys_Modifiers
  285. int8_t usb_keyboard_send(void)
  286. {
  287. uint8_t i, intr_state, timeout;
  288. if (!usb_configuration) return -1;
  289. intr_state = SREG;
  290. cli();
  291. UENUM = KEYBOARD_ENDPOINT;
  292. timeout = UDFNUML + 50;
  293. while (1) {
  294. // are we ready to transmit?
  295. if (UEINTX & (1<<RWAL)) break;
  296. SREG = intr_state;
  297. // has the USB gone offline?
  298. if (!usb_configuration) return -1;
  299. // have we waited too long?
  300. if (UDFNUML == timeout) return -1;
  301. // get ready to try checking again
  302. intr_state = SREG;
  303. cli();
  304. UENUM = KEYBOARD_ENDPOINT;
  305. }
  306. UEDATX = USBKeys_Modifiers;
  307. UEDATX = 0;
  308. for (i=0; i<6; i++) {
  309. UEDATX = USBKeys_Array[i];
  310. }
  311. UEINTX = 0x3A;
  312. keyboard_idle_count = 0;
  313. SREG = intr_state;
  314. return 0;
  315. }
  316. // transmit a character. 0 returned on success, -1 on error
  317. int8_t usb_debug_putchar(uint8_t c)
  318. {
  319. static uint8_t previous_timeout=0;
  320. uint8_t timeout, intr_state;
  321. // if we're not online (enumerated and configured), error
  322. if (!usb_configuration) return -1;
  323. // interrupts are disabled so these functions can be
  324. // used from the main program or interrupt context,
  325. // even both in the same program!
  326. intr_state = SREG;
  327. cli();
  328. UENUM = DEBUG_TX_ENDPOINT;
  329. // if we gave up due to timeout before, don't wait again
  330. if (previous_timeout) {
  331. if (!(UEINTX & (1<<RWAL))) {
  332. SREG = intr_state;
  333. return -1;
  334. }
  335. previous_timeout = 0;
  336. }
  337. // wait for the FIFO to be ready to accept data
  338. timeout = UDFNUML + 4;
  339. while (1) {
  340. // are we ready to transmit?
  341. if (UEINTX & (1<<RWAL)) break;
  342. SREG = intr_state;
  343. // have we waited too long?
  344. if (UDFNUML == timeout) {
  345. previous_timeout = 1;
  346. return -1;
  347. }
  348. // has the USB gone offline?
  349. if (!usb_configuration) return -1;
  350. // get ready to try checking again
  351. intr_state = SREG;
  352. cli();
  353. UENUM = DEBUG_TX_ENDPOINT;
  354. }
  355. // actually write the byte into the FIFO
  356. UEDATX = c;
  357. // if this completed a packet, transmit it now!
  358. if (!(UEINTX & (1<<RWAL))) {
  359. UEINTX = 0x3A;
  360. debug_flush_timer = 0;
  361. } else {
  362. debug_flush_timer = 2;
  363. }
  364. SREG = intr_state;
  365. return 0;
  366. }
  367. // immediately transmit any buffered output.
  368. void usb_debug_flush_output(void)
  369. {
  370. uint8_t intr_state;
  371. intr_state = SREG;
  372. cli();
  373. if (debug_flush_timer) {
  374. UENUM = DEBUG_TX_ENDPOINT;
  375. while ((UEINTX & (1<<RWAL))) {
  376. UEDATX = 0;
  377. }
  378. UEINTX = 0x3A;
  379. debug_flush_timer = 0;
  380. }
  381. SREG = intr_state;
  382. }
  383. /**************************************************************************
  384. *
  385. * Private Functions - not intended for general user consumption....
  386. *
  387. **************************************************************************/
  388. // USB Device Interrupt - handle all device-level events
  389. // the transmit buffer flushing is triggered by the start of frame
  390. //
  391. ISR(USB_GEN_vect)
  392. {
  393. uint8_t intbits, t, i;
  394. static uint8_t div4=0;
  395. intbits = UDINT;
  396. UDINT = 0;
  397. if (intbits & (1<<EORSTI)) {
  398. UENUM = 0;
  399. UECONX = 1;
  400. UECFG0X = EP_TYPE_CONTROL;
  401. UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
  402. UEIENX = (1<<RXSTPE);
  403. usb_configuration = 0;
  404. }
  405. if ((intbits & (1<<SOFI)) && usb_configuration) {
  406. t = debug_flush_timer;
  407. if (t) {
  408. debug_flush_timer = -- t;
  409. if (!t) {
  410. UENUM = DEBUG_TX_ENDPOINT;
  411. while ((UEINTX & (1<<RWAL))) {
  412. UEDATX = 0;
  413. }
  414. UEINTX = 0x3A;
  415. }
  416. }
  417. if (keyboard_idle_config && (++div4 & 3) == 0) {
  418. UENUM = KEYBOARD_ENDPOINT;
  419. if (UEINTX & (1<<RWAL)) {
  420. keyboard_idle_count++;
  421. if (keyboard_idle_count == keyboard_idle_config) {
  422. keyboard_idle_count = 0;
  423. UEDATX = USBKeys_Modifiers;
  424. UEDATX = 0;
  425. for (i=0; i<6; i++) {
  426. UEDATX = USBKeys_Array[i];
  427. }
  428. UEINTX = 0x3A;
  429. }
  430. }
  431. }
  432. }
  433. }
  434. // Misc functions to wait for ready and send/receive packets
  435. static inline void usb_wait_in_ready(void)
  436. {
  437. while (!(UEINTX & (1<<TXINI))) ;
  438. }
  439. static inline void usb_send_in(void)
  440. {
  441. UEINTX = ~(1<<TXINI);
  442. }
  443. static inline void usb_wait_receive_out(void)
  444. {
  445. while (!(UEINTX & (1<<RXOUTI))) ;
  446. }
  447. static inline void usb_ack_out(void)
  448. {
  449. UEINTX = ~(1<<RXOUTI);
  450. }
  451. // USB Endpoint Interrupt - endpoint 0 is handled here. The
  452. // other endpoints are manipulated by the user-callable
  453. // functions, and the start-of-frame interrupt.
  454. //
  455. ISR(USB_COM_vect)
  456. {
  457. uint8_t intbits;
  458. const uint8_t *list;
  459. const uint8_t *cfg;
  460. uint8_t i, n, len, en;
  461. uint8_t bmRequestType;
  462. uint8_t bRequest;
  463. uint16_t wValue;
  464. uint16_t wIndex;
  465. uint16_t wLength;
  466. uint16_t desc_val;
  467. const uint8_t *desc_addr;
  468. uint8_t desc_length;
  469. UENUM = 0;
  470. intbits = UEINTX;
  471. if (intbits & (1<<RXSTPI)) {
  472. bmRequestType = UEDATX;
  473. bRequest = UEDATX;
  474. wValue = UEDATX;
  475. wValue |= (UEDATX << 8);
  476. wIndex = UEDATX;
  477. wIndex |= (UEDATX << 8);
  478. wLength = UEDATX;
  479. wLength |= (UEDATX << 8);
  480. UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
  481. if (bRequest == GET_DESCRIPTOR) {
  482. list = (const uint8_t *)descriptor_list;
  483. for (i=0; ; i++) {
  484. if (i >= NUM_DESC_LIST) {
  485. UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
  486. return;
  487. }
  488. desc_val = pgm_read_word(list);
  489. if (desc_val != wValue) {
  490. list += sizeof(struct descriptor_list_struct);
  491. continue;
  492. }
  493. list += 2;
  494. desc_val = pgm_read_word(list);
  495. if (desc_val != wIndex) {
  496. list += sizeof(struct descriptor_list_struct)-2;
  497. continue;
  498. }
  499. list += 2;
  500. desc_addr = (const uint8_t *)pgm_read_word(list);
  501. list += 2;
  502. desc_length = pgm_read_byte(list);
  503. break;
  504. }
  505. len = (wLength < 256) ? wLength : 255;
  506. if (len > desc_length) len = desc_length;
  507. do {
  508. // wait for host ready for IN packet
  509. do {
  510. i = UEINTX;
  511. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  512. if (i & (1<<RXOUTI)) return; // abort
  513. // send IN packet
  514. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  515. for (i = n; i; i--) {
  516. UEDATX = pgm_read_byte(desc_addr++);
  517. }
  518. len -= n;
  519. usb_send_in();
  520. } while (len || n == ENDPOINT0_SIZE);
  521. return;
  522. }
  523. if (bRequest == SET_ADDRESS) {
  524. usb_send_in();
  525. usb_wait_in_ready();
  526. UDADDR = wValue | (1<<ADDEN);
  527. return;
  528. }
  529. if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
  530. usb_configuration = wValue;
  531. usb_send_in();
  532. cfg = endpoint_config_table;
  533. for (i=1; i<5; i++) {
  534. UENUM = i;
  535. en = pgm_read_byte(cfg++);
  536. UECONX = en;
  537. if (en) {
  538. UECFG0X = pgm_read_byte(cfg++);
  539. UECFG1X = pgm_read_byte(cfg++);
  540. }
  541. }
  542. UERST = 0x1E;
  543. UERST = 0;
  544. return;
  545. }
  546. if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
  547. usb_wait_in_ready();
  548. UEDATX = usb_configuration;
  549. usb_send_in();
  550. return;
  551. }
  552. if (bRequest == GET_STATUS) {
  553. usb_wait_in_ready();
  554. i = 0;
  555. #ifdef SUPPORT_ENDPOINT_HALT
  556. if (bmRequestType == 0x82) {
  557. UENUM = wIndex;
  558. if (UECONX & (1<<STALLRQ)) i = 1;
  559. UENUM = 0;
  560. }
  561. #endif
  562. UEDATX = i;
  563. UEDATX = 0;
  564. usb_send_in();
  565. return;
  566. }
  567. #ifdef SUPPORT_ENDPOINT_HALT
  568. if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
  569. && bmRequestType == 0x02 && wValue == 0) {
  570. i = wIndex & 0x7F;
  571. if (i >= 1 && i <= MAX_ENDPOINT) {
  572. usb_send_in();
  573. UENUM = i;
  574. if (bRequest == SET_FEATURE) {
  575. UECONX = (1<<STALLRQ)|(1<<EPEN);
  576. } else {
  577. UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
  578. UERST = (1 << i);
  579. UERST = 0;
  580. }
  581. return;
  582. }
  583. }
  584. #endif
  585. if (wIndex == KEYBOARD_INTERFACE) {
  586. if (bmRequestType == 0xA1) {
  587. if (bRequest == HID_GET_REPORT) {
  588. usb_wait_in_ready();
  589. UEDATX = USBKeys_Modifiers;
  590. UEDATX = 0;
  591. for (i=0; i<6; i++) {
  592. UEDATX = USBKeys_Array[i];
  593. }
  594. usb_send_in();
  595. return;
  596. }
  597. if (bRequest == HID_GET_IDLE) {
  598. usb_wait_in_ready();
  599. UEDATX = keyboard_idle_config;
  600. usb_send_in();
  601. return;
  602. }
  603. if (bRequest == HID_GET_PROTOCOL) {
  604. usb_wait_in_ready();
  605. UEDATX = keyboard_protocol;
  606. usb_send_in();
  607. return;
  608. }
  609. }
  610. if (bmRequestType == 0x21) {
  611. if (bRequest == HID_SET_REPORT) {
  612. usb_wait_receive_out();
  613. USBKeys_LEDs = UEDATX;
  614. usb_ack_out();
  615. usb_send_in();
  616. return;
  617. }
  618. if (bRequest == HID_SET_IDLE) {
  619. keyboard_idle_config = (wValue >> 8);
  620. keyboard_idle_count = 0;
  621. //usb_wait_in_ready();
  622. usb_send_in();
  623. return;
  624. }
  625. if (bRequest == HID_SET_PROTOCOL) {
  626. keyboard_protocol = wValue;
  627. //usb_wait_in_ready();
  628. usb_send_in();
  629. return;
  630. }
  631. }
  632. }
  633. if (wIndex == DEBUG_INTERFACE) {
  634. if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) {
  635. len = wLength;
  636. do {
  637. // wait for host ready for IN packet
  638. do {
  639. i = UEINTX;
  640. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  641. if (i & (1<<RXOUTI)) return; // abort
  642. // send IN packet
  643. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  644. for (i = n; i; i--) {
  645. UEDATX = 0;
  646. }
  647. len -= n;
  648. usb_send_in();
  649. } while (len || n == ENDPOINT0_SIZE);
  650. return;
  651. }
  652. }
  653. }
  654. UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
  655. }