stub_dev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 store_sockfd(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(usbip_sockfd, S_IWUSR, NULL, store_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 tcp_socket %p\n",
  120. ud->tcp_socket);
  121. kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
  122. }
  123. /* 1. stop threads */
  124. if (ud->tcp_rx) {
  125. kthread_stop_put(ud->tcp_rx);
  126. ud->tcp_rx = NULL;
  127. }
  128. if (ud->tcp_tx) {
  129. kthread_stop_put(ud->tcp_tx);
  130. ud->tcp_tx = NULL;
  131. }
  132. /*
  133. * 2. close the socket
  134. *
  135. * tcp_socket is freed after threads are killed so that usbip_xmit does
  136. * not touch NULL socket.
  137. */
  138. if (ud->tcp_socket) {
  139. sockfd_put(ud->tcp_socket);
  140. ud->tcp_socket = NULL;
  141. }
  142. /* 3. free used data */
  143. stub_device_cleanup_urbs(sdev);
  144. /* 4. free stub_unlink */
  145. {
  146. unsigned long flags;
  147. struct stub_unlink *unlink, *tmp;
  148. spin_lock_irqsave(&sdev->priv_lock, flags);
  149. list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
  150. list_del(&unlink->list);
  151. kfree(unlink);
  152. }
  153. list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
  154. list) {
  155. list_del(&unlink->list);
  156. kfree(unlink);
  157. }
  158. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  159. }
  160. }
  161. static void stub_device_reset(struct usbip_device *ud)
  162. {
  163. struct stub_device *sdev = container_of(ud, struct stub_device, ud);
  164. struct usb_device *udev = sdev->udev;
  165. int ret;
  166. dev_dbg(&udev->dev, "device reset");
  167. ret = usb_lock_device_for_reset(udev, NULL);
  168. if (ret < 0) {
  169. dev_err(&udev->dev, "lock for reset\n");
  170. spin_lock_irq(&ud->lock);
  171. ud->status = SDEV_ST_ERROR;
  172. spin_unlock_irq(&ud->lock);
  173. return;
  174. }
  175. /* try to reset the device */
  176. ret = usb_reset_device(udev);
  177. usb_unlock_device(udev);
  178. spin_lock_irq(&ud->lock);
  179. if (ret) {
  180. dev_err(&udev->dev, "device reset\n");
  181. ud->status = SDEV_ST_ERROR;
  182. } else {
  183. dev_info(&udev->dev, "device reset\n");
  184. ud->status = SDEV_ST_AVAILABLE;
  185. }
  186. spin_unlock_irq(&ud->lock);
  187. }
  188. static void stub_device_unusable(struct usbip_device *ud)
  189. {
  190. spin_lock_irq(&ud->lock);
  191. ud->status = SDEV_ST_ERROR;
  192. spin_unlock_irq(&ud->lock);
  193. }
  194. /**
  195. * stub_device_alloc - allocate a new stub_device struct
  196. * @udev: usb_device of a new device
  197. *
  198. * Allocates and initializes a new stub_device struct.
  199. */
  200. static struct stub_device *stub_device_alloc(struct usb_device *udev)
  201. {
  202. struct stub_device *sdev;
  203. int busnum = udev->bus->busnum;
  204. int devnum = udev->devnum;
  205. dev_dbg(&udev->dev, "allocating stub device");
  206. /* yes, it's a new device */
  207. sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
  208. if (!sdev)
  209. return NULL;
  210. sdev->udev = usb_get_dev(udev);
  211. /*
  212. * devid is defined with devnum when this driver is first allocated.
  213. * devnum may change later if a device is reset. However, devid never
  214. * changes during a usbip connection.
  215. */
  216. sdev->devid = (busnum << 16) | devnum;
  217. sdev->ud.side = USBIP_STUB;
  218. sdev->ud.status = SDEV_ST_AVAILABLE;
  219. spin_lock_init(&sdev->ud.lock);
  220. sdev->ud.tcp_socket = NULL;
  221. INIT_LIST_HEAD(&sdev->priv_init);
  222. INIT_LIST_HEAD(&sdev->priv_tx);
  223. INIT_LIST_HEAD(&sdev->priv_free);
  224. INIT_LIST_HEAD(&sdev->unlink_free);
  225. INIT_LIST_HEAD(&sdev->unlink_tx);
  226. spin_lock_init(&sdev->priv_lock);
  227. init_waitqueue_head(&sdev->tx_waitq);
  228. sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
  229. sdev->ud.eh_ops.reset = stub_device_reset;
  230. sdev->ud.eh_ops.unusable = stub_device_unusable;
  231. usbip_start_eh(&sdev->ud);
  232. dev_dbg(&udev->dev, "register new device\n");
  233. return sdev;
  234. }
  235. static void stub_device_free(struct stub_device *sdev)
  236. {
  237. kfree(sdev);
  238. }
  239. static int stub_probe(struct usb_device *udev)
  240. {
  241. struct stub_device *sdev = NULL;
  242. const char *udev_busid = dev_name(&udev->dev);
  243. struct bus_id_priv *busid_priv;
  244. int rc;
  245. dev_dbg(&udev->dev, "Enter\n");
  246. /* check we should claim or not by busid_table */
  247. busid_priv = get_busid_priv(udev_busid);
  248. if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
  249. (busid_priv->status == STUB_BUSID_OTHER)) {
  250. dev_info(&udev->dev,
  251. "%s is not in match_busid table... skip!\n",
  252. udev_busid);
  253. /*
  254. * Return value should be ENODEV or ENOXIO to continue trying
  255. * other matched drivers by the driver core.
  256. * See driver_probe_device() in driver/base/dd.c
  257. */
  258. return -ENODEV;
  259. }
  260. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
  261. dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
  262. udev_busid);
  263. return -ENODEV;
  264. }
  265. if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
  266. dev_dbg(&udev->dev,
  267. "%s is attached on vhci_hcd... skip!\n",
  268. udev_busid);
  269. return -ENODEV;
  270. }
  271. /* ok, this is my device */
  272. sdev = stub_device_alloc(udev);
  273. if (!sdev)
  274. return -ENOMEM;
  275. dev_info(&udev->dev,
  276. "usbip-host: register new device (bus %u dev %u)\n",
  277. udev->bus->busnum, udev->devnum);
  278. busid_priv->shutdown_busid = 0;
  279. /* set private data to usb_device */
  280. dev_set_drvdata(&udev->dev, sdev);
  281. busid_priv->sdev = sdev;
  282. busid_priv->udev = udev;
  283. /*
  284. * Claim this hub port.
  285. * It doesn't matter what value we pass as owner
  286. * (struct dev_state) as long as it is unique.
  287. */
  288. rc = usb_hub_claim_port(udev->parent, udev->portnum,
  289. (struct usb_dev_state *) udev);
  290. if (rc) {
  291. dev_dbg(&udev->dev, "unable to claim port\n");
  292. goto err_port;
  293. }
  294. rc = stub_add_files(&udev->dev);
  295. if (rc) {
  296. dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
  297. goto err_files;
  298. }
  299. busid_priv->status = STUB_BUSID_ALLOC;
  300. return 0;
  301. err_files:
  302. usb_hub_release_port(udev->parent, udev->portnum,
  303. (struct usb_dev_state *) udev);
  304. err_port:
  305. dev_set_drvdata(&udev->dev, NULL);
  306. usb_put_dev(udev);
  307. busid_priv->sdev = NULL;
  308. stub_device_free(sdev);
  309. return rc;
  310. }
  311. static void shutdown_busid(struct bus_id_priv *busid_priv)
  312. {
  313. if (busid_priv->sdev && !busid_priv->shutdown_busid) {
  314. busid_priv->shutdown_busid = 1;
  315. usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
  316. /* wait for the stop of the event handler */
  317. usbip_stop_eh(&busid_priv->sdev->ud);
  318. }
  319. }
  320. /*
  321. * called in usb_disconnect() or usb_deregister()
  322. * but only if actconfig(active configuration) exists
  323. */
  324. static void stub_disconnect(struct usb_device *udev)
  325. {
  326. struct stub_device *sdev;
  327. const char *udev_busid = dev_name(&udev->dev);
  328. struct bus_id_priv *busid_priv;
  329. int rc;
  330. dev_dbg(&udev->dev, "Enter\n");
  331. busid_priv = get_busid_priv(udev_busid);
  332. if (!busid_priv) {
  333. BUG();
  334. return;
  335. }
  336. sdev = dev_get_drvdata(&udev->dev);
  337. /* get stub_device */
  338. if (!sdev) {
  339. dev_err(&udev->dev, "could not get device");
  340. return;
  341. }
  342. dev_set_drvdata(&udev->dev, NULL);
  343. /*
  344. * NOTE: rx/tx threads are invoked for each usb_device.
  345. */
  346. stub_remove_files(&udev->dev);
  347. /* release port */
  348. rc = usb_hub_release_port(udev->parent, udev->portnum,
  349. (struct usb_dev_state *) udev);
  350. if (rc) {
  351. dev_dbg(&udev->dev, "unable to release port\n");
  352. return;
  353. }
  354. /* If usb reset is called from event handler */
  355. if (usbip_in_eh(current))
  356. return;
  357. /* shutdown the current connection */
  358. shutdown_busid(busid_priv);
  359. usb_put_dev(sdev->udev);
  360. /* free sdev */
  361. busid_priv->sdev = NULL;
  362. stub_device_free(sdev);
  363. if (busid_priv->status == STUB_BUSID_ALLOC) {
  364. busid_priv->status = STUB_BUSID_ADDED;
  365. } else {
  366. busid_priv->status = STUB_BUSID_OTHER;
  367. del_match_busid((char *)udev_busid);
  368. }
  369. }
  370. #ifdef CONFIG_PM
  371. /* These functions need usb_port_suspend and usb_port_resume,
  372. * which reside in drivers/usb/core/usb.h. Skip for now. */
  373. static int stub_suspend(struct usb_device *udev, pm_message_t message)
  374. {
  375. dev_dbg(&udev->dev, "stub_suspend\n");
  376. return 0;
  377. }
  378. static int stub_resume(struct usb_device *udev, pm_message_t message)
  379. {
  380. dev_dbg(&udev->dev, "stub_resume\n");
  381. return 0;
  382. }
  383. #endif /* CONFIG_PM */
  384. struct usb_device_driver stub_driver = {
  385. .name = "usbip-host",
  386. .probe = stub_probe,
  387. .disconnect = stub_disconnect,
  388. #ifdef CONFIG_PM
  389. .suspend = stub_suspend,
  390. .resume = stub_resume,
  391. #endif
  392. .supports_autosuspend = 0,
  393. };