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

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