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>
21 #include <linux/kthread.h>
23 #include "usbip_common.h"
28 static int stub_probe(struct usb_interface *interface,
29 const struct usb_device_id *id);
30 static void stub_disconnect(struct usb_interface *interface);
34 * Define device IDs here if you want to explicitly limit exportable devices.
35 * In the most cases, wild card matching will be ok because driver binding can
36 * be changed dynamically by a userland program.
38 static struct usb_device_id stub_table[] = {
41 { USB_DEVICE(0x05ac, 0x0301) }, /* Mac 1 button mouse */
42 { USB_DEVICE(0x0430, 0x0009) }, /* Plat Home Keyboard */
43 { USB_DEVICE(0x059b, 0x0001) }, /* Iomega USB Zip 100 */
44 { USB_DEVICE(0x04b3, 0x4427) }, /* IBM USB CD-ROM */
45 { USB_DEVICE(0x05a9, 0xa511) }, /* LifeView USB cam */
46 { USB_DEVICE(0x55aa, 0x0201) }, /* Imation card reader */
47 { USB_DEVICE(0x046d, 0x0870) }, /* Qcam Express(QV-30) */
48 { USB_DEVICE(0x04bb, 0x0101) }, /* IO-DATA HD 120GB */
49 { USB_DEVICE(0x04bb, 0x0904) }, /* IO-DATA USB-ET/TX */
50 { USB_DEVICE(0x04bb, 0x0201) }, /* IO-DATA USB-ET/TX */
51 { USB_DEVICE(0x08bb, 0x2702) }, /* ONKYO USB Speaker */
52 { USB_DEVICE(0x046d, 0x08b2) }, /* Logicool Qcam 4000 Pro */
54 /* magic for wild card */
56 { 0, } /* Terminating entry */
58 MODULE_DEVICE_TABLE(usb, stub_table);
60 struct usb_driver stub_driver = {
63 .disconnect = stub_disconnect,
64 .id_table = stub_table,
68 /*-------------------------------------------------------------------------*/
70 /* Define sysfs entries for a usbip-bound device */
74 * usbip_status shows status of usbip as long as this driver is bound to the
77 static ssize_t show_status(struct device *dev, struct device_attribute *attr,
80 struct stub_device *sdev = dev_get_drvdata(dev);
84 dev_err(dev, "sdev is null\n");
88 spin_lock(&sdev->ud.lock);
89 status = sdev->ud.status;
90 spin_unlock(&sdev->ud.lock);
92 return snprintf(buf, PAGE_SIZE, "%d\n", status);
94 static DEVICE_ATTR(usbip_status, S_IRUGO, show_status, NULL);
97 * usbip_sockfd gets a socket descriptor of an established TCP connection that
98 * is used to transfer usbip requests by kernel threads. -1 is a magic number
99 * by which usbip connection is finished.
101 static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
102 const char *buf, size_t count)
104 struct stub_device *sdev = dev_get_drvdata(dev);
106 struct socket *socket;
109 dev_err(dev, "sdev is null\n");
113 sscanf(buf, "%d", &sockfd);
116 dev_info(dev, "stub up\n");
118 spin_lock(&sdev->ud.lock);
120 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
121 dev_err(dev, "not ready\n");
122 spin_unlock(&sdev->ud.lock);
126 socket = sockfd_to_socket(sockfd);
128 spin_unlock(&sdev->ud.lock);
134 setkeepalive(socket);
138 sdev->ud.tcp_socket = socket;
140 spin_unlock(&sdev->ud.lock);
142 sdev->ud.tcp_rx = kthread_run(stub_rx_loop, &sdev->ud, "stub_rx");
143 sdev->ud.tcp_tx = kthread_run(stub_tx_loop, &sdev->ud, "stub_tx");
145 spin_lock(&sdev->ud.lock);
146 sdev->ud.status = SDEV_ST_USED;
147 spin_unlock(&sdev->ud.lock);
150 dev_info(dev, "stub down\n");
152 spin_lock(&sdev->ud.lock);
153 if (sdev->ud.status != SDEV_ST_USED) {
154 spin_unlock(&sdev->ud.lock);
157 spin_unlock(&sdev->ud.lock);
159 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
164 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
166 static int stub_add_files(struct device *dev)
170 err = device_create_file(dev, &dev_attr_usbip_status);
174 err = device_create_file(dev, &dev_attr_usbip_sockfd);
178 err = device_create_file(dev, &dev_attr_usbip_debug);
185 device_remove_file(dev, &dev_attr_usbip_sockfd);
188 device_remove_file(dev, &dev_attr_usbip_status);
194 static void stub_remove_files(struct device *dev)
196 device_remove_file(dev, &dev_attr_usbip_status);
197 device_remove_file(dev, &dev_attr_usbip_sockfd);
198 device_remove_file(dev, &dev_attr_usbip_debug);
203 /*-------------------------------------------------------------------------*/
205 /* Event handler functions called by an event handler thread */
207 static void stub_shutdown_connection(struct usbip_device *ud)
209 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
212 * When removing an exported device, kernel panic sometimes occurred
213 * and then EIP was sk_wait_data of stub_rx thread. Is this because
214 * sk_wait_data returned though stub_rx thread was already finished by
217 if (ud->tcp_socket) {
218 usbip_udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
219 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
222 /* 1. stop threads */
223 if (ud->tcp_rx && !task_is_dead(ud->tcp_rx))
224 kthread_stop(ud->tcp_rx);
225 if (ud->tcp_tx && !task_is_dead(ud->tcp_tx))
226 kthread_stop(ud->tcp_tx);
228 /* 2. close the socket */
230 * tcp_socket is freed after threads are killed.
231 * So usbip_xmit do not touch NULL socket.
233 if (ud->tcp_socket) {
234 sock_release(ud->tcp_socket);
235 ud->tcp_socket = NULL;
238 /* 3. free used data */
239 stub_device_cleanup_urbs(sdev);
241 /* 4. free stub_unlink */
244 struct stub_unlink *unlink, *tmp;
246 spin_lock_irqsave(&sdev->priv_lock, flags);
248 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
249 list_del(&unlink->list);
253 list_for_each_entry_safe(unlink, tmp,
254 &sdev->unlink_free, list) {
255 list_del(&unlink->list);
259 spin_unlock_irqrestore(&sdev->priv_lock, flags);
263 static void stub_device_reset(struct usbip_device *ud)
265 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
266 struct usb_device *udev = sdev->udev;
269 usbip_udbg("device reset");
271 ret = usb_lock_device_for_reset(udev, sdev->interface);
273 dev_err(&udev->dev, "lock for reset\n");
275 spin_lock(&ud->lock);
276 ud->status = SDEV_ST_ERROR;
277 spin_unlock(&ud->lock);
282 /* try to reset the device */
283 ret = usb_reset_device(udev);
285 usb_unlock_device(udev);
287 spin_lock(&ud->lock);
289 dev_err(&udev->dev, "device reset\n");
290 ud->status = SDEV_ST_ERROR;
293 dev_info(&udev->dev, "device reset\n");
294 ud->status = SDEV_ST_AVAILABLE;
297 spin_unlock(&ud->lock);
302 static void stub_device_unusable(struct usbip_device *ud)
304 spin_lock(&ud->lock);
305 ud->status = SDEV_ST_ERROR;
306 spin_unlock(&ud->lock);
310 /*-------------------------------------------------------------------------*/
313 * stub_device_alloc - allocate a new stub_device struct
314 * @interface: usb_interface of a new device
316 * Allocates and initializes a new stub_device struct.
318 static struct stub_device *stub_device_alloc(struct usb_device *udev,
319 struct usb_interface *interface)
321 struct stub_device *sdev;
322 int busnum = interface_to_busnum(interface);
323 int devnum = interface_to_devnum(interface);
325 dev_dbg(&interface->dev, "allocating stub device");
327 /* yes, it's a new device */
328 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
330 dev_err(&interface->dev, "no memory for stub_device\n");
334 sdev->interface = usb_get_intf(interface);
335 sdev->udev = usb_get_dev(udev);
338 * devid is defined with devnum when this driver is first allocated.
339 * devnum may change later if a device is reset. However, devid never
340 * changes during a usbip connection.
342 sdev->devid = (busnum << 16) | devnum;
344 sdev->ud.side = USBIP_STUB;
345 sdev->ud.status = SDEV_ST_AVAILABLE;
346 /* sdev->ud.lock = SPIN_LOCK_UNLOCKED; */
347 spin_lock_init(&sdev->ud.lock);
348 sdev->ud.tcp_socket = NULL;
350 INIT_LIST_HEAD(&sdev->priv_init);
351 INIT_LIST_HEAD(&sdev->priv_tx);
352 INIT_LIST_HEAD(&sdev->priv_free);
353 INIT_LIST_HEAD(&sdev->unlink_free);
354 INIT_LIST_HEAD(&sdev->unlink_tx);
355 /* sdev->priv_lock = SPIN_LOCK_UNLOCKED; */
356 spin_lock_init(&sdev->priv_lock);
358 init_waitqueue_head(&sdev->tx_waitq);
360 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
361 sdev->ud.eh_ops.reset = stub_device_reset;
362 sdev->ud.eh_ops.unusable = stub_device_unusable;
364 usbip_start_eh(&sdev->ud);
366 usbip_udbg("register new interface\n");
370 static int stub_device_free(struct stub_device *sdev)
376 usbip_udbg("kfree udev ok\n");
382 /*-------------------------------------------------------------------------*/
385 * If a usb device has multiple active interfaces, this driver is bound to all
386 * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
387 * active interface). Currently, a userland program must ensure that it
388 * looks at the usbip's sysfs entries of only the first active interface.
390 * TODO: use "struct usb_device_driver" to bind a usb device.
391 * However, it seems it is not fully supported in mainline kernel yet
394 static int stub_probe(struct usb_interface *interface,
395 const struct usb_device_id *id)
397 struct usb_device *udev = interface_to_usbdev(interface);
398 struct stub_device *sdev = NULL;
399 const char *udev_busid = dev_name(interface->dev.parent);
401 struct bus_id_priv *busid_priv;
403 dev_dbg(&interface->dev, "Enter\n");
405 /* check we should claim or not by busid_table */
406 busid_priv = get_busid_priv(udev_busid);
407 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
408 (busid_priv->status == STUB_BUSID_OTHER)) {
409 dev_info(&interface->dev,
410 "this device %s is not in match_busid table. skip!\n",
414 * Return value should be ENODEV or ENOXIO to continue trying
415 * other matched drivers by the driver core.
416 * See driver_probe_device() in driver/base/dd.c
421 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
422 usbip_udbg("this device %s is a usb hub device. skip!\n",
427 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
428 usbip_udbg("this device %s is attached on vhci_hcd. skip!\n",
434 if (busid_priv->status == STUB_BUSID_ALLOC) {
435 sdev = busid_priv->sdev;
439 busid_priv->interf_count++;
440 dev_info(&interface->dev,
441 "USB/IP Stub: register a new interface "
442 "(bus %u dev %u ifn %u)\n", udev->bus->busnum, udev->devnum,
443 interface->cur_altsetting->desc.bInterfaceNumber);
445 /* set private data to usb_interface */
446 usb_set_intfdata(interface, sdev);
448 err = stub_add_files(&interface->dev);
450 dev_err(&interface->dev, "create sysfs files for %s\n",
452 usb_set_intfdata(interface, NULL);
453 busid_priv->interf_count--;
458 usb_get_intf(interface);
462 /* ok. this is my device. */
463 sdev = stub_device_alloc(udev, interface);
467 dev_info(&interface->dev, "USB/IP Stub: register a new device "
468 "(bus %u dev %u ifn %u)\n", udev->bus->busnum, udev->devnum,
469 interface->cur_altsetting->desc.bInterfaceNumber);
471 busid_priv->interf_count = 0;
472 busid_priv->shutdown_busid = 0;
474 /* set private data to usb_interface */
475 usb_set_intfdata(interface, sdev);
476 busid_priv->interf_count++;
478 busid_priv->sdev = sdev;
480 err = stub_add_files(&interface->dev);
482 dev_err(&interface->dev, "create sysfs files for %s\n",
484 usb_set_intfdata(interface, NULL);
485 usb_put_intf(interface);
487 busid_priv->interf_count = 0;
489 busid_priv->sdev = NULL;
490 stub_device_free(sdev);
493 busid_priv->status = STUB_BUSID_ALLOC;
498 static void shutdown_busid(struct bus_id_priv *busid_priv)
500 if (busid_priv->sdev && !busid_priv->shutdown_busid) {
501 busid_priv->shutdown_busid = 1;
502 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
504 /* 2. wait for the stop of the event handler */
505 usbip_stop_eh(&busid_priv->sdev->ud);
512 * called in usb_disconnect() or usb_deregister()
513 * but only if actconfig(active configuration) exists
515 static void stub_disconnect(struct usb_interface *interface)
517 struct stub_device *sdev;
518 const char *udev_busid = dev_name(interface->dev.parent);
519 struct bus_id_priv *busid_priv;
521 busid_priv = get_busid_priv(udev_busid);
523 usbip_udbg("Enter\n");
530 sdev = usb_get_intfdata(interface);
532 /* get stub_device */
534 err(" could not get device from inteface data");
539 usb_set_intfdata(interface, NULL);
543 * rx/tx threads are invoked for each usb_device.
545 stub_remove_files(&interface->dev);
547 /*If usb reset called from event handler*/
548 if (busid_priv->sdev->ud.eh == current) {
549 busid_priv->interf_count--;
553 if (busid_priv->interf_count > 1) {
554 busid_priv->interf_count--;
555 shutdown_busid(busid_priv);
556 usb_put_intf(interface);
560 busid_priv->interf_count = 0;
563 /* 1. shutdown the current connection */
564 shutdown_busid(busid_priv);
566 usb_put_dev(sdev->udev);
567 usb_put_intf(interface);
570 busid_priv->sdev = NULL;
571 stub_device_free(sdev);
573 if (busid_priv->status == STUB_BUSID_ALLOC) {
574 busid_priv->status = STUB_BUSID_ADDED;
576 busid_priv->status = STUB_BUSID_OTHER;
577 del_match_busid((char *)udev_busid);