stub_main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #include <linux/string.h>
  20. #include <linux/module.h>
  21. #include <linux/device.h>
  22. #include "usbip_common.h"
  23. #include "stub.h"
  24. #define DRIVER_AUTHOR "Takahiro Hirofuchi"
  25. #define DRIVER_DESC "USB/IP Host Driver"
  26. struct kmem_cache *stub_priv_cache;
  27. /*
  28. * busid_tables defines matching busids that usbip can grab. A user can change
  29. * dynamically what device is locally used and what device is exported to a
  30. * remote host.
  31. */
  32. #define MAX_BUSID 16
  33. static struct bus_id_priv busid_table[MAX_BUSID];
  34. static spinlock_t busid_table_lock;
  35. static void init_busid_table(void)
  36. {
  37. /*
  38. * This also sets the bus_table[i].status to
  39. * STUB_BUSID_OTHER, which is 0.
  40. */
  41. memset(busid_table, 0, sizeof(busid_table));
  42. spin_lock_init(&busid_table_lock);
  43. }
  44. /*
  45. * Find the index of the busid by name.
  46. * Must be called with busid_table_lock held.
  47. */
  48. static int get_busid_idx(const char *busid)
  49. {
  50. int i;
  51. int idx = -1;
  52. for (i = 0; i < MAX_BUSID; i++)
  53. if (busid_table[i].name[0])
  54. if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
  55. idx = i;
  56. break;
  57. }
  58. return idx;
  59. }
  60. struct bus_id_priv *get_busid_priv(const char *busid)
  61. {
  62. int idx;
  63. struct bus_id_priv *bid = NULL;
  64. spin_lock(&busid_table_lock);
  65. idx = get_busid_idx(busid);
  66. if (idx >= 0)
  67. bid = &(busid_table[idx]);
  68. spin_unlock(&busid_table_lock);
  69. return bid;
  70. }
  71. static int add_match_busid(char *busid)
  72. {
  73. int i;
  74. int ret = -1;
  75. spin_lock(&busid_table_lock);
  76. /* already registered? */
  77. if (get_busid_idx(busid) >= 0) {
  78. ret = 0;
  79. goto out;
  80. }
  81. for (i = 0; i < MAX_BUSID; i++)
  82. if (!busid_table[i].name[0]) {
  83. strlcpy(busid_table[i].name, busid, BUSID_SIZE);
  84. if ((busid_table[i].status != STUB_BUSID_ALLOC) &&
  85. (busid_table[i].status != STUB_BUSID_REMOV))
  86. busid_table[i].status = STUB_BUSID_ADDED;
  87. ret = 0;
  88. break;
  89. }
  90. out:
  91. spin_unlock(&busid_table_lock);
  92. return ret;
  93. }
  94. int del_match_busid(char *busid)
  95. {
  96. int idx;
  97. int ret = -1;
  98. spin_lock(&busid_table_lock);
  99. idx = get_busid_idx(busid);
  100. if (idx < 0)
  101. goto out;
  102. /* found */
  103. ret = 0;
  104. if (busid_table[idx].status == STUB_BUSID_OTHER)
  105. memset(busid_table[idx].name, 0, BUSID_SIZE);
  106. if ((busid_table[idx].status != STUB_BUSID_OTHER) &&
  107. (busid_table[idx].status != STUB_BUSID_ADDED))
  108. busid_table[idx].status = STUB_BUSID_REMOV;
  109. out:
  110. spin_unlock(&busid_table_lock);
  111. return ret;
  112. }
  113. static ssize_t show_match_busid(struct device_driver *drv, char *buf)
  114. {
  115. int i;
  116. char *out = buf;
  117. spin_lock(&busid_table_lock);
  118. for (i = 0; i < MAX_BUSID; i++)
  119. if (busid_table[i].name[0])
  120. out += sprintf(out, "%s ", busid_table[i].name);
  121. spin_unlock(&busid_table_lock);
  122. out += sprintf(out, "\n");
  123. return out - buf;
  124. }
  125. static ssize_t store_match_busid(struct device_driver *dev, const char *buf,
  126. size_t count)
  127. {
  128. int len;
  129. char busid[BUSID_SIZE];
  130. if (count < 5)
  131. return -EINVAL;
  132. /* busid needs to include \0 termination */
  133. len = strlcpy(busid, buf + 4, BUSID_SIZE);
  134. if (sizeof(busid) <= len)
  135. return -EINVAL;
  136. if (!strncmp(buf, "add ", 4)) {
  137. if (add_match_busid(busid) < 0)
  138. return -ENOMEM;
  139. pr_debug("add busid %s\n", busid);
  140. return count;
  141. }
  142. if (!strncmp(buf, "del ", 4)) {
  143. if (del_match_busid(busid) < 0)
  144. return -ENODEV;
  145. pr_debug("del busid %s\n", busid);
  146. return count;
  147. }
  148. return -EINVAL;
  149. }
  150. static DRIVER_ATTR(match_busid, S_IRUSR | S_IWUSR, show_match_busid,
  151. store_match_busid);
  152. static ssize_t rebind_store(struct device_driver *dev, const char *buf,
  153. size_t count)
  154. {
  155. int ret;
  156. int len;
  157. struct bus_id_priv *bid;
  158. /* buf length should be less that BUSID_SIZE */
  159. len = strnlen(buf, BUSID_SIZE);
  160. if (!(len < BUSID_SIZE))
  161. return -EINVAL;
  162. bid = get_busid_priv(buf);
  163. if (!bid)
  164. return -ENODEV;
  165. ret = device_attach(&bid->udev->dev);
  166. if (ret < 0) {
  167. dev_err(&bid->udev->dev, "rebind failed\n");
  168. return ret;
  169. }
  170. return count;
  171. }
  172. static DRIVER_ATTR_WO(rebind);
  173. static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
  174. {
  175. struct stub_priv *priv, *tmp;
  176. list_for_each_entry_safe(priv, tmp, listhead, list) {
  177. list_del(&priv->list);
  178. return priv;
  179. }
  180. return NULL;
  181. }
  182. static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
  183. {
  184. unsigned long flags;
  185. struct stub_priv *priv;
  186. spin_lock_irqsave(&sdev->priv_lock, flags);
  187. priv = stub_priv_pop_from_listhead(&sdev->priv_init);
  188. if (priv)
  189. goto done;
  190. priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
  191. if (priv)
  192. goto done;
  193. priv = stub_priv_pop_from_listhead(&sdev->priv_free);
  194. done:
  195. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  196. return priv;
  197. }
  198. void stub_device_cleanup_urbs(struct stub_device *sdev)
  199. {
  200. struct stub_priv *priv;
  201. struct urb *urb;
  202. dev_dbg(&sdev->udev->dev, "free sdev %p\n", sdev);
  203. while ((priv = stub_priv_pop(sdev))) {
  204. urb = priv->urb;
  205. dev_dbg(&sdev->udev->dev, "free urb %p\n", urb);
  206. usb_kill_urb(urb);
  207. kmem_cache_free(stub_priv_cache, priv);
  208. kfree(urb->transfer_buffer);
  209. urb->transfer_buffer = NULL;
  210. kfree(urb->setup_packet);
  211. urb->setup_packet = NULL;
  212. usb_free_urb(urb);
  213. }
  214. }
  215. static int __init usbip_host_init(void)
  216. {
  217. int ret;
  218. init_busid_table();
  219. stub_priv_cache = KMEM_CACHE(stub_priv, SLAB_HWCACHE_ALIGN);
  220. if (!stub_priv_cache) {
  221. pr_err("kmem_cache_create failed\n");
  222. return -ENOMEM;
  223. }
  224. ret = usb_register_device_driver(&stub_driver, THIS_MODULE);
  225. if (ret) {
  226. pr_err("usb_register failed %d\n", ret);
  227. goto err_usb_register;
  228. }
  229. ret = driver_create_file(&stub_driver.drvwrap.driver,
  230. &driver_attr_match_busid);
  231. if (ret) {
  232. pr_err("driver_create_file failed\n");
  233. goto err_create_file;
  234. }
  235. ret = driver_create_file(&stub_driver.drvwrap.driver,
  236. &driver_attr_rebind);
  237. if (ret) {
  238. pr_err("driver_create_file failed\n");
  239. goto err_create_file;
  240. }
  241. pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
  242. return ret;
  243. err_create_file:
  244. usb_deregister_device_driver(&stub_driver);
  245. err_usb_register:
  246. kmem_cache_destroy(stub_priv_cache);
  247. return ret;
  248. }
  249. static void __exit usbip_host_exit(void)
  250. {
  251. driver_remove_file(&stub_driver.drvwrap.driver,
  252. &driver_attr_match_busid);
  253. driver_remove_file(&stub_driver.drvwrap.driver,
  254. &driver_attr_rebind);
  255. /*
  256. * deregister() calls stub_disconnect() for all devices. Device
  257. * specific data is cleared in stub_disconnect().
  258. */
  259. usb_deregister_device_driver(&stub_driver);
  260. kmem_cache_destroy(stub_priv_cache);
  261. }
  262. module_init(usbip_host_init);
  263. module_exit(usbip_host_exit);
  264. MODULE_AUTHOR(DRIVER_AUTHOR);
  265. MODULE_DESCRIPTION(DRIVER_DESC);
  266. MODULE_LICENSE("GPL");
  267. MODULE_VERSION(USBIP_VERSION);