stub_tx.c 10 KB

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