stub_main.c 9.3 KB

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