stub_dev.c 11.4 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
#include "usbip_common.h"
#include "stub.h"

/*
29 30
 * usbip_status shows the status of usbip-host as long as this driver is bound
 * to the target device.
31
 */
32 33
static ssize_t usbip_status_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
34 35 36 37 38 39 40 41 42
{
	struct stub_device *sdev = dev_get_drvdata(dev);
	int status;

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

43
	spin_lock_irq(&sdev->ud.lock);
44
	status = sdev->ud.status;
45
	spin_unlock_irq(&sdev->ud.lock);
46 47 48

	return snprintf(buf, PAGE_SIZE, "%d\n", status);
}
49
static DEVICE_ATTR_RO(usbip_status);
50 51 52 53 54 55 56 57 58 59 60 61

/*
 * 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;
62
	int rv;
63 64 65 66 67 68

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

69 70 71
	rv = sscanf(buf, "%d", &sockfd);
	if (rv != 1)
		return -EINVAL;
72 73

	if (sockfd != -1) {
74
		int err;
75

76 77
		dev_info(dev, "stub up\n");

78
		spin_lock_irq(&sdev->ud.lock);
79 80 81

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

85
		socket = sockfd_lookup(sockfd, &err);
86 87 88
		if (!socket)
			goto err;

89 90
		sdev->ud.tcp_socket = socket;

91
		spin_unlock_irq(&sdev->ud.lock);
92

93 94 95 96
		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");
97

98
		spin_lock_irq(&sdev->ud.lock);
99
		sdev->ud.status = SDEV_ST_USED;
100
		spin_unlock_irq(&sdev->ud.lock);
101 102 103 104

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

105
		spin_lock_irq(&sdev->ud.lock);
106 107 108
		if (sdev->ud.status != SDEV_ST_USED)
			goto err;

109
		spin_unlock_irq(&sdev->ud.lock);
110 111 112 113 114

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

	return count;
115 116 117

err:
	spin_unlock_irq(&sdev->ud.lock);
118
	return -EINVAL;
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 153 154 155 156 157 158 159 160 161 162 163 164 165
}
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) {
166 167
		dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n",
			ud->tcp_socket);
168 169 170 171
		kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
	}

	/* 1. stop threads */
172
	if (ud->tcp_rx) {
173
		kthread_stop_put(ud->tcp_rx);
174 175 176
		ud->tcp_rx = NULL;
	}
	if (ud->tcp_tx) {
177
		kthread_stop_put(ud->tcp_tx);
178 179
		ud->tcp_tx = NULL;
	}
180 181

	/*
182 183 184 185
	 * 2. close the socket
	 *
	 * tcp_socket is freed after threads are killed so that usbip_xmit does
	 * not touch NULL socket.
186 187
	 */
	if (ud->tcp_socket) {
188
		sockfd_put(ud->tcp_socket);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
		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);
		}
205 206
		list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
					 list) {
207 208 209 210 211 212 213 214 215 216
			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);
217
	struct usb_device *udev = sdev->udev;
218 219
	int ret;

220
	dev_dbg(&udev->dev, "device reset");
221

222 223 224
	ret = usb_lock_device_for_reset(udev, sdev->interface);
	if (ret < 0) {
		dev_err(&udev->dev, "lock for reset\n");
225
		spin_lock_irq(&ud->lock);
226
		ud->status = SDEV_ST_ERROR;
227
		spin_unlock_irq(&ud->lock);
228 229 230 231 232 233 234
		return;
	}

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

235
	spin_lock_irq(&ud->lock);
236 237 238 239 240 241 242
	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;
	}
243
	spin_unlock_irq(&ud->lock);
244 245 246 247
}

static void stub_device_unusable(struct usbip_device *ud)
{
248
	spin_lock_irq(&ud->lock);
249
	ud->status = SDEV_ST_ERROR;
250
	spin_unlock_irq(&ud->lock);
251 252 253 254 255 256 257 258
}

/**
 * stub_device_alloc - allocate a new stub_device struct
 * @interface: usb_interface of a new device
 *
 * Allocates and initializes a new stub_device struct.
 */
259
static struct stub_device *stub_device_alloc(struct usb_device *udev)
260 261
{
	struct stub_device *sdev;
262 263
	int busnum = udev->bus->busnum;
	int devnum = udev->devnum;
264

265
	dev_dbg(&udev->dev, "allocating stub device");
266 267 268

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

272
	sdev->udev = usb_get_dev(udev);
273 274 275 276 277 278

	/*
	 * 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.
	 */
279 280 281
	sdev->devid		= (busnum << 16) | devnum;
	sdev->ud.side		= USBIP_STUB;
	sdev->ud.status		= SDEV_ST_AVAILABLE;
282
	spin_lock_init(&sdev->ud.lock);
283
	sdev->ud.tcp_socket	= NULL;
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

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

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

302 303 304
	return sdev;
}

305
static void stub_device_free(struct stub_device *sdev)
306 307 308 309
{
	kfree(sdev);
}

310
static int stub_probe(struct usb_device *udev)
311 312
{
	struct stub_device *sdev = NULL;
313
	const char *udev_busid = dev_name(&udev->dev);
314
	int err = 0;
315
	struct bus_id_priv *busid_priv;
316
	int rc;
317

318
	dev_dbg(&udev->dev, "Enter\n");
319 320

	/* check we should claim or not by busid_table */
321
	busid_priv = get_busid_priv(udev_busid);
322
	if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
323
	    (busid_priv->status == STUB_BUSID_OTHER)) {
324
		dev_info(&udev->dev,
325 326
			"%s is not in match_busid table... skip!\n",
			udev_busid);
327 328 329 330 331 332 333 334 335

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

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

	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 348 349
		return -ENODEV;
	}

350
	/* ok, this is my device */
351
	sdev = stub_device_alloc(udev);
352 353 354
	if (!sdev)
		return -ENOMEM;

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

359 360
	busid_priv->shutdown_busid = 0;

361 362
	/* set private data to usb_device */
	dev_set_drvdata(&udev->dev, sdev);
363
	busid_priv->sdev = sdev;
364
	busid_priv->udev = udev;
365

366 367 368 369 370 371
	/*
	 * 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,
372
			(struct usb_dev_state *) udev);
373 374 375 376 377
	if (rc) {
		dev_dbg(&udev->dev, "unable to claim port\n");
		return rc;
	}

378
	err = stub_add_files(&udev->dev);
379
	if (err) {
380 381
		dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
		dev_set_drvdata(&udev->dev, NULL);
382 383
		usb_put_dev(udev);
		kthread_stop_put(sdev->ud.eh);
384

385
		busid_priv->sdev = NULL;
386
		stub_device_free(sdev);
387 388
		return err;
	}
389
	busid_priv->status = STUB_BUSID_ALLOC;
390 391 392 393

	return 0;
}

394 395 396 397 398 399
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);

400
		/* wait for the stop of the event handler */
401 402 403 404
		usbip_stop_eh(&busid_priv->sdev->ud);
	}
}

405 406 407 408
/*
 * called in usb_disconnect() or usb_deregister()
 * but only if actconfig(active configuration) exists
 */
409
static void stub_disconnect(struct usb_device *udev)
410
{
411
	struct stub_device *sdev;
412
	const char *udev_busid = dev_name(&udev->dev);
413
	struct bus_id_priv *busid_priv;
414
	int rc;
415

416
	dev_dbg(&udev->dev, "Enter\n");
417

418
	busid_priv = get_busid_priv(udev_busid);
419 420 421 422 423
	if (!busid_priv) {
		BUG();
		return;
	}

424
	sdev = dev_get_drvdata(&udev->dev);
425

426 427
	/* get stub_device */
	if (!sdev) {
428
		dev_err(&udev->dev, "could not get device");
429 430 431
		return;
	}

432
	dev_set_drvdata(&udev->dev, NULL);
433 434

	/*
435
	 * NOTE: rx/tx threads are invoked for each usb_device.
436
	 */
437
	stub_remove_files(&udev->dev);
438

439 440
	/* release port */
	rc = usb_hub_release_port(udev->parent, udev->portnum,
441
				  (struct usb_dev_state *) udev);
442 443 444 445 446
	if (rc) {
		dev_dbg(&udev->dev, "unable to release port\n");
		return;
	}

447
	/* If usb reset is called from event handler */
448
	if (busid_priv->sdev->ud.eh == current)
449
		return;
450

451
	/* shutdown the current connection */
452
	shutdown_busid(busid_priv);
453

454 455
	usb_put_dev(sdev->udev);

456
	/* free sdev */
457
	busid_priv->sdev = NULL;
458 459
	stub_device_free(sdev);

460 461 462 463 464 465
	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);
	}
466
}
467

468
#ifdef CONFIG_PM
469

470 471 472 473
/* 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)
474
{
475 476
	dev_dbg(&udev->dev, "stub_suspend\n");

477 478 479
	return 0;
}

480
static int stub_resume(struct usb_device *udev, pm_message_t message)
481
{
482 483
	dev_dbg(&udev->dev, "stub_resume\n");

484 485 486
	return 0;
}

487 488 489
#endif	/* CONFIG_PM */

struct usb_device_driver stub_driver = {
490 491 492
	.name		= "usbip-host",
	.probe		= stub_probe,
	.disconnect	= stub_disconnect,
493 494 495 496 497
#ifdef CONFIG_PM
	.suspend	= stub_suspend,
	.resume		= stub_resume,
#endif
	.supports_autosuspend	=	0,
498
};