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/device.h>
21 #include <linux/kthread.h>
23 #include "usbip_common.h"
27 * Define device IDs here if you want to explicitly limit exportable devices.
28 * In most cases, wildcard matching will be okay because driver binding can be
29 * changed dynamically by a userland program.
31 static struct usb_device_id stub_table
[] = {
34 { USB_DEVICE(0x05ac, 0x0301) }, /* Mac 1 button mouse */
35 { USB_DEVICE(0x0430, 0x0009) }, /* Plat Home Keyboard */
36 { USB_DEVICE(0x059b, 0x0001) }, /* Iomega USB Zip 100 */
37 { USB_DEVICE(0x04b3, 0x4427) }, /* IBM USB CD-ROM */
38 { USB_DEVICE(0x05a9, 0xa511) }, /* LifeView USB cam */
39 { USB_DEVICE(0x55aa, 0x0201) }, /* Imation card reader */
40 { USB_DEVICE(0x046d, 0x0870) }, /* Qcam Express(QV-30) */
41 { USB_DEVICE(0x04bb, 0x0101) }, /* IO-DATA HD 120GB */
42 { USB_DEVICE(0x04bb, 0x0904) }, /* IO-DATA USB-ET/TX */
43 { USB_DEVICE(0x04bb, 0x0201) }, /* IO-DATA USB-ET/TX */
44 { USB_DEVICE(0x08bb, 0x2702) }, /* ONKYO USB Speaker */
45 { USB_DEVICE(0x046d, 0x08b2) }, /* Logicool Qcam 4000 Pro */
47 /* magic for wild card */
49 { 0, } /* Terminating entry */
51 MODULE_DEVICE_TABLE(usb
, stub_table
);
54 * usbip_status shows the status of usbip-host as long as this driver is bound
55 * to the target device.
57 static ssize_t
show_status(struct device
*dev
, struct device_attribute
*attr
,
60 struct stub_device
*sdev
= dev_get_drvdata(dev
);
64 dev_err(dev
, "sdev is null\n");
68 spin_lock(&sdev
->ud
.lock
);
69 status
= sdev
->ud
.status
;
70 spin_unlock(&sdev
->ud
.lock
);
72 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
74 static DEVICE_ATTR(usbip_status
, S_IRUGO
, show_status
, NULL
);
77 * usbip_sockfd gets a socket descriptor of an established TCP connection that
78 * is used to transfer usbip requests by kernel threads. -1 is a magic number
79 * by which usbip connection is finished.
81 static ssize_t
store_sockfd(struct device
*dev
, struct device_attribute
*attr
,
82 const char *buf
, size_t count
)
84 struct stub_device
*sdev
= dev_get_drvdata(dev
);
86 struct socket
*socket
;
89 dev_err(dev
, "sdev is null\n");
93 sscanf(buf
, "%d", &sockfd
);
96 dev_info(dev
, "stub up\n");
98 spin_lock(&sdev
->ud
.lock
);
100 if (sdev
->ud
.status
!= SDEV_ST_AVAILABLE
) {
101 dev_err(dev
, "not ready\n");
102 spin_unlock(&sdev
->ud
.lock
);
106 socket
= sockfd_to_socket(sockfd
);
108 spin_unlock(&sdev
->ud
.lock
);
113 setkeepalive(socket
);
116 sdev
->ud
.tcp_socket
= socket
;
118 spin_unlock(&sdev
->ud
.lock
);
120 sdev
->ud
.tcp_rx
= kthread_run(stub_rx_loop
, &sdev
->ud
, "stub_rx");
121 sdev
->ud
.tcp_tx
= kthread_run(stub_tx_loop
, &sdev
->ud
, "stub_tx");
123 spin_lock(&sdev
->ud
.lock
);
124 sdev
->ud
.status
= SDEV_ST_USED
;
125 spin_unlock(&sdev
->ud
.lock
);
128 dev_info(dev
, "stub down\n");
130 spin_lock(&sdev
->ud
.lock
);
131 if (sdev
->ud
.status
!= SDEV_ST_USED
) {
132 spin_unlock(&sdev
->ud
.lock
);
135 spin_unlock(&sdev
->ud
.lock
);
137 usbip_event_add(&sdev
->ud
, SDEV_EVENT_DOWN
);
142 static DEVICE_ATTR(usbip_sockfd
, S_IWUSR
, NULL
, store_sockfd
);
144 static int stub_add_files(struct device
*dev
)
148 err
= device_create_file(dev
, &dev_attr_usbip_status
);
152 err
= device_create_file(dev
, &dev_attr_usbip_sockfd
);
156 err
= device_create_file(dev
, &dev_attr_usbip_debug
);
163 device_remove_file(dev
, &dev_attr_usbip_sockfd
);
165 device_remove_file(dev
, &dev_attr_usbip_status
);
170 static void stub_remove_files(struct device
*dev
)
172 device_remove_file(dev
, &dev_attr_usbip_status
);
173 device_remove_file(dev
, &dev_attr_usbip_sockfd
);
174 device_remove_file(dev
, &dev_attr_usbip_debug
);
177 static void stub_shutdown_connection(struct usbip_device
*ud
)
179 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
182 * When removing an exported device, kernel panic sometimes occurred
183 * and then EIP was sk_wait_data of stub_rx thread. Is this because
184 * sk_wait_data returned though stub_rx thread was already finished by
187 if (ud
->tcp_socket
) {
188 dev_dbg(&sdev
->udev
->dev
, "shutdown tcp_socket %p\n",
190 kernel_sock_shutdown(ud
->tcp_socket
, SHUT_RDWR
);
193 /* 1. stop threads */
194 if (ud
->tcp_rx
&& !task_is_dead(ud
->tcp_rx
))
195 kthread_stop(ud
->tcp_rx
);
196 if (ud
->tcp_tx
&& !task_is_dead(ud
->tcp_tx
))
197 kthread_stop(ud
->tcp_tx
);
200 * 2. close the socket
202 * tcp_socket is freed after threads are killed so that usbip_xmit does
203 * not touch NULL socket.
205 if (ud
->tcp_socket
) {
206 sock_release(ud
->tcp_socket
);
207 ud
->tcp_socket
= NULL
;
210 /* 3. free used data */
211 stub_device_cleanup_urbs(sdev
);
213 /* 4. free stub_unlink */
216 struct stub_unlink
*unlink
, *tmp
;
218 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
219 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_tx
, list
) {
220 list_del(&unlink
->list
);
223 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_free
,
225 list_del(&unlink
->list
);
228 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
232 static void stub_device_reset(struct usbip_device
*ud
)
234 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
235 struct usb_device
*udev
= sdev
->udev
;
238 dev_dbg(&udev
->dev
, "device reset");
240 ret
= usb_lock_device_for_reset(udev
, sdev
->interface
);
242 dev_err(&udev
->dev
, "lock for reset\n");
243 spin_lock(&ud
->lock
);
244 ud
->status
= SDEV_ST_ERROR
;
245 spin_unlock(&ud
->lock
);
249 /* try to reset the device */
250 ret
= usb_reset_device(udev
);
251 usb_unlock_device(udev
);
253 spin_lock(&ud
->lock
);
255 dev_err(&udev
->dev
, "device reset\n");
256 ud
->status
= SDEV_ST_ERROR
;
258 dev_info(&udev
->dev
, "device reset\n");
259 ud
->status
= SDEV_ST_AVAILABLE
;
261 spin_unlock(&ud
->lock
);
264 static void stub_device_unusable(struct usbip_device
*ud
)
266 spin_lock(&ud
->lock
);
267 ud
->status
= SDEV_ST_ERROR
;
268 spin_unlock(&ud
->lock
);
272 * stub_device_alloc - allocate a new stub_device struct
273 * @interface: usb_interface of a new device
275 * Allocates and initializes a new stub_device struct.
277 static struct stub_device
*stub_device_alloc(struct usb_device
*udev
,
278 struct usb_interface
*interface
)
280 struct stub_device
*sdev
;
281 int busnum
= interface_to_busnum(interface
);
282 int devnum
= interface_to_devnum(interface
);
284 dev_dbg(&interface
->dev
, "allocating stub device");
286 /* yes, it's a new device */
287 sdev
= kzalloc(sizeof(struct stub_device
), GFP_KERNEL
);
289 dev_err(&interface
->dev
, "no memory for stub_device\n");
293 sdev
->interface
= usb_get_intf(interface
);
294 sdev
->udev
= usb_get_dev(udev
);
297 * devid is defined with devnum when this driver is first allocated.
298 * devnum may change later if a device is reset. However, devid never
299 * changes during a usbip connection.
301 sdev
->devid
= (busnum
<< 16) | devnum
;
302 sdev
->ud
.side
= USBIP_STUB
;
303 sdev
->ud
.status
= SDEV_ST_AVAILABLE
;
304 /* sdev->ud.lock = SPIN_LOCK_UNLOCKED; */
305 spin_lock_init(&sdev
->ud
.lock
);
306 sdev
->ud
.tcp_socket
= NULL
;
308 INIT_LIST_HEAD(&sdev
->priv_init
);
309 INIT_LIST_HEAD(&sdev
->priv_tx
);
310 INIT_LIST_HEAD(&sdev
->priv_free
);
311 INIT_LIST_HEAD(&sdev
->unlink_free
);
312 INIT_LIST_HEAD(&sdev
->unlink_tx
);
313 /* sdev->priv_lock = SPIN_LOCK_UNLOCKED; */
314 spin_lock_init(&sdev
->priv_lock
);
316 init_waitqueue_head(&sdev
->tx_waitq
);
318 sdev
->ud
.eh_ops
.shutdown
= stub_shutdown_connection
;
319 sdev
->ud
.eh_ops
.reset
= stub_device_reset
;
320 sdev
->ud
.eh_ops
.unusable
= stub_device_unusable
;
322 usbip_start_eh(&sdev
->ud
);
324 dev_dbg(&interface
->dev
, "register new interface\n");
329 static int stub_device_free(struct stub_device
*sdev
)
335 pr_debug("kfree udev ok\n");
341 * If a usb device has multiple active interfaces, this driver is bound to all
342 * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
343 * active interface). Currently, a userland program must ensure that it
344 * looks at the usbip's sysfs entries of only the first active interface.
346 * TODO: use "struct usb_device_driver" to bind a usb device.
347 * However, it seems it is not fully supported in mainline kernel yet
350 static int stub_probe(struct usb_interface
*interface
,
351 const struct usb_device_id
*id
)
353 struct usb_device
*udev
= interface_to_usbdev(interface
);
354 struct stub_device
*sdev
= NULL
;
355 const char *udev_busid
= dev_name(interface
->dev
.parent
);
357 struct bus_id_priv
*busid_priv
;
359 dev_dbg(&interface
->dev
, "Enter\n");
361 /* check we should claim or not by busid_table */
362 busid_priv
= get_busid_priv(udev_busid
);
363 if (!busid_priv
|| (busid_priv
->status
== STUB_BUSID_REMOV
) ||
364 (busid_priv
->status
== STUB_BUSID_OTHER
)) {
365 dev_info(&interface
->dev
, "%s is not in match_busid table... "
366 "skip!\n", udev_busid
);
369 * Return value should be ENODEV or ENOXIO to continue trying
370 * other matched drivers by the driver core.
371 * See driver_probe_device() in driver/base/dd.c
376 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
) {
377 dev_dbg(&udev
->dev
, "%s is a usb hub device... skip!\n",
382 if (!strcmp(udev
->bus
->bus_name
, "vhci_hcd")) {
383 dev_dbg(&udev
->dev
, "%s is attached on vhci_hcd... skip!\n",
388 if (busid_priv
->status
== STUB_BUSID_ALLOC
) {
389 sdev
= busid_priv
->sdev
;
393 busid_priv
->interf_count
++;
394 dev_info(&interface
->dev
, "usbip-host: register new interface "
395 "(bus %u dev %u ifn %u)\n",
396 udev
->bus
->busnum
, udev
->devnum
,
397 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
399 /* set private data to usb_interface */
400 usb_set_intfdata(interface
, sdev
);
402 err
= stub_add_files(&interface
->dev
);
404 dev_err(&interface
->dev
, "stub_add_files for %s\n",
406 usb_set_intfdata(interface
, NULL
);
407 busid_priv
->interf_count
--;
411 usb_get_intf(interface
);
415 /* ok, this is my device */
416 sdev
= stub_device_alloc(udev
, interface
);
420 dev_info(&interface
->dev
, "usbip-host: register new device "
421 "(bus %u dev %u ifn %u)\n", udev
->bus
->busnum
, udev
->devnum
,
422 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
424 busid_priv
->interf_count
= 0;
425 busid_priv
->shutdown_busid
= 0;
427 /* set private data to usb_interface */
428 usb_set_intfdata(interface
, sdev
);
429 busid_priv
->interf_count
++;
430 busid_priv
->sdev
= sdev
;
432 err
= stub_add_files(&interface
->dev
);
434 dev_err(&interface
->dev
, "stub_add_files for %s\n", udev_busid
);
435 usb_set_intfdata(interface
, NULL
);
436 usb_put_intf(interface
);
438 busid_priv
->interf_count
= 0;
439 busid_priv
->sdev
= NULL
;
440 stub_device_free(sdev
);
443 busid_priv
->status
= STUB_BUSID_ALLOC
;
448 static void shutdown_busid(struct bus_id_priv
*busid_priv
)
450 if (busid_priv
->sdev
&& !busid_priv
->shutdown_busid
) {
451 busid_priv
->shutdown_busid
= 1;
452 usbip_event_add(&busid_priv
->sdev
->ud
, SDEV_EVENT_REMOVED
);
454 /* 2. wait for the stop of the event handler */
455 usbip_stop_eh(&busid_priv
->sdev
->ud
);
460 * called in usb_disconnect() or usb_deregister()
461 * but only if actconfig(active configuration) exists
463 static void stub_disconnect(struct usb_interface
*interface
)
465 struct stub_device
*sdev
;
466 const char *udev_busid
= dev_name(interface
->dev
.parent
);
467 struct bus_id_priv
*busid_priv
;
469 dev_dbg(&interface
->dev
, "Enter\n");
471 busid_priv
= get_busid_priv(udev_busid
);
477 sdev
= usb_get_intfdata(interface
);
479 /* get stub_device */
481 dev_err(&interface
->dev
, "could not get device");
486 usb_set_intfdata(interface
, NULL
);
490 * rx/tx threads are invoked for each usb_device.
492 stub_remove_files(&interface
->dev
);
494 /*If usb reset called from event handler*/
495 if (busid_priv
->sdev
->ud
.eh
== current
) {
496 busid_priv
->interf_count
--;
500 if (busid_priv
->interf_count
> 1) {
501 busid_priv
->interf_count
--;
502 shutdown_busid(busid_priv
);
503 usb_put_intf(interface
);
507 busid_priv
->interf_count
= 0;
509 /* 1. shutdown the current connection */
510 shutdown_busid(busid_priv
);
512 usb_put_dev(sdev
->udev
);
513 usb_put_intf(interface
);
516 busid_priv
->sdev
= NULL
;
517 stub_device_free(sdev
);
519 if (busid_priv
->status
== STUB_BUSID_ALLOC
) {
520 busid_priv
->status
= STUB_BUSID_ADDED
;
522 busid_priv
->status
= STUB_BUSID_OTHER
;
523 del_match_busid((char *)udev_busid
);
528 * Presence of pre_reset and post_reset prevents the driver from being unbound
529 * when the device is being reset
532 int stub_pre_reset(struct usb_interface
*interface
)
534 dev_dbg(&interface
->dev
, "pre_reset\n");
538 int stub_post_reset(struct usb_interface
*interface
)
540 dev_dbg(&interface
->dev
, "post_reset\n");
544 struct usb_driver stub_driver
= {
545 .name
= "usbip-host",
547 .disconnect
= stub_disconnect
,
548 .id_table
= stub_table
,
549 .pre_reset
= stub_pre_reset
,
550 .post_reset
= stub_post_reset
,