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>
22 #include <linux/module.h>
24 #include "usbip_common.h"
28 * Define device IDs here if you want to explicitly limit exportable devices.
29 * In most cases, wildcard matching will be okay because driver binding can be
30 * changed dynamically by a userland program.
32 static struct usb_device_id stub_table
[] = {
35 { USB_DEVICE(0x05ac, 0x0301) }, /* Mac 1 button mouse */
36 { USB_DEVICE(0x0430, 0x0009) }, /* Plat Home Keyboard */
37 { USB_DEVICE(0x059b, 0x0001) }, /* Iomega USB Zip 100 */
38 { USB_DEVICE(0x04b3, 0x4427) }, /* IBM USB CD-ROM */
39 { USB_DEVICE(0x05a9, 0xa511) }, /* LifeView USB cam */
40 { USB_DEVICE(0x55aa, 0x0201) }, /* Imation card reader */
41 { USB_DEVICE(0x046d, 0x0870) }, /* Qcam Express(QV-30) */
42 { USB_DEVICE(0x04bb, 0x0101) }, /* IO-DATA HD 120GB */
43 { USB_DEVICE(0x04bb, 0x0904) }, /* IO-DATA USB-ET/TX */
44 { USB_DEVICE(0x04bb, 0x0201) }, /* IO-DATA USB-ET/TX */
45 { USB_DEVICE(0x08bb, 0x2702) }, /* ONKYO USB Speaker */
46 { USB_DEVICE(0x046d, 0x08b2) }, /* Logicool Qcam 4000 Pro */
48 /* magic for wild card */
50 { 0, } /* Terminating entry */
52 MODULE_DEVICE_TABLE(usb
, stub_table
);
55 * usbip_status shows the status of usbip-host as long as this driver is bound
56 * to the target device.
58 static ssize_t
show_status(struct device
*dev
, struct device_attribute
*attr
,
61 struct stub_device
*sdev
= dev_get_drvdata(dev
);
65 dev_err(dev
, "sdev is null\n");
69 spin_lock(&sdev
->ud
.lock
);
70 status
= sdev
->ud
.status
;
71 spin_unlock(&sdev
->ud
.lock
);
73 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
75 static DEVICE_ATTR(usbip_status
, S_IRUGO
, show_status
, NULL
);
78 * usbip_sockfd gets a socket descriptor of an established TCP connection that
79 * is used to transfer usbip requests by kernel threads. -1 is a magic number
80 * by which usbip connection is finished.
82 static ssize_t
store_sockfd(struct device
*dev
, struct device_attribute
*attr
,
83 const char *buf
, size_t count
)
85 struct stub_device
*sdev
= dev_get_drvdata(dev
);
87 struct socket
*socket
;
90 dev_err(dev
, "sdev is null\n");
94 sscanf(buf
, "%d", &sockfd
);
97 dev_info(dev
, "stub up\n");
99 spin_lock(&sdev
->ud
.lock
);
101 if (sdev
->ud
.status
!= SDEV_ST_AVAILABLE
) {
102 dev_err(dev
, "not ready\n");
103 spin_unlock(&sdev
->ud
.lock
);
107 socket
= sockfd_to_socket(sockfd
);
109 spin_unlock(&sdev
->ud
.lock
);
114 setkeepalive(socket
);
117 sdev
->ud
.tcp_socket
= socket
;
119 spin_unlock(&sdev
->ud
.lock
);
121 sdev
->ud
.tcp_rx
= kthread_run(stub_rx_loop
, &sdev
->ud
, "stub_rx");
122 sdev
->ud
.tcp_tx
= kthread_run(stub_tx_loop
, &sdev
->ud
, "stub_tx");
124 spin_lock(&sdev
->ud
.lock
);
125 sdev
->ud
.status
= SDEV_ST_USED
;
126 spin_unlock(&sdev
->ud
.lock
);
129 dev_info(dev
, "stub down\n");
131 spin_lock(&sdev
->ud
.lock
);
132 if (sdev
->ud
.status
!= SDEV_ST_USED
) {
133 spin_unlock(&sdev
->ud
.lock
);
136 spin_unlock(&sdev
->ud
.lock
);
138 usbip_event_add(&sdev
->ud
, SDEV_EVENT_DOWN
);
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 */
195 if (ud
->tcp_rx
&& !task_is_dead(ud
->tcp_rx
))
196 kthread_stop(ud
->tcp_rx
);
197 if (ud
->tcp_tx
&& !task_is_dead(ud
->tcp_tx
))
198 kthread_stop(ud
->tcp_tx
);
201 * 2. close the socket
203 * tcp_socket is freed after threads are killed so that usbip_xmit does
204 * not touch NULL socket.
206 if (ud
->tcp_socket
) {
207 sock_release(ud
->tcp_socket
);
208 ud
->tcp_socket
= NULL
;
211 /* 3. free used data */
212 stub_device_cleanup_urbs(sdev
);
214 /* 4. free stub_unlink */
217 struct stub_unlink
*unlink
, *tmp
;
219 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
220 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_tx
, list
) {
221 list_del(&unlink
->list
);
224 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_free
,
226 list_del(&unlink
->list
);
229 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
233 static void stub_device_reset(struct usbip_device
*ud
)
235 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
236 struct usb_device
*udev
= sdev
->udev
;
239 dev_dbg(&udev
->dev
, "device reset");
241 ret
= usb_lock_device_for_reset(udev
, sdev
->interface
);
243 dev_err(&udev
->dev
, "lock for reset\n");
244 spin_lock(&ud
->lock
);
245 ud
->status
= SDEV_ST_ERROR
;
246 spin_unlock(&ud
->lock
);
250 /* try to reset the device */
251 ret
= usb_reset_device(udev
);
252 usb_unlock_device(udev
);
254 spin_lock(&ud
->lock
);
256 dev_err(&udev
->dev
, "device reset\n");
257 ud
->status
= SDEV_ST_ERROR
;
259 dev_info(&udev
->dev
, "device reset\n");
260 ud
->status
= SDEV_ST_AVAILABLE
;
262 spin_unlock(&ud
->lock
);
265 static void stub_device_unusable(struct usbip_device
*ud
)
267 spin_lock(&ud
->lock
);
268 ud
->status
= SDEV_ST_ERROR
;
269 spin_unlock(&ud
->lock
);
273 * stub_device_alloc - allocate a new stub_device struct
274 * @interface: usb_interface of a new device
276 * Allocates and initializes a new stub_device struct.
278 static struct stub_device
*stub_device_alloc(struct usb_device
*udev
,
279 struct usb_interface
*interface
)
281 struct stub_device
*sdev
;
282 int busnum
= interface_to_busnum(interface
);
283 int devnum
= interface_to_devnum(interface
);
285 dev_dbg(&interface
->dev
, "allocating stub device");
287 /* yes, it's a new device */
288 sdev
= kzalloc(sizeof(struct stub_device
), GFP_KERNEL
);
290 dev_err(&interface
->dev
, "no memory for stub_device\n");
294 sdev
->interface
= usb_get_intf(interface
);
295 sdev
->udev
= usb_get_dev(udev
);
298 * devid is defined with devnum when this driver is first allocated.
299 * devnum may change later if a device is reset. However, devid never
300 * changes during a usbip connection.
302 sdev
->devid
= (busnum
<< 16) | devnum
;
303 sdev
->ud
.side
= USBIP_STUB
;
304 sdev
->ud
.status
= SDEV_ST_AVAILABLE
;
305 /* sdev->ud.lock = SPIN_LOCK_UNLOCKED; */
306 spin_lock_init(&sdev
->ud
.lock
);
307 sdev
->ud
.tcp_socket
= NULL
;
309 INIT_LIST_HEAD(&sdev
->priv_init
);
310 INIT_LIST_HEAD(&sdev
->priv_tx
);
311 INIT_LIST_HEAD(&sdev
->priv_free
);
312 INIT_LIST_HEAD(&sdev
->unlink_free
);
313 INIT_LIST_HEAD(&sdev
->unlink_tx
);
314 /* sdev->priv_lock = SPIN_LOCK_UNLOCKED; */
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 int stub_device_free(struct stub_device
*sdev
)
336 pr_debug("kfree udev ok\n");
342 * If a usb device has multiple active interfaces, this driver is bound to all
343 * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
344 * active interface). Currently, a userland program must ensure that it
345 * looks at the usbip's sysfs entries of only the first active interface.
347 * TODO: use "struct usb_device_driver" to bind a usb device.
348 * However, it seems it is not fully supported in mainline kernel yet
351 static int stub_probe(struct usb_interface
*interface
,
352 const struct usb_device_id
*id
)
354 struct usb_device
*udev
= interface_to_usbdev(interface
);
355 struct stub_device
*sdev
= NULL
;
356 const char *udev_busid
= dev_name(interface
->dev
.parent
);
358 struct bus_id_priv
*busid_priv
;
360 dev_dbg(&interface
->dev
, "Enter\n");
362 /* check we should claim or not by busid_table */
363 busid_priv
= get_busid_priv(udev_busid
);
364 if (!busid_priv
|| (busid_priv
->status
== STUB_BUSID_REMOV
) ||
365 (busid_priv
->status
== STUB_BUSID_OTHER
)) {
366 dev_info(&interface
->dev
, "%s is not in match_busid table... "
367 "skip!\n", udev_busid
);
370 * Return value should be ENODEV or ENOXIO to continue trying
371 * other matched drivers by the driver core.
372 * See driver_probe_device() in driver/base/dd.c
377 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
) {
378 dev_dbg(&udev
->dev
, "%s is a usb hub device... skip!\n",
383 if (!strcmp(udev
->bus
->bus_name
, "vhci_hcd")) {
384 dev_dbg(&udev
->dev
, "%s is attached on vhci_hcd... skip!\n",
389 if (busid_priv
->status
== STUB_BUSID_ALLOC
) {
390 sdev
= busid_priv
->sdev
;
394 busid_priv
->interf_count
++;
395 dev_info(&interface
->dev
, "usbip-host: register new interface "
396 "(bus %u dev %u ifn %u)\n",
397 udev
->bus
->busnum
, udev
->devnum
,
398 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
400 /* set private data to usb_interface */
401 usb_set_intfdata(interface
, sdev
);
403 err
= stub_add_files(&interface
->dev
);
405 dev_err(&interface
->dev
, "stub_add_files for %s\n",
407 usb_set_intfdata(interface
, NULL
);
408 busid_priv
->interf_count
--;
412 usb_get_intf(interface
);
416 /* ok, this is my device */
417 sdev
= stub_device_alloc(udev
, interface
);
421 dev_info(&interface
->dev
, "usbip-host: register new device "
422 "(bus %u dev %u ifn %u)\n", udev
->bus
->busnum
, udev
->devnum
,
423 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
425 busid_priv
->interf_count
= 0;
426 busid_priv
->shutdown_busid
= 0;
428 /* set private data to usb_interface */
429 usb_set_intfdata(interface
, sdev
);
430 busid_priv
->interf_count
++;
431 busid_priv
->sdev
= sdev
;
433 err
= stub_add_files(&interface
->dev
);
435 dev_err(&interface
->dev
, "stub_add_files for %s\n", udev_busid
);
436 usb_set_intfdata(interface
, NULL
);
437 usb_put_intf(interface
);
439 busid_priv
->interf_count
= 0;
440 busid_priv
->sdev
= NULL
;
441 stub_device_free(sdev
);
444 busid_priv
->status
= STUB_BUSID_ALLOC
;
449 static void shutdown_busid(struct bus_id_priv
*busid_priv
)
451 if (busid_priv
->sdev
&& !busid_priv
->shutdown_busid
) {
452 busid_priv
->shutdown_busid
= 1;
453 usbip_event_add(&busid_priv
->sdev
->ud
, SDEV_EVENT_REMOVED
);
455 /* 2. wait for the stop of the event handler */
456 usbip_stop_eh(&busid_priv
->sdev
->ud
);
461 * called in usb_disconnect() or usb_deregister()
462 * but only if actconfig(active configuration) exists
464 static void stub_disconnect(struct usb_interface
*interface
)
466 struct stub_device
*sdev
;
467 const char *udev_busid
= dev_name(interface
->dev
.parent
);
468 struct bus_id_priv
*busid_priv
;
470 dev_dbg(&interface
->dev
, "Enter\n");
472 busid_priv
= get_busid_priv(udev_busid
);
478 sdev
= usb_get_intfdata(interface
);
480 /* get stub_device */
482 dev_err(&interface
->dev
, "could not get device");
487 usb_set_intfdata(interface
, NULL
);
491 * rx/tx threads are invoked for each usb_device.
493 stub_remove_files(&interface
->dev
);
495 /*If usb reset called from event handler*/
496 if (busid_priv
->sdev
->ud
.eh
== current
) {
497 busid_priv
->interf_count
--;
501 if (busid_priv
->interf_count
> 1) {
502 busid_priv
->interf_count
--;
503 shutdown_busid(busid_priv
);
504 usb_put_intf(interface
);
508 busid_priv
->interf_count
= 0;
510 /* 1. shutdown the current connection */
511 shutdown_busid(busid_priv
);
513 usb_put_dev(sdev
->udev
);
514 usb_put_intf(interface
);
517 busid_priv
->sdev
= NULL
;
518 stub_device_free(sdev
);
520 if (busid_priv
->status
== STUB_BUSID_ALLOC
) {
521 busid_priv
->status
= STUB_BUSID_ADDED
;
523 busid_priv
->status
= STUB_BUSID_OTHER
;
524 del_match_busid((char *)udev_busid
);
529 * Presence of pre_reset and post_reset prevents the driver from being unbound
530 * when the device is being reset
533 int stub_pre_reset(struct usb_interface
*interface
)
535 dev_dbg(&interface
->dev
, "pre_reset\n");
539 int stub_post_reset(struct usb_interface
*interface
)
541 dev_dbg(&interface
->dev
, "post_reset\n");
545 struct usb_driver stub_driver
= {
546 .name
= "usbip-host",
548 .disconnect
= stub_disconnect
,
549 .id_table
= stub_table
,
550 .pre_reset
= stub_pre_reset
,
551 .post_reset
= stub_post_reset
,