stub_dev.c 12 KB

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