stub_main.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. /*
  24. * This also sets the bus_table[i].status to
  25. * STUB_BUSID_OTHER, which is 0.
  26. */
  27. memset(busid_table, 0, sizeof(busid_table));
  28. spin_lock_init(&busid_table_lock);
  29. }
  30. /*
  31. * Find the index of the busid by name.
  32. * Must be called with busid_table_lock held.
  33. */
  34. static int get_busid_idx(const char *busid)
  35. {
  36. int i;
  37. int idx = -1;
  38. for (i = 0; i < MAX_BUSID; i++)
  39. if (busid_table[i].name[0])
  40. if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
  41. idx = i;
  42. break;
  43. }
  44. return idx;
  45. }
  46. struct bus_id_priv *get_busid_priv(const char *busid)
  47. {
  48. int idx;
  49. struct bus_id_priv *bid = NULL;
  50. spin_lock(&busid_table_lock);
  51. idx = get_busid_idx(busid);
  52. if (idx >= 0)
  53. bid = &(busid_table[idx]);
  54. spin_unlock(&busid_table_lock);
  55. return bid;
  56. }
  57. static int add_match_busid(char *busid)
  58. {
  59. int i;
  60. int ret = -1;
  61. spin_lock(&busid_table_lock);
  62. /* already registered? */
  63. if (get_busid_idx(busid) >= 0) {
  64. ret = 0;
  65. goto out;
  66. }
  67. for (i = 0; i < MAX_BUSID; i++)
  68. if (!busid_table[i].name[0]) {
  69. strlcpy(busid_table[i].name, busid, BUSID_SIZE);
  70. if ((busid_table[i].status != STUB_BUSID_ALLOC) &&
  71. (busid_table[i].status != STUB_BUSID_REMOV))
  72. busid_table[i].status = STUB_BUSID_ADDED;
  73. ret = 0;
  74. break;
  75. }
  76. out:
  77. spin_unlock(&busid_table_lock);
  78. return ret;
  79. }
  80. int del_match_busid(char *busid)
  81. {
  82. int idx;
  83. int ret = -1;
  84. spin_lock(&busid_table_lock);
  85. idx = get_busid_idx(busid);
  86. if (idx < 0)
  87. goto out;
  88. /* found */
  89. ret = 0;
  90. if (busid_table[idx].status == STUB_BUSID_OTHER)
  91. memset(busid_table[idx].name, 0, BUSID_SIZE);
  92. if ((busid_table[idx].status != STUB_BUSID_OTHER) &&
  93. (busid_table[idx].status != STUB_BUSID_ADDED))
  94. busid_table[idx].status = STUB_BUSID_REMOV;
  95. out:
  96. spin_unlock(&busid_table_lock);
  97. return ret;
  98. }
  99. static ssize_t match_busid_show(struct device_driver *drv, char *buf)
  100. {
  101. int i;
  102. char *out = buf;
  103. spin_lock(&busid_table_lock);
  104. for (i = 0; i < MAX_BUSID; i++)
  105. if (busid_table[i].name[0])
  106. out += sprintf(out, "%s ", busid_table[i].name);
  107. spin_unlock(&busid_table_lock);
  108. out += sprintf(out, "\n");
  109. return out - buf;
  110. }
  111. static ssize_t match_busid_store(struct device_driver *dev, const char *buf,
  112. size_t count)
  113. {
  114. int len;
  115. char busid[BUSID_SIZE];
  116. if (count < 5)
  117. return -EINVAL;
  118. /* busid needs to include \0 termination */
  119. len = strlcpy(busid, buf + 4, BUSID_SIZE);
  120. if (sizeof(busid) <= len)
  121. return -EINVAL;
  122. if (!strncmp(buf, "add ", 4)) {
  123. if (add_match_busid(busid) < 0)
  124. return -ENOMEM;
  125. pr_debug("add busid %s\n", busid);
  126. return count;
  127. }
  128. if (!strncmp(buf, "del ", 4)) {
  129. if (del_match_busid(busid) < 0)
  130. return -ENODEV;
  131. pr_debug("del busid %s\n", busid);
  132. return count;
  133. }
  134. return -EINVAL;
  135. }
  136. static DRIVER_ATTR_RW(match_busid);
  137. static ssize_t rebind_store(struct device_driver *dev, const char *buf,
  138. size_t count)
  139. {
  140. int ret;
  141. int len;
  142. struct bus_id_priv *bid;
  143. /* buf length should be less that BUSID_SIZE */
  144. len = strnlen(buf, BUSID_SIZE);
  145. if (!(len < BUSID_SIZE))
  146. return -EINVAL;
  147. bid = get_busid_priv(buf);
  148. if (!bid)
  149. return -ENODEV;
  150. ret = device_attach(&bid->udev->dev);
  151. if (ret < 0) {
  152. dev_err(&bid->udev->dev, "rebind failed\n");
  153. return ret;
  154. }
  155. return count;
  156. }
  157. static DRIVER_ATTR_WO(rebind);
  158. static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
  159. {
  160. struct stub_priv *priv, *tmp;
  161. list_for_each_entry_safe(priv, tmp, listhead, list) {
  162. list_del(&priv->list);
  163. return priv;
  164. }
  165. return NULL;
  166. }
  167. static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
  168. {
  169. unsigned long flags;
  170. struct stub_priv *priv;
  171. spin_lock_irqsave(&sdev->priv_lock, flags);
  172. priv = stub_priv_pop_from_listhead(&sdev->priv_init);
  173. if (priv)
  174. goto done;
  175. priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
  176. if (priv)
  177. goto done;
  178. priv = stub_priv_pop_from_listhead(&sdev->priv_free);
  179. done:
  180. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  181. return priv;
  182. }
  183. void stub_device_cleanup_urbs(struct stub_device *sdev)
  184. {
  185. struct stub_priv *priv;
  186. struct urb *urb;
  187. dev_dbg(&sdev->udev->dev, "free sdev %p\n", sdev);
  188. while ((priv = stub_priv_pop(sdev))) {
  189. urb = priv->urb;
  190. dev_dbg(&sdev->udev->dev, "free urb %p\n", urb);
  191. usb_kill_urb(urb);
  192. kmem_cache_free(stub_priv_cache, priv);
  193. kfree(urb->transfer_buffer);
  194. urb->transfer_buffer = NULL;
  195. kfree(urb->setup_packet);
  196. urb->setup_packet = NULL;
  197. usb_free_urb(urb);
  198. }
  199. }
  200. static int __init usbip_host_init(void)
  201. {
  202. int ret;
  203. init_busid_table();
  204. stub_priv_cache = KMEM_CACHE(stub_priv, SLAB_HWCACHE_ALIGN);
  205. if (!stub_priv_cache) {
  206. pr_err("kmem_cache_create failed\n");
  207. return -ENOMEM;
  208. }
  209. ret = usb_register_device_driver(&stub_driver, THIS_MODULE);
  210. if (ret) {
  211. pr_err("usb_register failed %d\n", ret);
  212. goto err_usb_register;
  213. }
  214. ret = driver_create_file(&stub_driver.drvwrap.driver,
  215. &driver_attr_match_busid);
  216. if (ret) {
  217. pr_err("driver_create_file failed\n");
  218. goto err_create_file;
  219. }
  220. ret = driver_create_file(&stub_driver.drvwrap.driver,
  221. &driver_attr_rebind);
  222. if (ret) {
  223. pr_err("driver_create_file failed\n");
  224. goto err_create_file;
  225. }
  226. return ret;
  227. err_create_file:
  228. usb_deregister_device_driver(&stub_driver);
  229. err_usb_register:
  230. kmem_cache_destroy(stub_priv_cache);
  231. return ret;
  232. }
  233. static void __exit usbip_host_exit(void)
  234. {
  235. driver_remove_file(&stub_driver.drvwrap.driver,
  236. &driver_attr_match_busid);
  237. driver_remove_file(&stub_driver.drvwrap.driver,
  238. &driver_attr_rebind);
  239. /*
  240. * deregister() calls stub_disconnect() for all devices. Device
  241. * specific data is cleared in stub_disconnect().
  242. */
  243. usb_deregister_device_driver(&stub_driver);
  244. kmem_cache_destroy(stub_priv_cache);
  245. }
  246. module_init(usbip_host_init);
  247. module_exit(usbip_host_exit);
  248. MODULE_AUTHOR(DRIVER_AUTHOR);
  249. MODULE_DESCRIPTION(DRIVER_DESC);
  250. MODULE_LICENSE("GPL");