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
[] = {
38 /* magic for wild card */
40 { 0, } /* Terminating entry */
42 MODULE_DEVICE_TABLE(usb
, stub_table
);
44 struct usb_driver stub_driver
= {
47 .disconnect
= stub_disconnect
,
48 .id_table
= stub_table
,
52 /*-------------------------------------------------------------------------*/
54 /* Define sysfs entries for a usbip-bound device */
58 * usbip_status shows status of usbip as long as this driver is bound to the
61 static ssize_t
show_status(struct device
*dev
, struct device_attribute
*attr
,
64 struct stub_device
*sdev
= dev_get_drvdata(dev
);
68 dev_err(dev
, "sdev is null\n");
72 spin_lock(&sdev
->ud
.lock
);
73 status
= sdev
->ud
.status
;
74 spin_unlock(&sdev
->ud
.lock
);
76 return snprintf(buf
, PAGE_SIZE
, "%d\n", status
);
78 static DEVICE_ATTR(usbip_status
, S_IRUGO
, show_status
, NULL
);
81 * usbip_sockfd gets a socket descriptor of an established TCP connection that
82 * is used to transfer usbip requests by kernel threads. -1 is a magic number
83 * by which usbip connection is finished.
85 static ssize_t
store_sockfd(struct device
*dev
, struct device_attribute
*attr
,
86 const char *buf
, size_t count
)
88 struct stub_device
*sdev
= dev_get_drvdata(dev
);
90 struct socket
*socket
;
93 dev_err(dev
, "sdev is null\n");
97 sscanf(buf
, "%d", &sockfd
);
100 dev_info(dev
, "stub up\n");
102 spin_lock(&sdev
->ud
.lock
);
104 if (sdev
->ud
.status
!= SDEV_ST_AVAILABLE
) {
105 dev_err(dev
, "not ready\n");
106 spin_unlock(&sdev
->ud
.lock
);
110 socket
= sockfd_to_socket(sockfd
);
112 spin_unlock(&sdev
->ud
.lock
);
117 sdev
->ud
.tcp_socket
= socket
;
119 spin_unlock(&sdev
->ud
.lock
);
121 usbip_start_threads(&sdev
->ud
);
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
);
166 device_remove_file(dev
, &dev_attr_usbip_status
);
172 static void stub_remove_files(struct device
*dev
)
174 device_remove_file(dev
, &dev_attr_usbip_status
);
175 device_remove_file(dev
, &dev_attr_usbip_sockfd
);
176 device_remove_file(dev
, &dev_attr_usbip_debug
);
181 /*-------------------------------------------------------------------------*/
183 /* Event handler functions called by an event handler thread */
185 static void stub_shutdown_connection(struct usbip_device
*ud
)
187 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
190 * When removing an exported device, kernel panic sometimes occurred
191 * and then EIP was sk_wait_data of stub_rx thread. Is this because
192 * sk_wait_data returned though stub_rx thread was already finished by
195 if (ud
->tcp_socket
) {
196 usbip_udbg("shutdown tcp_socket %p\n", ud
->tcp_socket
);
197 kernel_sock_shutdown(ud
->tcp_socket
, SHUT_RDWR
);
200 /* 1. stop threads */
201 usbip_stop_threads(ud
);
203 /* 2. close the socket */
205 * tcp_socket is freed after threads are killed.
206 * So usbip_xmit do not touch NULL socket.
208 if (ud
->tcp_socket
) {
209 sock_release(ud
->tcp_socket
);
210 ud
->tcp_socket
= NULL
;
213 /* 3. free used data */
214 stub_device_cleanup_urbs(sdev
);
216 /* 4. free stub_unlink */
219 struct stub_unlink
*unlink
, *tmp
;
221 spin_lock_irqsave(&sdev
->priv_lock
, flags
);
223 list_for_each_entry_safe(unlink
, tmp
, &sdev
->unlink_tx
, list
) {
224 list_del(&unlink
->list
);
228 list_for_each_entry_safe(unlink
, tmp
,
229 &sdev
->unlink_free
, list
) {
230 list_del(&unlink
->list
);
234 spin_unlock_irqrestore(&sdev
->priv_lock
, flags
);
238 static void stub_device_reset(struct usbip_device
*ud
)
240 struct stub_device
*sdev
= container_of(ud
, struct stub_device
, ud
);
241 struct usb_device
*udev
= interface_to_usbdev(sdev
->interface
);
244 usbip_udbg("device reset");
245 ret
= usb_lock_device_for_reset(udev
, sdev
->interface
);
247 dev_err(&udev
->dev
, "lock for reset\n");
249 spin_lock(&ud
->lock
);
250 ud
->status
= SDEV_ST_ERROR
;
251 spin_unlock(&ud
->lock
);
256 /* try to reset the device */
257 ret
= usb_reset_device(udev
);
259 usb_unlock_device(udev
);
261 spin_lock(&ud
->lock
);
263 dev_err(&udev
->dev
, "device reset\n");
264 ud
->status
= SDEV_ST_ERROR
;
267 dev_info(&udev
->dev
, "device reset\n");
268 ud
->status
= SDEV_ST_AVAILABLE
;
271 spin_unlock(&ud
->lock
);
276 static void stub_device_unusable(struct usbip_device
*ud
)
278 spin_lock(&ud
->lock
);
279 ud
->status
= SDEV_ST_ERROR
;
280 spin_unlock(&ud
->lock
);
284 /*-------------------------------------------------------------------------*/
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_interface
*interface
)
294 struct stub_device
*sdev
;
295 int busnum
= interface_to_busnum(interface
);
296 int devnum
= interface_to_devnum(interface
);
298 dev_dbg(&interface
->dev
, "allocating stub device");
300 /* yes, it's a new device */
301 sdev
= kzalloc(sizeof(struct stub_device
), GFP_KERNEL
);
303 dev_err(&interface
->dev
, "no memory for stub_device\n");
307 sdev
->interface
= interface
;
310 * devid is defined with devnum when this driver is first allocated.
311 * devnum may change later if a device is reset. However, devid never
312 * changes during a usbip connection.
314 sdev
->devid
= (busnum
<< 16) | devnum
;
316 usbip_task_init(&sdev
->ud
.tcp_rx
, "stub_rx", stub_rx_loop
);
317 usbip_task_init(&sdev
->ud
.tcp_tx
, "stub_tx", stub_tx_loop
);
319 sdev
->ud
.side
= USBIP_STUB
;
320 sdev
->ud
.status
= SDEV_ST_AVAILABLE
;
321 /* sdev->ud.lock = SPIN_LOCK_UNLOCKED; */
322 spin_lock_init(&sdev
->ud
.lock
);
323 sdev
->ud
.tcp_socket
= NULL
;
325 INIT_LIST_HEAD(&sdev
->priv_init
);
326 INIT_LIST_HEAD(&sdev
->priv_tx
);
327 INIT_LIST_HEAD(&sdev
->priv_free
);
328 INIT_LIST_HEAD(&sdev
->unlink_free
);
329 INIT_LIST_HEAD(&sdev
->unlink_tx
);
330 /* sdev->priv_lock = SPIN_LOCK_UNLOCKED; */
331 spin_lock_init(&sdev
->priv_lock
);
333 init_waitqueue_head(&sdev
->tx_waitq
);
335 sdev
->ud
.eh_ops
.shutdown
= stub_shutdown_connection
;
336 sdev
->ud
.eh_ops
.reset
= stub_device_reset
;
337 sdev
->ud
.eh_ops
.unusable
= stub_device_unusable
;
339 usbip_start_eh(&sdev
->ud
);
341 usbip_udbg("register new interface\n");
345 static int stub_device_free(struct stub_device
*sdev
)
351 usbip_udbg("kfree udev ok\n");
357 /*-------------------------------------------------------------------------*/
360 * If a usb device has multiple active interfaces, this driver is bound to all
361 * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
362 * active interface). Currently, a userland program must ensure that it
363 * looks at the usbip's sysfs entries of only the first active interface.
365 * TODO: use "struct usb_device_driver" to bind a usb device.
366 * However, it seems it is not fully supported in mainline kernel yet
369 static int stub_probe(struct usb_interface
*interface
,
370 const struct usb_device_id
*id
)
372 struct usb_device
*udev
= interface_to_usbdev(interface
);
373 struct stub_device
*sdev
= NULL
;
374 const char *udev_busid
= dev_name(interface
->dev
.parent
);
376 struct bus_id_priv
*busid_priv
;
378 dev_dbg(&interface
->dev
, "Enter\n");
380 /* check we should claim or not by busid_table */
381 busid_priv
= get_busid_priv(udev_busid
);
382 if (!busid_priv
|| (busid_priv
->status
== STUB_BUSID_REMOV
) ||
383 (busid_priv
->status
== STUB_BUSID_OTHER
)) {
384 dev_info(&interface
->dev
,
385 "this device %s is not in match_busid table. skip!\n",
389 * Return value should be ENODEV or ENOXIO to continue trying
390 * other matched drivers by the driver core.
391 * See driver_probe_device() in driver/base/dd.c
396 if (udev
->descriptor
.bDeviceClass
== USB_CLASS_HUB
) {
397 usbip_udbg("this device %s is a usb hub device. skip!\n",
402 if (!strcmp(udev
->bus
->bus_name
, "vhci_hcd")) {
403 usbip_udbg("this device %s is attached on vhci_hcd. skip!\n",
409 if (busid_priv
->status
== STUB_BUSID_ALLOC
) {
410 busid_priv
->interf_count
++;
411 sdev
= busid_priv
->sdev
;
415 dev_info(&interface
->dev
,
416 "USB/IP Stub: register a new interface "
417 "(bus %u dev %u ifn %u)\n", udev
->bus
->busnum
, udev
->devnum
,
418 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
420 /* set private data to usb_interface */
421 usb_set_intfdata(interface
, sdev
);
423 err
= stub_add_files(&interface
->dev
);
425 dev_err(&interface
->dev
, "create sysfs files for %s\n",
427 usb_set_intfdata(interface
, NULL
);
428 busid_priv
->interf_count
--;
436 /* ok. this is my device. */
437 sdev
= stub_device_alloc(interface
);
441 dev_info(&interface
->dev
, "USB/IP Stub: register a new device "
442 "(bus %u dev %u ifn %u)\n", udev
->bus
->busnum
, udev
->devnum
,
443 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
445 busid_priv
->interf_count
= 0;
446 busid_priv
->shutdown_busid
= 0;
448 /* set private data to usb_interface */
449 usb_set_intfdata(interface
, sdev
);
450 busid_priv
->interf_count
++;
452 busid_priv
->sdev
= sdev
;
454 err
= stub_add_files(&interface
->dev
);
456 dev_err(&interface
->dev
, "create sysfs files for %s\n",
458 usb_set_intfdata(interface
, NULL
);
459 busid_priv
->interf_count
= 0;
461 busid_priv
->sdev
= NULL
;
462 stub_device_free(sdev
);
465 busid_priv
->status
= STUB_BUSID_ALLOC
;
470 static void shutdown_busid(struct bus_id_priv
*busid_priv
)
472 if (busid_priv
->sdev
&& !busid_priv
->shutdown_busid
) {
473 busid_priv
->shutdown_busid
= 1;
474 usbip_event_add(&busid_priv
->sdev
->ud
, SDEV_EVENT_REMOVED
);
476 /* 2. wait for the stop of the event handler */
477 usbip_stop_eh(&busid_priv
->sdev
->ud
);
484 * called in usb_disconnect() or usb_deregister()
485 * but only if actconfig(active configuration) exists
487 static void stub_disconnect(struct usb_interface
*interface
)
489 struct stub_device
*sdev
;
490 const char *udev_busid
= dev_name(interface
->dev
.parent
);
491 struct bus_id_priv
*busid_priv
;
493 busid_priv
= get_busid_priv(udev_busid
);
495 usbip_udbg("Enter\n");
502 sdev
= usb_get_intfdata(interface
);
504 /* get stub_device */
506 err(" could not get device from inteface data");
511 usb_set_intfdata(interface
, NULL
);
515 * rx/tx threads are invoked for each usb_device.
517 stub_remove_files(&interface
->dev
);
519 /*If usb reset called from event handler*/
520 if (busid_priv
->sdev
->ud
.eh
.thread
== current
) {
521 busid_priv
->interf_count
--;
525 if (busid_priv
->interf_count
> 1) {
526 busid_priv
->interf_count
--;
527 shutdown_busid(busid_priv
);
531 busid_priv
->interf_count
= 0;
534 /* 1. shutdown the current connection */
535 shutdown_busid(busid_priv
);
538 busid_priv
->sdev
= NULL
;
539 stub_device_free(sdev
);
541 if (busid_priv
->status
== STUB_BUSID_ALLOC
) {
542 busid_priv
->status
= STUB_BUSID_ADDED
;
544 busid_priv
->status
= STUB_BUSID_OTHER
;
545 del_match_busid((char *)udev_busid
);