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"
27 static int stub_probe(struct usb_interface
*interface
,
28 const struct usb_device_id
*id
);
29 static void stub_disconnect(struct usb_interface
*interface
);
33 * Define device IDs here if you want to explicitly limit exportable devices.
34 * In the most cases, wild card matching will be ok because driver binding can
35 * be changed dynamically by a userland program.
37 static struct usb_device_id stub_table
[] = {
40 { USB_DEVICE(0x05ac, 0x0301) }, /* Mac 1 button mouse */
41 { USB_DEVICE(0x0430, 0x0009) }, /* Plat Home Keyboard */
42 { USB_DEVICE(0x059b, 0x0001) }, /* Iomega USB Zip 100 */
43 { USB_DEVICE(0x04b3, 0x4427) }, /* IBM USB CD-ROM */
44 { USB_DEVICE(0x05a9, 0xa511) }, /* LifeView USB cam */
45 { USB_DEVICE(0x55aa, 0x0201) }, /* Imation card reader */
46 { USB_DEVICE(0x046d, 0x0870) }, /* Qcam Express(QV-30) */
47 { USB_DEVICE(0x04bb, 0x0101) }, /* IO-DATA HD 120GB */
48 { USB_DEVICE(0x04bb, 0x0904) }, /* IO-DATA USB-ET/TX */
49 { USB_DEVICE(0x04bb, 0x0201) }, /* IO-DATA USB-ET/TX */
50 { USB_DEVICE(0x08bb, 0x2702) }, /* ONKYO USB Speaker */
51 { USB_DEVICE(0x046d, 0x08b2) }, /* Logicool Qcam 4000 Pro */
53 /* magic for wild card */
55 { 0, } /* Terminating entry */
57 MODULE_DEVICE_TABLE(usb
, stub_table
);
59 struct usb_driver stub_driver
= {
62 .disconnect
= stub_disconnect
,
63 .id_table
= stub_table
,
67 /*-------------------------------------------------------------------------*/
69 /* Define sysfs entries for a usbip-bound device */
73 * usbip_status shows status of usbip as long as this driver is bound to the
76 static ssize_t
show_status(struct device
*dev
, struct device_attribute
*attr
,
79 struct stub_device
*sdev
= dev_get_drvdata(dev
);
83 dev_err(dev
, "sdev is null\n");
87 spin_lock(&sdev
->ud
.lock
);
88 status
= sdev
->ud
.status
;
89 spin_unlock(&sdev
->ud
.lock
);
91 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
93 static DEVICE_ATTR(usbip_status
, S_IRUGO
, show_status
, NULL
);
96 * usbip_sockfd gets a socket descriptor of an established TCP connection that
97 * is used to transfer usbip requests by kernel threads. -1 is a magic number
98 * by which usbip connection is finished.
100 static ssize_t
store_sockfd(struct device
*dev
, struct device_attribute
*attr
,
101 const char *buf
, size_t count
)
103 struct stub_device
*sdev
= dev_get_drvdata(dev
);
105 struct socket
*socket
;
108 dev_err(dev
, "sdev is null\n");
112 sscanf(buf
, "%d", &sockfd
);
115 dev_info(dev
, "stub up\n");
117 spin_lock(&sdev
->ud
.lock
);
119 if (sdev
->ud
.status
!= SDEV_ST_AVAILABLE
) {
120 dev_err(dev
, "not ready\n");
121 spin_unlock(&sdev
->ud
.lock
);
125 socket
= sockfd_to_socket(sockfd
);
127 spin_unlock(&sdev
->ud
.lock
);
133 setkeepalive(socket
);
137 sdev
->ud
.tcp_socket
= socket
;
139 spin_unlock(&sdev
->ud
.lock
);
141 usbip_start_threads(&sdev
->ud
);
143 spin_lock(&sdev
->ud
.lock
);
144 sdev
->ud
.status
= SDEV_ST_USED
;
145 spin_unlock(&sdev
->ud
.lock
);
148 dev_info(dev
, "stub down\n");
150 spin_lock(&sdev
->ud
.lock
);
151 if (sdev
->ud
.status
!= SDEV_ST_USED
) {
152 spin_unlock(&sdev
->ud
.lock
);
155 spin_unlock(&sdev
->ud
.lock
);
157 usbip_event_add(&sdev
->ud
, SDEV_EVENT_DOWN
);
162 static DEVICE_ATTR(usbip_sockfd
, S_IWUSR
, NULL
, store_sockfd
);
164 static int stub_add_files(struct device
*dev
)
168 err
= device_create_file(dev
, &dev_attr_usbip_status
);
172 err
= device_create_file(dev
, &dev_attr_usbip_sockfd
);
176 err
= device_create_file(dev
, &dev_attr_usbip_debug
);
183 device_remove_file(dev
, &dev_attr_usbip_sockfd
);
186 device_remove_file(dev
, &dev_attr_usbip_status
);
192 static void stub_remove_files(struct device
*dev
)
194 device_remove_file(dev
, &dev_attr_usbip_status
);
195 device_remove_file(dev
, &dev_attr_usbip_sockfd
);
196 device_remove_file(dev
, &dev_attr_usbip_debug
);
201 /*-------------------------------------------------------------------------*/
203 /* Event handler functions called by an event handler thread */
205 static void stub_shutdown_connection(struct usbip_device
*ud
)
207 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
210 * When removing an exported device, kernel panic sometimes occurred
211 * and then EIP was sk_wait_data of stub_rx thread. Is this because
212 * sk_wait_data returned though stub_rx thread was already finished by
215 if (ud
->tcp_socket
) {
216 usbip_udbg("shutdown tcp_socket %p\n", ud
->tcp_socket
);
217 kernel_sock_shutdown(ud
->tcp_socket
, SHUT_RDWR
);
220 /* 1. stop threads */
221 usbip_stop_threads(ud
);
223 /* 2. close the socket */
225 * tcp_socket is freed after threads are killed.
226 * So usbip_xmit do not touch NULL socket.
228 if (ud
->tcp_socket
) {
229 sock_release(ud
->tcp_socket
);
230 ud
->tcp_socket
= NULL
;
233 /* 3. free used data */
234 stub_device_cleanup_urbs(sdev
);
236 /* 4. free stub_unlink */
239 struct stub_unlink
*unlink
, *tmp
;
241 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
243 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_tx
, list
) {
244 list_del(&unlink
->list
);
248 list_for_each_entry_safe(unlink
, tmp
,
249 &sdev
->unlink_free
, list
) {
250 list_del(&unlink
->list
);
254 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
258 static void stub_device_reset(struct usbip_device
*ud
)
260 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
261 struct usb_device
*udev
= sdev
->udev
;
264 usbip_udbg("device reset");
266 ret
= usb_lock_device_for_reset(udev
, sdev
->interface
);
268 dev_err(&udev
->dev
, "lock for reset\n");
270 spin_lock(&ud
->lock
);
271 ud
->status
= SDEV_ST_ERROR
;
272 spin_unlock(&ud
->lock
);
277 /* try to reset the device */
278 ret
= usb_reset_device(udev
);
280 usb_unlock_device(udev
);
282 spin_lock(&ud
->lock
);
284 dev_err(&udev
->dev
, "device reset\n");
285 ud
->status
= SDEV_ST_ERROR
;
288 dev_info(&udev
->dev
, "device reset\n");
289 ud
->status
= SDEV_ST_AVAILABLE
;
292 spin_unlock(&ud
->lock
);
297 static void stub_device_unusable(struct usbip_device
*ud
)
299 spin_lock(&ud
->lock
);
300 ud
->status
= SDEV_ST_ERROR
;
301 spin_unlock(&ud
->lock
);
305 /*-------------------------------------------------------------------------*/
308 * stub_device_alloc - allocate a new stub_device struct
309 * @interface: usb_interface of a new device
311 * Allocates and initializes a new stub_device struct.
313 static struct stub_device
*stub_device_alloc(struct usb_device
*udev
,
314 struct usb_interface
*interface
)
316 struct stub_device
*sdev
;
317 int busnum
= interface_to_busnum(interface
);
318 int devnum
= interface_to_devnum(interface
);
320 dev_dbg(&interface
->dev
, "allocating stub device");
322 /* yes, it's a new device */
323 sdev
= kzalloc(sizeof(struct stub_device
), GFP_KERNEL
);
325 dev_err(&interface
->dev
, "no memory for stub_device\n");
329 sdev
->interface
= usb_get_intf(interface
);
330 sdev
->udev
= usb_get_dev(udev
);
333 * devid is defined with devnum when this driver is first allocated.
334 * devnum may change later if a device is reset. However, devid never
335 * changes during a usbip connection.
337 sdev
->devid
= (busnum
<< 16) | devnum
;
339 usbip_task_init(&sdev
->ud
.tcp_rx
, "stub_rx", stub_rx_loop
);
340 usbip_task_init(&sdev
->ud
.tcp_tx
, "stub_tx", stub_tx_loop
);
342 sdev
->ud
.side
= USBIP_STUB
;
343 sdev
->ud
.status
= SDEV_ST_AVAILABLE
;
344 /* sdev->ud.lock = SPIN_LOCK_UNLOCKED; */
345 spin_lock_init(&sdev
->ud
.lock
);
346 sdev
->ud
.tcp_socket
= NULL
;
348 INIT_LIST_HEAD(&sdev
->priv_init
);
349 INIT_LIST_HEAD(&sdev
->priv_tx
);
350 INIT_LIST_HEAD(&sdev
->priv_free
);
351 INIT_LIST_HEAD(&sdev
->unlink_free
);
352 INIT_LIST_HEAD(&sdev
->unlink_tx
);
353 /* sdev->priv_lock = SPIN_LOCK_UNLOCKED; */
354 spin_lock_init(&sdev
->priv_lock
);
356 init_waitqueue_head(&sdev
->tx_waitq
);
358 sdev
->ud
.eh_ops
.shutdown
= stub_shutdown_connection
;
359 sdev
->ud
.eh_ops
.reset
= stub_device_reset
;
360 sdev
->ud
.eh_ops
.unusable
= stub_device_unusable
;
362 usbip_start_eh(&sdev
->ud
);
364 usbip_udbg("register new interface\n");
368 static int stub_device_free(struct stub_device
*sdev
)
374 usbip_udbg("kfree udev ok\n");
380 /*-------------------------------------------------------------------------*/
383 * If a usb device has multiple active interfaces, this driver is bound to all
384 * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
385 * active interface). Currently, a userland program must ensure that it
386 * looks at the usbip's sysfs entries of only the first active interface.
388 * TODO: use "struct usb_device_driver" to bind a usb device.
389 * However, it seems it is not fully supported in mainline kernel yet
392 static int stub_probe(struct usb_interface
*interface
,
393 const struct usb_device_id
*id
)
395 struct usb_device
*udev
= interface_to_usbdev(interface
);
396 struct stub_device
*sdev
= NULL
;
397 const char *udev_busid
= dev_name(interface
->dev
.parent
);
399 struct bus_id_priv
*busid_priv
;
401 dev_dbg(&interface
->dev
, "Enter\n");
403 /* check we should claim or not by busid_table */
404 busid_priv
= get_busid_priv(udev_busid
);
405 if (!busid_priv
|| (busid_priv
->status
== STUB_BUSID_REMOV
) ||
406 (busid_priv
->status
== STUB_BUSID_OTHER
)) {
407 dev_info(&interface
->dev
,
408 "this device %s is not in match_busid table. skip!\n",
412 * Return value should be ENODEV or ENOXIO to continue trying
413 * other matched drivers by the driver core.
414 * See driver_probe_device() in driver/base/dd.c
419 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
) {
420 usbip_udbg("this device %s is a usb hub device. skip!\n",
425 if (!strcmp(udev
->bus
->bus_name
, "vhci_hcd")) {
426 usbip_udbg("this device %s is attached on vhci_hcd. skip!\n",
432 if (busid_priv
->status
== STUB_BUSID_ALLOC
) {
433 sdev
= busid_priv
->sdev
;
437 busid_priv
->interf_count
++;
438 dev_info(&interface
->dev
,
439 "USB/IP Stub: register a new interface "
440 "(bus %u dev %u ifn %u)\n", udev
->bus
->busnum
, udev
->devnum
,
441 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
443 /* set private data to usb_interface */
444 usb_set_intfdata(interface
, sdev
);
446 err
= stub_add_files(&interface
->dev
);
448 dev_err(&interface
->dev
, "create sysfs files for %s\n",
450 usb_set_intfdata(interface
, NULL
);
451 busid_priv
->interf_count
--;
456 usb_get_intf(interface
);
460 /* ok. this is my device. */
461 sdev
= stub_device_alloc(udev
, interface
);
465 dev_info(&interface
->dev
, "USB/IP Stub: register a new device "
466 "(bus %u dev %u ifn %u)\n", udev
->bus
->busnum
, udev
->devnum
,
467 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
469 busid_priv
->interf_count
= 0;
470 busid_priv
->shutdown_busid
= 0;
472 /* set private data to usb_interface */
473 usb_set_intfdata(interface
, sdev
);
474 busid_priv
->interf_count
++;
476 busid_priv
->sdev
= sdev
;
478 err
= stub_add_files(&interface
->dev
);
480 dev_err(&interface
->dev
, "create sysfs files for %s\n",
482 usb_set_intfdata(interface
, NULL
);
483 usb_put_intf(interface
);
485 busid_priv
->interf_count
= 0;
487 busid_priv
->sdev
= NULL
;
488 stub_device_free(sdev
);
491 busid_priv
->status
= STUB_BUSID_ALLOC
;
496 static void shutdown_busid(struct bus_id_priv
*busid_priv
)
498 if (busid_priv
->sdev
&& !busid_priv
->shutdown_busid
) {
499 busid_priv
->shutdown_busid
= 1;
500 usbip_event_add(&busid_priv
->sdev
->ud
, SDEV_EVENT_REMOVED
);
502 /* 2. wait for the stop of the event handler */
503 usbip_stop_eh(&busid_priv
->sdev
->ud
);
510 * called in usb_disconnect() or usb_deregister()
511 * but only if actconfig(active configuration) exists
513 static void stub_disconnect(struct usb_interface
*interface
)
515 struct stub_device
*sdev
;
516 const char *udev_busid
= dev_name(interface
->dev
.parent
);
517 struct bus_id_priv
*busid_priv
;
519 busid_priv
= get_busid_priv(udev_busid
);
521 usbip_udbg("Enter\n");
528 sdev
= usb_get_intfdata(interface
);
530 /* get stub_device */
532 err(" could not get device from inteface data");
537 usb_set_intfdata(interface
, NULL
);
541 * rx/tx threads are invoked for each usb_device.
543 stub_remove_files(&interface
->dev
);
545 /*If usb reset called from event handler*/
546 if (busid_priv
->sdev
->ud
.eh
.thread
== current
) {
547 busid_priv
->interf_count
--;
551 if (busid_priv
->interf_count
> 1) {
552 busid_priv
->interf_count
--;
553 shutdown_busid(busid_priv
);
554 usb_put_intf(interface
);
558 busid_priv
->interf_count
= 0;
561 /* 1. shutdown the current connection */
562 shutdown_busid(busid_priv
);
564 usb_put_dev(sdev
->udev
);
565 usb_put_intf(interface
);
568 busid_priv
->sdev
= NULL
;
569 stub_device_free(sdev
);
571 if (busid_priv
->status
== STUB_BUSID_ALLOC
) {
572 busid_priv
->status
= STUB_BUSID_ADDED
;
574 busid_priv
->status
= STUB_BUSID_OTHER
;
575 del_match_busid((char *)udev_busid
);
This page took 0.043988 seconds and 5 git commands to generate.