Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Copyright (C) 2003-2008 Takahiro Hirofuchi
0004  */
0005 
0006 #include <linux/device.h>
0007 #include <linux/file.h>
0008 #include <linux/kthread.h>
0009 #include <linux/module.h>
0010 
0011 #include "usbip_common.h"
0012 #include "stub.h"
0013 
0014 /*
0015  * usbip_status shows the status of usbip-host as long as this driver is bound
0016  * to the target device.
0017  */
0018 static ssize_t usbip_status_show(struct device *dev,
0019                  struct device_attribute *attr, char *buf)
0020 {
0021     struct stub_device *sdev = dev_get_drvdata(dev);
0022     int status;
0023 
0024     if (!sdev) {
0025         dev_err(dev, "sdev is null\n");
0026         return -ENODEV;
0027     }
0028 
0029     spin_lock_irq(&sdev->ud.lock);
0030     status = sdev->ud.status;
0031     spin_unlock_irq(&sdev->ud.lock);
0032 
0033     return snprintf(buf, PAGE_SIZE, "%d\n", status);
0034 }
0035 static DEVICE_ATTR_RO(usbip_status);
0036 
0037 /*
0038  * usbip_sockfd gets a socket descriptor of an established TCP connection that
0039  * is used to transfer usbip requests by kernel threads. -1 is a magic number
0040  * by which usbip connection is finished.
0041  */
0042 static ssize_t usbip_sockfd_store(struct device *dev, struct device_attribute *attr,
0043                 const char *buf, size_t count)
0044 {
0045     struct stub_device *sdev = dev_get_drvdata(dev);
0046     int sockfd = 0;
0047     struct socket *socket;
0048     int rv;
0049     struct task_struct *tcp_rx = NULL;
0050     struct task_struct *tcp_tx = NULL;
0051 
0052     if (!sdev) {
0053         dev_err(dev, "sdev is null\n");
0054         return -ENODEV;
0055     }
0056 
0057     rv = sscanf(buf, "%d", &sockfd);
0058     if (rv != 1)
0059         return -EINVAL;
0060 
0061     if (sockfd != -1) {
0062         int err;
0063 
0064         dev_info(dev, "stub up\n");
0065 
0066         mutex_lock(&sdev->ud.sysfs_lock);
0067         spin_lock_irq(&sdev->ud.lock);
0068 
0069         if (sdev->ud.status != SDEV_ST_AVAILABLE) {
0070             dev_err(dev, "not ready\n");
0071             goto err;
0072         }
0073 
0074         socket = sockfd_lookup(sockfd, &err);
0075         if (!socket) {
0076             dev_err(dev, "failed to lookup sock");
0077             goto err;
0078         }
0079 
0080         if (socket->type != SOCK_STREAM) {
0081             dev_err(dev, "Expecting SOCK_STREAM - found %d",
0082                 socket->type);
0083             goto sock_err;
0084         }
0085 
0086         /* unlock and create threads and get tasks */
0087         spin_unlock_irq(&sdev->ud.lock);
0088         tcp_rx = kthread_create(stub_rx_loop, &sdev->ud, "stub_rx");
0089         if (IS_ERR(tcp_rx)) {
0090             sockfd_put(socket);
0091             goto unlock_mutex;
0092         }
0093         tcp_tx = kthread_create(stub_tx_loop, &sdev->ud, "stub_tx");
0094         if (IS_ERR(tcp_tx)) {
0095             kthread_stop(tcp_rx);
0096             sockfd_put(socket);
0097             goto unlock_mutex;
0098         }
0099 
0100         /* get task structs now */
0101         get_task_struct(tcp_rx);
0102         get_task_struct(tcp_tx);
0103 
0104         /* lock and update sdev->ud state */
0105         spin_lock_irq(&sdev->ud.lock);
0106         sdev->ud.tcp_socket = socket;
0107         sdev->ud.sockfd = sockfd;
0108         sdev->ud.tcp_rx = tcp_rx;
0109         sdev->ud.tcp_tx = tcp_tx;
0110         sdev->ud.status = SDEV_ST_USED;
0111         spin_unlock_irq(&sdev->ud.lock);
0112 
0113         wake_up_process(sdev->ud.tcp_rx);
0114         wake_up_process(sdev->ud.tcp_tx);
0115 
0116         mutex_unlock(&sdev->ud.sysfs_lock);
0117 
0118     } else {
0119         dev_info(dev, "stub down\n");
0120 
0121         spin_lock_irq(&sdev->ud.lock);
0122         if (sdev->ud.status != SDEV_ST_USED)
0123             goto err;
0124 
0125         spin_unlock_irq(&sdev->ud.lock);
0126 
0127         usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
0128         mutex_unlock(&sdev->ud.sysfs_lock);
0129     }
0130 
0131     return count;
0132 
0133 sock_err:
0134     sockfd_put(socket);
0135 err:
0136     spin_unlock_irq(&sdev->ud.lock);
0137 unlock_mutex:
0138     mutex_unlock(&sdev->ud.sysfs_lock);
0139     return -EINVAL;
0140 }
0141 static DEVICE_ATTR_WO(usbip_sockfd);
0142 
0143 static struct attribute *usbip_attrs[] = {
0144     &dev_attr_usbip_status.attr,
0145     &dev_attr_usbip_sockfd.attr,
0146     &dev_attr_usbip_debug.attr,
0147     NULL,
0148 };
0149 ATTRIBUTE_GROUPS(usbip);
0150 
0151 static void stub_shutdown_connection(struct usbip_device *ud)
0152 {
0153     struct stub_device *sdev = container_of(ud, struct stub_device, ud);
0154 
0155     /*
0156      * When removing an exported device, kernel panic sometimes occurred
0157      * and then EIP was sk_wait_data of stub_rx thread. Is this because
0158      * sk_wait_data returned though stub_rx thread was already finished by
0159      * step 1?
0160      */
0161     if (ud->tcp_socket) {
0162         dev_dbg(&sdev->udev->dev, "shutdown sockfd %d\n", ud->sockfd);
0163         kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
0164     }
0165 
0166     /* 1. stop threads */
0167     if (ud->tcp_rx) {
0168         kthread_stop_put(ud->tcp_rx);
0169         ud->tcp_rx = NULL;
0170     }
0171     if (ud->tcp_tx) {
0172         kthread_stop_put(ud->tcp_tx);
0173         ud->tcp_tx = NULL;
0174     }
0175 
0176     /*
0177      * 2. close the socket
0178      *
0179      * tcp_socket is freed after threads are killed so that usbip_xmit does
0180      * not touch NULL socket.
0181      */
0182     if (ud->tcp_socket) {
0183         sockfd_put(ud->tcp_socket);
0184         ud->tcp_socket = NULL;
0185         ud->sockfd = -1;
0186     }
0187 
0188     /* 3. free used data */
0189     stub_device_cleanup_urbs(sdev);
0190 
0191     /* 4. free stub_unlink */
0192     {
0193         unsigned long flags;
0194         struct stub_unlink *unlink, *tmp;
0195 
0196         spin_lock_irqsave(&sdev->priv_lock, flags);
0197         list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
0198             list_del(&unlink->list);
0199             kfree(unlink);
0200         }
0201         list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
0202                      list) {
0203             list_del(&unlink->list);
0204             kfree(unlink);
0205         }
0206         spin_unlock_irqrestore(&sdev->priv_lock, flags);
0207     }
0208 }
0209 
0210 static void stub_device_reset(struct usbip_device *ud)
0211 {
0212     struct stub_device *sdev = container_of(ud, struct stub_device, ud);
0213     struct usb_device *udev = sdev->udev;
0214     int ret;
0215 
0216     dev_dbg(&udev->dev, "device reset");
0217 
0218     ret = usb_lock_device_for_reset(udev, NULL);
0219     if (ret < 0) {
0220         dev_err(&udev->dev, "lock for reset\n");
0221         spin_lock_irq(&ud->lock);
0222         ud->status = SDEV_ST_ERROR;
0223         spin_unlock_irq(&ud->lock);
0224         return;
0225     }
0226 
0227     /* try to reset the device */
0228     ret = usb_reset_device(udev);
0229     usb_unlock_device(udev);
0230 
0231     spin_lock_irq(&ud->lock);
0232     if (ret) {
0233         dev_err(&udev->dev, "device reset\n");
0234         ud->status = SDEV_ST_ERROR;
0235     } else {
0236         dev_info(&udev->dev, "device reset\n");
0237         ud->status = SDEV_ST_AVAILABLE;
0238     }
0239     spin_unlock_irq(&ud->lock);
0240 }
0241 
0242 static void stub_device_unusable(struct usbip_device *ud)
0243 {
0244     spin_lock_irq(&ud->lock);
0245     ud->status = SDEV_ST_ERROR;
0246     spin_unlock_irq(&ud->lock);
0247 }
0248 
0249 /**
0250  * stub_device_alloc - allocate a new stub_device struct
0251  * @udev: usb_device of a new device
0252  *
0253  * Allocates and initializes a new stub_device struct.
0254  */
0255 static struct stub_device *stub_device_alloc(struct usb_device *udev)
0256 {
0257     struct stub_device *sdev;
0258     int busnum = udev->bus->busnum;
0259     int devnum = udev->devnum;
0260 
0261     dev_dbg(&udev->dev, "allocating stub device");
0262 
0263     /* yes, it's a new device */
0264     sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
0265     if (!sdev)
0266         return NULL;
0267 
0268     sdev->udev = usb_get_dev(udev);
0269 
0270     /*
0271      * devid is defined with devnum when this driver is first allocated.
0272      * devnum may change later if a device is reset. However, devid never
0273      * changes during a usbip connection.
0274      */
0275     sdev->devid     = (busnum << 16) | devnum;
0276     sdev->ud.side       = USBIP_STUB;
0277     sdev->ud.status     = SDEV_ST_AVAILABLE;
0278     spin_lock_init(&sdev->ud.lock);
0279     mutex_init(&sdev->ud.sysfs_lock);
0280     sdev->ud.tcp_socket = NULL;
0281     sdev->ud.sockfd     = -1;
0282 
0283     INIT_LIST_HEAD(&sdev->priv_init);
0284     INIT_LIST_HEAD(&sdev->priv_tx);
0285     INIT_LIST_HEAD(&sdev->priv_free);
0286     INIT_LIST_HEAD(&sdev->unlink_free);
0287     INIT_LIST_HEAD(&sdev->unlink_tx);
0288     spin_lock_init(&sdev->priv_lock);
0289 
0290     init_waitqueue_head(&sdev->tx_waitq);
0291 
0292     sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
0293     sdev->ud.eh_ops.reset    = stub_device_reset;
0294     sdev->ud.eh_ops.unusable = stub_device_unusable;
0295 
0296     usbip_start_eh(&sdev->ud);
0297 
0298     dev_dbg(&udev->dev, "register new device\n");
0299 
0300     return sdev;
0301 }
0302 
0303 static void stub_device_free(struct stub_device *sdev)
0304 {
0305     kfree(sdev);
0306 }
0307 
0308 static int stub_probe(struct usb_device *udev)
0309 {
0310     struct stub_device *sdev = NULL;
0311     const char *udev_busid = dev_name(&udev->dev);
0312     struct bus_id_priv *busid_priv;
0313     int rc = 0;
0314     char save_status;
0315 
0316     dev_dbg(&udev->dev, "Enter probe\n");
0317 
0318     /* Not sure if this is our device. Allocate here to avoid
0319      * calling alloc while holding busid_table lock.
0320      */
0321     sdev = stub_device_alloc(udev);
0322     if (!sdev)
0323         return -ENOMEM;
0324 
0325     /* check we should claim or not by busid_table */
0326     busid_priv = get_busid_priv(udev_busid);
0327     if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
0328         (busid_priv->status == STUB_BUSID_OTHER)) {
0329         dev_info(&udev->dev,
0330             "%s is not in match_busid table... skip!\n",
0331             udev_busid);
0332 
0333         /*
0334          * Return value should be ENODEV or ENOXIO to continue trying
0335          * other matched drivers by the driver core.
0336          * See driver_probe_device() in driver/base/dd.c
0337          */
0338         rc = -ENODEV;
0339         if (!busid_priv)
0340             goto sdev_free;
0341 
0342         goto call_put_busid_priv;
0343     }
0344 
0345     if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
0346         dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
0347              udev_busid);
0348         rc = -ENODEV;
0349         goto call_put_busid_priv;
0350     }
0351 
0352     if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
0353         dev_dbg(&udev->dev,
0354             "%s is attached on vhci_hcd... skip!\n",
0355             udev_busid);
0356 
0357         rc = -ENODEV;
0358         goto call_put_busid_priv;
0359     }
0360 
0361 
0362     dev_info(&udev->dev,
0363         "usbip-host: register new device (bus %u dev %u)\n",
0364         udev->bus->busnum, udev->devnum);
0365 
0366     busid_priv->shutdown_busid = 0;
0367 
0368     /* set private data to usb_device */
0369     dev_set_drvdata(&udev->dev, sdev);
0370 
0371     busid_priv->sdev = sdev;
0372     busid_priv->udev = udev;
0373 
0374     save_status = busid_priv->status;
0375     busid_priv->status = STUB_BUSID_ALLOC;
0376 
0377     /* release the busid_lock */
0378     put_busid_priv(busid_priv);
0379 
0380     /*
0381      * Claim this hub port.
0382      * It doesn't matter what value we pass as owner
0383      * (struct dev_state) as long as it is unique.
0384      */
0385     rc = usb_hub_claim_port(udev->parent, udev->portnum,
0386             (struct usb_dev_state *) udev);
0387     if (rc) {
0388         dev_dbg(&udev->dev, "unable to claim port\n");
0389         goto err_port;
0390     }
0391 
0392     return 0;
0393 
0394 err_port:
0395     dev_set_drvdata(&udev->dev, NULL);
0396 
0397     /* we already have busid_priv, just lock busid_lock */
0398     spin_lock(&busid_priv->busid_lock);
0399     busid_priv->sdev = NULL;
0400     busid_priv->status = save_status;
0401     spin_unlock(&busid_priv->busid_lock);
0402     /* lock is released - go to free */
0403     goto sdev_free;
0404 
0405 call_put_busid_priv:
0406     /* release the busid_lock */
0407     put_busid_priv(busid_priv);
0408 
0409 sdev_free:
0410     usb_put_dev(udev);
0411     stub_device_free(sdev);
0412 
0413     return rc;
0414 }
0415 
0416 static void shutdown_busid(struct bus_id_priv *busid_priv)
0417 {
0418     usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
0419 
0420     /* wait for the stop of the event handler */
0421     usbip_stop_eh(&busid_priv->sdev->ud);
0422 }
0423 
0424 /*
0425  * called in usb_disconnect() or usb_deregister()
0426  * but only if actconfig(active configuration) exists
0427  */
0428 static void stub_disconnect(struct usb_device *udev)
0429 {
0430     struct stub_device *sdev;
0431     const char *udev_busid = dev_name(&udev->dev);
0432     struct bus_id_priv *busid_priv;
0433     int rc;
0434 
0435     dev_dbg(&udev->dev, "Enter disconnect\n");
0436 
0437     busid_priv = get_busid_priv(udev_busid);
0438     if (!busid_priv) {
0439         BUG();
0440         return;
0441     }
0442 
0443     sdev = dev_get_drvdata(&udev->dev);
0444 
0445     /* get stub_device */
0446     if (!sdev) {
0447         dev_err(&udev->dev, "could not get device");
0448         /* release busid_lock */
0449         put_busid_priv(busid_priv);
0450         return;
0451     }
0452 
0453     dev_set_drvdata(&udev->dev, NULL);
0454 
0455     /* release busid_lock before call to remove device files */
0456     put_busid_priv(busid_priv);
0457 
0458     /*
0459      * NOTE: rx/tx threads are invoked for each usb_device.
0460      */
0461 
0462     /* release port */
0463     rc = usb_hub_release_port(udev->parent, udev->portnum,
0464                   (struct usb_dev_state *) udev);
0465     if (rc) {
0466         dev_dbg(&udev->dev, "unable to release port\n");
0467         return;
0468     }
0469 
0470     /* If usb reset is called from event handler */
0471     if (usbip_in_eh(current))
0472         return;
0473 
0474     /* we already have busid_priv, just lock busid_lock */
0475     spin_lock(&busid_priv->busid_lock);
0476     if (!busid_priv->shutdown_busid)
0477         busid_priv->shutdown_busid = 1;
0478     /* release busid_lock */
0479     spin_unlock(&busid_priv->busid_lock);
0480 
0481     /* shutdown the current connection */
0482     shutdown_busid(busid_priv);
0483 
0484     usb_put_dev(sdev->udev);
0485 
0486     /* we already have busid_priv, just lock busid_lock */
0487     spin_lock(&busid_priv->busid_lock);
0488     /* free sdev */
0489     busid_priv->sdev = NULL;
0490     stub_device_free(sdev);
0491 
0492     if (busid_priv->status == STUB_BUSID_ALLOC)
0493         busid_priv->status = STUB_BUSID_ADDED;
0494     /* release busid_lock */
0495     spin_unlock(&busid_priv->busid_lock);
0496     return;
0497 }
0498 
0499 #ifdef CONFIG_PM
0500 
0501 /* These functions need usb_port_suspend and usb_port_resume,
0502  * which reside in drivers/usb/core/usb.h. Skip for now. */
0503 
0504 static int stub_suspend(struct usb_device *udev, pm_message_t message)
0505 {
0506     dev_dbg(&udev->dev, "stub_suspend\n");
0507 
0508     return 0;
0509 }
0510 
0511 static int stub_resume(struct usb_device *udev, pm_message_t message)
0512 {
0513     dev_dbg(&udev->dev, "stub_resume\n");
0514 
0515     return 0;
0516 }
0517 
0518 #endif  /* CONFIG_PM */
0519 
0520 struct usb_device_driver stub_driver = {
0521     .name       = "usbip-host",
0522     .probe      = stub_probe,
0523     .disconnect = stub_disconnect,
0524 #ifdef CONFIG_PM
0525     .suspend    = stub_suspend,
0526     .resume     = stub_resume,
0527 #endif
0528     .supports_autosuspend   =   0,
0529     .dev_groups = usbip_groups,
0530 };