PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/staging/usbip/stub_tx.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 400 lines | 272 code | 71 blank | 57 comment | 29 complexity | f307b2d4032eda8fea1f79352da1c299 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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. dev_err(&sdev->interface->dev, "alloc stub_unlink\n");
  40. usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
  41. return;
  42. }
  43. unlink->seqnum = seqnum;
  44. unlink->status = status;
  45. list_add_tail(&unlink->list, &sdev->unlink_tx);
  46. }
  47. /**
  48. * stub_complete - completion handler of a usbip urb
  49. * @urb: pointer to the urb completed
  50. *
  51. * When a urb has completed, the USB core driver calls this function mostly in
  52. * the interrupt context. To return the result of a urb, the completed urb is
  53. * linked to the pending list of returning.
  54. *
  55. */
  56. void stub_complete(struct urb *urb)
  57. {
  58. struct stub_priv *priv = (struct stub_priv *) urb->context;
  59. struct stub_device *sdev = priv->sdev;
  60. unsigned long flags;
  61. usbip_dbg_stub_tx("complete! status %d\n", urb->status);
  62. switch (urb->status) {
  63. case 0:
  64. /* OK */
  65. break;
  66. case -ENOENT:
  67. dev_info(&urb->dev->dev, "stopped by a call to usb_kill_urb() "
  68. "because of cleaning up a virtual connection\n");
  69. return;
  70. case -ECONNRESET:
  71. dev_info(&urb->dev->dev, "unlinked by a call to "
  72. "usb_unlink_urb()\n");
  73. break;
  74. case -EPIPE:
  75. dev_info(&urb->dev->dev, "endpoint %d is stalled\n",
  76. usb_pipeendpoint(urb->pipe));
  77. break;
  78. case -ESHUTDOWN:
  79. dev_info(&urb->dev->dev, "device removed?\n");
  80. break;
  81. default:
  82. dev_info(&urb->dev->dev, "urb completion with non-zero status "
  83. "%d\n", 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. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  94. /* wake up tx_thread */
  95. wake_up(&sdev->tx_waitq);
  96. }
  97. static inline void setup_base_pdu(struct usbip_header_basic *base,
  98. __u32 command, __u32 seqnum)
  99. {
  100. base->command = command;
  101. base->seqnum = seqnum;
  102. base->devid = 0;
  103. base->ep = 0;
  104. base->direction = 0;
  105. }
  106. static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
  107. {
  108. struct stub_priv *priv = (struct stub_priv *) urb->context;
  109. setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
  110. usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
  111. }
  112. static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
  113. struct stub_unlink *unlink)
  114. {
  115. setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
  116. rpdu->u.ret_unlink.status = unlink->status;
  117. }
  118. static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
  119. {
  120. unsigned long flags;
  121. struct stub_priv *priv, *tmp;
  122. spin_lock_irqsave(&sdev->priv_lock, flags);
  123. list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
  124. list_move_tail(&priv->list, &sdev->priv_free);
  125. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  126. return priv;
  127. }
  128. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  129. return NULL;
  130. }
  131. static int stub_send_ret_submit(struct stub_device *sdev)
  132. {
  133. unsigned long flags;
  134. struct stub_priv *priv, *tmp;
  135. struct msghdr msg;
  136. size_t txsize;
  137. size_t total_size = 0;
  138. while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
  139. int ret;
  140. struct urb *urb = priv->urb;
  141. struct usbip_header pdu_header;
  142. void *iso_buffer = NULL;
  143. struct kvec *iov = NULL;
  144. int iovnum = 0;
  145. txsize = 0;
  146. memset(&pdu_header, 0, sizeof(pdu_header));
  147. memset(&msg, 0, sizeof(msg));
  148. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  149. iovnum = 2 + urb->number_of_packets;
  150. else
  151. iovnum = 2;
  152. iov = kzalloc(iovnum * sizeof(struct kvec), GFP_KERNEL);
  153. if (!iov) {
  154. usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_MALLOC);
  155. return -1;
  156. }
  157. iovnum = 0;
  158. /* 1. setup usbip_header */
  159. setup_ret_submit_pdu(&pdu_header, urb);
  160. usbip_dbg_stub_tx("setup txdata seqnum: %d urb: %p\n",
  161. pdu_header.base.seqnum, urb);
  162. /*usbip_dump_header(pdu_header);*/
  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 "
  197. "match iso packet sizes %lu\n",
  198. urb->actual_length,
  199. txsize-sizeof(pdu_header));
  200. kfree(iov);
  201. usbip_event_add(&sdev->ud,
  202. SDEV_EVENT_ERROR_TCP);
  203. return -1;
  204. }
  205. }
  206. /* 3. setup iso_packet_descriptor */
  207. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  208. ssize_t len = 0;
  209. iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
  210. if (!iso_buffer) {
  211. usbip_event_add(&sdev->ud,
  212. SDEV_EVENT_ERROR_MALLOC);
  213. kfree(iov);
  214. return -1;
  215. }
  216. iov[iovnum].iov_base = iso_buffer;
  217. iov[iovnum].iov_len = len;
  218. txsize += len;
  219. iovnum++;
  220. }
  221. ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg,
  222. iov, iovnum, txsize);
  223. if (ret != txsize) {
  224. dev_err(&sdev->interface->dev,
  225. "sendmsg failed!, retval %d for %zd\n",
  226. ret, txsize);
  227. kfree(iov);
  228. kfree(iso_buffer);
  229. usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
  230. return -1;
  231. }
  232. kfree(iov);
  233. kfree(iso_buffer);
  234. total_size += txsize;
  235. }
  236. spin_lock_irqsave(&sdev->priv_lock, flags);
  237. list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
  238. stub_free_priv_and_urb(priv);
  239. }
  240. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  241. return total_size;
  242. }
  243. static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
  244. {
  245. unsigned long flags;
  246. struct stub_unlink *unlink, *tmp;
  247. spin_lock_irqsave(&sdev->priv_lock, flags);
  248. list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
  249. list_move_tail(&unlink->list, &sdev->unlink_free);
  250. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  251. return unlink;
  252. }
  253. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  254. return NULL;
  255. }
  256. static int stub_send_ret_unlink(struct stub_device *sdev)
  257. {
  258. unsigned long flags;
  259. struct stub_unlink *unlink, *tmp;
  260. struct msghdr msg;
  261. struct kvec iov[1];
  262. size_t txsize;
  263. size_t total_size = 0;
  264. while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
  265. int ret;
  266. struct usbip_header pdu_header;
  267. txsize = 0;
  268. memset(&pdu_header, 0, sizeof(pdu_header));
  269. memset(&msg, 0, sizeof(msg));
  270. memset(&iov, 0, sizeof(iov));
  271. usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
  272. /* 1. setup usbip_header */
  273. setup_ret_unlink_pdu(&pdu_header, unlink);
  274. usbip_header_correct_endian(&pdu_header, 1);
  275. iov[0].iov_base = &pdu_header;
  276. iov[0].iov_len = sizeof(pdu_header);
  277. txsize += sizeof(pdu_header);
  278. ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
  279. 1, txsize);
  280. if (ret != txsize) {
  281. dev_err(&sdev->interface->dev,
  282. "sendmsg failed!, retval %d for %zd\n",
  283. ret, txsize);
  284. usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
  285. return -1;
  286. }
  287. usbip_dbg_stub_tx("send txdata\n");
  288. total_size += txsize;
  289. }
  290. spin_lock_irqsave(&sdev->priv_lock, flags);
  291. list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
  292. list_del(&unlink->list);
  293. kfree(unlink);
  294. }
  295. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  296. return total_size;
  297. }
  298. int stub_tx_loop(void *data)
  299. {
  300. struct usbip_device *ud = data;
  301. struct stub_device *sdev = container_of(ud, struct stub_device, ud);
  302. while (!kthread_should_stop()) {
  303. if (usbip_event_happened(ud))
  304. break;
  305. /*
  306. * send_ret_submit comes earlier than send_ret_unlink. stub_rx
  307. * looks at only priv_init queue. If the completion of a URB is
  308. * earlier than the receive of CMD_UNLINK, priv is moved to
  309. * priv_tx queue and stub_rx does not find the target priv. In
  310. * this case, vhci_rx receives the result of the submit request
  311. * and then receives the result of the unlink request. The
  312. * result of the submit is given back to the usbcore as the
  313. * completion of the unlink request. The request of the
  314. * unlink is ignored. This is ok because a driver who calls
  315. * usb_unlink_urb() understands the unlink was too late by
  316. * getting the status of the given-backed URB which has the
  317. * status of usb_submit_urb().
  318. */
  319. if (stub_send_ret_submit(sdev) < 0)
  320. break;
  321. if (stub_send_ret_unlink(sdev) < 0)
  322. break;
  323. wait_event_interruptible(sdev->tx_waitq,
  324. (!list_empty(&sdev->priv_tx) ||
  325. !list_empty(&sdev->unlink_tx) ||
  326. kthread_should_stop()));
  327. }
  328. return 0;
  329. }