stub_rx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. dev_info(&priv->urb->dev->dev, "unlink urb %p\n",
  190. priv->urb);
  191. /*
  192. * This matched urb is not completed yet (i.e., be in
  193. * flight in usb hcd hardware/driver). Now we are
  194. * cancelling it. The unlinking flag means that we are
  195. * now not going to return the normal result pdu of a
  196. * submission request, but going to return a result pdu
  197. * of the unlink request.
  198. */
  199. priv->unlinking = 1;
  200. /*
  201. * In the case that unlinking flag is on, prev->seqnum
  202. * is changed from the seqnum of the cancelling urb to
  203. * the seqnum of the unlink request. This will be used
  204. * to make the result pdu of the unlink request.
  205. */
  206. priv->seqnum = pdu->base.seqnum;
  207. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  208. /*
  209. * usb_unlink_urb() is now out of spinlocking to avoid
  210. * spinlock recursion since stub_complete() is
  211. * sometimes called in this context but not in the
  212. * interrupt context. If stub_complete() is executed
  213. * before we call usb_unlink_urb(), usb_unlink_urb()
  214. * will return an error value. In this case, stub_tx
  215. * will return the result pdu of this unlink request
  216. * though submission is completed and actual unlinking
  217. * is not executed. OK?
  218. */
  219. /* In the above case, urb->status is not -ECONNRESET,
  220. * so a driver in a client host will know the failure
  221. * of the unlink request ?
  222. */
  223. ret = usb_unlink_urb(priv->urb);
  224. if (ret != -EINPROGRESS)
  225. dev_err(&priv->urb->dev->dev,
  226. "failed to unlink a urb %p, ret %d\n",
  227. priv->urb, ret);
  228. return 0;
  229. }
  230. usbip_dbg_stub_rx("seqnum %d is not pending\n",
  231. pdu->u.cmd_unlink.seqnum);
  232. /*
  233. * The urb of the unlink target is not found in priv_init queue. It was
  234. * already completed and its results is/was going to be sent by a
  235. * CMD_RET pdu. In this case, usb_unlink_urb() is not needed. We only
  236. * return the completeness of this unlink request to vhci_hcd.
  237. */
  238. stub_enqueue_ret_unlink(sdev, pdu->base.seqnum, 0);
  239. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  240. return 0;
  241. }
  242. static int valid_request(struct stub_device *sdev, struct usbip_header *pdu)
  243. {
  244. struct usbip_device *ud = &sdev->ud;
  245. int valid = 0;
  246. if (pdu->base.devid == sdev->devid) {
  247. spin_lock_irq(&ud->lock);
  248. if (ud->status == SDEV_ST_USED) {
  249. /* A request is valid. */
  250. valid = 1;
  251. }
  252. spin_unlock_irq(&ud->lock);
  253. }
  254. return valid;
  255. }
  256. static struct stub_priv *stub_priv_alloc(struct stub_device *sdev,
  257. struct usbip_header *pdu)
  258. {
  259. struct stub_priv *priv;
  260. struct usbip_device *ud = &sdev->ud;
  261. unsigned long flags;
  262. spin_lock_irqsave(&sdev->priv_lock, flags);
  263. priv = kmem_cache_zalloc(stub_priv_cache, GFP_ATOMIC);
  264. if (!priv) {
  265. dev_err(&sdev->interface->dev, "alloc stub_priv\n");
  266. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  267. usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
  268. return NULL;
  269. }
  270. priv->seqnum = pdu->base.seqnum;
  271. priv->sdev = sdev;
  272. /*
  273. * After a stub_priv is linked to a list_head,
  274. * our error handler can free allocated data.
  275. */
  276. list_add_tail(&priv->list, &sdev->priv_init);
  277. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  278. return priv;
  279. }
  280. static int get_pipe(struct stub_device *sdev, int epnum, int dir)
  281. {
  282. struct usb_device *udev = sdev->udev;
  283. struct usb_host_endpoint *ep;
  284. struct usb_endpoint_descriptor *epd = NULL;
  285. if (dir == USBIP_DIR_IN)
  286. ep = udev->ep_in[epnum & 0x7f];
  287. else
  288. ep = udev->ep_out[epnum & 0x7f];
  289. if (!ep) {
  290. dev_err(&sdev->interface->dev, "no such endpoint?, %d\n",
  291. epnum);
  292. BUG();
  293. }
  294. epd = &ep->desc;
  295. if (usb_endpoint_xfer_control(epd)) {
  296. if (dir == USBIP_DIR_OUT)
  297. return usb_sndctrlpipe(udev, epnum);
  298. else
  299. return usb_rcvctrlpipe(udev, epnum);
  300. }
  301. if (usb_endpoint_xfer_bulk(epd)) {
  302. if (dir == USBIP_DIR_OUT)
  303. return usb_sndbulkpipe(udev, epnum);
  304. else
  305. return usb_rcvbulkpipe(udev, epnum);
  306. }
  307. if (usb_endpoint_xfer_int(epd)) {
  308. if (dir == USBIP_DIR_OUT)
  309. return usb_sndintpipe(udev, epnum);
  310. else
  311. return usb_rcvintpipe(udev, epnum);
  312. }
  313. if (usb_endpoint_xfer_isoc(epd)) {
  314. if (dir == USBIP_DIR_OUT)
  315. return usb_sndisocpipe(udev, epnum);
  316. else
  317. return usb_rcvisocpipe(udev, epnum);
  318. }
  319. /* NOT REACHED */
  320. dev_err(&sdev->interface->dev, "get pipe, epnum %d\n", epnum);
  321. return 0;
  322. }
  323. static void masking_bogus_flags(struct urb *urb)
  324. {
  325. int xfertype;
  326. struct usb_device *dev;
  327. struct usb_host_endpoint *ep;
  328. int is_out;
  329. unsigned int allowed;
  330. if (!urb || urb->hcpriv || !urb->complete)
  331. return;
  332. dev = urb->dev;
  333. if ((!dev) || (dev->state < USB_STATE_UNAUTHENTICATED))
  334. return;
  335. ep = (usb_pipein(urb->pipe) ? dev->ep_in : dev->ep_out)
  336. [usb_pipeendpoint(urb->pipe)];
  337. if (!ep)
  338. return;
  339. xfertype = usb_endpoint_type(&ep->desc);
  340. if (xfertype == USB_ENDPOINT_XFER_CONTROL) {
  341. struct usb_ctrlrequest *setup =
  342. (struct usb_ctrlrequest *) urb->setup_packet;
  343. if (!setup)
  344. return;
  345. is_out = !(setup->bRequestType & USB_DIR_IN) ||
  346. !setup->wLength;
  347. } else {
  348. is_out = usb_endpoint_dir_out(&ep->desc);
  349. }
  350. /* enforce simple/standard policy */
  351. allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT |
  352. URB_DIR_MASK | URB_FREE_BUFFER);
  353. switch (xfertype) {
  354. case USB_ENDPOINT_XFER_BULK:
  355. if (is_out)
  356. allowed |= URB_ZERO_PACKET;
  357. /* FALLTHROUGH */
  358. case USB_ENDPOINT_XFER_CONTROL:
  359. allowed |= URB_NO_FSBR; /* only affects UHCI */
  360. /* FALLTHROUGH */
  361. default: /* all non-iso endpoints */
  362. if (!is_out)
  363. allowed |= URB_SHORT_NOT_OK;
  364. break;
  365. case USB_ENDPOINT_XFER_ISOC:
  366. allowed |= URB_ISO_ASAP;
  367. break;
  368. }
  369. urb->transfer_flags &= allowed;
  370. }
  371. static void stub_recv_cmd_submit(struct stub_device *sdev,
  372. struct usbip_header *pdu)
  373. {
  374. int ret;
  375. struct stub_priv *priv;
  376. struct usbip_device *ud = &sdev->ud;
  377. struct usb_device *udev = sdev->udev;
  378. int pipe = get_pipe(sdev, pdu->base.ep, pdu->base.direction);
  379. priv = stub_priv_alloc(sdev, pdu);
  380. if (!priv)
  381. return;
  382. /* setup a urb */
  383. if (usb_pipeisoc(pipe))
  384. priv->urb = usb_alloc_urb(pdu->u.cmd_submit.number_of_packets,
  385. GFP_KERNEL);
  386. else
  387. priv->urb = usb_alloc_urb(0, GFP_KERNEL);
  388. if (!priv->urb) {
  389. dev_err(&sdev->interface->dev, "malloc urb\n");
  390. usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
  391. return;
  392. }
  393. /* allocate urb transfer buffer, if needed */
  394. if (pdu->u.cmd_submit.transfer_buffer_length > 0) {
  395. priv->urb->transfer_buffer =
  396. kzalloc(pdu->u.cmd_submit.transfer_buffer_length,
  397. GFP_KERNEL);
  398. if (!priv->urb->transfer_buffer) {
  399. usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
  400. return;
  401. }
  402. }
  403. /* copy urb setup packet */
  404. priv->urb->setup_packet = kmemdup(&pdu->u.cmd_submit.setup, 8,
  405. GFP_KERNEL);
  406. if (!priv->urb->setup_packet) {
  407. dev_err(&sdev->interface->dev, "allocate setup_packet\n");
  408. usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
  409. return;
  410. }
  411. /* set other members from the base header of pdu */
  412. priv->urb->context = (void *) priv;
  413. priv->urb->dev = udev;
  414. priv->urb->pipe = pipe;
  415. priv->urb->complete = stub_complete;
  416. usbip_pack_pdu(pdu, priv->urb, USBIP_CMD_SUBMIT, 0);
  417. if (usbip_recv_xbuff(ud, priv->urb) < 0)
  418. return;
  419. if (usbip_recv_iso(ud, priv->urb) < 0)
  420. return;
  421. /* no need to submit an intercepted request, but harmless? */
  422. tweak_special_requests(priv->urb);
  423. masking_bogus_flags(priv->urb);
  424. /* urb is now ready to submit */
  425. ret = usb_submit_urb(priv->urb, GFP_KERNEL);
  426. if (ret == 0)
  427. usbip_dbg_stub_rx("submit urb ok, seqnum %u\n",
  428. pdu->base.seqnum);
  429. else {
  430. dev_err(&sdev->interface->dev, "submit_urb error, %d\n", ret);
  431. usbip_dump_header(pdu);
  432. usbip_dump_urb(priv->urb);
  433. /*
  434. * Pessimistic.
  435. * This connection will be discarded.
  436. */
  437. usbip_event_add(ud, SDEV_EVENT_ERROR_SUBMIT);
  438. }
  439. usbip_dbg_stub_rx("Leave\n");
  440. }
  441. /* recv a pdu */
  442. static void stub_rx_pdu(struct usbip_device *ud)
  443. {
  444. int ret;
  445. struct usbip_header pdu;
  446. struct stub_device *sdev = container_of(ud, struct stub_device, ud);
  447. struct device *dev = &sdev->udev->dev;
  448. usbip_dbg_stub_rx("Enter\n");
  449. memset(&pdu, 0, sizeof(pdu));
  450. /* receive a pdu header */
  451. ret = usbip_recv(ud->tcp_socket, &pdu, sizeof(pdu));
  452. if (ret != sizeof(pdu)) {
  453. dev_err(dev, "recv a header, %d\n", ret);
  454. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  455. return;
  456. }
  457. usbip_header_correct_endian(&pdu, 0);
  458. if (usbip_dbg_flag_stub_rx)
  459. usbip_dump_header(&pdu);
  460. if (!valid_request(sdev, &pdu)) {
  461. dev_err(dev, "recv invalid request\n");
  462. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  463. return;
  464. }
  465. switch (pdu.base.command) {
  466. case USBIP_CMD_UNLINK:
  467. stub_recv_cmd_unlink(sdev, &pdu);
  468. break;
  469. case USBIP_CMD_SUBMIT:
  470. stub_recv_cmd_submit(sdev, &pdu);
  471. break;
  472. default:
  473. /* NOTREACHED */
  474. dev_err(dev, "unknown pdu\n");
  475. usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
  476. break;
  477. }
  478. }
  479. int stub_rx_loop(void *data)
  480. {
  481. struct usbip_device *ud = data;
  482. while (!kthread_should_stop()) {
  483. if (usbip_event_happened(ud))
  484. break;
  485. stub_rx_pdu(ud);
  486. }
  487. return 0;
  488. }