Keyboard firmwares for Atmel AVR and Cortex-M
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.

usb_device.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /* USB Keyboard Plus Debug Channel Example for Teensy USB Development Board
  2. * http://www.pjrc.com/teensy/usb_keyboard.html
  3. * Copyright (c) 2009 PJRC.COM, LLC
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include <avr/pgmspace.h>
  24. #include <avr/interrupt.h>
  25. #include "usb_device.h"
  26. #include "usb_keyboard.h"
  27. #include "usb_debug.h"
  28. /**************************************************************************
  29. *
  30. * Configurable Options
  31. *
  32. **************************************************************************/
  33. // You can change these to give your code its own name.
  34. #define STR_MANUFACTURER L"MfgName"
  35. #define STR_PRODUCT L"Keyboard"
  36. // Mac OS-X and Linux automatically load the correct drivers. On
  37. // Windows, even though the driver is supplied by Microsoft, an
  38. // INF file is needed to load the driver. These numbers need to
  39. // match the INF file.
  40. #define VENDOR_ID 0x16C0
  41. #define PRODUCT_ID 0x047D
  42. // USB devices are supposed to implment a halt feature, which is
  43. // rarely (if ever) used. If you comment this line out, the halt
  44. // code will be removed, saving 102 bytes of space (gcc 4.3.0).
  45. // This is not strictly USB compliant, but works with all major
  46. // operating systems.
  47. #define SUPPORT_ENDPOINT_HALT
  48. /**************************************************************************
  49. *
  50. * Endpoint Buffer Configuration
  51. *
  52. **************************************************************************/
  53. #define ENDPOINT0_SIZE 32
  54. static const uint8_t PROGMEM endpoint_config_table[] = {
  55. 0,
  56. 0,
  57. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KEYBOARD_SIZE) | KEYBOARD_BUFFER,
  58. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER
  59. };
  60. /**************************************************************************
  61. *
  62. * Descriptor Data
  63. *
  64. **************************************************************************/
  65. // Descriptors are the data that your computer reads when it auto-detects
  66. // this USB device (called "enumeration" in USB lingo). The most commonly
  67. // changed items are editable at the top of this file. Changing things
  68. // in here should only be done by those who've read chapter 9 of the USB
  69. // spec and relevant portions of any USB class specifications!
  70. static uint8_t PROGMEM device_descriptor[] = {
  71. 18, // bLength
  72. 1, // bDescriptorType
  73. 0x00, 0x02, // bcdUSB
  74. 0, // bDeviceClass
  75. 0, // bDeviceSubClass
  76. 0, // bDeviceProtocol
  77. ENDPOINT0_SIZE, // bMaxPacketSize0
  78. LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
  79. LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
  80. 0x00, 0x01, // bcdDevice
  81. 1, // iManufacturer
  82. 2, // iProduct
  83. 0, // iSerialNumber
  84. 1 // bNumConfigurations
  85. };
  86. // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
  87. static uint8_t PROGMEM keyboard_hid_report_desc[] = {
  88. 0x05, 0x01, // Usage Page (Generic Desktop),
  89. 0x09, 0x06, // Usage (Keyboard),
  90. 0xA1, 0x01, // Collection (Application),
  91. 0x75, 0x01, // Report Size (1),
  92. 0x95, 0x08, // Report Count (8),
  93. 0x05, 0x07, // Usage Page (Key Codes),
  94. 0x19, 0xE0, // Usage Minimum (224),
  95. 0x29, 0xE7, // Usage Maximum (231),
  96. 0x15, 0x00, // Logical Minimum (0),
  97. 0x25, 0x01, // Logical Maximum (1),
  98. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
  99. 0x95, 0x01, // Report Count (1),
  100. 0x75, 0x08, // Report Size (8),
  101. 0x81, 0x03, // Input (Constant), ;Reserved byte
  102. 0x95, 0x05, // Report Count (5),
  103. 0x75, 0x01, // Report Size (1),
  104. 0x05, 0x08, // Usage Page (LEDs),
  105. 0x19, 0x01, // Usage Minimum (1),
  106. 0x29, 0x05, // Usage Maximum (5),
  107. 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
  108. 0x95, 0x01, // Report Count (1),
  109. 0x75, 0x03, // Report Size (3),
  110. 0x91, 0x03, // Output (Constant), ;LED report padding
  111. 0x95, 0x06, // Report Count (6),
  112. 0x75, 0x08, // Report Size (8),
  113. 0x15, 0x00, // Logical Minimum (0),
  114. 0x25, 0x68, // Logical Maximum(104),
  115. 0x05, 0x07, // Usage Page (Key Codes),
  116. 0x19, 0x00, // Usage Minimum (0),
  117. 0x29, 0x68, // Usage Maximum (104),
  118. 0x81, 0x00, // Input (Data, Array),
  119. 0xc0 // End Collection
  120. };
  121. static uint8_t PROGMEM debug_hid_report_desc[] = {
  122. 0x06, 0x31, 0xFF, // Usage Page 0xFF31 (vendor defined)
  123. 0x09, 0x74, // Usage 0x74
  124. 0xA1, 0x53, // Collection 0x53
  125. 0x75, 0x08, // report size = 8 bits
  126. 0x15, 0x00, // logical minimum = 0
  127. 0x26, 0xFF, 0x00, // logical maximum = 255
  128. 0x95, DEBUG_TX_SIZE, // report count
  129. 0x09, 0x75, // usage
  130. 0x81, 0x02, // Input (array)
  131. 0xC0 // end collection
  132. };
  133. #define CONFIG1_DESC_SIZE (9+9+9+7+9+9+7)
  134. #define KEYBOARD_HID_DESC_OFFSET (9+9)
  135. #define DEBUG_HID_DESC_OFFSET (9+9+9+7+9)
  136. static uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
  137. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  138. 9, // bLength;
  139. 2, // bDescriptorType;
  140. LSB(CONFIG1_DESC_SIZE), // wTotalLength
  141. MSB(CONFIG1_DESC_SIZE),
  142. 2, // bNumInterfaces
  143. 1, // bConfigurationValue
  144. 0, // iConfiguration
  145. 0xC0, // bmAttributes
  146. 50, // bMaxPower
  147. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  148. 9, // bLength
  149. 4, // bDescriptorType
  150. KEYBOARD_INTERFACE, // bInterfaceNumber
  151. 0, // bAlternateSetting
  152. 1, // bNumEndpoints
  153. 0x03, // bInterfaceClass (0x03 = HID)
  154. 0x01, // bInterfaceSubClass (0x01 = Boot)
  155. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  156. 0, // iInterface
  157. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  158. 9, // bLength
  159. 0x21, // bDescriptorType
  160. 0x11, 0x01, // bcdHID
  161. 0, // bCountryCode
  162. 1, // bNumDescriptors
  163. 0x22, // bDescriptorType
  164. sizeof(keyboard_hid_report_desc), // wDescriptorLength
  165. 0,
  166. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  167. 7, // bLength
  168. 5, // bDescriptorType
  169. KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
  170. 0x03, // bmAttributes (0x03=intr)
  171. KEYBOARD_SIZE, 0, // wMaxPacketSize
  172. 1, // bInterval
  173. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  174. 9, // bLength
  175. 4, // bDescriptorType
  176. DEBUG_INTERFACE, // bInterfaceNumber
  177. 0, // bAlternateSetting
  178. 1, // bNumEndpoints
  179. 0x03, // bInterfaceClass (0x03 = HID)
  180. 0x00, // bInterfaceSubClass
  181. 0x00, // bInterfaceProtocol
  182. 0, // iInterface
  183. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  184. 9, // bLength
  185. 0x21, // bDescriptorType
  186. 0x11, 0x01, // bcdHID
  187. 0, // bCountryCode
  188. 1, // bNumDescriptors
  189. 0x22, // bDescriptorType
  190. sizeof(debug_hid_report_desc), // wDescriptorLength
  191. 0,
  192. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  193. 7, // bLength
  194. 5, // bDescriptorType
  195. DEBUG_TX_ENDPOINT | 0x80, // bEndpointAddress
  196. 0x03, // bmAttributes (0x03=intr)
  197. DEBUG_TX_SIZE, 0, // wMaxPacketSize
  198. 1 // bInterval
  199. };
  200. // If you're desperate for a little extra code memory, these strings
  201. // can be completely removed if iManufacturer, iProduct, iSerialNumber
  202. // in the device desciptor are changed to zeros.
  203. struct usb_string_descriptor_struct {
  204. uint8_t bLength;
  205. uint8_t bDescriptorType;
  206. int16_t wString[];
  207. };
  208. static struct usb_string_descriptor_struct PROGMEM string0 = {
  209. 4,
  210. 3,
  211. {0x0409}
  212. };
  213. static struct usb_string_descriptor_struct PROGMEM string1 = {
  214. sizeof(STR_MANUFACTURER),
  215. 3,
  216. STR_MANUFACTURER
  217. };
  218. static struct usb_string_descriptor_struct PROGMEM string2 = {
  219. sizeof(STR_PRODUCT),
  220. 3,
  221. STR_PRODUCT
  222. };
  223. // This table defines which descriptor data is sent for each specific
  224. // request from the host (in wValue and wIndex).
  225. static struct descriptor_list_struct {
  226. uint16_t wValue; // descriptor type
  227. uint16_t wIndex;
  228. const uint8_t *addr;
  229. uint8_t length;
  230. } PROGMEM descriptor_list[] = {
  231. // DEVICE descriptor
  232. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  233. // CONFIGURATION descriptor
  234. {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
  235. // HID REPORT
  236. {0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
  237. {0x2100, KEYBOARD_INTERFACE, config1_descriptor+KEYBOARD_HID_DESC_OFFSET, 9},
  238. // HID REPORT
  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. // STRING descriptor
  242. {0x0300, 0x0000, (const uint8_t *)&string0, 4},
  243. {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_MANUFACTURER)},
  244. {0x0302, 0x0409, (const uint8_t *)&string2, sizeof(STR_PRODUCT)}
  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. /**************************************************************************
  255. *
  256. * Public Functions - these are the API intended for the user
  257. *
  258. **************************************************************************/
  259. // initialize USB
  260. void usb_init(void)
  261. {
  262. HW_CONFIG();
  263. USB_FREEZE(); // enable USB
  264. PLL_CONFIG(); // config PLL
  265. while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
  266. USB_CONFIG(); // start USB clock
  267. UDCON = 0; // enable attach resistor
  268. usb_configuration = 0;
  269. UDIEN = (1<<EORSTE)|(1<<SOFE);
  270. sei();
  271. }
  272. // return 0 if the USB is not configured, or the configuration
  273. // number selected by the HOST
  274. uint8_t usb_configured(void)
  275. {
  276. return usb_configuration;
  277. }
  278. /**************************************************************************
  279. *
  280. * Private Functions - not intended for general user consumption....
  281. *
  282. **************************************************************************/
  283. // USB Device Interrupt - handle all device-level events
  284. // the transmit buffer flushing is triggered by the start of frame
  285. //
  286. ISR(USB_GEN_vect)
  287. {
  288. uint8_t intbits, t, i;
  289. static uint8_t div4=0;
  290. intbits = UDINT;
  291. UDINT = 0;
  292. if (intbits & (1<<EORSTI)) {
  293. UENUM = 0;
  294. UECONX = 1;
  295. UECFG0X = EP_TYPE_CONTROL;
  296. UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
  297. UEIENX = (1<<RXSTPE);
  298. usb_configuration = 0;
  299. }
  300. if ((intbits & (1<<SOFI)) && usb_configuration) {
  301. t = debug_flush_timer;
  302. if (t) {
  303. debug_flush_timer = -- t;
  304. if (!t) {
  305. UENUM = DEBUG_TX_ENDPOINT;
  306. while ((UEINTX & (1<<RWAL))) {
  307. UEDATX = 0;
  308. }
  309. UEINTX = 0x3A;
  310. }
  311. }
  312. if (keyboard_idle_config && (++div4 & 3) == 0) {
  313. UENUM = KEYBOARD_ENDPOINT;
  314. if (UEINTX & (1<<RWAL)) {
  315. keyboard_idle_count++;
  316. if (keyboard_idle_count == keyboard_idle_config) {
  317. keyboard_idle_count = 0;
  318. UEDATX = keyboard_modifier_keys;
  319. UEDATX = 0;
  320. for (i=0; i<6; i++) {
  321. UEDATX = keyboard_keys[i];
  322. }
  323. UEINTX = 0x3A;
  324. }
  325. }
  326. }
  327. }
  328. }
  329. // Misc functions to wait for ready and send/receive packets
  330. static inline void usb_wait_in_ready(void)
  331. {
  332. while (!(UEINTX & (1<<TXINI))) ;
  333. }
  334. static inline void usb_send_in(void)
  335. {
  336. UEINTX = ~(1<<TXINI);
  337. }
  338. static inline void usb_wait_receive_out(void)
  339. {
  340. while (!(UEINTX & (1<<RXOUTI))) ;
  341. }
  342. static inline void usb_ack_out(void)
  343. {
  344. UEINTX = ~(1<<RXOUTI);
  345. }
  346. // USB Endpoint Interrupt - endpoint 0 is handled here. The
  347. // other endpoints are manipulated by the user-callable
  348. // functions, and the start-of-frame interrupt.
  349. //
  350. ISR(USB_COM_vect)
  351. {
  352. uint8_t intbits;
  353. const uint8_t *list;
  354. const uint8_t *cfg;
  355. uint8_t i, n, len, en;
  356. uint8_t bmRequestType;
  357. uint8_t bRequest;
  358. uint16_t wValue;
  359. uint16_t wIndex;
  360. uint16_t wLength;
  361. uint16_t desc_val;
  362. const uint8_t *desc_addr;
  363. uint8_t desc_length;
  364. UENUM = 0;
  365. intbits = UEINTX;
  366. if (intbits & (1<<RXSTPI)) {
  367. bmRequestType = UEDATX;
  368. bRequest = UEDATX;
  369. wValue = UEDATX;
  370. wValue |= (UEDATX << 8);
  371. wIndex = UEDATX;
  372. wIndex |= (UEDATX << 8);
  373. wLength = UEDATX;
  374. wLength |= (UEDATX << 8);
  375. UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
  376. if (bRequest == GET_DESCRIPTOR) {
  377. list = (const uint8_t *)descriptor_list;
  378. for (i=0; ; i++) {
  379. if (i >= NUM_DESC_LIST) {
  380. UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
  381. return;
  382. }
  383. desc_val = pgm_read_word(list);
  384. if (desc_val != wValue) {
  385. list += sizeof(struct descriptor_list_struct);
  386. continue;
  387. }
  388. list += 2;
  389. desc_val = pgm_read_word(list);
  390. if (desc_val != wIndex) {
  391. list += sizeof(struct descriptor_list_struct)-2;
  392. continue;
  393. }
  394. list += 2;
  395. desc_addr = (const uint8_t *)pgm_read_word(list);
  396. list += 2;
  397. desc_length = pgm_read_byte(list);
  398. break;
  399. }
  400. len = (wLength < 256) ? wLength : 255;
  401. if (len > desc_length) len = desc_length;
  402. do {
  403. // wait for host ready for IN packet
  404. do {
  405. i = UEINTX;
  406. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  407. if (i & (1<<RXOUTI)) return; // abort
  408. // send IN packet
  409. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  410. for (i = n; i; i--) {
  411. UEDATX = pgm_read_byte(desc_addr++);
  412. }
  413. len -= n;
  414. usb_send_in();
  415. } while (len || n == ENDPOINT0_SIZE);
  416. return;
  417. }
  418. if (bRequest == SET_ADDRESS) {
  419. usb_send_in();
  420. usb_wait_in_ready();
  421. UDADDR = wValue | (1<<ADDEN);
  422. return;
  423. }
  424. if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
  425. usb_configuration = wValue;
  426. usb_send_in();
  427. cfg = endpoint_config_table;
  428. for (i=1; i<5; i++) {
  429. UENUM = i;
  430. en = pgm_read_byte(cfg++);
  431. UECONX = en;
  432. if (en) {
  433. UECFG0X = pgm_read_byte(cfg++);
  434. UECFG1X = pgm_read_byte(cfg++);
  435. }
  436. }
  437. UERST = 0x1E;
  438. UERST = 0;
  439. return;
  440. }
  441. if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
  442. usb_wait_in_ready();
  443. UEDATX = usb_configuration;
  444. usb_send_in();
  445. return;
  446. }
  447. if (bRequest == GET_STATUS) {
  448. usb_wait_in_ready();
  449. i = 0;
  450. #ifdef SUPPORT_ENDPOINT_HALT
  451. if (bmRequestType == 0x82) {
  452. UENUM = wIndex;
  453. if (UECONX & (1<<STALLRQ)) i = 1;
  454. UENUM = 0;
  455. }
  456. #endif
  457. UEDATX = i;
  458. UEDATX = 0;
  459. usb_send_in();
  460. return;
  461. }
  462. #ifdef SUPPORT_ENDPOINT_HALT
  463. if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
  464. && bmRequestType == 0x02 && wValue == 0) {
  465. i = wIndex & 0x7F;
  466. if (i >= 1 && i <= MAX_ENDPOINT) {
  467. usb_send_in();
  468. UENUM = i;
  469. if (bRequest == SET_FEATURE) {
  470. UECONX = (1<<STALLRQ)|(1<<EPEN);
  471. } else {
  472. UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
  473. UERST = (1 << i);
  474. UERST = 0;
  475. }
  476. return;
  477. }
  478. }
  479. #endif
  480. if (wIndex == KEYBOARD_INTERFACE) {
  481. if (bmRequestType == 0xA1) {
  482. if (bRequest == HID_GET_REPORT) {
  483. usb_wait_in_ready();
  484. UEDATX = keyboard_modifier_keys;
  485. UEDATX = 0;
  486. for (i=0; i<6; i++) {
  487. UEDATX = keyboard_keys[i];
  488. }
  489. usb_send_in();
  490. return;
  491. }
  492. if (bRequest == HID_GET_IDLE) {
  493. usb_wait_in_ready();
  494. UEDATX = keyboard_idle_config;
  495. usb_send_in();
  496. return;
  497. }
  498. if (bRequest == HID_GET_PROTOCOL) {
  499. usb_wait_in_ready();
  500. UEDATX = keyboard_protocol;
  501. usb_send_in();
  502. return;
  503. }
  504. }
  505. if (bmRequestType == 0x21) {
  506. if (bRequest == HID_SET_REPORT) {
  507. usb_wait_receive_out();
  508. keyboard_leds = UEDATX;
  509. usb_ack_out();
  510. usb_send_in();
  511. return;
  512. }
  513. if (bRequest == HID_SET_IDLE) {
  514. keyboard_idle_config = (wValue >> 8);
  515. keyboard_idle_count = 0;
  516. //usb_wait_in_ready();
  517. usb_send_in();
  518. return;
  519. }
  520. if (bRequest == HID_SET_PROTOCOL) {
  521. keyboard_protocol = wValue;
  522. //usb_wait_in_ready();
  523. usb_send_in();
  524. return;
  525. }
  526. }
  527. }
  528. if (wIndex == DEBUG_INTERFACE) {
  529. if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) {
  530. len = wLength;
  531. do {
  532. // wait for host ready for IN packet
  533. do {
  534. i = UEINTX;
  535. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  536. if (i & (1<<RXOUTI)) return; // abort
  537. // send IN packet
  538. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  539. for (i = n; i; i--) {
  540. UEDATX = 0;
  541. }
  542. len -= n;
  543. usb_send_in();
  544. } while (len || n == ENDPOINT0_SIZE);
  545. return;
  546. }
  547. }
  548. }
  549. UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
  550. }