stub_dev.c 12 KB

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