stub_tx.c 11 KB

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