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 * usbip_status shows the status of usbip-host as long as this driver is bound
30 * to the target device.
32 static ssize_t usbip_status_show(struct device *dev,
33 struct device_attribute *attr, char *buf)
35 struct stub_device *sdev = dev_get_drvdata(dev);
39 dev_err(dev, "sdev is null\n");
43 spin_lock_irq(&sdev->ud.lock);
44 status = sdev->ud.status;
45 spin_unlock_irq(&sdev->ud.lock);
47 return snprintf(buf, PAGE_SIZE, "%d\n", status);
49 static DEVICE_ATTR_RO(usbip_status);
52 * usbip_sockfd gets a socket descriptor of an established TCP connection that
53 * is used to transfer usbip requests by kernel threads. -1 is a magic number
54 * by which usbip connection is finished.
56 static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
57 const char *buf, size_t count)
59 struct stub_device *sdev = dev_get_drvdata(dev);
61 struct socket *socket;
63 struct task_struct *tcp_rx = NULL;
64 struct task_struct *tcp_tx = NULL;
67 dev_err(dev, "sdev is null\n");
71 rv = sscanf(buf, "%d", &sockfd);
78 dev_info(dev, "stub up\n");
80 mutex_lock(&sdev->ud.sysfs_lock);
81 spin_lock_irq(&sdev->ud.lock);
83 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
84 dev_err(dev, "not ready\n");
88 socket = sockfd_lookup(sockfd, &err);
90 dev_err(dev, "failed to lookup sock");
94 if (socket->type != SOCK_STREAM) {
95 dev_err(dev, "Expecting SOCK_STREAM - found %d",
100 /* unlock and create threads and get tasks */
101 spin_unlock_irq(&sdev->ud.lock);
102 tcp_rx = kthread_create(stub_rx_loop, &sdev->ud, "stub_rx");
103 if (IS_ERR(tcp_rx)) {
107 tcp_tx = kthread_create(stub_tx_loop, &sdev->ud, "stub_tx");
108 if (IS_ERR(tcp_tx)) {
109 kthread_stop(tcp_rx);
114 /* get task structs now */
115 get_task_struct(tcp_rx);
116 get_task_struct(tcp_tx);
118 /* lock and update sdev->ud state */
119 spin_lock_irq(&sdev->ud.lock);
120 sdev->ud.tcp_socket = socket;
121 sdev->ud.sockfd = sockfd;
122 sdev->ud.tcp_rx = tcp_rx;
123 sdev->ud.tcp_tx = tcp_tx;
124 sdev->ud.status = SDEV_ST_USED;
125 spin_unlock_irq(&sdev->ud.lock);
127 wake_up_process(sdev->ud.tcp_rx);
128 wake_up_process(sdev->ud.tcp_tx);
130 mutex_unlock(&sdev->ud.sysfs_lock);
133 dev_info(dev, "stub down\n");
135 spin_lock_irq(&sdev->ud.lock);
136 if (sdev->ud.status != SDEV_ST_USED)
139 spin_unlock_irq(&sdev->ud.lock);
141 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
142 mutex_unlock(&sdev->ud.sysfs_lock);
150 spin_unlock_irq(&sdev->ud.lock);
152 mutex_unlock(&sdev->ud.sysfs_lock);
155 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
157 static int stub_add_files(struct device *dev)
161 err = device_create_file(dev, &dev_attr_usbip_status);
165 err = device_create_file(dev, &dev_attr_usbip_sockfd);
169 err = device_create_file(dev, &dev_attr_usbip_debug);
176 device_remove_file(dev, &dev_attr_usbip_sockfd);
178 device_remove_file(dev, &dev_attr_usbip_status);
183 static void stub_remove_files(struct device *dev)
185 device_remove_file(dev, &dev_attr_usbip_status);
186 device_remove_file(dev, &dev_attr_usbip_sockfd);
187 device_remove_file(dev, &dev_attr_usbip_debug);
190 static void stub_shutdown_connection(struct usbip_device *ud)
192 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
195 * When removing an exported device, kernel panic sometimes occurred
196 * and then EIP was sk_wait_data of stub_rx thread. Is this because
197 * sk_wait_data returned though stub_rx thread was already finished by
200 if (ud->tcp_socket) {
201 dev_dbg(&sdev->udev->dev, "shutdown sockfd %d\n", ud->sockfd);
202 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
205 /* 1. stop threads */
207 kthread_stop_put(ud->tcp_rx);
211 kthread_stop_put(ud->tcp_tx);
216 * 2. close the socket
218 * tcp_socket is freed after threads are killed so that usbip_xmit does
219 * not touch NULL socket.
221 if (ud->tcp_socket) {
222 sockfd_put(ud->tcp_socket);
223 ud->tcp_socket = NULL;
227 /* 3. free used data */
228 stub_device_cleanup_urbs(sdev);
230 /* 4. free stub_unlink */
233 struct stub_unlink *unlink, *tmp;
235 spin_lock_irqsave(&sdev->priv_lock, flags);
236 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
237 list_del(&unlink->list);
240 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
242 list_del(&unlink->list);
245 spin_unlock_irqrestore(&sdev->priv_lock, flags);
249 static void stub_device_reset(struct usbip_device *ud)
251 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
252 struct usb_device *udev = sdev->udev;
255 dev_dbg(&udev->dev, "device reset");
257 ret = usb_lock_device_for_reset(udev, NULL);
259 dev_err(&udev->dev, "lock for reset\n");
260 spin_lock_irq(&ud->lock);
261 ud->status = SDEV_ST_ERROR;
262 spin_unlock_irq(&ud->lock);
266 /* try to reset the device */
267 ret = usb_reset_device(udev);
268 usb_unlock_device(udev);
270 spin_lock_irq(&ud->lock);
272 dev_err(&udev->dev, "device reset\n");
273 ud->status = SDEV_ST_ERROR;
275 dev_info(&udev->dev, "device reset\n");
276 ud->status = SDEV_ST_AVAILABLE;
278 spin_unlock_irq(&ud->lock);
281 static void stub_device_unusable(struct usbip_device *ud)
283 spin_lock_irq(&ud->lock);
284 ud->status = SDEV_ST_ERROR;
285 spin_unlock_irq(&ud->lock);
289 * stub_device_alloc - allocate a new stub_device struct
290 * @udev: usb_device of a new device
292 * Allocates and initializes a new stub_device struct.
294 static struct stub_device *stub_device_alloc(struct usb_device *udev)
296 struct stub_device *sdev;
297 int busnum = udev->bus->busnum;
298 int devnum = udev->devnum;
300 dev_dbg(&udev->dev, "allocating stub device");
302 /* yes, it's a new device */
303 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
307 sdev->udev = usb_get_dev(udev);
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;
315 sdev->ud.side = USBIP_STUB;
316 sdev->ud.status = SDEV_ST_AVAILABLE;
317 spin_lock_init(&sdev->ud.lock);
318 mutex_init(&sdev->ud.sysfs_lock);
319 sdev->ud.tcp_socket = NULL;
320 sdev->ud.sockfd = -1;
322 INIT_LIST_HEAD(&sdev->priv_init);
323 INIT_LIST_HEAD(&sdev->priv_tx);
324 INIT_LIST_HEAD(&sdev->priv_free);
325 INIT_LIST_HEAD(&sdev->unlink_free);
326 INIT_LIST_HEAD(&sdev->unlink_tx);
327 spin_lock_init(&sdev->priv_lock);
329 init_waitqueue_head(&sdev->tx_waitq);
331 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
332 sdev->ud.eh_ops.reset = stub_device_reset;
333 sdev->ud.eh_ops.unusable = stub_device_unusable;
335 usbip_start_eh(&sdev->ud);
337 dev_dbg(&udev->dev, "register new device\n");
342 static void stub_device_free(struct stub_device *sdev)
347 static int stub_probe(struct usb_device *udev)
349 struct stub_device *sdev = NULL;
350 const char *udev_busid = dev_name(&udev->dev);
351 struct bus_id_priv *busid_priv;
355 dev_dbg(&udev->dev, "Enter probe\n");
357 /* Not sure if this is our device. Allocate here to avoid
358 * calling alloc while holding busid_table lock.
360 sdev = stub_device_alloc(udev);
364 /* check we should claim or not by busid_table */
365 busid_priv = get_busid_priv(udev_busid);
366 if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
367 (busid_priv->status == STUB_BUSID_OTHER)) {
369 "%s is not in match_busid table... skip!\n",
373 * Return value should be ENODEV or ENOXIO to continue trying
374 * other matched drivers by the driver core.
375 * See driver_probe_device() in driver/base/dd.c
381 goto call_put_busid_priv;
384 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
385 dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
388 goto call_put_busid_priv;
391 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
393 "%s is attached on vhci_hcd... skip!\n",
397 goto call_put_busid_priv;
402 "usbip-host: register new device (bus %u dev %u)\n",
403 udev->bus->busnum, udev->devnum);
405 busid_priv->shutdown_busid = 0;
407 /* set private data to usb_device */
408 dev_set_drvdata(&udev->dev, sdev);
410 busid_priv->sdev = sdev;
411 busid_priv->udev = udev;
413 save_status = busid_priv->status;
414 busid_priv->status = STUB_BUSID_ALLOC;
416 /* release the busid_lock */
417 put_busid_priv(busid_priv);
420 * Claim this hub port.
421 * It doesn't matter what value we pass as owner
422 * (struct dev_state) as long as it is unique.
424 rc = usb_hub_claim_port(udev->parent, udev->portnum,
425 (struct usb_dev_state *) udev);
427 dev_dbg(&udev->dev, "unable to claim port\n");
431 rc = stub_add_files(&udev->dev);
433 dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
440 usb_hub_release_port(udev->parent, udev->portnum,
441 (struct usb_dev_state *) udev);
443 dev_set_drvdata(&udev->dev, NULL);
446 /* we already have busid_priv, just lock busid_lock */
447 spin_lock(&busid_priv->busid_lock);
448 busid_priv->sdev = NULL;
449 busid_priv->status = save_status;
450 spin_unlock(&busid_priv->busid_lock);
451 /* lock is released - go to free */
455 /* release the busid_lock */
456 put_busid_priv(busid_priv);
459 stub_device_free(sdev);
464 static void shutdown_busid(struct bus_id_priv *busid_priv)
466 usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
468 /* wait for the stop of the event handler */
469 usbip_stop_eh(&busid_priv->sdev->ud);
473 * called in usb_disconnect() or usb_deregister()
474 * but only if actconfig(active configuration) exists
476 static void stub_disconnect(struct usb_device *udev)
478 struct stub_device *sdev;
479 const char *udev_busid = dev_name(&udev->dev);
480 struct bus_id_priv *busid_priv;
483 dev_dbg(&udev->dev, "Enter disconnect\n");
485 busid_priv = get_busid_priv(udev_busid);
491 sdev = dev_get_drvdata(&udev->dev);
493 /* get stub_device */
495 dev_err(&udev->dev, "could not get device");
496 /* release busid_lock */
497 put_busid_priv(busid_priv);
501 dev_set_drvdata(&udev->dev, NULL);
503 /* release busid_lock before call to remove device files */
504 put_busid_priv(busid_priv);
507 * NOTE: rx/tx threads are invoked for each usb_device.
509 stub_remove_files(&udev->dev);
512 rc = usb_hub_release_port(udev->parent, udev->portnum,
513 (struct usb_dev_state *) udev);
515 dev_dbg(&udev->dev, "unable to release port\n");
519 /* If usb reset is called from event handler */
520 if (usbip_in_eh(current))
523 /* we already have busid_priv, just lock busid_lock */
524 spin_lock(&busid_priv->busid_lock);
525 if (!busid_priv->shutdown_busid)
526 busid_priv->shutdown_busid = 1;
527 /* release busid_lock */
528 spin_unlock(&busid_priv->busid_lock);
530 /* shutdown the current connection */
531 shutdown_busid(busid_priv);
533 usb_put_dev(sdev->udev);
535 /* we already have busid_priv, just lock busid_lock */
536 spin_lock(&busid_priv->busid_lock);
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;
543 /* release busid_lock */
544 spin_unlock(&busid_priv->busid_lock);
550 /* These functions need usb_port_suspend and usb_port_resume,
551 * which reside in drivers/usb/core/usb.h. Skip for now. */
553 static int stub_suspend(struct usb_device *udev, pm_message_t message)
555 dev_dbg(&udev->dev, "stub_suspend\n");
560 static int stub_resume(struct usb_device *udev, pm_message_t message)
562 dev_dbg(&udev->dev, "stub_resume\n");
567 #endif /* CONFIG_PM */
569 struct usb_device_driver stub_driver = {
570 .name = "usbip-host",
572 .disconnect = stub_disconnect,
574 .suspend = stub_suspend,
575 .resume = stub_resume,
577 .supports_autosuspend = 0,