stub_rx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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 <asm/byteorder.h>
  20. #include <linux/kthread.h>
  21. #include <linux/usb.h>
  22. #include <linux/usb/hcd.h>
  23. #include "usbip_common.h"
  24. #include "stub.h"
  25. static int is_clear_halt_cmd(struct urb *urb)
  26. {
  27. struct usb_ctrlrequest *req;
  28. req = (struct usb_ctrlrequest *) urb->setup_packet;
  29. return (req->bRequest == USB_REQ_CLEAR_FEATURE) &&
  30. (req->bRequestType == USB_RECIP_ENDPOINT) &&
  31. (req->wValue == USB_ENDPOINT_HALT);
  32. }
  33. static int is_set_interface_cmd(struct urb *urb)
  34. {
  35. struct usb_ctrlrequest *req;
  36. req = (struct usb_ctrlrequest *) urb->setup_packet;
  37. return (req->bRequest == USB_REQ_SET_INTERFACE) &&
  38. (req->bRequestType == USB_RECIP_INTERFACE);
  39. }
  40. static int is_set_configuration_cmd(struct urb *urb)
  41. {
  42. struct usb_ctrlrequest *req;
  43. req = (struct usb_ctrlrequest *) urb->setup_packet;
  44. return (req->bRequest == USB_REQ_SET_CONFIGURATION) &&
  45. (req->bRequestType == USB_RECIP_DEVICE);
  46. }
  47. static int is_reset_device_cmd(struct urb *urb)
  48. {
  49. struct usb_ctrlrequest *req;
  50. __u16 value;
  51. __u16 index;
  52. req = (struct usb_ctrlrequest *) urb->setup_packet;
  53. value = le16_to_cpu(req->wValue);
  54. index = le16_to_cpu(req->wIndex);
  55. if ((req->bRequest == USB_REQ_SET_FEATURE) &&
  56. (req->bRequestType == USB_RT_PORT) &&
  57. (value == USB_PORT_FEAT_RESET)) {
  58. usbip_dbg_stub_rx("reset_device_cmd, port %u\n", index);
  59. return 1;
  60. } else
  61. return 0;
  62. }
  63. static int tweak_clear_halt_cmd(struct urb *urb)
  64. {
  65. struct usb_ctrlrequest *req;
  66. int target_endp;
  67. int target_dir;
  68. int target_pipe;
  69. int ret;
  70. req = (struct usb_ctrlrequest *) urb->setup_packet;
  71. /*
  72. * The stalled endpoint is specified in the wIndex value. The endpoint
  73. * of the urb is the target of this clear_halt request (i.e., control
  74. * endpoint).
  75. */
  76. target_endp = le16_to_cpu(req->wIndex) & 0x000f;
  77. /* the stalled endpoint direction is IN or OUT?. USB_DIR_IN is 0x80. */
  78. target_dir = le16_to_cpu(req->wIndex) & 0x0080;
  79. if (target_dir)
  80. target_pipe = usb_rcvctrlpipe(urb->dev, target_endp);
  81. else
  82. target_pipe = usb_sndctrlpipe(urb->dev, target_endp);
  83. ret = usb_clear_halt(urb->dev, target_pipe);
  84. if (ret < 0)
  85. dev_err(&urb->dev->dev,
  86. "usb_clear_halt error: devnum %d endp %d ret %d\n",
  87. urb->dev->devnum, target_endp, ret);
  88. else
  89. dev_info(&urb->dev->dev,
  90. "usb_clear_halt done: devnum %d endp %d\n",
  91. urb->dev->devnum, target_endp);
  92. return ret;
  93. }
  94. static int tweak_set_interface_cmd(struct urb *urb)
  95. {
  96. struct usb_ctrlrequest *req;
  97. __u16 alternate;
  98. __u16 interface;
  99. int ret;
  100. req = (struct usb_ctrlrequest *) urb->setup_packet;
  101. alternate = le16_to_cpu(req->wValue);
  102. interface = le16_to_cpu(req->wIndex);
  103. usbip_dbg_stub_rx("set_interface: inf %u alt %u\n",
  104. interface, alternate);
  105. ret = usb_set_interface(urb->dev, interface, alternate);
  106. if (ret < 0)
  107. dev_err(&urb->dev->dev,
  108. "usb_set_interface error: inf %u alt %u ret %d\n",
  109. interface, alternate, ret);
  110. else
  111. dev_info(&urb->dev->dev,
  112. "usb_set_interface done: inf %u alt %u\n",
  113. interface, alternate);
  114. return ret;
  115. }
  116. static int tweak_set_configuration_cmd(struct urb *urb)
  117. {
  118. struct stub_priv *priv = (struct stub_priv *) urb->context;
  119. struct stub_device *sdev = priv->sdev;
  120. struct usb_ctrlrequest *req;
  121. __u16 config;
  122. int err;
  123. req = (struct usb_ctrlrequest *) urb->setup_packet;
  124. config = le16_to_cpu(req->wValue);
  125. err = usb_set_configuration(sdev->udev, config);
  126. if (err && err != -ENODEV)
  127. dev_err(&sdev->udev->dev, "can't set config #%d, error %d\n",
  128. config, err);
  129. return 0;
  130. }
  131. static int tweak_reset_device_cmd(struct urb *urb)
  132. {
  133. struct stub_priv *priv = (struct stub_priv *) urb->context;
  134. struct stub_device *sdev = priv->sdev;
  135. dev_info(&urb->dev->dev, "usb_queue_reset_device\n");
  136. /*
  137. * With the implementation of pre_reset and post_reset the driver no
  138. * longer unbinds. This allows the use of synchronous reset.
  139. */
  140. if (usb_lock_device_for_reset(sdev->udev, sdev->interface) < 0) {
  141. dev_err(&urb->dev->dev, "could not obtain lock to reset device\n");
  142. return 0;
  143. }
  144. usb_reset_device(sdev->udev);
  145. usb_unlock_device(sdev->udev);
  146. return 0;
  147. }
  148. /*
  149. * clear_halt, set_interface, and set_configuration require special tricks.
  150. */
  151. static void tweak_special_requests(struct urb *urb)
  152. {
  153. if (!urb || !urb->setup_packet)
  154. return;
  155. if (usb_pipetype(urb->pipe) != PIPE_CONTROL)
  156. return;
  157. if (is_clear_halt_cmd(urb))
  158. /* tweak clear_halt */
  159. tweak_clear_halt_cmd(urb);
  160. else if (is_set_interface_cmd(urb))
  161. /* tweak set_interface */
  162. tweak_set_interface_cmd(urb);
  163. else if (is_set_configuration_cmd(urb))
  164. /* tweak set_configuration */
  165. tweak_set_configuration_cmd(urb);
  166. else if (is_reset_device_cmd(urb))
  167. tweak_reset_device_cmd(urb);
  168. else
  169. usbip_dbg_stub_rx("no need to tweak\n");
  170. }
  171. /*
  172. * stub_recv_unlink() unlinks the URB by a call to usb_unlink_urb().
  173. * By unlinking the urb asynchronously, stub_rx can continuously
  174. * process coming urbs. Even if the urb is unlinked, its completion
  175. * handler will be called and stub_tx will send a return pdu.
  176. *
  177. * See also comments about unlinking strategy in vhci_hcd.c.
  178. */
  179. static int stub_recv_cmd_unlink(struct stub_device *sdev,
  180. struct usbip_header *pdu)
  181. {
  182. int ret;
  183. unsigned long flags;
  184. struct stub_priv *priv;
  185. spin_lock_irqsave(&sdev->priv_lock, flags);
  186. list_for_each_entry(priv, &sdev->priv_init, list) {
  187. if (priv->seqnum != pdu->u.cmd_unlink.seqnum)
  188. continue;
  189. /*
  190. * This matched urb is not completed yet (i.e., be in
  191. * flight in usb hcd hardware/driver). Now we are
  192. * cancelling it. The unlinking flag means that we are
  193. * now not going to return the normal result pdu of a
  194. * submission request, but going to return a result pdu
  195. * of the unlink request.
  196. */
  197. priv->unlinking = 1;
  198. /*
  199. * In the case that unlinking flag is on, prev->seqnum
  200. * is changed from the seqnum of the cancelling urb to
  201. * the seqnum of the unlink request. This will be used
  202. * to make the result pdu of the unlink request.
  203. */
  204. priv->seqnum = pdu->base.seqnum;
  205. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  206. /*
  207. * usb_unlink_urb() is now out of spinlocking to avoid
  208. * spinlock recursion since stub_complete() is
  209. * sometimes called in this context but not in the
  210. * interrupt context. If stub_complete() is executed
  211. * before we call usb_unlink_urb(), usb_unlink_urb()
  212. * will return an error value. In this case, stub_tx
  213. * will return the result pdu of this unlink request
  214. * though submission is completed and actual unlinking
  215. * is not executed. OK?
  216. */
  217. /* In the above case, urb->status is not -ECONNRESET,
  218. * so a driver in a client host will know the failure
  219. * of the unlink request ?
  220. */
  221. ret = usb_unlink_urb(priv->urb);
  222. if (ret != -EINPROGRESS)
  223. dev_err(&priv->urb->dev->dev,
  224. "failed to unlink a urb # %lu, ret %d\n",
  225. priv->seqnum, ret);
  226. return 0;
  227. }
  228. usbip_dbg_stub_rx("seqnum %d is not pending\n",
  229. pdu->u.cmd_unlink.seqnum);
  230. /*
  231. * The urb of the unlink target is not found in priv_init queue. It was
  232. * already completed and its results is/was going to be sent by a
  233. * CMD_RET pdu. In this case, usb_unlink_urb() is not needed. We only
  234. * return the completeness of this unlink request to vhci_hcd.
  235. */
  236. stub_enqueue_ret_unlink(sdev, pdu->base.seqnum, 0);
  237. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  238. return 0;
  239. }
  240. static int valid_request(struct stub_device *sdev, struct usbip_header *pdu)
  241. {
  242. struct usbip_device *ud = &sdev->ud;
  243. int valid = 0;
  244. if (pdu->base.devid == sdev->devid) {
  245. spin_lock_irq(&ud->lock);
  246. if (ud->status == SDEV_ST_USED) {
  247. /* A request is valid. */
  248. valid = 1;
  249. }
  250. spin_unlock_irq(&ud->lock);
  251. }
  252. return valid;
  253. }
  254. static struct stub_priv *stub_priv_alloc(struct stub_device *sdev,
  255. struct usbip_header *pdu)
  256. {
  257. struct stub_priv *priv;
  258. struct usbip_device *ud = &sdev->ud;
  259. unsigned long flags;
  260. spin_lock_irqsave(&sdev->priv_lock, flags);
  261. priv = kmem_cache_zalloc(stub_priv_cache, GFP_ATOMIC);
  262. if (!priv) {
  263. dev_err(&sdev->interface->dev, "alloc stub_priv\n");
  264. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  265. usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
  266. return NULL;
  267. }
  268. priv->seqnum = pdu->base.seqnum;
  269. priv->sdev = sdev;
  270. /*
  271. * After a stub_priv is linked to a list_head,
  272. * our error handler can free allocated data.
  273. */
  274. list_add_tail(&priv->list, &sdev->priv_init);
  275. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  276. return priv;
  277. }
  278. static int get_pipe(struct stub_device *sdev, struct usbip_header *pdu)
  279. {
  280. struct usb_device *udev = sdev->udev;
  281. struct usb_host_endpoint *ep;
  282. struct usb_endpoint_descriptor *epd = NULL;
  283. int epnum = pdu->base.ep;
  284. int dir = pdu->base.direction;
  285. if (epnum < 0 || epnum > 15)
  286. goto err_ret;
  287. if (dir == USBIP_DIR_IN)
  288. ep = udev->ep_in[epnum & 0x7f];
  289. else
  290. ep = udev->ep_out[epnum & 0x7f];
  291. if (!ep)
  292. goto err_ret;
  293. epd = &ep->desc;
  294. if (usb_endpoint_xfer_control(epd)) {
  295. if (dir == USBIP_DIR_OUT)
  296. return usb_sndctrlpipe(udev, epnum);
  297. else
  298. return usb_rcvctrlpipe(udev, epnum);
  299. }
  300. if (usb_endpoint_xfer_bulk(epd)) {
  301. if (dir == USBIP_DIR_OUT)
  302. return usb_sndbulkpipe(udev, epnum);
  303. else
  304. return usb_rcvbulkpipe(udev, epnum);
  305. }
  306. if (usb_endpoint_xfer_int(epd)) {
  307. if (dir == USBIP_DIR_OUT)
  308. return usb_sndintpipe(udev, epnum);
  309. else
  310. return usb_rcvintpipe(udev, epnum);
  311. }
  312. if (usb_endpoint_xfer_isoc(epd)) {
  313. /* validate packet size and number of packets */
  314. unsigned int maxp, packets, bytes;
  315. #define USB_EP_MAXP_MULT_SHIFT 11
  316. #define USB_EP_MAXP_MULT_MASK (3 << USB_EP_MAXP_MULT_SHIFT)
  317. #define USB_EP_MAXP_MULT(m) \
  318. (((m) & USB_EP_MAXP_MULT_MASK) >> USB_EP_MAXP_MULT_SHIFT)
  319. maxp = usb_endpoint_maxp(epd);
  320. maxp *= (USB_EP_MAXP_MULT(
  321. __le16_to_cpu(epd->wMaxPacketSize)) + 1);
  322. bytes = pdu->u.cmd_submit.transfer_buffer_length;
  323. packets = DIV_ROUND_UP(bytes, maxp);
  324. if (pdu->u.cmd_submit.number_of_packets < 0 ||
  325. pdu->u.cmd_submit.number_of_packets > packets) {
  326. dev_err(&sdev->udev->dev,
  327. "CMD_SUBMIT: isoc invalid num packets %d\n",
  328. pdu->u.cmd_submit.number_of_packets);
  329. return -1;
  330. }
  331. if (dir == USBIP_DIR_OUT)
  332. return usb_sndisocpipe(udev, epnum);
  333. else
  334. return usb_rcvisocpipe(udev, epnum);
  335. }
  336. err_ret:
  337. /* NOT REACHED */
  338. dev_err(&sdev->udev->dev, "CMD_SUBMIT: invalid epnum %d\n", epnum);
  339. return -1;
  340. }
  341. static void masking_bogus_flags(struct urb *urb)
  342. {
  343. int xfertype;
  344. struct usb_device *dev;
  345. struct usb_host_endpoint *ep;
  346. int is_out;
  347. unsigned int allowed;
  348. if (!urb || urb->hcpriv || !urb->complete)
  349. return;
  350. dev = urb->dev;
  351. if ((!dev) || (dev->state < USB_STATE_UNAUTHENTICATED))
  352. return;
  353. ep = (usb_pipein(urb->pipe) ? dev->ep_in : dev->ep_out)
  354. [usb_pipeendpoint(urb->pipe)];
  355. if (!ep)
  356. return;
  357. xfertype = usb_endpoint_type(&ep->desc);
  358. if (xfertype == USB_ENDPOINT_XFER_CONTROL) {
  359. struct usb_ctrlrequest *setup =
  360. (struct usb_ctrlrequest *) urb->setup_packet;
  361. if (!setup)
  362. return;
  363. is_out = !(setup->bRequestType & USB_DIR_IN) ||
  364. !setup->wLength;
  365. } else {
  366. is_out = usb_endpoint_dir_out(&ep->desc);
  367. }
  368. /* enforce simple/standard policy */
  369. allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT |
  370. URB_DIR_MASK | URB_FREE_BUFFER);
  371. switch (xfertype) {
  372. case USB_ENDPOINT_XFER_BULK:
  373. if (is_out)
  374. allowed |= URB_ZERO_PACKET;
  375. /* FALLTHROUGH */
  376. case USB_ENDPOINT_XFER_CONTROL:
  377. allowed |= URB_NO_FSBR; /* only affects UHCI */
  378. /* FALLTHROUGH */
  379. default: /* all non-iso endpoints */
  380. if (!is_out)
  381. allowed |= URB_SHORT_NOT_OK;
  382. break;
  383. case USB_ENDPOINT_XFER_ISOC:
  384. allowed |= URB_ISO_ASAP;
  385. break;
  386. }
  387. urb->transfer_flags &= allowed;
  388. }
  389. static void stub_recv_cmd_submit(struct stub_device *sdev,
  390. struct usbip_header *pdu)
  391. {
  392. int ret;
  393. struct stub_priv *priv;
  394. struct usbip_device *ud = &sdev->ud;
  395. struct usb_device *udev = sdev->udev;
  396. int pipe = get_pipe(sdev, pdu);
  397. if (pipe == -1)
  398. return;
  399. priv = stub_priv_alloc(sdev, pdu);
  400. if (!priv)
  401. return;
  402. /* setup a urb */
  403. if (usb_pipeisoc(pipe))
  404. priv->urb = usb_alloc_urb(pdu->u.cmd_submit.number_of_packets,
  405. GFP_KERNEL);
  406. else
  407. priv->urb = usb_alloc_urb(0, GFP_KERNEL);
  408. if (!priv->urb) {
  409. dev_err(&sdev->interface->dev, "malloc urb\n");
  410. usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
  411. return;
  412. }
  413. /* allocate urb transfer buffer, if needed */
  414. if (pdu->u.cmd_submit.transfer_buffer_length > 0) {
  415. priv->urb->transfer_buffer =
  416. kzalloc(pdu->u.cmd_submit.transfer_buffer_length,
  417. GFP_KERNEL);
  418. if (!priv->urb->transfer_buffer) {
  419. usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
  420. return;
  421. }
  422. }
  423. /* copy urb setup packet */
  424. priv->urb->setup_packet = kmemdup(&pdu->u.cmd_submit.setup, 8,
  425. GFP_KERNEL);
  426. if (!priv->urb->setup_packet) {
  427. dev_err(&sdev->interface->dev, "allocate setup_packet\n");
  428. usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
  429. return;
  430. }
  431. /* set other members from the base header of pdu */
  432. priv->urb->context = (void *) priv;
  433. priv->urb->dev = udev;
  434. priv->urb->pipe = pipe;
  435. priv->urb->complete = stub_complete;
  436. usbip_pack_pdu(pdu, priv->urb, USBIP_CMD_SUBMIT, 0);
  437. if (usbip_recv_xbuff(ud, priv->urb) < 0)
  438. return;
  439. if (usbip_recv_iso(ud, priv->urb) < 0)
  440. return;
  441. /* no need to submit an intercepted request, but harmless? */
  442. tweak_special_requests(priv->urb);
  443. masking_bogus_flags(priv->urb);
  444. /* urb is now ready to submit */
  445. ret = usb_submit_urb(priv->urb, GFP_KERNEL);
  446. if (ret == 0)
  447. usbip_dbg_stub_rx("submit urb ok, seqnum %u\n",
  448. pdu->base.seqnum);
  449. else {
  450. dev_err(&sdev->interface->dev, "submit_urb error, %d\n", ret);
  451. usbip_dump_header(pdu);
  452. usbip_dump_urb(priv->urb);
  453. /*
  454. * Pessimistic.
  455. * This connection will be discarded.
  456. */
  457. usbip_event_add(ud, SDEV_EVENT_ERROR_SUBMIT);
  458. }
  459. usbip_dbg_stub_rx("Leave\n");
  460. }
  461. /* recv a pdu */
  462. static void stub_rx_pdu(struct usbip_device *ud)
  463. {
  464. int ret;
  465. struct usbip_header pdu;
  466. struct stub_device *sdev = container_of(ud, struct stub_device, ud);
  467. struct device *dev = &sdev->udev->dev;
  468. usbip_dbg_stub_rx("Enter\n");
  469. memset(&pdu, 0, sizeof(pdu));
  470. /* receive a pdu header */
  471. ret = usbip_recv(ud->tcp_socket, &pdu, sizeof(pdu));
  472. if (ret != sizeof(pdu)) {
  473. dev_err(dev, "recv a header, %d\n", ret);
  474. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  475. return;
  476. }
  477. usbip_header_correct_endian(&pdu, 0);
  478. if (usbip_dbg_flag_stub_rx)
  479. usbip_dump_header(&pdu);
  480. if (!valid_request(sdev, &pdu)) {
  481. dev_err(dev, "recv invalid request\n");
  482. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  483. return;
  484. }
  485. switch (pdu.base.command) {
  486. case USBIP_CMD_UNLINK:
  487. stub_recv_cmd_unlink(sdev, &pdu);
  488. break;
  489. case USBIP_CMD_SUBMIT:
  490. stub_recv_cmd_submit(sdev, &pdu);
  491. break;
  492. default:
  493. /* NOTREACHED */
  494. dev_err(dev, "unknown pdu\n");
  495. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  496. break;
  497. }
  498. }
  499. int stub_rx_loop(void *data)
  500. {
  501. struct usbip_device *ud = data;
  502. while (!kthread_should_stop()) {
  503. if (usbip_event_happened(ud))
  504. break;
  505. stub_rx_pdu(ud);
  506. }
  507. return 0;
  508. }