^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/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/scatterlist.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) #define DRIVER_AUTHOR "Takahiro Hirofuchi"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define DRIVER_DESC "USB/IP Host Driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct kmem_cache *stub_priv_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * busid_tables defines matching busids that usbip can grab. A user can change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * dynamically what device is locally used and what device is exported to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * remote host.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define MAX_BUSID 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static struct bus_id_priv busid_table[MAX_BUSID];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static spinlock_t busid_table_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static void init_busid_table(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * This also sets the bus_table[i].status to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * STUB_BUSID_OTHER, which is 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) memset(busid_table, 0, sizeof(busid_table));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) spin_lock_init(&busid_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) for (i = 0; i < MAX_BUSID; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) spin_lock_init(&busid_table[i].busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * Find the index of the busid by name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Must be called with busid_table_lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int get_busid_idx(const char *busid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int idx = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) for (i = 0; i < MAX_BUSID; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) spin_lock(&busid_table[i].busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (busid_table[i].name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) idx = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) spin_unlock(&busid_table[i].busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) spin_unlock(&busid_table[i].busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* Returns holding busid_lock. Should call put_busid_priv() to unlock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct bus_id_priv *get_busid_priv(const char *busid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct bus_id_priv *bid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) spin_lock(&busid_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) idx = get_busid_idx(busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (idx >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) bid = &(busid_table[idx]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /* get busid_lock before returning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) spin_lock(&bid->busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) spin_unlock(&busid_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return bid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) void put_busid_priv(struct bus_id_priv *bid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (bid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) spin_unlock(&bid->busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static int add_match_busid(char *busid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) spin_lock(&busid_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* already registered? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (get_busid_idx(busid) >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) for (i = 0; i < MAX_BUSID; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) spin_lock(&busid_table[i].busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (!busid_table[i].name[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) strlcpy(busid_table[i].name, busid, BUSID_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if ((busid_table[i].status != STUB_BUSID_ALLOC) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) (busid_table[i].status != STUB_BUSID_REMOV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) busid_table[i].status = STUB_BUSID_ADDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) spin_unlock(&busid_table[i].busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) spin_unlock(&busid_table[i].busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) spin_unlock(&busid_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int del_match_busid(char *busid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) spin_lock(&busid_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) idx = get_busid_idx(busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (idx < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) spin_lock(&busid_table[idx].busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (busid_table[idx].status == STUB_BUSID_OTHER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) memset(busid_table[idx].name, 0, BUSID_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if ((busid_table[idx].status != STUB_BUSID_OTHER) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) (busid_table[idx].status != STUB_BUSID_ADDED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) busid_table[idx].status = STUB_BUSID_REMOV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) spin_unlock(&busid_table[idx].busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) spin_unlock(&busid_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static ssize_t match_busid_show(struct device_driver *drv, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) char *out = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) spin_lock(&busid_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) for (i = 0; i < MAX_BUSID; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) spin_lock(&busid_table[i].busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (busid_table[i].name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) out += sprintf(out, "%s ", busid_table[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) spin_unlock(&busid_table[i].busid_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) spin_unlock(&busid_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) out += sprintf(out, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return out - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static ssize_t match_busid_store(struct device_driver *dev, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) char busid[BUSID_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (count < 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /* busid needs to include \0 termination */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) len = strlcpy(busid, buf + 4, BUSID_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (sizeof(busid) <= len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (!strncmp(buf, "add ", 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (add_match_busid(busid) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) pr_debug("add busid %s\n", busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (!strncmp(buf, "del ", 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (del_match_busid(busid) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) pr_debug("del busid %s\n", busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static DRIVER_ATTR_RW(match_busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static int do_rebind(char *busid, struct bus_id_priv *busid_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /* device_attach() callers should hold parent lock for USB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (busid_priv->udev->dev.parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) device_lock(busid_priv->udev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) ret = device_attach(&busid_priv->udev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (busid_priv->udev->dev.parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) device_unlock(busid_priv->udev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) dev_err(&busid_priv->udev->dev, "rebind failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static void stub_device_rebind(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) #if IS_MODULE(CONFIG_USBIP_HOST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct bus_id_priv *busid_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /* update status to STUB_BUSID_OTHER so probe ignores the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) spin_lock(&busid_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) for (i = 0; i < MAX_BUSID; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (busid_table[i].name[0] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) busid_table[i].shutdown_busid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) busid_priv = &(busid_table[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) busid_priv->status = STUB_BUSID_OTHER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) spin_unlock(&busid_table_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* now run rebind - no need to hold locks. driver files are removed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) for (i = 0; i < MAX_BUSID; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (busid_table[i].name[0] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) busid_table[i].shutdown_busid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) busid_priv = &(busid_table[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) do_rebind(busid_table[i].name, busid_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static ssize_t rebind_store(struct device_driver *dev, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct bus_id_priv *bid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /* buf length should be less that BUSID_SIZE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) len = strnlen(buf, BUSID_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (!(len < BUSID_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) bid = get_busid_priv(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (!bid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /* mark the device for deletion so probe ignores it during rescan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) bid->status = STUB_BUSID_OTHER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) /* release the busid lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) put_busid_priv(bid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) ret = do_rebind((char *) buf, bid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) /* delete device from busid_table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) del_match_busid((char *) buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static DRIVER_ATTR_WO(rebind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct stub_priv *priv, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) list_for_each_entry_safe(priv, tmp, listhead, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) list_del_init(&priv->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) void stub_free_priv_and_urb(struct stub_priv *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) for (i = 0; i < priv->num_urbs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) urb = priv->urbs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (!urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) kfree(urb->setup_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) urb->setup_packet = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (urb->transfer_buffer && !priv->sgl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) kfree(urb->transfer_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) urb->transfer_buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (urb->num_sgs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) sgl_free(urb->sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) urb->sg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) urb->num_sgs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (!list_empty(&priv->list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) list_del(&priv->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (priv->sgl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) sgl_free(priv->sgl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) kfree(priv->urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) kmem_cache_free(stub_priv_cache, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct stub_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) spin_lock_irqsave(&sdev->priv_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) priv = stub_priv_pop_from_listhead(&sdev->priv_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) priv = stub_priv_pop_from_listhead(&sdev->priv_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) spin_unlock_irqrestore(&sdev->priv_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) void stub_device_cleanup_urbs(struct stub_device *sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) struct stub_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) dev_dbg(&sdev->udev->dev, "Stub device cleaning up urbs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) while ((priv = stub_priv_pop(sdev))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) for (i = 0; i < priv->num_urbs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) usb_kill_urb(priv->urbs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) stub_free_priv_and_urb(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static int __init usbip_host_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) init_busid_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) stub_priv_cache = KMEM_CACHE(stub_priv, SLAB_HWCACHE_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (!stub_priv_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) pr_err("kmem_cache_create failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) ret = usb_register_device_driver(&stub_driver, THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) pr_err("usb_register failed %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) goto err_usb_register;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) ret = driver_create_file(&stub_driver.drvwrap.driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) &driver_attr_match_busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) pr_err("driver_create_file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) goto err_create_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) ret = driver_create_file(&stub_driver.drvwrap.driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) &driver_attr_rebind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) pr_err("driver_create_file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) goto err_create_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) err_create_file:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) usb_deregister_device_driver(&stub_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) err_usb_register:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) kmem_cache_destroy(stub_priv_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static void __exit usbip_host_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) driver_remove_file(&stub_driver.drvwrap.driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) &driver_attr_match_busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) driver_remove_file(&stub_driver.drvwrap.driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) &driver_attr_rebind);
^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) * deregister() calls stub_disconnect() for all devices. Device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * specific data is cleared in stub_disconnect().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) usb_deregister_device_driver(&stub_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /* initiate scan to attach devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) stub_device_rebind();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) kmem_cache_destroy(stub_priv_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) module_init(usbip_host_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) module_exit(usbip_host_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) MODULE_AUTHOR(DRIVER_AUTHOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) MODULE_LICENSE("GPL");