stub_dev.c 11.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
#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
	ret = usb_lock_device_for_reset(udev, NULL);
223 224
	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
}

/**
 * stub_device_alloc - allocate a new stub_device struct
255
 * @udev: usb_device of a new device
256 257 258
 *
 * 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
	struct bus_id_priv *busid_priv;
315
	int rc;
316

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

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

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

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 339 340 341
		return -ENODEV;
	}

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

346 347 348
		return -ENODEV;
	}

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

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

358 359
	busid_priv->shutdown_busid = 0;

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

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

377 378
	rc = stub_add_files(&udev->dev);
	if (rc) {
379
		dev_err(&udev->dev, "stub_add_files for %s\n", udev_busid);
380
		goto err_files;
381
	}
382
	busid_priv->status = STUB_BUSID_ALLOC;
383 384

	return 0;
385 386 387 388 389 390 391 392 393 394
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);

	busid_priv->sdev = NULL;
	stub_device_free(sdev);
	return rc;
395 396
}

397 398 399 400 401 402
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);

403
		/* wait for the stop of the event handler */
404 405 406 407
		usbip_stop_eh(&busid_priv->sdev->ud);
	}
}

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

419
	dev_dbg(&udev->dev, "Enter\n");
420

421
	busid_priv = get_busid_priv(udev_busid);
422 423 424 425 426
	if (!busid_priv) {
		BUG();
		return;
	}

427
	sdev = dev_get_drvdata(&udev->dev);
428

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

435
	dev_set_drvdata(&udev->dev, NULL);
436 437

	/*
438
	 * NOTE: rx/tx threads are invoked for each usb_device.
439
	 */
440
	stub_remove_files(&udev->dev);
441

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

450
	/* If usb reset is called from event handler */
N
Nobuo Iwata 已提交
451
	if (usbip_in_eh(current))
452
		return;
453

454
	/* shutdown the current connection */
455
	shutdown_busid(busid_priv);
456

457 458
	usb_put_dev(sdev->udev);

459
	/* free sdev */
460
	busid_priv->sdev = NULL;
461 462
	stub_device_free(sdev);

463 464 465 466 467 468
	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);
	}
469
}
470

471
#ifdef CONFIG_PM
472

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

480 481 482
	return 0;
}

483
static int stub_resume(struct usb_device *udev, pm_message_t message)
484
{
485 486
	dev_dbg(&udev->dev, "stub_resume\n");

487 488 489
	return 0;
}

490 491 492
#endif	/* CONFIG_PM */

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