usb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Tiny USB stack
  3. *
  4. * Copyright (C) 2009-2016 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include "usb.h"
  16. #include "usb_config.h"
  17. #include "usb_application.h"
  18. #include "main.h"
  19. #include "util.h"
  20. #include <string.h>
  21. #include <avr/pgmspace.h>
  22. #if USB_MINI
  23. # include "descriptor_table_mini.h"
  24. #else
  25. # include "descriptor_table.h"
  26. #endif
  27. enum usb_frame_status {
  28. USB_FRAME_UNHANDLED,
  29. USB_FRAME_HANDLED,
  30. USB_FRAME_ERROR,
  31. };
  32. static uint8_t usb_control_buf[USBCFG_EP0_MAXSIZE];
  33. static uint8_t usb_control_ptr;
  34. static uint8_t usb_control_len;
  35. static bool usb_control_nullframe_pending;
  36. #if USB_WITH_EP1
  37. static uint8_t usb_ep1_buf[USBCFG_EP1_MAXSIZE];
  38. static uint8_t usb_ep1_ptr;
  39. static uint8_t usb_ep1_len;
  40. #endif /* WITH_EP1 */
  41. #if USB_WITH_EP2
  42. static uint8_t usb_ep2_buf[USBCFG_EP2_MAXSIZE];
  43. static uint8_t usb_ep2_ptr;
  44. static uint8_t usb_ep2_len;
  45. #endif /* WITH_EP2 */
  46. /* Current USB_DEVICE_... bits. */
  47. static uint16_t usb_device_status;
  48. /* The active bConfigurationValue (or zero if none). */
  49. static uint8_t usb_active_configuration;
  50. void usb_reset(void)
  51. {
  52. usb_control_len = 0;
  53. usb_control_nullframe_pending = 0;
  54. #if USB_WITH_EP1
  55. usb_ep1_len = 0;
  56. #endif
  57. #if USB_WITH_EP2
  58. usb_ep2_len = 0;
  59. #endif
  60. usb_device_status = ((!!USBCFG_SELFPOWERED) << USB_DEVICE_SELF_POWERED);
  61. usb_active_configuration = 0;
  62. usb_app_reset();
  63. }
  64. static uint8_t create_device_descriptor(void *buf)
  65. {
  66. DBG(usb_printstr("USB: Requested device descriptor"));
  67. usb_copy_from_pgm(buf, &device_descriptor, sizeof(device_descriptor));
  68. return sizeof(device_descriptor);
  69. }
  70. static uint8_t create_config_descriptor(void *buf, uint8_t index)
  71. {
  72. const uint8_t * USB_PROGMEM ptr;
  73. uint8_t size;
  74. DBG(usb_print1num("USB: Requested config descriptor", index));
  75. if (index >= ARRAY_SIZE(config_descriptor_ptrs)) {
  76. usb_printstr("USB: Get config descriptor index out of range");
  77. return 0xFF;
  78. }
  79. ptr = (void *)(uintptr_t)usb_pgm_read(&config_descriptor_ptrs[index].ptr);
  80. size = (uint8_t)usb_pgm_read(&config_descriptor_ptrs[index].size);
  81. usb_copy_from_pgm(buf, ptr, size);
  82. return size;
  83. }
  84. static uint8_t create_string_descriptor(void *buf, uint8_t index)
  85. {
  86. struct usb_string_descriptor *s = buf;
  87. const char * USB_PROGMEM string;
  88. uint8_t size;
  89. DBG(usb_print1num("USB: Requested string descriptor", index));
  90. if (index >= ARRAY_SIZE(string_descriptor_ptrs)) {
  91. usb_printstr("USB: Get string descriptor index out of range");
  92. return 0xFF;
  93. }
  94. string = (void *)(uintptr_t)usb_pgm_read(&string_descriptor_ptrs[index].ptr);
  95. size = (uint8_t)usb_pgm_read(&string_descriptor_ptrs[index].size);
  96. s->bLength = (uint8_t)(2u + size);
  97. s->bDescriptorType = USB_DT_STRING;
  98. usb_copy_from_pgm(s->string, string, size);
  99. return s->bLength;
  100. }
  101. static uint8_t usb_set_configuration(uint8_t bConfigurationValue)
  102. {
  103. DBG(usb_print1num("USB: Set configuration", bConfigurationValue));
  104. if (bConfigurationValue) {
  105. /* Select a configuration */
  106. if (bConfigurationValue - 1u >= ARRAY_SIZE(config_descriptor_ptrs)) {
  107. usb_printstr("USB: Invalid bConfigurationValue");
  108. return 1;
  109. }
  110. usb_enable_endpoints(1);
  111. usb_app_highpower(1);
  112. } else {
  113. /* Deconfigure the device */
  114. usb_app_highpower(0);
  115. usb_enable_endpoints(0);
  116. }
  117. usb_active_configuration = bConfigurationValue;
  118. return 0;
  119. }
  120. static uint8_t usb_control_endpoint_rx(struct usb_ctrl *ctl)
  121. {
  122. switch (ctl->bRequest) {
  123. case USB_REQ_GET_STATUS: {
  124. uint16_t index = le16_to_cpu(ctl->wIndex);
  125. DBG(usb_print1num("USB: EP get status on", index));
  126. usb_control_buf[0] = 0;
  127. usb_control_buf[1] = 0;
  128. usb_control_len = 2;
  129. if (index <= 0xFFu &&
  130. usb_endpoint_is_stalled((uint8_t)index))
  131. usb_control_buf[0] |= (1 << USB_ENDPOINT_HALT);
  132. break;
  133. }
  134. case USB_REQ_CLEAR_FEATURE: {
  135. uint16_t index = le16_to_cpu(ctl->wIndex);
  136. uint16_t feature = le16_to_cpu(ctl->wValue);
  137. DBG(usb_print2num("USB: EP clear feature", feature,
  138. "on", index));
  139. if (index <= 0xFFu &&
  140. (feature & (1 << USB_ENDPOINT_HALT)))
  141. usb_unstall_endpoint((uint8_t)index);
  142. break;
  143. }
  144. case USB_REQ_SET_FEATURE: {
  145. uint16_t index = le16_to_cpu(ctl->wIndex);
  146. uint16_t feature = le16_to_cpu(ctl->wValue);
  147. DBG(usb_print2num("USB: EP set feature", feature,
  148. "on", index));
  149. if (index <= 0xFFu &&
  150. (feature & (1 << USB_ENDPOINT_HALT)))
  151. usb_stall_endpoint((uint8_t)index);
  152. break;
  153. }
  154. default:
  155. return USB_FRAME_UNHANDLED;
  156. }
  157. return USB_FRAME_HANDLED;
  158. }
  159. static uint8_t usb_control_interface_rx(struct usb_ctrl *ctl)
  160. {
  161. switch (ctl->bRequest) {
  162. case USB_REQ_GET_INTERFACE: {
  163. uint8_t bAlternateSetting;
  164. DBG(usb_printstr("USB: IF get interface"));
  165. /* We only support one altsetting. */
  166. bAlternateSetting = 0;
  167. usb_control_buf[0] = bAlternateSetting;
  168. usb_control_len = 1;
  169. return USB_FRAME_HANDLED;
  170. }
  171. case USB_REQ_SET_INTERFACE: {
  172. uint16_t bInterfaceNumber = le16_to_cpu(ctl->wIndex);
  173. uint16_t bAlternateSetting = le16_to_cpu(ctl->wValue);
  174. DBG(usb_print2num("USB: IF set interface", bInterfaceNumber,
  175. "altsetting", bAlternateSetting));
  176. /* We only support one interface and altsetting */
  177. if (bInterfaceNumber != 0 ||
  178. bAlternateSetting != 0)
  179. return USB_FRAME_ERROR;
  180. return USB_FRAME_HANDLED;
  181. }
  182. case USB_REQ_GET_STATUS: {
  183. DBG(usb_printstr("USB: IF get status"));
  184. usb_control_buf[0] = 0;
  185. usb_control_buf[1] = 0;
  186. usb_control_len = 2;
  187. return USB_FRAME_HANDLED;
  188. }
  189. case USB_REQ_SET_FEATURE: {
  190. DBG(usb_printstr("USB: IF set feature"));
  191. return USB_FRAME_HANDLED;
  192. }
  193. case USB_REQ_CLEAR_FEATURE: {
  194. DBG(usb_printstr("USB: IF clear feature"));
  195. return USB_FRAME_HANDLED;
  196. } }
  197. return USB_FRAME_UNHANDLED;
  198. }
  199. static uint8_t usb_control_device_rx(struct usb_ctrl *ctl)
  200. {
  201. uint8_t res;
  202. switch (ctl->bRequest) {
  203. case USB_REQ_GET_DESCRIPTOR: {
  204. switch (le16_to_cpu(ctl->wValue) >> 8) {
  205. case USB_DT_DEVICE:
  206. res = create_device_descriptor(usb_control_buf);
  207. break;
  208. case USB_DT_CONFIG:
  209. res = create_config_descriptor(
  210. usb_control_buf, le16_to_cpu(ctl->wValue) & 0xFF);
  211. break;
  212. case USB_DT_STRING:
  213. res = create_string_descriptor(
  214. usb_control_buf, le16_to_cpu(ctl->wValue) & 0xFF);
  215. break;
  216. default:
  217. return USB_FRAME_UNHANDLED;
  218. }
  219. if (res == 0xFF)
  220. return USB_FRAME_ERROR;
  221. usb_control_len = res;
  222. break;
  223. }
  224. case USB_REQ_SET_ADDRESS: {
  225. uint16_t address = le16_to_cpu(ctl->wValue);
  226. if (address <= 0x7Fu) {
  227. DBG(usb_print1num("USB: DEV set address to", address));
  228. usb_set_address((uint8_t)address);
  229. }
  230. break;
  231. }
  232. case USB_REQ_GET_CONFIGURATION: {
  233. DBG(usb_printstr("USB: DEV get configuration"));
  234. usb_control_buf[0] = usb_active_configuration;
  235. usb_control_len = 1;
  236. break;
  237. }
  238. case USB_REQ_SET_CONFIGURATION: {
  239. uint16_t cfg = le16_to_cpu(ctl->wValue);
  240. if (cfg > 0xFFu ||
  241. usb_set_configuration((uint8_t)cfg))
  242. return USB_FRAME_ERROR;
  243. break;
  244. }
  245. case USB_REQ_GET_STATUS: {
  246. DBG(usb_printstr("USB: DEV get status"));
  247. usb_control_buf[0] = (uint8_t)usb_device_status;
  248. usb_control_buf[1] = (uint8_t)(usb_device_status >> 8);
  249. usb_control_len = 2;
  250. break;
  251. }
  252. case USB_REQ_SET_FEATURE: {
  253. uint16_t feature = le16_to_cpu(ctl->wValue);
  254. DBG(usb_printstr("USB: DEV set feature"));
  255. if (feature >= 16 || feature == USB_DEVICE_SELF_POWERED) {
  256. usb_print1num("USB: Illegal set feature request", feature);
  257. return USB_FRAME_ERROR;
  258. }
  259. usb_device_status |= ((uint16_t)1 << feature);
  260. break;
  261. }
  262. case USB_REQ_CLEAR_FEATURE: {
  263. uint16_t feature = le16_to_cpu(ctl->wValue);
  264. DBG(usb_printstr("USB: DEV clear feature"));
  265. if (feature >= 16 || feature == USB_DEVICE_SELF_POWERED) {
  266. usb_print1num("USB: Illegal clear feature request", feature);
  267. return USB_FRAME_ERROR;
  268. }
  269. usb_device_status &= ~((uint16_t)1 << feature);
  270. break;
  271. }
  272. default:
  273. return USB_FRAME_UNHANDLED;
  274. }
  275. return USB_FRAME_HANDLED;
  276. }
  277. uint8_t usb_control_setup_rx(struct usb_ctrl *ctl)
  278. {
  279. uint8_t status = USB_FRAME_UNHANDLED, res;
  280. usb_control_len = 0;
  281. usb_control_ptr = 0;
  282. usb_control_nullframe_pending = 0;
  283. switch (ctl->bRequestType & USB_RECIP_MASK) {
  284. case USB_RECIP_DEVICE:
  285. status = usb_control_device_rx(ctl);
  286. break;
  287. case USB_RECIP_INTERFACE:
  288. status = usb_control_interface_rx(ctl);
  289. break;
  290. case USB_RECIP_ENDPOINT:
  291. status = usb_control_endpoint_rx(ctl);
  292. break;
  293. }
  294. if (status == USB_FRAME_ERROR)
  295. return USB_RX_ERROR;
  296. if (status == USB_FRAME_UNHANDLED) {
  297. /* Nobody handled the frame.
  298. * Try if the application layer is able to service the frame. */
  299. res = usb_app_control_setup_rx(ctl, usb_control_buf);
  300. if (res == USB_APP_UNHANDLED) {
  301. usb_printstr("USB: Unhandled control frame:");
  302. usb_dumpmem(ctl, sizeof(*ctl));
  303. return USB_RX_DONE;
  304. }
  305. usb_control_len = res;
  306. }
  307. if (usb_ctrl_is_out(ctl)) { /* OUT transfer */
  308. usb_control_nullframe_pending = 1;
  309. if (unlikely(usb_control_len)) {
  310. usb_control_len = 0;
  311. usb_printstr("USB: Want to reply, but host did not request it");
  312. return USB_RX_ERROR;
  313. }
  314. } else { /* IN transfer */
  315. uint16_t len = le16_to_cpu(ctl->wLength);
  316. if ((uint16_t)usb_control_len > len)
  317. usb_control_len = (uint8_t)len;
  318. }
  319. return USB_RX_DONE;
  320. }
  321. uint8_t usb_control_rx(void *data, uint8_t size)
  322. {
  323. if (size == 0) {
  324. DBG(usb_printstr("USB: Received data ACK (zero size data1)"));
  325. return USB_RX_DONE;
  326. }
  327. usb_print1num("USB: Unhandled control RX of size", size);
  328. return USB_RX_ERROR;
  329. }
  330. static noinline uint8_t usb_generic_tx_poll(void **data, uint8_t chunksize,
  331. uint8_t *buffer, uint8_t *buffer_size, uint8_t *buffer_ptr)
  332. {
  333. if (chunksize > *buffer_size)
  334. chunksize = *buffer_size;
  335. *buffer_size = (uint8_t)(*buffer_size - chunksize);
  336. *data = buffer + *buffer_ptr;
  337. if (*buffer_size)
  338. *buffer_ptr = (uint8_t)(*buffer_ptr + chunksize);
  339. else
  340. *buffer_ptr = 0u;
  341. if (!chunksize)
  342. return USB_TX_POLL_NONE;
  343. return chunksize;
  344. }
  345. uint8_t usb_control_tx_poll(void **data, uint8_t chunksize)
  346. {
  347. uint8_t res;
  348. res = usb_generic_tx_poll(data, chunksize, usb_control_buf,
  349. &usb_control_len, &usb_control_ptr);
  350. if (res == USB_TX_POLL_NONE) {
  351. if (usb_control_nullframe_pending) {
  352. /* Send zero length frame */
  353. usb_control_nullframe_pending = 0;
  354. return 0;
  355. }
  356. }
  357. return res;
  358. }
  359. #if USB_WITH_EP1
  360. uint8_t usb_ep1_rx(void *data, uint8_t size)
  361. {
  362. uint8_t res;
  363. usb_ep1_len = 0;
  364. usb_ep1_ptr = 0;
  365. res = usb_app_ep1_rx(data, size, usb_ep1_buf);
  366. if (res != USB_APP_UNHANDLED) {
  367. usb_ep1_len = res;
  368. return USB_RX_DONE;
  369. }
  370. usb_printstr("USB: Unhandled EP1 frame:");
  371. usb_dumpmem(data, size);
  372. return USB_RX_DONE;
  373. }
  374. uint8_t usb_ep1_tx_poll(void **data, uint8_t chunksize)
  375. {
  376. uint8_t res;
  377. if (usb_ep1_len == 0) {
  378. res = usb_app_ep1_tx_poll(usb_ep1_buf);
  379. if (res == USB_APP_UNHANDLED)
  380. return USB_TX_POLL_NONE;
  381. usb_ep1_len = res;
  382. usb_ep1_ptr = 0;
  383. if (!res)
  384. return 0;
  385. }
  386. res = usb_generic_tx_poll(data, chunksize, usb_ep1_buf,
  387. &usb_ep1_len, &usb_ep1_ptr);
  388. return res;
  389. }
  390. #endif /* WITH_EP1 */
  391. #if USB_WITH_EP2
  392. uint8_t usb_ep2_rx(void *data, uint8_t size)
  393. {
  394. uint8_t res;
  395. usb_ep2_len = 0;
  396. usb_ep2_ptr = 0;
  397. res = usb_app_ep2_rx(data, size, usb_ep2_buf);
  398. if (res != USB_APP_UNHANDLED) {
  399. usb_ep2_len = res;
  400. return USB_RX_DONE;
  401. }
  402. usb_printstr("USB: Unhandled EP2 frame:");
  403. usb_dumpmem(data, size);
  404. return USB_RX_DONE;
  405. }
  406. uint8_t usb_ep2_tx_poll(void **data, uint8_t chunksize)
  407. {
  408. uint8_t res;
  409. if (usb_ep2_len == 0) {
  410. res = usb_app_ep2_tx_poll(usb_ep2_buf);
  411. if (res == USB_APP_UNHANDLED)
  412. return USB_TX_POLL_NONE;
  413. usb_ep2_len = res;
  414. usb_ep2_ptr = 0;
  415. if (!res)
  416. return 0;
  417. }
  418. res = usb_generic_tx_poll(data, chunksize, usb_ep2_buf,
  419. &usb_ep2_len, &usb_ep2_ptr);
  420. return res;
  421. }
  422. #endif /* WITH_EP2 */