vudc_transfer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Karol Kosik <karo9@interia.eu>
  4. * Copyright (C) 2015-2016 Samsung Electronics
  5. * Igor Kotrasinski <i.kotrasinsk@samsung.com>
  6. *
  7. * Based on dummy_hcd.c, which is:
  8. * Copyright (C) 2003 David Brownell
  9. * Copyright (C) 2003-2005 Alan Stern
  10. */
  11. #include <linux/usb.h>
  12. #include <linux/timer.h>
  13. #include <linux/usb/ch9.h>
  14. #include "vudc.h"
  15. #define DEV_REQUEST (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
  16. #define DEV_INREQUEST (DEV_REQUEST | USB_DIR_IN)
  17. #define INTF_REQUEST (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
  18. #define INTF_INREQUEST (INTF_REQUEST | USB_DIR_IN)
  19. #define EP_REQUEST (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
  20. #define EP_INREQUEST (EP_REQUEST | USB_DIR_IN)
  21. static int get_frame_limit(enum usb_device_speed speed)
  22. {
  23. switch (speed) {
  24. case USB_SPEED_LOW:
  25. return 8 /*bytes*/ * 12 /*packets*/;
  26. case USB_SPEED_FULL:
  27. return 64 /*bytes*/ * 19 /*packets*/;
  28. case USB_SPEED_HIGH:
  29. return 512 /*bytes*/ * 13 /*packets*/ * 8 /*uframes*/;
  30. case USB_SPEED_SUPER:
  31. /* Bus speed is 500000 bytes/ms, so use a little less */
  32. return 490000;
  33. default:
  34. /* error */
  35. return -1;
  36. }
  37. }
  38. /*
  39. * handle_control_request() - handles all control transfers
  40. * @udc: pointer to vudc
  41. * @urb: the urb request to handle
  42. * @setup: pointer to the setup data for a USB device control
  43. * request
  44. * @status: pointer to request handling status
  45. *
  46. * Return 0 - if the request was handled
  47. * 1 - if the request wasn't handles
  48. * error code on error
  49. *
  50. * Adapted from drivers/usb/gadget/udc/dummy_hcd.c
  51. */
  52. static int handle_control_request(struct vudc *udc, struct urb *urb,
  53. struct usb_ctrlrequest *setup,
  54. int *status)
  55. {
  56. struct vep *ep2;
  57. int ret_val = 1;
  58. unsigned int w_index;
  59. unsigned int w_value;
  60. w_index = le16_to_cpu(setup->wIndex);
  61. w_value = le16_to_cpu(setup->wValue);
  62. switch (setup->bRequest) {
  63. case USB_REQ_SET_ADDRESS:
  64. if (setup->bRequestType != DEV_REQUEST)
  65. break;
  66. udc->address = w_value;
  67. ret_val = 0;
  68. *status = 0;
  69. break;
  70. case USB_REQ_SET_FEATURE:
  71. if (setup->bRequestType == DEV_REQUEST) {
  72. ret_val = 0;
  73. switch (w_value) {
  74. case USB_DEVICE_REMOTE_WAKEUP:
  75. break;
  76. case USB_DEVICE_B_HNP_ENABLE:
  77. udc->gadget.b_hnp_enable = 1;
  78. break;
  79. case USB_DEVICE_A_HNP_SUPPORT:
  80. udc->gadget.a_hnp_support = 1;
  81. break;
  82. case USB_DEVICE_A_ALT_HNP_SUPPORT:
  83. udc->gadget.a_alt_hnp_support = 1;
  84. break;
  85. default:
  86. ret_val = -EOPNOTSUPP;
  87. }
  88. if (ret_val == 0) {
  89. udc->devstatus |= (1 << w_value);
  90. *status = 0;
  91. }
  92. } else if (setup->bRequestType == EP_REQUEST) {
  93. /* endpoint halt */
  94. ep2 = vudc_find_endpoint(udc, w_index);
  95. if (!ep2 || ep2->ep.name == udc->ep[0].ep.name) {
  96. ret_val = -EOPNOTSUPP;
  97. break;
  98. }
  99. ep2->halted = 1;
  100. ret_val = 0;
  101. *status = 0;
  102. }
  103. break;
  104. case USB_REQ_CLEAR_FEATURE:
  105. if (setup->bRequestType == DEV_REQUEST) {
  106. ret_val = 0;
  107. switch (w_value) {
  108. case USB_DEVICE_REMOTE_WAKEUP:
  109. w_value = USB_DEVICE_REMOTE_WAKEUP;
  110. break;
  111. case USB_DEVICE_U1_ENABLE:
  112. case USB_DEVICE_U2_ENABLE:
  113. case USB_DEVICE_LTM_ENABLE:
  114. ret_val = -EOPNOTSUPP;
  115. break;
  116. default:
  117. ret_val = -EOPNOTSUPP;
  118. break;
  119. }
  120. if (ret_val == 0) {
  121. udc->devstatus &= ~(1 << w_value);
  122. *status = 0;
  123. }
  124. } else if (setup->bRequestType == EP_REQUEST) {
  125. /* endpoint halt */
  126. ep2 = vudc_find_endpoint(udc, w_index);
  127. if (!ep2) {
  128. ret_val = -EOPNOTSUPP;
  129. break;
  130. }
  131. if (!ep2->wedged)
  132. ep2->halted = 0;
  133. ret_val = 0;
  134. *status = 0;
  135. }
  136. break;
  137. case USB_REQ_GET_STATUS:
  138. if (setup->bRequestType == DEV_INREQUEST
  139. || setup->bRequestType == INTF_INREQUEST
  140. || setup->bRequestType == EP_INREQUEST) {
  141. char *buf;
  142. /*
  143. * device: remote wakeup, selfpowered
  144. * interface: nothing
  145. * endpoint: halt
  146. */
  147. buf = (char *)urb->transfer_buffer;
  148. if (urb->transfer_buffer_length > 0) {
  149. if (setup->bRequestType == EP_INREQUEST) {
  150. ep2 = vudc_find_endpoint(udc, w_index);
  151. if (!ep2) {
  152. ret_val = -EOPNOTSUPP;
  153. break;
  154. }
  155. buf[0] = ep2->halted;
  156. } else if (setup->bRequestType ==
  157. DEV_INREQUEST) {
  158. buf[0] = (u8)udc->devstatus;
  159. } else
  160. buf[0] = 0;
  161. }
  162. if (urb->transfer_buffer_length > 1)
  163. buf[1] = 0;
  164. urb->actual_length = min_t(u32, 2,
  165. urb->transfer_buffer_length);
  166. ret_val = 0;
  167. *status = 0;
  168. }
  169. break;
  170. }
  171. return ret_val;
  172. }
  173. /* Adapted from dummy_hcd.c ; caller must hold lock */
  174. static int transfer(struct vudc *udc,
  175. struct urb *urb, struct vep *ep, int limit)
  176. {
  177. struct vrequest *req;
  178. int sent = 0;
  179. top:
  180. /* if there's no request queued, the device is NAKing; return */
  181. list_for_each_entry(req, &ep->req_queue, req_entry) {
  182. unsigned int host_len, dev_len, len;
  183. void *ubuf_pos, *rbuf_pos;
  184. int is_short, to_host;
  185. int rescan = 0;
  186. /*
  187. * 1..N packets of ep->ep.maxpacket each ... the last one
  188. * may be short (including zero length).
  189. *
  190. * writer can send a zlp explicitly (length 0) or implicitly
  191. * (length mod maxpacket zero, and 'zero' flag); they always
  192. * terminate reads.
  193. */
  194. host_len = urb->transfer_buffer_length - urb->actual_length;
  195. dev_len = req->req.length - req->req.actual;
  196. len = min(host_len, dev_len);
  197. to_host = usb_pipein(urb->pipe);
  198. if (unlikely(len == 0))
  199. is_short = 1;
  200. else {
  201. /* send multiple of maxpacket first, then remainder */
  202. if (len >= ep->ep.maxpacket) {
  203. is_short = 0;
  204. if (len % ep->ep.maxpacket > 0)
  205. rescan = 1;
  206. len -= len % ep->ep.maxpacket;
  207. } else {
  208. is_short = 1;
  209. }
  210. ubuf_pos = urb->transfer_buffer + urb->actual_length;
  211. rbuf_pos = req->req.buf + req->req.actual;
  212. if (urb->pipe & USB_DIR_IN)
  213. memcpy(ubuf_pos, rbuf_pos, len);
  214. else
  215. memcpy(rbuf_pos, ubuf_pos, len);
  216. urb->actual_length += len;
  217. req->req.actual += len;
  218. sent += len;
  219. }
  220. /*
  221. * short packets terminate, maybe with overflow/underflow.
  222. * it's only really an error to write too much.
  223. *
  224. * partially filling a buffer optionally blocks queue advances
  225. * (so completion handlers can clean up the queue) but we don't
  226. * need to emulate such data-in-flight.
  227. */
  228. if (is_short) {
  229. if (host_len == dev_len) {
  230. req->req.status = 0;
  231. urb->status = 0;
  232. } else if (to_host) {
  233. req->req.status = 0;
  234. if (dev_len > host_len)
  235. urb->status = -EOVERFLOW;
  236. else
  237. urb->status = 0;
  238. } else {
  239. urb->status = 0;
  240. if (host_len > dev_len)
  241. req->req.status = -EOVERFLOW;
  242. else
  243. req->req.status = 0;
  244. }
  245. /* many requests terminate without a short packet */
  246. /* also check if we need to send zlp */
  247. } else {
  248. if (req->req.length == req->req.actual) {
  249. if (req->req.zero && to_host)
  250. rescan = 1;
  251. else
  252. req->req.status = 0;
  253. }
  254. if (urb->transfer_buffer_length == urb->actual_length) {
  255. if (urb->transfer_flags & URB_ZERO_PACKET &&
  256. !to_host)
  257. rescan = 1;
  258. else
  259. urb->status = 0;
  260. }
  261. }
  262. /* device side completion --> continuable */
  263. if (req->req.status != -EINPROGRESS) {
  264. list_del_init(&req->req_entry);
  265. spin_unlock(&udc->lock);
  266. usb_gadget_giveback_request(&ep->ep, &req->req);
  267. spin_lock(&udc->lock);
  268. /* requests might have been unlinked... */
  269. rescan = 1;
  270. }
  271. /* host side completion --> terminate */
  272. if (urb->status != -EINPROGRESS)
  273. break;
  274. /* rescan to continue with any other queued i/o */
  275. if (rescan)
  276. goto top;
  277. }
  278. return sent;
  279. }
  280. static void v_timer(struct timer_list *t)
  281. {
  282. struct vudc *udc = from_timer(udc, t, tr_timer.timer);
  283. struct transfer_timer *timer = &udc->tr_timer;
  284. struct urbp *urb_p, *tmp;
  285. unsigned long flags;
  286. struct usb_ep *_ep;
  287. struct vep *ep;
  288. int ret = 0;
  289. int total, limit;
  290. spin_lock_irqsave(&udc->lock, flags);
  291. total = get_frame_limit(udc->gadget.speed);
  292. if (total < 0) { /* unknown speed, or not set yet */
  293. timer->state = VUDC_TR_IDLE;
  294. spin_unlock_irqrestore(&udc->lock, flags);
  295. return;
  296. }
  297. /* is it next frame now? */
  298. if (time_after(jiffies, timer->frame_start + msecs_to_jiffies(1))) {
  299. timer->frame_limit = total;
  300. /* FIXME: how to make it accurate? */
  301. timer->frame_start = jiffies;
  302. } else {
  303. total = timer->frame_limit;
  304. }
  305. /* We have to clear ep0 flags separately as it's not on the list */
  306. udc->ep[0].already_seen = 0;
  307. list_for_each_entry(_ep, &udc->gadget.ep_list, ep_list) {
  308. ep = to_vep(_ep);
  309. ep->already_seen = 0;
  310. }
  311. restart:
  312. list_for_each_entry_safe(urb_p, tmp, &udc->urb_queue, urb_entry) {
  313. struct urb *urb = urb_p->urb;
  314. ep = urb_p->ep;
  315. if (urb->unlinked)
  316. goto return_urb;
  317. if (timer->state != VUDC_TR_RUNNING)
  318. continue;
  319. if (!ep) {
  320. urb->status = -EPROTO;
  321. goto return_urb;
  322. }
  323. /* Used up bandwidth? */
  324. if (total <= 0 && ep->type == USB_ENDPOINT_XFER_BULK)
  325. continue;
  326. if (ep->already_seen)
  327. continue;
  328. ep->already_seen = 1;
  329. if (ep == &udc->ep[0] && urb_p->new) {
  330. ep->setup_stage = 1;
  331. urb_p->new = 0;
  332. }
  333. if (ep->halted && !ep->setup_stage) {
  334. urb->status = -EPIPE;
  335. goto return_urb;
  336. }
  337. if (ep == &udc->ep[0] && ep->setup_stage) {
  338. /* TODO - flush any stale requests */
  339. ep->setup_stage = 0;
  340. ep->halted = 0;
  341. ret = handle_control_request(udc, urb,
  342. (struct usb_ctrlrequest *) urb->setup_packet,
  343. (&urb->status));
  344. if (ret > 0) {
  345. spin_unlock(&udc->lock);
  346. ret = udc->driver->setup(&udc->gadget,
  347. (struct usb_ctrlrequest *)
  348. urb->setup_packet);
  349. spin_lock(&udc->lock);
  350. }
  351. if (ret >= 0) {
  352. /* no delays (max 64kb data stage) */
  353. limit = 64 * 1024;
  354. goto treat_control_like_bulk;
  355. } else {
  356. urb->status = -EPIPE;
  357. urb->actual_length = 0;
  358. goto return_urb;
  359. }
  360. }
  361. limit = total;
  362. switch (ep->type) {
  363. case USB_ENDPOINT_XFER_ISOC:
  364. /* TODO: support */
  365. urb->status = -EXDEV;
  366. break;
  367. case USB_ENDPOINT_XFER_INT:
  368. /*
  369. * TODO: figure out bandwidth guarantees
  370. * for now, give unlimited bandwidth
  371. */
  372. limit += urb->transfer_buffer_length;
  373. fallthrough;
  374. default:
  375. treat_control_like_bulk:
  376. total -= transfer(udc, urb, ep, limit);
  377. }
  378. if (urb->status == -EINPROGRESS)
  379. continue;
  380. return_urb:
  381. if (ep)
  382. ep->already_seen = ep->setup_stage = 0;
  383. spin_lock(&udc->lock_tx);
  384. list_del(&urb_p->urb_entry);
  385. if (!urb->unlinked) {
  386. v_enqueue_ret_submit(udc, urb_p);
  387. } else {
  388. v_enqueue_ret_unlink(udc, urb_p->seqnum,
  389. urb->unlinked);
  390. free_urbp_and_urb(urb_p);
  391. }
  392. wake_up(&udc->tx_waitq);
  393. spin_unlock(&udc->lock_tx);
  394. goto restart;
  395. }
  396. /* TODO - also wait on empty usb_request queues? */
  397. if (list_empty(&udc->urb_queue))
  398. timer->state = VUDC_TR_IDLE;
  399. else
  400. mod_timer(&timer->timer,
  401. timer->frame_start + msecs_to_jiffies(1));
  402. spin_unlock_irqrestore(&udc->lock, flags);
  403. }
  404. /* All timer functions are run with udc->lock held */
  405. void v_init_timer(struct vudc *udc)
  406. {
  407. struct transfer_timer *t = &udc->tr_timer;
  408. timer_setup(&t->timer, v_timer, 0);
  409. t->state = VUDC_TR_STOPPED;
  410. }
  411. void v_start_timer(struct vudc *udc)
  412. {
  413. struct transfer_timer *t = &udc->tr_timer;
  414. dev_dbg(&udc->pdev->dev, "timer start");
  415. switch (t->state) {
  416. case VUDC_TR_RUNNING:
  417. return;
  418. case VUDC_TR_IDLE:
  419. return v_kick_timer(udc, jiffies);
  420. case VUDC_TR_STOPPED:
  421. t->state = VUDC_TR_IDLE;
  422. t->frame_start = jiffies;
  423. t->frame_limit = get_frame_limit(udc->gadget.speed);
  424. return v_kick_timer(udc, jiffies);
  425. }
  426. }
  427. void v_kick_timer(struct vudc *udc, unsigned long time)
  428. {
  429. struct transfer_timer *t = &udc->tr_timer;
  430. dev_dbg(&udc->pdev->dev, "timer kick");
  431. switch (t->state) {
  432. case VUDC_TR_RUNNING:
  433. return;
  434. case VUDC_TR_IDLE:
  435. t->state = VUDC_TR_RUNNING;
  436. fallthrough;
  437. case VUDC_TR_STOPPED:
  438. /* we may want to kick timer to unqueue urbs */
  439. mod_timer(&t->timer, time);
  440. }
  441. }
  442. void v_stop_timer(struct vudc *udc)
  443. {
  444. struct transfer_timer *t = &udc->tr_timer;
  445. /* timer itself will take care of stopping */
  446. dev_dbg(&udc->pdev->dev, "timer stop");
  447. t->state = VUDC_TR_STOPPED;
  448. }