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"
26 static int stub_probe(struct usb_interface
*interface
,
27 const struct usb_device_id
*id
);
28 static void stub_disconnect(struct usb_interface
*interface
);
31 * Define device IDs here if you want to explicitly limit exportable devices.
32 * In the most cases, wild card matching will be ok because driver binding can
33 * be changed dynamically by a userland program.
35 static struct usb_device_id stub_table
[] = {
38 { USB_DEVICE(0x05ac, 0x0301) }, /* Mac 1 button mouse */
39 { USB_DEVICE(0x0430, 0x0009) }, /* Plat Home Keyboard */
40 { USB_DEVICE(0x059b, 0x0001) }, /* Iomega USB Zip 100 */
41 { USB_DEVICE(0x04b3, 0x4427) }, /* IBM USB CD-ROM */
42 { USB_DEVICE(0x05a9, 0xa511) }, /* LifeView USB cam */
43 { USB_DEVICE(0x55aa, 0x0201) }, /* Imation card reader */
44 { USB_DEVICE(0x046d, 0x0870) }, /* Qcam Express(QV-30) */
45 { USB_DEVICE(0x04bb, 0x0101) }, /* IO-DATA HD 120GB */
46 { USB_DEVICE(0x04bb, 0x0904) }, /* IO-DATA USB-ET/TX */
47 { USB_DEVICE(0x04bb, 0x0201) }, /* IO-DATA USB-ET/TX */
48 { USB_DEVICE(0x08bb, 0x2702) }, /* ONKYO USB Speaker */
49 { USB_DEVICE(0x046d, 0x08b2) }, /* Logicool Qcam 4000 Pro */
51 /* magic for wild card */
53 { 0, } /* Terminating entry */
55 MODULE_DEVICE_TABLE(usb
, stub_table
);
57 struct usb_driver stub_driver
= {
60 .disconnect
= stub_disconnect
,
61 .id_table
= stub_table
,
65 * usbip_status shows status of usbip as long as this driver is bound to the
68 static ssize_t
show_status(struct device
*dev
, struct device_attribute
*attr
,
71 struct stub_device
*sdev
= dev_get_drvdata(dev
);
75 dev_err(dev
, "sdev is null\n");
79 spin_lock(&sdev
->ud
.lock
);
80 status
= sdev
->ud
.status
;
81 spin_unlock(&sdev
->ud
.lock
);
83 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
85 static DEVICE_ATTR(usbip_status
, S_IRUGO
, show_status
, NULL
);
88 * usbip_sockfd gets a socket descriptor of an established TCP connection that
89 * is used to transfer usbip requests by kernel threads. -1 is a magic number
90 * by which usbip connection is finished.
92 static ssize_t
store_sockfd(struct device
*dev
, struct device_attribute
*attr
,
93 const char *buf
, size_t count
)
95 struct stub_device
*sdev
= dev_get_drvdata(dev
);
97 struct socket
*socket
;
100 dev_err(dev
, "sdev is null\n");
104 sscanf(buf
, "%d", &sockfd
);
107 dev_info(dev
, "stub up\n");
109 spin_lock(&sdev
->ud
.lock
);
111 if (sdev
->ud
.status
!= SDEV_ST_AVAILABLE
) {
112 dev_err(dev
, "not ready\n");
113 spin_unlock(&sdev
->ud
.lock
);
117 socket
= sockfd_to_socket(sockfd
);
119 spin_unlock(&sdev
->ud
.lock
);
124 setkeepalive(socket
);
127 sdev
->ud
.tcp_socket
= socket
;
129 spin_unlock(&sdev
->ud
.lock
);
131 sdev
->ud
.tcp_rx
= kthread_run(stub_rx_loop
, &sdev
->ud
, "stub_rx");
132 sdev
->ud
.tcp_tx
= kthread_run(stub_tx_loop
, &sdev
->ud
, "stub_tx");
134 spin_lock(&sdev
->ud
.lock
);
135 sdev
->ud
.status
= SDEV_ST_USED
;
136 spin_unlock(&sdev
->ud
.lock
);
139 dev_info(dev
, "stub down\n");
141 spin_lock(&sdev
->ud
.lock
);
142 if (sdev
->ud
.status
!= SDEV_ST_USED
) {
143 spin_unlock(&sdev
->ud
.lock
);
146 spin_unlock(&sdev
->ud
.lock
);
148 usbip_event_add(&sdev
->ud
, SDEV_EVENT_DOWN
);
153 static DEVICE_ATTR(usbip_sockfd
, S_IWUSR
, NULL
, store_sockfd
);
155 static int stub_add_files(struct device
*dev
)
159 err
= device_create_file(dev
, &dev_attr_usbip_status
);
163 err
= device_create_file(dev
, &dev_attr_usbip_sockfd
);
167 err
= device_create_file(dev
, &dev_attr_usbip_debug
);
174 device_remove_file(dev
, &dev_attr_usbip_sockfd
);
176 device_remove_file(dev
, &dev_attr_usbip_status
);
181 static void stub_remove_files(struct device
*dev
)
183 device_remove_file(dev
, &dev_attr_usbip_status
);
184 device_remove_file(dev
, &dev_attr_usbip_sockfd
);
185 device_remove_file(dev
, &dev_attr_usbip_debug
);
188 static void stub_shutdown_connection(struct usbip_device
*ud
)
190 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
193 * When removing an exported device, kernel panic sometimes occurred
194 * and then EIP was sk_wait_data of stub_rx thread. Is this because
195 * sk_wait_data returned though stub_rx thread was already finished by
198 if (ud
->tcp_socket
) {
199 usbip_udbg("shutdown tcp_socket %p\n", ud
->tcp_socket
);
200 kernel_sock_shutdown(ud
->tcp_socket
, SHUT_RDWR
);
203 /* 1. stop threads */
204 if (ud
->tcp_rx
&& !task_is_dead(ud
->tcp_rx
))
205 kthread_stop(ud
->tcp_rx
);
206 if (ud
->tcp_tx
&& !task_is_dead(ud
->tcp_tx
))
207 kthread_stop(ud
->tcp_tx
);
209 /* 2. close the socket */
211 * tcp_socket is freed after threads are killed.
212 * So usbip_xmit do not touch NULL socket.
214 if (ud
->tcp_socket
) {
215 sock_release(ud
->tcp_socket
);
216 ud
->tcp_socket
= NULL
;
219 /* 3. free used data */
220 stub_device_cleanup_urbs(sdev
);
222 /* 4. free stub_unlink */
225 struct stub_unlink
*unlink
, *tmp
;
227 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
228 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_tx
, list
) {
229 list_del(&unlink
->list
);
232 list_for_each_entry_safe(unlink
, tmp
,
233 &sdev
->unlink_free
, list
) {
234 list_del(&unlink
->list
);
237 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
241 static void stub_device_reset(struct usbip_device
*ud
)
243 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
244 struct usb_device
*udev
= sdev
->udev
;
247 usbip_udbg("device reset");
249 ret
= usb_lock_device_for_reset(udev
, sdev
->interface
);
251 dev_err(&udev
->dev
, "lock for reset\n");
253 spin_lock(&ud
->lock
);
254 ud
->status
= SDEV_ST_ERROR
;
255 spin_unlock(&ud
->lock
);
259 /* try to reset the device */
260 ret
= usb_reset_device(udev
);
262 usb_unlock_device(udev
);
264 spin_lock(&ud
->lock
);
266 dev_err(&udev
->dev
, "device reset\n");
267 ud
->status
= SDEV_ST_ERROR
;
270 dev_info(&udev
->dev
, "device reset\n");
271 ud
->status
= SDEV_ST_AVAILABLE
;
274 spin_unlock(&ud
->lock
);
279 static void stub_device_unusable(struct usbip_device
*ud
)
281 spin_lock(&ud
->lock
);
282 ud
->status
= SDEV_ST_ERROR
;
283 spin_unlock(&ud
->lock
);
287 * stub_device_alloc - allocate a new stub_device struct
288 * @interface: usb_interface of a new device
290 * Allocates and initializes a new stub_device struct.
292 static struct stub_device
*stub_device_alloc(struct usb_device
*udev
,
293 struct usb_interface
*interface
)
295 struct stub_device
*sdev
;
296 int busnum
= interface_to_busnum(interface
);
297 int devnum
= interface_to_devnum(interface
);
299 dev_dbg(&interface
->dev
, "allocating stub device");
301 /* yes, it's a new device */
302 sdev
= kzalloc(sizeof(struct stub_device
), GFP_KERNEL
);
304 dev_err(&interface
->dev
, "no memory for stub_device\n");
308 sdev
->interface
= usb_get_intf(interface
);
309 sdev
->udev
= usb_get_dev(udev
);
312 * devid is defined with devnum when this driver is first allocated.
313 * devnum may change later if a device is reset. However, devid never
314 * changes during a usbip connection.
316 sdev
->devid
= (busnum
<< 16) | devnum
;
317 sdev
->ud
.side
= USBIP_STUB
;
318 sdev
->ud
.status
= SDEV_ST_AVAILABLE
;
319 /* sdev->ud.lock = SPIN_LOCK_UNLOCKED; */
320 spin_lock_init(&sdev
->ud
.lock
);
321 sdev
->ud
.tcp_socket
= NULL
;
323 INIT_LIST_HEAD(&sdev
->priv_init
);
324 INIT_LIST_HEAD(&sdev
->priv_tx
);
325 INIT_LIST_HEAD(&sdev
->priv_free
);
326 INIT_LIST_HEAD(&sdev
->unlink_free
);
327 INIT_LIST_HEAD(&sdev
->unlink_tx
);
328 /* sdev->priv_lock = SPIN_LOCK_UNLOCKED; */
329 spin_lock_init(&sdev
->priv_lock
);
331 init_waitqueue_head(&sdev
->tx_waitq
);
333 sdev
->ud
.eh_ops
.shutdown
= stub_shutdown_connection
;
334 sdev
->ud
.eh_ops
.reset
= stub_device_reset
;
335 sdev
->ud
.eh_ops
.unusable
= stub_device_unusable
;
337 usbip_start_eh(&sdev
->ud
);
339 usbip_udbg("register new interface\n");
343 static int stub_device_free(struct stub_device
*sdev
)
349 usbip_udbg("kfree udev ok\n");
355 * If a usb device has multiple active interfaces, this driver is bound to all
356 * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
357 * active interface). Currently, a userland program must ensure that it
358 * looks at the usbip's sysfs entries of only the first active interface.
360 * TODO: use "struct usb_device_driver" to bind a usb device.
361 * However, it seems it is not fully supported in mainline kernel yet
364 static int stub_probe(struct usb_interface
*interface
,
365 const struct usb_device_id
*id
)
367 struct usb_device
*udev
= interface_to_usbdev(interface
);
368 struct stub_device
*sdev
= NULL
;
369 const char *udev_busid
= dev_name(interface
->dev
.parent
);
371 struct bus_id_priv
*busid_priv
;
373 dev_dbg(&interface
->dev
, "Enter\n");
375 /* check we should claim or not by busid_table */
376 busid_priv
= get_busid_priv(udev_busid
);
377 if (!busid_priv
|| (busid_priv
->status
== STUB_BUSID_REMOV
) ||
378 (busid_priv
->status
== STUB_BUSID_OTHER
)) {
379 dev_info(&interface
->dev
,
380 "this device %s is not in match_busid table. skip!\n",
384 * Return value should be ENODEV or ENOXIO to continue trying
385 * other matched drivers by the driver core.
386 * See driver_probe_device() in driver/base/dd.c
391 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
) {
392 usbip_udbg("this device %s is a usb hub device. skip!\n",
397 if (!strcmp(udev
->bus
->bus_name
, "vhci_hcd")) {
398 usbip_udbg("this device %s is attached on vhci_hcd. skip!\n",
403 if (busid_priv
->status
== STUB_BUSID_ALLOC
) {
404 sdev
= busid_priv
->sdev
;
408 busid_priv
->interf_count
++;
409 dev_info(&interface
->dev
,
410 "USB/IP Stub: register a new interface "
411 "(bus %u dev %u ifn %u)\n",
412 udev
->bus
->busnum
, udev
->devnum
,
413 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
415 /* set private data to usb_interface */
416 usb_set_intfdata(interface
, sdev
);
418 err
= stub_add_files(&interface
->dev
);
420 dev_err(&interface
->dev
, "create sysfs files for %s\n",
422 usb_set_intfdata(interface
, NULL
);
423 busid_priv
->interf_count
--;
428 usb_get_intf(interface
);
432 /* ok. this is my device. */
433 sdev
= stub_device_alloc(udev
, interface
);
437 dev_info(&interface
->dev
, "USB/IP Stub: register a new device "
438 "(bus %u dev %u ifn %u)\n", udev
->bus
->busnum
, udev
->devnum
,
439 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
441 busid_priv
->interf_count
= 0;
442 busid_priv
->shutdown_busid
= 0;
444 /* set private data to usb_interface */
445 usb_set_intfdata(interface
, sdev
);
446 busid_priv
->interf_count
++;
448 busid_priv
->sdev
= sdev
;
450 err
= stub_add_files(&interface
->dev
);
452 dev_err(&interface
->dev
, "create sysfs files for %s\n",
454 usb_set_intfdata(interface
, NULL
);
455 usb_put_intf(interface
);
457 busid_priv
->interf_count
= 0;
459 busid_priv
->sdev
= NULL
;
460 stub_device_free(sdev
);
463 busid_priv
->status
= STUB_BUSID_ALLOC
;
468 static void shutdown_busid(struct bus_id_priv
*busid_priv
)
470 if (busid_priv
->sdev
&& !busid_priv
->shutdown_busid
) {
471 busid_priv
->shutdown_busid
= 1;
472 usbip_event_add(&busid_priv
->sdev
->ud
, SDEV_EVENT_REMOVED
);
474 /* 2. wait for the stop of the event handler */
475 usbip_stop_eh(&busid_priv
->sdev
->ud
);
480 * called in usb_disconnect() or usb_deregister()
481 * but only if actconfig(active configuration) exists
483 static void stub_disconnect(struct usb_interface
*interface
)
485 struct stub_device
*sdev
;
486 const char *udev_busid
= dev_name(interface
->dev
.parent
);
487 struct bus_id_priv
*busid_priv
;
489 busid_priv
= get_busid_priv(udev_busid
);
491 usbip_udbg("Enter\n");
498 sdev
= usb_get_intfdata(interface
);
500 /* get stub_device */
502 err(" could not get device from inteface data");
507 usb_set_intfdata(interface
, NULL
);
511 * rx/tx threads are invoked for each usb_device.
513 stub_remove_files(&interface
->dev
);
515 /*If usb reset called from event handler*/
516 if (busid_priv
->sdev
->ud
.eh
== current
) {
517 busid_priv
->interf_count
--;
521 if (busid_priv
->interf_count
> 1) {
522 busid_priv
->interf_count
--;
523 shutdown_busid(busid_priv
);
524 usb_put_intf(interface
);
528 busid_priv
->interf_count
= 0;
530 /* 1. shutdown the current connection */
531 shutdown_busid(busid_priv
);
533 usb_put_dev(sdev
->udev
);
534 usb_put_intf(interface
);
537 busid_priv
->sdev
= NULL
;
538 stub_device_free(sdev
);
540 if (busid_priv
->status
== STUB_BUSID_ALLOC
) {
541 busid_priv
->status
= STUB_BUSID_ADDED
;
543 busid_priv
->status
= STUB_BUSID_OTHER
;
544 del_match_busid((char *)udev_busid
);