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

6
#include <linux/device.h>
7
#include <linux/file.h>
8
#include <linux/kthread.h>
9
#include <linux/module.h>
10

11 12 13 14
#include "usbip_common.h"
#include "stub.h"

/*
15 16
 * usbip_status shows the status of usbip-host as long as this driver is bound
 * to the target device.
17
 */
18 19
static ssize_t usbip_status_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
20 21 22 23 24 25 26 27 28
{
	struct stub_device *sdev = dev_get_drvdata(dev);
	int status;

	if (!sdev) {
		dev_err(dev, "sdev is null\n");
		return -ENODEV;
	}

29
	spin_lock_irq(&sdev->ud.lock);
30
	status = sdev->ud.status;
31
	spin_unlock_irq(&sdev->ud.lock);
32 33 34

	return snprintf(buf, PAGE_SIZE, "%d\n", status);
}
35
static DEVICE_ATTR_RO(usbip_status);
36 37 38 39 40 41

/*
 * usbip_sockfd gets a socket descriptor of an established TCP connection that
 * is used to transfer usbip requests by kernel threads. -1 is a magic number
 * by which usbip connection is finished.
 */
42
static ssize_t usbip_sockfd_store(struct device *dev, struct device_attribute *attr,
43 44 45 46 47
			    const char *buf, size_t count)
{
	struct stub_device *sdev = dev_get_drvdata(dev);
	int sockfd = 0;
	struct socket *socket;
48
	int rv;
49 50 51 52 53 54

	if (!sdev) {
		dev_err(dev, "sdev is null\n");
		return -ENODEV;
	}

55 56 57
	rv = sscanf(buf, "%d", &sockfd);
	if (rv != 1)
		return -EINVAL;
58 59

	if (sockfd != -1) {
60
		int err;
61

62 63
		dev_info(dev, "stub up\n");

64
		spin_lock_irq(&sdev->ud.lock);
65 66 67

		if (sdev->ud.status != SDEV_ST_AVAILABLE) {
			dev_err(dev, "not ready\n");
68
			goto err;
69 70
		}

71
		socket = sockfd_lookup(sockfd, &err);
72 73 74
		if (!socket)
			goto err;

75
		sdev->ud.tcp_socket = socket;
76
		sdev->ud.sockfd = sockfd;
77

78
		spin_unlock_irq(&sdev->ud.lock);
79

80 81 82 83
		sdev->ud.tcp_rx = kthread_get_run(stub_rx_loop, &sdev->ud,
						  "stub_rx");
		sdev->ud.tcp_tx = kthread_get_run(stub_tx_loop, &sdev->ud,
						  "stub_tx");
84

85
		spin_lock_irq(&sdev->ud.lock);
86
		sdev->ud.status = SDEV_ST_USED;
87
		spin_unlock_irq(&sdev->ud.lock);
88 89 90 91

	} else {
		dev_info(dev, "stub down\n");

92
		spin_lock_irq(&sdev->ud.lock);
93 94 95
		if (sdev->ud.status != SDEV_ST_USED)
			goto err;

96
		spin_unlock_irq(&sdev->ud.lock);
97 98 99 100 101

		usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
	}

	return count;
102 103 104

err:
	spin_unlock_irq(&sdev->ud.lock);
105
	return -EINVAL;
106
}
107
static DEVICE_ATTR_WO(usbip_sockfd);
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

static int stub_add_files(struct device *dev)
{
	int err = 0;

	err = device_create_file(dev, &dev_attr_usbip_status);
	if (err)
		goto err_status;

	err = device_create_file(dev, &dev_attr_usbip_sockfd);
	if (err)
		goto err_sockfd;

	err = device_create_file(dev, &dev_attr_usbip_debug);
	if (err)
		goto err_debug;

	return 0;

err_debug:
	device_remove_file(dev, &dev_attr_usbip_sockfd);
err_sockfd:
	device_remove_file(dev, &dev_attr_usbip_status);
err_status:
	return err;
}

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

static void stub_shutdown_connection(struct usbip_device *ud)
{
	struct stub_device *sdev = container_of(ud, struct stub_device, ud);

	/*
	 * When removing an exported device, kernel panic sometimes occurred
	 * and then EIP was sk_wait_data of stub_rx thread. Is this because
	 * sk_wait_data returned though stub_rx thread was already finished by
	 * step 1?
	 */
	if (ud->tcp_socket) {
153
		dev_dbg(&sdev->udev->dev, "shutdown sockfd %d\n", ud->sockfd);
154 155 156 157
		kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
	}

	/* 1. stop threads */
158
	if (ud->tcp_rx) {
159
		kthread_stop_put(ud->tcp_rx);
160 161 162
		ud->tcp_rx = NULL;
	}
	if (ud->tcp_tx) {
163
		kthread_stop_put(ud->tcp_tx);
164 165
		ud->tcp_tx = NULL;
	}
166 167

	/*
168 169 170 171
	 * 2. close the socket
	 *
	 * tcp_socket is freed after threads are killed so that usbip_xmit does
	 * not touch NULL socket.
172 173
	 */
	if (ud->tcp_socket) {
174
		sockfd_put(ud->tcp_socket);
175
		ud->tcp_socket = NULL;
176
		ud->sockfd = -1;
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	}

	/* 3. free used data */
	stub_device_cleanup_urbs(sdev);

	/* 4. free stub_unlink */
	{
		unsigned long flags;
		struct stub_unlink *unlink, *tmp;

		spin_lock_irqsave(&sdev->priv_lock, flags);
		list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
			list_del(&unlink->list);
			kfree(unlink);
		}
192 193
		list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
					 list) {
194 195 196 197 198 199 200 201 202 203
			list_del(&unlink->list);
			kfree(unlink);
		}
		spin_unlock_irqrestore(&sdev->priv_lock, flags);
	}
}

static void stub_device_reset(struct usbip_device *ud)
{
	struct stub_device *sdev = container_of(ud, struct stub_device, ud);
204
	struct usb_device *udev = sdev->udev;
205 206
	int ret;

207
	dev_dbg(&udev->dev, "device reset");
208

209
	ret = usb_lock_device_for_reset(udev, NULL);
210 211
	if (ret < 0) {
		dev_err(&udev->dev, "lock for reset\n");
212
		spin_lock_irq(&ud->lock);
213
		ud->status = SDEV_ST_ERROR;
214
		spin_unlock_irq(&ud->lock);
215 216 217 218 219 220 221
		return;
	}

	/* try to reset the device */
	ret = usb_reset_device(udev);
	usb_unlock_device(udev);

222
	spin_lock_irq(&ud->lock);
223 224 225 226 227 228 229
	if (ret) {
		dev_err(&udev->dev, "device reset\n");
		ud->status = SDEV_ST_ERROR;
	} else {
		dev_info(&udev->dev, "device reset\n");
		ud->status = SDEV_ST_AVAILABLE;
	}
230
	spin_unlock_irq(&ud->lock);
231 232 233 234
}

static void stub_device_unusable(struct usbip_device *ud)
{
235
	spin_lock_irq(&ud->lock);
236
	ud->status = SDEV_ST_ERROR;
237
	spin_unlock_irq(&ud->lock);
238 239 240 241
}

/**
 * stub_device_alloc - allocate a new stub_device struct
242
 * @udev: usb_device of a new device
243 244 245
 *
 * Allocates and initializes a new stub_device struct.
 */
246
static struct stub_device *stub_device_alloc(struct usb_device *udev)
247 248
{
	struct stub_device *sdev;
249 250
	int busnum = udev->bus->busnum;
	int devnum = udev->devnum;
251

252
	dev_dbg(&udev->dev, "allocating stub device");
253 254 255

	/* yes, it's a new device */
	sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
256
	if (!sdev)
257 258
		return NULL;

259
	sdev->udev = usb_get_dev(udev);
260 261 262 263 264 265

	/*
	 * devid is defined with devnum when this driver is first allocated.
	 * devnum may change later if a device is reset. However, devid never
	 * changes during a usbip connection.
	 */
266 267 268
	sdev->devid		= (busnum << 16) | devnum;
	sdev->ud.side		= USBIP_STUB;
	sdev->ud.status		= SDEV_ST_AVAILABLE;
269
	spin_lock_init(&sdev->ud.lock);
270
	sdev->ud.tcp_socket	= NULL;
271
	sdev->ud.sockfd		= -1;
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

	INIT_LIST_HEAD(&sdev->priv_init);
	INIT_LIST_HEAD(&sdev->priv_tx);
	INIT_LIST_HEAD(&sdev->priv_free);
	INIT_LIST_HEAD(&sdev->unlink_free);
	INIT_LIST_HEAD(&sdev->unlink_tx);
	spin_lock_init(&sdev->priv_lock);

	init_waitqueue_head(&sdev->tx_waitq);

	sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
	sdev->ud.eh_ops.reset    = stub_device_reset;
	sdev->ud.eh_ops.unusable = stub_device_unusable;

	usbip_start_eh(&sdev->ud);

288
	dev_dbg(&udev->dev, "register new device\n");
289

290 291 292
	return sdev;
}

293
static void stub_device_free(struct stub_device *sdev)
294 295 296 297
{
	kfree(sdev);
}

298
static int stub_probe(struct usb_device *udev)
299 300
{
	struct stub_device *sdev = NULL;
301
	const char *udev_busid = dev_name(&udev->dev);
302
	struct bus_id_priv *busid_priv;
303
	int rc = 0;
304
	char save_status;
305

306
	dev_dbg(&udev->dev, "Enter probe\n");
307

308 309 310 311 312 313 314
	/* Not sure if this is our device. Allocate here to avoid
	 * calling alloc while holding busid_table lock.
	 */
	sdev = stub_device_alloc(udev);
	if (!sdev)
		return -ENOMEM;

315
	/* check we should claim or not by busid_table */
316
	busid_priv = get_busid_priv(udev_busid);
317
	if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
318
	    (busid_priv->status == STUB_BUSID_OTHER)) {
319
		dev_info(&udev->dev,
320 321
			"%s is not in match_busid table... skip!\n",
			udev_busid);
322 323 324 325 326 327

		/*
		 * Return value should be ENODEV or ENOXIO to continue trying
		 * other matched drivers by the driver core.
		 * See driver_probe_device() in driver/base/dd.c
		 */
328
		rc = -ENODEV;
329 330 331 332
		if (!busid_priv)
			goto sdev_free;

		goto call_put_busid_priv;
333 334
	}

335 336 337
	if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
		dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
			 udev_busid);
338
		rc = -ENODEV;
339
		goto call_put_busid_priv;
340 341 342
	}

	if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
343 344 345 346
		dev_dbg(&udev->dev,
			"%s is attached on vhci_hcd... skip!\n",
			udev_busid);

347
		rc = -ENODEV;
348
		goto call_put_busid_priv;
349 350 351
	}


352 353 354
	dev_info(&udev->dev,
		"usbip-host: register new device (bus %u dev %u)\n",
		udev->bus->busnum, udev->devnum);
355

356 357
	busid_priv->shutdown_busid = 0;

358 359
	/* set private data to usb_device */
	dev_set_drvdata(&udev->dev, sdev);
360

361
	busid_priv->sdev = sdev;
362
	busid_priv->udev = udev;
363

364 365 366
	save_status = busid_priv->status;
	busid_priv->status = STUB_BUSID_ALLOC;

367 368 369
	/* release the busid_lock */
	put_busid_priv(busid_priv);

370 371 372 373 374 375
	/*
	 * Claim this hub port.
	 * It doesn't matter what value we pass as owner
	 * (struct dev_state) as long as it is unique.
	 */
	rc = usb_hub_claim_port(udev->parent, udev->portnum,
376
			(struct usb_dev_state *) udev);
377 378
	if (rc) {
		dev_dbg(&udev->dev, "unable to claim port\n");
379
		goto err_port;
380 381
	}

382 383
	rc = stub_add_files(&udev->dev);
	if (rc) {
384
		dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
385
		goto err_files;
386 387
	}

388
	return 0;
389

390 391 392 393 394 395 396
err_files:
	usb_hub_release_port(udev->parent, udev->portnum,
			     (struct usb_dev_state *) udev);
err_port:
	dev_set_drvdata(&udev->dev, NULL);
	usb_put_dev(udev);

397 398
	/* we already have busid_priv, just lock busid_lock */
	spin_lock(&busid_priv->busid_lock);
399
	busid_priv->sdev = NULL;
400
	busid_priv->status = save_status;
401 402 403 404 405
	spin_unlock(&busid_priv->busid_lock);
	/* lock is released - go to free */
	goto sdev_free;

call_put_busid_priv:
406
	/* release the busid_lock */
407
	put_busid_priv(busid_priv);
408

409 410 411
sdev_free:
	stub_device_free(sdev);

412
	return rc;
413 414
}

415 416
static void shutdown_busid(struct bus_id_priv *busid_priv)
{
417
	usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
418

419 420
	/* wait for the stop of the event handler */
	usbip_stop_eh(&busid_priv->sdev->ud);
421 422
}

423 424 425 426
/*
 * called in usb_disconnect() or usb_deregister()
 * but only if actconfig(active configuration) exists
 */
427
static void stub_disconnect(struct usb_device *udev)
428
{
429
	struct stub_device *sdev;
430
	const char *udev_busid = dev_name(&udev->dev);
431
	struct bus_id_priv *busid_priv;
432
	int rc;
433

434
	dev_dbg(&udev->dev, "Enter disconnect\n");
435

436
	busid_priv = get_busid_priv(udev_busid);
437 438 439 440 441
	if (!busid_priv) {
		BUG();
		return;
	}

442
	sdev = dev_get_drvdata(&udev->dev);
443

444 445
	/* get stub_device */
	if (!sdev) {
446
		dev_err(&udev->dev, "could not get device");
447 448 449
		/* release busid_lock */
		put_busid_priv(busid_priv);
		return;
450 451
	}

452
	dev_set_drvdata(&udev->dev, NULL);
453

454 455 456
	/* release busid_lock before call to remove device files */
	put_busid_priv(busid_priv);

457
	/*
458
	 * NOTE: rx/tx threads are invoked for each usb_device.
459
	 */
460
	stub_remove_files(&udev->dev);
461

462 463
	/* release port */
	rc = usb_hub_release_port(udev->parent, udev->portnum,
464
				  (struct usb_dev_state *) udev);
465 466
	if (rc) {
		dev_dbg(&udev->dev, "unable to release port\n");
467
		return;
468 469
	}

470
	/* If usb reset is called from event handler */
N
Nobuo Iwata 已提交
471
	if (usbip_in_eh(current))
472 473 474 475 476 477 478
		return;

	/* we already have busid_priv, just lock busid_lock */
	spin_lock(&busid_priv->busid_lock);
	if (!busid_priv->shutdown_busid)
		busid_priv->shutdown_busid = 1;
	/* release busid_lock */
479
	spin_unlock(&busid_priv->busid_lock);
480

481
	/* shutdown the current connection */
482
	shutdown_busid(busid_priv);
483

484 485
	usb_put_dev(sdev->udev);

486 487
	/* we already have busid_priv, just lock busid_lock */
	spin_lock(&busid_priv->busid_lock);
488
	/* free sdev */
489
	busid_priv->sdev = NULL;
490 491
	stub_device_free(sdev);

492
	if (busid_priv->status == STUB_BUSID_ALLOC)
493
		busid_priv->status = STUB_BUSID_ADDED;
494
	/* release busid_lock */
495 496
	spin_unlock(&busid_priv->busid_lock);
	return;
497
}
498

499
#ifdef CONFIG_PM
500

501 502 503 504
/* These functions need usb_port_suspend and usb_port_resume,
 * which reside in drivers/usb/core/usb.h. Skip for now. */

static int stub_suspend(struct usb_device *udev, pm_message_t message)
505
{
506 507
	dev_dbg(&udev->dev, "stub_suspend\n");

508 509 510
	return 0;
}

511
static int stub_resume(struct usb_device *udev, pm_message_t message)
512
{
513 514
	dev_dbg(&udev->dev, "stub_resume\n");

515 516 517
	return 0;
}

518 519 520
#endif	/* CONFIG_PM */

struct usb_device_driver stub_driver = {
521 522 523
	.name		= "usbip-host",
	.probe		= stub_probe,
	.disconnect	= stub_disconnect,
524 525 526 527 528
#ifdef CONFIG_PM
	.suspend	= stub_suspend,
	.resume		= stub_resume,
#endif
	.supports_autosuspend	=	0,
529
};