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;
92 dev_err(dev, "sdev is null\n");
96 rv = sscanf(buf, "%d", &sockfd);
103 dev_info(dev, "stub up\n");
105 spin_lock_irq(&sdev->ud.lock);
107 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
108 dev_err(dev, "not ready\n");
112 socket = sockfd_lookup(sockfd, &err);
116 sdev->ud.tcp_socket = socket;
118 spin_unlock_irq(&sdev->ud.lock);
120 sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud,
122 sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud,
125 spin_lock_irq(&sdev->ud.lock);
126 sdev->ud.status = SDEV_ST_USED;
127 spin_unlock_irq(&sdev->ud.lock);
130 dev_info(dev, "stub down\n");
132 spin_lock_irq(&sdev->ud.lock);
133 if (sdev->ud.status != SDEV_ST_USED)
136 spin_unlock_irq(&sdev->ud.lock);
138 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
144 spin_unlock_irq(&sdev->ud.lock);
147 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
149 static int stub_add_files(struct device *dev)
153 err = device_create_file(dev, &dev_attr_usbip_status);
157 err = device_create_file(dev, &dev_attr_usbip_sockfd);
161 err = device_create_file(dev, &dev_attr_usbip_debug);
168 device_remove_file(dev, &dev_attr_usbip_sockfd);
170 device_remove_file(dev, &dev_attr_usbip_status);
175 static void stub_remove_files(struct device *dev)
177 device_remove_file(dev, &dev_attr_usbip_status);
178 device_remove_file(dev, &dev_attr_usbip_sockfd);
179 device_remove_file(dev, &dev_attr_usbip_debug);
182 static void stub_shutdown_connection(struct usbip_device *ud)
184 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
187 * When removing an exported device, kernel panic sometimes occurred
188 * and then EIP was sk_wait_data of stub_rx thread. Is this because
189 * sk_wait_data returned though stub_rx thread was already finished by
192 if (ud->tcp_socket) {
193 dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n",
195 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
198 /* 1. stop threads */
200 kthread_stop_put(ud->tcp_rx);
204 kthread_stop_put(ud->tcp_tx);
209 * 2. close the socket
211 * tcp_socket is freed after threads are killed so that usbip_xmit does
212 * not touch NULL socket.
214 if (ud->tcp_socket) {
215 sockfd_put(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, &sdev->unlink_free,
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 dev_dbg(&udev->dev, "device reset");
249 ret = usb_lock_device_for_reset(udev, sdev->interface);
251 dev_err(&udev->dev, "lock for reset\n");
252 spin_lock_irq(&ud->lock);
253 ud->status = SDEV_ST_ERROR;
254 spin_unlock_irq(&ud->lock);
258 /* try to reset the device */
259 ret = usb_reset_device(udev);
260 usb_unlock_device(udev);
262 spin_lock_irq(&ud->lock);
264 dev_err(&udev->dev, "device reset\n");
265 ud->status = SDEV_ST_ERROR;
267 dev_info(&udev->dev, "device reset\n");
268 ud->status = SDEV_ST_AVAILABLE;
270 spin_unlock_irq(&ud->lock);
273 static void stub_device_unusable(struct usbip_device *ud)
275 spin_lock_irq(&ud->lock);
276 ud->status = SDEV_ST_ERROR;
277 spin_unlock_irq(&ud->lock);
281 * stub_device_alloc - allocate a new stub_device struct
282 * @interface: usb_interface of a new device
284 * Allocates and initializes a new stub_device struct.
286 static struct stub_device *stub_device_alloc(struct usb_device *udev)
288 struct stub_device *sdev;
289 int busnum = udev->bus->busnum;
290 int devnum = udev->devnum;
292 dev_dbg(&udev->dev, "allocating stub device");
294 /* yes, it's a new device */
295 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
299 sdev->udev = usb_get_dev(udev);
302 * devid is defined with devnum when this driver is first allocated.
303 * devnum may change later if a device is reset. However, devid never
304 * changes during a usbip connection.
306 sdev->devid = (busnum << 16) | devnum;
307 sdev->ud.side = USBIP_STUB;
308 sdev->ud.status = SDEV_ST_AVAILABLE;
309 spin_lock_init(&sdev->ud.lock);
310 sdev->ud.tcp_socket = NULL;
312 INIT_LIST_HEAD(&sdev->priv_init);
313 INIT_LIST_HEAD(&sdev->priv_tx);
314 INIT_LIST_HEAD(&sdev->priv_free);
315 INIT_LIST_HEAD(&sdev->unlink_free);
316 INIT_LIST_HEAD(&sdev->unlink_tx);
317 spin_lock_init(&sdev->priv_lock);
319 init_waitqueue_head(&sdev->tx_waitq);
321 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
322 sdev->ud.eh_ops.reset = stub_device_reset;
323 sdev->ud.eh_ops.unusable = stub_device_unusable;
325 usbip_start_eh(&sdev->ud);
327 dev_dbg(&udev->dev, "register new device\n");
332 static void stub_device_free(struct stub_device *sdev)
337 static int stub_probe(struct usb_device *udev)
339 struct stub_device *sdev = NULL;
340 const char *udev_busid = dev_name(&udev->dev);
342 struct bus_id_priv *busid_priv;
345 dev_dbg(&udev->dev, "Enter\n");
347 /* check we should claim or not by busid_table */
348 busid_priv = get_busid_priv(udev_busid);
349 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
350 (busid_priv->status == STUB_BUSID_OTHER)) {
352 "%s is not in match_busid table... skip!\n",
356 * Return value should be ENODEV or ENOXIO to continue trying
357 * other matched drivers by the driver core.
358 * See driver_probe_device() in driver/base/dd.c
363 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
364 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
369 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
371 "%s is attached on vhci_hcd... skip!\n",
377 /* ok, this is my device */
378 sdev = stub_device_alloc(udev);
383 "usbip-host: register new device (bus %u dev %u)\n",
384 udev->bus->busnum, udev->devnum);
386 busid_priv->shutdown_busid = 0;
388 /* set private data to usb_device */
389 dev_set_drvdata(&udev->dev, sdev);
390 busid_priv->sdev = sdev;
391 busid_priv->udev = udev;
394 * Claim this hub port.
395 * It doesn't matter what value we pass as owner
396 * (struct dev_state) as long as it is unique.
398 rc = usb_hub_claim_port(udev->parent, udev->portnum,
399 (struct usb_dev_state *) udev);
401 dev_dbg(&udev->dev, "unable to claim port\n");
405 err = stub_add_files(&udev->dev);
407 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
408 dev_set_drvdata(&udev->dev, NULL);
410 kthread_stop_put(sdev->ud.eh);
412 busid_priv->sdev = NULL;
413 stub_device_free(sdev);
416 busid_priv->status = STUB_BUSID_ALLOC;
421 static void shutdown_busid(struct bus_id_priv *busid_priv)
423 if (busid_priv->sdev && !busid_priv->shutdown_busid) {
424 busid_priv->shutdown_busid = 1;
425 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
427 /* wait for the stop of the event handler */
428 usbip_stop_eh(&busid_priv->sdev->ud);
433 * called in usb_disconnect() or usb_deregister()
434 * but only if actconfig(active configuration) exists
436 static void stub_disconnect(struct usb_device *udev)
438 struct stub_device *sdev;
439 const char *udev_busid = dev_name(&udev->dev);
440 struct bus_id_priv *busid_priv;
443 dev_dbg(&udev->dev, "Enter\n");
445 busid_priv = get_busid_priv(udev_busid);
451 sdev = dev_get_drvdata(&udev->dev);
453 /* get stub_device */
455 dev_err(&udev->dev, "could not get device");
459 dev_set_drvdata(&udev->dev, NULL);
462 * NOTE: rx/tx threads are invoked for each usb_device.
464 stub_remove_files(&udev->dev);
467 rc = usb_hub_release_port(udev->parent, udev->portnum,
468 (struct usb_dev_state *) udev);
470 dev_dbg(&udev->dev, "unable to release port\n");
474 /* If usb reset is called from event handler */
475 if (busid_priv->sdev->ud.eh == current)
478 /* shutdown the current connection */
479 shutdown_busid(busid_priv);
481 usb_put_dev(sdev->udev);
484 busid_priv->sdev = NULL;
485 stub_device_free(sdev);
487 if (busid_priv->status == STUB_BUSID_ALLOC) {
488 busid_priv->status = STUB_BUSID_ADDED;
490 busid_priv->status = STUB_BUSID_OTHER;
491 del_match_busid((char *)udev_busid);
497 /* These functions need usb_port_suspend and usb_port_resume,
498 * which reside in drivers/usb/core/usb.h. Skip for now. */
500 static int stub_suspend(struct usb_device *udev, pm_message_t message)
502 dev_dbg(&udev->dev, "stub_suspend\n");
507 static int stub_resume(struct usb_device *udev, pm_message_t message)
509 dev_dbg(&udev->dev, "stub_resume\n");
514 #endif /* CONFIG_PM */
516 struct usb_device_driver stub_driver = {
517 .name = "usbip-host",
519 .disconnect = stub_disconnect,
521 .suspend = stub_suspend,
522 .resume = stub_resume,
524 .supports_autosuspend = 0,