PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/usb/usbip/stub_dev.c

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