stub_dev.c 12.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2003-2008 Takahiro Hirofuchi
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 * USA.
 */

20
#include <linux/device.h>
21
#include <linux/file.h>
22
#include <linux/kthread.h>
23
#include <linux/module.h>
24

25 26 27 28 29
#include "usbip_common.h"
#include "stub.h"

/*
 * Define device IDs here if you want to explicitly limit exportable devices.
30 31
 * In most cases, wildcard matching will be okay because driver binding can be
 * changed dynamically by a userland program.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 */
static struct usb_device_id stub_table[] = {
#if 0
	/* just an example */
	{ USB_DEVICE(0x05ac, 0x0301) },   /* Mac 1 button mouse */
	{ USB_DEVICE(0x0430, 0x0009) },   /* Plat Home Keyboard */
	{ USB_DEVICE(0x059b, 0x0001) },   /* Iomega USB Zip 100 */
	{ USB_DEVICE(0x04b3, 0x4427) },   /* IBM USB CD-ROM */
	{ USB_DEVICE(0x05a9, 0xa511) },   /* LifeView USB cam */
	{ USB_DEVICE(0x55aa, 0x0201) },   /* Imation card reader */
	{ USB_DEVICE(0x046d, 0x0870) },   /* Qcam Express(QV-30) */
	{ USB_DEVICE(0x04bb, 0x0101) },   /* IO-DATA HD 120GB */
	{ USB_DEVICE(0x04bb, 0x0904) },   /* IO-DATA USB-ET/TX */
	{ USB_DEVICE(0x04bb, 0x0201) },   /* IO-DATA USB-ET/TX */
	{ USB_DEVICE(0x08bb, 0x2702) },   /* ONKYO USB Speaker */
	{ USB_DEVICE(0x046d, 0x08b2) },   /* Logicool Qcam 4000 Pro */
#endif
	/* magic for wild card */
	{ .driver_info = 1 },
	{ 0, }                                     /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, stub_table);

/*
56 57
 * usbip_status shows the status of usbip-host as long as this driver is bound
 * to the target device.
58
 */
59 60
static ssize_t usbip_status_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
61 62 63 64 65 66 67 68 69
{
	struct stub_device *sdev = dev_get_drvdata(dev);
	int status;

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

70
	spin_lock_irq(&sdev->ud.lock);
71
	status = sdev->ud.status;
72
	spin_unlock_irq(&sdev->ud.lock);
73 74 75

	return snprintf(buf, PAGE_SIZE, "%d\n", status);
}
76
static DEVICE_ATTR_RO(usbip_status);
77 78 79 80 81 82 83 84 85 86 87 88

/*
 * 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.
 */
static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
			    const char *buf, size_t count)
{
	struct stub_device *sdev = dev_get_drvdata(dev);
	int sockfd = 0;
	struct socket *socket;
89
	int rv;
90 91 92 93 94 95

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

96 97 98
	rv = sscanf(buf, "%d", &sockfd);
	if (rv != 1)
		return -EINVAL;
99 100

	if (sockfd != -1) {
101
		int err;
102

103 104
		dev_info(dev, "stub up\n");

105
		spin_lock_irq(&sdev->ud.lock);
106 107 108

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

112
		socket = sockfd_lookup(sockfd, &err);
113 114 115
		if (!socket)
			goto err;

116 117
		sdev->ud.tcp_socket = socket;

118
		spin_unlock_irq(&sdev->ud.lock);
119

120 121 122 123
		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");
124

125
		spin_lock_irq(&sdev->ud.lock);
126
		sdev->ud.status = SDEV_ST_USED;
127
		spin_unlock_irq(&sdev->ud.lock);
128 129 130 131

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

132
		spin_lock_irq(&sdev->ud.lock);
133 134 135
		if (sdev->ud.status != SDEV_ST_USED)
			goto err;

136
		spin_unlock_irq(&sdev->ud.lock);
137 138 139 140 141

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

	return count;
142 143 144

err:
	spin_unlock_irq(&sdev->ud.lock);
145
	return -EINVAL;
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
}
static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);

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) {
193 194
		dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n",
			ud->tcp_socket);
195 196 197 198
		kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
	}

	/* 1. stop threads */
199
	if (ud->tcp_rx) {
200
		kthread_stop_put(ud->tcp_rx);
201 202 203
		ud->tcp_rx = NULL;
	}
	if (ud->tcp_tx) {
204
		kthread_stop_put(ud->tcp_tx);
205 206
		ud->tcp_tx = NULL;
	}
207 208

	/*
209 210 211 212
	 * 2. close the socket
	 *
	 * tcp_socket is freed after threads are killed so that usbip_xmit does
	 * not touch NULL socket.
213 214
	 */
	if (ud->tcp_socket) {
215
		sockfd_put(ud->tcp_socket);
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
		ud->tcp_socket = NULL;
	}

	/* 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);
		}
232 233
		list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
					 list) {
234 235 236 237 238 239 240 241 242 243
			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);
244
	struct usb_device *udev = sdev->udev;
245 246
	int ret;

247
	dev_dbg(&udev->dev, "device reset");
248

249 250 251
	ret = usb_lock_device_for_reset(udev, sdev->interface);
	if (ret < 0) {
		dev_err(&udev->dev, "lock for reset\n");
252
		spin_lock_irq(&ud->lock);
253
		ud->status = SDEV_ST_ERROR;
254
		spin_unlock_irq(&ud->lock);
255 256 257 258 259 260 261
		return;
	}

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

262
	spin_lock_irq(&ud->lock);
263 264 265 266 267 268 269
	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;
	}
270
	spin_unlock_irq(&ud->lock);
271 272 273 274
}

static void stub_device_unusable(struct usbip_device *ud)
{
275
	spin_lock_irq(&ud->lock);
276
	ud->status = SDEV_ST_ERROR;
277
	spin_unlock_irq(&ud->lock);
278 279 280 281 282 283 284 285
}

/**
 * stub_device_alloc - allocate a new stub_device struct
 * @interface: usb_interface of a new device
 *
 * Allocates and initializes a new stub_device struct.
 */
286
static struct stub_device *stub_device_alloc(struct usb_device *udev)
287 288
{
	struct stub_device *sdev;
289 290
	int busnum = udev->bus->busnum;
	int devnum = udev->devnum;
291

292
	dev_dbg(&udev->dev, "allocating stub device");
293 294 295

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

299
	sdev->udev = usb_get_dev(udev);
300 301 302 303 304 305

	/*
	 * 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.
	 */
306 307 308
	sdev->devid		= (busnum << 16) | devnum;
	sdev->ud.side		= USBIP_STUB;
	sdev->ud.status		= SDEV_ST_AVAILABLE;
309
	spin_lock_init(&sdev->ud.lock);
310
	sdev->ud.tcp_socket	= NULL;
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

	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);

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

329 330 331
	return sdev;
}

332
static void stub_device_free(struct stub_device *sdev)
333 334 335 336
{
	kfree(sdev);
}

337
static int stub_probe(struct usb_device *udev)
338 339
{
	struct stub_device *sdev = NULL;
340
	const char *udev_busid = dev_name(&udev->dev);
341
	int err = 0;
342
	struct bus_id_priv *busid_priv;
343
	int rc;
344

345
	dev_dbg(&udev->dev, "Enter\n");
346 347

	/* check we should claim or not by busid_table */
348
	busid_priv = get_busid_priv(udev_busid);
349
	if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
350
	    (busid_priv->status == STUB_BUSID_OTHER)) {
351
		dev_info(&udev->dev,
352 353
			"%s is not in match_busid table... skip!\n",
			udev_busid);
354 355 356 357 358 359 360 361 362

		/*
		 * 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
		 */
		return -ENODEV;
	}

363 364 365
	if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
		dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
			 udev_busid);
366 367 368 369
		return -ENODEV;
	}

	if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
370 371 372 373
		dev_dbg(&udev->dev,
			"%s is attached on vhci_hcd... skip!\n",
			udev_busid);

374 375 376
		return -ENODEV;
	}

377
	/* ok, this is my device */
378
	sdev = stub_device_alloc(udev);
379 380 381
	if (!sdev)
		return -ENOMEM;

382 383 384
	dev_info(&udev->dev,
		"usbip-host: register new device (bus %u dev %u)\n",
		udev->bus->busnum, udev->devnum);
385

386 387
	busid_priv->shutdown_busid = 0;

388 389
	/* set private data to usb_device */
	dev_set_drvdata(&udev->dev, sdev);
390
	busid_priv->sdev = sdev;
391
	busid_priv->udev = udev;
392

393 394 395 396 397 398
	/*
	 * 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,
399
			(struct usb_dev_state *) udev);
400 401 402 403 404
	if (rc) {
		dev_dbg(&udev->dev, "unable to claim port\n");
		return rc;
	}

405
	err = stub_add_files(&udev->dev);
406
	if (err) {
407 408
		dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
		dev_set_drvdata(&udev->dev, NULL);
409 410
		usb_put_dev(udev);
		kthread_stop_put(sdev->ud.eh);
411

412
		busid_priv->sdev = NULL;
413
		stub_device_free(sdev);
414 415
		return err;
	}
416
	busid_priv->status = STUB_BUSID_ALLOC;
417 418 419 420

	return 0;
}

421 422 423 424 425 426
static void shutdown_busid(struct bus_id_priv *busid_priv)
{
	if (busid_priv->sdev && !busid_priv->shutdown_busid) {
		busid_priv->shutdown_busid = 1;
		usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);

427
		/* wait for the stop of the event handler */
428 429 430 431
		usbip_stop_eh(&busid_priv->sdev->ud);
	}
}

432 433 434 435
/*
 * called in usb_disconnect() or usb_deregister()
 * but only if actconfig(active configuration) exists
 */
436
static void stub_disconnect(struct usb_device *udev)
437
{
438
	struct stub_device *sdev;
439
	const char *udev_busid = dev_name(&udev->dev);
440
	struct bus_id_priv *busid_priv;
441
	int rc;
442

443
	dev_dbg(&udev->dev, "Enter\n");
444

445
	busid_priv = get_busid_priv(udev_busid);
446 447 448 449 450
	if (!busid_priv) {
		BUG();
		return;
	}

451
	sdev = dev_get_drvdata(&udev->dev);
452

453 454
	/* get stub_device */
	if (!sdev) {
455
		dev_err(&udev->dev, "could not get device");
456 457 458
		return;
	}

459
	dev_set_drvdata(&udev->dev, NULL);
460 461

	/*
462
	 * NOTE: rx/tx threads are invoked for each usb_device.
463
	 */
464
	stub_remove_files(&udev->dev);
465

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

474
	/* If usb reset is called from event handler */
475
	if (busid_priv->sdev->ud.eh == current)
476
		return;
477

478
	/* shutdown the current connection */
479
	shutdown_busid(busid_priv);
480

481 482
	usb_put_dev(sdev->udev);

483
	/* free sdev */
484
	busid_priv->sdev = NULL;
485 486
	stub_device_free(sdev);

487 488 489 490 491 492
	if (busid_priv->status == STUB_BUSID_ALLOC) {
		busid_priv->status = STUB_BUSID_ADDED;
	} else {
		busid_priv->status = STUB_BUSID_OTHER;
		del_match_busid((char *)udev_busid);
	}
493
}
494

495
#ifdef CONFIG_PM
496

497 498 499 500
/* 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)
501
{
502 503
	dev_dbg(&udev->dev, "stub_suspend\n");

504 505 506
	return 0;
}

507
static int stub_resume(struct usb_device *udev, pm_message_t message)
508
{
509 510
	dev_dbg(&udev->dev, "stub_resume\n");

511 512 513
	return 0;
}

514 515 516
#endif	/* CONFIG_PM */

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