vudc_rx.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 int alloc_urb_from_cmd(struct urb **urbp,
  25. struct usbip_header *pdu, u8 type)
  26. {
  27. struct urb *urb;
  28. if (type == USB_ENDPOINT_XFER_ISOC)
  29. urb = usb_alloc_urb(pdu->u.cmd_submit.number_of_packets,
  30. GFP_KERNEL);
  31. else
  32. urb = usb_alloc_urb(0, GFP_KERNEL);
  33. if (!urb)
  34. goto err;
  35. usbip_pack_pdu(pdu, urb, USBIP_CMD_SUBMIT, 0);
  36. if (urb->transfer_buffer_length > 0) {
  37. urb->transfer_buffer = kzalloc(urb->transfer_buffer_length,
  38. GFP_KERNEL);
  39. if (!urb->transfer_buffer)
  40. goto free_urb;
  41. }
  42. urb->setup_packet = kmemdup(&pdu->u.cmd_submit.setup, 8,
  43. GFP_KERNEL);
  44. if (!urb->setup_packet)
  45. goto free_buffer;
  46. /*
  47. * FIXME - we only setup pipe enough for usbip functions
  48. * to behave nicely
  49. */
  50. urb->pipe |= pdu->base.direction == USBIP_DIR_IN ?
  51. USB_DIR_IN : USB_DIR_OUT;
  52. *urbp = urb;
  53. return 0;
  54. free_buffer:
  55. kfree(urb->transfer_buffer);
  56. urb->transfer_buffer = NULL;
  57. free_urb:
  58. usb_free_urb(urb);
  59. err:
  60. return -ENOMEM;
  61. }
  62. static int v_recv_cmd_unlink(struct vudc *udc,
  63. struct usbip_header *pdu)
  64. {
  65. unsigned long flags;
  66. struct urbp *urb_p;
  67. spin_lock_irqsave(&udc->lock, flags);
  68. list_for_each_entry(urb_p, &udc->urb_queue, urb_entry) {
  69. if (urb_p->seqnum != pdu->u.cmd_unlink.seqnum)
  70. continue;
  71. urb_p->urb->unlinked = -ECONNRESET;
  72. urb_p->seqnum = pdu->base.seqnum;
  73. v_kick_timer(udc, jiffies);
  74. spin_unlock_irqrestore(&udc->lock, flags);
  75. return 0;
  76. }
  77. /* Not found, completed / not queued */
  78. spin_lock(&udc->lock_tx);
  79. v_enqueue_ret_unlink(udc, pdu->base.seqnum, 0);
  80. wake_up(&udc->tx_waitq);
  81. spin_unlock(&udc->lock_tx);
  82. spin_unlock_irqrestore(&udc->lock, flags);
  83. return 0;
  84. }
  85. static int v_recv_cmd_submit(struct vudc *udc,
  86. struct usbip_header *pdu)
  87. {
  88. int ret = 0;
  89. struct urbp *urb_p;
  90. u8 address;
  91. unsigned long flags;
  92. urb_p = alloc_urbp();
  93. if (!urb_p) {
  94. usbip_event_add(&udc->ud, VUDC_EVENT_ERROR_MALLOC);
  95. return -ENOMEM;
  96. }
  97. /* base.ep is pipeendpoint(pipe) */
  98. address = pdu->base.ep;
  99. if (pdu->base.direction == USBIP_DIR_IN)
  100. address |= USB_DIR_IN;
  101. spin_lock_irq(&udc->lock);
  102. urb_p->ep = vudc_find_endpoint(udc, address);
  103. if (!urb_p->ep) {
  104. /* we don't know the type, there may be isoc data! */
  105. dev_err(&udc->pdev->dev, "request to nonexistent endpoint");
  106. spin_unlock_irq(&udc->lock);
  107. usbip_event_add(&udc->ud, VUDC_EVENT_ERROR_TCP);
  108. ret = -EPIPE;
  109. goto free_urbp;
  110. }
  111. urb_p->type = urb_p->ep->type;
  112. spin_unlock_irq(&udc->lock);
  113. urb_p->new = 1;
  114. urb_p->seqnum = pdu->base.seqnum;
  115. if (urb_p->ep->type == USB_ENDPOINT_XFER_ISOC) {
  116. /* validate packet size and number of packets */
  117. unsigned int maxp, packets, bytes;
  118. maxp = usb_endpoint_maxp(urb_p->ep->desc);
  119. maxp *= usb_endpoint_maxp_mult(urb_p->ep->desc);
  120. bytes = pdu->u.cmd_submit.transfer_buffer_length;
  121. packets = DIV_ROUND_UP(bytes, maxp);
  122. if (pdu->u.cmd_submit.number_of_packets < 0 ||
  123. pdu->u.cmd_submit.number_of_packets > packets) {
  124. dev_err(&udc->gadget.dev,
  125. "CMD_SUBMIT: isoc invalid num packets %d\n",
  126. pdu->u.cmd_submit.number_of_packets);
  127. ret = -EMSGSIZE;
  128. goto free_urbp;
  129. }
  130. }
  131. ret = alloc_urb_from_cmd(&urb_p->urb, pdu, urb_p->ep->type);
  132. if (ret) {
  133. usbip_event_add(&udc->ud, VUDC_EVENT_ERROR_MALLOC);
  134. ret = -ENOMEM;
  135. goto free_urbp;
  136. }
  137. urb_p->urb->status = -EINPROGRESS;
  138. /* FIXME: more pipe setup to please usbip_common */
  139. urb_p->urb->pipe &= ~(3 << 30);
  140. switch (urb_p->ep->type) {
  141. case USB_ENDPOINT_XFER_BULK:
  142. urb_p->urb->pipe |= (PIPE_BULK << 30);
  143. break;
  144. case USB_ENDPOINT_XFER_INT:
  145. urb_p->urb->pipe |= (PIPE_INTERRUPT << 30);
  146. break;
  147. case USB_ENDPOINT_XFER_CONTROL:
  148. urb_p->urb->pipe |= (PIPE_CONTROL << 30);
  149. break;
  150. case USB_ENDPOINT_XFER_ISOC:
  151. urb_p->urb->pipe |= (PIPE_ISOCHRONOUS << 30);
  152. break;
  153. }
  154. ret = usbip_recv_xbuff(&udc->ud, urb_p->urb);
  155. if (ret < 0)
  156. goto free_urbp;
  157. ret = usbip_recv_iso(&udc->ud, urb_p->urb);
  158. if (ret < 0)
  159. goto free_urbp;
  160. spin_lock_irqsave(&udc->lock, flags);
  161. v_kick_timer(udc, jiffies);
  162. list_add_tail(&urb_p->urb_entry, &udc->urb_queue);
  163. spin_unlock_irqrestore(&udc->lock, flags);
  164. return 0;
  165. free_urbp:
  166. free_urbp_and_urb(urb_p);
  167. return ret;
  168. }
  169. static int v_rx_pdu(struct usbip_device *ud)
  170. {
  171. int ret;
  172. struct usbip_header pdu;
  173. struct vudc *udc = container_of(ud, struct vudc, ud);
  174. memset(&pdu, 0, sizeof(pdu));
  175. ret = usbip_recv(ud->tcp_socket, &pdu, sizeof(pdu));
  176. if (ret != sizeof(pdu)) {
  177. usbip_event_add(ud, VUDC_EVENT_ERROR_TCP);
  178. if (ret >= 0)
  179. return -EPIPE;
  180. return ret;
  181. }
  182. usbip_header_correct_endian(&pdu, 0);
  183. spin_lock_irq(&ud->lock);
  184. ret = (ud->status == SDEV_ST_USED);
  185. spin_unlock_irq(&ud->lock);
  186. if (!ret) {
  187. usbip_event_add(ud, VUDC_EVENT_ERROR_TCP);
  188. return -EBUSY;
  189. }
  190. switch (pdu.base.command) {
  191. case USBIP_CMD_UNLINK:
  192. ret = v_recv_cmd_unlink(udc, &pdu);
  193. break;
  194. case USBIP_CMD_SUBMIT:
  195. ret = v_recv_cmd_submit(udc, &pdu);
  196. break;
  197. default:
  198. ret = -EPIPE;
  199. pr_err("rx: unknown command");
  200. break;
  201. }
  202. return ret;
  203. }
  204. int v_rx_loop(void *data)
  205. {
  206. struct usbip_device *ud = data;
  207. int ret = 0;
  208. while (!kthread_should_stop()) {
  209. if (usbip_event_happened(ud))
  210. break;
  211. ret = v_rx_pdu(ud);
  212. if (ret < 0) {
  213. pr_warn("v_rx exit with error %d", ret);
  214. break;
  215. }
  216. }
  217. return ret;
  218. }