stub_main.c 6.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2 3 4 5
/*
 * Copyright (C) 2003-2008 Takahiro Hirofuchi
 */

6
#include <linux/string.h>
7
#include <linux/module.h>
8
#include <linux/device.h>
9 10 11 12 13

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

#define DRIVER_AUTHOR "Takahiro Hirofuchi"
14
#define DRIVER_DESC "USB/IP Host Driver"
15 16 17 18 19 20 21 22

struct kmem_cache *stub_priv_cache;
/*
 * busid_tables defines matching busids that usbip can grab. A user can change
 * dynamically what device is locally used and what device is exported to a
 * remote host.
 */
#define MAX_BUSID 16
23
static struct bus_id_priv busid_table[MAX_BUSID];
24 25
static spinlock_t busid_table_lock;

26 27
static void init_busid_table(void)
{
28 29 30 31
	/*
	 * This also sets the bus_table[i].status to
	 * STUB_BUSID_OTHER, which is 0.
	 */
32
	memset(busid_table, 0, sizeof(busid_table));
33 34 35 36

	spin_lock_init(&busid_table_lock);
}

37 38 39 40 41
/*
 * Find the index of the busid by name.
 * Must be called with busid_table_lock held.
 */
static int get_busid_idx(const char *busid)
42 43
{
	int i;
44
	int idx = -1;
45 46

	for (i = 0; i < MAX_BUSID; i++)
47 48
		if (busid_table[i].name[0])
			if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
49 50
				idx = i;
				break;
51
			}
52
	return idx;
53 54
}

55 56
struct bus_id_priv *get_busid_priv(const char *busid)
{
57 58
	int idx;
	struct bus_id_priv *bid = NULL;
59 60

	spin_lock(&busid_table_lock);
61 62 63
	idx = get_busid_idx(busid);
	if (idx >= 0)
		bid = &(busid_table[idx]);
64 65
	spin_unlock(&busid_table_lock);

66
	return bid;
67 68
}

69 70 71
static int add_match_busid(char *busid)
{
	int i;
72
	int ret = -1;
73 74

	spin_lock(&busid_table_lock);
75 76 77 78 79 80
	/* already registered? */
	if (get_busid_idx(busid) >= 0) {
		ret = 0;
		goto out;
	}

81
	for (i = 0; i < MAX_BUSID; i++)
82
		if (!busid_table[i].name[0]) {
83
			strlcpy(busid_table[i].name, busid, BUSID_SIZE);
84 85 86
			if ((busid_table[i].status != STUB_BUSID_ALLOC) &&
			    (busid_table[i].status != STUB_BUSID_REMOV))
				busid_table[i].status = STUB_BUSID_ADDED;
87 88
			ret = 0;
			break;
89
		}
90 91

out:
92 93
	spin_unlock(&busid_table_lock);

94
	return ret;
95 96
}

97
int del_match_busid(char *busid)
98
{
99 100
	int idx;
	int ret = -1;
101 102

	spin_lock(&busid_table_lock);
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	idx = get_busid_idx(busid);
	if (idx < 0)
		goto out;

	/* found */
	ret = 0;

	if (busid_table[idx].status == STUB_BUSID_OTHER)
		memset(busid_table[idx].name, 0, BUSID_SIZE);

	if ((busid_table[idx].status != STUB_BUSID_OTHER) &&
	    (busid_table[idx].status != STUB_BUSID_ADDED))
		busid_table[idx].status = STUB_BUSID_REMOV;

out:
118 119
	spin_unlock(&busid_table_lock);

120
	return ret;
121
}
122

123
static ssize_t match_busid_show(struct device_driver *drv, char *buf)
124 125
{
	int i;
126
	char *out = buf;
127

128 129 130 131 132 133
	spin_lock(&busid_table_lock);
	for (i = 0; i < MAX_BUSID; i++)
		if (busid_table[i].name[0])
			out += sprintf(out, "%s ", busid_table[i].name);
	spin_unlock(&busid_table_lock);
	out += sprintf(out, "\n");
134

135
	return out - buf;
136
}
137

138
static ssize_t match_busid_store(struct device_driver *dev, const char *buf,
139
				 size_t count)
140 141
{
	int len;
142
	char busid[BUSID_SIZE];
143 144 145 146 147

	if (count < 5)
		return -EINVAL;

	/* busid needs to include \0 termination */
148 149
	len = strlcpy(busid, buf + 4, BUSID_SIZE);
	if (sizeof(busid) <= len)
150 151 152
		return -EINVAL;

	if (!strncmp(buf, "add ", 4)) {
153
		if (add_match_busid(busid) < 0)
154
			return -ENOMEM;
155 156 157 158 159 160 161

		pr_debug("add busid %s\n", busid);
		return count;
	}

	if (!strncmp(buf, "del ", 4)) {
		if (del_match_busid(busid) < 0)
162
			return -ENODEV;
163 164 165

		pr_debug("del busid %s\n", busid);
		return count;
166
	}
167 168

	return -EINVAL;
169
}
170
static DRIVER_ATTR_RW(match_busid);
171

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
static ssize_t rebind_store(struct device_driver *dev, const char *buf,
				 size_t count)
{
	int ret;
	int len;
	struct bus_id_priv *bid;

	/* buf length should be less that BUSID_SIZE */
	len = strnlen(buf, BUSID_SIZE);

	if (!(len < BUSID_SIZE))
		return -EINVAL;

	bid = get_busid_priv(buf);
	if (!bid)
		return -ENODEV;

189 190 191
	/* device_attach() callers should hold parent lock for USB */
	if (bid->udev->dev.parent)
		device_lock(bid->udev->dev.parent);
192
	ret = device_attach(&bid->udev->dev);
193 194
	if (bid->udev->dev.parent)
		device_unlock(bid->udev->dev.parent);
195 196 197 198 199 200 201 202 203 204
	if (ret < 0) {
		dev_err(&bid->udev->dev, "rebind failed\n");
		return ret;
	}

	return count;
}

static DRIVER_ATTR_WO(rebind);

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
{
	struct stub_priv *priv, *tmp;

	list_for_each_entry_safe(priv, tmp, listhead, list) {
		list_del(&priv->list);
		return priv;
	}

	return NULL;
}

static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
{
	unsigned long flags;
	struct stub_priv *priv;

	spin_lock_irqsave(&sdev->priv_lock, flags);

	priv = stub_priv_pop_from_listhead(&sdev->priv_init);
225 226
	if (priv)
		goto done;
227 228

	priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
229 230
	if (priv)
		goto done;
231 232 233

	priv = stub_priv_pop_from_listhead(&sdev->priv_free);

234
done:
235
	spin_unlock_irqrestore(&sdev->priv_lock, flags);
236 237

	return priv;
238 239 240 241 242
}

void stub_device_cleanup_urbs(struct stub_device *sdev)
{
	struct stub_priv *priv;
243
	struct urb *urb;
244

245
	dev_dbg(&sdev->udev->dev, "Stub device cleaning up urbs\n");
246 247

	while ((priv = stub_priv_pop(sdev))) {
248
		urb = priv->urb;
249 250
		dev_dbg(&sdev->udev->dev, "free urb seqnum %lu\n",
			priv->seqnum);
251 252 253 254
		usb_kill_urb(urb);

		kmem_cache_free(stub_priv_cache, priv);

255
		kfree(urb->transfer_buffer);
256 257
		urb->transfer_buffer = NULL;

258
		kfree(urb->setup_packet);
259 260
		urb->setup_packet = NULL;

261 262 263 264
		usb_free_urb(urb);
	}
}

265
static int __init usbip_host_init(void)
266 267 268
{
	int ret;

269
	init_busid_table();
270

271
	stub_priv_cache = KMEM_CACHE(stub_priv, SLAB_HWCACHE_ALIGN);
272
	if (!stub_priv_cache) {
273
		pr_err("kmem_cache_create failed\n");
274 275 276
		return -ENOMEM;
	}

277
	ret = usb_register_device_driver(&stub_driver, THIS_MODULE);
278
	if (ret) {
279
		pr_err("usb_register failed %d\n", ret);
280
		goto err_usb_register;
281 282 283 284
	}

	ret = driver_create_file(&stub_driver.drvwrap.driver,
				 &driver_attr_match_busid);
285
	if (ret) {
286 287
		pr_err("driver_create_file failed\n");
		goto err_create_file;
288 289
	}

290 291 292 293 294 295 296
	ret = driver_create_file(&stub_driver.drvwrap.driver,
				 &driver_attr_rebind);
	if (ret) {
		pr_err("driver_create_file failed\n");
		goto err_create_file;
	}

297
	return ret;
298 299

err_create_file:
300
	usb_deregister_device_driver(&stub_driver);
301
err_usb_register:
302 303 304 305
	kmem_cache_destroy(stub_priv_cache);
	return ret;
}

306
static void __exit usbip_host_exit(void)
307 308 309 310
{
	driver_remove_file(&stub_driver.drvwrap.driver,
			   &driver_attr_match_busid);

311 312 313
	driver_remove_file(&stub_driver.drvwrap.driver,
			   &driver_attr_rebind);

314 315 316 317
	/*
	 * deregister() calls stub_disconnect() for all devices. Device
	 * specific data is cleared in stub_disconnect().
	 */
318
	usb_deregister_device_driver(&stub_driver);
319 320 321 322

	kmem_cache_destroy(stub_priv_cache);
}

323 324
module_init(usbip_host_init);
module_exit(usbip_host_exit);
325 326 327 328

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");