1 /*
2  * zero.c -- Gadget Zero, for USB development
3  *
4  * Copyright (C) 2003-2004 David Brownell
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The names of the above-listed copyright holders may not be used
17  *    to endorse or promote products derived from this software without
18  *    specific prior written permission.
19  *
20  * ALTERNATIVELY, this software may be distributed under the terms of the
21  * GNU General Public License ("GPL") as published by the Free Software
22  * Foundation, either version 2 of that License or (at your option) any
23  * later version.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 
39 /*
40  * Gadget Zero only needs two bulk endpoints, and is an example of how you
41  * can write a hardware-agnostic gadget driver running inside a USB device.
42  *
43  * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
44  * affect most of the driver.
45  *
46  * Use it with the Linux host/master side "usbtest" driver to get a basic
47  * functional test of your device-side usb stack, or with "usb-skeleton".
48  *
49  * It supports two similar configurations.  One sinks whatever the usb host
50  * writes, and in return sources zeroes.  The other loops whatever the host
51  * writes back, so the host can read it.  Module options include:
52  *
53  *   buflen=N		default N=4096, buffer size used
54  *   qlen=N		default N=32, how many buffers in the loopback queue
55  *   loopdefault	default false, list loopback config first
56  *
57  * Many drivers will only have one configuration, letting them be much
58  * simpler if they also don't support high speed operation (like this
59  * driver does).
60  */
61 
62 #define DEBUG 1
63 // #define VERBOSE
64 
65 #include <linux/config.h>
66 #include <linux/kernel.h>
67 #include <linux/module.h>
68 #include <linux/delay.h>
69 #include <linux/ioport.h>
70 #include <linux/sched.h>
71 #include <linux/slab.h>
72 #include <linux/smp_lock.h>
73 #include <linux/errno.h>
74 #include <linux/init.h>
75 #include <linux/timer.h>
76 #include <linux/list.h>
77 #include <linux/interrupt.h>
78 #include <linux/uts.h>
79 #include <linux/version.h>
80 
81 #include <asm/byteorder.h>
82 #include <asm/io.h>
83 #include <asm/irq.h>
84 #include <asm/system.h>
85 #include <asm/unaligned.h>
86 
87 #include <linux/usb_ch9.h>
88 #include <linux/usb_gadget.h>
89 
90 #include "gadget_chips.h"
91 
92 
93 /*-------------------------------------------------------------------------*/
94 
95 #define DRIVER_VERSION		"St Patrick's Day 2004"
96 
97 static const char shortname [] = "zero";
98 static const char longname [] = "Gadget Zero";
99 
100 static const char source_sink [] = "source and sink data";
101 static const char loopback [] = "loop input to output";
102 
103 /*-------------------------------------------------------------------------*/
104 
105 /*
106  * driver assumes self-powered hardware, and
107  * has no way for users to trigger remote wakeup.
108  *
109  * this version autoconfigures as much as possible,
110  * which is reasonable for most "bulk-only" drivers.
111  */
112 static const char *EP_IN_NAME;		/* source */
113 static const char *EP_OUT_NAME;		/* sink */
114 
115 /*-------------------------------------------------------------------------*/
116 
117 /* big enough to hold our biggest descriptor */
118 #define USB_BUFSIZ	256
119 
120 struct zero_dev {
121 	spinlock_t		lock;
122 	struct usb_gadget	*gadget;
123 	struct usb_request	*req;		/* for control responses */
124 
125 	/* when configured, we have one of two configs:
126 	 * - source data (in to host) and sink it (out from host)
127 	 * - or loop it back (out from host back in to host)
128 	 */
129 	u8			config;
130 	struct usb_ep		*in_ep, *out_ep;
131 
132 	/* autoresume timer */
133 	struct timer_list	resume;
134 };
135 
136 #define xprintk(d,level,fmt,args...) \
137 	printk(level "%s %s: " fmt , shortname , (d)->gadget->dev.bus_id , \
138 		## args)
139 
140 #ifdef DEBUG
141 #define DBG(dev,fmt,args...) \
142 	xprintk(dev , KERN_DEBUG , fmt , ## args)
143 #else
144 #define DBG(dev,fmt,args...) \
145 	do { } while (0)
146 #endif /* DEBUG */
147 
148 #ifdef VERBOSE
149 #define VDBG	DBG
150 #else
151 #define VDBG(dev,fmt,args...) \
152 	do { } while (0)
153 #endif /* VERBOSE */
154 
155 #define ERROR(dev,fmt,args...) \
156 	xprintk(dev , KERN_ERR , fmt , ## args)
157 #define WARN(dev,fmt,args...) \
158 	xprintk(dev , KERN_WARNING , fmt , ## args)
159 #define INFO(dev,fmt,args...) \
160 	xprintk(dev , KERN_INFO , fmt , ## args)
161 
162 /*-------------------------------------------------------------------------*/
163 
164 static unsigned buflen = 4096;
165 static unsigned qlen = 32;
166 static unsigned pattern = 0;
167 
168 /*
169  * Normally the "loopback" configuration is second (index 1) so
170  * it's not the default.  Here's where to change that order, to
171  * work better with hosts where config changes are problematic.
172  * Or controllers (like superh) that only support one config.
173  */
174 static int loopdefault = 0;
175 
176 
177 MODULE_PARM (buflen, "i");
178 MODULE_PARM_DESC (buflen, "size of i/o buffers");
179 
180 MODULE_PARM (qlen, "i");
181 MODULE_PARM_DESC (qlen, "depth of loopback buffering");
182 
183 MODULE_PARM (pattern, "i");
184 MODULE_PARM_DESC (pattern, "0 for default all-zeroes, 1 for mod63");
185 
186 MODULE_PARM (loopdefault, "b");
187 MODULE_PARM_DESC (loopdefault, "true to have default config be loopback");
188 
189 /*
190  * if it's nonzero, autoresume says how many seconds to wait
191  * before trying to wake up the host after suspend.
192  */
193 static unsigned autoresume = 0;
194 MODULE_PARM (autoresume, "i");
195 
196 /*-------------------------------------------------------------------------*/
197 
198 /* Thanks to NetChip Technologies for donating this product ID.
199  *
200  * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
201  * Instead:  allocate your own, using normal USB-IF procedures.
202  */
203 #ifndef	CONFIG_USB_ZERO_HNPTEST
204 #define DRIVER_VENDOR_NUM	0x0525		/* NetChip */
205 #define DRIVER_PRODUCT_NUM	0xa4a0		/* Linux-USB "Gadget Zero" */
206 #else
207 #define DRIVER_VENDOR_NUM	0x1a0a		/* OTG test device IDs */
208 #define DRIVER_PRODUCT_NUM	0xbadd
209 #endif
210 
211 /*-------------------------------------------------------------------------*/
212 
213 /*
214  * DESCRIPTORS ... most are static, but strings and (full)
215  * configuration descriptors are built on demand.
216  */
217 
218 #define STRING_MANUFACTURER		25
219 #define STRING_PRODUCT			42
220 #define STRING_SERIAL			101
221 #define STRING_SOURCE_SINK		250
222 #define STRING_LOOPBACK			251
223 
224 /*
225  * This device advertises two configurations; these numbers work
226  * on a pxa250 as well as more flexible hardware.
227  */
228 #define	CONFIG_SOURCE_SINK	3
229 #define	CONFIG_LOOPBACK		2
230 
231 static struct usb_device_descriptor
232 device_desc = {
233 	.bLength =		sizeof device_desc,
234 	.bDescriptorType =	USB_DT_DEVICE,
235 
236 	.bcdUSB =		__constant_cpu_to_le16 (0x0200),
237 	.bDeviceClass =		USB_CLASS_VENDOR_SPEC,
238 
239 	.idVendor =		__constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
240 	.idProduct =		__constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
241 	.iManufacturer =	STRING_MANUFACTURER,
242 	.iProduct =		STRING_PRODUCT,
243 	.iSerialNumber =	STRING_SERIAL,
244 	.bNumConfigurations =	2,
245 };
246 
247 static struct usb_config_descriptor
248 source_sink_config = {
249 	.bLength =		sizeof source_sink_config,
250 	.bDescriptorType =	USB_DT_CONFIG,
251 
252 	/* compute wTotalLength on the fly */
253 	.bNumInterfaces =	1,
254 	.bConfigurationValue =	CONFIG_SOURCE_SINK,
255 	.iConfiguration =	STRING_SOURCE_SINK,
256 	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
257 	.bMaxPower =		1,	/* self-powered */
258 };
259 
260 static struct usb_config_descriptor
261 loopback_config = {
262 	.bLength =		sizeof loopback_config,
263 	.bDescriptorType =	USB_DT_CONFIG,
264 
265 	/* compute wTotalLength on the fly */
266 	.bNumInterfaces =	1,
267 	.bConfigurationValue =	CONFIG_LOOPBACK,
268 	.iConfiguration =	STRING_LOOPBACK,
269 	.bmAttributes =		USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
270 	.bMaxPower =		1,	/* self-powered */
271 };
272 
273 static struct usb_otg_descriptor
274 otg_descriptor = {
275 	.bLength =		sizeof otg_descriptor,
276 	.bDescriptorType =	USB_DT_OTG,
277 
278 	.bmAttributes =		USB_OTG_SRP,
279 };
280 
281 /* one interface in each configuration */
282 
283 static const struct usb_interface_descriptor
284 source_sink_intf = {
285 	.bLength =		sizeof source_sink_intf,
286 	.bDescriptorType =	USB_DT_INTERFACE,
287 
288 	.bNumEndpoints =	2,
289 	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC,
290 	.iInterface =		STRING_SOURCE_SINK,
291 };
292 
293 static const struct usb_interface_descriptor
294 loopback_intf = {
295 	.bLength =		sizeof loopback_intf,
296 	.bDescriptorType =	USB_DT_INTERFACE,
297 
298 	.bNumEndpoints =	2,
299 	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC,
300 	.iInterface =		STRING_LOOPBACK,
301 };
302 
303 /* two full speed bulk endpoints; their use is config-dependent */
304 
305 static struct usb_endpoint_descriptor
306 fs_source_desc = {
307 	.bLength =		USB_DT_ENDPOINT_SIZE,
308 	.bDescriptorType =	USB_DT_ENDPOINT,
309 
310 	.bEndpointAddress =	USB_DIR_IN,
311 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
312 };
313 
314 static struct usb_endpoint_descriptor
315 fs_sink_desc = {
316 	.bLength =		USB_DT_ENDPOINT_SIZE,
317 	.bDescriptorType =	USB_DT_ENDPOINT,
318 
319 	.bEndpointAddress =	USB_DIR_OUT,
320 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
321 };
322 
323 static const struct usb_descriptor_header *fs_source_sink_function [] = {
324 	(struct usb_descriptor_header *) &otg_descriptor,
325 	(struct usb_descriptor_header *) &source_sink_intf,
326 	(struct usb_descriptor_header *) &fs_sink_desc,
327 	(struct usb_descriptor_header *) &fs_source_desc,
328 	NULL,
329 };
330 
331 static const struct usb_descriptor_header *fs_loopback_function [] = {
332 	(struct usb_descriptor_header *) &otg_descriptor,
333 	(struct usb_descriptor_header *) &loopback_intf,
334 	(struct usb_descriptor_header *) &fs_sink_desc,
335 	(struct usb_descriptor_header *) &fs_source_desc,
336 	NULL,
337 };
338 
339 #ifdef	CONFIG_USB_GADGET_DUALSPEED
340 
341 /*
342  * usb 2.0 devices need to expose both high speed and full speed
343  * descriptors, unless they only run at full speed.
344  *
345  * that means alternate endpoint descriptors (bigger packets)
346  * and a "device qualifier" ... plus more construction options
347  * for the config descriptor.
348  */
349 
350 static struct usb_endpoint_descriptor
351 hs_source_desc = {
352 	.bLength =		USB_DT_ENDPOINT_SIZE,
353 	.bDescriptorType =	USB_DT_ENDPOINT,
354 
355 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
356 	.wMaxPacketSize =	__constant_cpu_to_le16 (512),
357 };
358 
359 static struct usb_endpoint_descriptor
360 hs_sink_desc = {
361 	.bLength =		USB_DT_ENDPOINT_SIZE,
362 	.bDescriptorType =	USB_DT_ENDPOINT,
363 
364 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
365 	.wMaxPacketSize =	__constant_cpu_to_le16 (512),
366 };
367 
368 static struct usb_qualifier_descriptor
369 dev_qualifier = {
370 	.bLength =		sizeof dev_qualifier,
371 	.bDescriptorType =	USB_DT_DEVICE_QUALIFIER,
372 
373 	.bcdUSB =		__constant_cpu_to_le16 (0x0200),
374 	.bDeviceClass =		USB_CLASS_VENDOR_SPEC,
375 
376 	.bNumConfigurations =	2,
377 };
378 
379 static const struct usb_descriptor_header *hs_source_sink_function [] = {
380 	(struct usb_descriptor_header *) &otg_descriptor,
381 	(struct usb_descriptor_header *) &source_sink_intf,
382 	(struct usb_descriptor_header *) &hs_source_desc,
383 	(struct usb_descriptor_header *) &hs_sink_desc,
384 	NULL,
385 };
386 
387 static const struct usb_descriptor_header *hs_loopback_function [] = {
388 	(struct usb_descriptor_header *) &otg_descriptor,
389 	(struct usb_descriptor_header *) &loopback_intf,
390 	(struct usb_descriptor_header *) &hs_source_desc,
391 	(struct usb_descriptor_header *) &hs_sink_desc,
392 	NULL,
393 };
394 
395 /* maxpacket and other transfer characteristics vary by speed. */
396 #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
397 
398 #else
399 
400 /* if there's no high speed support, maxpacket doesn't change. */
401 #define ep_desc(g,hs,fs) fs
402 
403 #endif	/* !CONFIG_USB_GADGET_DUALSPEED */
404 
405 static char				manufacturer [50];
406 static char				serial [40];
407 
408 /* static strings, in UTF-8 */
409 static struct usb_string		strings [] = {
410 	{ STRING_MANUFACTURER, manufacturer, },
411 	{ STRING_PRODUCT, longname, },
412 	{ STRING_SERIAL, serial, },
413 	{ STRING_LOOPBACK, loopback, },
414 	{ STRING_SOURCE_SINK, source_sink, },
415 	{  }			/* end of list */
416 };
417 
418 static struct usb_gadget_strings	stringtab = {
419 	.language	= 0x0409,	/* en-us */
420 	.strings	= strings,
421 };
422 
423 /*
424  * config descriptors are also handcrafted.  these must agree with code
425  * that sets configurations, and with code managing interfaces and their
426  * altsettings.  other complexity may come from:
427  *
428  *  - high speed support, including "other speed config" rules
429  *  - multiple configurations
430  *  - interfaces with alternate settings
431  *  - embedded class or vendor-specific descriptors
432  *
433  * this handles high speed, and has a second config that could as easily
434  * have been an alternate interface setting (on most hardware).
435  *
436  * NOTE:  to demonstrate (and test) more USB capabilities, this driver
437  * should include an altsetting to test interrupt transfers, including
438  * high bandwidth modes at high speed.  (Maybe work like Intel's test
439  * device?)
440  */
441 static int
config_buf(struct usb_gadget * gadget,u8 * buf,u8 type,unsigned index)442 config_buf (struct usb_gadget *gadget,
443 		u8 *buf, u8 type, unsigned index)
444 {
445 	int				is_source_sink;
446 	int				len;
447 	const struct usb_descriptor_header **function;
448 #ifdef CONFIG_USB_GADGET_DUALSPEED
449 	int				hs = (gadget->speed == USB_SPEED_HIGH);
450 #endif
451 
452 	/* two configurations will always be index 0 and index 1 */
453 	if (index > 1)
454 		return -EINVAL;
455 	is_source_sink = loopdefault ? (index == 1) : (index == 0);
456 
457 #ifdef CONFIG_USB_GADGET_DUALSPEED
458 	if (type == USB_DT_OTHER_SPEED_CONFIG)
459 		hs = !hs;
460 	if (hs)
461 		function = is_source_sink
462 			? hs_source_sink_function
463 			: hs_loopback_function;
464 	else
465 #endif
466 		function = is_source_sink
467 			? fs_source_sink_function
468 			: fs_loopback_function;
469 
470 	/* for now, don't advertise srp-only devices */
471 	if (!gadget->is_otg)
472 		function++;
473 
474 	len = usb_gadget_config_buf (is_source_sink
475 					? &source_sink_config
476 					: &loopback_config,
477 			buf, USB_BUFSIZ, function);
478 	if (len < 0)
479 		return len;
480 	((struct usb_config_descriptor *) buf)->bDescriptorType = type;
481 	return len;
482 }
483 
484 /*-------------------------------------------------------------------------*/
485 
486 static struct usb_request *
alloc_ep_req(struct usb_ep * ep,unsigned length)487 alloc_ep_req (struct usb_ep *ep, unsigned length)
488 {
489 	struct usb_request	*req;
490 
491 	req = usb_ep_alloc_request (ep, GFP_ATOMIC);
492 	if (req) {
493 		req->length = length;
494 		req->buf = usb_ep_alloc_buffer (ep, length,
495 				&req->dma, GFP_ATOMIC);
496 		if (!req->buf) {
497 			usb_ep_free_request (ep, req);
498 			req = NULL;
499 		}
500 	}
501 	return req;
502 }
503 
free_ep_req(struct usb_ep * ep,struct usb_request * req)504 static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
505 {
506 	if (req->buf)
507 		usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
508 	usb_ep_free_request (ep, req);
509 }
510 
511 /*-------------------------------------------------------------------------*/
512 
513 /* optionally require specific source/sink data patterns  */
514 
515 static int
check_read_data(struct zero_dev * dev,struct usb_ep * ep,struct usb_request * req)516 check_read_data (
517 	struct zero_dev		*dev,
518 	struct usb_ep		*ep,
519 	struct usb_request	*req
520 )
521 {
522 	unsigned	i;
523 	u8		*buf = req->buf;
524 
525 	for (i = 0; i < req->actual; i++, buf++) {
526 		switch (pattern) {
527 		/* all-zeroes has no synchronization issues */
528 		case 0:
529 			if (*buf == 0)
530 				continue;
531 			break;
532 		/* mod63 stays in sync with short-terminated transfers,
533 		 * or otherwise when host and gadget agree on how large
534 		 * each usb transfer request should be.  resync is done
535 		 * with set_interface or set_config.
536 		 */
537 		case 1:
538 			if (*buf == (u8)(i % 63))
539 				continue;
540 			break;
541 		}
542 		ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
543 		usb_ep_set_halt (ep);
544 		return -EINVAL;
545 	}
546 	return 0;
547 }
548 
549 static void
reinit_write_data(struct zero_dev * dev,struct usb_ep * ep,struct usb_request * req)550 reinit_write_data (
551 	struct zero_dev		*dev,
552 	struct usb_ep		*ep,
553 	struct usb_request	*req
554 )
555 {
556 	unsigned	i;
557 	u8		*buf = req->buf;
558 
559 	switch (pattern) {
560 	case 0:
561 		memset (req->buf, 0, req->length);
562 		break;
563 	case 1:
564 		for  (i = 0; i < req->length; i++)
565 			*buf++ = (u8) (i % 63);
566 		break;
567 	}
568 }
569 
570 /* if there is only one request in the queue, there'll always be an
571  * irq delay between end of one request and start of the next.
572  * that prevents using hardware dma queues.
573  */
source_sink_complete(struct usb_ep * ep,struct usb_request * req)574 static void source_sink_complete (struct usb_ep *ep, struct usb_request *req)
575 {
576 	struct zero_dev	*dev = ep->driver_data;
577 	int		status = req->status;
578 
579 	switch (status) {
580 
581 	case 0: 			/* normal completion? */
582 		if (ep == dev->out_ep)
583 			check_read_data (dev, ep, req);
584 		else
585 			reinit_write_data (dev, ep, req);
586 		break;
587 
588 	/* this endpoint is normally active while we're configured */
589 	case -ECONNABORTED: 		/* hardware forced ep reset */
590 	case -ECONNRESET:		/* request dequeued */
591 	case -ESHUTDOWN:		/* disconnect from host */
592 		VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
593 				req->actual, req->length);
594 		if (ep == dev->out_ep)
595 			check_read_data (dev, ep, req);
596 		free_ep_req (ep, req);
597 		return;
598 
599 	case -EOVERFLOW:		/* buffer overrun on read means that
600 					 * we didn't provide a big enough
601 					 * buffer.
602 					 */
603 	default:
604 #if 1
605 		DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
606 				status, req->actual, req->length);
607 #endif
608 	case -EREMOTEIO:		/* short read */
609 		break;
610 	}
611 
612 	status = usb_ep_queue (ep, req, GFP_ATOMIC);
613 	if (status) {
614 		ERROR (dev, "kill %s:  resubmit %d bytes --> %d\n",
615 				ep->name, req->length, status);
616 		usb_ep_set_halt (ep);
617 		/* FIXME recover later ... somehow */
618 	}
619 }
620 
621 static struct usb_request *
source_sink_start_ep(struct usb_ep * ep,int gfp_flags)622 source_sink_start_ep (struct usb_ep *ep, int gfp_flags)
623 {
624 	struct usb_request	*req;
625 	int			status;
626 
627 	req = alloc_ep_req (ep, buflen);
628 	if (!req)
629 		return NULL;
630 
631 	memset (req->buf, 0, req->length);
632 	req->complete = source_sink_complete;
633 
634 	if (strcmp (ep->name, EP_IN_NAME) == 0)
635 		reinit_write_data (ep->driver_data, ep, req);
636 
637 	status = usb_ep_queue (ep, req, gfp_flags);
638 	if (status) {
639 		struct zero_dev	*dev = ep->driver_data;
640 
641 		ERROR (dev, "start %s --> %d\n", ep->name, status);
642 		free_ep_req (ep, req);
643 		req = NULL;
644 	}
645 
646 	return req;
647 }
648 
649 static int
set_source_sink_config(struct zero_dev * dev,int gfp_flags)650 set_source_sink_config (struct zero_dev *dev, int gfp_flags)
651 {
652 	int			result = 0;
653 	struct usb_ep		*ep;
654 	struct usb_gadget	*gadget = dev->gadget;
655 
656 	gadget_for_each_ep (ep, gadget) {
657 		const struct usb_endpoint_descriptor	*d;
658 
659 		/* one endpoint writes (sources) zeroes in (to the host) */
660 		if (strcmp (ep->name, EP_IN_NAME) == 0) {
661 			d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
662 			result = usb_ep_enable (ep, d);
663 			if (result == 0) {
664 				ep->driver_data = dev;
665 				if (source_sink_start_ep (ep, gfp_flags) != 0) {
666 					dev->in_ep = ep;
667 					continue;
668 				}
669 				usb_ep_disable (ep);
670 				result = -EIO;
671 			}
672 
673 		/* one endpoint reads (sinks) anything out (from the host) */
674 		} else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
675 			d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
676 			result = usb_ep_enable (ep, d);
677 			if (result == 0) {
678 				ep->driver_data = dev;
679 				if (source_sink_start_ep (ep, gfp_flags) != 0) {
680 					dev->out_ep = ep;
681 					continue;
682 				}
683 				usb_ep_disable (ep);
684 				result = -EIO;
685 			}
686 
687 		/* ignore any other endpoints */
688 		} else
689 			continue;
690 
691 		/* stop on error */
692 		ERROR (dev, "can't start %s, result %d\n", ep->name, result);
693 		break;
694 	}
695 	if (result == 0)
696 		DBG (dev, "buflen %d\n", buflen);
697 
698 	/* caller is responsible for cleanup on error */
699 	return result;
700 }
701 
702 /*-------------------------------------------------------------------------*/
703 
loopback_complete(struct usb_ep * ep,struct usb_request * req)704 static void loopback_complete (struct usb_ep *ep, struct usb_request *req)
705 {
706 	struct zero_dev	*dev = ep->driver_data;
707 	int		status = req->status;
708 
709 	switch (status) {
710 
711 	case 0: 			/* normal completion? */
712 		if (ep == dev->out_ep) {
713 			/* loop this OUT packet back IN to the host */
714 			req->zero = (req->actual < req->length);
715 			req->length = req->actual;
716 			status = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
717 			if (status == 0)
718 				return;
719 
720 			/* "should never get here" */
721 			ERROR (dev, "can't loop %s to %s: %d\n",
722 				ep->name, dev->in_ep->name,
723 				status);
724 		}
725 
726 		/* queue the buffer for some later OUT packet */
727 		req->length = buflen;
728 		status = usb_ep_queue (dev->out_ep, req, GFP_ATOMIC);
729 		if (status == 0)
730 			return;
731 
732 		/* "should never get here" */
733 		/* FALLTHROUGH */
734 
735 	default:
736 		ERROR (dev, "%s loop complete --> %d, %d/%d\n", ep->name,
737 				status, req->actual, req->length);
738 		/* FALLTHROUGH */
739 
740 	/* NOTE:  since this driver doesn't maintain an explicit record
741 	 * of requests it submitted (just maintains qlen count), we
742 	 * rely on the hardware driver to clean up on disconnect or
743 	 * endpoint disable.
744 	 */
745 	case -ECONNABORTED: 		/* hardware forced ep reset */
746 	case -ECONNRESET:		/* request dequeued */
747 	case -ESHUTDOWN:		/* disconnect from host */
748 		free_ep_req (ep, req);
749 		return;
750 	}
751 }
752 
753 static int
set_loopback_config(struct zero_dev * dev,int gfp_flags)754 set_loopback_config (struct zero_dev *dev, int gfp_flags)
755 {
756 	int			result = 0;
757 	struct usb_ep		*ep;
758 	struct usb_gadget	*gadget = dev->gadget;
759 
760 	gadget_for_each_ep (ep, gadget) {
761 		const struct usb_endpoint_descriptor	*d;
762 
763 		/* one endpoint writes data back IN to the host */
764 		if (strcmp (ep->name, EP_IN_NAME) == 0) {
765 			d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
766 			result = usb_ep_enable (ep, d);
767 			if (result == 0) {
768 				ep->driver_data = dev;
769 				dev->in_ep = ep;
770 				continue;
771 			}
772 
773 		/* one endpoint just reads OUT packets */
774 		} else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
775 			d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
776 			result = usb_ep_enable (ep, d);
777 			if (result == 0) {
778 				ep->driver_data = dev;
779 				dev->out_ep = ep;
780 				continue;
781 			}
782 
783 		/* ignore any other endpoints */
784 		} else
785 			continue;
786 
787 		/* stop on error */
788 		ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
789 		break;
790 	}
791 
792 	/* allocate a bunch of read buffers and queue them all at once.
793 	 * we buffer at most 'qlen' transfers; fewer if any need more
794 	 * than 'buflen' bytes each.
795 	 */
796 	if (result == 0) {
797 		struct usb_request	*req;
798 		unsigned		i;
799 
800 		ep = dev->out_ep;
801 		for (i = 0; i < qlen && result == 0; i++) {
802 			req = alloc_ep_req (ep, buflen);
803 			if (req) {
804 				req->complete = loopback_complete;
805 				result = usb_ep_queue (ep, req, GFP_ATOMIC);
806 				if (result)
807 					DBG (dev, "%s queue req --> %d\n",
808 							ep->name, result);
809 			} else
810 				result = -ENOMEM;
811 		}
812 	}
813 	if (result == 0)
814 		DBG (dev, "qlen %d, buflen %d\n", qlen, buflen);
815 
816 	/* caller is responsible for cleanup on error */
817 	return result;
818 }
819 
820 /*-------------------------------------------------------------------------*/
821 
zero_reset_config(struct zero_dev * dev)822 static void zero_reset_config (struct zero_dev *dev)
823 {
824 	if (dev->config == 0)
825 		return;
826 
827 	DBG (dev, "reset config\n");
828 
829 	/* just disable endpoints, forcing completion of pending i/o.
830 	 * all our completion handlers free their requests in this case.
831 	 */
832 	if (dev->in_ep) {
833 		usb_ep_disable (dev->in_ep);
834 		dev->in_ep = NULL;
835 	}
836 	if (dev->out_ep) {
837 		usb_ep_disable (dev->out_ep);
838 		dev->out_ep = NULL;
839 	}
840 	dev->config = 0;
841 	del_timer (&dev->resume);
842 }
843 
844 /* change our operational config.  this code must agree with the code
845  * that returns config descriptors, and altsetting code.
846  *
847  * it's also responsible for power management interactions. some
848  * configurations might not work with our current power sources.
849  *
850  * note that some device controller hardware will constrain what this
851  * code can do, perhaps by disallowing more than one configuration or
852  * by limiting configuration choices (like the pxa2xx).
853  */
854 static int
zero_set_config(struct zero_dev * dev,unsigned number,int gfp_flags)855 zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
856 {
857 	int			result = 0;
858 	struct usb_gadget	*gadget = dev->gadget;
859 
860 	if (number == dev->config)
861 		return 0;
862 
863 	if (gadget_is_sa1100 (gadget) && dev->config) {
864 		/* tx fifo is full, but we can't clear it...*/
865 		INFO (dev, "can't change configurations\n");
866 		return -ESPIPE;
867 	}
868 	zero_reset_config (dev);
869 
870 	switch (number) {
871 	case CONFIG_SOURCE_SINK:
872 		result = set_source_sink_config (dev, gfp_flags);
873 		break;
874 	case CONFIG_LOOPBACK:
875 		result = set_loopback_config (dev, gfp_flags);
876 		break;
877 	default:
878 		result = -EINVAL;
879 		/* FALL THROUGH */
880 	case 0:
881 		return result;
882 	}
883 
884 	if (!result && (!dev->in_ep || !dev->out_ep))
885 		result = -ENODEV;
886 	if (result)
887 		zero_reset_config (dev);
888 	else {
889 		char *speed;
890 
891 		switch (gadget->speed) {
892 		case USB_SPEED_LOW:	speed = "low"; break;
893 		case USB_SPEED_FULL:	speed = "full"; break;
894 		case USB_SPEED_HIGH:	speed = "high"; break;
895 		default: 		speed = "?"; break;
896 		}
897 
898 		dev->config = number;
899 		INFO (dev, "%s speed config #%d: %s\n", speed, number,
900 				(number == CONFIG_SOURCE_SINK)
901 					? source_sink : loopback);
902 	}
903 	return result;
904 }
905 
906 /*-------------------------------------------------------------------------*/
907 
zero_setup_complete(struct usb_ep * ep,struct usb_request * req)908 static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
909 {
910 	if (req->status || req->actual != req->length)
911 		DBG ((struct zero_dev *) ep->driver_data,
912 				"setup complete --> %d, %d/%d\n",
913 				req->status, req->actual, req->length);
914 }
915 
916 /*
917  * The setup() callback implements all the ep0 functionality that's
918  * not handled lower down, in hardware or the hardware driver (like
919  * device and endpoint feature flags, and their status).  It's all
920  * housekeeping for the gadget function we're implementing.  Most of
921  * the work is in config-specific setup.
922  */
923 static int
zero_setup(struct usb_gadget * gadget,const struct usb_ctrlrequest * ctrl)924 zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
925 {
926 	struct zero_dev		*dev = get_gadget_data (gadget);
927 	struct usb_request	*req = dev->req;
928 	int			value = -EOPNOTSUPP;
929 
930 	/* usually this stores reply data in the pre-allocated ep0 buffer,
931 	 * but config change events will reconfigure hardware.
932 	 */
933 	req->zero = 0;
934 	switch (ctrl->bRequest) {
935 
936 	case USB_REQ_GET_DESCRIPTOR:
937 		if (ctrl->bRequestType != USB_DIR_IN)
938 			goto unknown;
939 		switch (ctrl->wValue >> 8) {
940 
941 		case USB_DT_DEVICE:
942 			value = min (ctrl->wLength, (u16) sizeof device_desc);
943 			memcpy (req->buf, &device_desc, value);
944 			break;
945 #ifdef CONFIG_USB_GADGET_DUALSPEED
946 		case USB_DT_DEVICE_QUALIFIER:
947 			if (!gadget->is_dualspeed)
948 				break;
949 			value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
950 			memcpy (req->buf, &dev_qualifier, value);
951 			break;
952 
953 		case USB_DT_OTHER_SPEED_CONFIG:
954 			if (!gadget->is_dualspeed)
955 				break;
956 			// FALLTHROUGH
957 #endif /* CONFIG_USB_GADGET_DUALSPEED */
958 		case USB_DT_CONFIG:
959 			value = config_buf (gadget, req->buf,
960 					ctrl->wValue >> 8,
961 					ctrl->wValue & 0xff);
962 			if (value >= 0)
963 				value = min (ctrl->wLength, (u16) value);
964 			break;
965 
966 		case USB_DT_STRING:
967 			/* wIndex == language code.
968 			 * this driver only handles one language, you can
969 			 * add string tables for other languages, using
970 			 * any UTF-8 characters
971 			 */
972 			value = usb_gadget_get_string (&stringtab,
973 					ctrl->wValue & 0xff, req->buf);
974 			if (value >= 0)
975 				value = min (ctrl->wLength, (u16) value);
976 			break;
977 		}
978 		break;
979 
980 	/* currently two configs, two speeds */
981 	case USB_REQ_SET_CONFIGURATION:
982 		if (ctrl->bRequestType != 0)
983 			goto unknown;
984 		if (gadget->a_hnp_support)
985 			DBG (dev, "HNP available\n");
986 		else if (gadget->a_alt_hnp_support)
987 			DBG (dev, "HNP needs a different root port\n");
988 		else
989 			VDBG (dev, "HNP inactive\n");
990 		spin_lock (&dev->lock);
991 		value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
992 		spin_unlock (&dev->lock);
993 		break;
994 	case USB_REQ_GET_CONFIGURATION:
995 		if (ctrl->bRequestType != USB_DIR_IN)
996 			goto unknown;
997 		*(u8 *)req->buf = dev->config;
998 		value = min (ctrl->wLength, (u16) 1);
999 		break;
1000 
1001 	/* until we add altsetting support, or other interfaces,
1002 	 * only 0/0 are possible.  pxa2xx only supports 0/0 (poorly)
1003 	 * and already killed pending endpoint I/O.
1004 	 */
1005 	case USB_REQ_SET_INTERFACE:
1006 		if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1007 			goto unknown;
1008 		spin_lock (&dev->lock);
1009 		if (dev->config && ctrl->wIndex == 0 && ctrl->wValue == 0) {
1010 			u8		config = dev->config;
1011 
1012 			/* resets interface configuration, forgets about
1013 			 * previous transaction state (queued bufs, etc)
1014 			 * and re-inits endpoint state (toggle etc)
1015 			 * no response queued, just zero status == success.
1016 			 * if we had more than one interface we couldn't
1017 			 * use this "reset the config" shortcut.
1018 			 */
1019 			zero_reset_config (dev);
1020 			zero_set_config (dev, config, GFP_ATOMIC);
1021 			value = 0;
1022 		}
1023 		spin_unlock (&dev->lock);
1024 		break;
1025 	case USB_REQ_GET_INTERFACE:
1026 		if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1027 			goto unknown;
1028 		if (!dev->config)
1029 			break;
1030 		if (ctrl->wIndex != 0) {
1031 			value = -EDOM;
1032 			break;
1033 		}
1034 		*(u8 *)req->buf = 0;
1035 		value = min (ctrl->wLength, (u16) 1);
1036 		break;
1037 
1038 	/*
1039 	 * These are the same vendor-specific requests supported by
1040 	 * Intel's USB 2.0 compliance test devices.  We exceed that
1041 	 * device spec by allowing multiple-packet requests.
1042 	 */
1043 	case 0x5b:	/* control WRITE test -- fill the buffer */
1044 		if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
1045 			goto unknown;
1046 		if (ctrl->wValue || ctrl->wIndex)
1047 			break;
1048 		/* just read that many bytes into the buffer */
1049 		if (ctrl->wLength > USB_BUFSIZ)
1050 			break;
1051 		value = ctrl->wLength;
1052 		break;
1053 	case 0x5c:	/* control READ test -- return the buffer */
1054 		if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
1055 			goto unknown;
1056 		if (ctrl->wValue || ctrl->wIndex)
1057 			break;
1058 		/* expect those bytes are still in the buffer; send back */
1059 		if (ctrl->wLength > USB_BUFSIZ
1060 				|| ctrl->wLength != req->length)
1061 			break;
1062 		value = ctrl->wLength;
1063 		break;
1064 
1065 	default:
1066 unknown:
1067 		VDBG (dev,
1068 			"unknown control req%02x.%02x v%04x i%04x l%d\n",
1069 			ctrl->bRequestType, ctrl->bRequest,
1070 			ctrl->wValue, ctrl->wIndex, ctrl->wLength);
1071 	}
1072 
1073 	/* respond with data transfer before status phase? */
1074 	if (value >= 0) {
1075 		req->length = value;
1076 		req->zero = value < ctrl->wLength
1077 				&& (value % gadget->ep0->maxpacket) == 0;
1078 		value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
1079 		if (value < 0) {
1080 			DBG (dev, "ep_queue --> %d\n", value);
1081 			req->status = 0;
1082 			zero_setup_complete (gadget->ep0, req);
1083 		}
1084 	}
1085 
1086 	/* device either stalls (value < 0) or reports success */
1087 	return value;
1088 }
1089 
1090 static void
zero_disconnect(struct usb_gadget * gadget)1091 zero_disconnect (struct usb_gadget *gadget)
1092 {
1093 	struct zero_dev		*dev = get_gadget_data (gadget);
1094 	unsigned long		flags;
1095 
1096 	spin_lock_irqsave (&dev->lock, flags);
1097 	zero_reset_config (dev);
1098 
1099 	/* a more significant application might have some non-usb
1100 	 * activities to quiesce here, saving resources like power
1101 	 * or pushing the notification up a network stack.
1102 	 */
1103 	spin_unlock_irqrestore (&dev->lock, flags);
1104 
1105 	/* next we may get setup() calls to enumerate new connections;
1106 	 * or an unbind() during shutdown (including removing module).
1107 	 */
1108 }
1109 
1110 static void
zero_autoresume(unsigned long _dev)1111 zero_autoresume (unsigned long _dev)
1112 {
1113 	struct zero_dev	*dev = (struct zero_dev *) _dev;
1114 	int		status;
1115 
1116 	/* normally the host would be woken up for something
1117 	 * more significant than just a timer firing...
1118 	 */
1119 	if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
1120 		status = usb_gadget_wakeup (dev->gadget);
1121 		DBG (dev, "wakeup --> %d\n", status);
1122 	}
1123 }
1124 
1125 /*-------------------------------------------------------------------------*/
1126 
1127 static void
zero_unbind(struct usb_gadget * gadget)1128 zero_unbind (struct usb_gadget *gadget)
1129 {
1130 	struct zero_dev		*dev = get_gadget_data (gadget);
1131 
1132 	DBG (dev, "unbind\n");
1133 
1134 	/* we've already been disconnected ... no i/o is active */
1135 	if (dev->req)
1136 		free_ep_req (gadget->ep0, dev->req);
1137 	del_timer_sync (&dev->resume);
1138 	kfree (dev);
1139 	set_gadget_data (gadget, NULL);
1140 }
1141 
1142 static int
zero_bind(struct usb_gadget * gadget)1143 zero_bind (struct usb_gadget *gadget)
1144 {
1145 	struct zero_dev		*dev;
1146 	struct usb_ep		*ep;
1147 
1148 	/* Bulk-only drivers like this one SHOULD be able to
1149 	 * autoconfigure on any sane usb controller driver,
1150 	 * but there may also be important quirks to address.
1151 	 */
1152 	usb_ep_autoconfig_reset (gadget);
1153 	ep = usb_ep_autoconfig (gadget, &fs_source_desc);
1154 	if (!ep) {
1155 autoconf_fail:
1156 		printk (KERN_ERR "%s: can't autoconfigure on %s\n",
1157 			shortname, gadget->name);
1158 		return -ENODEV;
1159 	}
1160 	EP_IN_NAME = ep->name;
1161 	ep->driver_data = ep;	/* claim */
1162 
1163 	ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
1164 	if (!ep)
1165 		goto autoconf_fail;
1166 	EP_OUT_NAME = ep->name;
1167 	ep->driver_data = ep;	/* claim */
1168 
1169 
1170 	/*
1171 	 * DRIVER POLICY CHOICE:  you may want to do this differently.
1172 	 * One thing to avoid is reusing a bcdDevice revision code
1173 	 * with different host-visible configurations or behavior
1174 	 * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
1175 	 */
1176 	if (gadget_is_net2280 (gadget)) {
1177 		device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
1178 	} else if (gadget_is_pxa (gadget)) {
1179 		device_desc.bcdDevice = __constant_cpu_to_le16 (0x0203);
1180 #if 0
1181 	} else if (gadget_is_sh(gadget)) {
1182 		device_desc.bcdDevice = __constant_cpu_to_le16 (0x0204);
1183 		/* SH has only one configuration; see "loopdefault" */
1184 		device_desc.bNumConfigurations = 1;
1185 		/* FIXME make 1 == default.bConfigurationValue */
1186 #endif
1187 	} else if (gadget_is_sa1100 (gadget)) {
1188 		device_desc.bcdDevice = __constant_cpu_to_le16 (0x0205);
1189 	} else if (gadget_is_goku (gadget)) {
1190 		device_desc.bcdDevice = __constant_cpu_to_le16 (0x0206);
1191 	} else if (gadget_is_mq11xx (gadget)) {
1192 		device_desc.bcdDevice = __constant_cpu_to_le16 (0x0207);
1193 	} else if (gadget_is_omap (gadget)) {
1194 		device_desc.bcdDevice = __constant_cpu_to_le16 (0x0208);
1195 	} else if (gadget_is_lh7a40x(gadget)) {
1196 		device_desc.bcdDevice = __constant_cpu_to_le16 (0x0209);
1197 	} else if (gadget_is_n9604(gadget)) {
1198 		device_desc.bcdDevice = __constant_cpu_to_le16 (0x0210);
1199 	} else if (gadget_is_pxa27x(gadget)) {
1200 		device_desc.bcdDevice = __constant_cpu_to_le16 (0x0211);
1201 	} else {
1202 		/* gadget zero is so simple (for now, no altsettings) that
1203 		 * it SHOULD NOT have problems with bulk-capable hardware.
1204 		 * so warn about unrcognized controllers, don't panic.
1205 		 *
1206 		 * things like configuration and altsetting numbering
1207 		 * can need hardware-specific attention though.
1208 		 */
1209 		printk (KERN_WARNING "%s: controller '%s' not recognized\n",
1210 			shortname, gadget->name);
1211 		device_desc.bcdDevice = __constant_cpu_to_le16 (0x9999);
1212 	}
1213 
1214 
1215 	/* ok, we made sense of the hardware ... */
1216 	dev = kmalloc (sizeof *dev, SLAB_KERNEL);
1217 	if (!dev)
1218 		return -ENOMEM;
1219 	memset (dev, 0, sizeof *dev);
1220 	spin_lock_init (&dev->lock);
1221 	dev->gadget = gadget;
1222 	set_gadget_data (gadget, dev);
1223 
1224 	/* preallocate control response and buffer */
1225 	dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
1226 	if (!dev->req)
1227 		goto enomem;
1228 	dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
1229 				&dev->req->dma, GFP_KERNEL);
1230 	if (!dev->req->buf)
1231 		goto enomem;
1232 
1233 	dev->req->complete = zero_setup_complete;
1234 
1235 	device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1236 
1237 #ifdef CONFIG_USB_GADGET_DUALSPEED
1238 	/* assume ep0 uses the same value for both speeds ... */
1239 	dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
1240 
1241 	/* and that all endpoints are dual-speed */
1242 	hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
1243 	hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
1244 #endif
1245 
1246 	if (gadget->is_otg) {
1247 		otg_descriptor.bmAttributes |= USB_OTG_HNP,
1248 		source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1249 		loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1250 	}
1251 
1252 	if (gadget->is_otg) {
1253 		otg_descriptor.bmAttributes |= USB_OTG_HNP,
1254 		source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1255 		loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1256 	}
1257 
1258 	usb_gadget_set_selfpowered (gadget);
1259 
1260 	init_timer (&dev->resume);
1261 	dev->resume.function = zero_autoresume;
1262 	dev->resume.data = (unsigned long) dev;
1263 	if (autoresume) {
1264 		source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1265 		loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1266 	}
1267 
1268 	gadget->ep0->driver_data = dev;
1269 
1270 	INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
1271 	INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
1272 		EP_OUT_NAME, EP_IN_NAME);
1273 
1274 	snprintf (manufacturer, sizeof manufacturer,
1275 		UTS_SYSNAME " " UTS_RELEASE " with %s",
1276 		gadget->name);
1277 
1278 	return 0;
1279 
1280 enomem:
1281 	zero_unbind (gadget);
1282 	return -ENOMEM;
1283 }
1284 
1285 /*-------------------------------------------------------------------------*/
1286 
1287 static void
zero_suspend(struct usb_gadget * gadget)1288 zero_suspend (struct usb_gadget *gadget)
1289 {
1290 	struct zero_dev		*dev = get_gadget_data (gadget);
1291 
1292 	if (gadget->speed == USB_SPEED_UNKNOWN)
1293 		return;
1294 
1295 	if (autoresume) {
1296 		mod_timer (&dev->resume, jiffies + (HZ * autoresume));
1297 		DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
1298 	} else
1299 		DBG (dev, "suspend\n");
1300 }
1301 
1302 static void
zero_resume(struct usb_gadget * gadget)1303 zero_resume (struct usb_gadget *gadget)
1304 {
1305 	struct zero_dev		*dev = get_gadget_data (gadget);
1306 
1307 	DBG (dev, "resume\n");
1308 	del_timer (&dev->resume);
1309 }
1310 
1311 
1312 /*-------------------------------------------------------------------------*/
1313 
1314 static struct usb_gadget_driver zero_driver = {
1315 #ifdef CONFIG_USB_GADGET_DUALSPEED
1316 	.speed		= USB_SPEED_HIGH,
1317 #else
1318 	.speed		= USB_SPEED_FULL,
1319 #endif
1320 	.function	= (char *) longname,
1321 	.bind		= zero_bind,
1322 	.unbind		= zero_unbind,
1323 
1324 	.setup		= zero_setup,
1325 	.disconnect	= zero_disconnect,
1326 
1327 	.suspend	= zero_suspend,
1328 	.resume		= zero_resume,
1329 
1330 	.driver 	= {
1331 		.name		= (char *) shortname,
1332 		// .shutdown = ...
1333 		// .suspend = ...
1334 		// .resume = ...
1335 	},
1336 };
1337 
1338 MODULE_AUTHOR ("David Brownell");
1339 MODULE_LICENSE ("Dual BSD/GPL");
1340 
1341 
init(void)1342 static int __init init (void)
1343 {
1344 	/* a real value would likely come through some id prom
1345 	 * or module option.  this one takes at least two packets.
1346 	 */
1347 	strncpy (serial, "0123456789.0123456789.0123456789", sizeof serial);
1348 	serial [sizeof serial - 1] = 0;
1349 
1350 	return usb_gadget_register_driver (&zero_driver);
1351 }
1352 module_init (init);
1353 
cleanup(void)1354 static void __exit cleanup (void)
1355 {
1356 	usb_gadget_unregister_driver (&zero_driver);
1357 }
1358 module_exit (cleanup);
1359 
1360