zero.c 33.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
/*
 * zero.c -- Gadget Zero, for USB development
 *
4
 * Copyright (C) 2003-2007 David Brownell
Linus Torvalds's avatar
Linus Torvalds committed
5
6
 * All rights reserved.
 *
7
8
9
10
 * 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.
Linus Torvalds's avatar
Linus Torvalds committed
11
 *
12
13
14
15
 * This program 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.
Linus Torvalds's avatar
Linus Torvalds committed
16
 *
17
18
19
 * 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
Linus Torvalds's avatar
Linus Torvalds committed
20
21
22
23
24
25
 */


/*
 * Gadget Zero only needs two bulk endpoints, and is an example of how you
 * can write a hardware-agnostic gadget driver running inside a USB device.
26
 * Some hardware details are visible, but don't affect most of the driver.
Linus Torvalds's avatar
Linus Torvalds committed
27
28
29
30
31
32
33
34
35
36
37
 *
 * Use it with the Linux host/master side "usbtest" driver to get a basic
 * functional test of your device-side usb stack, or with "usb-skeleton".
 *
 * It supports two similar configurations.  One sinks whatever the usb host
 * writes, and in return sources zeroes.  The other loops whatever the host
 * writes back, so the host can read it.  Module options include:
 *
 *   buflen=N		default N=4096, buffer size used
 *   qlen=N		default N=32, how many buffers in the loopback queue
 *   loopdefault	default false, list loopback config first
38
 *   autoresume=N	default N=0, seconds before triggering remote wakeup
Linus Torvalds's avatar
Linus Torvalds committed
39
40
41
42
 *
 * Many drivers will only have one configuration, letting them be much
 * simpler if they also don't support high speed operation (like this
 * driver does).
43
44
45
46
47
 *
 * Why is *this* driver using two configurations, rather than setting up
 * two interfaces with different functions?  To help verify that multiple
 * configuration infrastucture is working correctly; also, so that it can
 * work with low capability USB controllers without four bulk endpoints.
Linus Torvalds's avatar
Linus Torvalds committed
48
49
 */

50
/* #define VERBOSE_DEBUG */
Linus Torvalds's avatar
Linus Torvalds committed
51
52
53
54
55

#include <linux/kernel.h>
#include <linux/utsname.h>
#include <linux/device.h>

56
#include <linux/usb/ch9.h>
57
#include <linux/usb/gadget.h>
Linus Torvalds's avatar
Linus Torvalds committed
58
59
60
61
62
63

#include "gadget_chips.h"


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

64
#define DRIVER_VERSION		"Earth Day 2008"
Linus Torvalds's avatar
Linus Torvalds committed
65

66
67
static const char shortname[] = "zero";
static const char longname[] = "Gadget Zero";
Linus Torvalds's avatar
Linus Torvalds committed
68

69
70
static const char source_sink[] = "source and sink data";
static const char loopback[] = "loop input to output";
Linus Torvalds's avatar
Linus Torvalds committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

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

/*
 * driver assumes self-powered hardware, and
 * has no way for users to trigger remote wakeup.
 *
 * this version autoconfigures as much as possible,
 * which is reasonable for most "bulk-only" drivers.
 */
static const char *EP_IN_NAME;		/* source */
static const char *EP_OUT_NAME;		/* sink */

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

/* big enough to hold our biggest descriptor */
#define USB_BUFSIZ	256

struct zero_dev {
	spinlock_t		lock;
	struct usb_gadget	*gadget;
	struct usb_request	*req;		/* for control responses */

	/* when configured, we have one of two configs:
	 * - source data (in to host) and sink it (out from host)
	 * - or loop it back (out from host back in to host)
	 */
	u8			config;
	struct usb_ep		*in_ep, *out_ep;

	/* autoresume timer */
	struct timer_list	resume;
};

105
106
107
108
109
110
111
112
113
114
#define DBG(d, fmt, args...) \
	dev_dbg(&(d)->gadget->dev , fmt , ## args)
#define VDBG(d, fmt, args...) \
	dev_vdbg(&(d)->gadget->dev , fmt , ## args)
#define ERROR(d, fmt, args...) \
	dev_err(&(d)->gadget->dev , fmt , ## args)
#define WARN(d, fmt, args...) \
	dev_warn(&(d)->gadget->dev , fmt , ## args)
#define INFO(d, fmt, args...) \
	dev_info(&(d)->gadget->dev , fmt , ## args)
Linus Torvalds's avatar
Linus Torvalds committed
115
116
117
118
119
120
121

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

static unsigned buflen = 4096;
static unsigned qlen = 32;
static unsigned pattern = 0;

122
123
124
module_param(buflen, uint, S_IRUGO);
module_param(qlen, uint, S_IRUGO);
module_param(pattern, uint, S_IRUGO|S_IWUSR);
Linus Torvalds's avatar
Linus Torvalds committed
125
126
127
128
129
130

/*
 * if it's nonzero, autoresume says how many seconds to wait
 * before trying to wake up the host after suspend.
 */
static unsigned autoresume = 0;
131
module_param(autoresume, uint, 0);
Linus Torvalds's avatar
Linus Torvalds committed
132
133
134
135
136
137
138
139

/*
 * Normally the "loopback" configuration is second (index 1) so
 * it's not the default.  Here's where to change that order, to
 * work better with hosts where config changes are problematic.
 * Or controllers (like superh) that only support one config.
 */
static int loopdefault = 0;
140
module_param(loopdefault, bool, S_IRUGO|S_IWUSR);
Linus Torvalds's avatar
Linus Torvalds committed
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
166
167
168
169
170
171
172
173
174
175
176

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

/* Thanks to NetChip Technologies for donating this product ID.
 *
 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
 * Instead:  allocate your own, using normal USB-IF procedures.
 */
#ifndef	CONFIG_USB_ZERO_HNPTEST
#define DRIVER_VENDOR_NUM	0x0525		/* NetChip */
#define DRIVER_PRODUCT_NUM	0xa4a0		/* Linux-USB "Gadget Zero" */
#else
#define DRIVER_VENDOR_NUM	0x1a0a		/* OTG test device IDs */
#define DRIVER_PRODUCT_NUM	0xbadd
#endif

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

/*
 * DESCRIPTORS ... most are static, but strings and (full)
 * configuration descriptors are built on demand.
 */

#define STRING_MANUFACTURER		25
#define STRING_PRODUCT			42
#define STRING_SERIAL			101
#define STRING_SOURCE_SINK		250
#define STRING_LOOPBACK			251

/*
 * This device advertises two configurations; these numbers work
 * on a pxa250 as well as more flexible hardware.
 */
#define	CONFIG_SOURCE_SINK	3
#define	CONFIG_LOOPBACK		2

177
static struct usb_device_descriptor device_desc = {
Linus Torvalds's avatar
Linus Torvalds committed
178
179
180
	.bLength =		sizeof device_desc,
	.bDescriptorType =	USB_DT_DEVICE,

181
	.bcdUSB =		__constant_cpu_to_le16(0x0200),
Linus Torvalds's avatar
Linus Torvalds committed
182
183
	.bDeviceClass =		USB_CLASS_VENDOR_SPEC,

184
185
	.idVendor =		__constant_cpu_to_le16(DRIVER_VENDOR_NUM),
	.idProduct =		__constant_cpu_to_le16(DRIVER_PRODUCT_NUM),
Linus Torvalds's avatar
Linus Torvalds committed
186
187
188
189
190
191
	.iManufacturer =	STRING_MANUFACTURER,
	.iProduct =		STRING_PRODUCT,
	.iSerialNumber =	STRING_SERIAL,
	.bNumConfigurations =	2,
};

192
static struct usb_config_descriptor source_sink_config = {
Linus Torvalds's avatar
Linus Torvalds committed
193
194
195
196
197
198
199
200
201
202
203
	.bLength =		sizeof source_sink_config,
	.bDescriptorType =	USB_DT_CONFIG,

	/* compute wTotalLength on the fly */
	.bNumInterfaces =	1,
	.bConfigurationValue =	CONFIG_SOURCE_SINK,
	.iConfiguration =	STRING_SOURCE_SINK,
	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
	.bMaxPower =		1,	/* self-powered */
};

204
static struct usb_config_descriptor loopback_config = {
Linus Torvalds's avatar
Linus Torvalds committed
205
206
207
208
209
210
211
212
213
214
215
	.bLength =		sizeof loopback_config,
	.bDescriptorType =	USB_DT_CONFIG,

	/* compute wTotalLength on the fly */
	.bNumInterfaces =	1,
	.bConfigurationValue =	CONFIG_LOOPBACK,
	.iConfiguration =	STRING_LOOPBACK,
	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
	.bMaxPower =		1,	/* self-powered */
};

216
static struct usb_otg_descriptor otg_descriptor = {
Linus Torvalds's avatar
Linus Torvalds committed
217
218
219
220
221
222
223
224
	.bLength =		sizeof otg_descriptor,
	.bDescriptorType =	USB_DT_OTG,

	.bmAttributes =		USB_OTG_SRP,
};

/* one interface in each configuration */

225
static const struct usb_interface_descriptor source_sink_intf = {
Linus Torvalds's avatar
Linus Torvalds committed
226
227
228
229
230
231
232
233
	.bLength =		sizeof source_sink_intf,
	.bDescriptorType =	USB_DT_INTERFACE,

	.bNumEndpoints =	2,
	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC,
	.iInterface =		STRING_SOURCE_SINK,
};

234
static const struct usb_interface_descriptor loopback_intf = {
Linus Torvalds's avatar
Linus Torvalds committed
235
236
237
238
239
240
241
242
243
244
	.bLength =		sizeof loopback_intf,
	.bDescriptorType =	USB_DT_INTERFACE,

	.bNumEndpoints =	2,
	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC,
	.iInterface =		STRING_LOOPBACK,
};

/* two full speed bulk endpoints; their use is config-dependent */

245
static struct usb_endpoint_descriptor fs_source_desc = {
Linus Torvalds's avatar
Linus Torvalds committed
246
247
248
249
250
251
252
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,

	.bEndpointAddress =	USB_DIR_IN,
	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
};

253
static struct usb_endpoint_descriptor fs_sink_desc = {
Linus Torvalds's avatar
Linus Torvalds committed
254
255
256
257
258
259
260
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,

	.bEndpointAddress =	USB_DIR_OUT,
	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
};

261
static const struct usb_descriptor_header *fs_source_sink_function[] = {
Linus Torvalds's avatar
Linus Torvalds committed
262
263
264
265
266
267
268
	(struct usb_descriptor_header *) &otg_descriptor,
	(struct usb_descriptor_header *) &source_sink_intf,
	(struct usb_descriptor_header *) &fs_sink_desc,
	(struct usb_descriptor_header *) &fs_source_desc,
	NULL,
};

269
static const struct usb_descriptor_header *fs_loopback_function[] = {
Linus Torvalds's avatar
Linus Torvalds committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
	(struct usb_descriptor_header *) &otg_descriptor,
	(struct usb_descriptor_header *) &loopback_intf,
	(struct usb_descriptor_header *) &fs_sink_desc,
	(struct usb_descriptor_header *) &fs_source_desc,
	NULL,
};

/*
 * usb 2.0 devices need to expose both high speed and full speed
 * descriptors, unless they only run at full speed.
 *
 * that means alternate endpoint descriptors (bigger packets)
 * and a "device qualifier" ... plus more construction options
 * for the config descriptor.
 */

286
static struct usb_endpoint_descriptor hs_source_desc = {
Linus Torvalds's avatar
Linus Torvalds committed
287
288
289
290
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,

	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
291
	.wMaxPacketSize =	__constant_cpu_to_le16(512),
Linus Torvalds's avatar
Linus Torvalds committed
292
293
};

294
static struct usb_endpoint_descriptor hs_sink_desc = {
Linus Torvalds's avatar
Linus Torvalds committed
295
296
297
298
	.bLength =		USB_DT_ENDPOINT_SIZE,
	.bDescriptorType =	USB_DT_ENDPOINT,

	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
299
	.wMaxPacketSize =	__constant_cpu_to_le16(512),
Linus Torvalds's avatar
Linus Torvalds committed
300
301
};

302
static struct usb_qualifier_descriptor dev_qualifier = {
Linus Torvalds's avatar
Linus Torvalds committed
303
304
305
	.bLength =		sizeof dev_qualifier,
	.bDescriptorType =	USB_DT_DEVICE_QUALIFIER,

306
	.bcdUSB =		__constant_cpu_to_le16(0x0200),
Linus Torvalds's avatar
Linus Torvalds committed
307
308
309
310
311
	.bDeviceClass =		USB_CLASS_VENDOR_SPEC,

	.bNumConfigurations =	2,
};

312
static const struct usb_descriptor_header *hs_source_sink_function[] = {
Linus Torvalds's avatar
Linus Torvalds committed
313
314
315
316
317
318
319
	(struct usb_descriptor_header *) &otg_descriptor,
	(struct usb_descriptor_header *) &source_sink_intf,
	(struct usb_descriptor_header *) &hs_source_desc,
	(struct usb_descriptor_header *) &hs_sink_desc,
	NULL,
};

320
static const struct usb_descriptor_header *hs_loopback_function[] = {
Linus Torvalds's avatar
Linus Torvalds committed
321
322
323
324
325
326
327
328
	(struct usb_descriptor_header *) &otg_descriptor,
	(struct usb_descriptor_header *) &loopback_intf,
	(struct usb_descriptor_header *) &hs_source_desc,
	(struct usb_descriptor_header *) &hs_sink_desc,
	NULL,
};

/* maxpacket and other transfer characteristics vary by speed. */
329
330
331
332
333
334
335
336
static inline struct usb_endpoint_descriptor *
ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
		struct usb_endpoint_descriptor *fs)
{
	if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
		return hs;
	return fs;
}
Linus Torvalds's avatar
Linus Torvalds committed
337

338
static char manufacturer[50];
Linus Torvalds's avatar
Linus Torvalds committed
339

340
341
/* default serial number takes at least two packets */
static char serial[] = "0123456789.0123456789.0123456789";
Linus Torvalds's avatar
Linus Torvalds committed
342
343
344


/* static strings, in UTF-8 */
345
static struct usb_string strings[] = {
Linus Torvalds's avatar
Linus Torvalds committed
346
347
348
349
350
351
352
353
	{ STRING_MANUFACTURER, manufacturer, },
	{ STRING_PRODUCT, longname, },
	{ STRING_SERIAL, serial, },
	{ STRING_LOOPBACK, loopback, },
	{ STRING_SOURCE_SINK, source_sink, },
	{  }			/* end of list */
};

354
static struct usb_gadget_strings stringtab = {
Linus Torvalds's avatar
Linus Torvalds committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
	.language	= 0x0409,	/* en-us */
	.strings	= strings,
};

/*
 * config descriptors are also handcrafted.  these must agree with code
 * that sets configurations, and with code managing interfaces and their
 * altsettings.  other complexity may come from:
 *
 *  - high speed support, including "other speed config" rules
 *  - multiple configurations
 *  - interfaces with alternate settings
 *  - embedded class or vendor-specific descriptors
 *
 * this handles high speed, and has a second config that could as easily
 * have been an alternate interface setting (on most hardware).
 *
 * NOTE:  to demonstrate (and test) more USB capabilities, this driver
 * should include an altsetting to test interrupt transfers, including
 * high bandwidth modes at high speed.  (Maybe work like Intel's test
 * device?)
 */
377
static int config_buf(struct usb_gadget *gadget,
Linus Torvalds's avatar
Linus Torvalds committed
378
379
380
381
382
		u8 *buf, u8 type, unsigned index)
{
	int				is_source_sink;
	int				len;
	const struct usb_descriptor_header **function;
383
	int				hs = 0;
Linus Torvalds's avatar
Linus Torvalds committed
384
385
386
387
388
389

	/* two configurations will always be index 0 and index 1 */
	if (index > 1)
		return -EINVAL;
	is_source_sink = loopdefault ? (index == 1) : (index == 0);

390
391
392
393
394
	if (gadget_is_dualspeed(gadget)) {
		hs = (gadget->speed == USB_SPEED_HIGH);
		if (type == USB_DT_OTHER_SPEED_CONFIG)
			hs = !hs;
	}
Linus Torvalds's avatar
Linus Torvalds committed
395
396
397
398
399
400
401
402
403
404
	if (hs)
		function = is_source_sink
			? hs_source_sink_function
			: hs_loopback_function;
	else
		function = is_source_sink
			? fs_source_sink_function
			: fs_loopback_function;

	/* for now, don't advertise srp-only devices */
405
	if (!gadget_is_otg(gadget))
Linus Torvalds's avatar
Linus Torvalds committed
406
407
		function++;

408
	len = usb_gadget_config_buf(is_source_sink
Linus Torvalds's avatar
Linus Torvalds committed
409
410
411
412
413
414
415
416
417
418
419
					? &source_sink_config
					: &loopback_config,
			buf, USB_BUFSIZ, function);
	if (len < 0)
		return len;
	((struct usb_config_descriptor *) buf)->bDescriptorType = type;
	return len;
}

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

420
static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length)
Linus Torvalds's avatar
Linus Torvalds committed
421
422
423
{
	struct usb_request	*req;

424
	req = usb_ep_alloc_request(ep, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
425
426
	if (req) {
		req->length = length;
427
		req->buf = kmalloc(length, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
428
		if (!req->buf) {
429
			usb_ep_free_request(ep, req);
Linus Torvalds's avatar
Linus Torvalds committed
430
431
432
433
434
435
			req = NULL;
		}
	}
	return req;
}

436
static void free_ep_req(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds's avatar
Linus Torvalds committed
437
{
438
	kfree(req->buf);
439
	usb_ep_free_request(ep, req);
Linus Torvalds's avatar
Linus Torvalds committed
440
441
442
443
}

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

444
445
446
447
448
449
450
451
452
453
454
455
456
/*
 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripherals,
 * this just sinks bulk packets OUT to the peripheral and sources them IN
 * to the host, optionally with specific data patterns.
 *
 * In terms of control messaging, this supports all the standard requests
 * plus two that support control-OUT tests.
 *
 * Note that because this doesn't queue more than one request at a time,
 * some other function must be used to test queueing logic.  The network
 * link (g_ether) is probably the best option for that.
 */

Linus Torvalds's avatar
Linus Torvalds committed
457
458
459
/* optionally require specific source/sink data patterns  */

static int
460
check_read_data(
Linus Torvalds's avatar
Linus Torvalds committed
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
	struct zero_dev		*dev,
	struct usb_ep		*ep,
	struct usb_request	*req
)
{
	unsigned	i;
	u8		*buf = req->buf;

	for (i = 0; i < req->actual; i++, buf++) {
		switch (pattern) {
		/* all-zeroes has no synchronization issues */
		case 0:
			if (*buf == 0)
				continue;
			break;
		/* mod63 stays in sync with short-terminated transfers,
		 * or otherwise when host and gadget agree on how large
		 * each usb transfer request should be.  resync is done
		 * with set_interface or set_config.
		 */
		case 1:
			if (*buf == (u8)(i % 63))
				continue;
			break;
		}
486
487
		ERROR(dev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
		usb_ep_set_halt(ep);
Linus Torvalds's avatar
Linus Torvalds committed
488
489
490
491
492
		return -EINVAL;
	}
	return 0;
}

493
static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds's avatar
Linus Torvalds committed
494
495
496
497
498
499
{
	unsigned	i;
	u8		*buf = req->buf;

	switch (pattern) {
	case 0:
500
		memset(req->buf, 0, req->length);
Linus Torvalds's avatar
Linus Torvalds committed
501
502
503
504
505
506
507
508
509
510
511
512
		break;
	case 1:
		for  (i = 0; i < req->length; i++)
			*buf++ = (u8) (i % 63);
		break;
	}
}

/* if there is only one request in the queue, there'll always be an
 * irq delay between end of one request and start of the next.
 * that prevents using hardware dma queues.
 */
513
static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds's avatar
Linus Torvalds committed
514
515
516
517
518
519
{
	struct zero_dev	*dev = ep->driver_data;
	int		status = req->status;

	switch (status) {

520
	case 0:				/* normal completion? */
521
		if (ep == dev->out_ep) {
522
523
			check_read_data(dev, ep, req);
			memset(req->buf, 0x55, req->length);
524
		} else
525
			reinit_write_data(ep, req);
Linus Torvalds's avatar
Linus Torvalds committed
526
527
528
		break;

	/* this endpoint is normally active while we're configured */
529
	case -ECONNABORTED:		/* hardware forced ep reset */
Linus Torvalds's avatar
Linus Torvalds committed
530
531
	case -ECONNRESET:		/* request dequeued */
	case -ESHUTDOWN:		/* disconnect from host */
532
		VDBG(dev, "%s gone (%d), %d/%d\n", ep->name, status,
Linus Torvalds's avatar
Linus Torvalds committed
533
534
				req->actual, req->length);
		if (ep == dev->out_ep)
535
536
			check_read_data(dev, ep, req);
		free_ep_req(ep, req);
Linus Torvalds's avatar
Linus Torvalds committed
537
538
539
540
541
542
543
544
		return;

	case -EOVERFLOW:		/* buffer overrun on read means that
					 * we didn't provide a big enough
					 * buffer.
					 */
	default:
#if 1
545
		DBG(dev, "%s complete --> %d, %d/%d\n", ep->name,
Linus Torvalds's avatar
Linus Torvalds committed
546
547
548
549
550
551
				status, req->actual, req->length);
#endif
	case -EREMOTEIO:		/* short read */
		break;
	}

552
	status = usb_ep_queue(ep, req, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
553
	if (status) {
554
		ERROR(dev, "kill %s:  resubmit %d bytes --> %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
555
				ep->name, req->length, status);
556
		usb_ep_set_halt(ep);
Linus Torvalds's avatar
Linus Torvalds committed
557
558
559
560
		/* FIXME recover later ... somehow */
	}
}

561
static struct usb_request *source_sink_start_ep(struct usb_ep *ep)
Linus Torvalds's avatar
Linus Torvalds committed
562
563
564
565
{
	struct usb_request	*req;
	int			status;

566
	req = alloc_ep_req(ep, buflen);
Linus Torvalds's avatar
Linus Torvalds committed
567
568
569
	if (!req)
		return NULL;

570
	memset(req->buf, 0, req->length);
Linus Torvalds's avatar
Linus Torvalds committed
571
572
	req->complete = source_sink_complete;

573
	if (strcmp(ep->name, EP_IN_NAME) == 0)
574
		reinit_write_data(ep, req);
575
	else
576
		memset(req->buf, 0x55, req->length);
Linus Torvalds's avatar
Linus Torvalds committed
577

578
	status = usb_ep_queue(ep, req, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
579
580
581
	if (status) {
		struct zero_dev	*dev = ep->driver_data;

582
583
		ERROR(dev, "start %s --> %d\n", ep->name, status);
		free_ep_req(ep, req);
Linus Torvalds's avatar
Linus Torvalds committed
584
585
586
587
588
589
		req = NULL;
	}

	return req;
}

590
static int set_source_sink_config(struct zero_dev *dev)
Linus Torvalds's avatar
Linus Torvalds committed
591
592
593
594
595
{
	int			result = 0;
	struct usb_ep		*ep;
	struct usb_gadget	*gadget = dev->gadget;

596
	gadget_for_each_ep(ep, gadget) {
Linus Torvalds's avatar
Linus Torvalds committed
597
598
599
		const struct usb_endpoint_descriptor	*d;

		/* one endpoint writes (sources) zeroes in (to the host) */
600
601
602
		if (strcmp(ep->name, EP_IN_NAME) == 0) {
			d = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
			result = usb_ep_enable(ep, d);
Linus Torvalds's avatar
Linus Torvalds committed
603
604
			if (result == 0) {
				ep->driver_data = dev;
605
				if (source_sink_start_ep(ep) != NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
606
607
608
					dev->in_ep = ep;
					continue;
				}
609
				usb_ep_disable(ep);
Linus Torvalds's avatar
Linus Torvalds committed
610
611
612
613
				result = -EIO;
			}

		/* one endpoint reads (sinks) anything out (from the host) */
614
615
616
		} else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
			d = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
			result = usb_ep_enable(ep, d);
Linus Torvalds's avatar
Linus Torvalds committed
617
618
			if (result == 0) {
				ep->driver_data = dev;
619
				if (source_sink_start_ep(ep) != NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
620
621
622
					dev->out_ep = ep;
					continue;
				}
623
				usb_ep_disable(ep);
Linus Torvalds's avatar
Linus Torvalds committed
624
625
626
627
628
629
630
631
				result = -EIO;
			}

		/* ignore any other endpoints */
		} else
			continue;

		/* stop on error */
632
		ERROR(dev, "can't start %s, result %d\n", ep->name, result);
Linus Torvalds's avatar
Linus Torvalds committed
633
634
635
		break;
	}
	if (result == 0)
636
		DBG(dev, "buflen %d\n", buflen);
Linus Torvalds's avatar
Linus Torvalds committed
637
638
639
640
641
642
643

	/* caller is responsible for cleanup on error */
	return result;
}

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

644
static void loopback_complete(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds's avatar
Linus Torvalds committed
645
646
647
648
649
650
{
	struct zero_dev	*dev = ep->driver_data;
	int		status = req->status;

	switch (status) {

651
	case 0:				/* normal completion? */
Linus Torvalds's avatar
Linus Torvalds committed
652
653
654
655
		if (ep == dev->out_ep) {
			/* loop this OUT packet back IN to the host */
			req->zero = (req->actual < req->length);
			req->length = req->actual;
656
			status = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
657
658
659
660
			if (status == 0)
				return;

			/* "should never get here" */
661
			ERROR(dev, "can't loop %s to %s: %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
662
663
664
665
666
667
				ep->name, dev->in_ep->name,
				status);
		}

		/* queue the buffer for some later OUT packet */
		req->length = buflen;
668
		status = usb_ep_queue(dev->out_ep, req, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
669
670
671
672
673
674
675
		if (status == 0)
			return;

		/* "should never get here" */
		/* FALLTHROUGH */

	default:
676
		ERROR(dev, "%s loop complete --> %d, %d/%d\n", ep->name,
Linus Torvalds's avatar
Linus Torvalds committed
677
678
679
680
681
682
683
684
				status, req->actual, req->length);
		/* FALLTHROUGH */

	/* NOTE:  since this driver doesn't maintain an explicit record
	 * of requests it submitted (just maintains qlen count), we
	 * rely on the hardware driver to clean up on disconnect or
	 * endpoint disable.
	 */
685
	case -ECONNABORTED:		/* hardware forced ep reset */
Linus Torvalds's avatar
Linus Torvalds committed
686
687
	case -ECONNRESET:		/* request dequeued */
	case -ESHUTDOWN:		/* disconnect from host */
688
		free_ep_req(ep, req);
Linus Torvalds's avatar
Linus Torvalds committed
689
690
691
692
		return;
	}
}

693
static int set_loopback_config(struct zero_dev *dev)
Linus Torvalds's avatar
Linus Torvalds committed
694
695
696
697
698
{
	int			result = 0;
	struct usb_ep		*ep;
	struct usb_gadget	*gadget = dev->gadget;

699
	gadget_for_each_ep(ep, gadget) {
Linus Torvalds's avatar
Linus Torvalds committed
700
701
702
		const struct usb_endpoint_descriptor	*d;

		/* one endpoint writes data back IN to the host */
703
704
705
		if (strcmp(ep->name, EP_IN_NAME) == 0) {
			d = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
			result = usb_ep_enable(ep, d);
Linus Torvalds's avatar
Linus Torvalds committed
706
707
708
709
710
711
712
			if (result == 0) {
				ep->driver_data = dev;
				dev->in_ep = ep;
				continue;
			}

		/* one endpoint just reads OUT packets */
713
714
715
		} else if (strcmp(ep->name, EP_OUT_NAME) == 0) {
			d = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
			result = usb_ep_enable(ep, d);
Linus Torvalds's avatar
Linus Torvalds committed
716
717
718
719
720
721
722
723
724
725
726
			if (result == 0) {
				ep->driver_data = dev;
				dev->out_ep = ep;
				continue;
			}

		/* ignore any other endpoints */
		} else
			continue;

		/* stop on error */
727
		ERROR(dev, "can't enable %s, result %d\n", ep->name, result);
Linus Torvalds's avatar
Linus Torvalds committed
728
729
730
731
732
733
734
735
736
737
738
739
740
		break;
	}

	/* allocate a bunch of read buffers and queue them all at once.
	 * we buffer at most 'qlen' transfers; fewer if any need more
	 * than 'buflen' bytes each.
	 */
	if (result == 0) {
		struct usb_request	*req;
		unsigned		i;

		ep = dev->out_ep;
		for (i = 0; i < qlen && result == 0; i++) {
741
			req = alloc_ep_req(ep, buflen);
Linus Torvalds's avatar
Linus Torvalds committed
742
743
			if (req) {
				req->complete = loopback_complete;
744
				result = usb_ep_queue(ep, req, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
745
				if (result)
746
					DBG(dev, "%s queue req --> %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
747
748
749
750
751
752
							ep->name, result);
			} else
				result = -ENOMEM;
		}
	}
	if (result == 0)
753
		DBG(dev, "qlen %d, buflen %d\n", qlen, buflen);
Linus Torvalds's avatar
Linus Torvalds committed
754
755
756
757
758
759
760

	/* caller is responsible for cleanup on error */
	return result;
}

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

761
static void zero_reset_config(struct zero_dev *dev)
Linus Torvalds's avatar
Linus Torvalds committed
762
763
764
765
{
	if (dev->config == 0)
		return;

766
	DBG(dev, "reset config\n");
Linus Torvalds's avatar
Linus Torvalds committed
767
768
769
770
771

	/* just disable endpoints, forcing completion of pending i/o.
	 * all our completion handlers free their requests in this case.
	 */
	if (dev->in_ep) {
772
		usb_ep_disable(dev->in_ep);
Linus Torvalds's avatar
Linus Torvalds committed
773
774
775
		dev->in_ep = NULL;
	}
	if (dev->out_ep) {
776
		usb_ep_disable(dev->out_ep);
Linus Torvalds's avatar
Linus Torvalds committed
777
778
779
		dev->out_ep = NULL;
	}
	dev->config = 0;
780
	del_timer(&dev->resume);
Linus Torvalds's avatar
Linus Torvalds committed
781
782
783
784
785
786
787
788
789
790
791
792
}

/* change our operational config.  this code must agree with the code
 * that returns config descriptors, and altsetting code.
 *
 * it's also responsible for power management interactions. some
 * configurations might not work with our current power sources.
 *
 * note that some device controller hardware will constrain what this
 * code can do, perhaps by disallowing more than one configuration or
 * by limiting configuration choices (like the pxa2xx).
 */
793
static int zero_set_config(struct zero_dev *dev, unsigned number)
Linus Torvalds's avatar
Linus Torvalds committed
794
795
796
797
798
799
800
{
	int			result = 0;
	struct usb_gadget	*gadget = dev->gadget;

	if (number == dev->config)
		return 0;

801
	if (gadget_is_sa1100(gadget) && dev->config) {
Linus Torvalds's avatar
Linus Torvalds committed
802
		/* tx fifo is full, but we can't clear it...*/
803
		ERROR(dev, "can't change configurations\n");
Linus Torvalds's avatar
Linus Torvalds committed
804
805
		return -ESPIPE;
	}
806
	zero_reset_config(dev);
Linus Torvalds's avatar
Linus Torvalds committed
807
808
809

	switch (number) {
	case CONFIG_SOURCE_SINK:
810
		result = set_source_sink_config(dev);
Linus Torvalds's avatar
Linus Torvalds committed
811
812
		break;
	case CONFIG_LOOPBACK:
813
		result = set_loopback_config(dev);
Linus Torvalds's avatar
Linus Torvalds committed
814
815
816
817
818
819
820
821
822
823
824
		break;
	default:
		result = -EINVAL;
		/* FALL THROUGH */
	case 0:
		return result;
	}

	if (!result && (!dev->in_ep || !dev->out_ep))
		result = -ENODEV;
	if (result)
825
		zero_reset_config(dev);
Linus Torvalds's avatar
Linus Torvalds committed
826
827
828
829
830
831
832
	else {
		char *speed;

		switch (gadget->speed) {
		case USB_SPEED_LOW:	speed = "low"; break;
		case USB_SPEED_FULL:	speed = "full"; break;
		case USB_SPEED_HIGH:	speed = "high"; break;
833
		default:		speed = "?"; break;
Linus Torvalds's avatar
Linus Torvalds committed
834
835
836
		}

		dev->config = number;
837
		INFO(dev, "%s speed config #%d: %s\n", speed, number,
Linus Torvalds's avatar
Linus Torvalds committed
838
839
840
841
842
843
844
845
				(number == CONFIG_SOURCE_SINK)
					? source_sink : loopback);
	}
	return result;
}

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

846
static void zero_setup_complete(struct usb_ep *ep, struct usb_request *req)
Linus Torvalds's avatar
Linus Torvalds committed
847
848
{
	if (req->status || req->actual != req->length)
849
		DBG((struct zero_dev *) ep->driver_data,
Linus Torvalds's avatar
Linus Torvalds committed
850
851
852
853
854
855
856
857
858
859
860
861
				"setup complete --> %d, %d/%d\n",
				req->status, req->actual, req->length);
}

/*
 * The setup() callback implements all the ep0 functionality that's
 * not handled lower down, in hardware or the hardware driver (like
 * device and endpoint feature flags, and their status).  It's all
 * housekeeping for the gadget function we're implementing.  Most of
 * the work is in config-specific setup.
 */
static int
862
zero_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
Linus Torvalds's avatar
Linus Torvalds committed
863
{
864
	struct zero_dev		*dev = get_gadget_data(gadget);
Linus Torvalds's avatar
Linus Torvalds committed
865
866
	struct usb_request	*req = dev->req;
	int			value = -EOPNOTSUPP;
867
868
869
	u16			w_index = le16_to_cpu(ctrl->wIndex);
	u16			w_value = le16_to_cpu(ctrl->wValue);
	u16			w_length = le16_to_cpu(ctrl->wLength);
Linus Torvalds's avatar
Linus Torvalds committed
870
871
872
873
874
875
876
877
878
879
880
881
882

	/* usually this stores reply data in the pre-allocated ep0 buffer,
	 * but config change events will reconfigure hardware.
	 */
	req->zero = 0;
	switch (ctrl->bRequest) {

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

		case USB_DT_DEVICE:
883
884
			value = min(w_length, (u16) sizeof device_desc);
			memcpy(req->buf, &device_desc, value);
Linus Torvalds's avatar
Linus Torvalds committed
885
886
			break;
		case USB_DT_DEVICE_QUALIFIER:
887
			if (!gadget_is_dualspeed(gadget))
Linus Torvalds's avatar
Linus Torvalds committed
888
				break;
889
890
			value = min(w_length, (u16) sizeof dev_qualifier);
			memcpy(req->buf, &dev_qualifier, value);
Linus Torvalds's avatar
Linus Torvalds committed
891
892
893
			break;

		case USB_DT_OTHER_SPEED_CONFIG:
894
			if (!gadget_is_dualspeed(gadget))
Linus Torvalds's avatar
Linus Torvalds committed
895
896
897
				break;
			// FALLTHROUGH
		case USB_DT_CONFIG:
898
			value = config_buf(gadget, req->buf,
Linus Torvalds's avatar
Linus Torvalds committed
899
900
901
					w_value >> 8,
					w_value & 0xff);
			if (value >= 0)
902
				value = min(w_length, (u16) value);
Linus Torvalds's avatar
Linus Torvalds committed
903
904
905
906
907
908
909
910
			break;

		case USB_DT_STRING:
			/* wIndex == language code.
			 * this driver only handles one language, you can
			 * add string tables for other languages, using
			 * any UTF-8 characters
			 */
911
			value = usb_gadget_get_string(&stringtab,
Linus Torvalds's avatar
Linus Torvalds committed
912
913
					w_value & 0xff, req->buf);
			if (value >= 0)
914
				value = min(w_length, (u16) value);
Linus Torvalds's avatar
Linus Torvalds committed
915
916
917
918
919
920
921
922
923
			break;
		}
		break;

	/* currently two configs, two speeds */
	case USB_REQ_SET_CONFIGURATION:
		if (ctrl->bRequestType != 0)
			goto unknown;
		if (gadget->a_hnp_support)
924
			DBG(dev, "HNP available\n");
Linus Torvalds's avatar
Linus Torvalds committed
925
		else if (gadget->a_alt_hnp_support)
926
			DBG(dev, "HNP needs a different root port\n");
Linus Torvalds's avatar
Linus Torvalds committed
927
		else
928
929
			VDBG(dev, "HNP inactive\n");
		spin_lock(&dev->lock);
930
		value = zero_set_config(dev, w_value);
931
		spin_unlock(&dev->lock);
Linus Torvalds's avatar
Linus Torvalds committed
932
933
934
935
936
		break;
	case USB_REQ_GET_CONFIGURATION:
		if (ctrl->bRequestType != USB_DIR_IN)
			goto unknown;
		*(u8 *)req->buf = dev->config;
937
		value = min(w_length, (u16) 1);
Linus Torvalds's avatar
Linus Torvalds committed
938
939
940
941
942
943
944
945
946
		break;

	/* until we add altsetting support, or other interfaces,
	 * only 0/0 are possible.  pxa2xx only supports 0/0 (poorly)
	 * and already killed pending endpoint I/O.
	 */
	case USB_REQ_SET_INTERFACE:
		if (ctrl->bRequestType != USB_RECIP_INTERFACE)
			goto unknown;
947
		spin_lock(&dev->lock);
Linus Torvalds's avatar
Linus Torvalds committed
948
949
950
951
952
953
954
955
956
957
		if (dev->config && w_index == 0 && w_value == 0) {
			u8		config = dev->config;

			/* resets interface configuration, forgets about
			 * previous transaction state (queued bufs, etc)
			 * and re-inits endpoint state (toggle etc)
			 * no response queued, just zero status == success.
			 * if we had more than one interface we couldn't
			 * use this "reset the config" shortcut.
			 */
958
			zero_reset_config(dev);
959
			zero_set_config(dev, config);
Linus Torvalds's avatar
Linus Torvalds committed
960
961
			value = 0;
		}
962
		spin_unlock(&dev->lock);
Linus Torvalds's avatar
Linus Torvalds committed
963
964
965
966
967
968
969
970
971
972
973
		break;
	case USB_REQ_GET_INTERFACE:
		if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
			goto unknown;
		if (!dev->config)
			break;
		if (w_index != 0) {
			value = -EDOM;
			break;
		}
		*(u8 *)req->buf = 0;
974
		value = min(w_length, (u16) 1);
Linus Torvalds's avatar
Linus Torvalds committed
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
		break;

	/*
	 * These are the same vendor-specific requests supported by
	 * Intel's USB 2.0 compliance test devices.  We exceed that
	 * device spec by allowing multiple-packet requests.
	 */
	case 0x5b:	/* control WRITE test -- fill the buffer */
		if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
			goto unknown;
		if (w_value || w_index)
			break;
		/* just read that many bytes into the buffer */
		if (w_length > USB_BUFSIZ)
			break;
		value = w_length;
		break;
	case 0x5c:	/* control READ test -- return the buffer */
		if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
			goto unknown;
		if (w_value || w_index)
			break;
		/* expect those bytes are still in the buffer; send back */
		if (w_length > USB_BUFSIZ
				|| w_length != req->length)
			break;
		value = w_length;
		break;

	default:
unknown:
1006
		VDBG(dev,
Linus Torvalds's avatar
Linus Torvalds committed
1007
1008
1009
1010
1011
1012
1013
1014
1015
			"unknown control req%02x.%02x v%04x i%04x l%d\n",
			ctrl->bRequestType, ctrl->bRequest,
			w_value, w_index, w_length);
	}

	/* respond with data transfer before status phase? */
	if (value >= 0) {
		req->length = value;
		req->zero = value < w_length;
1016
		value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
1017
		if (value < 0) {
1018
			DBG(dev, "ep_queue --> %d\n", value);
Linus Torvalds's avatar
Linus Torvalds committed
1019
			req->status = 0;
1020
			zero_setup_complete(gadget->ep0, req);
Linus Torvalds's avatar
Linus Torvalds committed
1021
1022
1023
1024
1025
1026
1027
		}
	}

	/* device either stalls (value < 0) or reports success */
	return value;
}

1028
static void zero_disconnect(struct usb_gadget *gadget)
Linus Torvalds's avatar
Linus Torvalds committed
1029
{
1030
	struct zero_dev		*dev = get_gadget_data(gadget);
Linus Torvalds's avatar
Linus Torvalds committed
1031
1032
	unsigned long		flags;

1033
1034
	spin_lock_irqsave(&dev->lock, flags);
	zero_reset_config(dev);
Linus Torvalds's avatar
Linus Torvalds committed
1035
1036
1037
1038
1039

	/* a more significant application might have some non-usb
	 * activities to quiesce here, saving resources like power
	 * or pushing the notification up a network stack.
	 */
1040
	spin_unlock_irqrestore(&dev->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1041
1042
1043
1044
1045
1046

	/* next we may get setup() calls to enumerate new connections;
	 * or an unbind() during shutdown (including removing module).
	 */
}

1047
static void zero_autoresume(unsigned long _dev)
Linus Torvalds's avatar
Linus Torvalds committed
1048
1049
1050
1051
1052
1053
1054
1055
{
	struct zero_dev	*dev = (struct zero_dev *) _dev;
	int		status;

	/* normally the host would be woken up for something
	 * more significant than just a timer firing...
	 */
	if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
1056
1057
		status = usb_gadget_wakeup(dev->gadget);
		DBG(dev, "wakeup --> %d\n", status);
Linus Torvalds's avatar
Linus Torvalds committed
1058
1059
1060
1061
1062
	}
}

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

1063
static void zero_unbind(struct usb_gadget *gadget)
Linus Torvalds's avatar
Linus Torvalds committed
1064
{
1065
	struct zero_dev		*dev = get_gadget_data(gadget);
Linus Torvalds's avatar
Linus Torvalds committed
1066

1067
	DBG(dev, "unbind\n");
Linus Torvalds's avatar
Linus Torvalds committed
1068
1069

	/* we've already been disconnected ... no i/o is active */
1070
1071
	if (dev->req) {
		dev->req->length = USB_BUFSIZ;
1072
		free_ep_req(gadget->ep0, dev->req);
1073
	}
1074
1075
1076
	del_timer_sync(&dev->resume);
	kfree(dev);
	set_gadget_data(gadget, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
1077
1078
}

1079
static int __init zero_bind(struct usb_gadget *gadget)
Linus Torvalds's avatar
Linus Torvalds committed
1080
1081
1082
{
	struct zero_dev		*dev;
	struct usb_ep		*ep;
1083
1084
1085
1086
1087
1088
1089
	int			gcnum;

	/* FIXME this can't yet work right with SH ... it has only
	 * one configuration, numbered one.
	 */
	if (gadget_is_sh(gadget))
		return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
1090
1091
1092
1093
1094

	/* Bulk-only drivers like this one SHOULD be able to
	 * autoconfigure on any sane usb controller driver,
	 * but there may also be important quirks to address.
	 */
1095
1096
	usb_ep_autoconfig_reset(gadget);
	ep = usb_ep_autoconfig(gadget, &fs_source_desc);
Linus Torvalds's avatar
Linus Torvalds committed
1097
1098
	if (!ep) {
autoconf_fail:
1099
		pr_err("%s: can't autoconfigure on %s\n",
Linus Torvalds's avatar
Linus Torvalds committed
1100
1101
1102
1103
1104
			shortname, gadget->name);
		return -ENODEV;
	}
	EP_IN_NAME = ep->name;
	ep->driver_data = ep;	/* claim */
1105

1106
	ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
Linus Torvalds's avatar
Linus Torvalds committed
1107
1108
1109
1110
1111
	if (!ep)
		goto autoconf_fail;
	EP_OUT_NAME = ep->name;
	ep->driver_data = ep;	/* claim */

1112
	gcnum = usb_gadget_controller_number(gadget);
1113
	if (gcnum >= 0)
1114
		device_desc.bcdDevice = cpu_to_le16(0x0200 + gcnum);
1115
	else {
Linus Torvalds's avatar
Linus Torvalds committed
1116
1117
1118
1119
1120
1121
1122
		/* gadget zero is so simple (for now, no altsettings) that
		 * it SHOULD NOT have problems with bulk-capable hardware.
		 * so warn about unrcognized controllers, don't panic.
		 *
		 * things like configuration and altsetting numbering
		 * can need hardware-specific attention though.
		 */
1123
		pr_warning("%s: controller '%s' not recognized\n",
Linus Torvalds's avatar
Linus Torvalds committed
1124
			shortname, gadget->name);
1125
		device_desc.bcdDevice = __constant_cpu_to_le16(0x9999);
Linus Torvalds's avatar
Linus Torvalds committed
1126
1127
1128
1129
	}


	/* ok, we made sense of the hardware ... */
1130
	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1131
1132
	if (!dev)
		return -ENOMEM;
1133
	spin_lock_init(&dev->lock);
Linus Torvalds's avatar
Linus Torvalds committed
1134
	dev->gadget = gadget;
1135
	set_gadget_data(gadget, dev);
Linus Torvalds's avatar
Linus Torvalds committed
1136

David Lopo's avatar
David Lopo committed
1137
1138
1139
1140
	init_timer(&dev->resume);
	dev->resume.function = zero_autoresume;
	dev->resume.data = (unsigned long) dev;

Linus Torvalds's avatar
Linus Torvalds committed
1141
	/* preallocate control response and buffer */
1142
	dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1143
1144
	if (!dev->req)
		goto enomem;
1145
	dev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1146
1147
1148
1149
1150
1151
1152
	if (!dev->req->buf)
		goto enomem;

	dev->req->complete = zero_setup_complete;

	device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;

1153
1154
1155
	if (gadget_is_dualspeed(gadget)) {
		/* assume ep0 uses the same value for both speeds ... */
		dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
Linus Torvalds's avatar
Linus Torvalds committed
1156

1157
1158
1159
1160
1161
1162
		/* and that all endpoints are dual-speed */
		hs_source_desc.bEndpointAddress =
				fs_source_desc.bEndpointAddress;
		hs_sink_desc.bEndpointAddress =
				fs_sink_desc.bEndpointAddress;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1163

1164
	if (gadget_is_otg(gadget)) {
Linus Torvalds's avatar
Linus Torvalds committed
1165
1166
1167
1168
1169
		otg_descriptor.bmAttributes |= USB_OTG_HNP,
		source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
		loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
	}

1170
	usb_gadget_set_selfpowered(gadget);
Linus Torvalds's avatar
Linus Torvalds committed
1171
1172
1173
1174
1175
1176
1177
1178

	if (autoresume) {
		source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
		loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
	}

	gadget->ep0->driver_data = dev;

1179
1180
	INFO(dev, "%s, version: " DRIVER_VERSION "\n", longname);
	INFO(dev, "using %s, OUT %s IN %s\n", gadget->name,
Linus Torvalds's avatar
Linus Torvalds committed
1181
1182
		EP_OUT_NAME, EP_IN_NAME);

1183
	snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
1184
		init_utsname()->sysname, init_utsname()->release,
Linus Torvalds's avatar
Linus Torvalds committed
1185
1186
1187
1188
1189
		gadget->name);

	return 0;

enomem:
1190
	zero_unbind(gadget);
Linus Torvalds's avatar
Linus Torvalds committed
1191
1192
1193
1194
1195
	return -ENOMEM;
}

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

1196
static void zero_suspend(struct usb_gadget *gadget)
Linus Torvalds's avatar
Linus Torvalds committed
1197
{
1198
	struct zero_dev		*dev = get_gadget_data(gadget);
Linus Torvalds's avatar
Linus Torvalds committed
1199
1200
1201
1202
1203

	if (gadget->speed == USB_SPEED_UNKNOWN)
		return;

	if (autoresume) {
1204
1205
		mod_timer(&dev->resume, jiffies + (HZ * autoresume));
		DBG(dev, "suspend, wakeup in %d seconds\n", autoresume);
Linus Torvalds's avatar
Linus Torvalds committed
1206
	} else
1207
		DBG(dev, "suspend\n");
Linus Torvalds's avatar
Linus Torvalds committed
1208
1209
}

1210
static void zero_resume(struct usb_gadget *gadget)
Linus Torvalds's avatar
Linus Torvalds committed
1211
{
1212
	struct zero_dev		*dev = get_gadget_data(gadget);
Linus Torvalds's avatar
Linus Torvalds committed
1213

1214
1215
	DBG(dev, "resume\n");
	del_timer(&dev->resume);
Linus Torvalds's avatar
Linus Torvalds committed
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
}


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

static struct usb_gadget_driver zero_driver = {
#ifdef CONFIG_USB_GADGET_DUALSPEED
	.speed		= USB_SPEED_HIGH,
#else
	.speed		= USB_SPEED_FULL,
#endif
	.function	= (char *) longname,
	.bind		= zero_bind,
1229
	.unbind		= __exit_p(zero_unbind),
Linus Torvalds's avatar
Linus Torvalds committed
1230
1231
1232
1233
1234
1235
1236

	.setup		= zero_setup,
	.disconnect	= zero_disconnect,

	.suspend	= zero_suspend,
	.resume		= zero_resume,

1237
	.driver		= {
Linus Torvalds's avatar
Linus Torvalds committed
1238
		.name		= (char *) shortname,
1239
		.owner		= THIS_MODULE,
Linus Torvalds's avatar
Linus Torvalds committed
1240
1241
1242
	},
};

1243
1244
MODULE_AUTHOR("David Brownell");
MODULE_LICENSE("GPL");
Linus Torvalds's avatar
Linus Torvalds committed
1245
1246


1247
static int __init init(void)
Linus Torvalds's avatar
Linus Torvalds committed
1248
{
1249
	return usb_gadget_register_driver(&zero_driver);
Linus Torvalds's avatar
Linus Torvalds committed
1250
}
1251
module_init(init);
Linus Torvalds's avatar
Linus Torvalds committed
1252

1253
static void __exit cleanup(void)
Linus Torvalds's avatar
Linus Torvalds committed
1254
{
1255
	usb_gadget_unregister_driver(&zero_driver);
Linus Torvalds's avatar
Linus Torvalds committed
1256
}
1257
module_exit(cleanup);
Linus Torvalds's avatar
Linus Torvalds committed
1258