stub_tx.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  4. */
  5. #include <linux/kthread.h>
  6. #include <linux/socket.h>
  7. #include "usbip_common.h"
  8. #include "stub.h"
  9. static void stub_free_priv_and_urb(struct stub_priv *priv)
  10. {
  11. struct urb *urb = priv->urb;
  12. kfree(urb->setup_packet);
  13. urb->setup_packet = NULL;
  14. kfree(urb->transfer_buffer);
  15. urb->transfer_buffer = NULL;
  16. list_del(&priv->list);
  17. kmem_cache_free(stub_priv_cache, priv);
  18. usb_free_urb(urb);
  19. }
  20. /* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
  21. void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
  22. __u32 status)
  23. {
  24. struct stub_unlink *unlink;
  25. unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC);
  26. if (!unlink) {
  27. usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
  28. return;
  29. }
  30. unlink->seqnum = seqnum;
  31. unlink->status = status;
  32. list_add_tail(&unlink->list, &sdev->unlink_tx);
  33. }
  34. /**
  35. * stub_complete - completion handler of a usbip urb
  36. * @urb: pointer to the urb completed
  37. *
  38. * When a urb has completed, the USB core driver calls this function mostly in
  39. * the interrupt context. To return the result of a urb, the completed urb is
  40. * linked to the pending list of returning.
  41. *
  42. */
  43. void stub_complete(struct urb *urb)
  44. {
  45. struct stub_priv *priv = (struct stub_priv *) urb->context;
  46. struct stub_device *sdev = priv->sdev;
  47. unsigned long flags;
  48. usbip_dbg_stub_tx("complete! status %d\n", urb->status);
  49. switch (urb->status) {
  50. case 0:
  51. /* OK */
  52. break;
  53. case -ENOENT:
  54. dev_info(&urb->dev->dev,
  55. "stopped by a call to usb_kill_urb() because of cleaning up a virtual connection\n");
  56. return;
  57. case -ECONNRESET:
  58. dev_info(&urb->dev->dev,
  59. "unlinked by a call to usb_unlink_urb()\n");
  60. break;
  61. case -EPIPE:
  62. dev_info(&urb->dev->dev, "endpoint %d is stalled\n",
  63. usb_pipeendpoint(urb->pipe));
  64. break;
  65. case -ESHUTDOWN:
  66. dev_info(&urb->dev->dev, "device removed?\n");
  67. break;
  68. default:
  69. dev_info(&urb->dev->dev,
  70. "urb completion with non-zero status %d\n",
  71. urb->status);
  72. break;
  73. }
  74. /* link a urb to the queue of tx. */
  75. spin_lock_irqsave(&sdev->priv_lock, flags);
  76. if (sdev->ud.tcp_socket == NULL) {
  77. usbip_dbg_stub_tx("ignore urb for closed connection\n");
  78. /* It will be freed in stub_device_cleanup_urbs(). */
  79. } else if (priv->unlinking) {
  80. stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
  81. stub_free_priv_and_urb(priv);
  82. } else {
  83. list_move_tail(&priv->list, &sdev->priv_tx);
  84. }
  85. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  86. /* wake up tx_thread */
  87. wake_up(&sdev->tx_waitq);
  88. }
  89. static inline void setup_base_pdu(struct usbip_header_basic *base,
  90. __u32 command, __u32 seqnum)
  91. {
  92. base->command = command;
  93. base->seqnum = seqnum;
  94. base->devid = 0;
  95. base->ep = 0;
  96. base->direction = 0;
  97. }
  98. static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
  99. {
  100. struct stub_priv *priv = (struct stub_priv *) urb->context;
  101. setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
  102. usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
  103. }
  104. static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
  105. struct stub_unlink *unlink)
  106. {
  107. setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
  108. rpdu->u.ret_unlink.status = unlink->status;
  109. }
  110. static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
  111. {
  112. unsigned long flags;
  113. struct stub_priv *priv, *tmp;
  114. spin_lock_irqsave(&sdev->priv_lock, flags);
  115. list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
  116. list_move_tail(&priv->list, &sdev->priv_free);
  117. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  118. return priv;
  119. }
  120. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  121. return NULL;
  122. }
  123. static int stub_send_ret_submit(struct stub_device *sdev)
  124. {
  125. unsigned long flags;
  126. struct stub_priv *priv, *tmp;
  127. struct msghdr msg;
  128. size_t txsize;
  129. size_t total_size = 0;
  130. while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
  131. int ret;
  132. struct urb *urb = priv->urb;
  133. struct usbip_header pdu_header;
  134. struct usbip_iso_packet_descriptor *iso_buffer = NULL;
  135. struct kvec *iov = NULL;
  136. int iovnum = 0;
  137. txsize = 0;
  138. memset(&pdu_header, 0, sizeof(pdu_header));
  139. memset(&msg, 0, sizeof(msg));
  140. if (urb->actual_length > 0 && !urb->transfer_buffer) {
  141. dev_err(&sdev->udev->dev,
  142. "urb: actual_length %d transfer_buffer null\n",
  143. urb->actual_length);
  144. return -1;
  145. }
  146. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  147. iovnum = 2 + urb->number_of_packets;
  148. else
  149. iovnum = 2;
  150. iov = kcalloc(iovnum, sizeof(struct kvec), GFP_KERNEL);
  151. if (!iov) {
  152. usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_MALLOC);
  153. return -1;
  154. }
  155. iovnum = 0;
  156. /* 1. setup usbip_header */
  157. setup_ret_submit_pdu(&pdu_header, urb);
  158. usbip_dbg_stub_tx("setup txdata seqnum: %d\n",
  159. pdu_header.base.seqnum);
  160. usbip_header_correct_endian(&pdu_header, 1);
  161. iov[iovnum].iov_base = &pdu_header;
  162. iov[iovnum].iov_len = sizeof(pdu_header);
  163. iovnum++;
  164. txsize += sizeof(pdu_header);
  165. /* 2. setup transfer buffer */
  166. if (usb_pipein(urb->pipe) &&
  167. usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS &&
  168. urb->actual_length > 0) {
  169. iov[iovnum].iov_base = urb->transfer_buffer;
  170. iov[iovnum].iov_len = urb->actual_length;
  171. iovnum++;
  172. txsize += urb->actual_length;
  173. } else if (usb_pipein(urb->pipe) &&
  174. usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  175. /*
  176. * For isochronous packets: actual length is the sum of
  177. * the actual length of the individual, packets, but as
  178. * the packet offsets are not changed there will be
  179. * padding between the packets. To optimally use the
  180. * bandwidth the padding is not transmitted.
  181. */
  182. int i;
  183. for (i = 0; i < urb->number_of_packets; i++) {
  184. iov[iovnum].iov_base = urb->transfer_buffer +
  185. urb->iso_frame_desc[i].offset;
  186. iov[iovnum].iov_len =
  187. urb->iso_frame_desc[i].actual_length;
  188. iovnum++;
  189. txsize += urb->iso_frame_desc[i].actual_length;
  190. }
  191. if (txsize != sizeof(pdu_header) + urb->actual_length) {
  192. dev_err(&sdev->udev->dev,
  193. "actual length of urb %d does not match iso packet sizes %zu\n",
  194. urb->actual_length,
  195. txsize-sizeof(pdu_header));
  196. kfree(iov);
  197. usbip_event_add(&sdev->ud,
  198. SDEV_EVENT_ERROR_TCP);
  199. return -1;
  200. }
  201. }
  202. /* 3. setup iso_packet_descriptor */
  203. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  204. ssize_t len = 0;
  205. iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
  206. if (!iso_buffer) {
  207. usbip_event_add(&sdev->ud,
  208. SDEV_EVENT_ERROR_MALLOC);
  209. kfree(iov);
  210. return -1;
  211. }
  212. iov[iovnum].iov_base = iso_buffer;
  213. iov[iovnum].iov_len = len;
  214. txsize += len;
  215. iovnum++;
  216. }
  217. ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg,
  218. iov, iovnum, txsize);
  219. if (ret != txsize) {
  220. dev_err(&sdev->udev->dev,
  221. "sendmsg failed!, retval %d for %zd\n",
  222. ret, txsize);
  223. kfree(iov);
  224. kfree(iso_buffer);
  225. usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
  226. return -1;
  227. }
  228. kfree(iov);
  229. kfree(iso_buffer);
  230. total_size += txsize;
  231. }
  232. spin_lock_irqsave(&sdev->priv_lock, flags);
  233. list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
  234. stub_free_priv_and_urb(priv);
  235. }
  236. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  237. return total_size;
  238. }
  239. static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
  240. {
  241. unsigned long flags;
  242. struct stub_unlink *unlink, *tmp;
  243. spin_lock_irqsave(&sdev->priv_lock, flags);
  244. list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
  245. list_move_tail(&unlink->list, &sdev->unlink_free);
  246. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  247. return unlink;
  248. }
  249. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  250. return NULL;
  251. }
  252. static int stub_send_ret_unlink(struct stub_device *sdev)
  253. {
  254. unsigned long flags;
  255. struct stub_unlink *unlink, *tmp;
  256. struct msghdr msg;
  257. struct kvec iov[1];
  258. size_t txsize;
  259. size_t total_size = 0;
  260. while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
  261. int ret;
  262. struct usbip_header pdu_header;
  263. txsize = 0;
  264. memset(&pdu_header, 0, sizeof(pdu_header));
  265. memset(&msg, 0, sizeof(msg));
  266. memset(&iov, 0, sizeof(iov));
  267. usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
  268. /* 1. setup usbip_header */
  269. setup_ret_unlink_pdu(&pdu_header, unlink);
  270. usbip_header_correct_endian(&pdu_header, 1);
  271. iov[0].iov_base = &pdu_header;
  272. iov[0].iov_len = sizeof(pdu_header);
  273. txsize += sizeof(pdu_header);
  274. ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
  275. 1, txsize);
  276. if (ret != txsize) {
  277. dev_err(&sdev->udev->dev,
  278. "sendmsg failed!, retval %d for %zd\n",
  279. ret, txsize);
  280. usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
  281. return -1;
  282. }
  283. usbip_dbg_stub_tx("send txdata\n");
  284. total_size += txsize;
  285. }
  286. spin_lock_irqsave(&sdev->priv_lock, flags);
  287. list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
  288. list_del(&unlink->list);
  289. kfree(unlink);
  290. }
  291. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  292. return total_size;
  293. }
  294. int stub_tx_loop(void *data)
  295. {
  296. struct usbip_device *ud = data;
  297. struct stub_device *sdev = container_of(ud, struct stub_device, ud);
  298. while (!kthread_should_stop()) {
  299. if (usbip_event_happened(ud))
  300. break;
  301. /*
  302. * send_ret_submit comes earlier than send_ret_unlink. stub_rx
  303. * looks at only priv_init queue. If the completion of a URB is
  304. * earlier than the receive of CMD_UNLINK, priv is moved to
  305. * priv_tx queue and stub_rx does not find the target priv. In
  306. * this case, vhci_rx receives the result of the submit request
  307. * and then receives the result of the unlink request. The
  308. * result of the submit is given back to the usbcore as the
  309. * completion of the unlink request. The request of the
  310. * unlink is ignored. This is ok because a driver who calls
  311. * usb_unlink_urb() understands the unlink was too late by
  312. * getting the status of the given-backed URB which has the
  313. * status of usb_submit_urb().
  314. */
  315. if (stub_send_ret_submit(sdev) < 0)
  316. break;
  317. if (stub_send_ret_unlink(sdev) < 0)
  318. break;
  319. wait_event_interruptible(sdev->tx_waitq,
  320. (!list_empty(&sdev->priv_tx) ||
  321. !list_empty(&sdev->unlink_tx) ||
  322. kthread_should_stop()));
  323. }
  324. return 0;
  325. }