vudc_tx.c 6.7 KB

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