vudc_dev.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. * Krzysztof Opasiak <k.opasiak@samsung.com>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/list.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/usb.h>
  13. #include <linux/usb/gadget.h>
  14. #include <linux/usb/hcd.h>
  15. #include <linux/kthread.h>
  16. #include <linux/file.h>
  17. #include <linux/byteorder/generic.h>
  18. #include "usbip_common.h"
  19. #include "vudc.h"
  20. #define VIRTUAL_ENDPOINTS (1 /* ep0 */ + 15 /* in eps */ + 15 /* out eps */)
  21. /* urb-related structures alloc / free */
  22. static void free_urb(struct urb *urb)
  23. {
  24. if (!urb)
  25. return;
  26. kfree(urb->setup_packet);
  27. urb->setup_packet = NULL;
  28. kfree(urb->transfer_buffer);
  29. urb->transfer_buffer = NULL;
  30. usb_free_urb(urb);
  31. }
  32. struct urbp *alloc_urbp(void)
  33. {
  34. struct urbp *urb_p;
  35. urb_p = kzalloc(sizeof(*urb_p), GFP_KERNEL);
  36. if (!urb_p)
  37. return urb_p;
  38. urb_p->urb = NULL;
  39. urb_p->ep = NULL;
  40. INIT_LIST_HEAD(&urb_p->urb_entry);
  41. return urb_p;
  42. }
  43. static void free_urbp(struct urbp *urb_p)
  44. {
  45. kfree(urb_p);
  46. }
  47. void free_urbp_and_urb(struct urbp *urb_p)
  48. {
  49. if (!urb_p)
  50. return;
  51. free_urb(urb_p->urb);
  52. free_urbp(urb_p);
  53. }
  54. /* utilities ; almost verbatim from dummy_hcd.c */
  55. /* called with spinlock held */
  56. static void nuke(struct vudc *udc, struct vep *ep)
  57. {
  58. struct vrequest *req;
  59. while (!list_empty(&ep->req_queue)) {
  60. req = list_first_entry(&ep->req_queue, struct vrequest,
  61. req_entry);
  62. list_del_init(&req->req_entry);
  63. req->req.status = -ESHUTDOWN;
  64. spin_unlock(&udc->lock);
  65. usb_gadget_giveback_request(&ep->ep, &req->req);
  66. spin_lock(&udc->lock);
  67. }
  68. }
  69. /* caller must hold lock */
  70. static void stop_activity(struct vudc *udc)
  71. {
  72. int i;
  73. struct urbp *urb_p, *tmp;
  74. udc->address = 0;
  75. for (i = 0; i < VIRTUAL_ENDPOINTS; i++)
  76. nuke(udc, &udc->ep[i]);
  77. list_for_each_entry_safe(urb_p, tmp, &udc->urb_queue, urb_entry) {
  78. list_del(&urb_p->urb_entry);
  79. free_urbp_and_urb(urb_p);
  80. }
  81. }
  82. struct vep *vudc_find_endpoint(struct vudc *udc, u8 address)
  83. {
  84. int i;
  85. if ((address & ~USB_DIR_IN) == 0)
  86. return &udc->ep[0];
  87. for (i = 1; i < VIRTUAL_ENDPOINTS; i++) {
  88. struct vep *ep = &udc->ep[i];
  89. if (!ep->desc)
  90. continue;
  91. if (ep->desc->bEndpointAddress == address)
  92. return ep;
  93. }
  94. return NULL;
  95. }
  96. /* gadget ops */
  97. static int vgadget_get_frame(struct usb_gadget *_gadget)
  98. {
  99. struct timespec64 now;
  100. struct vudc *udc = usb_gadget_to_vudc(_gadget);
  101. ktime_get_ts64(&now);
  102. return ((now.tv_sec - udc->start_time.tv_sec) * 1000 +
  103. (now.tv_nsec - udc->start_time.tv_nsec) / NSEC_PER_MSEC)
  104. & 0x7FF;
  105. }
  106. static int vgadget_set_selfpowered(struct usb_gadget *_gadget, int value)
  107. {
  108. struct vudc *udc = usb_gadget_to_vudc(_gadget);
  109. if (value)
  110. udc->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
  111. else
  112. udc->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
  113. return 0;
  114. }
  115. static int vgadget_pullup(struct usb_gadget *_gadget, int value)
  116. {
  117. struct vudc *udc = usb_gadget_to_vudc(_gadget);
  118. unsigned long flags;
  119. int ret;
  120. spin_lock_irqsave(&udc->lock, flags);
  121. value = !!value;
  122. if (value == udc->pullup)
  123. goto unlock;
  124. udc->pullup = value;
  125. if (value) {
  126. udc->gadget.speed = min_t(u8, USB_SPEED_HIGH,
  127. udc->driver->max_speed);
  128. udc->ep[0].ep.maxpacket = 64;
  129. /*
  130. * This is the first place where we can ask our
  131. * gadget driver for descriptors.
  132. */
  133. ret = get_gadget_descs(udc);
  134. if (ret) {
  135. dev_err(&udc->gadget.dev, "Unable go get desc: %d", ret);
  136. goto unlock;
  137. }
  138. spin_unlock_irqrestore(&udc->lock, flags);
  139. usbip_start_eh(&udc->ud);
  140. } else {
  141. /* Invalidate descriptors */
  142. udc->desc_cached = 0;
  143. spin_unlock_irqrestore(&udc->lock, flags);
  144. usbip_event_add(&udc->ud, VUDC_EVENT_REMOVED);
  145. usbip_stop_eh(&udc->ud); /* Wait for eh completion */
  146. }
  147. return 0;
  148. unlock:
  149. spin_unlock_irqrestore(&udc->lock, flags);
  150. return 0;
  151. }
  152. static int vgadget_udc_start(struct usb_gadget *g,
  153. struct usb_gadget_driver *driver)
  154. {
  155. struct vudc *udc = usb_gadget_to_vudc(g);
  156. unsigned long flags;
  157. spin_lock_irqsave(&udc->lock, flags);
  158. udc->driver = driver;
  159. udc->pullup = udc->connected = udc->desc_cached = 0;
  160. spin_unlock_irqrestore(&udc->lock, flags);
  161. return 0;
  162. }
  163. static int vgadget_udc_stop(struct usb_gadget *g)
  164. {
  165. struct vudc *udc = usb_gadget_to_vudc(g);
  166. unsigned long flags;
  167. spin_lock_irqsave(&udc->lock, flags);
  168. udc->driver = NULL;
  169. spin_unlock_irqrestore(&udc->lock, flags);
  170. return 0;
  171. }
  172. static const struct usb_gadget_ops vgadget_ops = {
  173. .get_frame = vgadget_get_frame,
  174. .set_selfpowered = vgadget_set_selfpowered,
  175. .pullup = vgadget_pullup,
  176. .udc_start = vgadget_udc_start,
  177. .udc_stop = vgadget_udc_stop,
  178. };
  179. /* endpoint ops */
  180. static int vep_enable(struct usb_ep *_ep,
  181. const struct usb_endpoint_descriptor *desc)
  182. {
  183. struct vep *ep;
  184. struct vudc *udc;
  185. unsigned int maxp;
  186. unsigned long flags;
  187. ep = to_vep(_ep);
  188. udc = ep_to_vudc(ep);
  189. if (!_ep || !desc || ep->desc || _ep->caps.type_control
  190. || desc->bDescriptorType != USB_DT_ENDPOINT)
  191. return -EINVAL;
  192. if (!udc->driver)
  193. return -ESHUTDOWN;
  194. spin_lock_irqsave(&udc->lock, flags);
  195. maxp = usb_endpoint_maxp(desc);
  196. _ep->maxpacket = maxp;
  197. ep->desc = desc;
  198. ep->type = usb_endpoint_type(desc);
  199. ep->halted = ep->wedged = 0;
  200. spin_unlock_irqrestore(&udc->lock, flags);
  201. return 0;
  202. }
  203. static int vep_disable(struct usb_ep *_ep)
  204. {
  205. struct vep *ep;
  206. struct vudc *udc;
  207. unsigned long flags;
  208. ep = to_vep(_ep);
  209. udc = ep_to_vudc(ep);
  210. if (!_ep || !ep->desc || _ep->caps.type_control)
  211. return -EINVAL;
  212. spin_lock_irqsave(&udc->lock, flags);
  213. ep->desc = NULL;
  214. nuke(udc, ep);
  215. spin_unlock_irqrestore(&udc->lock, flags);
  216. return 0;
  217. }
  218. static struct usb_request *vep_alloc_request(struct usb_ep *_ep,
  219. gfp_t mem_flags)
  220. {
  221. struct vrequest *req;
  222. if (!_ep)
  223. return NULL;
  224. req = kzalloc(sizeof(*req), mem_flags);
  225. if (!req)
  226. return NULL;
  227. INIT_LIST_HEAD(&req->req_entry);
  228. return &req->req;
  229. }
  230. static void vep_free_request(struct usb_ep *_ep, struct usb_request *_req)
  231. {
  232. struct vrequest *req;
  233. if (WARN_ON(!_ep || !_req))
  234. return;
  235. req = to_vrequest(_req);
  236. kfree(req);
  237. }
  238. static int vep_queue(struct usb_ep *_ep, struct usb_request *_req,
  239. gfp_t mem_flags)
  240. {
  241. struct vep *ep;
  242. struct vrequest *req;
  243. struct vudc *udc;
  244. unsigned long flags;
  245. if (!_ep || !_req)
  246. return -EINVAL;
  247. ep = to_vep(_ep);
  248. req = to_vrequest(_req);
  249. udc = ep_to_vudc(ep);
  250. spin_lock_irqsave(&udc->lock, flags);
  251. _req->actual = 0;
  252. _req->status = -EINPROGRESS;
  253. list_add_tail(&req->req_entry, &ep->req_queue);
  254. spin_unlock_irqrestore(&udc->lock, flags);
  255. return 0;
  256. }
  257. static int vep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  258. {
  259. struct vep *ep;
  260. struct vrequest *req;
  261. struct vudc *udc;
  262. struct vrequest *lst;
  263. unsigned long flags;
  264. int ret = -EINVAL;
  265. if (!_ep || !_req)
  266. return ret;
  267. ep = to_vep(_ep);
  268. req = to_vrequest(_req);
  269. udc = req->udc;
  270. if (!udc->driver)
  271. return -ESHUTDOWN;
  272. spin_lock_irqsave(&udc->lock, flags);
  273. list_for_each_entry(lst, &ep->req_queue, req_entry) {
  274. if (&lst->req == _req) {
  275. list_del_init(&lst->req_entry);
  276. _req->status = -ECONNRESET;
  277. ret = 0;
  278. break;
  279. }
  280. }
  281. spin_unlock_irqrestore(&udc->lock, flags);
  282. if (ret == 0)
  283. usb_gadget_giveback_request(_ep, _req);
  284. return ret;
  285. }
  286. static int
  287. vep_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
  288. {
  289. struct vep *ep;
  290. struct vudc *udc;
  291. unsigned long flags;
  292. int ret = 0;
  293. ep = to_vep(_ep);
  294. if (!_ep)
  295. return -EINVAL;
  296. udc = ep_to_vudc(ep);
  297. if (!udc->driver)
  298. return -ESHUTDOWN;
  299. spin_lock_irqsave(&udc->lock, flags);
  300. if (!value)
  301. ep->halted = ep->wedged = 0;
  302. else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
  303. !list_empty(&ep->req_queue))
  304. ret = -EAGAIN;
  305. else {
  306. ep->halted = 1;
  307. if (wedged)
  308. ep->wedged = 1;
  309. }
  310. spin_unlock_irqrestore(&udc->lock, flags);
  311. return ret;
  312. }
  313. static int
  314. vep_set_halt(struct usb_ep *_ep, int value)
  315. {
  316. return vep_set_halt_and_wedge(_ep, value, 0);
  317. }
  318. static int vep_set_wedge(struct usb_ep *_ep)
  319. {
  320. return vep_set_halt_and_wedge(_ep, 1, 1);
  321. }
  322. static const struct usb_ep_ops vep_ops = {
  323. .enable = vep_enable,
  324. .disable = vep_disable,
  325. .alloc_request = vep_alloc_request,
  326. .free_request = vep_free_request,
  327. .queue = vep_queue,
  328. .dequeue = vep_dequeue,
  329. .set_halt = vep_set_halt,
  330. .set_wedge = vep_set_wedge,
  331. };
  332. /* shutdown / reset / error handlers */
  333. static void vudc_shutdown(struct usbip_device *ud)
  334. {
  335. struct vudc *udc = container_of(ud, struct vudc, ud);
  336. int call_disconnect = 0;
  337. unsigned long flags;
  338. dev_dbg(&udc->pdev->dev, "device shutdown");
  339. if (ud->tcp_socket)
  340. kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
  341. if (ud->tcp_rx) {
  342. kthread_stop_put(ud->tcp_rx);
  343. ud->tcp_rx = NULL;
  344. }
  345. if (ud->tcp_tx) {
  346. kthread_stop_put(ud->tcp_tx);
  347. ud->tcp_tx = NULL;
  348. }
  349. if (ud->tcp_socket) {
  350. sockfd_put(ud->tcp_socket);
  351. ud->tcp_socket = NULL;
  352. }
  353. spin_lock_irqsave(&udc->lock, flags);
  354. stop_activity(udc);
  355. if (udc->connected && udc->driver->disconnect)
  356. call_disconnect = 1;
  357. udc->connected = 0;
  358. spin_unlock_irqrestore(&udc->lock, flags);
  359. if (call_disconnect)
  360. udc->driver->disconnect(&udc->gadget);
  361. }
  362. static void vudc_device_reset(struct usbip_device *ud)
  363. {
  364. struct vudc *udc = container_of(ud, struct vudc, ud);
  365. unsigned long flags;
  366. dev_dbg(&udc->pdev->dev, "device reset");
  367. spin_lock_irqsave(&udc->lock, flags);
  368. stop_activity(udc);
  369. spin_unlock_irqrestore(&udc->lock, flags);
  370. if (udc->driver)
  371. usb_gadget_udc_reset(&udc->gadget, udc->driver);
  372. spin_lock_irqsave(&ud->lock, flags);
  373. ud->status = SDEV_ST_AVAILABLE;
  374. spin_unlock_irqrestore(&ud->lock, flags);
  375. }
  376. static void vudc_device_unusable(struct usbip_device *ud)
  377. {
  378. unsigned long flags;
  379. spin_lock_irqsave(&ud->lock, flags);
  380. ud->status = SDEV_ST_ERROR;
  381. spin_unlock_irqrestore(&ud->lock, flags);
  382. }
  383. /* device setup / cleanup */
  384. struct vudc_device *alloc_vudc_device(int devid)
  385. {
  386. struct vudc_device *udc_dev = NULL;
  387. udc_dev = kzalloc(sizeof(*udc_dev), GFP_KERNEL);
  388. if (!udc_dev)
  389. goto out;
  390. INIT_LIST_HEAD(&udc_dev->dev_entry);
  391. udc_dev->pdev = platform_device_alloc(GADGET_NAME, devid);
  392. if (!udc_dev->pdev) {
  393. kfree(udc_dev);
  394. udc_dev = NULL;
  395. }
  396. out:
  397. return udc_dev;
  398. }
  399. void put_vudc_device(struct vudc_device *udc_dev)
  400. {
  401. platform_device_put(udc_dev->pdev);
  402. kfree(udc_dev);
  403. }
  404. static int init_vudc_hw(struct vudc *udc)
  405. {
  406. int i;
  407. struct usbip_device *ud = &udc->ud;
  408. struct vep *ep;
  409. udc->ep = kcalloc(VIRTUAL_ENDPOINTS, sizeof(*udc->ep), GFP_KERNEL);
  410. if (!udc->ep)
  411. goto nomem_ep;
  412. INIT_LIST_HEAD(&udc->gadget.ep_list);
  413. /* create ep0 and 15 in, 15 out general purpose eps */
  414. for (i = 0; i < VIRTUAL_ENDPOINTS; ++i) {
  415. int is_out = i % 2;
  416. int num = (i + 1) / 2;
  417. ep = &udc->ep[i];
  418. sprintf(ep->name, "ep%d%s", num,
  419. i ? (is_out ? "out" : "in") : "");
  420. ep->ep.name = ep->name;
  421. ep->ep.ops = &vep_ops;
  422. usb_ep_set_maxpacket_limit(&ep->ep, ~0);
  423. ep->ep.max_streams = 16;
  424. ep->gadget = &udc->gadget;
  425. INIT_LIST_HEAD(&ep->req_queue);
  426. if (i == 0) {
  427. /* ep0 */
  428. ep->ep.caps.type_control = true;
  429. ep->ep.caps.dir_out = true;
  430. ep->ep.caps.dir_in = true;
  431. udc->gadget.ep0 = &ep->ep;
  432. } else {
  433. /* All other eps */
  434. ep->ep.caps.type_iso = true;
  435. ep->ep.caps.type_int = true;
  436. ep->ep.caps.type_bulk = true;
  437. if (is_out)
  438. ep->ep.caps.dir_out = true;
  439. else
  440. ep->ep.caps.dir_in = true;
  441. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  442. }
  443. }
  444. spin_lock_init(&udc->lock);
  445. spin_lock_init(&udc->lock_tx);
  446. INIT_LIST_HEAD(&udc->urb_queue);
  447. INIT_LIST_HEAD(&udc->tx_queue);
  448. init_waitqueue_head(&udc->tx_waitq);
  449. spin_lock_init(&ud->lock);
  450. ud->status = SDEV_ST_AVAILABLE;
  451. ud->side = USBIP_VUDC;
  452. ud->eh_ops.shutdown = vudc_shutdown;
  453. ud->eh_ops.reset = vudc_device_reset;
  454. ud->eh_ops.unusable = vudc_device_unusable;
  455. v_init_timer(udc);
  456. return 0;
  457. nomem_ep:
  458. return -ENOMEM;
  459. }
  460. static void cleanup_vudc_hw(struct vudc *udc)
  461. {
  462. kfree(udc->ep);
  463. }
  464. /* platform driver ops */
  465. int vudc_probe(struct platform_device *pdev)
  466. {
  467. struct vudc *udc;
  468. int ret = -ENOMEM;
  469. udc = kzalloc(sizeof(*udc), GFP_KERNEL);
  470. if (!udc)
  471. goto out;
  472. udc->gadget.name = GADGET_NAME;
  473. udc->gadget.ops = &vgadget_ops;
  474. udc->gadget.max_speed = USB_SPEED_HIGH;
  475. udc->gadget.dev.parent = &pdev->dev;
  476. udc->pdev = pdev;
  477. ret = init_vudc_hw(udc);
  478. if (ret)
  479. goto err_init_vudc_hw;
  480. ret = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
  481. if (ret < 0)
  482. goto err_add_udc;
  483. ret = sysfs_create_group(&pdev->dev.kobj, &vudc_attr_group);
  484. if (ret) {
  485. dev_err(&udc->pdev->dev, "create sysfs files\n");
  486. goto err_sysfs;
  487. }
  488. platform_set_drvdata(pdev, udc);
  489. return ret;
  490. err_sysfs:
  491. usb_del_gadget_udc(&udc->gadget);
  492. err_add_udc:
  493. cleanup_vudc_hw(udc);
  494. err_init_vudc_hw:
  495. kfree(udc);
  496. out:
  497. return ret;
  498. }
  499. int vudc_remove(struct platform_device *pdev)
  500. {
  501. struct vudc *udc = platform_get_drvdata(pdev);
  502. sysfs_remove_group(&pdev->dev.kobj, &vudc_attr_group);
  503. usb_del_gadget_udc(&udc->gadget);
  504. cleanup_vudc_hw(udc);
  505. kfree(udc);
  506. return 0;
  507. }