stub_dev.c 11 KB

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