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>
22 #include "usbip_common.h"
25 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
26 #define DRIVER_DESC "USB/IP Host Driver"
28 /* stub_priv is allocated from stub_priv_cache */
29 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 int match_busid(const char *busid
)
44 spin_lock(&busid_table_lock
);
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
)) {
49 /* already registerd */
50 spin_unlock(&busid_table_lock
);
54 spin_unlock(&busid_table_lock
);
59 struct bus_id_priv
*get_busid_priv(const char *busid
)
63 spin_lock(&busid_table_lock
);
65 for (i
= 0; i
< MAX_BUSID
; i
++)
66 if (busid_table
[i
].name
[0])
67 if (!strncmp(busid_table
[i
].name
, busid
, BUSID_SIZE
)) {
68 /* already registerd */
69 spin_unlock(&busid_table_lock
);
70 return &(busid_table
[i
]);
73 spin_unlock(&busid_table_lock
);
78 static ssize_t
show_match_busid(struct device_driver
*drv
, char *buf
)
83 spin_lock(&busid_table_lock
);
85 for (i
= 0; i
< MAX_BUSID
; i
++)
86 if (busid_table
[i
].name
[0])
87 out
+= sprintf(out
, "%s ", busid_table
[i
].name
);
89 spin_unlock(&busid_table_lock
);
91 out
+= sprintf(out
, "\n");
96 static int add_match_busid(char *busid
)
100 if (!match_busid(busid
))
103 spin_lock(&busid_table_lock
);
105 for (i
= 0; i
< MAX_BUSID
; i
++)
106 if (!busid_table
[i
].name
[0]) {
107 strncpy(busid_table
[i
].name
, busid
, BUSID_SIZE
);
108 if ((busid_table
[i
].status
!= STUB_BUSID_ALLOC
) &&
109 (busid_table
[i
].status
!= STUB_BUSID_REMOV
))
110 busid_table
[i
].status
= STUB_BUSID_ADDED
;
111 spin_unlock(&busid_table_lock
);
115 spin_unlock(&busid_table_lock
);
120 int del_match_busid(char *busid
)
124 spin_lock(&busid_table_lock
);
126 for (i
= 0; i
< MAX_BUSID
; i
++)
127 if (!strncmp(busid_table
[i
].name
, busid
, BUSID_SIZE
)) {
129 if (busid_table
[i
].status
== STUB_BUSID_OTHER
)
130 memset(busid_table
[i
].name
, 0, BUSID_SIZE
);
131 if ((busid_table
[i
].status
!= STUB_BUSID_OTHER
) &&
132 (busid_table
[i
].status
!= STUB_BUSID_ADDED
)) {
133 busid_table
[i
].status
= STUB_BUSID_REMOV
;
135 spin_unlock(&busid_table_lock
);
139 spin_unlock(&busid_table_lock
);
144 static void init_busid_table(void)
148 for (i
= 0; i
< MAX_BUSID
; i
++) {
149 memset(busid_table
[i
].name
, 0, BUSID_SIZE
);
150 busid_table
[i
].status
= STUB_BUSID_OTHER
;
151 busid_table
[i
].interf_count
= 0;
152 busid_table
[i
].sdev
= NULL
;
153 busid_table
[i
].shutdown_busid
= 0;
156 spin_lock_init(&busid_table_lock
);
159 static ssize_t
store_match_busid(struct device_driver
*dev
, const char *buf
,
163 char busid
[BUSID_SIZE
];
168 /* strnlen() does not include \0 */
169 len
= strnlen(buf
+ 4, BUSID_SIZE
);
171 /* busid needs to include \0 termination */
172 if (!(len
< BUSID_SIZE
))
175 strncpy(busid
, buf
+ 4, BUSID_SIZE
);
177 if (!strncmp(buf
, "add ", 4)) {
178 if (add_match_busid(busid
) < 0)
181 pr_debug("add busid %s\n", busid
);
184 } else if (!strncmp(buf
, "del ", 4)) {
185 if (del_match_busid(busid
) < 0)
188 pr_debug("del busid %s\n", busid
);
194 static DRIVER_ATTR(match_busid
, S_IRUSR
|S_IWUSR
, show_match_busid
,
197 static struct stub_priv
*stub_priv_pop_from_listhead(struct list_head
*listhead
)
199 struct stub_priv
*priv
, *tmp
;
201 list_for_each_entry_safe(priv
, tmp
, listhead
, list
) {
202 list_del(&priv
->list
);
209 static struct stub_priv
*stub_priv_pop(struct stub_device
*sdev
)
212 struct stub_priv
*priv
;
214 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
216 priv
= stub_priv_pop_from_listhead(&sdev
->priv_init
);
218 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
222 priv
= stub_priv_pop_from_listhead(&sdev
->priv_tx
);
224 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
228 priv
= stub_priv_pop_from_listhead(&sdev
->priv_free
);
230 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
234 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
238 void stub_device_cleanup_urbs(struct stub_device
*sdev
)
240 struct stub_priv
*priv
;
242 dev_dbg(&sdev
->udev
->dev
, "free sdev %p\n", sdev
);
244 while ((priv
= stub_priv_pop(sdev
))) {
245 struct urb
*urb
= priv
->urb
;
247 dev_dbg(&sdev
->udev
->dev
, "free urb %p\n", urb
);
250 kmem_cache_free(stub_priv_cache
, priv
);
252 kfree(urb
->transfer_buffer
);
253 kfree(urb
->setup_packet
);
259 static int __init
usb_stub_init(void)
263 stub_priv_cache
= kmem_cache_create("stub_priv",
264 sizeof(struct stub_priv
), 0,
265 SLAB_HWCACHE_ALIGN
, NULL
);
267 if (!stub_priv_cache
) {
268 pr_err("create stub_priv_cache error\n");
272 ret
= usb_register(&stub_driver
);
274 pr_err("usb_register failed %d\n", ret
);
275 goto error_usb_register
;
278 pr_info(DRIVER_DESC
" " USBIP_VERSION
"\n");
282 ret
= driver_create_file(&stub_driver
.drvwrap
.driver
,
283 &driver_attr_match_busid
);
286 pr_err("create driver sysfs\n");
287 goto error_create_file
;
292 usb_deregister(&stub_driver
);
294 kmem_cache_destroy(stub_priv_cache
);
298 static void __exit
usb_stub_exit(void)
300 driver_remove_file(&stub_driver
.drvwrap
.driver
,
301 &driver_attr_match_busid
);
304 * deregister() calls stub_disconnect() for all devices. Device
305 * specific data is cleared in stub_disconnect().
307 usb_deregister(&stub_driver
);
309 kmem_cache_destroy(stub_priv_cache
);
312 module_init(usb_stub_init
);
313 module_exit(usb_stub_exit
);
315 MODULE_AUTHOR(DRIVER_AUTHOR
);
316 MODULE_DESCRIPTION(DRIVER_DESC
);
317 MODULE_LICENSE("GPL");
318 MODULE_VERSION(USBIP_VERSION
);