inode.c 52.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * inode.c -- user mode filesystem api for usb gadget controllers
 *
 * Copyright (C) 2003-2004 David Brownell
 * Copyright (C) 2003 Agilent Technologies
 *
 * This program 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.
 */


14
/* #define VERBOSE_DEBUG */
L
Linus Torvalds 已提交
15 16 17 18 19 20 21 22 23

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
#include <linux/uts.h>
#include <linux/wait.h>
#include <linux/compiler.h>
#include <asm/uaccess.h>
24
#include <linux/sched.h>
L
Linus Torvalds 已提交
25
#include <linux/slab.h>
26
#include <linux/poll.h>
L
Linus Torvalds 已提交
27 28 29 30

#include <linux/device.h>
#include <linux/moduleparam.h>

31
#include <linux/usb/gadgetfs.h>
32
#include <linux/usb/gadget.h>
L
Linus Torvalds 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52


/*
 * The gadgetfs API maps each endpoint to a file descriptor so that you
 * can use standard synchronous read/write calls for I/O.  There's some
 * O_NONBLOCK and O_ASYNC/FASYNC style i/o support.  Example usermode
 * drivers show how this works in practice.  You can also use AIO to
 * eliminate I/O gaps between requests, to help when streaming data.
 *
 * Key parts that must be USB-specific are protocols defining how the
 * read/write operations relate to the hardware state machines.  There
 * are two types of files.  One type is for the device, implementing ep0.
 * The other type is for each IN or OUT endpoint.  In both cases, the
 * user mode driver must configure the hardware before using it.
 *
 * - First, dev_config() is called when /dev/gadget/$CHIP is configured
 *   (by writing configuration and device descriptors).  Afterwards it
 *   may serve as a source of device events, used to handle all control
 *   requests other than basic enumeration.
 *
53 54 55 56 57
 * - Then, after a SET_CONFIGURATION control request, ep_config() is
 *   called when each /dev/gadget/ep* file is configured (by writing
 *   endpoint descriptors).  Afterwards these files are used to write()
 *   IN data or to read() OUT data.  To halt the endpoint, a "wrong
 *   direction" request is issued (like reading an IN endpoint).
L
Linus Torvalds 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
 *
 * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe
 * not possible on all hardware.  For example, precise fault handling with
 * respect to data left in endpoint fifos after aborted operations; or
 * selective clearing of endpoint halts, to implement SET_INTERFACE.
 */

#define	DRIVER_DESC	"USB Gadget filesystem"
#define	DRIVER_VERSION	"24 Aug 2004"

static const char driver_desc [] = DRIVER_DESC;
static const char shortname [] = "gadgetfs";

MODULE_DESCRIPTION (DRIVER_DESC);
MODULE_AUTHOR ("David Brownell");
MODULE_LICENSE ("GPL");


/*----------------------------------------------------------------------*/

#define GADGETFS_MAGIC		0xaee71ee7
#define DMA_ADDR_INVALID	(~(dma_addr_t)0)

/* /dev/gadget/$CHIP represents ep0 and the whole device */
enum ep0_state {
	/* DISBLED is the initial state.
	 */
	STATE_DEV_DISABLED = 0,

	/* Only one open() of /dev/gadget/$CHIP; only one file tracks
	 * ep0/device i/o modes and binding to the controller.  Driver
	 * must always write descriptors to initialize the device, then
	 * the device becomes UNCONNECTED until enumeration.
	 */
D
David Brownell 已提交
92
	STATE_DEV_OPENED,
L
Linus Torvalds 已提交
93 94 95 96 97 98

	/* From then on, ep0 fd is in either of two basic modes:
	 * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
	 * - SETUP: read/write will transfer control data and succeed;
	 *   or if "wrong direction", performs protocol stall
	 */
D
David Brownell 已提交
99 100 101
	STATE_DEV_UNCONNECTED,
	STATE_DEV_CONNECTED,
	STATE_DEV_SETUP,
L
Linus Torvalds 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114

	/* UNBOUND means the driver closed ep0, so the device won't be
	 * accessible again (DEV_DISABLED) until all fds are closed.
	 */
	STATE_DEV_UNBOUND,
};

/* enough for the whole queue: most events invalidate others */
#define	N_EVENT			5

struct dev_data {
	spinlock_t			lock;
	atomic_t			count;
D
David Brownell 已提交
115
	enum ep0_state			state;		/* P: lock */
L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129
	struct usb_gadgetfs_event	event [N_EVENT];
	unsigned			ev_next;
	struct fasync_struct		*fasync;
	u8				current_config;

	/* drivers reading ep0 MUST handle control requests (SETUP)
	 * reported that way; else the host will time out.
	 */
	unsigned			usermode_setup : 1,
					setup_in : 1,
					setup_can_stall : 1,
					setup_out_ready : 1,
					setup_out_error : 1,
					setup_abort : 1;
130
	unsigned			setup_wLength;
L
Linus Torvalds 已提交
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

	/* the rest is basically write-once */
	struct usb_config_descriptor	*config, *hs_config;
	struct usb_device_descriptor	*dev;
	struct usb_request		*req;
	struct usb_gadget		*gadget;
	struct list_head		epfiles;
	void				*buf;
	wait_queue_head_t		wait;
	struct super_block		*sb;
	struct dentry			*dentry;

	/* except this scratch i/o buffer for ep0 */
	u8				rbuf [256];
};

static inline void get_dev (struct dev_data *data)
{
	atomic_inc (&data->count);
}

static void put_dev (struct dev_data *data)
{
	if (likely (!atomic_dec_and_test (&data->count)))
		return;
	/* needs no more cleanup */
	BUG_ON (waitqueue_active (&data->wait));
	kfree (data);
}

static struct dev_data *dev_new (void)
{
	struct dev_data		*dev;

165
	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	if (!dev)
		return NULL;
	dev->state = STATE_DEV_DISABLED;
	atomic_set (&dev->count, 1);
	spin_lock_init (&dev->lock);
	INIT_LIST_HEAD (&dev->epfiles);
	init_waitqueue_head (&dev->wait);
	return dev;
}

/*----------------------------------------------------------------------*/

/* other /dev/gadget/$ENDPOINT files represent endpoints */
enum ep_state {
	STATE_EP_DISABLED = 0,
	STATE_EP_READY,
	STATE_EP_ENABLED,
	STATE_EP_UNBOUND,
};

struct ep_data {
187
	struct mutex			lock;
L
Linus Torvalds 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	enum ep_state			state;
	atomic_t			count;
	struct dev_data			*dev;
	/* must hold dev->lock before accessing ep or req */
	struct usb_ep			*ep;
	struct usb_request		*req;
	ssize_t				status;
	char				name [16];
	struct usb_endpoint_descriptor	desc, hs_desc;
	struct list_head		epfiles;
	wait_queue_head_t		wait;
	struct dentry			*dentry;
	struct inode			*inode;
};

static inline void get_ep (struct ep_data *data)
{
	atomic_inc (&data->count);
}

static void put_ep (struct ep_data *data)
{
	if (likely (!atomic_dec_and_test (&data->count)))
		return;
	put_dev (data->dev);
	/* needs no more cleanup */
	BUG_ON (!list_empty (&data->epfiles));
	BUG_ON (waitqueue_active (&data->wait));
	kfree (data);
}

/*----------------------------------------------------------------------*/

/* most "how to use the hardware" policy choices are in userspace:
 * mapping endpoint roles (which the driver needs) to the capabilities
 * which the usb controller has.  most of those capabilities are exposed
 * implicitly, starting with the driver name and then endpoint names.
 */

static const char *CHIP;

/*----------------------------------------------------------------------*/

/* NOTE:  don't use dev_printk calls before binding to the gadget
 * at the end of ep0 configuration, or after unbind.
 */

/* too wordy: dev_printk(level , &(d)->gadget->dev , fmt , ## args) */
#define xprintk(d,level,fmt,args...) \
	printk(level "%s: " fmt , shortname , ## args)

#ifdef DEBUG
#define DBG(dev,fmt,args...) \
	xprintk(dev , KERN_DEBUG , fmt , ## args)
#else
#define DBG(dev,fmt,args...) \
	do { } while (0)
#endif /* DEBUG */

247
#ifdef VERBOSE_DEBUG
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
#define VDEBUG	DBG
#else
#define VDEBUG(dev,fmt,args...) \
	do { } while (0)
#endif /* DEBUG */

#define ERROR(dev,fmt,args...) \
	xprintk(dev , KERN_ERR , fmt , ## args)
#define INFO(dev,fmt,args...) \
	xprintk(dev , KERN_INFO , fmt , ## args)


/*----------------------------------------------------------------------*/

/* SYNCHRONOUS ENDPOINT OPERATIONS (bulk/intr/iso)
 *
 * After opening, configure non-control endpoints.  Then use normal
 * stream read() and write() requests; and maybe ioctl() to get more
266
 * precise FIFO status when recovering from cancellation.
L
Linus Torvalds 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
 */

static void epio_complete (struct usb_ep *ep, struct usb_request *req)
{
	struct ep_data	*epdata = ep->driver_data;

	if (!req->context)
		return;
	if (req->status)
		epdata->status = req->status;
	else
		epdata->status = req->actual;
	complete ((struct completion *)req->context);
}

/* tasklock endpoint, returning when it's connected.
 * still need dev->lock to use epdata->ep.
 */
static int
get_ready_ep (unsigned f_flags, struct ep_data *epdata)
{
	int	val;

	if (f_flags & O_NONBLOCK) {
291
		if (!mutex_trylock(&epdata->lock))
L
Linus Torvalds 已提交
292 293
			goto nonblock;
		if (epdata->state != STATE_EP_ENABLED) {
294
			mutex_unlock(&epdata->lock);
L
Linus Torvalds 已提交
295 296 297 298 299 300 301
nonblock:
			val = -EAGAIN;
		} else
			val = 0;
		return val;
	}

302 303
	val = mutex_lock_interruptible(&epdata->lock);
	if (val < 0)
L
Linus Torvalds 已提交
304
		return val;
305

L
Linus Torvalds 已提交
306 307 308 309 310 311 312 313 314 315 316
	switch (epdata->state) {
	case STATE_EP_ENABLED:
		break;
	// case STATE_EP_DISABLED:		/* "can't happen" */
	// case STATE_EP_READY:			/* "can't happen" */
	default:				/* error! */
		pr_debug ("%s: ep %p not available, state %d\n",
				shortname, epdata, epdata->state);
		// FALLTHROUGH
	case STATE_EP_UNBOUND:			/* clean disconnect */
		val = -ENODEV;
317
		mutex_unlock(&epdata->lock);
L
Linus Torvalds 已提交
318 319 320 321 322 323 324
	}
	return val;
}

static ssize_t
ep_io (struct ep_data *epdata, void *buf, unsigned len)
{
325
	DECLARE_COMPLETION_ONSTACK (done);
L
Linus Torvalds 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
	int value;

	spin_lock_irq (&epdata->dev->lock);
	if (likely (epdata->ep != NULL)) {
		struct usb_request	*req = epdata->req;

		req->context = &done;
		req->complete = epio_complete;
		req->buf = buf;
		req->length = len;
		value = usb_ep_queue (epdata->ep, req, GFP_ATOMIC);
	} else
		value = -ENODEV;
	spin_unlock_irq (&epdata->dev->lock);

	if (likely (value == 0)) {
		value = wait_event_interruptible (done.wait, done.done);
		if (value != 0) {
			spin_lock_irq (&epdata->dev->lock);
			if (likely (epdata->ep != NULL)) {
				DBG (epdata->dev, "%s i/o interrupted\n",
						epdata->name);
				usb_ep_dequeue (epdata->ep, epdata->req);
				spin_unlock_irq (&epdata->dev->lock);

				wait_event (done.wait, done.done);
				if (epdata->status == -ECONNRESET)
					epdata->status = -EINTR;
			} else {
				spin_unlock_irq (&epdata->dev->lock);

				DBG (epdata->dev, "endpoint gone\n");
				epdata->status = -ENODEV;
			}
		}
		return epdata->status;
	}
	return value;
}


/* handle a synchronous OUT bulk/intr/iso transfer */
static ssize_t
ep_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
{
	struct ep_data		*data = fd->private_data;
	void			*kbuf;
	ssize_t			value;

	if ((value = get_ready_ep (fd->f_flags, data)) < 0)
		return value;

	/* halt any endpoint by doing a "wrong direction" i/o call */
379
	if (usb_endpoint_dir_in(&data->desc)) {
380 381
		if (usb_endpoint_xfer_isoc(&data->desc)) {
			mutex_unlock(&data->lock);
L
Linus Torvalds 已提交
382
			return -EINVAL;
383
		}
L
Linus Torvalds 已提交
384 385 386 387 388
		DBG (data->dev, "%s halt\n", data->name);
		spin_lock_irq (&data->dev->lock);
		if (likely (data->ep != NULL))
			usb_ep_set_halt (data->ep);
		spin_unlock_irq (&data->dev->lock);
389
		mutex_unlock(&data->lock);
L
Linus Torvalds 已提交
390 391 392 393 394 395
		return -EBADMSG;
	}

	/* FIXME readahead for O_NONBLOCK and poll(); careful with ZLPs */

	value = -ENOMEM;
396
	kbuf = kmalloc (len, GFP_KERNEL);
L
Linus Torvalds 已提交
397 398 399 400
	if (unlikely (!kbuf))
		goto free1;

	value = ep_io (data, kbuf, len);
401 402
	VDEBUG (data->dev, "%s read %zu OUT, status %d\n",
		data->name, len, (int) value);
L
Linus Torvalds 已提交
403 404 405 406
	if (value >= 0 && copy_to_user (buf, kbuf, value))
		value = -EFAULT;

free1:
407
	mutex_unlock(&data->lock);
L
Linus Torvalds 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
	kfree (kbuf);
	return value;
}

/* handle a synchronous IN bulk/intr/iso transfer */
static ssize_t
ep_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
{
	struct ep_data		*data = fd->private_data;
	void			*kbuf;
	ssize_t			value;

	if ((value = get_ready_ep (fd->f_flags, data)) < 0)
		return value;

	/* halt any endpoint by doing a "wrong direction" i/o call */
424
	if (!usb_endpoint_dir_in(&data->desc)) {
425 426
		if (usb_endpoint_xfer_isoc(&data->desc)) {
			mutex_unlock(&data->lock);
L
Linus Torvalds 已提交
427
			return -EINVAL;
428
		}
L
Linus Torvalds 已提交
429 430 431 432 433
		DBG (data->dev, "%s halt\n", data->name);
		spin_lock_irq (&data->dev->lock);
		if (likely (data->ep != NULL))
			usb_ep_set_halt (data->ep);
		spin_unlock_irq (&data->dev->lock);
434
		mutex_unlock(&data->lock);
L
Linus Torvalds 已提交
435 436 437 438 439 440
		return -EBADMSG;
	}

	/* FIXME writebehind for O_NONBLOCK and poll(), qlen = 1 */

	value = -ENOMEM;
441
	kbuf = kmalloc (len, GFP_KERNEL);
L
Linus Torvalds 已提交
442 443 444 445 446 447 448 449
	if (!kbuf)
		goto free1;
	if (copy_from_user (kbuf, buf, len)) {
		value = -EFAULT;
		goto free1;
	}

	value = ep_io (data, kbuf, len);
450 451
	VDEBUG (data->dev, "%s write %zu IN, status %d\n",
		data->name, len, (int) value);
L
Linus Torvalds 已提交
452
free1:
453
	mutex_unlock(&data->lock);
L
Linus Torvalds 已提交
454 455 456 457 458 459 460 461
	kfree (kbuf);
	return value;
}

static int
ep_release (struct inode *inode, struct file *fd)
{
	struct ep_data		*data = fd->private_data;
462 463
	int value;

464 465
	value = mutex_lock_interruptible(&data->lock);
	if (value < 0)
466
		return value;
L
Linus Torvalds 已提交
467 468 469 470 471 472

	/* clean up if this can be reopened */
	if (data->state != STATE_EP_UNBOUND) {
		data->state = STATE_EP_DISABLED;
		data->desc.bDescriptorType = 0;
		data->hs_desc.bDescriptorType = 0;
473
		usb_ep_disable(data->ep);
L
Linus Torvalds 已提交
474
	}
475
	mutex_unlock(&data->lock);
L
Linus Torvalds 已提交
476 477 478 479
	put_ep (data);
	return 0;
}

480
static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
L
Linus Torvalds 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
{
	struct ep_data		*data = fd->private_data;
	int			status;

	if ((status = get_ready_ep (fd->f_flags, data)) < 0)
		return status;

	spin_lock_irq (&data->dev->lock);
	if (likely (data->ep != NULL)) {
		switch (code) {
		case GADGETFS_FIFO_STATUS:
			status = usb_ep_fifo_status (data->ep);
			break;
		case GADGETFS_FIFO_FLUSH:
			usb_ep_fifo_flush (data->ep);
			break;
		case GADGETFS_CLEAR_HALT:
			status = usb_ep_clear_halt (data->ep);
			break;
		default:
			status = -ENOTTY;
		}
	} else
		status = -ENODEV;
	spin_unlock_irq (&data->dev->lock);
506
	mutex_unlock(&data->lock);
L
Linus Torvalds 已提交
507 508 509 510 511 512 513 514 515 516 517
	return status;
}

/*----------------------------------------------------------------------*/

/* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */

struct kiocb_priv {
	struct usb_request	*req;
	struct ep_data		*epdata;
	void			*buf;
518 519
	const struct iovec	*iv;
	unsigned long		nr_segs;
L
Linus Torvalds 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
	unsigned		actual;
};

static int ep_aio_cancel(struct kiocb *iocb, struct io_event *e)
{
	struct kiocb_priv	*priv = iocb->private;
	struct ep_data		*epdata;
	int			value;

	local_irq_disable();
	epdata = priv->epdata;
	// spin_lock(&epdata->dev->lock);
	kiocbSetCancelled(iocb);
	if (likely(epdata && epdata->ep && priv->req))
		value = usb_ep_dequeue (epdata->ep, priv->req);
	else
		value = -EINVAL;
	// spin_unlock(&epdata->dev->lock);
	local_irq_enable();

	aio_put_req(iocb);
	return value;
}

static ssize_t ep_aio_read_retry(struct kiocb *iocb)
{
	struct kiocb_priv	*priv = iocb->private;
547
	ssize_t			len, total;
548
	void			*to_copy;
549 550
	int			i;

551 552 553 554 555
	/* we "retry" to get the right mm context for this: */

	/* copy stuff into user buffers */
	total = priv->actual;
	len = 0;
556
	to_copy = priv->buf;
557 558 559
	for (i=0; i < priv->nr_segs; i++) {
		ssize_t this = min((ssize_t)(priv->iv[i].iov_len), total);

560
		if (copy_to_user(priv->iv[i].iov_base, to_copy, this)) {
561 562 563 564 565 566 567
			if (len == 0)
				len = -EFAULT;
			break;
		}

		total -= this;
		len += this;
568
		to_copy += this;
569 570 571 572 573 574
		if (total == 0)
			break;
	}
	kfree(priv->buf);
	kfree(priv);
	return len;
L
Linus Torvalds 已提交
575 576 577 578 579 580 581 582 583 584 585 586
}

static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
{
	struct kiocb		*iocb = req->context;
	struct kiocb_priv	*priv = iocb->private;
	struct ep_data		*epdata = priv->epdata;

	/* lock against disconnect (and ideally, cancel) */
	spin_lock(&epdata->dev->lock);
	priv->req = NULL;
	priv->epdata = NULL;
A
Alan Stern 已提交
587 588 589 590 591 592

	/* if this was a write or a read returning no data then we
	 * don't need to copy anything to userspace, so we can
	 * complete the aio request immediately.
	 */
	if (priv->iv == NULL || unlikely(req->actual == 0)) {
L
Linus Torvalds 已提交
593 594 595 596
		kfree(req->buf);
		kfree(priv);
		iocb->private = NULL;
		/* aio_complete() reports bytes-transferred _and_ faults */
A
Alan Stern 已提交
597
		aio_complete(iocb, req->actual ? req->actual : req->status,
L
Linus Torvalds 已提交
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
				req->status);
	} else {
		/* retry() won't report both; so we hide some faults */
		if (unlikely(0 != req->status))
			DBG(epdata->dev, "%s fault %d len %d\n",
				ep->name, req->status, req->actual);

		priv->buf = req->buf;
		priv->actual = req->actual;
		kick_iocb(iocb);
	}
	spin_unlock(&epdata->dev->lock);

	usb_ep_free_request(ep, req);
	put_ep(epdata);
}

static ssize_t
ep_aio_rwtail(
	struct kiocb	*iocb,
	char		*buf,
	size_t		len,
	struct ep_data	*epdata,
621
	const struct iovec *iv,
622
	unsigned long	nr_segs
L
Linus Torvalds 已提交
623 624
)
{
625
	struct kiocb_priv	*priv;
L
Linus Torvalds 已提交
626 627 628 629 630 631 632 633 634 635 636
	struct usb_request	*req;
	ssize_t			value;

	priv = kmalloc(sizeof *priv, GFP_KERNEL);
	if (!priv) {
		value = -ENOMEM;
fail:
		kfree(buf);
		return value;
	}
	iocb->private = priv;
637 638
	priv->iv = iv;
	priv->nr_segs = nr_segs;
L
Linus Torvalds 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671

	value = get_ready_ep(iocb->ki_filp->f_flags, epdata);
	if (unlikely(value < 0)) {
		kfree(priv);
		goto fail;
	}

	iocb->ki_cancel = ep_aio_cancel;
	get_ep(epdata);
	priv->epdata = epdata;
	priv->actual = 0;

	/* each kiocb is coupled to one usb_request, but we can't
	 * allocate or submit those if the host disconnected.
	 */
	spin_lock_irq(&epdata->dev->lock);
	if (likely(epdata->ep)) {
		req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC);
		if (likely(req)) {
			priv->req = req;
			req->buf = buf;
			req->length = len;
			req->complete = ep_aio_complete;
			req->context = iocb;
			value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC);
			if (unlikely(0 != value))
				usb_ep_free_request(epdata->ep, req);
		} else
			value = -EAGAIN;
	} else
		value = -ENODEV;
	spin_unlock_irq(&epdata->dev->lock);

672
	mutex_unlock(&epdata->lock);
L
Linus Torvalds 已提交
673 674 675 676 677

	if (unlikely(value)) {
		kfree(priv);
		put_ep(epdata);
	} else
678
		value = (iv ? -EIOCBRETRY : -EIOCBQUEUED);
L
Linus Torvalds 已提交
679 680 681 682
	return value;
}

static ssize_t
683 684
ep_aio_read(struct kiocb *iocb, const struct iovec *iov,
		unsigned long nr_segs, loff_t o)
L
Linus Torvalds 已提交
685 686 687 688
{
	struct ep_data		*epdata = iocb->ki_filp->private_data;
	char			*buf;

689
	if (unlikely(usb_endpoint_dir_in(&epdata->desc)))
L
Linus Torvalds 已提交
690
		return -EINVAL;
691 692

	buf = kmalloc(iocb->ki_left, GFP_KERNEL);
L
Linus Torvalds 已提交
693 694
	if (unlikely(!buf))
		return -ENOMEM;
695

L
Linus Torvalds 已提交
696
	iocb->ki_retry = ep_aio_read_retry;
697
	return ep_aio_rwtail(iocb, buf, iocb->ki_left, epdata, iov, nr_segs);
L
Linus Torvalds 已提交
698 699 700
}

static ssize_t
701 702
ep_aio_write(struct kiocb *iocb, const struct iovec *iov,
		unsigned long nr_segs, loff_t o)
L
Linus Torvalds 已提交
703 704 705
{
	struct ep_data		*epdata = iocb->ki_filp->private_data;
	char			*buf;
706 707
	size_t			len = 0;
	int			i = 0;
L
Linus Torvalds 已提交
708

709
	if (unlikely(!usb_endpoint_dir_in(&epdata->desc)))
L
Linus Torvalds 已提交
710
		return -EINVAL;
711 712

	buf = kmalloc(iocb->ki_left, GFP_KERNEL);
L
Linus Torvalds 已提交
713 714
	if (unlikely(!buf))
		return -ENOMEM;
715 716 717 718 719 720 721 722

	for (i=0; i < nr_segs; i++) {
		if (unlikely(copy_from_user(&buf[len], iov[i].iov_base,
				iov[i].iov_len) != 0)) {
			kfree(buf);
			return -EFAULT;
		}
		len += iov[i].iov_len;
L
Linus Torvalds 已提交
723
	}
724
	return ep_aio_rwtail(iocb, buf, len, epdata, NULL, 0);
L
Linus Torvalds 已提交
725 726 727 728 729
}

/*----------------------------------------------------------------------*/

/* used after endpoint configuration */
730
static const struct file_operations ep_io_operations = {
L
Linus Torvalds 已提交
731 732 733 734 735
	.owner =	THIS_MODULE,
	.llseek =	no_llseek,

	.read =		ep_read,
	.write =	ep_write,
736
	.unlocked_ioctl = ep_ioctl,
L
Linus Torvalds 已提交
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
	.release =	ep_release,

	.aio_read =	ep_aio_read,
	.aio_write =	ep_aio_write,
};

/* ENDPOINT INITIALIZATION
 *
 *     fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
 *     status = write (fd, descriptors, sizeof descriptors)
 *
 * That write establishes the endpoint configuration, configuring
 * the controller to process bulk, interrupt, or isochronous transfers
 * at the right maxpacket size, and so on.
 *
 * The descriptors are message type 1, identified by a host order u32
 * at the beginning of what's written.  Descriptor order is: full/low
 * speed descriptor, then optional high speed descriptor.
 */
static ssize_t
ep_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
{
	struct ep_data		*data = fd->private_data;
	struct usb_ep		*ep;
	u32			tag;
762
	int			value, length = len;
L
Linus Torvalds 已提交
763

764 765
	value = mutex_lock_interruptible(&data->lock);
	if (value < 0)
L
Linus Torvalds 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
		return value;

	if (data->state != STATE_EP_READY) {
		value = -EL2HLT;
		goto fail;
	}

	value = len;
	if (len < USB_DT_ENDPOINT_SIZE + 4)
		goto fail0;

	/* we might need to change message format someday */
	if (copy_from_user (&tag, buf, 4)) {
		goto fail1;
	}
	if (tag != 1) {
		DBG(data->dev, "config %s, bad tag %d\n", data->name, tag);
		goto fail0;
	}
	buf += 4;
	len -= 4;

	/* NOTE:  audio endpoint extensions not accepted here;
	 * just don't include the extra bytes.
	 */

	/* full/low speed descriptor, then high speed */
	if (copy_from_user (&data->desc, buf, USB_DT_ENDPOINT_SIZE)) {
		goto fail1;
	}
	if (data->desc.bLength != USB_DT_ENDPOINT_SIZE
			|| data->desc.bDescriptorType != USB_DT_ENDPOINT)
		goto fail0;
	if (len != USB_DT_ENDPOINT_SIZE) {
		if (len != 2 * USB_DT_ENDPOINT_SIZE)
			goto fail0;
		if (copy_from_user (&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE,
					USB_DT_ENDPOINT_SIZE)) {
			goto fail1;
		}
		if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE
				|| data->hs_desc.bDescriptorType
					!= USB_DT_ENDPOINT) {
			DBG(data->dev, "config %s, bad hs length or type\n",
					data->name);
			goto fail0;
		}
	}

	spin_lock_irq (&data->dev->lock);
	if (data->dev->state == STATE_DEV_UNBOUND) {
		value = -ENOENT;
		goto gone;
	} else if ((ep = data->ep) == NULL) {
		value = -ENODEV;
		goto gone;
	}
	switch (data->dev->gadget->speed) {
	case USB_SPEED_LOW:
	case USB_SPEED_FULL:
826 827
		ep->desc = &data->desc;
		value = usb_ep_enable(ep);
L
Linus Torvalds 已提交
828 829 830
		if (value == 0)
			data->state = STATE_EP_ENABLED;
		break;
831
#ifdef	CONFIG_USB_GADGET_DUALSPEED
L
Linus Torvalds 已提交
832 833
	case USB_SPEED_HIGH:
		/* fails if caller didn't provide that descriptor... */
834 835
		ep->desc = &data->hs_desc;
		value = usb_ep_enable(ep);
L
Linus Torvalds 已提交
836 837 838 839 840
		if (value == 0)
			data->state = STATE_EP_ENABLED;
		break;
#endif
	default:
841
		DBG(data->dev, "unconnected, %s init abandoned\n",
L
Linus Torvalds 已提交
842
				data->name);
843
		value = -EINVAL;
L
Linus Torvalds 已提交
844
	}
845
	if (value == 0) {
L
Linus Torvalds 已提交
846
		fd->f_op = &ep_io_operations;
847 848
		value = length;
	}
L
Linus Torvalds 已提交
849 850 851 852 853 854 855
gone:
	spin_unlock_irq (&data->dev->lock);
	if (value < 0) {
fail:
		data->desc.bDescriptorType = 0;
		data->hs_desc.bDescriptorType = 0;
	}
856
	mutex_unlock(&data->lock);
L
Linus Torvalds 已提交
857 858 859 860 861 862 863 864 865 866 867 868
	return value;
fail0:
	value = -EINVAL;
	goto fail;
fail1:
	value = -EFAULT;
	goto fail;
}

static int
ep_open (struct inode *inode, struct file *fd)
{
869
	struct ep_data		*data = inode->i_private;
L
Linus Torvalds 已提交
870 871
	int			value = -EBUSY;

872
	if (mutex_lock_interruptible(&data->lock) != 0)
L
Linus Torvalds 已提交
873 874 875 876 877 878 879 880 881 882 883 884 885 886
		return -EINTR;
	spin_lock_irq (&data->dev->lock);
	if (data->dev->state == STATE_DEV_UNBOUND)
		value = -ENOENT;
	else if (data->state == STATE_EP_DISABLED) {
		value = 0;
		data->state = STATE_EP_READY;
		get_ep (data);
		fd->private_data = data;
		VDEBUG (data->dev, "%s ready\n", data->name);
	} else
		DBG (data->dev, "%s state %d\n",
			data->name, data->state);
	spin_unlock_irq (&data->dev->lock);
887
	mutex_unlock(&data->lock);
L
Linus Torvalds 已提交
888 889 890 891
	return value;
}

/* used before endpoint configuration */
892
static const struct file_operations ep_config_operations = {
L
Linus Torvalds 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
	.owner =	THIS_MODULE,
	.llseek =	no_llseek,

	.open =		ep_open,
	.write =	ep_config,
	.release =	ep_release,
};

/*----------------------------------------------------------------------*/

/* EP0 IMPLEMENTATION can be partly in userspace.
 *
 * Drivers that use this facility receive various events, including
 * control requests the kernel doesn't handle.  Drivers that don't
 * use this facility may be too simple-minded for real applications.
 */

static inline void ep0_readable (struct dev_data *dev)
{
	wake_up (&dev->wait);
	kill_fasync (&dev->fasync, SIGIO, POLL_IN);
}

static void clean_req (struct usb_ep *ep, struct usb_request *req)
{
	struct dev_data		*dev = ep->driver_data;

	if (req->buf != dev->rbuf) {
921
		kfree(req->buf);
L
Linus Torvalds 已提交
922 923 924 925 926 927 928 929 930 931
		req->buf = dev->rbuf;
		req->dma = DMA_ADDR_INVALID;
	}
	req->complete = epio_complete;
	dev->setup_out_ready = 0;
}

static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
{
	struct dev_data		*dev = ep->driver_data;
D
David Brownell 已提交
932
	unsigned long		flags;
L
Linus Torvalds 已提交
933 934 935
	int			free = 1;

	/* for control OUT, data must still get to userspace */
D
David Brownell 已提交
936
	spin_lock_irqsave(&dev->lock, flags);
L
Linus Torvalds 已提交
937 938 939 940 941 942
	if (!dev->setup_in) {
		dev->setup_out_error = (req->status != 0);
		if (!dev->setup_out_error)
			free = 0;
		dev->setup_out_ready = 1;
		ep0_readable (dev);
D
David Brownell 已提交
943
	}
L
Linus Torvalds 已提交
944 945 946 947 948

	/* clean up as appropriate */
	if (free && req->buf != &dev->rbuf)
		clean_req (ep, req);
	req->complete = epio_complete;
D
David Brownell 已提交
949
	spin_unlock_irqrestore(&dev->lock, flags);
L
Linus Torvalds 已提交
950 951 952 953 954 955 956 957 958 959 960
}

static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
{
	struct dev_data	*dev = ep->driver_data;

	if (dev->setup_out_ready) {
		DBG (dev, "ep0 request busy!\n");
		return -EBUSY;
	}
	if (len > sizeof (dev->rbuf))
961
		req->buf = kmalloc(len, GFP_ATOMIC);
962
	if (req->buf == NULL) {
L
Linus Torvalds 已提交
963 964 965 966 967
		req->buf = dev->rbuf;
		return -ENOMEM;
	}
	req->complete = ep0_complete;
	req->length = len;
968
	req->zero = 0;
L
Linus Torvalds 已提交
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
	return 0;
}

static ssize_t
ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
{
	struct dev_data			*dev = fd->private_data;
	ssize_t				retval;
	enum ep0_state			state;

	spin_lock_irq (&dev->lock);

	/* report fd mode change before acting on it */
	if (dev->setup_abort) {
		dev->setup_abort = 0;
		retval = -EIDRM;
		goto done;
	}

	/* control DATA stage */
D
David Brownell 已提交
989
	if ((state = dev->state) == STATE_DEV_SETUP) {
L
Linus Torvalds 已提交
990 991 992 993 994

		if (dev->setup_in) {		/* stall IN */
			VDEBUG(dev, "ep0in stall\n");
			(void) usb_ep_set_halt (dev->gadget->ep0);
			retval = -EL2HLT;
D
David Brownell 已提交
995
			dev->state = STATE_DEV_CONNECTED;
L
Linus Torvalds 已提交
996 997 998 999 1000 1001 1002

		} else if (len == 0) {		/* ack SET_CONFIGURATION etc */
			struct usb_ep		*ep = dev->gadget->ep0;
			struct usb_request	*req = dev->req;

			if ((retval = setup_req (ep, req, 0)) == 0)
				retval = usb_ep_queue (ep, req, GFP_ATOMIC);
D
David Brownell 已提交
1003
			dev->state = STATE_DEV_CONNECTED;
L
Linus Torvalds 已提交
1004 1005 1006 1007

			/* assume that was SET_CONFIGURATION */
			if (dev->current_config) {
				unsigned power;
1008 1009 1010 1011

				if (gadget_is_dualspeed(dev->gadget)
						&& (dev->gadget->speed
							== USB_SPEED_HIGH))
L
Linus Torvalds 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
					power = dev->hs_config->bMaxPower;
				else
					power = dev->config->bMaxPower;
				usb_gadget_vbus_draw(dev->gadget, 2 * power);
			}

		} else {			/* collect OUT data */
			if ((fd->f_flags & O_NONBLOCK) != 0
					&& !dev->setup_out_ready) {
				retval = -EAGAIN;
				goto done;
			}
			spin_unlock_irq (&dev->lock);
			retval = wait_event_interruptible (dev->wait,
					dev->setup_out_ready != 0);

			/* FIXME state could change from under us */
			spin_lock_irq (&dev->lock);
			if (retval)
				goto done;
D
David Brownell 已提交
1032 1033 1034 1035 1036 1037 1038

			if (dev->state != STATE_DEV_SETUP) {
				retval = -ECANCELED;
				goto done;
			}
			dev->state = STATE_DEV_CONNECTED;

L
Linus Torvalds 已提交
1039 1040 1041 1042 1043
			if (dev->setup_out_error)
				retval = -EIO;
			else {
				len = min (len, (size_t)dev->req->actual);
// FIXME don't call this with the spinlock held ...
S
Skip Hansen 已提交
1044
				if (copy_to_user (buf, dev->req->buf, len))
L
Linus Torvalds 已提交
1045
					retval = -EFAULT;
1046 1047
				else
					retval = len;
L
Linus Torvalds 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
				clean_req (dev->gadget->ep0, dev->req);
				/* NOTE userspace can't yet choose to stall */
			}
		}
		goto done;
	}

	/* else normal: return event data */
	if (len < sizeof dev->event [0]) {
		retval = -EINVAL;
		goto done;
	}
	len -= len % sizeof (struct usb_gadgetfs_event);
	dev->usermode_setup = 1;

scan:
	/* return queued events right away */
	if (dev->ev_next != 0) {
		unsigned		i, n;

		n = len / sizeof (struct usb_gadgetfs_event);
D
David Brownell 已提交
1069 1070
		if (dev->ev_next < n)
			n = dev->ev_next;
L
Linus Torvalds 已提交
1071

D
David Brownell 已提交
1072
		/* ep0 i/o has special semantics during STATE_DEV_SETUP */
L
Linus Torvalds 已提交
1073 1074
		for (i = 0; i < n; i++) {
			if (dev->event [i].type == GADGETFS_SETUP) {
D
David Brownell 已提交
1075 1076
				dev->state = STATE_DEV_SETUP;
				n = i + 1;
L
Linus Torvalds 已提交
1077 1078 1079 1080
				break;
			}
		}
		spin_unlock_irq (&dev->lock);
D
David Brownell 已提交
1081
		len = n * sizeof (struct usb_gadgetfs_event);
L
Linus Torvalds 已提交
1082 1083 1084 1085 1086 1087 1088 1089 1090
		if (copy_to_user (buf, &dev->event, len))
			retval = -EFAULT;
		else
			retval = len;
		if (len > 0) {
			/* NOTE this doesn't guard against broken drivers;
			 * concurrent ep0 readers may lose events.
			 */
			spin_lock_irq (&dev->lock);
D
David Brownell 已提交
1091 1092
			if (dev->ev_next > n) {
				memmove(&dev->event[0], &dev->event[n],
L
Linus Torvalds 已提交
1093
					sizeof (struct usb_gadgetfs_event)
D
David Brownell 已提交
1094 1095 1096
						* (dev->ev_next - n));
			}
			dev->ev_next -= n;
L
Linus Torvalds 已提交
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
			spin_unlock_irq (&dev->lock);
		}
		return retval;
	}
	if (fd->f_flags & O_NONBLOCK) {
		retval = -EAGAIN;
		goto done;
	}

	switch (state) {
	default:
1108
		DBG (dev, "fail %s, state %d\n", __func__, state);
L
Linus Torvalds 已提交
1109 1110
		retval = -ESRCH;
		break;
D
David Brownell 已提交
1111 1112
	case STATE_DEV_UNCONNECTED:
	case STATE_DEV_CONNECTED:
L
Linus Torvalds 已提交
1113
		spin_unlock_irq (&dev->lock);
1114
		DBG (dev, "%s wait\n", __func__);
L
Linus Torvalds 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138

		/* wait for events */
		retval = wait_event_interruptible (dev->wait,
				dev->ev_next != 0);
		if (retval < 0)
			return retval;
		spin_lock_irq (&dev->lock);
		goto scan;
	}

done:
	spin_unlock_irq (&dev->lock);
	return retval;
}

static struct usb_gadgetfs_event *
next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
{
	struct usb_gadgetfs_event	*event;
	unsigned			i;

	switch (type) {
	/* these events purge the queue */
	case GADGETFS_DISCONNECT:
D
David Brownell 已提交
1139
		if (dev->state == STATE_DEV_SETUP)
L
Linus Torvalds 已提交
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
			dev->setup_abort = 1;
		// FALL THROUGH
	case GADGETFS_CONNECT:
		dev->ev_next = 0;
		break;
	case GADGETFS_SETUP:		/* previous request timed out */
	case GADGETFS_SUSPEND:		/* same effect */
		/* these events can't be repeated */
		for (i = 0; i != dev->ev_next; i++) {
			if (dev->event [i].type != type)
				continue;
D
David Brownell 已提交
1151
			DBG(dev, "discard old event[%d] %d\n", i, type);
L
Linus Torvalds 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
			dev->ev_next--;
			if (i == dev->ev_next)
				break;
			/* indices start at zero, for simplicity */
			memmove (&dev->event [i], &dev->event [i + 1],
				sizeof (struct usb_gadgetfs_event)
					* (dev->ev_next - i));
		}
		break;
	default:
		BUG ();
	}
D
David Brownell 已提交
1164
	VDEBUG(dev, "event[%d] = %d\n", dev->ev_next, type);
L
Linus Torvalds 已提交
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
	event = &dev->event [dev->ev_next++];
	BUG_ON (dev->ev_next > N_EVENT);
	memset (event, 0, sizeof *event);
	event->type = type;
	return event;
}

static ssize_t
ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
{
	struct dev_data		*dev = fd->private_data;
	ssize_t			retval = -ESRCH;

	spin_lock_irq (&dev->lock);

	/* report fd mode change before acting on it */
	if (dev->setup_abort) {
		dev->setup_abort = 0;
		retval = -EIDRM;

	/* data and/or status stage for control request */
D
David Brownell 已提交
1186
	} else if (dev->state == STATE_DEV_SETUP) {
L
Linus Torvalds 已提交
1187 1188 1189 1190 1191

		/* IN DATA+STATUS caller makes len <= wLength */
		if (dev->setup_in) {
			retval = setup_req (dev->gadget->ep0, dev->req, len);
			if (retval == 0) {
D
David Brownell 已提交
1192
				dev->state = STATE_DEV_CONNECTED;
L
Linus Torvalds 已提交
1193 1194 1195
				spin_unlock_irq (&dev->lock);
				if (copy_from_user (dev->req->buf, buf, len))
					retval = -EFAULT;
1196 1197 1198
				else {
					if (len < dev->setup_wLength)
						dev->req->zero = 1;
L
Linus Torvalds 已提交
1199 1200 1201
					retval = usb_ep_queue (
						dev->gadget->ep0, dev->req,
						GFP_KERNEL);
1202
				}
L
Linus Torvalds 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
				if (retval < 0) {
					spin_lock_irq (&dev->lock);
					clean_req (dev->gadget->ep0, dev->req);
					spin_unlock_irq (&dev->lock);
				} else
					retval = len;

				return retval;
			}

		/* can stall some OUT transfers */
		} else if (dev->setup_can_stall) {
			VDEBUG(dev, "ep0out stall\n");
			(void) usb_ep_set_halt (dev->gadget->ep0);
			retval = -EL2HLT;
D
David Brownell 已提交
1218
			dev->state = STATE_DEV_CONNECTED;
L
Linus Torvalds 已提交
1219 1220 1221 1222
		} else {
			DBG(dev, "bogus ep0out stall!\n");
		}
	} else
1223
		DBG (dev, "fail %s, state %d\n", __func__, dev->state);
L
Linus Torvalds 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233

	spin_unlock_irq (&dev->lock);
	return retval;
}

static int
ep0_fasync (int f, struct file *fd, int on)
{
	struct dev_data		*dev = fd->private_data;
	// caller must F_SETOWN before signal delivery happens
1234
	VDEBUG (dev, "%s %s\n", __func__, on ? "on" : "off");
L
Linus Torvalds 已提交
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
	return fasync_helper (f, fd, on, &dev->fasync);
}

static struct usb_gadget_driver gadgetfs_driver;

static int
dev_release (struct inode *inode, struct file *fd)
{
	struct dev_data		*dev = fd->private_data;

	/* closing ep0 === shutdown all */

	usb_gadget_unregister_driver (&gadgetfs_driver);

	/* at this point "good" hardware has disconnected the
	 * device from USB; the host won't see it any more.
	 * alternatively, all host requests will time out.
	 */

	kfree (dev->buf);
	dev->buf = NULL;
	put_dev (dev);

	/* other endpoints were all decoupled from this device */
D
David Brownell 已提交
1259
	spin_lock_irq(&dev->lock);
L
Linus Torvalds 已提交
1260
	dev->state = STATE_DEV_DISABLED;
D
David Brownell 已提交
1261
	spin_unlock_irq(&dev->lock);
L
Linus Torvalds 已提交
1262 1263 1264
	return 0;
}

1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
static unsigned int
ep0_poll (struct file *fd, poll_table *wait)
{
       struct dev_data         *dev = fd->private_data;
       int                     mask = 0;

       poll_wait(fd, &dev->wait, wait);

       spin_lock_irq (&dev->lock);

       /* report fd mode change before acting on it */
       if (dev->setup_abort) {
               dev->setup_abort = 0;
               mask = POLLHUP;
               goto out;
       }

D
David Brownell 已提交
1282
       if (dev->state == STATE_DEV_SETUP) {
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
               if (dev->setup_in || dev->setup_can_stall)
                       mask = POLLOUT;
       } else {
               if (dev->ev_next != 0)
                       mask = POLLIN;
       }
out:
       spin_unlock_irq(&dev->lock);
       return mask;
}

1294
static long dev_ioctl (struct file *fd, unsigned code, unsigned long value)
L
Linus Torvalds 已提交
1295 1296 1297
{
	struct dev_data		*dev = fd->private_data;
	struct usb_gadget	*gadget = dev->gadget;
1298
	long ret = -ENOTTY;
L
Linus Torvalds 已提交
1299

1300
	if (gadget->ops->ioctl)
1301
		ret = gadget->ops->ioctl (gadget, code, value);
1302

1303
	return ret;
L
Linus Torvalds 已提交
1304 1305 1306
}

/* used after device configuration */
1307
static const struct file_operations ep0_io_operations = {
L
Linus Torvalds 已提交
1308 1309 1310 1311 1312 1313
	.owner =	THIS_MODULE,
	.llseek =	no_llseek,

	.read =		ep0_read,
	.write =	ep0_write,
	.fasync =	ep0_fasync,
1314
	.poll =		ep0_poll,
1315
	.unlocked_ioctl =	dev_ioctl,
L
Linus Torvalds 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
	.release =	dev_release,
};

/*----------------------------------------------------------------------*/

/* The in-kernel gadget driver handles most ep0 issues, in particular
 * enumerating the single configuration (as provided from user space).
 *
 * Unrecognized ep0 requests may be handled in user space.
 */

1327
#ifdef	CONFIG_USB_GADGET_DUALSPEED
L
Linus Torvalds 已提交
1328 1329 1330 1331 1332 1333 1334
static void make_qualifier (struct dev_data *dev)
{
	struct usb_qualifier_descriptor		qual;
	struct usb_device_descriptor		*desc;

	qual.bLength = sizeof qual;
	qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
1335
	qual.bcdUSB = cpu_to_le16 (0x0200);
L
Linus Torvalds 已提交
1336 1337 1338 1339 1340 1341 1342

	desc = dev->dev;
	qual.bDeviceClass = desc->bDeviceClass;
	qual.bDeviceSubClass = desc->bDeviceSubClass;
	qual.bDeviceProtocol = desc->bDeviceProtocol;

	/* assumes ep0 uses the same value for both speeds ... */
1343
	qual.bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
L
Linus Torvalds 已提交
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355

	qual.bNumConfigurations = 1;
	qual.bRESERVED = 0;

	memcpy (dev->rbuf, &qual, sizeof qual);
}
#endif

static int
config_buf (struct dev_data *dev, u8 type, unsigned index)
{
	int		len;
1356
	int		hs = 0;
L
Linus Torvalds 已提交
1357 1358 1359 1360 1361

	/* only one configuration */
	if (index > 0)
		return -EINVAL;

1362 1363 1364 1365 1366
	if (gadget_is_dualspeed(dev->gadget)) {
		hs = (dev->gadget->speed == USB_SPEED_HIGH);
		if (type == USB_DT_OTHER_SPEED_CONFIG)
			hs = !hs;
	}
L
Linus Torvalds 已提交
1367 1368
	if (hs) {
		dev->req->buf = dev->hs_config;
1369
		len = le16_to_cpu(dev->hs_config->wTotalLength);
1370
	} else {
L
Linus Torvalds 已提交
1371
		dev->req->buf = dev->config;
1372
		len = le16_to_cpu(dev->config->wTotalLength);
L
Linus Torvalds 已提交
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
	}
	((u8 *)dev->req->buf) [1] = type;
	return len;
}

static int
gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
{
	struct dev_data			*dev = get_gadget_data (gadget);
	struct usb_request		*req = dev->req;
	int				value = -EOPNOTSUPP;
	struct usb_gadgetfs_event	*event;
1385 1386
	u16				w_value = le16_to_cpu(ctrl->wValue);
	u16				w_length = le16_to_cpu(ctrl->wLength);
L
Linus Torvalds 已提交
1387 1388 1389

	spin_lock (&dev->lock);
	dev->setup_abort = 0;
D
David Brownell 已提交
1390
	if (dev->state == STATE_DEV_UNCONNECTED) {
1391 1392 1393
		if (gadget_is_dualspeed(gadget)
				&& gadget->speed == USB_SPEED_HIGH
				&& dev->hs_config == NULL) {
1394
			spin_unlock(&dev->lock);
L
Linus Torvalds 已提交
1395 1396 1397 1398
			ERROR (dev, "no high speed config??\n");
			return -EINVAL;
		}

1399 1400
		dev->state = STATE_DEV_CONNECTED;

L
Linus Torvalds 已提交
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
		INFO (dev, "connected\n");
		event = next_event (dev, GADGETFS_CONNECT);
		event->u.speed = gadget->speed;
		ep0_readable (dev);

	/* host may have given up waiting for response.  we can miss control
	 * requests handled lower down (device/endpoint status and features);
	 * then ep0_{read,write} will report the wrong status. controller
	 * driver will have aborted pending i/o.
	 */
D
David Brownell 已提交
1411
	} else if (dev->state == STATE_DEV_SETUP)
L
Linus Torvalds 已提交
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
		dev->setup_abort = 1;

	req->buf = dev->rbuf;
	req->dma = DMA_ADDR_INVALID;
	req->context = NULL;
	value = -EOPNOTSUPP;
	switch (ctrl->bRequest) {

	case USB_REQ_GET_DESCRIPTOR:
		if (ctrl->bRequestType != USB_DIR_IN)
			goto unrecognized;
		switch (w_value >> 8) {

		case USB_DT_DEVICE:
			value = min (w_length, (u16) sizeof *dev->dev);
1427
			dev->dev->bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
L
Linus Torvalds 已提交
1428 1429
			req->buf = dev->dev;
			break;
1430
#ifdef	CONFIG_USB_GADGET_DUALSPEED
L
Linus Torvalds 已提交
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
		case USB_DT_DEVICE_QUALIFIER:
			if (!dev->hs_config)
				break;
			value = min (w_length, (u16)
				sizeof (struct usb_qualifier_descriptor));
			make_qualifier (dev);
			break;
		case USB_DT_OTHER_SPEED_CONFIG:
			// FALLTHROUGH
#endif
		case USB_DT_CONFIG:
			value = config_buf (dev,
					w_value >> 8,
					w_value & 0xff);
			if (value >= 0)
				value = min (w_length, (u16) value);
			break;
		case USB_DT_STRING:
			goto unrecognized;

		default:		// all others are errors
			break;
		}
		break;

	/* currently one config, two speeds */
	case USB_REQ_SET_CONFIGURATION:
		if (ctrl->bRequestType != 0)
1459
			goto unrecognized;
L
Linus Torvalds 已提交
1460 1461 1462 1463 1464 1465 1466
		if (0 == (u8) w_value) {
			value = 0;
			dev->current_config = 0;
			usb_gadget_vbus_draw(gadget, 8 /* mA */ );
			// user mode expected to disable endpoints
		} else {
			u8	config, power;
1467 1468 1469

			if (gadget_is_dualspeed(gadget)
					&& gadget->speed == USB_SPEED_HIGH) {
L
Linus Torvalds 已提交
1470 1471
				config = dev->hs_config->bConfigurationValue;
				power = dev->hs_config->bMaxPower;
1472
			} else {
L
Linus Torvalds 已提交
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
				config = dev->config->bConfigurationValue;
				power = dev->config->bMaxPower;
			}

			if (config == (u8) w_value) {
				value = 0;
				dev->current_config = config;
				usb_gadget_vbus_draw(gadget, 2 * power);
			}
		}

		/* report SET_CONFIGURATION like any other control request,
		 * except that usermode may not stall this.  the next
		 * request mustn't be allowed start until this finishes:
		 * endpoints and threads set up, etc.
		 *
		 * NOTE:  older PXA hardware (before PXA 255: without UDCCFR)
		 * has bad/racey automagic that prevents synchronizing here.
		 * even kernel mode drivers often miss them.
		 */
		if (value == 0) {
			INFO (dev, "configuration #%d\n", dev->current_config);
			if (dev->usermode_setup) {
				dev->setup_can_stall = 0;
				goto delegate;
			}
		}
		break;

1502
#ifndef	CONFIG_USB_GADGET_PXA25X
L
Linus Torvalds 已提交
1503 1504 1505
	/* PXA automagically handles this request too */
	case USB_REQ_GET_CONFIGURATION:
		if (ctrl->bRequestType != 0x80)
1506
			goto unrecognized;
L
Linus Torvalds 已提交
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
		*(u8 *)req->buf = dev->current_config;
		value = min (w_length, (u16) 1);
		break;
#endif

	default:
unrecognized:
		VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
			dev->usermode_setup ? "delegate" : "fail",
			ctrl->bRequestType, ctrl->bRequest,
			w_value, le16_to_cpu(ctrl->wIndex), w_length);

		/* if there's an ep0 reader, don't stall */
		if (dev->usermode_setup) {
			dev->setup_can_stall = 1;
delegate:
			dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
						? 1 : 0;
1525
			dev->setup_wLength = w_length;
L
Linus Torvalds 已提交
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
			dev->setup_out_ready = 0;
			dev->setup_out_error = 0;
			value = 0;

			/* read DATA stage for OUT right away */
			if (unlikely (!dev->setup_in && w_length)) {
				value = setup_req (gadget->ep0, dev->req,
							w_length);
				if (value < 0)
					break;
				value = usb_ep_queue (gadget->ep0, dev->req,
							GFP_ATOMIC);
				if (value < 0) {
					clean_req (gadget->ep0, dev->req);
					break;
				}

				/* we can't currently stall these */
				dev->setup_can_stall = 0;
			}

			/* state changes when reader collects event */
			event = next_event (dev, GADGETFS_SETUP);
			event->u.setup = *ctrl;
			ep0_readable (dev);
			spin_unlock (&dev->lock);
			return 0;
		}
	}

	/* proceed with data transfer and status phases? */
D
David Brownell 已提交
1557
	if (value >= 0 && dev->state != STATE_DEV_SETUP) {
L
Linus Torvalds 已提交
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
		req->length = value;
		req->zero = value < w_length;
		value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
		if (value < 0) {
			DBG (dev, "ep_queue --> %d\n", value);
			req->status = 0;
		}
	}

	/* device stalls when value < 0 */
	spin_unlock (&dev->lock);
	return value;
}

static void destroy_ep_files (struct dev_data *dev)
{
1574
	DBG (dev, "%s %d\n", __func__, dev->state);
L
Linus Torvalds 已提交
1575 1576 1577

	/* dev->state must prevent interference */
	spin_lock_irq (&dev->lock);
A
Al Viro 已提交
1578
	while (!list_empty(&dev->epfiles)) {
L
Linus Torvalds 已提交
1579 1580 1581 1582 1583
		struct ep_data	*ep;
		struct inode	*parent;
		struct dentry	*dentry;

		/* break link to FS */
A
Al Viro 已提交
1584
		ep = list_first_entry (&dev->epfiles, struct ep_data, epfiles);
L
Linus Torvalds 已提交
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
		list_del_init (&ep->epfiles);
		dentry = ep->dentry;
		ep->dentry = NULL;
		parent = dentry->d_parent->d_inode;

		/* break link to controller */
		if (ep->state == STATE_EP_ENABLED)
			(void) usb_ep_disable (ep->ep);
		ep->state = STATE_EP_UNBOUND;
		usb_ep_free_request (ep->ep, ep->req);
		ep->ep = NULL;
		wake_up (&ep->wait);
		put_ep (ep);

		spin_unlock_irq (&dev->lock);

		/* break link to dcache */
1602
		mutex_lock (&parent->i_mutex);
L
Linus Torvalds 已提交
1603 1604
		d_delete (dentry);
		dput (dentry);
1605
		mutex_unlock (&parent->i_mutex);
L
Linus Torvalds 已提交
1606

A
Al Viro 已提交
1607
		spin_lock_irq (&dev->lock);
L
Linus Torvalds 已提交
1608 1609 1610 1611 1612 1613 1614
	}
	spin_unlock_irq (&dev->lock);
}


static struct inode *
gadgetfs_create_file (struct super_block *sb, char const *name,
1615
		void *data, const struct file_operations *fops,
L
Linus Torvalds 已提交
1616 1617 1618 1619 1620
		struct dentry **dentry_p);

static int activate_ep_files (struct dev_data *dev)
{
	struct usb_ep	*ep;
A
Alan Stern 已提交
1621
	struct ep_data	*data;
L
Linus Torvalds 已提交
1622 1623 1624

	gadget_for_each_ep (ep, dev->gadget) {

1625
		data = kzalloc(sizeof(*data), GFP_KERNEL);
L
Linus Torvalds 已提交
1626
		if (!data)
A
Alan Stern 已提交
1627
			goto enomem0;
L
Linus Torvalds 已提交
1628
		data->state = STATE_EP_DISABLED;
1629
		mutex_init(&data->lock);
L
Linus Torvalds 已提交
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
		init_waitqueue_head (&data->wait);

		strncpy (data->name, ep->name, sizeof (data->name) - 1);
		atomic_set (&data->count, 1);
		data->dev = dev;
		get_dev (dev);

		data->ep = ep;
		ep->driver_data = data;

		data->req = usb_ep_alloc_request (ep, GFP_KERNEL);
		if (!data->req)
A
Alan Stern 已提交
1642
			goto enomem1;
L
Linus Torvalds 已提交
1643 1644 1645 1646

		data->inode = gadgetfs_create_file (dev->sb, data->name,
				data, &ep_config_operations,
				&data->dentry);
A
Alan Stern 已提交
1647 1648
		if (!data->inode)
			goto enomem2;
L
Linus Torvalds 已提交
1649 1650 1651 1652
		list_add_tail (&data->epfiles, &dev->epfiles);
	}
	return 0;

A
Alan Stern 已提交
1653 1654 1655 1656 1657 1658
enomem2:
	usb_ep_free_request (ep, data->req);
enomem1:
	put_dev (dev);
	kfree (data);
enomem0:
1659
	DBG (dev, "%s enomem\n", __func__);
L
Linus Torvalds 已提交
1660 1661 1662 1663 1664 1665 1666 1667 1668
	destroy_ep_files (dev);
	return -ENOMEM;
}

static void
gadgetfs_unbind (struct usb_gadget *gadget)
{
	struct dev_data		*dev = get_gadget_data (gadget);

1669
	DBG (dev, "%s\n", __func__);
L
Linus Torvalds 已提交
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681

	spin_lock_irq (&dev->lock);
	dev->state = STATE_DEV_UNBOUND;
	spin_unlock_irq (&dev->lock);

	destroy_ep_files (dev);
	gadget->ep0->driver_data = NULL;
	set_gadget_data (gadget, NULL);

	/* we've already been disconnected ... no i/o is active */
	if (dev->req)
		usb_ep_free_request (gadget->ep0, dev->req);
1682
	DBG (dev, "%s done\n", __func__);
L
Linus Torvalds 已提交
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
	put_dev (dev);
}

static struct dev_data		*the_device;

static int
gadgetfs_bind (struct usb_gadget *gadget)
{
	struct dev_data		*dev = the_device;

	if (!dev)
		return -ESRCH;
	if (0 != strcmp (CHIP, gadget->name)) {
1696
		pr_err("%s expected %s controller not %s\n",
L
Linus Torvalds 已提交
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
			shortname, CHIP, gadget->name);
		return -ENODEV;
	}

	set_gadget_data (gadget, dev);
	dev->gadget = gadget;
	gadget->ep0->driver_data = dev;

	/* preallocate control response and buffer */
	dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
	if (!dev->req)
		goto enomem;
	dev->req->context = NULL;
	dev->req->complete = epio_complete;

	if (activate_ep_files (dev) < 0)
		goto enomem;

	INFO (dev, "bound to %s driver\n", gadget->name);
D
David Brownell 已提交
1716 1717 1718
	spin_lock_irq(&dev->lock);
	dev->state = STATE_DEV_UNCONNECTED;
	spin_unlock_irq(&dev->lock);
L
Linus Torvalds 已提交
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
	get_dev (dev);
	return 0;

enomem:
	gadgetfs_unbind (gadget);
	return -ENOMEM;
}

static void
gadgetfs_disconnect (struct usb_gadget *gadget)
{
	struct dev_data		*dev = get_gadget_data (gadget);
1731
	unsigned long		flags;
L
Linus Torvalds 已提交
1732

1733
	spin_lock_irqsave (&dev->lock, flags);
D
David Brownell 已提交
1734
	if (dev->state == STATE_DEV_UNCONNECTED)
1735
		goto exit;
D
David Brownell 已提交
1736
	dev->state = STATE_DEV_UNCONNECTED;
L
Linus Torvalds 已提交
1737 1738 1739 1740

	INFO (dev, "disconnected\n");
	next_event (dev, GADGETFS_DISCONNECT);
	ep0_readable (dev);
1741
exit:
1742
	spin_unlock_irqrestore (&dev->lock, flags);
L
Linus Torvalds 已提交
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
}

static void
gadgetfs_suspend (struct usb_gadget *gadget)
{
	struct dev_data		*dev = get_gadget_data (gadget);

	INFO (dev, "suspended from state %d\n", dev->state);
	spin_lock (&dev->lock);
	switch (dev->state) {
D
David Brownell 已提交
1753 1754 1755
	case STATE_DEV_SETUP:		// VERY odd... host died??
	case STATE_DEV_CONNECTED:
	case STATE_DEV_UNCONNECTED:
L
Linus Torvalds 已提交
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
		next_event (dev, GADGETFS_SUSPEND);
		ep0_readable (dev);
		/* FALLTHROUGH */
	default:
		break;
	}
	spin_unlock (&dev->lock);
}

static struct usb_gadget_driver gadgetfs_driver = {
1766
#ifdef	CONFIG_USB_GADGET_DUALSPEED
1767
	.max_speed	= USB_SPEED_HIGH,
L
Linus Torvalds 已提交
1768
#else
1769
	.max_speed	= USB_SPEED_FULL,
L
Linus Torvalds 已提交
1770 1771 1772 1773 1774 1775 1776
#endif
	.function	= (char *) driver_desc,
	.unbind		= gadgetfs_unbind,
	.setup		= gadgetfs_setup,
	.disconnect	= gadgetfs_disconnect,
	.suspend	= gadgetfs_suspend,

1777
	.driver	= {
L
Linus Torvalds 已提交
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
		.name		= (char *) shortname,
	},
};

/*----------------------------------------------------------------------*/

static void gadgetfs_nop(struct usb_gadget *arg) { }

static int gadgetfs_probe (struct usb_gadget *gadget)
{
	CHIP = gadget->name;
	return -EISNAM;
}

static struct usb_gadget_driver probe_driver = {
1793
	.max_speed	= USB_SPEED_HIGH,
L
Linus Torvalds 已提交
1794 1795 1796
	.unbind		= gadgetfs_nop,
	.setup		= (void *)gadgetfs_nop,
	.disconnect	= gadgetfs_nop,
1797
	.driver	= {
L
Linus Torvalds 已提交
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
		.name		= "nop",
	},
};


/* DEVICE INITIALIZATION
 *
 *     fd = open ("/dev/gadget/$CHIP", O_RDWR)
 *     status = write (fd, descriptors, sizeof descriptors)
 *
 * That write establishes the device configuration, so the kernel can
 * bind to the controller ... guaranteeing it can handle enumeration
 * at all necessary speeds.  Descriptor order is:
 *
 * . message tag (u32, host order) ... for now, must be zero; it
 *	would change to support features like multi-config devices
 * . full/low speed config ... all wTotalLength bytes (with interface,
 *	class, altsetting, endpoint, and other descriptors)
 * . high speed config ... all descriptors, for high speed operation;
1817
 *	this one's optional except for high-speed hardware
L
Linus Torvalds 已提交
1818 1819
 * . device descriptor
 *
1820 1821
 * Endpoints are not yet enabled. Drivers must wait until device
 * configuration and interface altsetting changes create
L
Linus Torvalds 已提交
1822 1823 1824
 * the need to configure (or unconfigure) them.
 *
 * After initialization, the device stays active for as long as that
1825 1826
 * $CHIP file is open.  Events must then be read from that descriptor,
 * such as configuration notifications.
L
Linus Torvalds 已提交
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
 */

static int is_valid_config (struct usb_config_descriptor *config)
{
	return config->bDescriptorType == USB_DT_CONFIG
		&& config->bLength == USB_DT_CONFIG_SIZE
		&& config->bConfigurationValue != 0
		&& (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
		&& (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
	/* FIXME if gadget->is_otg, _must_ include an otg descriptor */
	/* FIXME check lengths: walk to end */
}

static ssize_t
dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
{
	struct dev_data		*dev = fd->private_data;
	ssize_t			value = len, length = len;
	unsigned		total;
	u32			tag;
	char			*kbuf;

	if (len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4))
		return -EINVAL;

	/* we might need to change message format someday */
	if (copy_from_user (&tag, buf, 4))
		return -EFAULT;
	if (tag != 0)
		return -EINVAL;
	buf += 4;
	length -= 4;

J
Julia Lawall 已提交
1860 1861 1862
	kbuf = memdup_user(buf, length);
	if (IS_ERR(kbuf))
		return PTR_ERR(kbuf);
L
Linus Torvalds 已提交
1863 1864 1865 1866 1867 1868 1869 1870 1871

	spin_lock_irq (&dev->lock);
	value = -EINVAL;
	if (dev->buf)
		goto fail;
	dev->buf = kbuf;

	/* full or low speed config */
	dev->config = (void *) kbuf;
1872
	total = le16_to_cpu(dev->config->wTotalLength);
L
Linus Torvalds 已提交
1873 1874 1875 1876 1877 1878 1879 1880
	if (!is_valid_config (dev->config) || total >= length)
		goto fail;
	kbuf += total;
	length -= total;

	/* optional high speed config */
	if (kbuf [1] == USB_DT_CONFIG) {
		dev->hs_config = (void *) kbuf;
1881
		total = le16_to_cpu(dev->hs_config->wTotalLength);
L
Linus Torvalds 已提交
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
		if (!is_valid_config (dev->hs_config) || total >= length)
			goto fail;
		kbuf += total;
		length -= total;
	}

	/* could support multiple configs, using another encoding! */

	/* device descriptor (tweaked for paranoia) */
	if (length != USB_DT_DEVICE_SIZE)
		goto fail;
	dev->dev = (void *)kbuf;
	if (dev->dev->bLength != USB_DT_DEVICE_SIZE
			|| dev->dev->bDescriptorType != USB_DT_DEVICE
			|| dev->dev->bNumConfigurations != 1)
		goto fail;
	dev->dev->bNumConfigurations = 1;
1899
	dev->dev->bcdUSB = cpu_to_le16 (0x0200);
L
Linus Torvalds 已提交
1900 1901 1902

	/* triggers gadgetfs_bind(); then we can enumerate. */
	spin_unlock_irq (&dev->lock);
1903
	value = usb_gadget_probe_driver(&gadgetfs_driver, gadgetfs_bind);
L
Linus Torvalds 已提交
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
	if (value != 0) {
		kfree (dev->buf);
		dev->buf = NULL;
	} else {
		/* at this point "good" hardware has for the first time
		 * let the USB the host see us.  alternatively, if users
		 * unplug/replug that will clear all the error state.
		 *
		 * note:  everything running before here was guaranteed
		 * to choke driver model style diagnostics.  from here
		 * on, they can work ... except in cleanup paths that
		 * kick in after the ep0 descriptor is closed.
		 */
		fd->f_op = &ep0_io_operations;
		value = len;
	}
	return value;

fail:
	spin_unlock_irq (&dev->lock);
1924
	pr_debug ("%s: %s fail %Zd, %p\n", shortname, __func__, value, dev);
L
Linus Torvalds 已提交
1925 1926 1927 1928 1929 1930 1931 1932
	kfree (dev->buf);
	dev->buf = NULL;
	return value;
}

static int
dev_open (struct inode *inode, struct file *fd)
{
1933
	struct dev_data		*dev = inode->i_private;
L
Linus Torvalds 已提交
1934 1935
	int			value = -EBUSY;

D
David Brownell 已提交
1936
	spin_lock_irq(&dev->lock);
L
Linus Torvalds 已提交
1937 1938
	if (dev->state == STATE_DEV_DISABLED) {
		dev->ev_next = 0;
D
David Brownell 已提交
1939
		dev->state = STATE_DEV_OPENED;
L
Linus Torvalds 已提交
1940 1941 1942 1943
		fd->private_data = dev;
		get_dev (dev);
		value = 0;
	}
D
David Brownell 已提交
1944
	spin_unlock_irq(&dev->lock);
L
Linus Torvalds 已提交
1945 1946 1947
	return value;
}

1948
static const struct file_operations dev_init_operations = {
L
Linus Torvalds 已提交
1949 1950 1951 1952 1953 1954
	.owner =	THIS_MODULE,
	.llseek =	no_llseek,

	.open =		dev_open,
	.write =	dev_config,
	.fasync =	ep0_fasync,
1955
	.unlocked_ioctl = dev_ioctl,
L
Linus Torvalds 已提交
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
	.release =	dev_release,
};

/*----------------------------------------------------------------------*/

/* FILESYSTEM AND SUPERBLOCK OPERATIONS
 *
 * Mounting the filesystem creates a controller file, used first for
 * device configuration then later for event monitoring.
 */


/* FIXME PAM etc could set this security policy without mount options
 * if epfiles inherited ownership and permissons from ep0 ...
 */

static unsigned default_uid;
static unsigned default_gid;
static unsigned default_perm = S_IRUSR | S_IWUSR;

module_param (default_uid, uint, 0644);
module_param (default_gid, uint, 0644);
module_param (default_perm, uint, 0644);


static struct inode *
gadgetfs_make_inode (struct super_block *sb,
1983
		void *data, const struct file_operations *fops,
L
Linus Torvalds 已提交
1984 1985 1986 1987 1988
		int mode)
{
	struct inode *inode = new_inode (sb);

	if (inode) {
1989
		inode->i_ino = get_next_ino();
L
Linus Torvalds 已提交
1990 1991 1992 1993 1994
		inode->i_mode = mode;
		inode->i_uid = default_uid;
		inode->i_gid = default_gid;
		inode->i_atime = inode->i_mtime = inode->i_ctime
				= CURRENT_TIME;
1995
		inode->i_private = data;
L
Linus Torvalds 已提交
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
		inode->i_fop = fops;
	}
	return inode;
}

/* creates in fs root directory, so non-renamable and non-linkable.
 * so inode and dentry are paired, until device reconfig.
 */
static struct inode *
gadgetfs_create_file (struct super_block *sb, char const *name,
2006
		void *data, const struct file_operations *fops,
L
Linus Torvalds 已提交
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
		struct dentry **dentry_p)
{
	struct dentry	*dentry;
	struct inode	*inode;

	dentry = d_alloc_name(sb->s_root, name);
	if (!dentry)
		return NULL;

	inode = gadgetfs_make_inode (sb, data, fops,
			S_IFREG | (default_perm & S_IRWXUGO));
	if (!inode) {
		dput(dentry);
		return NULL;
	}
	d_add (dentry, inode);
	*dentry_p = dentry;
	return inode;
}

2027
static const struct super_operations gadget_fs_operations = {
L
Linus Torvalds 已提交
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
	.statfs =	simple_statfs,
	.drop_inode =	generic_delete_inode,
};

static int
gadgetfs_fill_super (struct super_block *sb, void *opts, int silent)
{
	struct inode	*inode;
	struct dev_data	*dev;

	if (the_device)
		return -ESRCH;

	/* fake probe to determine $CHIP */
2042
	(void) usb_gadget_probe_driver(&probe_driver, gadgetfs_probe);
L
Linus Torvalds 已提交
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
	if (!CHIP)
		return -ENODEV;

	/* superblock */
	sb->s_blocksize = PAGE_CACHE_SIZE;
	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
	sb->s_magic = GADGETFS_MAGIC;
	sb->s_op = &gadget_fs_operations;
	sb->s_time_gran = 1;

	/* root inode */
	inode = gadgetfs_make_inode (sb,
			NULL, &simple_dir_operations,
			S_IFDIR | S_IRUGO | S_IXUGO);
	if (!inode)
A
Al Viro 已提交
2058
		goto Enomem;
L
Linus Torvalds 已提交
2059
	inode->i_op = &simple_dir_inode_operations;
2060
	if (!(sb->s_root = d_make_root (inode)))
A
Al Viro 已提交
2061
		goto Enomem;
L
Linus Torvalds 已提交
2062 2063 2064 2065 2066 2067

	/* the ep0 file is named after the controller we expect;
	 * user mode code can use it for sanity checks, like we do.
	 */
	dev = dev_new ();
	if (!dev)
A
Al Viro 已提交
2068
		goto Enomem;
L
Linus Torvalds 已提交
2069 2070

	dev->sb = sb;
A
Alan Stern 已提交
2071
	if (!gadgetfs_create_file (sb, CHIP,
L
Linus Torvalds 已提交
2072
				dev, &dev_init_operations,
A
Al Viro 已提交
2073 2074 2075 2076
				&dev->dentry)) {
		put_dev(dev);
		goto Enomem;
	}
L
Linus Torvalds 已提交
2077 2078 2079 2080 2081 2082

	/* other endpoint files are available after hardware setup,
	 * from binding to a controller.
	 */
	the_device = dev;
	return 0;
A
Alan Stern 已提交
2083

A
Al Viro 已提交
2084
Enomem:
A
Alan Stern 已提交
2085
	return -ENOMEM;
L
Linus Torvalds 已提交
2086 2087 2088
}

/* "mount -t gadgetfs path /dev/gadget" ends up here */
A
Al Viro 已提交
2089 2090 2091
static struct dentry *
gadgetfs_mount (struct file_system_type *t, int flags,
		const char *path, void *opts)
L
Linus Torvalds 已提交
2092
{
A
Al Viro 已提交
2093
	return mount_single (t, flags, opts, gadgetfs_fill_super);
L
Linus Torvalds 已提交
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110
}

static void
gadgetfs_kill_sb (struct super_block *sb)
{
	kill_litter_super (sb);
	if (the_device) {
		put_dev (the_device);
		the_device = NULL;
	}
}

/*----------------------------------------------------------------------*/

static struct file_system_type gadgetfs_type = {
	.owner		= THIS_MODULE,
	.name		= shortname,
A
Al Viro 已提交
2111
	.mount		= gadgetfs_mount,
L
Linus Torvalds 已提交
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
	.kill_sb	= gadgetfs_kill_sb,
};

/*----------------------------------------------------------------------*/

static int __init init (void)
{
	int status;

	status = register_filesystem (&gadgetfs_type);
	if (status == 0)
		pr_info ("%s: %s, version " DRIVER_VERSION "\n",
			shortname, driver_desc);
	return status;
}
module_init (init);

static void __exit cleanup (void)
{
	pr_debug ("unregister %s\n", shortname);
	unregister_filesystem (&gadgetfs_type);
}
module_exit (cleanup);