stub_dev.c 11 KB

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