1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2003-2008 Takahiro Hirofuchi
6 #include <linux/string.h>
7 #include <linux/module.h>
8 #include <linux/device.h>
10 #include "usbip_common.h"
13 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
14 #define DRIVER_DESC "USB/IP Host Driver"
16 struct kmem_cache *stub_priv_cache;
18 * busid_tables defines matching busids that usbip can grab. A user can change
19 * dynamically what device is locally used and what device is exported to a
23 static struct bus_id_priv busid_table[MAX_BUSID];
24 static spinlock_t busid_table_lock;
26 static void init_busid_table(void)
29 * This also sets the bus_table[i].status to
30 * STUB_BUSID_OTHER, which is 0.
32 memset(busid_table, 0, sizeof(busid_table));
34 spin_lock_init(&busid_table_lock);
38 * Find the index of the busid by name.
39 * Must be called with busid_table_lock held.
41 static int get_busid_idx(const char *busid)
46 for (i = 0; i < MAX_BUSID; i++)
47 if (busid_table[i].name[0])
48 if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
55 struct bus_id_priv *get_busid_priv(const char *busid)
58 struct bus_id_priv *bid = NULL;
60 spin_lock(&busid_table_lock);
61 idx = get_busid_idx(busid);
63 bid = &(busid_table[idx]);
64 spin_unlock(&busid_table_lock);
69 static int add_match_busid(char *busid)
74 spin_lock(&busid_table_lock);
75 /* already registered? */
76 if (get_busid_idx(busid) >= 0) {
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;
92 spin_unlock(&busid_table_lock);
97 int del_match_busid(char *busid)
102 spin_lock(&busid_table_lock);
103 idx = get_busid_idx(busid);
110 if (busid_table[idx].status == STUB_BUSID_OTHER)
111 memset(busid_table[idx].name, 0, BUSID_SIZE);
113 if ((busid_table[idx].status != STUB_BUSID_OTHER) &&
114 (busid_table[idx].status != STUB_BUSID_ADDED))
115 busid_table[idx].status = STUB_BUSID_REMOV;
118 spin_unlock(&busid_table_lock);
123 static ssize_t match_busid_show(struct device_driver *drv, char *buf)
128 spin_lock(&busid_table_lock);
129 for (i = 0; i < MAX_BUSID; i++)
130 if (busid_table[i].name[0])
131 out += sprintf(out, "%s ", busid_table[i].name);
132 spin_unlock(&busid_table_lock);
133 out += sprintf(out, "\n");
138 static ssize_t match_busid_store(struct device_driver *dev, const char *buf,
142 char busid[BUSID_SIZE];
147 /* busid needs to include \0 termination */
148 len = strlcpy(busid, buf + 4, BUSID_SIZE);
149 if (sizeof(busid) <= len)
152 if (!strncmp(buf, "add ", 4)) {
153 if (add_match_busid(busid) < 0)
156 pr_debug("add busid %s\n", busid);
160 if (!strncmp(buf, "del ", 4)) {
161 if (del_match_busid(busid) < 0)
164 pr_debug("del busid %s\n", busid);
170 static DRIVER_ATTR_RW(match_busid);
172 static ssize_t rebind_store(struct device_driver *dev, const char *buf,
177 struct bus_id_priv *bid;
179 /* buf length should be less that BUSID_SIZE */
180 len = strnlen(buf, BUSID_SIZE);
182 if (!(len < BUSID_SIZE))
185 bid = get_busid_priv(buf);
189 ret = device_attach(&bid->udev->dev);
191 dev_err(&bid->udev->dev, "rebind failed\n");
198 static DRIVER_ATTR_WO(rebind);
200 static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
202 struct stub_priv *priv, *tmp;
204 list_for_each_entry_safe(priv, tmp, listhead, list) {
205 list_del(&priv->list);
212 static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
215 struct stub_priv *priv;
217 spin_lock_irqsave(&sdev->priv_lock, flags);
219 priv = stub_priv_pop_from_listhead(&sdev->priv_init);
223 priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
227 priv = stub_priv_pop_from_listhead(&sdev->priv_free);
230 spin_unlock_irqrestore(&sdev->priv_lock, flags);
235 void stub_device_cleanup_urbs(struct stub_device *sdev)
237 struct stub_priv *priv;
240 dev_dbg(&sdev->udev->dev, "free sdev %p\n", sdev);
242 while ((priv = stub_priv_pop(sdev))) {
244 dev_dbg(&sdev->udev->dev, "free urb %p\n", urb);
247 kmem_cache_free(stub_priv_cache, priv);
249 kfree(urb->transfer_buffer);
250 urb->transfer_buffer = NULL;
252 kfree(urb->setup_packet);
253 urb->setup_packet = NULL;
259 static int __init usbip_host_init(void)
265 stub_priv_cache = KMEM_CACHE(stub_priv, SLAB_HWCACHE_ALIGN);
266 if (!stub_priv_cache) {
267 pr_err("kmem_cache_create failed\n");
271 ret = usb_register_device_driver(&stub_driver, THIS_MODULE);
273 pr_err("usb_register failed %d\n", ret);
274 goto err_usb_register;
277 ret = driver_create_file(&stub_driver.drvwrap.driver,
278 &driver_attr_match_busid);
280 pr_err("driver_create_file failed\n");
281 goto err_create_file;
284 ret = driver_create_file(&stub_driver.drvwrap.driver,
285 &driver_attr_rebind);
287 pr_err("driver_create_file failed\n");
288 goto err_create_file;
294 usb_deregister_device_driver(&stub_driver);
296 kmem_cache_destroy(stub_priv_cache);
300 static void __exit usbip_host_exit(void)
302 driver_remove_file(&stub_driver.drvwrap.driver,
303 &driver_attr_match_busid);
305 driver_remove_file(&stub_driver.drvwrap.driver,
306 &driver_attr_rebind);
309 * deregister() calls stub_disconnect() for all devices. Device
310 * specific data is cleared in stub_disconnect().
312 usb_deregister_device_driver(&stub_driver);
314 kmem_cache_destroy(stub_priv_cache);
317 module_init(usbip_host_init);
318 module_exit(usbip_host_exit);
320 MODULE_AUTHOR(DRIVER_AUTHOR);
321 MODULE_DESCRIPTION(DRIVER_DESC);
322 MODULE_LICENSE("GPL");