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/file.h>
22 #include <linux/kthread.h>
23 #include <linux/module.h>
25 #include "usbip_common.h"
29 * Define device IDs here if you want to explicitly limit exportable devices.
30 * In most cases, wildcard matching will be okay because driver binding can be
31 * changed dynamically by a userland program.
33 static struct usb_device_id stub_table[] = {
36 { USB_DEVICE(0x05ac, 0x0301) }, /* Mac 1 button mouse */
37 { USB_DEVICE(0x0430, 0x0009) }, /* Plat Home Keyboard */
38 { USB_DEVICE(0x059b, 0x0001) }, /* Iomega USB Zip 100 */
39 { USB_DEVICE(0x04b3, 0x4427) }, /* IBM USB CD-ROM */
40 { USB_DEVICE(0x05a9, 0xa511) }, /* LifeView USB cam */
41 { USB_DEVICE(0x55aa, 0x0201) }, /* Imation card reader */
42 { USB_DEVICE(0x046d, 0x0870) }, /* Qcam Express(QV-30) */
43 { USB_DEVICE(0x04bb, 0x0101) }, /* IO-DATA HD 120GB */
44 { USB_DEVICE(0x04bb, 0x0904) }, /* IO-DATA USB-ET/TX */
45 { USB_DEVICE(0x04bb, 0x0201) }, /* IO-DATA USB-ET/TX */
46 { USB_DEVICE(0x08bb, 0x2702) }, /* ONKYO USB Speaker */
47 { USB_DEVICE(0x046d, 0x08b2) }, /* Logicool Qcam 4000 Pro */
49 /* magic for wild card */
51 { 0, } /* Terminating entry */
53 MODULE_DEVICE_TABLE(usb, stub_table);
56 * usbip_status shows the status of usbip-host as long as this driver is bound
57 * to the target device.
59 static ssize_t usbip_status_show(struct device *dev,
60 struct device_attribute *attr, char *buf)
62 struct stub_device *sdev = dev_get_drvdata(dev);
66 dev_err(dev, "sdev is null\n");
70 spin_lock_irq(&sdev->ud.lock);
71 status = sdev->ud.status;
72 spin_unlock_irq(&sdev->ud.lock);
74 return snprintf(buf, PAGE_SIZE, "%d\n", status);
76 static DEVICE_ATTR_RO(usbip_status);
79 * usbip_sockfd gets a socket descriptor of an established TCP connection that
80 * is used to transfer usbip requests by kernel threads. -1 is a magic number
81 * by which usbip connection is finished.
83 static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
84 const char *buf, size_t count)
86 struct stub_device *sdev = dev_get_drvdata(dev);
88 struct socket *socket;
89 ssize_t err = -EINVAL;
93 dev_err(dev, "sdev is null\n");
97 rv = sscanf(buf, "%d", &sockfd);
102 dev_info(dev, "stub up\n");
104 spin_lock_irq(&sdev->ud.lock);
106 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
107 dev_err(dev, "not ready\n");
111 socket = sockfd_to_socket(sockfd);
115 sdev->ud.tcp_socket = socket;
117 spin_unlock_irq(&sdev->ud.lock);
119 sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud,
121 sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud,
124 spin_lock_irq(&sdev->ud.lock);
125 sdev->ud.status = SDEV_ST_USED;
126 spin_unlock_irq(&sdev->ud.lock);
129 dev_info(dev, "stub down\n");
131 spin_lock_irq(&sdev->ud.lock);
132 if (sdev->ud.status != SDEV_ST_USED)
135 spin_unlock_irq(&sdev->ud.lock);
137 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
143 spin_unlock_irq(&sdev->ud.lock);
146 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
148 static int stub_add_files(struct device *dev)
152 err = device_create_file(dev, &dev_attr_usbip_status);
156 err = device_create_file(dev, &dev_attr_usbip_sockfd);
160 err = device_create_file(dev, &dev_attr_usbip_debug);
167 device_remove_file(dev, &dev_attr_usbip_sockfd);
169 device_remove_file(dev, &dev_attr_usbip_status);
174 static void stub_remove_files(struct device *dev)
176 device_remove_file(dev, &dev_attr_usbip_status);
177 device_remove_file(dev, &dev_attr_usbip_sockfd);
178 device_remove_file(dev, &dev_attr_usbip_debug);
181 static void stub_shutdown_connection(struct usbip_device *ud)
183 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
186 * When removing an exported device, kernel panic sometimes occurred
187 * and then EIP was sk_wait_data of stub_rx thread. Is this because
188 * sk_wait_data returned though stub_rx thread was already finished by
191 if (ud->tcp_socket) {
192 dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n",
194 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
197 /* 1. stop threads */
199 kthread_stop_put(ud->tcp_rx);
203 kthread_stop_put(ud->tcp_tx);
208 * 2. close the socket
210 * tcp_socket is freed after threads are killed so that usbip_xmit does
211 * not touch NULL socket.
213 if (ud->tcp_socket) {
214 fput(ud->tcp_socket->file);
215 ud->tcp_socket = NULL;
218 /* 3. free used data */
219 stub_device_cleanup_urbs(sdev);
221 /* 4. free stub_unlink */
224 struct stub_unlink *unlink, *tmp;
226 spin_lock_irqsave(&sdev->priv_lock, flags);
227 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
228 list_del(&unlink->list);
231 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
233 list_del(&unlink->list);
236 spin_unlock_irqrestore(&sdev->priv_lock, flags);
240 static void stub_device_reset(struct usbip_device *ud)
242 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
243 struct usb_device *udev = sdev->udev;
246 dev_dbg(&udev->dev, "device reset");
248 ret = usb_lock_device_for_reset(udev, sdev->interface);
250 dev_err(&udev->dev, "lock for reset\n");
251 spin_lock_irq(&ud->lock);
252 ud->status = SDEV_ST_ERROR;
253 spin_unlock_irq(&ud->lock);
257 /* try to reset the device */
258 ret = usb_reset_device(udev);
259 usb_unlock_device(udev);
261 spin_lock_irq(&ud->lock);
263 dev_err(&udev->dev, "device reset\n");
264 ud->status = SDEV_ST_ERROR;
266 dev_info(&udev->dev, "device reset\n");
267 ud->status = SDEV_ST_AVAILABLE;
269 spin_unlock_irq(&ud->lock);
272 static void stub_device_unusable(struct usbip_device *ud)
274 spin_lock_irq(&ud->lock);
275 ud->status = SDEV_ST_ERROR;
276 spin_unlock_irq(&ud->lock);
280 * stub_device_alloc - allocate a new stub_device struct
281 * @interface: usb_interface of a new device
283 * Allocates and initializes a new stub_device struct.
285 static struct stub_device *stub_device_alloc(struct usb_device *udev)
287 struct stub_device *sdev;
288 int busnum = udev->bus->busnum;
289 int devnum = udev->devnum;
291 dev_dbg(&udev->dev, "allocating stub device");
293 /* yes, it's a new device */
294 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
298 sdev->udev = usb_get_dev(udev);
301 * devid is defined with devnum when this driver is first allocated.
302 * devnum may change later if a device is reset. However, devid never
303 * changes during a usbip connection.
305 sdev->devid = (busnum << 16) | devnum;
306 sdev->ud.side = USBIP_STUB;
307 sdev->ud.status = SDEV_ST_AVAILABLE;
308 spin_lock_init(&sdev->ud.lock);
309 sdev->ud.tcp_socket = NULL;
311 INIT_LIST_HEAD(&sdev->priv_init);
312 INIT_LIST_HEAD(&sdev->priv_tx);
313 INIT_LIST_HEAD(&sdev->priv_free);
314 INIT_LIST_HEAD(&sdev->unlink_free);
315 INIT_LIST_HEAD(&sdev->unlink_tx);
316 spin_lock_init(&sdev->priv_lock);
318 init_waitqueue_head(&sdev->tx_waitq);
320 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
321 sdev->ud.eh_ops.reset = stub_device_reset;
322 sdev->ud.eh_ops.unusable = stub_device_unusable;
324 usbip_start_eh(&sdev->ud);
326 dev_dbg(&udev->dev, "register new device\n");
331 static void stub_device_free(struct stub_device *sdev)
336 static int stub_probe(struct usb_device *udev)
338 struct stub_device *sdev = NULL;
339 const char *udev_busid = dev_name(&udev->dev);
341 struct bus_id_priv *busid_priv;
344 dev_dbg(&udev->dev, "Enter\n");
346 /* check we should claim or not by busid_table */
347 busid_priv = get_busid_priv(udev_busid);
348 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
349 (busid_priv->status == STUB_BUSID_OTHER)) {
351 "%s is not in match_busid table... skip!\n",
355 * Return value should be ENODEV or ENOXIO to continue trying
356 * other matched drivers by the driver core.
357 * See driver_probe_device() in driver/base/dd.c
362 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
363 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
368 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
370 "%s is attached on vhci_hcd... skip!\n",
376 /* ok, this is my device */
377 sdev = stub_device_alloc(udev);
382 "usbip-host: register new device (bus %u dev %u)\n",
383 udev->bus->busnum, udev->devnum);
385 busid_priv->shutdown_busid = 0;
387 /* set private data to usb_device */
388 dev_set_drvdata(&udev->dev, sdev);
389 busid_priv->sdev = sdev;
390 busid_priv->udev = udev;
393 * Claim this hub port.
394 * It doesn't matter what value we pass as owner
395 * (struct dev_state) as long as it is unique.
397 rc = usb_hub_claim_port(udev->parent, udev->portnum,
398 (struct usb_dev_state *) udev);
400 dev_dbg(&udev->dev, "unable to claim port\n");
404 err = stub_add_files(&udev->dev);
406 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
407 dev_set_drvdata(&udev->dev, NULL);
409 kthread_stop_put(sdev->ud.eh);
411 busid_priv->sdev = NULL;
412 stub_device_free(sdev);
415 busid_priv->status = STUB_BUSID_ALLOC;
420 static void shutdown_busid(struct bus_id_priv *busid_priv)
422 if (busid_priv->sdev && !busid_priv->shutdown_busid) {
423 busid_priv->shutdown_busid = 1;
424 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
426 /* wait for the stop of the event handler */
427 usbip_stop_eh(&busid_priv->sdev->ud);
432 * called in usb_disconnect() or usb_deregister()
433 * but only if actconfig(active configuration) exists
435 static void stub_disconnect(struct usb_device *udev)
437 struct stub_device *sdev;
438 const char *udev_busid = dev_name(&udev->dev);
439 struct bus_id_priv *busid_priv;
442 dev_dbg(&udev->dev, "Enter\n");
444 busid_priv = get_busid_priv(udev_busid);
450 sdev = dev_get_drvdata(&udev->dev);
452 /* get stub_device */
454 dev_err(&udev->dev, "could not get device");
458 dev_set_drvdata(&udev->dev, NULL);
461 * NOTE: rx/tx threads are invoked for each usb_device.
463 stub_remove_files(&udev->dev);
466 rc = usb_hub_release_port(udev->parent, udev->portnum,
467 (struct usb_dev_state *) udev);
469 dev_dbg(&udev->dev, "unable to release port\n");
473 /* If usb reset is called from event handler */
474 if (busid_priv->sdev->ud.eh == current)
477 /* shutdown the current connection */
478 shutdown_busid(busid_priv);
480 usb_put_dev(sdev->udev);
483 busid_priv->sdev = NULL;
484 stub_device_free(sdev);
486 if (busid_priv->status == STUB_BUSID_ALLOC) {
487 busid_priv->status = STUB_BUSID_ADDED;
489 busid_priv->status = STUB_BUSID_OTHER;
490 del_match_busid((char *)udev_busid);
496 /* These functions need usb_port_suspend and usb_port_resume,
497 * which reside in drivers/usb/core/usb.h. Skip for now. */
499 static int stub_suspend(struct usb_device *udev, pm_message_t message)
501 dev_dbg(&udev->dev, "stub_suspend\n");
506 static int stub_resume(struct usb_device *udev, pm_message_t message)
508 dev_dbg(&udev->dev, "stub_resume\n");
513 #endif /* CONFIG_PM */
515 struct usb_device_driver stub_driver = {
516 .name = "usbip-host",
518 .disconnect = stub_disconnect,
520 .suspend = stub_suspend,
521 .resume = stub_resume,
523 .supports_autosuspend = 0,