linux/drivers/staging/usbip/stub_dev.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
   3 *
   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.
   8 *
   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.
  13 *
  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,
  17 * USA.
  18 */
  19
  20#include "usbip_common.h"
  21#include "stub.h"
  22
  23
  24
  25static int stub_probe(struct usb_interface *interface,
  26                                const struct usb_device_id *id);
  27static void stub_disconnect(struct usb_interface *interface);
  28
  29
  30/*
  31 * Define device IDs here if you want to explicitly limit exportable devices.
  32 * In the most cases, wild card matching will be ok because driver binding can
  33 * be changed dynamically by a userland program.
  34 */
  35static struct usb_device_id stub_table[] = {
  36#if 0
  37        /* just an example */
  38        { USB_DEVICE(0x05ac, 0x0301) },   /* Mac 1 button mouse */
  39        { USB_DEVICE(0x0430, 0x0009) },   /* Plat Home Keyboard */
  40        { USB_DEVICE(0x059b, 0x0001) },   /* Iomega USB Zip 100 */
  41        { USB_DEVICE(0x04b3, 0x4427) },   /* IBM USB CD-ROM */
  42        { USB_DEVICE(0x05a9, 0xa511) },   /* LifeView USB cam */
  43        { USB_DEVICE(0x55aa, 0x0201) },   /* Imation card reader */
  44        { USB_DEVICE(0x046d, 0x0870) },   /* Qcam Express(QV-30) */
  45        { USB_DEVICE(0x04bb, 0x0101) },   /* IO-DATA HD 120GB */
  46        { USB_DEVICE(0x04bb, 0x0904) },   /* IO-DATA USB-ET/TX */
  47        { USB_DEVICE(0x04bb, 0x0201) },   /* IO-DATA USB-ET/TX */
  48        { USB_DEVICE(0x08bb, 0x2702) },   /* ONKYO USB Speaker */
  49        { USB_DEVICE(0x046d, 0x08b2) },   /* Logicool Qcam 4000 Pro */
  50#endif
  51        /* magic for wild card */
  52        { .driver_info = 1 },
  53        { 0, }                                     /* Terminating entry */
  54};
  55MODULE_DEVICE_TABLE(usb, stub_table);
  56
  57struct usb_driver stub_driver = {
  58        .name           = "usbip",
  59        .probe          = stub_probe,
  60        .disconnect     = stub_disconnect,
  61        .id_table       = stub_table,
  62};
  63
  64
  65/*-------------------------------------------------------------------------*/
  66
  67/* Define sysfs entries for a usbip-bound device */
  68
  69
  70/*
  71 * usbip_status shows status of usbip as long as this driver is bound to the
  72 * target device.
  73 */
  74static ssize_t show_status(struct device *dev, struct device_attribute *attr,
  75                           char *buf)
  76{
  77        struct stub_device *sdev = dev_get_drvdata(dev);
  78        int status;
  79
  80        if (!sdev) {
  81                dev_err(dev, "sdev is null\n");
  82                return -ENODEV;
  83        }
  84
  85        spin_lock(&sdev->ud.lock);
  86        status = sdev->ud.status;
  87        spin_unlock(&sdev->ud.lock);
  88
  89        return snprintf(buf, PAGE_SIZE, "%d\n", status);
  90}
  91static DEVICE_ATTR(usbip_status, S_IRUGO, show_status, NULL);
  92
  93/*
  94 * usbip_sockfd gets a socket descriptor of an established TCP connection that
  95 * is used to transfer usbip requests by kernel threads. -1 is a magic number
  96 * by which usbip connection is finished.
  97 */
  98static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
  99                            const char *buf, size_t count)
 100{
 101        struct stub_device *sdev = dev_get_drvdata(dev);
 102        int sockfd = 0;
 103        struct socket *socket;
 104
 105        if (!sdev) {
 106                dev_err(dev, "sdev is null\n");
 107                return -ENODEV;
 108        }
 109
 110        sscanf(buf, "%d", &sockfd);
 111
 112        if (sockfd != -1) {
 113                dev_info(dev, "stub up\n");
 114
 115                spin_lock(&sdev->ud.lock);
 116
 117                if (sdev->ud.status != SDEV_ST_AVAILABLE) {
 118                        dev_err(dev, "not ready\n");
 119                        spin_unlock(&sdev->ud.lock);
 120                        return -EINVAL;
 121                }
 122
 123                socket = sockfd_to_socket(sockfd);
 124                if (!socket) {
 125                        spin_unlock(&sdev->ud.lock);
 126                        return -EINVAL;
 127                }
 128
 129#if 0
 130                setnodelay(socket);
 131                setkeepalive(socket);
 132                setreuse(socket);
 133#endif
 134
 135                sdev->ud.tcp_socket = socket;
 136
 137                spin_unlock(&sdev->ud.lock);
 138
 139                usbip_start_threads(&sdev->ud);
 140
 141                spin_lock(&sdev->ud.lock);
 142                sdev->ud.status = SDEV_ST_USED;
 143                spin_unlock(&sdev->ud.lock);
 144
 145        } else {
 146                dev_info(dev, "stub down\n");
 147
 148                spin_lock(&sdev->ud.lock);
 149                if (sdev->ud.status != SDEV_ST_USED) {
 150                        spin_unlock(&sdev->ud.lock);
 151                        return -EINVAL;
 152                }
 153                spin_unlock(&sdev->ud.lock);
 154
 155                usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
 156        }
 157
 158        return count;
 159}
 160static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
 161
 162static int stub_add_files(struct device *dev)
 163{
 164        int err = 0;
 165
 166        err = device_create_file(dev, &dev_attr_usbip_status);
 167        if (err)
 168                goto err_status;
 169
 170        err = device_create_file(dev, &dev_attr_usbip_sockfd);
 171        if (err)
 172                goto err_sockfd;
 173
 174        err = device_create_file(dev, &dev_attr_usbip_debug);
 175        if (err)
 176                goto err_debug;
 177
 178        return 0;
 179
 180err_debug:
 181        device_remove_file(dev, &dev_attr_usbip_sockfd);
 182
 183err_sockfd:
 184        device_remove_file(dev, &dev_attr_usbip_status);
 185
 186err_status:
 187        return err;
 188}
 189
 190static void stub_remove_files(struct device *dev)
 191{
 192        device_remove_file(dev, &dev_attr_usbip_status);
 193        device_remove_file(dev, &dev_attr_usbip_sockfd);
 194        device_remove_file(dev, &dev_attr_usbip_debug);
 195}
 196
 197
 198
 199/*-------------------------------------------------------------------------*/
 200
 201/* Event handler functions called by an event handler thread */
 202
 203static void stub_shutdown_connection(struct usbip_device *ud)
 204{
 205        struct stub_device *sdev = container_of(ud, struct stub_device, ud);
 206
 207        /*
 208         * When removing an exported device, kernel panic sometimes occurred
 209         * and then EIP was sk_wait_data of stub_rx thread. Is this because
 210         * sk_wait_data returned though stub_rx thread was already finished by
 211         * step 1?
 212         */
 213        if (ud->tcp_socket) {
 214                usbip_udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
 215                kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
 216        }
 217
 218        /* 1. stop threads */
 219        usbip_stop_threads(ud);
 220
 221        /* 2. close the socket */
 222        /*
 223         * tcp_socket is freed after threads are killed.
 224         * So usbip_xmit do not touch NULL socket.
 225         */
 226        if (ud->tcp_socket) {
 227                sock_release(ud->tcp_socket);
 228                ud->tcp_socket = NULL;
 229        }
 230
 231        /* 3. free used data */
 232        stub_device_cleanup_urbs(sdev);
 233
 234        /* 4. free stub_unlink */
 235        {
 236                unsigned long flags;
 237                struct stub_unlink *unlink, *tmp;
 238
 239                spin_lock_irqsave(&sdev->priv_lock, flags);
 240
 241                list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
 242                        list_del(&unlink->list);
 243                        kfree(unlink);
 244                }
 245
 246                list_for_each_entry_safe(unlink, tmp,
 247                                                 &sdev->unlink_free, list) {
 248                        list_del(&unlink->list);
 249                        kfree(unlink);
 250                }
 251
 252                spin_unlock_irqrestore(&sdev->priv_lock, flags);
 253        }
 254}
 255
 256static void stub_device_reset(struct usbip_device *ud)
 257{
 258        struct stub_device *sdev = container_of(ud, struct stub_device, ud);
 259        struct usb_device *udev = interface_to_usbdev(sdev->interface);
 260        int ret;
 261
 262        usbip_udbg("device reset");
 263        ret = usb_lock_device_for_reset(udev, sdev->interface);
 264        if (ret < 0) {
 265                dev_err(&udev->dev, "lock for reset\n");
 266
 267                spin_lock(&ud->lock);
 268                ud->status = SDEV_ST_ERROR;
 269                spin_unlock(&ud->lock);
 270
 271                return;
 272        }
 273
 274        /* try to reset the device */
 275        ret = usb_reset_device(udev);
 276
 277        usb_unlock_device(udev);
 278
 279        spin_lock(&ud->lock);
 280        if (ret) {
 281                dev_err(&udev->dev, "device reset\n");
 282                ud->status = SDEV_ST_ERROR;
 283
 284        } else {
 285                dev_info(&udev->dev, "device reset\n");
 286                ud->status = SDEV_ST_AVAILABLE;
 287
 288        }
 289        spin_unlock(&ud->lock);
 290
 291        return;
 292}
 293
 294static void stub_device_unusable(struct usbip_device *ud)
 295{
 296        spin_lock(&ud->lock);
 297        ud->status = SDEV_ST_ERROR;
 298        spin_unlock(&ud->lock);
 299}
 300
 301
 302/*-------------------------------------------------------------------------*/
 303
 304/**
 305 * stub_device_alloc - allocate a new stub_device struct
 306 * @interface: usb_interface of a new device
 307 *
 308 * Allocates and initializes a new stub_device struct.
 309 */
 310static struct stub_device *stub_device_alloc(struct usb_interface *interface)
 311{
 312        struct stub_device *sdev;
 313        int busnum = interface_to_busnum(interface);
 314        int devnum = interface_to_devnum(interface);
 315
 316        dev_dbg(&interface->dev, "allocating stub device");
 317
 318        /* yes, it's a new device */
 319        sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
 320        if (!sdev) {
 321                dev_err(&interface->dev, "no memory for stub_device\n");
 322                return NULL;
 323        }
 324
 325        sdev->interface = interface;
 326
 327        /*
 328         * devid is defined with devnum when this driver is first allocated.
 329         * devnum may change later if a device is reset. However, devid never
 330         * changes during a usbip connection.
 331         */
 332        sdev->devid     = (busnum << 16) | devnum;
 333
 334        usbip_task_init(&sdev->ud.tcp_rx, "stub_rx", stub_rx_loop);
 335        usbip_task_init(&sdev->ud.tcp_tx, "stub_tx", stub_tx_loop);
 336
 337        sdev->ud.side = USBIP_STUB;
 338        sdev->ud.status = SDEV_ST_AVAILABLE;
 339        /* sdev->ud.lock = SPIN_LOCK_UNLOCKED; */
 340        spin_lock_init(&sdev->ud.lock);
 341        sdev->ud.tcp_socket = NULL;
 342
 343        INIT_LIST_HEAD(&sdev->priv_init);
 344        INIT_LIST_HEAD(&sdev->priv_tx);
 345        INIT_LIST_HEAD(&sdev->priv_free);
 346        INIT_LIST_HEAD(&sdev->unlink_free);
 347        INIT_LIST_HEAD(&sdev->unlink_tx);
 348        /* sdev->priv_lock = SPIN_LOCK_UNLOCKED; */
 349        spin_lock_init(&sdev->priv_lock);
 350
 351        init_waitqueue_head(&sdev->tx_waitq);
 352
 353        sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
 354        sdev->ud.eh_ops.reset    = stub_device_reset;
 355        sdev->ud.eh_ops.unusable = stub_device_unusable;
 356
 357        usbip_start_eh(&sdev->ud);
 358
 359        usbip_udbg("register new interface\n");
 360        return sdev;
 361}
 362
 363static int stub_device_free(struct stub_device *sdev)
 364{
 365        if (!sdev)
 366                return -EINVAL;
 367
 368        kfree(sdev);
 369        usbip_udbg("kfree udev ok\n");
 370
 371        return 0;
 372}
 373
 374
 375/*-------------------------------------------------------------------------*/
 376
 377/*
 378 * If a usb device has multiple active interfaces, this driver is bound to all
 379 * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
 380 * active interface). Currently, a userland program must ensure that it
 381 * looks at the usbip's sysfs entries of only the first active interface.
 382 *
 383 * TODO: use "struct usb_device_driver" to bind a usb device.
 384 * However, it seems it is not fully supported in mainline kernel yet
 385 * (2.6.19.2).
 386 */
 387static int stub_probe(struct usb_interface *interface,
 388                      const struct usb_device_id *id)
 389{
 390        struct usb_device *udev = interface_to_usbdev(interface);
 391        struct stub_device *sdev = NULL;
 392        const char *udev_busid = dev_name(interface->dev.parent);
 393        int err = 0;
 394
 395        dev_dbg(&interface->dev, "Enter\n");
 396
 397        /* check we should claim or not by busid_table */
 398        if (match_busid(udev_busid)) {
 399                dev_info(&interface->dev,
 400                         "this device %s is not in match_busid table. skip!\n",
 401                         udev_busid);
 402
 403                /*
 404                 * Return value should be ENODEV or ENOXIO to continue trying
 405                 * other matched drivers by the driver core.
 406                 * See driver_probe_device() in driver/base/dd.c
 407                 */
 408                return -ENODEV;
 409        }
 410
 411        if (udev->descriptor.bDeviceClass ==  USB_CLASS_HUB) {
 412                usbip_udbg("this device %s is a usb hub device. skip!\n",
 413                                                                udev_busid);
 414                return -ENODEV;
 415        }
 416
 417        if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
 418                usbip_udbg("this device %s is attached on vhci_hcd. skip!\n",
 419                                                                udev_busid);
 420                return -ENODEV;
 421        }
 422
 423        /* ok. this is my device. */
 424        sdev = stub_device_alloc(interface);
 425        if (!sdev)
 426                return -ENOMEM;
 427
 428        dev_info(&interface->dev, "USB/IP Stub: register a new interface "
 429                 "(bus %u dev %u ifn %u)\n", udev->bus->busnum, udev->devnum,
 430                 interface->cur_altsetting->desc.bInterfaceNumber);
 431
 432        /* set private data to usb_interface */
 433        usb_set_intfdata(interface, sdev);
 434
 435        err = stub_add_files(&interface->dev);
 436        if (err) {
 437                dev_err(&interface->dev, "create sysfs files for %s\n",
 438                        udev_busid);
 439                return err;
 440        }
 441
 442        return 0;
 443}
 444
 445
 446/*
 447 * called in usb_disconnect() or usb_deregister()
 448 * but only if actconfig(active configuration) exists
 449 */
 450static void stub_disconnect(struct usb_interface *interface)
 451{
 452        struct stub_device *sdev = usb_get_intfdata(interface);
 453
 454        usbip_udbg("Enter\n");
 455
 456        /* get stub_device */
 457        if (!sdev) {
 458                err(" could not get device from inteface data");
 459                /* BUG(); */
 460                return;
 461        }
 462
 463        usb_set_intfdata(interface, NULL);
 464
 465
 466        /*
 467         * NOTE:
 468         * rx/tx threads are invoked for each usb_device.
 469         */
 470        stub_remove_files(&interface->dev);
 471
 472        /* 1. shutdown the current connection */
 473        usbip_event_add(&sdev->ud, SDEV_EVENT_REMOVED);
 474
 475        /* 2. wait for the stop of the event handler */
 476        usbip_stop_eh(&sdev->ud);
 477
 478        /* 3. free sdev */
 479        stub_device_free(sdev);
 480
 481
 482        usbip_udbg("bye\n");
 483}
 484