15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
296c27377SValentina Manea /*
396c27377SValentina Manea * Copyright (C) 2003-2008 Takahiro Hirofuchi
496c27377SValentina Manea */
596c27377SValentina Manea
696c27377SValentina Manea #include <linux/device.h>
796c27377SValentina Manea #include <linux/file.h>
896c27377SValentina Manea #include <linux/kthread.h>
996c27377SValentina Manea #include <linux/module.h>
1096c27377SValentina Manea
1196c27377SValentina Manea #include "usbip_common.h"
1296c27377SValentina Manea #include "stub.h"
1396c27377SValentina Manea
1496c27377SValentina Manea /*
1596c27377SValentina Manea * usbip_status shows the status of usbip-host as long as this driver is bound
1696c27377SValentina Manea * to the target device.
1796c27377SValentina Manea */
usbip_status_show(struct device * dev,struct device_attribute * attr,char * buf)1896c27377SValentina Manea static ssize_t usbip_status_show(struct device *dev,
1996c27377SValentina Manea struct device_attribute *attr, char *buf)
2096c27377SValentina Manea {
2196c27377SValentina Manea struct stub_device *sdev = dev_get_drvdata(dev);
2296c27377SValentina Manea int status;
2396c27377SValentina Manea
2496c27377SValentina Manea if (!sdev) {
2596c27377SValentina Manea dev_err(dev, "sdev is null\n");
2696c27377SValentina Manea return -ENODEV;
2796c27377SValentina Manea }
2896c27377SValentina Manea
2996c27377SValentina Manea spin_lock_irq(&sdev->ud.lock);
3096c27377SValentina Manea status = sdev->ud.status;
3196c27377SValentina Manea spin_unlock_irq(&sdev->ud.lock);
3296c27377SValentina Manea
3327ef01e3SXuezhi Zhang return sysfs_emit(buf, "%d\n", status);
3496c27377SValentina Manea }
3596c27377SValentina Manea static DEVICE_ATTR_RO(usbip_status);
3696c27377SValentina Manea
3796c27377SValentina Manea /*
3896c27377SValentina Manea * usbip_sockfd gets a socket descriptor of an established TCP connection that
3996c27377SValentina Manea * is used to transfer usbip requests by kernel threads. -1 is a magic number
4096c27377SValentina Manea * by which usbip connection is finished.
4196c27377SValentina Manea */
usbip_sockfd_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)42ca35910aSGreg Kroah-Hartman static ssize_t usbip_sockfd_store(struct device *dev, struct device_attribute *attr,
4396c27377SValentina Manea const char *buf, size_t count)
4496c27377SValentina Manea {
4596c27377SValentina Manea struct stub_device *sdev = dev_get_drvdata(dev);
4696c27377SValentina Manea int sockfd = 0;
4796c27377SValentina Manea struct socket *socket;
4896c27377SValentina Manea int rv;
499380afd6SShuah Khan struct task_struct *tcp_rx = NULL;
509380afd6SShuah Khan struct task_struct *tcp_tx = NULL;
5196c27377SValentina Manea
5296c27377SValentina Manea if (!sdev) {
5396c27377SValentina Manea dev_err(dev, "sdev is null\n");
5496c27377SValentina Manea return -ENODEV;
5596c27377SValentina Manea }
5696c27377SValentina Manea
5796c27377SValentina Manea rv = sscanf(buf, "%d", &sockfd);
5896c27377SValentina Manea if (rv != 1)
5996c27377SValentina Manea return -EINVAL;
6096c27377SValentina Manea
6196c27377SValentina Manea if (sockfd != -1) {
6296c27377SValentina Manea int err;
6396c27377SValentina Manea
6496c27377SValentina Manea dev_info(dev, "stub up\n");
6596c27377SValentina Manea
669dbf34a8SShuah Khan mutex_lock(&sdev->ud.sysfs_lock);
6796c27377SValentina Manea spin_lock_irq(&sdev->ud.lock);
6896c27377SValentina Manea
6996c27377SValentina Manea if (sdev->ud.status != SDEV_ST_AVAILABLE) {
7096c27377SValentina Manea dev_err(dev, "not ready\n");
7196c27377SValentina Manea goto err;
7296c27377SValentina Manea }
7396c27377SValentina Manea
7496c27377SValentina Manea socket = sockfd_lookup(sockfd, &err);
7547ccc8fcSShuah Khan if (!socket) {
7647ccc8fcSShuah Khan dev_err(dev, "failed to lookup sock");
7796c27377SValentina Manea goto err;
7847ccc8fcSShuah Khan }
7947ccc8fcSShuah Khan
8047ccc8fcSShuah Khan if (socket->type != SOCK_STREAM) {
8147ccc8fcSShuah Khan dev_err(dev, "Expecting SOCK_STREAM - found %d",
8247ccc8fcSShuah Khan socket->type);
8347ccc8fcSShuah Khan goto sock_err;
8447ccc8fcSShuah Khan }
8596c27377SValentina Manea
869380afd6SShuah Khan /* unlock and create threads and get tasks */
879380afd6SShuah Khan spin_unlock_irq(&sdev->ud.lock);
889380afd6SShuah Khan tcp_rx = kthread_create(stub_rx_loop, &sdev->ud, "stub_rx");
899380afd6SShuah Khan if (IS_ERR(tcp_rx)) {
909380afd6SShuah Khan sockfd_put(socket);
919dbf34a8SShuah Khan goto unlock_mutex;
929380afd6SShuah Khan }
939380afd6SShuah Khan tcp_tx = kthread_create(stub_tx_loop, &sdev->ud, "stub_tx");
949380afd6SShuah Khan if (IS_ERR(tcp_tx)) {
959380afd6SShuah Khan kthread_stop(tcp_rx);
969380afd6SShuah Khan sockfd_put(socket);
979dbf34a8SShuah Khan goto unlock_mutex;
989380afd6SShuah Khan }
999380afd6SShuah Khan
1009380afd6SShuah Khan /* get task structs now */
1019380afd6SShuah Khan get_task_struct(tcp_rx);
1029380afd6SShuah Khan get_task_struct(tcp_tx);
1039380afd6SShuah Khan
1049380afd6SShuah Khan /* lock and update sdev->ud state */
1059380afd6SShuah Khan spin_lock_irq(&sdev->ud.lock);
10696c27377SValentina Manea sdev->ud.tcp_socket = socket;
107009f41aeSShuah Khan sdev->ud.sockfd = sockfd;
1089380afd6SShuah Khan sdev->ud.tcp_rx = tcp_rx;
1099380afd6SShuah Khan sdev->ud.tcp_tx = tcp_tx;
11096c27377SValentina Manea sdev->ud.status = SDEV_ST_USED;
11196c27377SValentina Manea spin_unlock_irq(&sdev->ud.lock);
11296c27377SValentina Manea
1139380afd6SShuah Khan wake_up_process(sdev->ud.tcp_rx);
1149380afd6SShuah Khan wake_up_process(sdev->ud.tcp_tx);
1159380afd6SShuah Khan
1169dbf34a8SShuah Khan mutex_unlock(&sdev->ud.sysfs_lock);
1179dbf34a8SShuah Khan
11896c27377SValentina Manea } else {
11996c27377SValentina Manea dev_info(dev, "stub down\n");
12096c27377SValentina Manea
1219b6447e0SJose Ignacio Tornos Martinez mutex_lock(&sdev->ud.sysfs_lock);
1229b6447e0SJose Ignacio Tornos Martinez
12396c27377SValentina Manea spin_lock_irq(&sdev->ud.lock);
12496c27377SValentina Manea if (sdev->ud.status != SDEV_ST_USED)
12596c27377SValentina Manea goto err;
12696c27377SValentina Manea
12796c27377SValentina Manea spin_unlock_irq(&sdev->ud.lock);
12896c27377SValentina Manea
12996c27377SValentina Manea usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
1309dbf34a8SShuah Khan mutex_unlock(&sdev->ud.sysfs_lock);
13196c27377SValentina Manea }
13296c27377SValentina Manea
13396c27377SValentina Manea return count;
13496c27377SValentina Manea
13547ccc8fcSShuah Khan sock_err:
13647ccc8fcSShuah Khan sockfd_put(socket);
13796c27377SValentina Manea err:
13896c27377SValentina Manea spin_unlock_irq(&sdev->ud.lock);
1399dbf34a8SShuah Khan unlock_mutex:
1409dbf34a8SShuah Khan mutex_unlock(&sdev->ud.sysfs_lock);
14196c27377SValentina Manea return -EINVAL;
14296c27377SValentina Manea }
143ca35910aSGreg Kroah-Hartman static DEVICE_ATTR_WO(usbip_sockfd);
14496c27377SValentina Manea
145c5501d23SGreg Kroah-Hartman static struct attribute *usbip_attrs[] = {
146c5501d23SGreg Kroah-Hartman &dev_attr_usbip_status.attr,
147c5501d23SGreg Kroah-Hartman &dev_attr_usbip_sockfd.attr,
148c5501d23SGreg Kroah-Hartman &dev_attr_usbip_debug.attr,
149c5501d23SGreg Kroah-Hartman NULL,
150c5501d23SGreg Kroah-Hartman };
151c5501d23SGreg Kroah-Hartman ATTRIBUTE_GROUPS(usbip);
15296c27377SValentina Manea
stub_shutdown_connection(struct usbip_device * ud)15396c27377SValentina Manea static void stub_shutdown_connection(struct usbip_device *ud)
15496c27377SValentina Manea {
15596c27377SValentina Manea struct stub_device *sdev = container_of(ud, struct stub_device, ud);
15696c27377SValentina Manea
15796c27377SValentina Manea /*
15896c27377SValentina Manea * When removing an exported device, kernel panic sometimes occurred
15996c27377SValentina Manea * and then EIP was sk_wait_data of stub_rx thread. Is this because
16096c27377SValentina Manea * sk_wait_data returned though stub_rx thread was already finished by
16196c27377SValentina Manea * step 1?
16296c27377SValentina Manea */
16396c27377SValentina Manea if (ud->tcp_socket) {
16490120d15SShuah Khan dev_dbg(&sdev->udev->dev, "shutdown sockfd %d\n", ud->sockfd);
16596c27377SValentina Manea kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
16696c27377SValentina Manea }
16796c27377SValentina Manea
16896c27377SValentina Manea /* 1. stop threads */
16996c27377SValentina Manea if (ud->tcp_rx) {
17096c27377SValentina Manea kthread_stop_put(ud->tcp_rx);
17196c27377SValentina Manea ud->tcp_rx = NULL;
17296c27377SValentina Manea }
17396c27377SValentina Manea if (ud->tcp_tx) {
17496c27377SValentina Manea kthread_stop_put(ud->tcp_tx);
17596c27377SValentina Manea ud->tcp_tx = NULL;
17696c27377SValentina Manea }
17796c27377SValentina Manea
17896c27377SValentina Manea /*
17996c27377SValentina Manea * 2. close the socket
18096c27377SValentina Manea *
18196c27377SValentina Manea * tcp_socket is freed after threads are killed so that usbip_xmit does
18296c27377SValentina Manea * not touch NULL socket.
18396c27377SValentina Manea */
18496c27377SValentina Manea if (ud->tcp_socket) {
18596c27377SValentina Manea sockfd_put(ud->tcp_socket);
18696c27377SValentina Manea ud->tcp_socket = NULL;
187009f41aeSShuah Khan ud->sockfd = -1;
18896c27377SValentina Manea }
18996c27377SValentina Manea
19096c27377SValentina Manea /* 3. free used data */
19196c27377SValentina Manea stub_device_cleanup_urbs(sdev);
19296c27377SValentina Manea
19396c27377SValentina Manea /* 4. free stub_unlink */
19496c27377SValentina Manea {
19596c27377SValentina Manea unsigned long flags;
19696c27377SValentina Manea struct stub_unlink *unlink, *tmp;
19796c27377SValentina Manea
19896c27377SValentina Manea spin_lock_irqsave(&sdev->priv_lock, flags);
19996c27377SValentina Manea list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
20096c27377SValentina Manea list_del(&unlink->list);
20196c27377SValentina Manea kfree(unlink);
20296c27377SValentina Manea }
20396c27377SValentina Manea list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
20496c27377SValentina Manea list) {
20596c27377SValentina Manea list_del(&unlink->list);
20696c27377SValentina Manea kfree(unlink);
20796c27377SValentina Manea }
20896c27377SValentina Manea spin_unlock_irqrestore(&sdev->priv_lock, flags);
20996c27377SValentina Manea }
21096c27377SValentina Manea }
21196c27377SValentina Manea
stub_device_reset(struct usbip_device * ud)21296c27377SValentina Manea static void stub_device_reset(struct usbip_device *ud)
21396c27377SValentina Manea {
21496c27377SValentina Manea struct stub_device *sdev = container_of(ud, struct stub_device, ud);
21596c27377SValentina Manea struct usb_device *udev = sdev->udev;
21696c27377SValentina Manea int ret;
21796c27377SValentina Manea
21896c27377SValentina Manea dev_dbg(&udev->dev, "device reset");
21996c27377SValentina Manea
2208c7003a3SAlexander Popov ret = usb_lock_device_for_reset(udev, NULL);
22196c27377SValentina Manea if (ret < 0) {
22296c27377SValentina Manea dev_err(&udev->dev, "lock for reset\n");
22396c27377SValentina Manea spin_lock_irq(&ud->lock);
22496c27377SValentina Manea ud->status = SDEV_ST_ERROR;
22596c27377SValentina Manea spin_unlock_irq(&ud->lock);
22696c27377SValentina Manea return;
22796c27377SValentina Manea }
22896c27377SValentina Manea
22996c27377SValentina Manea /* try to reset the device */
23096c27377SValentina Manea ret = usb_reset_device(udev);
23196c27377SValentina Manea usb_unlock_device(udev);
23296c27377SValentina Manea
23396c27377SValentina Manea spin_lock_irq(&ud->lock);
23496c27377SValentina Manea if (ret) {
23596c27377SValentina Manea dev_err(&udev->dev, "device reset\n");
23696c27377SValentina Manea ud->status = SDEV_ST_ERROR;
23796c27377SValentina Manea } else {
23896c27377SValentina Manea dev_info(&udev->dev, "device reset\n");
23996c27377SValentina Manea ud->status = SDEV_ST_AVAILABLE;
24096c27377SValentina Manea }
24196c27377SValentina Manea spin_unlock_irq(&ud->lock);
24296c27377SValentina Manea }
24396c27377SValentina Manea
stub_device_unusable(struct usbip_device * ud)24496c27377SValentina Manea static void stub_device_unusable(struct usbip_device *ud)
24596c27377SValentina Manea {
24696c27377SValentina Manea spin_lock_irq(&ud->lock);
24796c27377SValentina Manea ud->status = SDEV_ST_ERROR;
24896c27377SValentina Manea spin_unlock_irq(&ud->lock);
24996c27377SValentina Manea }
25096c27377SValentina Manea
25196c27377SValentina Manea /**
25296c27377SValentina Manea * stub_device_alloc - allocate a new stub_device struct
2538c7003a3SAlexander Popov * @udev: usb_device of a new device
25496c27377SValentina Manea *
25596c27377SValentina Manea * Allocates and initializes a new stub_device struct.
25696c27377SValentina Manea */
stub_device_alloc(struct usb_device * udev)25796c27377SValentina Manea static struct stub_device *stub_device_alloc(struct usb_device *udev)
25896c27377SValentina Manea {
25996c27377SValentina Manea struct stub_device *sdev;
26096c27377SValentina Manea int busnum = udev->bus->busnum;
26196c27377SValentina Manea int devnum = udev->devnum;
26296c27377SValentina Manea
26396c27377SValentina Manea dev_dbg(&udev->dev, "allocating stub device");
26496c27377SValentina Manea
26596c27377SValentina Manea /* yes, it's a new device */
26696c27377SValentina Manea sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
26796c27377SValentina Manea if (!sdev)
26896c27377SValentina Manea return NULL;
26996c27377SValentina Manea
27096c27377SValentina Manea sdev->udev = usb_get_dev(udev);
27196c27377SValentina Manea
27296c27377SValentina Manea /*
27396c27377SValentina Manea * devid is defined with devnum when this driver is first allocated.
27496c27377SValentina Manea * devnum may change later if a device is reset. However, devid never
27596c27377SValentina Manea * changes during a usbip connection.
27696c27377SValentina Manea */
27796c27377SValentina Manea sdev->devid = (busnum << 16) | devnum;
27896c27377SValentina Manea sdev->ud.side = USBIP_STUB;
27996c27377SValentina Manea sdev->ud.status = SDEV_ST_AVAILABLE;
28096c27377SValentina Manea spin_lock_init(&sdev->ud.lock);
2819dbf34a8SShuah Khan mutex_init(&sdev->ud.sysfs_lock);
28296c27377SValentina Manea sdev->ud.tcp_socket = NULL;
283009f41aeSShuah Khan sdev->ud.sockfd = -1;
28496c27377SValentina Manea
28596c27377SValentina Manea INIT_LIST_HEAD(&sdev->priv_init);
28696c27377SValentina Manea INIT_LIST_HEAD(&sdev->priv_tx);
28796c27377SValentina Manea INIT_LIST_HEAD(&sdev->priv_free);
28896c27377SValentina Manea INIT_LIST_HEAD(&sdev->unlink_free);
28996c27377SValentina Manea INIT_LIST_HEAD(&sdev->unlink_tx);
29096c27377SValentina Manea spin_lock_init(&sdev->priv_lock);
29196c27377SValentina Manea
29296c27377SValentina Manea init_waitqueue_head(&sdev->tx_waitq);
29396c27377SValentina Manea
29496c27377SValentina Manea sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
29596c27377SValentina Manea sdev->ud.eh_ops.reset = stub_device_reset;
29696c27377SValentina Manea sdev->ud.eh_ops.unusable = stub_device_unusable;
29796c27377SValentina Manea
29896c27377SValentina Manea usbip_start_eh(&sdev->ud);
29996c27377SValentina Manea
30096c27377SValentina Manea dev_dbg(&udev->dev, "register new device\n");
30196c27377SValentina Manea
30296c27377SValentina Manea return sdev;
30396c27377SValentina Manea }
30496c27377SValentina Manea
stub_device_free(struct stub_device * sdev)30596c27377SValentina Manea static void stub_device_free(struct stub_device *sdev)
30696c27377SValentina Manea {
30796c27377SValentina Manea kfree(sdev);
30896c27377SValentina Manea }
30996c27377SValentina Manea
stub_probe(struct usb_device * udev)31096c27377SValentina Manea static int stub_probe(struct usb_device *udev)
31196c27377SValentina Manea {
31296c27377SValentina Manea struct stub_device *sdev = NULL;
31396c27377SValentina Manea const char *udev_busid = dev_name(&udev->dev);
31496c27377SValentina Manea struct bus_id_priv *busid_priv;
31522076557SShuah Khan (Samsung OSG) int rc = 0;
3160c9e8b3cSShuah Khan char save_status;
31796c27377SValentina Manea
31828b68accSShuah Khan dev_dbg(&udev->dev, "Enter probe\n");
31996c27377SValentina Manea
3200c9e8b3cSShuah Khan /* Not sure if this is our device. Allocate here to avoid
3210c9e8b3cSShuah Khan * calling alloc while holding busid_table lock.
3220c9e8b3cSShuah Khan */
3230c9e8b3cSShuah Khan sdev = stub_device_alloc(udev);
3240c9e8b3cSShuah Khan if (!sdev)
3250c9e8b3cSShuah Khan return -ENOMEM;
3260c9e8b3cSShuah Khan
32796c27377SValentina Manea /* check we should claim or not by busid_table */
32896c27377SValentina Manea busid_priv = get_busid_priv(udev_busid);
32996c27377SValentina Manea if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
33096c27377SValentina Manea (busid_priv->status == STUB_BUSID_OTHER)) {
33196c27377SValentina Manea dev_info(&udev->dev,
33296c27377SValentina Manea "%s is not in match_busid table... skip!\n",
33396c27377SValentina Manea udev_busid);
33496c27377SValentina Manea
33596c27377SValentina Manea /*
33696c27377SValentina Manea * Return value should be ENODEV or ENOXIO to continue trying
33796c27377SValentina Manea * other matched drivers by the driver core.
33896c27377SValentina Manea * See driver_probe_device() in driver/base/dd.c
33996c27377SValentina Manea */
34022076557SShuah Khan (Samsung OSG) rc = -ENODEV;
3413ea3091fSShuah Khan if (!busid_priv)
3420c9e8b3cSShuah Khan goto sdev_free;
3433ea3091fSShuah Khan
3443ea3091fSShuah Khan goto call_put_busid_priv;
34596c27377SValentina Manea }
34696c27377SValentina Manea
34796c27377SValentina Manea if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
34896c27377SValentina Manea dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
34996c27377SValentina Manea udev_busid);
35022076557SShuah Khan (Samsung OSG) rc = -ENODEV;
3513ea3091fSShuah Khan goto call_put_busid_priv;
35296c27377SValentina Manea }
35396c27377SValentina Manea
35496c27377SValentina Manea if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
35596c27377SValentina Manea dev_dbg(&udev->dev,
35696c27377SValentina Manea "%s is attached on vhci_hcd... skip!\n",
35796c27377SValentina Manea udev_busid);
35896c27377SValentina Manea
35922076557SShuah Khan (Samsung OSG) rc = -ENODEV;
3603ea3091fSShuah Khan goto call_put_busid_priv;
36196c27377SValentina Manea }
36296c27377SValentina Manea
36396c27377SValentina Manea
36496c27377SValentina Manea dev_info(&udev->dev,
36596c27377SValentina Manea "usbip-host: register new device (bus %u dev %u)\n",
36696c27377SValentina Manea udev->bus->busnum, udev->devnum);
36796c27377SValentina Manea
36896c27377SValentina Manea busid_priv->shutdown_busid = 0;
36996c27377SValentina Manea
37096c27377SValentina Manea /* set private data to usb_device */
37196c27377SValentina Manea dev_set_drvdata(&udev->dev, sdev);
3720c9e8b3cSShuah Khan
37396c27377SValentina Manea busid_priv->sdev = sdev;
37496c27377SValentina Manea busid_priv->udev = udev;
37596c27377SValentina Manea
3760c9e8b3cSShuah Khan save_status = busid_priv->status;
3770c9e8b3cSShuah Khan busid_priv->status = STUB_BUSID_ALLOC;
3780c9e8b3cSShuah Khan
3793ea3091fSShuah Khan /* release the busid_lock */
3803ea3091fSShuah Khan put_busid_priv(busid_priv);
3813ea3091fSShuah Khan
38296c27377SValentina Manea /*
38396c27377SValentina Manea * Claim this hub port.
38496c27377SValentina Manea * It doesn't matter what value we pass as owner
38596c27377SValentina Manea * (struct dev_state) as long as it is unique.
38696c27377SValentina Manea */
38796c27377SValentina Manea rc = usb_hub_claim_port(udev->parent, udev->portnum,
38896c27377SValentina Manea (struct usb_dev_state *) udev);
38996c27377SValentina Manea if (rc) {
39096c27377SValentina Manea dev_dbg(&udev->dev, "unable to claim port\n");
3913ff67445SAlexey Khoroshilov goto err_port;
39296c27377SValentina Manea }
39396c27377SValentina Manea
3940c9e8b3cSShuah Khan return 0;
39522076557SShuah Khan (Samsung OSG)
3963ff67445SAlexey Khoroshilov err_port:
39796c27377SValentina Manea dev_set_drvdata(&udev->dev, NULL);
39896c27377SValentina Manea
3990c9e8b3cSShuah Khan /* we already have busid_priv, just lock busid_lock */
4000c9e8b3cSShuah Khan spin_lock(&busid_priv->busid_lock);
40196c27377SValentina Manea busid_priv->sdev = NULL;
4020c9e8b3cSShuah Khan busid_priv->status = save_status;
4033ea3091fSShuah Khan spin_unlock(&busid_priv->busid_lock);
4043ea3091fSShuah Khan /* lock is released - go to free */
4053ea3091fSShuah Khan goto sdev_free;
4063ea3091fSShuah Khan
4073ea3091fSShuah Khan call_put_busid_priv:
4080c9e8b3cSShuah Khan /* release the busid_lock */
40922076557SShuah Khan (Samsung OSG) put_busid_priv(busid_priv);
4100c9e8b3cSShuah Khan
4113ea3091fSShuah Khan sdev_free:
4129ec4cbf1SHangyu Hua usb_put_dev(udev);
4133ea3091fSShuah Khan stub_device_free(sdev);
4143ea3091fSShuah Khan
4153ff67445SAlexey Khoroshilov return rc;
41696c27377SValentina Manea }
41796c27377SValentina Manea
shutdown_busid(struct bus_id_priv * busid_priv)41896c27377SValentina Manea static void shutdown_busid(struct bus_id_priv *busid_priv)
41996c27377SValentina Manea {
42096c27377SValentina Manea usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
42196c27377SValentina Manea
42296c27377SValentina Manea /* wait for the stop of the event handler */
42396c27377SValentina Manea usbip_stop_eh(&busid_priv->sdev->ud);
42496c27377SValentina Manea }
42596c27377SValentina Manea
42696c27377SValentina Manea /*
42796c27377SValentina Manea * called in usb_disconnect() or usb_deregister()
42896c27377SValentina Manea * but only if actconfig(active configuration) exists
42996c27377SValentina Manea */
stub_disconnect(struct usb_device * udev)43096c27377SValentina Manea static void stub_disconnect(struct usb_device *udev)
43196c27377SValentina Manea {
43296c27377SValentina Manea struct stub_device *sdev;
43396c27377SValentina Manea const char *udev_busid = dev_name(&udev->dev);
43496c27377SValentina Manea struct bus_id_priv *busid_priv;
43596c27377SValentina Manea int rc;
43696c27377SValentina Manea
43728b68accSShuah Khan dev_dbg(&udev->dev, "Enter disconnect\n");
43896c27377SValentina Manea
43996c27377SValentina Manea busid_priv = get_busid_priv(udev_busid);
44096c27377SValentina Manea if (!busid_priv) {
44196c27377SValentina Manea BUG();
44296c27377SValentina Manea return;
44396c27377SValentina Manea }
44496c27377SValentina Manea
44596c27377SValentina Manea sdev = dev_get_drvdata(&udev->dev);
44696c27377SValentina Manea
44796c27377SValentina Manea /* get stub_device */
44896c27377SValentina Manea if (!sdev) {
44996c27377SValentina Manea dev_err(&udev->dev, "could not get device");
4503ea3091fSShuah Khan /* release busid_lock */
4513ea3091fSShuah Khan put_busid_priv(busid_priv);
4523ea3091fSShuah Khan return;
45396c27377SValentina Manea }
45496c27377SValentina Manea
45596c27377SValentina Manea dev_set_drvdata(&udev->dev, NULL);
45696c27377SValentina Manea
4570c9e8b3cSShuah Khan /* release busid_lock before call to remove device files */
4580c9e8b3cSShuah Khan put_busid_priv(busid_priv);
4590c9e8b3cSShuah Khan
46096c27377SValentina Manea /*
46196c27377SValentina Manea * NOTE: rx/tx threads are invoked for each usb_device.
46296c27377SValentina Manea */
46396c27377SValentina Manea
46496c27377SValentina Manea /* release port */
46596c27377SValentina Manea rc = usb_hub_release_port(udev->parent, udev->portnum,
46696c27377SValentina Manea (struct usb_dev_state *) udev);
467*0976757aSJonas Blixt /*
468*0976757aSJonas Blixt * NOTE: If a HUB disconnect triggered disconnect of the down stream
469*0976757aSJonas Blixt * device usb_hub_release_port will return -ENODEV so we can safely ignore
470*0976757aSJonas Blixt * that error here.
471*0976757aSJonas Blixt */
472*0976757aSJonas Blixt if (rc && (rc != -ENODEV)) {
473*0976757aSJonas Blixt dev_dbg(&udev->dev, "unable to release port (%i)\n", rc);
4740c9e8b3cSShuah Khan return;
47596c27377SValentina Manea }
47696c27377SValentina Manea
47796c27377SValentina Manea /* If usb reset is called from event handler */
478bb7871adSNobuo Iwata if (usbip_in_eh(current))
4790c9e8b3cSShuah Khan return;
4800c9e8b3cSShuah Khan
4810c9e8b3cSShuah Khan /* we already have busid_priv, just lock busid_lock */
4820c9e8b3cSShuah Khan spin_lock(&busid_priv->busid_lock);
4830c9e8b3cSShuah Khan if (!busid_priv->shutdown_busid)
4840c9e8b3cSShuah Khan busid_priv->shutdown_busid = 1;
4850c9e8b3cSShuah Khan /* release busid_lock */
4863ea3091fSShuah Khan spin_unlock(&busid_priv->busid_lock);
48796c27377SValentina Manea
48896c27377SValentina Manea /* shutdown the current connection */
48996c27377SValentina Manea shutdown_busid(busid_priv);
49096c27377SValentina Manea
49196c27377SValentina Manea usb_put_dev(sdev->udev);
49296c27377SValentina Manea
4930c9e8b3cSShuah Khan /* we already have busid_priv, just lock busid_lock */
4940c9e8b3cSShuah Khan spin_lock(&busid_priv->busid_lock);
49596c27377SValentina Manea /* free sdev */
49696c27377SValentina Manea busid_priv->sdev = NULL;
49796c27377SValentina Manea stub_device_free(sdev);
49896c27377SValentina Manea
4997510df3fSShuah Khan (Samsung OSG) if (busid_priv->status == STUB_BUSID_ALLOC)
50096c27377SValentina Manea busid_priv->status = STUB_BUSID_ADDED;
5010c9e8b3cSShuah Khan /* release busid_lock */
5023ea3091fSShuah Khan spin_unlock(&busid_priv->busid_lock);
5033ea3091fSShuah Khan return;
50496c27377SValentina Manea }
50596c27377SValentina Manea
50696c27377SValentina Manea #ifdef CONFIG_PM
50796c27377SValentina Manea
50896c27377SValentina Manea /* These functions need usb_port_suspend and usb_port_resume,
50996c27377SValentina Manea * which reside in drivers/usb/core/usb.h. Skip for now. */
51096c27377SValentina Manea
stub_suspend(struct usb_device * udev,pm_message_t message)51196c27377SValentina Manea static int stub_suspend(struct usb_device *udev, pm_message_t message)
51296c27377SValentina Manea {
51396c27377SValentina Manea dev_dbg(&udev->dev, "stub_suspend\n");
51496c27377SValentina Manea
51596c27377SValentina Manea return 0;
51696c27377SValentina Manea }
51796c27377SValentina Manea
stub_resume(struct usb_device * udev,pm_message_t message)51896c27377SValentina Manea static int stub_resume(struct usb_device *udev, pm_message_t message)
51996c27377SValentina Manea {
52096c27377SValentina Manea dev_dbg(&udev->dev, "stub_resume\n");
52196c27377SValentina Manea
52296c27377SValentina Manea return 0;
52396c27377SValentina Manea }
52496c27377SValentina Manea
52596c27377SValentina Manea #endif /* CONFIG_PM */
52696c27377SValentina Manea
52796c27377SValentina Manea struct usb_device_driver stub_driver = {
52896c27377SValentina Manea .name = "usbip-host",
52996c27377SValentina Manea .probe = stub_probe,
53096c27377SValentina Manea .disconnect = stub_disconnect,
53196c27377SValentina Manea #ifdef CONFIG_PM
53296c27377SValentina Manea .suspend = stub_suspend,
53396c27377SValentina Manea .resume = stub_resume,
53496c27377SValentina Manea #endif
53596c27377SValentina Manea .supports_autosuspend = 0,
536c5501d23SGreg Kroah-Hartman .dev_groups = usbip_groups,
53796c27377SValentina Manea };
538