2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
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.
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.
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,
20 #include <linux/string.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
24 #include "usbip_common.h"
27 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
28 #define DRIVER_DESC "USB/IP Host Driver"
30 struct kmem_cache *stub_priv_cache;
32 * busid_tables defines matching busids that usbip can grab. A user can change
33 * dynamically what device is locally used and what device is exported to a
37 static struct bus_id_priv busid_table[MAX_BUSID];
38 static spinlock_t busid_table_lock;
40 static void init_busid_table(void)
43 * This also sets the bus_table[i].status to
44 * STUB_BUSID_OTHER, which is 0.
46 memset(busid_table, 0, sizeof(busid_table));
48 spin_lock_init(&busid_table_lock);
52 * Find the index of the busid by name.
53 * Must be called with busid_table_lock held.
55 static int get_busid_idx(const char *busid)
60 for (i = 0; i < MAX_BUSID; i++)
61 if (busid_table[i].name[0])
62 if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
69 struct bus_id_priv *get_busid_priv(const char *busid)
72 struct bus_id_priv *bid = NULL;
74 spin_lock(&busid_table_lock);
75 idx = get_busid_idx(busid);
77 bid = &(busid_table[idx]);
78 spin_unlock(&busid_table_lock);
83 static int add_match_busid(char *busid)
88 spin_lock(&busid_table_lock);
89 /* already registered? */
90 if (get_busid_idx(busid) >= 0) {
95 for (i = 0; i < MAX_BUSID; i++)
96 if (!busid_table[i].name[0]) {
97 strncpy(busid_table[i].name, busid, BUSID_SIZE);
98 if ((busid_table[i].status != STUB_BUSID_ALLOC) &&
99 (busid_table[i].status != STUB_BUSID_REMOV))
100 busid_table[i].status = STUB_BUSID_ADDED;
106 spin_unlock(&busid_table_lock);
111 int del_match_busid(char *busid)
116 spin_lock(&busid_table_lock);
117 idx = get_busid_idx(busid);
124 if (busid_table[idx].status == STUB_BUSID_OTHER)
125 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;
132 spin_unlock(&busid_table_lock);
137 static ssize_t show_match_busid(struct device_driver *drv, char *buf)
142 spin_lock(&busid_table_lock);
143 for (i = 0; i < MAX_BUSID; i++)
144 if (busid_table[i].name[0])
145 out += sprintf(out, "%s ", busid_table[i].name);
146 spin_unlock(&busid_table_lock);
147 out += sprintf(out, "\n");
152 static ssize_t store_match_busid(struct device_driver *dev, const char *buf,
156 char busid[BUSID_SIZE];
161 /* strnlen() does not include \0 */
162 len = strnlen(buf + 4, BUSID_SIZE);
164 /* busid needs to include \0 termination */
165 if (!(len < BUSID_SIZE))
168 strncpy(busid, buf + 4, BUSID_SIZE);
170 if (!strncmp(buf, "add ", 4)) {
171 if (add_match_busid(busid) < 0)
174 pr_debug("add busid %s\n", busid);
178 if (!strncmp(buf, "del ", 4)) {
179 if (del_match_busid(busid) < 0)
182 pr_debug("del busid %s\n", busid);
188 static DRIVER_ATTR(match_busid, S_IRUSR | S_IWUSR, show_match_busid,
191 static ssize_t rebind_store(struct device_driver *dev, const char *buf,
196 struct bus_id_priv *bid;
198 /* buf length should be less that BUSID_SIZE */
199 len = strnlen(buf, BUSID_SIZE);
201 if (!(len < BUSID_SIZE))
204 bid = get_busid_priv(buf);
208 ret = device_attach(&bid->udev->dev);
210 dev_err(&bid->udev->dev, "rebind failed\n");
217 static DRIVER_ATTR_WO(rebind);
219 static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
221 struct stub_priv *priv, *tmp;
223 list_for_each_entry_safe(priv, tmp, listhead, list) {
224 list_del(&priv->list);
231 static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
234 struct stub_priv *priv;
236 spin_lock_irqsave(&sdev->priv_lock, flags);
238 priv = stub_priv_pop_from_listhead(&sdev->priv_init);
242 priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
246 priv = stub_priv_pop_from_listhead(&sdev->priv_free);
249 spin_unlock_irqrestore(&sdev->priv_lock, flags);
254 void stub_device_cleanup_urbs(struct stub_device *sdev)
256 struct stub_priv *priv;
259 dev_dbg(&sdev->udev->dev, "free sdev %p\n", sdev);
261 while ((priv = stub_priv_pop(sdev))) {
263 dev_dbg(&sdev->udev->dev, "free urb %p\n", urb);
266 kmem_cache_free(stub_priv_cache, priv);
268 kfree(urb->transfer_buffer);
269 kfree(urb->setup_packet);
274 static int __init usbip_host_init(void)
280 stub_priv_cache = KMEM_CACHE(stub_priv, SLAB_HWCACHE_ALIGN);
281 if (!stub_priv_cache) {
282 pr_err("kmem_cache_create failed\n");
286 ret = usb_register_device_driver(&stub_driver, THIS_MODULE);
288 pr_err("usb_register failed %d\n", ret);
289 goto err_usb_register;
292 ret = driver_create_file(&stub_driver.drvwrap.driver,
293 &driver_attr_match_busid);
295 pr_err("driver_create_file failed\n");
296 goto err_create_file;
299 ret = driver_create_file(&stub_driver.drvwrap.driver,
300 &driver_attr_rebind);
302 pr_err("driver_create_file failed\n");
303 goto err_create_file;
306 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
310 usb_deregister_device_driver(&stub_driver);
312 kmem_cache_destroy(stub_priv_cache);
316 static void __exit usbip_host_exit(void)
318 driver_remove_file(&stub_driver.drvwrap.driver,
319 &driver_attr_match_busid);
321 driver_remove_file(&stub_driver.drvwrap.driver,
322 &driver_attr_rebind);
325 * deregister() calls stub_disconnect() for all devices. Device
326 * specific data is cleared in stub_disconnect().
328 usb_deregister_device_driver(&stub_driver);
330 kmem_cache_destroy(stub_priv_cache);
333 module_init(usbip_host_init);
334 module_exit(usbip_host_exit);
336 MODULE_AUTHOR(DRIVER_AUTHOR);
337 MODULE_DESCRIPTION(DRIVER_DESC);
338 MODULE_LICENSE("GPL");
339 MODULE_VERSION(USBIP_VERSION);