stub_tx.c 10 KB

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