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
;
33 * busid_tables defines matching busids that usbip can grab. A user can change
34 * dynamically what device is locally used and what device is exported to a
38 static struct bus_id_priv busid_table
[MAX_BUSID
];
39 static spinlock_t busid_table_lock
;
41 static void init_busid_table(void)
46 * This also sets the bus_table[i].status to
47 * STUB_BUSID_OTHER, which is 0.
49 memset(busid_table
, 0, sizeof(busid_table
));
51 spin_lock_init(&busid_table_lock
);
53 for (i
= 0; i
< MAX_BUSID
; i
++)
54 spin_lock_init(&busid_table
[i
].busid_lock
);
58 * Find the index of the busid by name.
59 * Must be called with busid_table_lock held.
61 static int get_busid_idx(const char *busid
)
66 for (i
= 0; i
< MAX_BUSID
; i
++) {
67 spin_lock(&busid_table
[i
].busid_lock
);
68 if (busid_table
[i
].name
[0])
69 if (!strncmp(busid_table
[i
].name
, busid
, BUSID_SIZE
)) {
71 spin_unlock(&busid_table
[i
].busid_lock
);
74 spin_unlock(&busid_table
[i
].busid_lock
);
79 /* Returns holding busid_lock. Should call put_busid_priv() to unlock */
80 struct bus_id_priv
*get_busid_priv(const char *busid
)
83 struct bus_id_priv
*bid
= NULL
;
85 spin_lock(&busid_table_lock
);
86 idx
= get_busid_idx(busid
);
88 bid
= &(busid_table
[idx
]);
89 /* get busid_lock before returning */
90 spin_lock(&bid
->busid_lock
);
92 spin_unlock(&busid_table_lock
);
97 void put_busid_priv(struct bus_id_priv
*bid
)
100 spin_unlock(&bid
->busid_lock
);
103 static int add_match_busid(char *busid
)
108 spin_lock(&busid_table_lock
);
109 /* already registered? */
110 if (get_busid_idx(busid
) >= 0) {
115 for (i
= 0; i
< MAX_BUSID
; i
++) {
116 spin_lock(&busid_table
[i
].busid_lock
);
117 if (!busid_table
[i
].name
[0]) {
118 strlcpy(busid_table
[i
].name
, busid
, BUSID_SIZE
);
119 if ((busid_table
[i
].status
!= STUB_BUSID_ALLOC
) &&
120 (busid_table
[i
].status
!= STUB_BUSID_REMOV
))
121 busid_table
[i
].status
= STUB_BUSID_ADDED
;
123 spin_unlock(&busid_table
[i
].busid_lock
);
126 spin_unlock(&busid_table
[i
].busid_lock
);
130 spin_unlock(&busid_table_lock
);
135 int del_match_busid(char *busid
)
140 spin_lock(&busid_table_lock
);
141 idx
= get_busid_idx(busid
);
148 spin_lock(&busid_table
[idx
].busid_lock
);
150 if (busid_table
[idx
].status
== STUB_BUSID_OTHER
)
151 memset(busid_table
[idx
].name
, 0, BUSID_SIZE
);
153 if ((busid_table
[idx
].status
!= STUB_BUSID_OTHER
) &&
154 (busid_table
[idx
].status
!= STUB_BUSID_ADDED
))
155 busid_table
[idx
].status
= STUB_BUSID_REMOV
;
157 spin_unlock(&busid_table
[idx
].busid_lock
);
159 spin_unlock(&busid_table_lock
);
164 static ssize_t
show_match_busid(struct device_driver
*drv
, char *buf
)
169 spin_lock(&busid_table_lock
);
170 for (i
= 0; i
< MAX_BUSID
; i
++) {
171 spin_lock(&busid_table
[i
].busid_lock
);
172 if (busid_table
[i
].name
[0])
173 out
+= sprintf(out
, "%s ", busid_table
[i
].name
);
174 spin_unlock(&busid_table
[i
].busid_lock
);
176 spin_unlock(&busid_table_lock
);
177 out
+= sprintf(out
, "\n");
182 static ssize_t
store_match_busid(struct device_driver
*dev
, const char *buf
,
186 char busid
[BUSID_SIZE
];
191 /* busid needs to include \0 termination */
192 len
= strlcpy(busid
, buf
+ 4, BUSID_SIZE
);
193 if (sizeof(busid
) <= len
)
196 if (!strncmp(buf
, "add ", 4)) {
197 if (add_match_busid(busid
) < 0)
200 pr_debug("add busid %s\n", busid
);
204 if (!strncmp(buf
, "del ", 4)) {
205 if (del_match_busid(busid
) < 0)
208 pr_debug("del busid %s\n", busid
);
214 static DRIVER_ATTR(match_busid
, S_IRUSR
| S_IWUSR
, show_match_busid
,
217 static int do_rebind(char *busid
, struct bus_id_priv
*busid_priv
)
221 /* device_attach() callers should hold parent lock for USB */
222 if (busid_priv
->udev
->dev
.parent
)
223 device_lock(busid_priv
->udev
->dev
.parent
);
224 ret
= device_attach(&busid_priv
->udev
->dev
);
225 if (busid_priv
->udev
->dev
.parent
)
226 device_unlock(busid_priv
->udev
->dev
.parent
);
228 dev_err(&busid_priv
->udev
->dev
, "rebind failed\n");
234 static void stub_device_rebind(void)
236 #if IS_MODULE(CONFIG_USBIP_HOST)
237 struct bus_id_priv
*busid_priv
;
240 /* update status to STUB_BUSID_OTHER so probe ignores the device */
241 spin_lock(&busid_table_lock
);
242 for (i
= 0; i
< MAX_BUSID
; i
++) {
243 if (busid_table
[i
].name
[0] &&
244 busid_table
[i
].shutdown_busid
) {
245 busid_priv
= &(busid_table
[i
]);
246 busid_priv
->status
= STUB_BUSID_OTHER
;
249 spin_unlock(&busid_table_lock
);
251 /* now run rebind - no need to hold locks. driver files are removed */
252 for (i
= 0; i
< MAX_BUSID
; i
++) {
253 if (busid_table
[i
].name
[0] &&
254 busid_table
[i
].shutdown_busid
) {
255 busid_priv
= &(busid_table
[i
]);
256 do_rebind(busid_table
[i
].name
, busid_priv
);
262 static ssize_t
rebind_store(struct device_driver
*dev
, const char *buf
,
267 struct bus_id_priv
*bid
;
269 /* buf length should be less that BUSID_SIZE */
270 len
= strnlen(buf
, BUSID_SIZE
);
272 if (!(len
< BUSID_SIZE
))
275 bid
= get_busid_priv(buf
);
279 /* mark the device for deletion so probe ignores it during rescan */
280 bid
->status
= STUB_BUSID_OTHER
;
281 /* release the busid lock */
284 ret
= do_rebind((char *) buf
, bid
);
288 /* delete device from busid_table */
289 del_match_busid((char *) buf
);
294 static DRIVER_ATTR_WO(rebind
);
296 static struct stub_priv
*stub_priv_pop_from_listhead(struct list_head
*listhead
)
298 struct stub_priv
*priv
, *tmp
;
300 list_for_each_entry_safe(priv
, tmp
, listhead
, list
) {
301 list_del(&priv
->list
);
308 static struct stub_priv
*stub_priv_pop(struct stub_device
*sdev
)
311 struct stub_priv
*priv
;
313 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
315 priv
= stub_priv_pop_from_listhead(&sdev
->priv_init
);
319 priv
= stub_priv_pop_from_listhead(&sdev
->priv_tx
);
323 priv
= stub_priv_pop_from_listhead(&sdev
->priv_free
);
326 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
331 void stub_device_cleanup_urbs(struct stub_device
*sdev
)
333 struct stub_priv
*priv
;
336 dev_dbg(&sdev
->udev
->dev
, "Stub device cleaning up urbs\n");
338 while ((priv
= stub_priv_pop(sdev
))) {
340 dev_dbg(&sdev
->udev
->dev
, "free urb seqnum %lu\n",
344 kmem_cache_free(stub_priv_cache
, priv
);
346 kfree(urb
->transfer_buffer
);
347 urb
->transfer_buffer
= NULL
;
349 kfree(urb
->setup_packet
);
350 urb
->setup_packet
= NULL
;
356 static int __init
usbip_host_init(void)
362 stub_priv_cache
= KMEM_CACHE(stub_priv
, SLAB_HWCACHE_ALIGN
);
363 if (!stub_priv_cache
) {
364 pr_err("kmem_cache_create failed\n");
368 ret
= usb_register_device_driver(&stub_driver
, THIS_MODULE
);
370 pr_err("usb_register failed %d\n", ret
);
371 goto err_usb_register
;
374 ret
= driver_create_file(&stub_driver
.drvwrap
.driver
,
375 &driver_attr_match_busid
);
377 pr_err("driver_create_file failed\n");
378 goto err_create_file
;
381 ret
= driver_create_file(&stub_driver
.drvwrap
.driver
,
382 &driver_attr_rebind
);
384 pr_err("driver_create_file failed\n");
385 goto err_create_file
;
388 pr_info(DRIVER_DESC
" v" USBIP_VERSION
"\n");
392 usb_deregister_device_driver(&stub_driver
);
394 kmem_cache_destroy(stub_priv_cache
);
398 static void __exit
usbip_host_exit(void)
400 driver_remove_file(&stub_driver
.drvwrap
.driver
,
401 &driver_attr_match_busid
);
403 driver_remove_file(&stub_driver
.drvwrap
.driver
,
404 &driver_attr_rebind
);
407 * deregister() calls stub_disconnect() for all devices. Device
408 * specific data is cleared in stub_disconnect().
410 usb_deregister_device_driver(&stub_driver
);
412 /* initiate scan to attach devices */
413 stub_device_rebind();
415 kmem_cache_destroy(stub_priv_cache
);
418 module_init(usbip_host_init
);
419 module_exit(usbip_host_exit
);
421 MODULE_AUTHOR(DRIVER_AUTHOR
);
422 MODULE_DESCRIPTION(DRIVER_DESC
);
423 MODULE_LICENSE("GPL");
424 MODULE_VERSION(USBIP_VERSION
);