^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2003-2008 Takahiro Hirofuchi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "usbip_common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "stub.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * usbip_status shows the status of usbip-host as long as this driver is bound
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * to the target device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static ssize_t usbip_status_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct stub_device *sdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (!sdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) dev_err(dev, "sdev is null\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) spin_lock_irq(&sdev->ud.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) status = sdev->ud.status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) spin_unlock_irq(&sdev->ud.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return snprintf(buf, PAGE_SIZE, "%d\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static DEVICE_ATTR_RO(usbip_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * usbip_sockfd gets a socket descriptor of an established TCP connection that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * is used to transfer usbip requests by kernel threads. -1 is a magic number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * by which usbip connection is finished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static ssize_t usbip_sockfd_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct stub_device *sdev = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int sockfd = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct socket *socket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct task_struct *tcp_rx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct task_struct *tcp_tx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (!sdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) dev_err(dev, "sdev is null\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) rv = sscanf(buf, "%d", &sockfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (rv != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (sockfd != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) dev_info(dev, "stub up\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) mutex_lock(&sdev->ud.sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) spin_lock_irq(&sdev->ud.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (sdev->ud.status != SDEV_ST_AVAILABLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) dev_err(dev, "not ready\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) socket = sockfd_lookup(sockfd, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (!socket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) dev_err(dev, "failed to lookup sock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (socket->type != SOCK_STREAM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) dev_err(dev, "Expecting SOCK_STREAM - found %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) socket->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) goto sock_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /* unlock and create threads and get tasks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) spin_unlock_irq(&sdev->ud.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) tcp_rx = kthread_create(stub_rx_loop, &sdev->ud, "stub_rx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (IS_ERR(tcp_rx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) sockfd_put(socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) goto unlock_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) tcp_tx = kthread_create(stub_tx_loop, &sdev->ud, "stub_tx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (IS_ERR(tcp_tx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) kthread_stop(tcp_rx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) sockfd_put(socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) goto unlock_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /* get task structs now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) get_task_struct(tcp_rx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) get_task_struct(tcp_tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* lock and update sdev->ud state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) spin_lock_irq(&sdev->ud.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) sdev->ud.tcp_socket = socket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) sdev->ud.sockfd = sockfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) sdev->ud.tcp_rx = tcp_rx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) sdev->ud.tcp_tx = tcp_tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) sdev->ud.status = SDEV_ST_USED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) spin_unlock_irq(&sdev->ud.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) wake_up_process(sdev->ud.tcp_rx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) wake_up_process(sdev->ud.tcp_tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) mutex_unlock(&sdev->ud.sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) dev_info(dev, "stub down\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) spin_lock_irq(&sdev->ud.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (sdev->ud.status != SDEV_ST_USED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) spin_unlock_irq(&sdev->ud.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) mutex_unlock(&sdev->ud.sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) sock_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) sockfd_put(socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) spin_unlock_irq(&sdev->ud.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) unlock_mutex:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) mutex_unlock(&sdev->ud.sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static DEVICE_ATTR_WO(usbip_sockfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static struct attribute *usbip_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) &dev_attr_usbip_status.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) &dev_attr_usbip_sockfd.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) &dev_attr_usbip_debug.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) ATTRIBUTE_GROUPS(usbip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static void stub_shutdown_connection(struct usbip_device *ud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct stub_device *sdev = container_of(ud, struct stub_device, ud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * When removing an exported device, kernel panic sometimes occurred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * and then EIP was sk_wait_data of stub_rx thread. Is this because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * sk_wait_data returned though stub_rx thread was already finished by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * step 1?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (ud->tcp_socket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) dev_dbg(&sdev->udev->dev, "shutdown sockfd %d\n", ud->sockfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /* 1. stop threads */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (ud->tcp_rx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) kthread_stop_put(ud->tcp_rx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ud->tcp_rx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (ud->tcp_tx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) kthread_stop_put(ud->tcp_tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ud->tcp_tx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * 2. close the socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * tcp_socket is freed after threads are killed so that usbip_xmit does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * not touch NULL socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (ud->tcp_socket) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) sockfd_put(ud->tcp_socket);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ud->tcp_socket = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) ud->sockfd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* 3. free used data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) stub_device_cleanup_urbs(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* 4. free stub_unlink */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct stub_unlink *unlink, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) spin_lock_irqsave(&sdev->priv_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) list_del(&unlink->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) kfree(unlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) list_del(&unlink->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) kfree(unlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) spin_unlock_irqrestore(&sdev->priv_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static void stub_device_reset(struct usbip_device *ud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct stub_device *sdev = container_of(ud, struct stub_device, ud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct usb_device *udev = sdev->udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) dev_dbg(&udev->dev, "device reset");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ret = usb_lock_device_for_reset(udev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) dev_err(&udev->dev, "lock for reset\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) spin_lock_irq(&ud->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) ud->status = SDEV_ST_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) spin_unlock_irq(&ud->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) /* try to reset the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) ret = usb_reset_device(udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) usb_unlock_device(udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) spin_lock_irq(&ud->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) dev_err(&udev->dev, "device reset\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ud->status = SDEV_ST_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) dev_info(&udev->dev, "device reset\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ud->status = SDEV_ST_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) spin_unlock_irq(&ud->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static void stub_device_unusable(struct usbip_device *ud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) spin_lock_irq(&ud->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) ud->status = SDEV_ST_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) spin_unlock_irq(&ud->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * stub_device_alloc - allocate a new stub_device struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * @udev: usb_device of a new device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * Allocates and initializes a new stub_device struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static struct stub_device *stub_device_alloc(struct usb_device *udev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct stub_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int busnum = udev->bus->busnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) int devnum = udev->devnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) dev_dbg(&udev->dev, "allocating stub device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /* yes, it's a new device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (!sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) sdev->udev = usb_get_dev(udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * devid is defined with devnum when this driver is first allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * devnum may change later if a device is reset. However, devid never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * changes during a usbip connection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) sdev->devid = (busnum << 16) | devnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) sdev->ud.side = USBIP_STUB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) sdev->ud.status = SDEV_ST_AVAILABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) spin_lock_init(&sdev->ud.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) mutex_init(&sdev->ud.sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) sdev->ud.tcp_socket = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) sdev->ud.sockfd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) INIT_LIST_HEAD(&sdev->priv_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) INIT_LIST_HEAD(&sdev->priv_tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) INIT_LIST_HEAD(&sdev->priv_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) INIT_LIST_HEAD(&sdev->unlink_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) INIT_LIST_HEAD(&sdev->unlink_tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) spin_lock_init(&sdev->priv_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) init_waitqueue_head(&sdev->tx_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) sdev->ud.eh_ops.reset = stub_device_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) sdev->ud.eh_ops.unusable = stub_device_unusable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) usbip_start_eh(&sdev->ud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) dev_dbg(&udev->dev, "register new device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static void stub_device_free(struct stub_device *sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) kfree(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static int stub_probe(struct usb_device *udev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) struct stub_device *sdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) const char *udev_busid = dev_name(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct bus_id_priv *busid_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) char save_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) dev_dbg(&udev->dev, "Enter probe\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) /* Not sure if this is our device. Allocate here to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * calling alloc while holding busid_table lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) sdev = stub_device_alloc(udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (!sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) /* check we should claim or not by busid_table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) busid_priv = get_busid_priv(udev_busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) (busid_priv->status == STUB_BUSID_OTHER)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) dev_info(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) "%s is not in match_busid table... skip!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) udev_busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * Return value should be ENODEV or ENOXIO to continue trying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * other matched drivers by the driver core.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * See driver_probe_device() in driver/base/dd.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (!busid_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) goto sdev_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) goto call_put_busid_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) udev_busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) goto call_put_busid_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) dev_dbg(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) "%s is attached on vhci_hcd... skip!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) udev_busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) goto call_put_busid_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) dev_info(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) "usbip-host: register new device (bus %u dev %u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) udev->bus->busnum, udev->devnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) busid_priv->shutdown_busid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) /* set private data to usb_device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) dev_set_drvdata(&udev->dev, sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) busid_priv->sdev = sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) busid_priv->udev = udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) save_status = busid_priv->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) busid_priv->status = STUB_BUSID_ALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) /* release the busid_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) put_busid_priv(busid_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * Claim this hub port.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) * It doesn't matter what value we pass as owner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * (struct dev_state) as long as it is unique.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) rc = usb_hub_claim_port(udev->parent, udev->portnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) (struct usb_dev_state *) udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) dev_dbg(&udev->dev, "unable to claim port\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) goto err_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) err_port:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) dev_set_drvdata(&udev->dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) usb_put_dev(udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /* we already have busid_priv, just lock busid_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) spin_lock(&busid_priv->busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) busid_priv->sdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) busid_priv->status = save_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) spin_unlock(&busid_priv->busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /* lock is released - go to free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) goto sdev_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) call_put_busid_priv:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) /* release the busid_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) put_busid_priv(busid_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) sdev_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) stub_device_free(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static void shutdown_busid(struct bus_id_priv *busid_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) /* wait for the stop of the event handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) usbip_stop_eh(&busid_priv->sdev->ud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * called in usb_disconnect() or usb_deregister()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * but only if actconfig(active configuration) exists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static void stub_disconnect(struct usb_device *udev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) struct stub_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) const char *udev_busid = dev_name(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) struct bus_id_priv *busid_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) dev_dbg(&udev->dev, "Enter disconnect\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) busid_priv = get_busid_priv(udev_busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (!busid_priv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) sdev = dev_get_drvdata(&udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) /* get stub_device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (!sdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) dev_err(&udev->dev, "could not get device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) /* release busid_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) put_busid_priv(busid_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) dev_set_drvdata(&udev->dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) /* release busid_lock before call to remove device files */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) put_busid_priv(busid_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * NOTE: rx/tx threads are invoked for each usb_device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) /* release port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) rc = usb_hub_release_port(udev->parent, udev->portnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) (struct usb_dev_state *) udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) dev_dbg(&udev->dev, "unable to release port\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) /* If usb reset is called from event handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if (usbip_in_eh(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) /* we already have busid_priv, just lock busid_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) spin_lock(&busid_priv->busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) if (!busid_priv->shutdown_busid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) busid_priv->shutdown_busid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) /* release busid_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) spin_unlock(&busid_priv->busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) /* shutdown the current connection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) shutdown_busid(busid_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) usb_put_dev(sdev->udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) /* we already have busid_priv, just lock busid_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) spin_lock(&busid_priv->busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) /* free sdev */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) busid_priv->sdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) stub_device_free(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (busid_priv->status == STUB_BUSID_ALLOC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) busid_priv->status = STUB_BUSID_ADDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) /* release busid_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) spin_unlock(&busid_priv->busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) /* These functions need usb_port_suspend and usb_port_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) * which reside in drivers/usb/core/usb.h. Skip for now. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) static int stub_suspend(struct usb_device *udev, pm_message_t message)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) dev_dbg(&udev->dev, "stub_suspend\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) static int stub_resume(struct usb_device *udev, pm_message_t message)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) dev_dbg(&udev->dev, "stub_resume\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) #endif /* CONFIG_PM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) struct usb_device_driver stub_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) .name = "usbip-host",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) .probe = stub_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) .disconnect = stub_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) .suspend = stub_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) .resume = stub_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) .supports_autosuspend = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) .dev_groups = usbip_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) };