stub_rx.c 14 KB

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