stub_dev.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 <linux/device.h>
  20. #include <linux/file.h>
  21. #include <linux/kthread.h>
  22. #include <linux/module.h>
  23. #include "usbip_common.h"
  24. #include "stub.h"
  25. /*
  26. * usbip_status shows the status of usbip-host as long as this driver is bound
  27. * to the target device.
  28. */
  29. static ssize_t usbip_status_show(struct device *dev,
  30. struct device_attribute *attr, char *buf)
  31. {
  32. struct stub_device *sdev = dev_get_drvdata(dev);
  33. int status;
  34. if (!sdev) {
  35. dev_err(dev, "sdev is null\n");
  36. return -ENODEV;
  37. }
  38. spin_lock_irq(&sdev->ud.lock);
  39. status = sdev->ud.status;
  40. spin_unlock_irq(&sdev->ud.lock);
  41. return snprintf(buf, PAGE_SIZE, "%d\n", status);
  42. }
  43. static DEVICE_ATTR_RO(usbip_status);
  44. /*
  45. * usbip_sockfd gets a socket descriptor of an established TCP connection that
  46. * is used to transfer usbip requests by kernel threads. -1 is a magic number
  47. * by which usbip connection is finished.
  48. */
  49. static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
  50. const char *buf, size_t count)
  51. {
  52. struct stub_device *sdev = dev_get_drvdata(dev);
  53. int sockfd = 0;
  54. struct socket *socket;
  55. int rv;
  56. if (!sdev) {
  57. dev_err(dev, "sdev is null\n");
  58. return -ENODEV;
  59. }
  60. rv = sscanf(buf, "%d", &sockfd);
  61. if (rv != 1)
  62. return -EINVAL;
  63. if (sockfd != -1) {
  64. int err;
  65. dev_info(dev, "stub up\n");
  66. spin_lock_irq(&sdev->ud.lock);
  67. if (sdev->ud.status != SDEV_ST_AVAILABLE) {
  68. dev_err(dev, "not ready\n");
  69. goto err;
  70. }
  71. socket = sockfd_lookup(sockfd, &err);
  72. if (!socket)
  73. goto err;
  74. sdev->ud.tcp_socket = socket;
  75. sdev->ud.sockfd = sockfd;
  76. spin_unlock_irq(&sdev->ud.lock);
  77. sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud,
  78. "stub_rx");
  79. sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud,
  80. "stub_tx");
  81. spin_lock_irq(&sdev->ud.lock);
  82. sdev->ud.status = SDEV_ST_USED;
  83. spin_unlock_irq(&sdev->ud.lock);
  84. } else {
  85. dev_info(dev, "stub down\n");
  86. spin_lock_irq(&sdev->ud.lock);
  87. if (sdev->ud.status != SDEV_ST_USED)
  88. goto err;
  89. spin_unlock_irq(&sdev->ud.lock);
  90. usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
  91. }
  92. return count;
  93. err:
  94. spin_unlock_irq(&sdev->ud.lock);
  95. return -EINVAL;
  96. }
  97. static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
  98. static int stub_add_files(struct device *dev)
  99. {
  100. int err = 0;
  101. err = device_create_file(dev, &dev_attr_usbip_status);
  102. if (err)
  103. goto err_status;
  104. err = device_create_file(dev, &dev_attr_usbip_sockfd);
  105. if (err)
  106. goto err_sockfd;
  107. err = device_create_file(dev, &dev_attr_usbip_debug);
  108. if (err)
  109. goto err_debug;
  110. return 0;
  111. err_debug:
  112. device_remove_file(dev, &dev_attr_usbip_sockfd);
  113. err_sockfd:
  114. device_remove_file(dev, &dev_attr_usbip_status);
  115. err_status:
  116. return err;
  117. }
  118. static void stub_remove_files(struct device *dev)
  119. {
  120. device_remove_file(dev, &dev_attr_usbip_status);
  121. device_remove_file(dev, &dev_attr_usbip_sockfd);
  122. device_remove_file(dev, &dev_attr_usbip_debug);
  123. }
  124. static void stub_shutdown_connection(struct usbip_device *ud)
  125. {
  126. struct stub_device *sdev = container_of(ud, struct stub_device, ud);
  127. /*
  128. * When removing an exported device, kernel panic sometimes occurred
  129. * and then EIP was sk_wait_data of stub_rx thread. Is this because
  130. * sk_wait_data returned though stub_rx thread was already finished by
  131. * step 1?
  132. */
  133. if (ud->tcp_socket) {
  134. dev_dbg(&sdev->udev->dev, "shutdown sockfd %d\n", ud->sockfd);
  135. kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
  136. }
  137. /* 1. stop threads */
  138. if (ud->tcp_rx) {
  139. kthread_stop_put(ud->tcp_rx);
  140. ud->tcp_rx = NULL;
  141. }
  142. if (ud->tcp_tx) {
  143. kthread_stop_put(ud->tcp_tx);
  144. ud->tcp_tx = NULL;
  145. }
  146. /*
  147. * 2. close the socket
  148. *
  149. * tcp_socket is freed after threads are killed so that usbip_xmit does
  150. * not touch NULL socket.
  151. */
  152. if (ud->tcp_socket) {
  153. sockfd_put(ud->tcp_socket);
  154. ud->tcp_socket = NULL;
  155. ud->sockfd = -1;
  156. }
  157. /* 3. free used data */
  158. stub_device_cleanup_urbs(sdev);
  159. /* 4. free stub_unlink */
  160. {
  161. unsigned long flags;
  162. struct stub_unlink *unlink, *tmp;
  163. spin_lock_irqsave(&sdev->priv_lock, flags);
  164. list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
  165. list_del(&unlink->list);
  166. kfree(unlink);
  167. }
  168. list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
  169. list) {
  170. list_del(&unlink->list);
  171. kfree(unlink);
  172. }
  173. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  174. }
  175. }
  176. static void stub_device_reset(struct usbip_device *ud)
  177. {
  178. struct stub_device *sdev = container_of(ud, struct stub_device, ud);
  179. struct usb_device *udev = sdev->udev;
  180. int ret;
  181. dev_dbg(&udev->dev, "device reset");
  182. ret = usb_lock_device_for_reset(udev, sdev->interface);
  183. if (ret < 0) {
  184. dev_err(&udev->dev, "lock for reset\n");
  185. spin_lock_irq(&ud->lock);
  186. ud->status = SDEV_ST_ERROR;
  187. spin_unlock_irq(&ud->lock);
  188. return;
  189. }
  190. /* try to reset the device */
  191. ret = usb_reset_device(udev);
  192. usb_unlock_device(udev);
  193. spin_lock_irq(&ud->lock);
  194. if (ret) {
  195. dev_err(&udev->dev, "device reset\n");
  196. ud->status = SDEV_ST_ERROR;
  197. } else {
  198. dev_info(&udev->dev, "device reset\n");
  199. ud->status = SDEV_ST_AVAILABLE;
  200. }
  201. spin_unlock_irq(&ud->lock);
  202. }
  203. static void stub_device_unusable(struct usbip_device *ud)
  204. {
  205. spin_lock_irq(&ud->lock);
  206. ud->status = SDEV_ST_ERROR;
  207. spin_unlock_irq(&ud->lock);
  208. }
  209. /**
  210. * stub_device_alloc - allocate a new stub_device struct
  211. * @interface: usb_interface of a new device
  212. *
  213. * Allocates and initializes a new stub_device struct.
  214. */
  215. static struct stub_device *stub_device_alloc(struct usb_device *udev)
  216. {
  217. struct stub_device *sdev;
  218. int busnum = udev->bus->busnum;
  219. int devnum = udev->devnum;
  220. dev_dbg(&udev->dev, "allocating stub device");
  221. /* yes, it's a new device */
  222. sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
  223. if (!sdev)
  224. return NULL;
  225. sdev->udev = usb_get_dev(udev);
  226. /*
  227. * devid is defined with devnum when this driver is first allocated.
  228. * devnum may change later if a device is reset. However, devid never
  229. * changes during a usbip connection.
  230. */
  231. sdev->devid = (busnum << 16) | devnum;
  232. sdev->ud.side = USBIP_STUB;
  233. sdev->ud.status = SDEV_ST_AVAILABLE;
  234. spin_lock_init(&sdev->ud.lock);
  235. sdev->ud.tcp_socket = NULL;
  236. sdev->ud.sockfd = -1;
  237. INIT_LIST_HEAD(&sdev->priv_init);
  238. INIT_LIST_HEAD(&sdev->priv_tx);
  239. INIT_LIST_HEAD(&sdev->priv_free);
  240. INIT_LIST_HEAD(&sdev->unlink_free);
  241. INIT_LIST_HEAD(&sdev->unlink_tx);
  242. spin_lock_init(&sdev->priv_lock);
  243. init_waitqueue_head(&sdev->tx_waitq);
  244. sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
  245. sdev->ud.eh_ops.reset = stub_device_reset;
  246. sdev->ud.eh_ops.unusable = stub_device_unusable;
  247. usbip_start_eh(&sdev->ud);
  248. dev_dbg(&udev->dev, "register new device\n");
  249. return sdev;
  250. }
  251. static void stub_device_free(struct stub_device *sdev)
  252. {
  253. kfree(sdev);
  254. }
  255. static int stub_probe(struct usb_device *udev)
  256. {
  257. struct stub_device *sdev = NULL;
  258. const char *udev_busid = dev_name(&udev->dev);
  259. struct bus_id_priv *busid_priv;
  260. int rc = 0;
  261. dev_dbg(&udev->dev, "Enter probe\n");
  262. /* check we should claim or not by busid_table */
  263. busid_priv = get_busid_priv(udev_busid);
  264. if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
  265. (busid_priv->status == STUB_BUSID_OTHER)) {
  266. dev_info(&udev->dev,
  267. "%s is not in match_busid table... skip!\n",
  268. udev_busid);
  269. /*
  270. * Return value should be ENODEV or ENOXIO to continue trying
  271. * other matched drivers by the driver core.
  272. * See driver_probe_device() in driver/base/dd.c
  273. */
  274. rc = -ENODEV;
  275. goto call_put_busid_priv;
  276. }
  277. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
  278. dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
  279. udev_busid);
  280. rc = -ENODEV;
  281. goto call_put_busid_priv;
  282. }
  283. if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
  284. dev_dbg(&udev->dev,
  285. "%s is attached on vhci_hcd... skip!\n",
  286. udev_busid);
  287. rc = -ENODEV;
  288. goto call_put_busid_priv;
  289. }
  290. /* ok, this is my device */
  291. sdev = stub_device_alloc(udev);
  292. if (!sdev) {
  293. rc = -ENOMEM;
  294. goto call_put_busid_priv;
  295. }
  296. dev_info(&udev->dev,
  297. "usbip-host: register new device (bus %u dev %u)\n",
  298. udev->bus->busnum, udev->devnum);
  299. busid_priv->shutdown_busid = 0;
  300. /* set private data to usb_device */
  301. dev_set_drvdata(&udev->dev, sdev);
  302. busid_priv->sdev = sdev;
  303. busid_priv->udev = udev;
  304. /*
  305. * Claim this hub port.
  306. * It doesn't matter what value we pass as owner
  307. * (struct dev_state) as long as it is unique.
  308. */
  309. rc = usb_hub_claim_port(udev->parent, udev->portnum,
  310. (struct usb_dev_state *) udev);
  311. if (rc) {
  312. dev_dbg(&udev->dev, "unable to claim port\n");
  313. goto err_port;
  314. }
  315. rc = stub_add_files(&udev->dev);
  316. if (rc) {
  317. dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
  318. goto err_files;
  319. }
  320. busid_priv->status = STUB_BUSID_ALLOC;
  321. rc = 0;
  322. goto call_put_busid_priv;
  323. err_files:
  324. usb_hub_release_port(udev->parent, udev->portnum,
  325. (struct usb_dev_state *) udev);
  326. err_port:
  327. dev_set_drvdata(&udev->dev, NULL);
  328. usb_put_dev(udev);
  329. kthread_stop_put(sdev->ud.eh);
  330. busid_priv->sdev = NULL;
  331. stub_device_free(sdev);
  332. call_put_busid_priv:
  333. put_busid_priv(busid_priv);
  334. return rc;
  335. }
  336. static void shutdown_busid(struct bus_id_priv *busid_priv)
  337. {
  338. if (busid_priv->sdev && !busid_priv->shutdown_busid) {
  339. busid_priv->shutdown_busid = 1;
  340. usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
  341. /* wait for the stop of the event handler */
  342. usbip_stop_eh(&busid_priv->sdev->ud);
  343. }
  344. }
  345. /*
  346. * called in usb_disconnect() or usb_deregister()
  347. * but only if actconfig(active configuration) exists
  348. */
  349. static void stub_disconnect(struct usb_device *udev)
  350. {
  351. struct stub_device *sdev;
  352. const char *udev_busid = dev_name(&udev->dev);
  353. struct bus_id_priv *busid_priv;
  354. int rc;
  355. dev_dbg(&udev->dev, "Enter disconnect\n");
  356. busid_priv = get_busid_priv(udev_busid);
  357. if (!busid_priv) {
  358. BUG();
  359. return;
  360. }
  361. sdev = dev_get_drvdata(&udev->dev);
  362. /* get stub_device */
  363. if (!sdev) {
  364. dev_err(&udev->dev, "could not get device");
  365. goto call_put_busid_priv;
  366. }
  367. dev_set_drvdata(&udev->dev, NULL);
  368. /*
  369. * NOTE: rx/tx threads are invoked for each usb_device.
  370. */
  371. stub_remove_files(&udev->dev);
  372. /* release port */
  373. rc = usb_hub_release_port(udev->parent, udev->portnum,
  374. (struct usb_dev_state *) udev);
  375. if (rc) {
  376. dev_dbg(&udev->dev, "unable to release port\n");
  377. goto call_put_busid_priv;
  378. }
  379. /* If usb reset is called from event handler */
  380. if (busid_priv->sdev->ud.eh == current)
  381. goto call_put_busid_priv;
  382. /* shutdown the current connection */
  383. shutdown_busid(busid_priv);
  384. usb_put_dev(sdev->udev);
  385. /* free sdev */
  386. busid_priv->sdev = NULL;
  387. stub_device_free(sdev);
  388. if (busid_priv->status == STUB_BUSID_ALLOC)
  389. busid_priv->status = STUB_BUSID_ADDED;
  390. call_put_busid_priv:
  391. put_busid_priv(busid_priv);
  392. }
  393. #ifdef CONFIG_PM
  394. /* These functions need usb_port_suspend and usb_port_resume,
  395. * which reside in drivers/usb/core/usb.h. Skip for now. */
  396. static int stub_suspend(struct usb_device *udev, pm_message_t message)
  397. {
  398. dev_dbg(&udev->dev, "stub_suspend\n");
  399. return 0;
  400. }
  401. static int stub_resume(struct usb_device *udev, pm_message_t message)
  402. {
  403. dev_dbg(&udev->dev, "stub_resume\n");
  404. return 0;
  405. }
  406. #endif /* CONFIG_PM */
  407. struct usb_device_driver stub_driver = {
  408. .name = "usbip-host",
  409. .probe = stub_probe,
  410. .disconnect = stub_disconnect,
  411. #ifdef CONFIG_PM
  412. .suspend = stub_suspend,
  413. .resume = stub_resume,
  414. #endif
  415. .supports_autosuspend = 0,
  416. };