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/file.h>
22 #include <linux/kthread.h>
23 #include <linux/module.h>
25 #include "usbip_common.h"
29 * Define device IDs here if you want to explicitly limit exportable devices.
30 * In most cases, wildcard matching will be okay because driver binding can be
31 * changed dynamically by a userland program.
33 static struct usb_device_id stub_table
[] = {
36 { USB_DEVICE(0x05ac, 0x0301) }, /* Mac 1 button mouse */
37 { USB_DEVICE(0x0430, 0x0009) }, /* Plat Home Keyboard */
38 { USB_DEVICE(0x059b, 0x0001) }, /* Iomega USB Zip 100 */
39 { USB_DEVICE(0x04b3, 0x4427) }, /* IBM USB CD-ROM */
40 { USB_DEVICE(0x05a9, 0xa511) }, /* LifeView USB cam */
41 { USB_DEVICE(0x55aa, 0x0201) }, /* Imation card reader */
42 { USB_DEVICE(0x046d, 0x0870) }, /* Qcam Express(QV-30) */
43 { USB_DEVICE(0x04bb, 0x0101) }, /* IO-DATA HD 120GB */
44 { USB_DEVICE(0x04bb, 0x0904) }, /* IO-DATA USB-ET/TX */
45 { USB_DEVICE(0x04bb, 0x0201) }, /* IO-DATA USB-ET/TX */
46 { USB_DEVICE(0x08bb, 0x2702) }, /* ONKYO USB Speaker */
47 { USB_DEVICE(0x046d, 0x08b2) }, /* Logicool Qcam 4000 Pro */
49 /* magic for wild card */
51 { 0, } /* Terminating entry */
53 MODULE_DEVICE_TABLE(usb
, stub_table
);
56 * usbip_status shows the status of usbip-host as long as this driver is bound
57 * to the target device.
59 static ssize_t
show_status(struct device
*dev
, struct device_attribute
*attr
,
62 struct stub_device
*sdev
= dev_get_drvdata(dev
);
66 dev_err(dev
, "sdev is null\n");
70 spin_lock_irq(&sdev
->ud
.lock
);
71 status
= sdev
->ud
.status
;
72 spin_unlock_irq(&sdev
->ud
.lock
);
74 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
76 static DEVICE_ATTR(usbip_status
, S_IRUGO
, show_status
, NULL
);
79 * usbip_sockfd gets a socket descriptor of an established TCP connection that
80 * is used to transfer usbip requests by kernel threads. -1 is a magic number
81 * by which usbip connection is finished.
83 static ssize_t
store_sockfd(struct device
*dev
, struct device_attribute
*attr
,
84 const char *buf
, size_t count
)
86 struct stub_device
*sdev
= dev_get_drvdata(dev
);
88 struct socket
*socket
;
89 ssize_t err
= -EINVAL
;
92 dev_err(dev
, "sdev is null\n");
96 sscanf(buf
, "%d", &sockfd
);
99 dev_info(dev
, "stub up\n");
101 spin_lock_irq(&sdev
->ud
.lock
);
103 if (sdev
->ud
.status
!= SDEV_ST_AVAILABLE
) {
104 dev_err(dev
, "not ready\n");
108 socket
= sockfd_to_socket(sockfd
);
112 sdev
->ud
.tcp_socket
= socket
;
114 spin_unlock_irq(&sdev
->ud
.lock
);
116 sdev
->ud
.tcp_rx
= kthread_get_run(stub_rx_loop
, &sdev
->ud
,
118 sdev
->ud
.tcp_tx
= kthread_get_run(stub_tx_loop
, &sdev
->ud
,
121 spin_lock_irq(&sdev
->ud
.lock
);
122 sdev
->ud
.status
= SDEV_ST_USED
;
123 spin_unlock_irq(&sdev
->ud
.lock
);
126 dev_info(dev
, "stub down\n");
128 spin_lock_irq(&sdev
->ud
.lock
);
129 if (sdev
->ud
.status
!= SDEV_ST_USED
)
132 spin_unlock_irq(&sdev
->ud
.lock
);
134 usbip_event_add(&sdev
->ud
, SDEV_EVENT_DOWN
);
140 spin_unlock_irq(&sdev
->ud
.lock
);
143 static DEVICE_ATTR(usbip_sockfd
, S_IWUSR
, NULL
, store_sockfd
);
145 static int stub_add_files(struct device
*dev
)
149 err
= device_create_file(dev
, &dev_attr_usbip_status
);
153 err
= device_create_file(dev
, &dev_attr_usbip_sockfd
);
157 err
= device_create_file(dev
, &dev_attr_usbip_debug
);
164 device_remove_file(dev
, &dev_attr_usbip_sockfd
);
166 device_remove_file(dev
, &dev_attr_usbip_status
);
171 static void stub_remove_files(struct device
*dev
)
173 device_remove_file(dev
, &dev_attr_usbip_status
);
174 device_remove_file(dev
, &dev_attr_usbip_sockfd
);
175 device_remove_file(dev
, &dev_attr_usbip_debug
);
178 static void stub_shutdown_connection(struct usbip_device
*ud
)
180 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
183 * When removing an exported device, kernel panic sometimes occurred
184 * and then EIP was sk_wait_data of stub_rx thread. Is this because
185 * sk_wait_data returned though stub_rx thread was already finished by
188 if (ud
->tcp_socket
) {
189 dev_dbg(&sdev
->udev
->dev
, "shutdown tcp_socket %p\n",
191 kernel_sock_shutdown(ud
->tcp_socket
, SHUT_RDWR
);
194 /* 1. stop threads */
196 kthread_stop_put(ud
->tcp_rx
);
200 kthread_stop_put(ud
->tcp_tx
);
205 * 2. close the socket
207 * tcp_socket is freed after threads are killed so that usbip_xmit does
208 * not touch NULL socket.
210 if (ud
->tcp_socket
) {
211 fput(ud
->tcp_socket
->file
);
212 ud
->tcp_socket
= NULL
;
215 /* 3. free used data */
216 stub_device_cleanup_urbs(sdev
);
218 /* 4. free stub_unlink */
221 struct stub_unlink
*unlink
, *tmp
;
223 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
224 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_tx
, list
) {
225 list_del(&unlink
->list
);
228 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_free
,
230 list_del(&unlink
->list
);
233 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
237 static void stub_device_reset(struct usbip_device
*ud
)
239 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
240 struct usb_device
*udev
= sdev
->udev
;
243 dev_dbg(&udev
->dev
, "device reset");
245 ret
= usb_lock_device_for_reset(udev
, sdev
->interface
);
247 dev_err(&udev
->dev
, "lock for reset\n");
248 spin_lock_irq(&ud
->lock
);
249 ud
->status
= SDEV_ST_ERROR
;
250 spin_unlock_irq(&ud
->lock
);
254 /* try to reset the device */
255 ret
= usb_reset_device(udev
);
256 usb_unlock_device(udev
);
258 spin_lock_irq(&ud
->lock
);
260 dev_err(&udev
->dev
, "device reset\n");
261 ud
->status
= SDEV_ST_ERROR
;
263 dev_info(&udev
->dev
, "device reset\n");
264 ud
->status
= SDEV_ST_AVAILABLE
;
266 spin_unlock_irq(&ud
->lock
);
269 static void stub_device_unusable(struct usbip_device
*ud
)
271 spin_lock_irq(&ud
->lock
);
272 ud
->status
= SDEV_ST_ERROR
;
273 spin_unlock_irq(&ud
->lock
);
277 * stub_device_alloc - allocate a new stub_device struct
278 * @interface: usb_interface of a new device
280 * Allocates and initializes a new stub_device struct.
282 static struct stub_device
*stub_device_alloc(struct usb_device
*udev
,
283 struct usb_interface
*interface
)
285 struct stub_device
*sdev
;
286 int busnum
= interface_to_busnum(interface
);
287 int devnum
= interface_to_devnum(interface
);
289 dev_dbg(&interface
->dev
, "allocating stub device");
291 /* yes, it's a new device */
292 sdev
= kzalloc(sizeof(struct stub_device
), GFP_KERNEL
);
296 sdev
->interface
= usb_get_intf(interface
);
297 sdev
->udev
= usb_get_dev(udev
);
300 * devid is defined with devnum when this driver is first allocated.
301 * devnum may change later if a device is reset. However, devid never
302 * changes during a usbip connection.
304 sdev
->devid
= (busnum
<< 16) | devnum
;
305 sdev
->ud
.side
= USBIP_STUB
;
306 sdev
->ud
.status
= SDEV_ST_AVAILABLE
;
307 spin_lock_init(&sdev
->ud
.lock
);
308 sdev
->ud
.tcp_socket
= NULL
;
310 INIT_LIST_HEAD(&sdev
->priv_init
);
311 INIT_LIST_HEAD(&sdev
->priv_tx
);
312 INIT_LIST_HEAD(&sdev
->priv_free
);
313 INIT_LIST_HEAD(&sdev
->unlink_free
);
314 INIT_LIST_HEAD(&sdev
->unlink_tx
);
315 spin_lock_init(&sdev
->priv_lock
);
317 init_waitqueue_head(&sdev
->tx_waitq
);
319 sdev
->ud
.eh_ops
.shutdown
= stub_shutdown_connection
;
320 sdev
->ud
.eh_ops
.reset
= stub_device_reset
;
321 sdev
->ud
.eh_ops
.unusable
= stub_device_unusable
;
323 usbip_start_eh(&sdev
->ud
);
325 dev_dbg(&interface
->dev
, "register new interface\n");
330 static void stub_device_free(struct stub_device
*sdev
)
336 * If a usb device has multiple active interfaces, this driver is bound to all
337 * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
338 * active interface). Currently, a userland program must ensure that it
339 * looks at the usbip's sysfs entries of only the first active interface.
341 * TODO: use "struct usb_device_driver" to bind a usb device.
342 * However, it seems it is not fully supported in mainline kernel yet
345 static int stub_probe(struct usb_interface
*interface
,
346 const struct usb_device_id
*id
)
348 struct usb_device
*udev
= interface_to_usbdev(interface
);
349 struct stub_device
*sdev
= NULL
;
350 const char *udev_busid
= dev_name(interface
->dev
.parent
);
352 struct bus_id_priv
*busid_priv
;
354 dev_dbg(&interface
->dev
, "Enter\n");
356 /* check we should claim or not by busid_table */
357 busid_priv
= get_busid_priv(udev_busid
);
358 if (!busid_priv
|| (busid_priv
->status
== STUB_BUSID_REMOV
) ||
359 (busid_priv
->status
== STUB_BUSID_OTHER
)) {
360 dev_info(&interface
->dev
, "%s is not in match_busid table... "
361 "skip!\n", udev_busid
);
364 * Return value should be ENODEV or ENOXIO to continue trying
365 * other matched drivers by the driver core.
366 * See driver_probe_device() in driver/base/dd.c
371 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
) {
372 dev_dbg(&udev
->dev
, "%s is a usb hub device... skip!\n",
377 if (!strcmp(udev
->bus
->bus_name
, "vhci_hcd")) {
378 dev_dbg(&udev
->dev
, "%s is attached on vhci_hcd... skip!\n",
383 if (busid_priv
->status
== STUB_BUSID_ALLOC
) {
384 sdev
= busid_priv
->sdev
;
388 busid_priv
->interf_count
++;
389 dev_info(&interface
->dev
, "usbip-host: register new interface "
390 "(bus %u dev %u ifn %u)\n",
391 udev
->bus
->busnum
, udev
->devnum
,
392 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
394 /* set private data to usb_interface */
395 usb_set_intfdata(interface
, sdev
);
397 err
= stub_add_files(&interface
->dev
);
399 dev_err(&interface
->dev
, "stub_add_files for %s\n",
401 usb_set_intfdata(interface
, NULL
);
402 busid_priv
->interf_count
--;
406 usb_get_intf(interface
);
410 /* ok, this is my device */
411 sdev
= stub_device_alloc(udev
, interface
);
415 dev_info(&interface
->dev
, "usbip-host: register new device "
416 "(bus %u dev %u ifn %u)\n", udev
->bus
->busnum
, udev
->devnum
,
417 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
419 busid_priv
->interf_count
= 0;
420 busid_priv
->shutdown_busid
= 0;
422 /* set private data to usb_interface */
423 usb_set_intfdata(interface
, sdev
);
424 busid_priv
->interf_count
++;
425 busid_priv
->sdev
= sdev
;
427 err
= stub_add_files(&interface
->dev
);
429 dev_err(&interface
->dev
, "stub_add_files for %s\n", udev_busid
);
430 usb_set_intfdata(interface
, NULL
);
431 usb_put_intf(interface
);
433 kthread_stop_put(sdev
->ud
.eh
);
435 busid_priv
->interf_count
= 0;
436 busid_priv
->sdev
= NULL
;
437 stub_device_free(sdev
);
440 busid_priv
->status
= STUB_BUSID_ALLOC
;
445 static void shutdown_busid(struct bus_id_priv
*busid_priv
)
447 if (busid_priv
->sdev
&& !busid_priv
->shutdown_busid
) {
448 busid_priv
->shutdown_busid
= 1;
449 usbip_event_add(&busid_priv
->sdev
->ud
, SDEV_EVENT_REMOVED
);
451 /* wait for the stop of the event handler */
452 usbip_stop_eh(&busid_priv
->sdev
->ud
);
457 * called in usb_disconnect() or usb_deregister()
458 * but only if actconfig(active configuration) exists
460 static void stub_disconnect(struct usb_interface
*interface
)
462 struct stub_device
*sdev
;
463 const char *udev_busid
= dev_name(interface
->dev
.parent
);
464 struct bus_id_priv
*busid_priv
;
466 dev_dbg(&interface
->dev
, "Enter\n");
468 busid_priv
= get_busid_priv(udev_busid
);
474 sdev
= usb_get_intfdata(interface
);
476 /* get stub_device */
478 dev_err(&interface
->dev
, "could not get device");
482 usb_set_intfdata(interface
, NULL
);
485 * NOTE: rx/tx threads are invoked for each usb_device.
487 stub_remove_files(&interface
->dev
);
489 /* If usb reset is called from event handler */
490 if (busid_priv
->sdev
->ud
.eh
== current
) {
491 busid_priv
->interf_count
--;
495 if (busid_priv
->interf_count
> 1) {
496 busid_priv
->interf_count
--;
497 shutdown_busid(busid_priv
);
498 usb_put_intf(interface
);
502 busid_priv
->interf_count
= 0;
504 /* shutdown the current connection */
505 shutdown_busid(busid_priv
);
507 usb_put_dev(sdev
->udev
);
508 usb_put_intf(interface
);
511 busid_priv
->sdev
= NULL
;
512 stub_device_free(sdev
);
514 if (busid_priv
->status
== STUB_BUSID_ALLOC
) {
515 busid_priv
->status
= STUB_BUSID_ADDED
;
517 busid_priv
->status
= STUB_BUSID_OTHER
;
518 del_match_busid((char *)udev_busid
);
523 * Presence of pre_reset and post_reset prevents the driver from being unbound
524 * when the device is being reset
527 static int stub_pre_reset(struct usb_interface
*interface
)
529 dev_dbg(&interface
->dev
, "pre_reset\n");
533 static int stub_post_reset(struct usb_interface
*interface
)
535 dev_dbg(&interface
->dev
, "post_reset\n");
539 struct usb_driver stub_driver
= {
540 .name
= "usbip-host",
542 .disconnect
= stub_disconnect
,
543 .id_table
= stub_table
,
544 .pre_reset
= stub_pre_reset
,
545 .post_reset
= stub_post_reset
,