stub_dev.c 13 KB

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