stub_main.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  4. */
  5. #include <linux/string.h>
  6. #include <linux/module.h>
  7. #include <linux/device.h>
  8. #include "usbip_common.h"
  9. #include "stub.h"
  10. #define DRIVER_AUTHOR "Takahiro Hirofuchi"
  11. #define DRIVER_DESC "USB/IP Host Driver"
  12. struct kmem_cache *stub_priv_cache;
  13. /*
  14. * busid_tables defines matching busids that usbip can grab. A user can change
  15. * dynamically what device is locally used and what device is exported to a
  16. * remote host.
  17. */
  18. #define MAX_BUSID 16
  19. static struct bus_id_priv busid_table[MAX_BUSID];
  20. static spinlock_t busid_table_lock;
  21. static void init_busid_table(void)
  22. {
  23. int i;
  24. /*
  25. * This also sets the bus_table[i].status to
  26. * STUB_BUSID_OTHER, which is 0.
  27. */
  28. memset(busid_table, 0, sizeof(busid_table));
  29. spin_lock_init(&busid_table_lock);
  30. for (i = 0; i < MAX_BUSID; i++)
  31. spin_lock_init(&busid_table[i].busid_lock);
  32. }
  33. /*
  34. * Find the index of the busid by name.
  35. * Must be called with busid_table_lock held.
  36. */
  37. static int get_busid_idx(const char *busid)
  38. {
  39. int i;
  40. int idx = -1;
  41. for (i = 0; i < MAX_BUSID; i++) {
  42. spin_lock(&busid_table[i].busid_lock);
  43. if (busid_table[i].name[0])
  44. if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
  45. idx = i;
  46. spin_unlock(&busid_table[i].busid_lock);
  47. break;
  48. }
  49. spin_unlock(&busid_table[i].busid_lock);
  50. }
  51. return idx;
  52. }
  53. /* Returns holding busid_lock. Should call put_busid_priv() to unlock */
  54. struct bus_id_priv *get_busid_priv(const char *busid)
  55. {
  56. int idx;
  57. struct bus_id_priv *bid = NULL;
  58. spin_lock(&busid_table_lock);
  59. idx = get_busid_idx(busid);
  60. if (idx >= 0) {
  61. bid = &(busid_table[idx]);
  62. /* get busid_lock before returning */
  63. spin_lock(&bid->busid_lock);
  64. }
  65. spin_unlock(&busid_table_lock);
  66. return bid;
  67. }
  68. void put_busid_priv(struct bus_id_priv *bid)
  69. {
  70. spin_unlock(&bid->busid_lock);
  71. }
  72. static int add_match_busid(char *busid)
  73. {
  74. int i;
  75. int ret = -1;
  76. spin_lock(&busid_table_lock);
  77. /* already registered? */
  78. if (get_busid_idx(busid) >= 0) {
  79. ret = 0;
  80. goto out;
  81. }
  82. for (i = 0; i < MAX_BUSID; i++) {
  83. spin_lock(&busid_table[i].busid_lock);
  84. if (!busid_table[i].name[0]) {
  85. strlcpy(busid_table[i].name, busid, BUSID_SIZE);
  86. if ((busid_table[i].status != STUB_BUSID_ALLOC) &&
  87. (busid_table[i].status != STUB_BUSID_REMOV))
  88. busid_table[i].status = STUB_BUSID_ADDED;
  89. ret = 0;
  90. spin_unlock(&busid_table[i].busid_lock);
  91. break;
  92. }
  93. spin_unlock(&busid_table[i].busid_lock);
  94. }
  95. out:
  96. spin_unlock(&busid_table_lock);
  97. return ret;
  98. }
  99. int del_match_busid(char *busid)
  100. {
  101. int idx;
  102. int ret = -1;
  103. spin_lock(&busid_table_lock);
  104. idx = get_busid_idx(busid);
  105. if (idx < 0)
  106. goto out;
  107. /* found */
  108. ret = 0;
  109. spin_lock(&busid_table[idx].busid_lock);
  110. if (busid_table[idx].status == STUB_BUSID_OTHER)
  111. memset(busid_table[idx].name, 0, BUSID_SIZE);
  112. if ((busid_table[idx].status != STUB_BUSID_OTHER) &&
  113. (busid_table[idx].status != STUB_BUSID_ADDED))
  114. busid_table[idx].status = STUB_BUSID_REMOV;
  115. spin_unlock(&busid_table[idx].busid_lock);
  116. out:
  117. spin_unlock(&busid_table_lock);
  118. return ret;
  119. }
  120. static ssize_t match_busid_show(struct device_driver *drv, char *buf)
  121. {
  122. int i;
  123. char *out = buf;
  124. spin_lock(&busid_table_lock);
  125. for (i = 0; i < MAX_BUSID; i++) {
  126. spin_lock(&busid_table[i].busid_lock);
  127. if (busid_table[i].name[0])
  128. out += sprintf(out, "%s ", busid_table[i].name);
  129. spin_unlock(&busid_table[i].busid_lock);
  130. }
  131. spin_unlock(&busid_table_lock);
  132. out += sprintf(out, "\n");
  133. return out - buf;
  134. }
  135. static ssize_t match_busid_store(struct device_driver *dev, const char *buf,
  136. size_t count)
  137. {
  138. int len;
  139. char busid[BUSID_SIZE];
  140. if (count < 5)
  141. return -EINVAL;
  142. /* busid needs to include \0 termination */
  143. len = strlcpy(busid, buf + 4, BUSID_SIZE);
  144. if (sizeof(busid) <= len)
  145. return -EINVAL;
  146. if (!strncmp(buf, "add ", 4)) {
  147. if (add_match_busid(busid) < 0)
  148. return -ENOMEM;
  149. pr_debug("add busid %s\n", busid);
  150. return count;
  151. }
  152. if (!strncmp(buf, "del ", 4)) {
  153. if (del_match_busid(busid) < 0)
  154. return -ENODEV;
  155. pr_debug("del busid %s\n", busid);
  156. return count;
  157. }
  158. return -EINVAL;
  159. }
  160. static DRIVER_ATTR_RW(match_busid);
  161. static int do_rebind(char *busid, struct bus_id_priv *busid_priv)
  162. {
  163. int ret;
  164. /* device_attach() callers should hold parent lock for USB */
  165. if (busid_priv->udev->dev.parent)
  166. device_lock(busid_priv->udev->dev.parent);
  167. ret = device_attach(&busid_priv->udev->dev);
  168. if (busid_priv->udev->dev.parent)
  169. device_unlock(busid_priv->udev->dev.parent);
  170. if (ret < 0) {
  171. dev_err(&busid_priv->udev->dev, "rebind failed\n");
  172. return ret;
  173. }
  174. return 0;
  175. }
  176. static void stub_device_rebind(void)
  177. {
  178. #if IS_MODULE(CONFIG_USBIP_HOST)
  179. struct bus_id_priv *busid_priv;
  180. int i;
  181. /* update status to STUB_BUSID_OTHER so probe ignores the device */
  182. spin_lock(&busid_table_lock);
  183. for (i = 0; i < MAX_BUSID; i++) {
  184. if (busid_table[i].name[0] &&
  185. busid_table[i].shutdown_busid) {
  186. busid_priv = &(busid_table[i]);
  187. busid_priv->status = STUB_BUSID_OTHER;
  188. }
  189. }
  190. spin_unlock(&busid_table_lock);
  191. /* now run rebind - no need to hold locks. driver files are removed */
  192. for (i = 0; i < MAX_BUSID; i++) {
  193. if (busid_table[i].name[0] &&
  194. busid_table[i].shutdown_busid) {
  195. busid_priv = &(busid_table[i]);
  196. do_rebind(busid_table[i].name, busid_priv);
  197. }
  198. }
  199. #endif
  200. }
  201. static ssize_t rebind_store(struct device_driver *dev, const char *buf,
  202. size_t count)
  203. {
  204. int ret;
  205. int len;
  206. struct bus_id_priv *bid;
  207. /* buf length should be less that BUSID_SIZE */
  208. len = strnlen(buf, BUSID_SIZE);
  209. if (!(len < BUSID_SIZE))
  210. return -EINVAL;
  211. bid = get_busid_priv(buf);
  212. if (!bid)
  213. return -ENODEV;
  214. /* mark the device for deletion so probe ignores it during rescan */
  215. bid->status = STUB_BUSID_OTHER;
  216. /* release the busid lock */
  217. put_busid_priv(bid);
  218. ret = do_rebind((char *) buf, bid);
  219. if (ret < 0)
  220. return ret;
  221. /* delete device from busid_table */
  222. del_match_busid((char *) buf);
  223. return count;
  224. }
  225. static DRIVER_ATTR_WO(rebind);
  226. static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
  227. {
  228. struct stub_priv *priv, *tmp;
  229. list_for_each_entry_safe(priv, tmp, listhead, list) {
  230. list_del(&priv->list);
  231. return priv;
  232. }
  233. return NULL;
  234. }
  235. static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
  236. {
  237. unsigned long flags;
  238. struct stub_priv *priv;
  239. spin_lock_irqsave(&sdev->priv_lock, flags);
  240. priv = stub_priv_pop_from_listhead(&sdev->priv_init);
  241. if (priv)
  242. goto done;
  243. priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
  244. if (priv)
  245. goto done;
  246. priv = stub_priv_pop_from_listhead(&sdev->priv_free);
  247. done:
  248. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  249. return priv;
  250. }
  251. void stub_device_cleanup_urbs(struct stub_device *sdev)
  252. {
  253. struct stub_priv *priv;
  254. struct urb *urb;
  255. dev_dbg(&sdev->udev->dev, "Stub device cleaning up urbs\n");
  256. while ((priv = stub_priv_pop(sdev))) {
  257. urb = priv->urb;
  258. dev_dbg(&sdev->udev->dev, "free urb seqnum %lu\n",
  259. priv->seqnum);
  260. usb_kill_urb(urb);
  261. kmem_cache_free(stub_priv_cache, priv);
  262. kfree(urb->transfer_buffer);
  263. urb->transfer_buffer = NULL;
  264. kfree(urb->setup_packet);
  265. urb->setup_packet = NULL;
  266. usb_free_urb(urb);
  267. }
  268. }
  269. static int __init usbip_host_init(void)
  270. {
  271. int ret;
  272. init_busid_table();
  273. stub_priv_cache = KMEM_CACHE(stub_priv, SLAB_HWCACHE_ALIGN);
  274. if (!stub_priv_cache) {
  275. pr_err("kmem_cache_create failed\n");
  276. return -ENOMEM;
  277. }
  278. ret = usb_register_device_driver(&stub_driver, THIS_MODULE);
  279. if (ret) {
  280. pr_err("usb_register failed %d\n", ret);
  281. goto err_usb_register;
  282. }
  283. ret = driver_create_file(&stub_driver.drvwrap.driver,
  284. &driver_attr_match_busid);
  285. if (ret) {
  286. pr_err("driver_create_file failed\n");
  287. goto err_create_file;
  288. }
  289. ret = driver_create_file(&stub_driver.drvwrap.driver,
  290. &driver_attr_rebind);
  291. if (ret) {
  292. pr_err("driver_create_file failed\n");
  293. goto err_create_file;
  294. }
  295. return ret;
  296. err_create_file:
  297. usb_deregister_device_driver(&stub_driver);
  298. err_usb_register:
  299. kmem_cache_destroy(stub_priv_cache);
  300. return ret;
  301. }
  302. static void __exit usbip_host_exit(void)
  303. {
  304. driver_remove_file(&stub_driver.drvwrap.driver,
  305. &driver_attr_match_busid);
  306. driver_remove_file(&stub_driver.drvwrap.driver,
  307. &driver_attr_rebind);
  308. /*
  309. * deregister() calls stub_disconnect() for all devices. Device
  310. * specific data is cleared in stub_disconnect().
  311. */
  312. usb_deregister_device_driver(&stub_driver);
  313. /* initiate scan to attach devices */
  314. stub_device_rebind();
  315. kmem_cache_destroy(stub_priv_cache);
  316. }
  317. module_init(usbip_host_init);
  318. module_exit(usbip_host_exit);
  319. MODULE_AUTHOR(DRIVER_AUTHOR);
  320. MODULE_DESCRIPTION(DRIVER_DESC);
  321. MODULE_LICENSE("GPL");