cregit-Linux how code gets into the kernel

Release 4.11 drivers/usb/usbip/stub_dev.c

/*
 * Copyright (C) 2003-2008 Takahiro Hirofuchi
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 * USA.
 */

#include <linux/device.h>
#include <linux/file.h>
#include <linux/kthread.h>
#include <linux/module.h>

#include "usbip_common.h"
#include "stub.h"

/*
 * usbip_status shows the status of usbip-host as long as this driver is bound
 * to the target device.
 */

static ssize_t usbip_status_show(struct device *dev, struct device_attribute *attr, char *buf) { struct stub_device *sdev = dev_get_drvdata(dev); int status; if (!sdev) { dev_err(dev, "sdev is null\n"); return -ENODEV; } spin_lock_irq(&sdev->ud.lock); status = sdev->ud.status; spin_unlock_irq(&sdev->ud.lock); return snprintf(buf, PAGE_SIZE, "%d\n", status); }

Contributors

PersonTokensPropCommitsCommitProp
Takahiro Hirofuchi8896.70%133.33%
Huawei (Harvey) Yang22.20%133.33%
Greg Kroah-Hartman11.10%133.33%
Total91100.00%3100.00%

static DEVICE_ATTR_RO(usbip_status); /* * usbip_sockfd gets a socket descriptor of an established TCP connection that * is used to transfer usbip requests by kernel threads. -1 is a magic number * by which usbip connection is finished. */
static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct stub_device *sdev = dev_get_drvdata(dev); int sockfd = 0; struct socket *socket; int rv; if (!sdev) { dev_err(dev, "sdev is null\n"); return -ENODEV; } rv = sscanf(buf, "%d", &sockfd); if (rv != 1) return -EINVAL; if (sockfd != -1) { int err; dev_info(dev, "stub up\n"); spin_lock_irq(&sdev->ud.lock); if (sdev->ud.status != SDEV_ST_AVAILABLE) { dev_err(dev, "not ready\n"); goto err; } socket = sockfd_lookup(sockfd, &err); if (!socket) goto err; sdev->ud.tcp_socket = socket; spin_unlock_irq(&sdev->ud.lock); sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud, "stub_rx"); sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud, "stub_tx"); spin_lock_irq(&sdev->ud.lock); sdev->ud.status = SDEV_ST_USED; spin_unlock_irq(&sdev->ud.lock); } else { dev_info(dev, "stub down\n"); spin_lock_irq(&sdev->ud.lock); if (sdev->ud.status != SDEV_ST_USED) goto err; spin_unlock_irq(&sdev->ud.lock); usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN); } return count; err: spin_unlock_irq(&sdev->ud.lock); return -EINVAL; }

Contributors

PersonTokensPropCommitsCommitProp
Takahiro Hirofuchi23074.19%114.29%
Arnd Bergmann278.71%114.29%
Kurt Kanzenbach216.77%114.29%
Elena Oat154.84%114.29%
Al Viro92.90%114.29%
Huawei (Harvey) Yang61.94%114.29%
Oleg Nesterov20.65%114.29%
Total310100.00%7100.00%

static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
static int stub_add_files(struct device *dev) { int err = 0; err = device_create_file(dev, &dev_attr_usbip_status); if (err) goto err_status; err = device_create_file(dev, &dev_attr_usbip_sockfd); if (err) goto err_sockfd; err = device_create_file(dev, &dev_attr_usbip_debug); if (err) goto err_debug; return 0; err_debug: device_remove_file(dev, &dev_attr_usbip_sockfd); err_sockfd: device_remove_file(dev, &dev_attr_usbip_status); err_status: return err; }

Contributors

PersonTokensPropCommitsCommitProp
Takahiro Hirofuchi95100.00%1100.00%
Total95100.00%1100.00%


static void stub_remove_files(struct device *dev) { device_remove_file(dev, &dev_attr_usbip_status); device_remove_file(dev, &dev_attr_usbip_sockfd); device_remove_file(dev, &dev_attr_usbip_debug); }

Contributors

PersonTokensPropCommitsCommitProp
Takahiro Hirofuchi35100.00%1100.00%
Total35100.00%1100.00%


static void stub_shutdown_connection(struct usbip_device *ud) { struct stub_device *sdev = container_of(ud, struct stub_device, ud); /* * When removing an exported device, kernel panic sometimes occurred * and then EIP was sk_wait_data of stub_rx thread. Is this because * sk_wait_data returned though stub_rx thread was already finished by * step 1? */ if (ud->tcp_socket) { dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n", ud->tcp_socket); kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR); } /* 1. stop threads */ if (ud->tcp_rx) { kthread_stop_put(ud->tcp_rx); ud->tcp_rx = NULL; } if (ud->tcp_tx) { kthread_stop_put(ud->tcp_tx); ud->tcp_tx = NULL; } /* * 2. close the socket * * tcp_socket is freed after threads are killed so that usbip_xmit does * not touch NULL socket. */ if (ud->tcp_socket) { sockfd_put(ud->tcp_socket); ud->tcp_socket = NULL; } /* 3. free used data */ stub_device_cleanup_urbs(sdev); /* 4. free stub_unlink */ { unsigned long flags; struct stub_unlink *unlink, *tmp; spin_lock_irqsave(&sdev->priv_lock, flags); list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) { list_del(&unlink->list); kfree(unlink); } list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) { list_del(&unlink->list); kfree(unlink); } spin_unlock_irqrestore(&sdev->priv_lock, flags); } }

Contributors

PersonTokensPropCommitsCommitProp
Takahiro Hirofuchi16877.78%112.50%
Navin Patidar167.41%112.50%
Arjan Mels125.56%112.50%
Matt Mooney94.17%225.00%
Arnd Bergmann83.70%112.50%
Oleg Nesterov20.93%112.50%
Al Viro10.46%112.50%
Total216100.00%8100.00%


static void stub_device_reset(struct usbip_device *ud) { struct stub_device *sdev = container_of(ud, struct stub_device, ud); struct usb_device *udev = sdev->udev; int ret; dev_dbg(&udev->dev, "device reset"); ret = usb_lock_device_for_reset(udev, NULL); if (ret < 0) { dev_err(&udev->dev, "lock for reset\n"); spin_lock_irq(&ud->lock); ud->status = SDEV_ST_ERROR; spin_unlock_irq(&ud->lock); return; } /* try to reset the device */ ret = usb_reset_device(udev); usb_unlock_device(udev); spin_lock_irq(&ud->lock); if (ret) { dev_err(&udev->dev, "device reset\n"); ud->status = SDEV_ST_ERROR; } else { dev_info(&udev->dev, "device reset\n"); ud->status = SDEV_ST_AVAILABLE; } spin_unlock_irq(&ud->lock); }

Contributors

PersonTokensPropCommitsCommitProp
Takahiro Hirofuchi15692.86%120.00%
Matt Mooney63.57%120.00%
Huawei (Harvey) Yang42.38%120.00%
Max Vozeler10.60%120.00%
Alexander Popov10.60%120.00%
Total168100.00%5100.00%


static void stub_device_unusable(struct usbip_device *ud) { spin_lock_irq(&ud->lock); ud->status = SDEV_ST_ERROR; spin_unlock_irq(&ud->lock); }

Contributors

PersonTokensPropCommitsCommitProp
Takahiro Hirofuchi3193.94%150.00%
Huawei (Harvey) Yang26.06%150.00%
Total33100.00%2100.00%

/** * stub_device_alloc - allocate a new stub_device struct * @udev: usb_device of a new device * * Allocates and initializes a new stub_device struct. */
static struct stub_device *stub_device_alloc(struct usb_device *udev) { struct stub_device *sdev; int busnum = udev->bus->busnum; int devnum = udev->devnum; dev_dbg(&udev->dev, "allocating stub device"); /* yes, it's a new device */ sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL); if (!sdev) return NULL; sdev->udev = usb_get_dev(udev); /* * devid is defined with devnum when this driver is first allocated. * devnum may change later if a device is reset. However, devid never * changes during a usbip connection. */ sdev->devid = (busnum << 16) | devnum; sdev->ud.side = USBIP_STUB; sdev->ud.status = SDEV_ST_AVAILABLE; spin_lock_init(&sdev->ud.lock); sdev->ud.tcp_socket = NULL; INIT_LIST_HEAD(&sdev->priv_init); INIT_LIST_HEAD(&sdev->priv_tx); INIT_LIST_HEAD(&sdev->priv_free); INIT_LIST_HEAD(&sdev->unlink_free); INIT_LIST_HEAD(&sdev->unlink_tx); spin_lock_init(&sdev->priv_lock); init_waitqueue_head(&sdev->tx_waitq); sdev->ud.eh_ops.shutdown = stub_shutdown_connection; sdev->ud.eh_ops.reset = stub_device_reset; sdev->ud.eh_ops.unusable = stub_device_unusable; usbip_start_eh(&sdev->ud); dev_dbg(&udev->dev, "register new device\n"); return sdev; }

Contributors

PersonTokensPropCommitsCommitProp
Takahiro Hirofuchi20489.08%125.00%
Valentina Manea114.80%125.00%
Max Vozeler93.93%125.00%
Matt Mooney52.18%125.00%
Total229100.00%4100.00%


static void stub_device_free(struct stub_device *sdev) { kfree(sdev); }

Contributors

PersonTokensPropCommitsCommitProp
Takahiro Hirofuchi1593.75%150.00%
Kurt Kanzenbach16.25%150.00%
Total16100.00%2100.00%


static int stub_probe(struct usb_device *udev) { struct stub_device *sdev = NULL; const char *udev_busid = dev_name(&udev->dev); struct bus_id_priv *busid_priv; int rc; dev_dbg(&udev->dev, "Enter\n"); /* check we should claim or not by busid_table */ busid_priv = get_busid_priv(udev_busid); if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) || (busid_priv->status == STUB_BUSID_OTHER)) { dev_info(&udev->dev, "%s is not in match_busid table... skip!\n", udev_busid); /* * Return value should be ENODEV or ENOXIO to continue trying * other matched drivers by the driver core. * See driver_probe_device() in driver/base/dd.c */ return -ENODEV; } if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) { dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n", udev_busid); return -ENODEV; } if (!strcmp(udev->bus->bus_name, "vhci_hcd")) { dev_dbg(&udev->dev, "%s is attached on vhci_hcd... skip!\n", udev_busid); return -ENODEV; } /* ok, this is my device */ sdev = stub_device_alloc(udev); if (!sdev) return -ENOMEM; dev_info(&udev->dev, "usbip-host: register new device (bus %u dev %u)\n", udev->bus->busnum, udev->devnum); busid_priv->shutdown_busid = 0; /* set private data to usb_device */ dev_set_drvdata(&udev->dev, sdev); busid_priv->sdev = sdev; busid_priv->udev = udev; /* * Claim this hub port. * It doesn't matter what value we pass as owner * (struct dev_state) as long as it is unique. */ rc = usb_hub_claim_port(udev->parent, udev->portnum, (struct usb_dev_state *) udev); if (rc) { dev_dbg(&udev->dev, "unable to claim port\n"); goto err_port; } rc = stub_add_files(&udev->dev); if (rc) { dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid); goto err_files; } busid_priv->status = STUB_BUSID_ALLOC; return 0; err_files: usb_hub_release_port(udev->parent, udev->portnum, (struct usb_dev_state *) udev); err_port: dev_set_drvdata(&udev->dev, NULL); usb_put_dev(udev); busid_priv->sdev = NULL; stub_device_free(sdev); return rc; }

Contributors

PersonTokensPropCommitsCommitProp
Takahiro Hirofuchi12936.44%18.33%
Valentina Manea7822.03%433.33%
Endre Kollar7220.34%18.33%
Alexey Khoroshilov4111.58%18.33%
Matt Mooney154.24%18.33%
Vasiliy Kulikov92.54%18.33%
Huawei (Harvey) Yang51.41%18.33%
Kay Sievers41.13%18.33%
Elad Wexler10.28%18.33%
Total354100.00%12100.00%


static void shutdown_busid(struct bus_id_priv *busid_priv) { if (busid_priv->sdev && !busid_priv->shutdown_busid) { busid_priv->shutdown_busid = 1; usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED); /* wait for the stop of the event handler */ usbip_stop_eh(&busid_priv->sdev->ud); } }

Contributors

PersonTokensPropCommitsCommitProp
Endre Kollar5298.11%150.00%
Kurt Kanzenbach11.89%150.00%
Total53100.00%2100.00%

/* * called in usb_disconnect() or usb_deregister() * but only if actconfig(active configuration) exists */
static void stub_disconnect(struct usb_device *udev) { struct stub_device *sdev; const char *udev_busid = dev_name(&udev->dev); struct bus_id_priv *busid_priv; int rc; dev_dbg(&udev->dev, "Enter\n"); busid_priv = get_busid_priv(udev_busid); if (!busid_priv) { BUG(); return; } sdev = dev_get_drvdata(&udev->dev); /* get stub_device */ if (!sdev) { dev_err(&udev->dev, "could not get device"); return; } dev_set_drvdata(&udev->dev, NULL); /* * NOTE: rx/tx threads are invoked for each usb_device. */ stub_remove_files(&udev->dev); /* release port */ rc = usb_hub_release_port(udev->parent, udev->portnum, (struct usb_dev_state *) udev); if (rc) { dev_dbg(&udev->dev, "unable to release port\n"); return; } /* If usb reset is called from event handler */ if (usbip_in_eh(current)) return; /* shutdown the current connection */ shutdown_busid(busid_priv); usb_put_dev(sdev->udev); /* free sdev */ busid_priv->sdev = NULL; stub_device_free(sdev); if (busid_priv->status == STUB_BUSID_ALLOC) { busid_priv->status = STUB_BUSID_ADDED; } else { busid_priv->status = STUB_BUSID_OTHER; del_match_busid((char *)udev_busid); } }

Contributors

PersonTokensPropCommitsCommitProp
Endre Kollar8438.36%111.11%
Valentina Manea5826.48%333.33%
Takahiro Hirofuchi4821.92%111.11%
Matt Mooney156.85%111.11%
Max Vozeler73.20%111.11%
Bart Westgeest41.83%111.11%
Nobuo Iwata31.37%111.11%
Total219100.00%9100.00%

#ifdef CONFIG_PM /* These functions need usb_port_suspend and usb_port_resume, * which reside in drivers/usb/core/usb.h. Skip for now. */
static int stub_suspend(struct usb_device *udev, pm_message_t message) { dev_dbg(&udev->dev, "stub_suspend\n"); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Arjan Mels1866.67%133.33%
Valentina Manea829.63%133.33%
Peter Hüwe13.70%133.33%
Total27100.00%3100.00%


static int stub_resume(struct usb_device *udev, pm_message_t message) { dev_dbg(&udev->dev, "stub_resume\n"); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Arjan Mels1866.67%133.33%
Valentina Manea829.63%133.33%
Peter Hüwe13.70%133.33%
Total27100.00%3100.00%

#endif /* CONFIG_PM */ struct usb_device_driver stub_driver = { .name = "usbip-host", .probe = stub_probe, .disconnect = stub_disconnect, #ifdef CONFIG_PM .suspend = stub_suspend, .resume = stub_resume, #endif .supports_autosuspend = 0, };

Overall Contributors

PersonTokensPropCommitsCommitProp
Takahiro Hirofuchi122562.44%12.78%
Endre Kollar20810.60%12.78%
Valentina Manea1829.28%411.11%
Matt Mooney743.77%411.11%
Arjan Mels542.75%25.56%
Alexey Khoroshilov412.09%12.78%
Arnd Bergmann381.94%12.78%
Kurt Kanzenbach231.17%38.33%
Huawei (Harvey) Yang190.97%25.56%
Max Vozeler170.87%12.78%
Navin Patidar160.82%12.78%
Elena Oat150.76%12.78%
Al Viro100.51%12.78%
Vasiliy Kulikov90.46%12.78%
Bart Westgeest40.20%12.78%
Kay Sievers40.20%12.78%
Oleg Nesterov40.20%12.78%
Nobuo Iwata30.15%12.78%
Bernard Blackham30.15%12.78%
Paul Gortmaker30.15%12.78%
Peter Hüwe20.10%12.78%
Tejun Heo20.10%12.78%
Alexander Popov20.10%12.78%
Greg Kroah-Hartman20.10%12.78%
Elad Wexler10.05%12.78%
Akshay Joshi10.05%12.78%
Total1962100.00%36100.00%
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.