vudc_tx.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Karol Kosik <karo9@interia.eu>
  4. * Copyright (C) 2015-2016 Samsung Electronics
  5. * Igor Kotrasinski <i.kotrasinsk@samsung.com>
  6. */
  7. #include <net/sock.h>
  8. #include <linux/list.h>
  9. #include <linux/kthread.h>
  10. #include "usbip_common.h"
  11. #include "vudc.h"
  12. static inline void setup_base_pdu(struct usbip_header_basic *base,
  13. __u32 command, __u32 seqnum)
  14. {
  15. base->command = command;
  16. base->seqnum = seqnum;
  17. base->devid = 0;
  18. base->ep = 0;
  19. base->direction = 0;
  20. }
  21. static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urbp *urb_p)
  22. {
  23. setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, urb_p->seqnum);
  24. usbip_pack_pdu(rpdu, urb_p->urb, USBIP_RET_SUBMIT, 1);
  25. }
  26. static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
  27. struct v_unlink *unlink)
  28. {
  29. setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
  30. rpdu->u.ret_unlink.status = unlink->status;
  31. }
  32. static int v_send_ret_unlink(struct vudc *udc, struct v_unlink *unlink)
  33. {
  34. struct msghdr msg;
  35. struct kvec iov[1];
  36. size_t txsize;
  37. int ret;
  38. struct usbip_header pdu_header;
  39. txsize = 0;
  40. memset(&pdu_header, 0, sizeof(pdu_header));
  41. memset(&msg, 0, sizeof(msg));
  42. memset(&iov, 0, sizeof(iov));
  43. /* 1. setup usbip_header */
  44. setup_ret_unlink_pdu(&pdu_header, unlink);
  45. usbip_header_correct_endian(&pdu_header, 1);
  46. iov[0].iov_base = &pdu_header;
  47. iov[0].iov_len = sizeof(pdu_header);
  48. txsize += sizeof(pdu_header);
  49. ret = kernel_sendmsg(udc->ud.tcp_socket, &msg, iov,
  50. 1, txsize);
  51. if (ret != txsize) {
  52. usbip_event_add(&udc->ud, VUDC_EVENT_ERROR_TCP);
  53. if (ret >= 0)
  54. return -EPIPE;
  55. return ret;
  56. }
  57. kfree(unlink);
  58. return txsize;
  59. }
  60. static int v_send_ret_submit(struct vudc *udc, struct urbp *urb_p)
  61. {
  62. struct urb *urb = urb_p->urb;
  63. struct usbip_header pdu_header;
  64. struct usbip_iso_packet_descriptor *iso_buffer = NULL;
  65. struct kvec *iov = NULL;
  66. int iovnum = 0;
  67. int ret = 0;
  68. size_t txsize;
  69. struct msghdr msg;
  70. txsize = 0;
  71. memset(&pdu_header, 0, sizeof(pdu_header));
  72. memset(&msg, 0, sizeof(msg));
  73. if (urb_p->type == USB_ENDPOINT_XFER_ISOC)
  74. iovnum = 2 + urb->number_of_packets;
  75. else
  76. iovnum = 2;
  77. iov = kcalloc(iovnum, sizeof(*iov), GFP_KERNEL);
  78. if (!iov) {
  79. usbip_event_add(&udc->ud, VUDC_EVENT_ERROR_MALLOC);
  80. ret = -ENOMEM;
  81. goto out;
  82. }
  83. iovnum = 0;
  84. /* 1. setup usbip_header */
  85. setup_ret_submit_pdu(&pdu_header, urb_p);
  86. usbip_dbg_stub_tx("setup txdata seqnum: %d urb: %p\n",
  87. pdu_header.base.seqnum, urb);
  88. usbip_header_correct_endian(&pdu_header, 1);
  89. iov[iovnum].iov_base = &pdu_header;
  90. iov[iovnum].iov_len = sizeof(pdu_header);
  91. iovnum++;
  92. txsize += sizeof(pdu_header);
  93. /* 2. setup transfer buffer */
  94. if (urb_p->type != USB_ENDPOINT_XFER_ISOC &&
  95. usb_pipein(urb->pipe) && urb->actual_length > 0) {
  96. iov[iovnum].iov_base = urb->transfer_buffer;
  97. iov[iovnum].iov_len = urb->actual_length;
  98. iovnum++;
  99. txsize += urb->actual_length;
  100. } else if (urb_p->type == USB_ENDPOINT_XFER_ISOC &&
  101. usb_pipein(urb->pipe)) {
  102. /* FIXME - copypasted from stub_tx, refactor */
  103. int i;
  104. for (i = 0; i < urb->number_of_packets; i++) {
  105. iov[iovnum].iov_base = urb->transfer_buffer +
  106. urb->iso_frame_desc[i].offset;
  107. iov[iovnum].iov_len =
  108. urb->iso_frame_desc[i].actual_length;
  109. iovnum++;
  110. txsize += urb->iso_frame_desc[i].actual_length;
  111. }
  112. if (txsize != sizeof(pdu_header) + urb->actual_length) {
  113. usbip_event_add(&udc->ud, VUDC_EVENT_ERROR_TCP);
  114. ret = -EPIPE;
  115. goto out;
  116. }
  117. }
  118. /* else - no buffer to send */
  119. /* 3. setup iso_packet_descriptor */
  120. if (urb_p->type == USB_ENDPOINT_XFER_ISOC) {
  121. ssize_t len = 0;
  122. iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
  123. if (!iso_buffer) {
  124. usbip_event_add(&udc->ud,
  125. VUDC_EVENT_ERROR_MALLOC);
  126. ret = -ENOMEM;
  127. goto out;
  128. }
  129. iov[iovnum].iov_base = iso_buffer;
  130. iov[iovnum].iov_len = len;
  131. txsize += len;
  132. iovnum++;
  133. }
  134. ret = kernel_sendmsg(udc->ud.tcp_socket, &msg,
  135. iov, iovnum, txsize);
  136. if (ret != txsize) {
  137. usbip_event_add(&udc->ud, VUDC_EVENT_ERROR_TCP);
  138. if (ret >= 0)
  139. ret = -EPIPE;
  140. goto out;
  141. }
  142. out:
  143. kfree(iov);
  144. kfree(iso_buffer);
  145. free_urbp_and_urb(urb_p);
  146. if (ret < 0)
  147. return ret;
  148. return txsize;
  149. }
  150. static int v_send_ret(struct vudc *udc)
  151. {
  152. unsigned long flags;
  153. struct tx_item *txi;
  154. size_t total_size = 0;
  155. int ret = 0;
  156. spin_lock_irqsave(&udc->lock_tx, flags);
  157. while (!list_empty(&udc->tx_queue)) {
  158. txi = list_first_entry(&udc->tx_queue, struct tx_item,
  159. tx_entry);
  160. list_del(&txi->tx_entry);
  161. spin_unlock_irqrestore(&udc->lock_tx, flags);
  162. switch (txi->type) {
  163. case TX_SUBMIT:
  164. ret = v_send_ret_submit(udc, txi->s);
  165. break;
  166. case TX_UNLINK:
  167. ret = v_send_ret_unlink(udc, txi->u);
  168. break;
  169. }
  170. kfree(txi);
  171. if (ret < 0)
  172. return ret;
  173. total_size += ret;
  174. spin_lock_irqsave(&udc->lock_tx, flags);
  175. }
  176. spin_unlock_irqrestore(&udc->lock_tx, flags);
  177. return total_size;
  178. }
  179. int v_tx_loop(void *data)
  180. {
  181. struct usbip_device *ud = (struct usbip_device *) data;
  182. struct vudc *udc = container_of(ud, struct vudc, ud);
  183. int ret;
  184. while (!kthread_should_stop()) {
  185. if (usbip_event_happened(&udc->ud))
  186. break;
  187. ret = v_send_ret(udc);
  188. if (ret < 0) {
  189. pr_warn("v_tx exit with error %d", ret);
  190. break;
  191. }
  192. wait_event_interruptible(udc->tx_waitq,
  193. (!list_empty(&udc->tx_queue) ||
  194. kthread_should_stop()));
  195. }
  196. return 0;
  197. }
  198. /* called with spinlocks held */
  199. void v_enqueue_ret_unlink(struct vudc *udc, __u32 seqnum, __u32 status)
  200. {
  201. struct tx_item *txi;
  202. struct v_unlink *unlink;
  203. txi = kzalloc(sizeof(*txi), GFP_ATOMIC);
  204. if (!txi) {
  205. usbip_event_add(&udc->ud, VDEV_EVENT_ERROR_MALLOC);
  206. return;
  207. }
  208. unlink = kzalloc(sizeof(*unlink), GFP_ATOMIC);
  209. if (!unlink) {
  210. kfree(txi);
  211. usbip_event_add(&udc->ud, VDEV_EVENT_ERROR_MALLOC);
  212. return;
  213. }
  214. unlink->seqnum = seqnum;
  215. unlink->status = status;
  216. txi->type = TX_UNLINK;
  217. txi->u = unlink;
  218. list_add_tail(&txi->tx_entry, &udc->tx_queue);
  219. }
  220. /* called with spinlocks held */
  221. void v_enqueue_ret_submit(struct vudc *udc, struct urbp *urb_p)
  222. {
  223. struct tx_item *txi;
  224. txi = kzalloc(sizeof(*txi), GFP_ATOMIC);
  225. if (!txi) {
  226. usbip_event_add(&udc->ud, VDEV_EVENT_ERROR_MALLOC);
  227. return;
  228. }
  229. txi->type = TX_SUBMIT;
  230. txi->s = urb_p;
  231. list_add_tail(&txi->tx_entry, &udc->tx_queue);
  232. }