vudc_rx.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. ret = alloc_urb_from_cmd(&urb_p->urb, pdu, urb_p->ep->type);
  116. if (ret) {
  117. usbip_event_add(&udc->ud, VUDC_EVENT_ERROR_MALLOC);
  118. ret = -ENOMEM;
  119. goto free_urbp;
  120. }
  121. urb_p->urb->status = -EINPROGRESS;
  122. /* FIXME: more pipe setup to please usbip_common */
  123. urb_p->urb->pipe &= ~(3 << 30);
  124. switch (urb_p->ep->type) {
  125. case USB_ENDPOINT_XFER_BULK:
  126. urb_p->urb->pipe |= (PIPE_BULK << 30);
  127. break;
  128. case USB_ENDPOINT_XFER_INT:
  129. urb_p->urb->pipe |= (PIPE_INTERRUPT << 30);
  130. break;
  131. case USB_ENDPOINT_XFER_CONTROL:
  132. urb_p->urb->pipe |= (PIPE_CONTROL << 30);
  133. break;
  134. case USB_ENDPOINT_XFER_ISOC:
  135. urb_p->urb->pipe |= (PIPE_ISOCHRONOUS << 30);
  136. break;
  137. }
  138. ret = usbip_recv_xbuff(&udc->ud, urb_p->urb);
  139. if (ret < 0)
  140. goto free_urbp;
  141. ret = usbip_recv_iso(&udc->ud, urb_p->urb);
  142. if (ret < 0)
  143. goto free_urbp;
  144. spin_lock_irqsave(&udc->lock, flags);
  145. v_kick_timer(udc, jiffies);
  146. list_add_tail(&urb_p->urb_entry, &udc->urb_queue);
  147. spin_unlock_irqrestore(&udc->lock, flags);
  148. return 0;
  149. free_urbp:
  150. free_urbp_and_urb(urb_p);
  151. return ret;
  152. }
  153. static int v_rx_pdu(struct usbip_device *ud)
  154. {
  155. int ret;
  156. struct usbip_header pdu;
  157. struct vudc *udc = container_of(ud, struct vudc, ud);
  158. memset(&pdu, 0, sizeof(pdu));
  159. ret = usbip_recv(ud->tcp_socket, &pdu, sizeof(pdu));
  160. if (ret != sizeof(pdu)) {
  161. usbip_event_add(ud, VUDC_EVENT_ERROR_TCP);
  162. if (ret >= 0)
  163. return -EPIPE;
  164. return ret;
  165. }
  166. usbip_header_correct_endian(&pdu, 0);
  167. spin_lock_irq(&ud->lock);
  168. ret = (ud->status == SDEV_ST_USED);
  169. spin_unlock_irq(&ud->lock);
  170. if (!ret) {
  171. usbip_event_add(ud, VUDC_EVENT_ERROR_TCP);
  172. return -EBUSY;
  173. }
  174. switch (pdu.base.command) {
  175. case USBIP_CMD_UNLINK:
  176. ret = v_recv_cmd_unlink(udc, &pdu);
  177. break;
  178. case USBIP_CMD_SUBMIT:
  179. ret = v_recv_cmd_submit(udc, &pdu);
  180. break;
  181. default:
  182. ret = -EPIPE;
  183. pr_err("rx: unknown command");
  184. break;
  185. }
  186. return ret;
  187. }
  188. int v_rx_loop(void *data)
  189. {
  190. struct usbip_device *ud = data;
  191. int ret = 0;
  192. while (!kthread_should_stop()) {
  193. if (usbip_event_happened(ud))
  194. break;
  195. ret = v_rx_pdu(ud);
  196. if (ret < 0) {
  197. pr_warn("v_rx exit with error %d", ret);
  198. break;
  199. }
  200. }
  201. return ret;
  202. }