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/slab.h>
22 #include "usbip_common.h"
25 /* Version Information */
26 #define DRIVER_VERSION "1.0"
27 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
28 #define DRIVER_DESC "Stub Driver for USB/IP"
30 /* stub_priv is allocated from stub_priv_cache */
31 struct kmem_cache
*stub_priv_cache
;
33 /*-------------------------------------------------------------------------*/
35 /* Define sysfs entries for the usbip driver */
39 * busid_tables defines matching busids that usbip can grab. A user can change
40 * dynamically what device is locally used and what device is exported to a
44 static struct bus_id_priv busid_table
[MAX_BUSID
];
45 static spinlock_t busid_table_lock
;
48 int match_busid(const char *busid
)
52 spin_lock(&busid_table_lock
);
54 for (i
= 0; i
< MAX_BUSID
; i
++)
55 if (busid_table
[i
].name
[0])
56 if (!strncmp(busid_table
[i
].name
, busid
, BUSID_SIZE
)) {
57 /* already registerd */
58 spin_unlock(&busid_table_lock
);
62 spin_unlock(&busid_table_lock
);
67 struct bus_id_priv
*get_busid_priv(const char *busid
)
71 spin_lock(&busid_table_lock
);
73 for (i
= 0; i
< MAX_BUSID
; i
++)
74 if (busid_table
[i
].name
[0])
75 if (!strncmp(busid_table
[i
].name
, busid
, BUSID_SIZE
)) {
76 /* already registerd */
77 spin_unlock(&busid_table_lock
);
78 return &(busid_table
[i
]);
81 spin_unlock(&busid_table_lock
);
86 static ssize_t
show_match_busid(struct device_driver
*drv
, char *buf
)
91 spin_lock(&busid_table_lock
);
93 for (i
= 0; i
< MAX_BUSID
; i
++)
94 if (busid_table
[i
].name
[0])
95 out
+= sprintf(out
, "%s ", busid_table
[i
].name
);
97 spin_unlock(&busid_table_lock
);
99 out
+= sprintf(out
, "\n");
104 static int add_match_busid(char *busid
)
108 if (!match_busid(busid
))
111 spin_lock(&busid_table_lock
);
113 for (i
= 0; i
< MAX_BUSID
; i
++)
114 if (!busid_table
[i
].name
[0]) {
115 strncpy(busid_table
[i
].name
, busid
, BUSID_SIZE
);
116 if ((busid_table
[i
].status
!= STUB_BUSID_ALLOC
) &&
117 (busid_table
[i
].status
!= STUB_BUSID_REMOV
))
118 busid_table
[i
].status
= STUB_BUSID_ADDED
;
119 spin_unlock(&busid_table_lock
);
123 spin_unlock(&busid_table_lock
);
128 int del_match_busid(char *busid
)
132 spin_lock(&busid_table_lock
);
134 for (i
= 0; i
< MAX_BUSID
; i
++)
135 if (!strncmp(busid_table
[i
].name
, busid
, BUSID_SIZE
)) {
137 if (busid_table
[i
].status
== STUB_BUSID_OTHER
)
138 memset(busid_table
[i
].name
, 0, BUSID_SIZE
);
139 if ((busid_table
[i
].status
!= STUB_BUSID_OTHER
) &&
140 (busid_table
[i
].status
!= STUB_BUSID_ADDED
)) {
141 busid_table
[i
].status
= STUB_BUSID_REMOV
;
143 spin_unlock(&busid_table_lock
);
147 spin_unlock(&busid_table_lock
);
151 static void init_busid_table(void)
156 for (i
= 0; i
< MAX_BUSID
; i
++) {
157 memset(busid_table
[i
].name
, 0, BUSID_SIZE
);
158 busid_table
[i
].status
= STUB_BUSID_OTHER
;
159 busid_table
[i
].interf_count
= 0;
160 busid_table
[i
].sdev
= NULL
;
161 busid_table
[i
].shutdown_busid
= 0;
163 spin_lock_init(&busid_table_lock
);
166 static ssize_t
store_match_busid(struct device_driver
*dev
, const char *buf
,
170 char busid
[BUSID_SIZE
];
175 /* strnlen() does not include \0 */
176 len
= strnlen(buf
+ 4, BUSID_SIZE
);
178 /* busid needs to include \0 termination */
179 if (!(len
< BUSID_SIZE
))
182 strncpy(busid
, buf
+ 4, BUSID_SIZE
);
185 if (!strncmp(buf
, "add ", 4)) {
186 if (add_match_busid(busid
) < 0)
189 usbip_udbg("add busid %s\n", busid
);
192 } else if (!strncmp(buf
, "del ", 4)) {
193 if (del_match_busid(busid
) < 0)
196 usbip_udbg("del busid %s\n", busid
);
203 static DRIVER_ATTR(match_busid
, S_IRUSR
|S_IWUSR
, show_match_busid
,
208 /*-------------------------------------------------------------------------*/
210 /* Cleanup functions used to free private data */
212 static struct stub_priv
*stub_priv_pop_from_listhead(struct list_head
*listhead
)
214 struct stub_priv
*priv
, *tmp
;
216 list_for_each_entry_safe(priv
, tmp
, listhead
, list
) {
217 list_del(&priv
->list
);
224 static struct stub_priv
*stub_priv_pop(struct stub_device
*sdev
)
227 struct stub_priv
*priv
;
229 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
231 priv
= stub_priv_pop_from_listhead(&sdev
->priv_init
);
233 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
237 priv
= stub_priv_pop_from_listhead(&sdev
->priv_tx
);
239 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
243 priv
= stub_priv_pop_from_listhead(&sdev
->priv_free
);
245 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
249 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
253 void stub_device_cleanup_urbs(struct stub_device
*sdev
)
255 struct stub_priv
*priv
;
257 usbip_udbg("free sdev %p\n", sdev
);
259 while ((priv
= stub_priv_pop(sdev
))) {
260 struct urb
*urb
= priv
->urb
;
262 usbip_udbg(" free urb %p\n", urb
);
265 kmem_cache_free(stub_priv_cache
, priv
);
267 if (urb
->transfer_buffer
!= NULL
)
268 kfree(urb
->transfer_buffer
);
270 if (urb
->setup_packet
!= NULL
)
271 kfree(urb
->setup_packet
);
278 /*-------------------------------------------------------------------------*/
280 static int __init
usb_stub_init(void)
284 stub_priv_cache
= kmem_cache_create("stub_priv",
285 sizeof(struct stub_priv
), 0,
286 SLAB_HWCACHE_ALIGN
, NULL
);
288 if (!stub_priv_cache
) {
289 printk(KERN_ERR KBUILD_MODNAME
290 ": create stub_priv_cache error\n");
294 ret
= usb_register(&stub_driver
);
296 printk(KERN_ERR KBUILD_MODNAME
": usb_register failed %d\n",
298 goto error_usb_register
;
301 printk(KERN_INFO KBUILD_MODNAME
":"
302 DRIVER_DESC
":" DRIVER_VERSION
"\n");
306 ret
= driver_create_file(&stub_driver
.drvwrap
.driver
,
307 &driver_attr_match_busid
);
310 printk(KERN_ERR KBUILD_MODNAME
": create driver sysfs\n");
311 goto error_create_file
;
316 usb_deregister(&stub_driver
);
318 kmem_cache_destroy(stub_priv_cache
);
322 static void __exit
usb_stub_exit(void)
324 driver_remove_file(&stub_driver
.drvwrap
.driver
,
325 &driver_attr_match_busid
);
328 * deregister() calls stub_disconnect() for all devices. Device
329 * specific data is cleared in stub_disconnect().
331 usb_deregister(&stub_driver
);
333 kmem_cache_destroy(stub_priv_cache
);
336 module_init(usb_stub_init
);
337 module_exit(usb_stub_exit
);
339 MODULE_AUTHOR(DRIVER_AUTHOR
);
340 MODULE_DESCRIPTION(DRIVER_DESC
);
341 MODULE_LICENSE("GPL");