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

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