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,
21 #include "usbip_common.h"
24 /* Version Information */
25 #define DRIVER_VERSION "1.0"
26 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
27 #define DRIVER_DESC "Stub Driver for USB/IP"
29 /* stub_priv is allocated from stub_priv_cache */
30 struct kmem_cache *stub_priv_cache;
32 /*-------------------------------------------------------------------------*/
34 /* Define sysfs entries for the usbip driver */
38 * busid_tables defines matching busids that usbip can grab. A user can change
39 * dynamically what device is locally used and what device is exported to a
43 static char busid_table[MAX_BUSID][BUS_ID_SIZE];
44 static spinlock_t busid_table_lock;
47 int match_busid(char *busid)
51 spin_lock(&busid_table_lock);
53 for (i = 0; i < MAX_BUSID; i++)
54 if (busid_table[i][0])
55 if (!strncmp(busid_table[i], busid, BUS_ID_SIZE)) {
56 /* already registerd */
57 spin_unlock(&busid_table_lock);
61 spin_unlock(&busid_table_lock);
66 static ssize_t show_match_busid(struct device_driver *drv, char *buf)
71 spin_lock(&busid_table_lock);
73 for (i = 0; i < MAX_BUSID; i++)
74 if (busid_table[i][0])
75 out += sprintf(out, "%s ", busid_table[i]);
77 spin_unlock(&busid_table_lock);
79 out += sprintf(out, "\n");
84 static int add_match_busid(char *busid)
88 if (!match_busid(busid))
91 spin_lock(&busid_table_lock);
93 for (i = 0; i < MAX_BUSID; i++)
94 if (!busid_table[i][0]) {
95 strncpy(busid_table[i], busid, BUS_ID_SIZE);
96 spin_unlock(&busid_table_lock);
100 spin_unlock(&busid_table_lock);
105 static int del_match_busid(char *busid)
109 spin_lock(&busid_table_lock);
111 for (i = 0; i < MAX_BUSID; i++)
112 if (!strncmp(busid_table[i], busid, BUS_ID_SIZE)) {
114 memset(busid_table[i], 0, BUS_ID_SIZE);
115 spin_unlock(&busid_table_lock);
119 spin_unlock(&busid_table_lock);
124 static ssize_t store_match_busid(struct device_driver *dev, const char *buf,
128 char busid[BUS_ID_SIZE];
133 /* strnlen() does not include \0 */
134 len = strnlen(buf + 4, BUS_ID_SIZE);
136 /* busid needs to include \0 termination */
137 if (!(len < BUS_ID_SIZE))
140 strncpy(busid, buf + 4, BUS_ID_SIZE);
143 if (!strncmp(buf, "add ", 4)) {
144 if (add_match_busid(busid) < 0)
147 udbg("add busid %s\n", busid);
150 } else if (!strncmp(buf, "del ", 4)) {
151 if (del_match_busid(busid) < 0)
154 udbg("del busid %s\n", busid);
161 static DRIVER_ATTR(match_busid, S_IRUSR|S_IWUSR, show_match_busid,
166 /*-------------------------------------------------------------------------*/
168 /* Cleanup functions used to free private data */
170 static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
172 struct stub_priv *priv, *tmp;
174 list_for_each_entry_safe(priv, tmp, listhead, list) {
175 list_del(&priv->list);
182 static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
185 struct stub_priv *priv;
187 spin_lock_irqsave(&sdev->priv_lock, flags);
189 priv = stub_priv_pop_from_listhead(&sdev->priv_init);
191 spin_unlock_irqrestore(&sdev->priv_lock, flags);
195 priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
197 spin_unlock_irqrestore(&sdev->priv_lock, flags);
201 priv = stub_priv_pop_from_listhead(&sdev->priv_free);
203 spin_unlock_irqrestore(&sdev->priv_lock, flags);
207 spin_unlock_irqrestore(&sdev->priv_lock, flags);
211 void stub_device_cleanup_urbs(struct stub_device *sdev)
213 struct stub_priv *priv;
215 udbg("free sdev %p\n", sdev);
217 while ((priv = stub_priv_pop(sdev))) {
218 struct urb *urb = priv->urb;
220 udbg(" free urb %p\n", urb);
223 kmem_cache_free(stub_priv_cache, priv);
225 if (urb->transfer_buffer != NULL)
226 kfree(urb->transfer_buffer);
228 if (urb->setup_packet != NULL)
229 kfree(urb->setup_packet);
236 /*-------------------------------------------------------------------------*/
238 static int __init usb_stub_init(void)
242 stub_priv_cache = kmem_cache_create("stub_priv",
243 sizeof(struct stub_priv), 0,
244 SLAB_HWCACHE_ALIGN, NULL);
246 if (!stub_priv_cache) {
247 printk(KERN_ERR KBUILD_MODNAME
248 ": create stub_priv_cache error\n");
252 ret = usb_register(&stub_driver);
254 printk(KERN_ERR KBUILD_MODNAME ": usb_register failed %d\n",
256 goto error_usb_register;
259 printk(KERN_INFO KBUILD_MODNAME ":"
260 DRIVER_DESC ":" DRIVER_VERSION "\n");
262 memset(busid_table, 0, sizeof(busid_table));
263 spin_lock_init(&busid_table_lock);
265 ret = driver_create_file(&stub_driver.drvwrap.driver,
266 &driver_attr_match_busid);
269 printk(KERN_ERR KBUILD_MODNAME ": create driver sysfs\n");
270 goto error_create_file;
275 usb_deregister(&stub_driver);
277 kmem_cache_destroy(stub_priv_cache);
281 static void __exit usb_stub_exit(void)
283 driver_remove_file(&stub_driver.drvwrap.driver,
284 &driver_attr_match_busid);
287 * deregister() calls stub_disconnect() for all devices. Device
288 * specific data is cleared in stub_disconnect().
290 usb_deregister(&stub_driver);
292 kmem_cache_destroy(stub_priv_cache);
295 module_init(usb_stub_init);
296 module_exit(usb_stub_exit);
298 MODULE_AUTHOR(DRIVER_AUTHOR);
299 MODULE_DESCRIPTION(DRIVER_DESC);
300 MODULE_LICENSE("GPL");