vudc_transfer.c 12 KB

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