stub_dev.c 12 KB

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