dummy_hcd.c revision 0fb5759952e934dd5ff25fd79f45cb5d69c8d2d1
1/*
2 * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
3 *
4 * Maintainer: Alan Stern <stern@rowland.harvard.edu>
5 *
6 * Copyright (C) 2003 David Brownell
7 * Copyright (C) 2003-2005 Alan Stern
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 */
23
24
25/*
26 * This exposes a device side "USB gadget" API, driven by requests to a
27 * Linux-USB host controller driver.  USB traffic is simulated; there's
28 * no need for USB hardware.  Use this with two other drivers:
29 *
30 *  - Gadget driver, responding to requests (slave);
31 *  - Host-side device driver, as already familiar in Linux.
32 *
33 * Having this all in one kernel can help some stages of development,
34 * bypassing some hardware (and driver) issues.  UML could help too.
35 */
36
37#include <linux/module.h>
38#include <linux/kernel.h>
39#include <linux/delay.h>
40#include <linux/ioport.h>
41#include <linux/slab.h>
42#include <linux/errno.h>
43#include <linux/init.h>
44#include <linux/timer.h>
45#include <linux/list.h>
46#include <linux/interrupt.h>
47#include <linux/platform_device.h>
48#include <linux/usb.h>
49#include <linux/usb/gadget.h>
50#include <linux/usb/hcd.h>
51
52#include <asm/byteorder.h>
53#include <asm/io.h>
54#include <asm/irq.h>
55#include <asm/system.h>
56#include <asm/unaligned.h>
57
58
59#define DRIVER_DESC	"USB Host+Gadget Emulator"
60#define DRIVER_VERSION	"02 May 2005"
61
62#define POWER_BUDGET	500	/* in mA; use 8 for low-power port testing */
63
64static const char	driver_name [] = "dummy_hcd";
65static const char	driver_desc [] = "USB Host+Gadget Emulator";
66
67static const char	gadget_name [] = "dummy_udc";
68
69MODULE_DESCRIPTION (DRIVER_DESC);
70MODULE_AUTHOR ("David Brownell");
71MODULE_LICENSE ("GPL");
72
73struct dummy_hcd_module_parameters {
74	bool is_super_speed;
75	bool is_high_speed;
76};
77
78static struct dummy_hcd_module_parameters mod_data = {
79	.is_super_speed = false,
80	.is_high_speed = true,
81};
82module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
83MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
84module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
85MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
86/*-------------------------------------------------------------------------*/
87
88/* gadget side driver data structres */
89struct dummy_ep {
90	struct list_head		queue;
91	unsigned long			last_io;	/* jiffies timestamp */
92	struct usb_gadget		*gadget;
93	const struct usb_endpoint_descriptor *desc;
94	struct usb_ep			ep;
95	unsigned			halted : 1;
96	unsigned			wedged : 1;
97	unsigned			already_seen : 1;
98	unsigned			setup_stage : 1;
99};
100
101struct dummy_request {
102	struct list_head		queue;		/* ep's requests */
103	struct usb_request		req;
104};
105
106static inline struct dummy_ep *usb_ep_to_dummy_ep (struct usb_ep *_ep)
107{
108	return container_of (_ep, struct dummy_ep, ep);
109}
110
111static inline struct dummy_request *usb_request_to_dummy_request
112		(struct usb_request *_req)
113{
114	return container_of (_req, struct dummy_request, req);
115}
116
117/*-------------------------------------------------------------------------*/
118
119/*
120 * Every device has ep0 for control requests, plus up to 30 more endpoints,
121 * in one of two types:
122 *
123 *   - Configurable:  direction (in/out), type (bulk, iso, etc), and endpoint
124 *     number can be changed.  Names like "ep-a" are used for this type.
125 *
126 *   - Fixed Function:  in other cases.  some characteristics may be mutable;
127 *     that'd be hardware-specific.  Names like "ep12out-bulk" are used.
128 *
129 * Gadget drivers are responsible for not setting up conflicting endpoint
130 * configurations, illegal or unsupported packet lengths, and so on.
131 */
132
133static const char ep0name [] = "ep0";
134
135static const char *const ep_name [] = {
136	ep0name,				/* everyone has ep0 */
137
138	/* act like a net2280: high speed, six configurable endpoints */
139	"ep-a", "ep-b", "ep-c", "ep-d", "ep-e", "ep-f",
140
141	/* or like pxa250: fifteen fixed function endpoints */
142	"ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
143	"ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
144	"ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
145		"ep15in-int",
146
147	/* or like sa1100: two fixed function endpoints */
148	"ep1out-bulk", "ep2in-bulk",
149};
150#define DUMMY_ENDPOINTS	ARRAY_SIZE(ep_name)
151
152/*-------------------------------------------------------------------------*/
153
154#define FIFO_SIZE		64
155
156struct urbp {
157	struct urb		*urb;
158	struct list_head	urbp_list;
159};
160
161
162enum dummy_rh_state {
163	DUMMY_RH_RESET,
164	DUMMY_RH_SUSPENDED,
165	DUMMY_RH_RUNNING
166};
167
168struct dummy_hcd {
169	struct dummy			*dum;
170	enum dummy_rh_state		rh_state;
171	struct timer_list		timer;
172	u32				port_status;
173	u32				old_status;
174	unsigned long			re_timeout;
175
176	struct usb_device		*udev;
177	struct list_head		urbp_list;
178
179	unsigned			active:1;
180	unsigned			old_active:1;
181	unsigned			resuming:1;
182};
183
184struct dummy {
185	spinlock_t			lock;
186
187	/*
188	 * SLAVE/GADGET side support
189	 */
190	struct dummy_ep			ep [DUMMY_ENDPOINTS];
191	int				address;
192	struct usb_gadget		gadget;
193	struct usb_gadget_driver	*driver;
194	struct dummy_request		fifo_req;
195	u8				fifo_buf [FIFO_SIZE];
196	u16				devstatus;
197	unsigned			udc_suspended:1;
198	unsigned			pullup:1;
199
200	/*
201	 * MASTER/HOST side support
202	 */
203	struct dummy_hcd		*hs_hcd;
204	struct dummy_hcd		*ss_hcd;
205};
206
207static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
208{
209	return (struct dummy_hcd *) (hcd->hcd_priv);
210}
211
212static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
213{
214	return container_of((void *) dum, struct usb_hcd, hcd_priv);
215}
216
217static inline struct device *dummy_dev(struct dummy_hcd *dum)
218{
219	return dummy_hcd_to_hcd(dum)->self.controller;
220}
221
222static inline struct device *udc_dev (struct dummy *dum)
223{
224	return dum->gadget.dev.parent;
225}
226
227static inline struct dummy *ep_to_dummy (struct dummy_ep *ep)
228{
229	return container_of (ep->gadget, struct dummy, gadget);
230}
231
232static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
233{
234	struct dummy *dum = container_of(gadget, struct dummy, gadget);
235	if (dum->gadget.speed == USB_SPEED_SUPER)
236		return dum->ss_hcd;
237	else
238		return dum->hs_hcd;
239}
240
241static inline struct dummy *gadget_dev_to_dummy (struct device *dev)
242{
243	return container_of (dev, struct dummy, gadget.dev);
244}
245
246static struct dummy			the_controller;
247
248/*-------------------------------------------------------------------------*/
249
250/* SLAVE/GADGET SIDE UTILITY ROUTINES */
251
252/* called with spinlock held */
253static void nuke (struct dummy *dum, struct dummy_ep *ep)
254{
255	while (!list_empty (&ep->queue)) {
256		struct dummy_request	*req;
257
258		req = list_entry (ep->queue.next, struct dummy_request, queue);
259		list_del_init (&req->queue);
260		req->req.status = -ESHUTDOWN;
261
262		spin_unlock (&dum->lock);
263		req->req.complete (&ep->ep, &req->req);
264		spin_lock (&dum->lock);
265	}
266}
267
268/* caller must hold lock */
269static void
270stop_activity (struct dummy *dum)
271{
272	struct dummy_ep	*ep;
273
274	/* prevent any more requests */
275	dum->address = 0;
276
277	/* The timer is left running so that outstanding URBs can fail */
278
279	/* nuke any pending requests first, so driver i/o is quiesced */
280	list_for_each_entry (ep, &dum->gadget.ep_list, ep.ep_list)
281		nuke (dum, ep);
282
283	/* driver now does any non-usb quiescing necessary */
284}
285
286/**
287 * set_link_state_by_speed() - Sets the current state of the link according to
288 *	the hcd speed
289 * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
290 *
291 * This function updates the port_status according to the link state and the
292 * speed of the hcd.
293 */
294static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
295{
296	struct dummy *dum = dum_hcd->dum;
297
298	if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
299		if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
300			dum_hcd->port_status = 0;
301		} else if (!dum->pullup || dum->udc_suspended) {
302			/* UDC suspend must cause a disconnect */
303			dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
304						USB_PORT_STAT_ENABLE);
305			if ((dum_hcd->old_status &
306			     USB_PORT_STAT_CONNECTION) != 0)
307				dum_hcd->port_status |=
308					(USB_PORT_STAT_C_CONNECTION << 16);
309		} else {
310			/* device is connected and not suspended */
311			dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
312						 USB_PORT_STAT_SPEED_5GBPS) ;
313			if ((dum_hcd->old_status &
314			     USB_PORT_STAT_CONNECTION) == 0)
315				dum_hcd->port_status |=
316					(USB_PORT_STAT_C_CONNECTION << 16);
317			if ((dum_hcd->port_status &
318			     USB_PORT_STAT_ENABLE) == 1 &&
319				(dum_hcd->port_status &
320				 USB_SS_PORT_LS_U0) == 1 &&
321				dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
322				dum_hcd->active = 1;
323		}
324	} else {
325		if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
326			dum_hcd->port_status = 0;
327		} else if (!dum->pullup || dum->udc_suspended) {
328			/* UDC suspend must cause a disconnect */
329			dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
330						USB_PORT_STAT_ENABLE |
331						USB_PORT_STAT_LOW_SPEED |
332						USB_PORT_STAT_HIGH_SPEED |
333						USB_PORT_STAT_SUSPEND);
334			if ((dum_hcd->old_status &
335			     USB_PORT_STAT_CONNECTION) != 0)
336				dum_hcd->port_status |=
337					(USB_PORT_STAT_C_CONNECTION << 16);
338		} else {
339			dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
340			if ((dum_hcd->old_status &
341			     USB_PORT_STAT_CONNECTION) == 0)
342				dum_hcd->port_status |=
343					(USB_PORT_STAT_C_CONNECTION << 16);
344			if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
345				dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
346			else if ((dum_hcd->port_status &
347				  USB_PORT_STAT_SUSPEND) == 0 &&
348					dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
349				dum_hcd->active = 1;
350		}
351	}
352}
353
354/* caller must hold lock */
355static void set_link_state(struct dummy_hcd *dum_hcd)
356{
357	struct dummy *dum = dum_hcd->dum;
358
359	dum_hcd->active = 0;
360	if (dum->pullup)
361		if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
362		     dum->gadget.speed != USB_SPEED_SUPER) ||
363		    (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
364		     dum->gadget.speed == USB_SPEED_SUPER))
365			return;
366
367	set_link_state_by_speed(dum_hcd);
368
369	if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
370	     dum_hcd->active)
371		dum_hcd->resuming = 0;
372
373	/* if !connected or reset */
374	if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
375			(dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
376		/*
377		 * We're connected and not reset (reset occurred now),
378		 * and driver attached - disconnect!
379		 */
380		if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
381		    (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
382		    dum->driver) {
383			stop_activity(dum);
384			spin_unlock(&dum->lock);
385			dum->driver->disconnect(&dum->gadget);
386			spin_lock(&dum->lock);
387		}
388	} else if (dum_hcd->active != dum_hcd->old_active) {
389		if (dum_hcd->old_active && dum->driver->suspend) {
390			spin_unlock(&dum->lock);
391			dum->driver->suspend(&dum->gadget);
392			spin_lock(&dum->lock);
393		} else if (!dum_hcd->old_active &&  dum->driver->resume) {
394			spin_unlock(&dum->lock);
395			dum->driver->resume(&dum->gadget);
396			spin_lock(&dum->lock);
397		}
398	}
399
400	dum_hcd->old_status = dum_hcd->port_status;
401	dum_hcd->old_active = dum_hcd->active;
402}
403
404/*-------------------------------------------------------------------------*/
405
406/* SLAVE/GADGET SIDE DRIVER
407 *
408 * This only tracks gadget state.  All the work is done when the host
409 * side tries some (emulated) i/o operation.  Real device controller
410 * drivers would do real i/o using dma, fifos, irqs, timers, etc.
411 */
412
413#define is_enabled(dum) \
414	(dum->port_status & USB_PORT_STAT_ENABLE)
415
416static int
417dummy_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
418{
419	struct dummy		*dum;
420	struct dummy_hcd	*dum_hcd;
421	struct dummy_ep		*ep;
422	unsigned		max;
423	int			retval;
424
425	ep = usb_ep_to_dummy_ep (_ep);
426	if (!_ep || !desc || ep->desc || _ep->name == ep0name
427			|| desc->bDescriptorType != USB_DT_ENDPOINT)
428		return -EINVAL;
429	dum = ep_to_dummy (ep);
430	if (!dum->driver)
431		return -ESHUTDOWN;
432
433	dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
434	if (!is_enabled(dum_hcd))
435		return -ESHUTDOWN;
436
437	/*
438	 * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
439	 * maximum packet size.
440	 * For SS devices the wMaxPacketSize is limited by 1024.
441	 */
442	max = le16_to_cpu(desc->wMaxPacketSize) & 0x7ff;
443
444	/* drivers must not request bad settings, since lower levels
445	 * (hardware or its drivers) may not check.  some endpoints
446	 * can't do iso, many have maxpacket limitations, etc.
447	 *
448	 * since this "hardware" driver is here to help debugging, we
449	 * have some extra sanity checks.  (there could be more though,
450	 * especially for "ep9out" style fixed function ones.)
451	 */
452	retval = -EINVAL;
453	switch (desc->bmAttributes & 0x03) {
454	case USB_ENDPOINT_XFER_BULK:
455		if (strstr (ep->ep.name, "-iso")
456				|| strstr (ep->ep.name, "-int")) {
457			goto done;
458		}
459		switch (dum->gadget.speed) {
460		case USB_SPEED_SUPER:
461			if (max == 1024)
462				break;
463			goto done;
464		case USB_SPEED_HIGH:
465			if (max == 512)
466				break;
467			goto done;
468		case USB_SPEED_FULL:
469			if (max == 8 || max == 16 || max == 32 || max == 64)
470				/* we'll fake any legal size */
471				break;
472			/* save a return statement */
473		default:
474			goto done;
475		}
476		break;
477	case USB_ENDPOINT_XFER_INT:
478		if (strstr (ep->ep.name, "-iso")) /* bulk is ok */
479			goto done;
480		/* real hardware might not handle all packet sizes */
481		switch (dum->gadget.speed) {
482		case USB_SPEED_SUPER:
483		case USB_SPEED_HIGH:
484			if (max <= 1024)
485				break;
486			/* save a return statement */
487		case USB_SPEED_FULL:
488			if (max <= 64)
489				break;
490			/* save a return statement */
491		default:
492			if (max <= 8)
493				break;
494			goto done;
495		}
496		break;
497	case USB_ENDPOINT_XFER_ISOC:
498		if (strstr (ep->ep.name, "-bulk")
499				|| strstr (ep->ep.name, "-int"))
500			goto done;
501		/* real hardware might not handle all packet sizes */
502		switch (dum->gadget.speed) {
503		case USB_SPEED_SUPER:
504		case USB_SPEED_HIGH:
505			if (max <= 1024)
506				break;
507			/* save a return statement */
508		case USB_SPEED_FULL:
509			if (max <= 1023)
510				break;
511			/* save a return statement */
512		default:
513			goto done;
514		}
515		break;
516	default:
517		/* few chips support control except on ep0 */
518		goto done;
519	}
520
521	_ep->maxpacket = max;
522	ep->desc = desc;
523
524	dev_dbg (udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d\n",
525		_ep->name,
526		desc->bEndpointAddress & 0x0f,
527		(desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
528		({ char *val;
529		 switch (desc->bmAttributes & 0x03) {
530		 case USB_ENDPOINT_XFER_BULK:
531			 val = "bulk";
532			 break;
533		 case USB_ENDPOINT_XFER_ISOC:
534			 val = "iso";
535			 break;
536		 case USB_ENDPOINT_XFER_INT:
537			 val = "intr";
538			 break;
539		 default:
540			 val = "ctrl";
541			 break;
542		 }; val; }),
543		max);
544
545	/* at this point real hardware should be NAKing transfers
546	 * to that endpoint, until a buffer is queued to it.
547	 */
548	ep->halted = ep->wedged = 0;
549	retval = 0;
550done:
551	return retval;
552}
553
554static int dummy_disable (struct usb_ep *_ep)
555{
556	struct dummy_ep		*ep;
557	struct dummy		*dum;
558	unsigned long		flags;
559	int			retval;
560
561	ep = usb_ep_to_dummy_ep (_ep);
562	if (!_ep || !ep->desc || _ep->name == ep0name)
563		return -EINVAL;
564	dum = ep_to_dummy (ep);
565
566	spin_lock_irqsave (&dum->lock, flags);
567	ep->desc = NULL;
568	retval = 0;
569	nuke (dum, ep);
570	spin_unlock_irqrestore (&dum->lock, flags);
571
572	dev_dbg (udc_dev(dum), "disabled %s\n", _ep->name);
573	return retval;
574}
575
576static struct usb_request *
577dummy_alloc_request (struct usb_ep *_ep, gfp_t mem_flags)
578{
579	struct dummy_ep		*ep;
580	struct dummy_request	*req;
581
582	if (!_ep)
583		return NULL;
584	ep = usb_ep_to_dummy_ep (_ep);
585
586	req = kzalloc(sizeof(*req), mem_flags);
587	if (!req)
588		return NULL;
589	INIT_LIST_HEAD (&req->queue);
590	return &req->req;
591}
592
593static void
594dummy_free_request (struct usb_ep *_ep, struct usb_request *_req)
595{
596	struct dummy_ep		*ep;
597	struct dummy_request	*req;
598
599	ep = usb_ep_to_dummy_ep (_ep);
600	if (!ep || !_req || (!ep->desc && _ep->name != ep0name))
601		return;
602
603	req = usb_request_to_dummy_request (_req);
604	WARN_ON (!list_empty (&req->queue));
605	kfree (req);
606}
607
608static void
609fifo_complete (struct usb_ep *ep, struct usb_request *req)
610{
611}
612
613static int
614dummy_queue (struct usb_ep *_ep, struct usb_request *_req,
615		gfp_t mem_flags)
616{
617	struct dummy_ep		*ep;
618	struct dummy_request	*req;
619	struct dummy		*dum;
620	struct dummy_hcd	*dum_hcd;
621	unsigned long		flags;
622
623	req = usb_request_to_dummy_request (_req);
624	if (!_req || !list_empty (&req->queue) || !_req->complete)
625		return -EINVAL;
626
627	ep = usb_ep_to_dummy_ep (_ep);
628	if (!_ep || (!ep->desc && _ep->name != ep0name))
629		return -EINVAL;
630
631	dum = ep_to_dummy (ep);
632	dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
633	if (!dum->driver || !is_enabled(dum_hcd))
634		return -ESHUTDOWN;
635
636#if 0
637	dev_dbg (udc_dev(dum), "ep %p queue req %p to %s, len %d buf %p\n",
638			ep, _req, _ep->name, _req->length, _req->buf);
639#endif
640
641	_req->status = -EINPROGRESS;
642	_req->actual = 0;
643	spin_lock_irqsave (&dum->lock, flags);
644
645	/* implement an emulated single-request FIFO */
646	if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
647			list_empty (&dum->fifo_req.queue) &&
648			list_empty (&ep->queue) &&
649			_req->length <= FIFO_SIZE) {
650		req = &dum->fifo_req;
651		req->req = *_req;
652		req->req.buf = dum->fifo_buf;
653		memcpy (dum->fifo_buf, _req->buf, _req->length);
654		req->req.context = dum;
655		req->req.complete = fifo_complete;
656
657		list_add_tail(&req->queue, &ep->queue);
658		spin_unlock (&dum->lock);
659		_req->actual = _req->length;
660		_req->status = 0;
661		_req->complete (_ep, _req);
662		spin_lock (&dum->lock);
663	}  else
664		list_add_tail(&req->queue, &ep->queue);
665	spin_unlock_irqrestore (&dum->lock, flags);
666
667	/* real hardware would likely enable transfers here, in case
668	 * it'd been left NAKing.
669	 */
670	return 0;
671}
672
673static int dummy_dequeue (struct usb_ep *_ep, struct usb_request *_req)
674{
675	struct dummy_ep		*ep;
676	struct dummy		*dum;
677	int			retval = -EINVAL;
678	unsigned long		flags;
679	struct dummy_request	*req = NULL;
680
681	if (!_ep || !_req)
682		return retval;
683	ep = usb_ep_to_dummy_ep (_ep);
684	dum = ep_to_dummy (ep);
685
686	if (!dum->driver)
687		return -ESHUTDOWN;
688
689	local_irq_save (flags);
690	spin_lock (&dum->lock);
691	list_for_each_entry (req, &ep->queue, queue) {
692		if (&req->req == _req) {
693			list_del_init (&req->queue);
694			_req->status = -ECONNRESET;
695			retval = 0;
696			break;
697		}
698	}
699	spin_unlock (&dum->lock);
700
701	if (retval == 0) {
702		dev_dbg (udc_dev(dum),
703				"dequeued req %p from %s, len %d buf %p\n",
704				req, _ep->name, _req->length, _req->buf);
705		_req->complete (_ep, _req);
706	}
707	local_irq_restore (flags);
708	return retval;
709}
710
711static int
712dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
713{
714	struct dummy_ep		*ep;
715	struct dummy		*dum;
716
717	if (!_ep)
718		return -EINVAL;
719	ep = usb_ep_to_dummy_ep (_ep);
720	dum = ep_to_dummy (ep);
721	if (!dum->driver)
722		return -ESHUTDOWN;
723	if (!value)
724		ep->halted = ep->wedged = 0;
725	else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
726			!list_empty (&ep->queue))
727		return -EAGAIN;
728	else {
729		ep->halted = 1;
730		if (wedged)
731			ep->wedged = 1;
732	}
733	/* FIXME clear emulated data toggle too */
734	return 0;
735}
736
737static int
738dummy_set_halt(struct usb_ep *_ep, int value)
739{
740	return dummy_set_halt_and_wedge(_ep, value, 0);
741}
742
743static int dummy_set_wedge(struct usb_ep *_ep)
744{
745	if (!_ep || _ep->name == ep0name)
746		return -EINVAL;
747	return dummy_set_halt_and_wedge(_ep, 1, 1);
748}
749
750static const struct usb_ep_ops dummy_ep_ops = {
751	.enable		= dummy_enable,
752	.disable	= dummy_disable,
753
754	.alloc_request	= dummy_alloc_request,
755	.free_request	= dummy_free_request,
756
757	.queue		= dummy_queue,
758	.dequeue	= dummy_dequeue,
759
760	.set_halt	= dummy_set_halt,
761	.set_wedge	= dummy_set_wedge,
762};
763
764/*-------------------------------------------------------------------------*/
765
766/* there are both host and device side versions of this call ... */
767static int dummy_g_get_frame (struct usb_gadget *_gadget)
768{
769	struct timeval	tv;
770
771	do_gettimeofday (&tv);
772	return tv.tv_usec / 1000;
773}
774
775static int dummy_wakeup (struct usb_gadget *_gadget)
776{
777	struct dummy_hcd *dum_hcd;
778
779	dum_hcd = gadget_to_dummy_hcd(_gadget);
780	if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
781				| (1 << USB_DEVICE_REMOTE_WAKEUP))))
782		return -EINVAL;
783	if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
784		return -ENOLINK;
785	if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
786			 dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
787		return -EIO;
788
789	/* FIXME: What if the root hub is suspended but the port isn't? */
790
791	/* hub notices our request, issues downstream resume, etc */
792	dum_hcd->resuming = 1;
793	dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
794	mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
795	return 0;
796}
797
798static int dummy_set_selfpowered (struct usb_gadget *_gadget, int value)
799{
800	struct dummy	*dum;
801
802	dum = (gadget_to_dummy_hcd(_gadget))->dum;
803	if (value)
804		dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
805	else
806		dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
807	return 0;
808}
809
810static int dummy_pullup (struct usb_gadget *_gadget, int value)
811{
812	struct dummy_hcd *dum_hcd;
813	struct dummy	*dum;
814	unsigned long	flags;
815
816	dum_hcd = gadget_to_dummy_hcd(_gadget);
817	dum = dum_hcd->dum;
818
819	spin_lock_irqsave (&dum->lock, flags);
820	dum->pullup = (value != 0);
821	set_link_state(dum_hcd);
822	spin_unlock_irqrestore (&dum->lock, flags);
823	usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
824	return 0;
825}
826
827static int dummy_udc_start(struct usb_gadget_driver *driver,
828		int (*bind)(struct usb_gadget *));
829static int dummy_udc_stop(struct usb_gadget_driver *driver);
830
831static const struct usb_gadget_ops dummy_ops = {
832	.get_frame	= dummy_g_get_frame,
833	.wakeup		= dummy_wakeup,
834	.set_selfpowered = dummy_set_selfpowered,
835	.pullup		= dummy_pullup,
836	.start		= dummy_udc_start,
837	.stop		= dummy_udc_stop,
838};
839
840/*-------------------------------------------------------------------------*/
841
842/* "function" sysfs attribute */
843static ssize_t
844show_function (struct device *dev, struct device_attribute *attr, char *buf)
845{
846	struct dummy	*dum = gadget_dev_to_dummy (dev);
847
848	if (!dum->driver || !dum->driver->function)
849		return 0;
850	return scnprintf (buf, PAGE_SIZE, "%s\n", dum->driver->function);
851}
852static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
853
854/*-------------------------------------------------------------------------*/
855
856/*
857 * Driver registration/unregistration.
858 *
859 * This is basically hardware-specific; there's usually only one real USB
860 * device (not host) controller since that's how USB devices are intended
861 * to work.  So most implementations of these api calls will rely on the
862 * fact that only one driver will ever bind to the hardware.  But curious
863 * hardware can be built with discrete components, so the gadget API doesn't
864 * require that assumption.
865 *
866 * For this emulator, it might be convenient to create a usb slave device
867 * for each driver that registers:  just add to a big root hub.
868 */
869
870static int dummy_udc_start(struct usb_gadget_driver *driver,
871		int (*bind)(struct usb_gadget *))
872{
873	struct dummy	*dum = &the_controller;
874	int		retval, i;
875
876	if (!dum)
877		return -EINVAL;
878	if (dum->driver)
879		return -EBUSY;
880	if (!bind || !driver->setup || driver->speed == USB_SPEED_UNKNOWN)
881		return -EINVAL;
882
883	/*
884	 * SLAVE side init ... the layer above hardware, which
885	 * can't enumerate without help from the driver we're binding.
886	 */
887
888	dum->devstatus = 0;
889
890	if (mod_data.is_super_speed)
891		dum->gadget.speed = driver->speed;
892	else if (mod_data.is_high_speed)
893		dum->gadget.speed = min_t(u8, USB_SPEED_HIGH, driver->speed);
894	else
895		dum->gadget.speed = USB_SPEED_FULL;
896	if (dum->gadget.speed < driver->speed)
897		dev_dbg(udc_dev(dum), "This device can perform faster"
898				" if you connect it to a %s port...\n",
899			(driver->speed == USB_SPEED_SUPER ?
900			 "SuperSpeed" : "HighSpeed"));
901
902	if (dum->gadget.speed == USB_SPEED_SUPER) {
903		for (i = 0; i < DUMMY_ENDPOINTS; i++)
904			dum->ep[i].ep.max_streams = 0x10;
905		dum->ep[0].ep.maxpacket = 9;
906	} else {
907		for (i = 0; i < DUMMY_ENDPOINTS; i++)
908			dum->ep[i].ep.max_streams = 0;
909		dum->ep[0].ep.maxpacket = 64;
910	}
911
912	if (dum->gadget.speed == USB_SPEED_SUPER)
913		dum->gadget.is_otg =
914			(dummy_hcd_to_hcd(dum->ss_hcd)->self.otg_port != 0);
915	else
916		dum->gadget.is_otg =
917			(dummy_hcd_to_hcd(dum->hs_hcd)->self.otg_port != 0);
918
919	driver->driver.bus = NULL;
920	dum->driver = driver;
921	dum->gadget.dev.driver = &driver->driver;
922	dev_dbg (udc_dev(dum), "binding gadget driver '%s'\n",
923			driver->driver.name);
924	retval = bind(&dum->gadget);
925	if (retval) {
926		dum->driver = NULL;
927		dum->gadget.dev.driver = NULL;
928		return retval;
929	}
930
931	/* khubd will enumerate this in a while */
932	dummy_pullup(&dum->gadget, 1);
933	return 0;
934}
935
936static int dummy_udc_stop(struct usb_gadget_driver *driver)
937{
938	struct dummy	*dum = &the_controller;
939
940	if (!dum)
941		return -ENODEV;
942	if (!driver || driver != dum->driver || !driver->unbind)
943		return -EINVAL;
944
945	dev_dbg (udc_dev(dum), "unregister gadget driver '%s'\n",
946			driver->driver.name);
947
948	dummy_pullup(&dum->gadget, 0);
949
950	driver->unbind (&dum->gadget);
951	dum->gadget.dev.driver = NULL;
952	dum->driver = NULL;
953
954	dummy_pullup(&dum->gadget, 0);
955	return 0;
956}
957
958#undef is_enabled
959
960/* The gadget structure is stored inside the hcd structure and will be
961 * released along with it. */
962static void
963dummy_gadget_release (struct device *dev)
964{
965	return;
966}
967
968static void init_dummy_udc_hw(struct dummy *dum)
969{
970	int i;
971
972	INIT_LIST_HEAD(&dum->gadget.ep_list);
973	for (i = 0; i < DUMMY_ENDPOINTS; i++) {
974		struct dummy_ep	*ep = &dum->ep[i];
975
976		if (!ep_name[i])
977			break;
978		ep->ep.name = ep_name[i];
979		ep->ep.ops = &dummy_ep_ops;
980		list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
981		ep->halted = ep->wedged = ep->already_seen =
982				ep->setup_stage = 0;
983		ep->ep.maxpacket = ~0;
984		ep->last_io = jiffies;
985		ep->gadget = &dum->gadget;
986		ep->desc = NULL;
987		INIT_LIST_HEAD(&ep->queue);
988	}
989
990	dum->gadget.ep0 = &dum->ep[0].ep;
991	list_del_init(&dum->ep[0].ep.ep_list);
992	INIT_LIST_HEAD(&dum->fifo_req.queue);
993}
994
995static int dummy_udc_probe (struct platform_device *pdev)
996{
997	struct dummy	*dum = &the_controller;
998	int		rc;
999
1000	dum->gadget.name = gadget_name;
1001	dum->gadget.ops = &dummy_ops;
1002	dum->gadget.is_dualspeed = 1;
1003
1004	dev_set_name(&dum->gadget.dev, "gadget");
1005	dum->gadget.dev.parent = &pdev->dev;
1006	dum->gadget.dev.release = dummy_gadget_release;
1007	rc = device_register (&dum->gadget.dev);
1008	if (rc < 0) {
1009		put_device(&dum->gadget.dev);
1010		return rc;
1011	}
1012
1013	init_dummy_udc_hw(dum);
1014
1015	rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
1016	if (rc < 0)
1017		goto err_udc;
1018
1019	rc = device_create_file (&dum->gadget.dev, &dev_attr_function);
1020	if (rc < 0)
1021		goto err_dev;
1022	platform_set_drvdata(pdev, dum);
1023	return rc;
1024
1025err_dev:
1026	usb_del_gadget_udc(&dum->gadget);
1027err_udc:
1028	device_unregister(&dum->gadget.dev);
1029	return rc;
1030}
1031
1032static int dummy_udc_remove (struct platform_device *pdev)
1033{
1034	struct dummy	*dum = platform_get_drvdata (pdev);
1035
1036	usb_del_gadget_udc(&dum->gadget);
1037	platform_set_drvdata (pdev, NULL);
1038	device_remove_file (&dum->gadget.dev, &dev_attr_function);
1039	device_unregister (&dum->gadget.dev);
1040	return 0;
1041}
1042
1043static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
1044		int suspend)
1045{
1046	spin_lock_irq(&dum->lock);
1047	dum->udc_suspended = suspend;
1048	set_link_state(dum_hcd);
1049	spin_unlock_irq(&dum->lock);
1050}
1051
1052static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
1053{
1054	struct dummy		*dum = platform_get_drvdata(pdev);
1055	struct dummy_hcd	*dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1056
1057	dev_dbg(&pdev->dev, "%s\n", __func__);
1058	dummy_udc_pm(dum, dum_hcd, 1);
1059	usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1060	return 0;
1061}
1062
1063static int dummy_udc_resume(struct platform_device *pdev)
1064{
1065	struct dummy		*dum = platform_get_drvdata(pdev);
1066	struct dummy_hcd	*dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
1067
1068	dev_dbg(&pdev->dev, "%s\n", __func__);
1069	dummy_udc_pm(dum, dum_hcd, 0);
1070	usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
1071	return 0;
1072}
1073
1074static struct platform_driver dummy_udc_driver = {
1075	.probe		= dummy_udc_probe,
1076	.remove		= dummy_udc_remove,
1077	.suspend	= dummy_udc_suspend,
1078	.resume		= dummy_udc_resume,
1079	.driver		= {
1080		.name	= (char *) gadget_name,
1081		.owner	= THIS_MODULE,
1082	},
1083};
1084
1085/*-------------------------------------------------------------------------*/
1086
1087/* MASTER/HOST SIDE DRIVER
1088 *
1089 * this uses the hcd framework to hook up to host side drivers.
1090 * its root hub will only have one device, otherwise it acts like
1091 * a normal host controller.
1092 *
1093 * when urbs are queued, they're just stuck on a list that we
1094 * scan in a timer callback.  that callback connects writes from
1095 * the host with reads from the device, and so on, based on the
1096 * usb 2.0 rules.
1097 */
1098
1099static int dummy_urb_enqueue (
1100	struct usb_hcd			*hcd,
1101	struct urb			*urb,
1102	gfp_t				mem_flags
1103) {
1104	struct dummy_hcd *dum_hcd;
1105	struct urbp	*urbp;
1106	unsigned long	flags;
1107	int		rc;
1108
1109	if (!urb->transfer_buffer && urb->transfer_buffer_length)
1110		return -EINVAL;
1111
1112	urbp = kmalloc (sizeof *urbp, mem_flags);
1113	if (!urbp)
1114		return -ENOMEM;
1115	urbp->urb = urb;
1116
1117	dum_hcd = hcd_to_dummy_hcd(hcd);
1118	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1119	rc = usb_hcd_link_urb_to_ep(hcd, urb);
1120	if (rc) {
1121		kfree(urbp);
1122		goto done;
1123	}
1124
1125	if (!dum_hcd->udev) {
1126		dum_hcd->udev = urb->dev;
1127		usb_get_dev(dum_hcd->udev);
1128	} else if (unlikely(dum_hcd->udev != urb->dev))
1129		dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
1130
1131	list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
1132	urb->hcpriv = urbp;
1133	if (usb_pipetype (urb->pipe) == PIPE_CONTROL)
1134		urb->error_count = 1;		/* mark as a new urb */
1135
1136	/* kick the scheduler, it'll do the rest */
1137	if (!timer_pending(&dum_hcd->timer))
1138		mod_timer(&dum_hcd->timer, jiffies + 1);
1139
1140 done:
1141	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1142	return rc;
1143}
1144
1145static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1146{
1147	struct dummy_hcd *dum_hcd;
1148	unsigned long	flags;
1149	int		rc;
1150
1151	/* giveback happens automatically in timer callback,
1152	 * so make sure the callback happens */
1153	dum_hcd = hcd_to_dummy_hcd(hcd);
1154	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1155
1156	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1157	if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
1158			!list_empty(&dum_hcd->urbp_list))
1159		mod_timer(&dum_hcd->timer, jiffies);
1160
1161	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1162	return rc;
1163}
1164
1165/* transfer up to a frame's worth; caller must own lock */
1166static int
1167transfer(struct dummy *dum, struct urb *urb, struct dummy_ep *ep, int limit,
1168		int *status)
1169{
1170	struct dummy_request	*req;
1171
1172top:
1173	/* if there's no request queued, the device is NAKing; return */
1174	list_for_each_entry (req, &ep->queue, queue) {
1175		unsigned	host_len, dev_len, len;
1176		int		is_short, to_host;
1177		int		rescan = 0;
1178
1179		/* 1..N packets of ep->ep.maxpacket each ... the last one
1180		 * may be short (including zero length).
1181		 *
1182		 * writer can send a zlp explicitly (length 0) or implicitly
1183		 * (length mod maxpacket zero, and 'zero' flag); they always
1184		 * terminate reads.
1185		 */
1186		host_len = urb->transfer_buffer_length - urb->actual_length;
1187		dev_len = req->req.length - req->req.actual;
1188		len = min (host_len, dev_len);
1189
1190		/* FIXME update emulated data toggle too */
1191
1192		to_host = usb_pipein (urb->pipe);
1193		if (unlikely (len == 0))
1194			is_short = 1;
1195		else {
1196			char		*ubuf, *rbuf;
1197
1198			/* not enough bandwidth left? */
1199			if (limit < ep->ep.maxpacket && limit < len)
1200				break;
1201			len = min (len, (unsigned) limit);
1202			if (len == 0)
1203				break;
1204
1205			/* use an extra pass for the final short packet */
1206			if (len > ep->ep.maxpacket) {
1207				rescan = 1;
1208				len -= (len % ep->ep.maxpacket);
1209			}
1210			is_short = (len % ep->ep.maxpacket) != 0;
1211
1212			/* else transfer packet(s) */
1213			ubuf = urb->transfer_buffer + urb->actual_length;
1214			rbuf = req->req.buf + req->req.actual;
1215			if (to_host)
1216				memcpy (ubuf, rbuf, len);
1217			else
1218				memcpy (rbuf, ubuf, len);
1219			ep->last_io = jiffies;
1220
1221			limit -= len;
1222			urb->actual_length += len;
1223			req->req.actual += len;
1224		}
1225
1226		/* short packets terminate, maybe with overflow/underflow.
1227		 * it's only really an error to write too much.
1228		 *
1229		 * partially filling a buffer optionally blocks queue advances
1230		 * (so completion handlers can clean up the queue) but we don't
1231		 * need to emulate such data-in-flight.
1232		 */
1233		if (is_short) {
1234			if (host_len == dev_len) {
1235				req->req.status = 0;
1236				*status = 0;
1237			} else if (to_host) {
1238				req->req.status = 0;
1239				if (dev_len > host_len)
1240					*status = -EOVERFLOW;
1241				else
1242					*status = 0;
1243			} else if (!to_host) {
1244				*status = 0;
1245				if (host_len > dev_len)
1246					req->req.status = -EOVERFLOW;
1247				else
1248					req->req.status = 0;
1249			}
1250
1251		/* many requests terminate without a short packet */
1252		} else {
1253			if (req->req.length == req->req.actual
1254					&& !req->req.zero)
1255				req->req.status = 0;
1256			if (urb->transfer_buffer_length == urb->actual_length
1257					&& !(urb->transfer_flags
1258						& URB_ZERO_PACKET))
1259				*status = 0;
1260		}
1261
1262		/* device side completion --> continuable */
1263		if (req->req.status != -EINPROGRESS) {
1264			list_del_init (&req->queue);
1265
1266			spin_unlock (&dum->lock);
1267			req->req.complete (&ep->ep, &req->req);
1268			spin_lock (&dum->lock);
1269
1270			/* requests might have been unlinked... */
1271			rescan = 1;
1272		}
1273
1274		/* host side completion --> terminate */
1275		if (*status != -EINPROGRESS)
1276			break;
1277
1278		/* rescan to continue with any other queued i/o */
1279		if (rescan)
1280			goto top;
1281	}
1282	return limit;
1283}
1284
1285static int periodic_bytes (struct dummy *dum, struct dummy_ep *ep)
1286{
1287	int	limit = ep->ep.maxpacket;
1288
1289	if (dum->gadget.speed == USB_SPEED_HIGH) {
1290		int	tmp;
1291
1292		/* high bandwidth mode */
1293		tmp = le16_to_cpu(ep->desc->wMaxPacketSize);
1294		tmp = (tmp >> 11) & 0x03;
1295		tmp *= 8 /* applies to entire frame */;
1296		limit += limit * tmp;
1297	}
1298	if (dum->gadget.speed == USB_SPEED_SUPER) {
1299		switch (ep->desc->bmAttributes & 0x03) {
1300		case USB_ENDPOINT_XFER_ISOC:
1301			/* Sec. 4.4.8.2 USB3.0 Spec */
1302			limit = 3 * 16 * 1024 * 8;
1303			break;
1304		case USB_ENDPOINT_XFER_INT:
1305			/* Sec. 4.4.7.2 USB3.0 Spec */
1306			limit = 3 * 1024 * 8;
1307			break;
1308		case USB_ENDPOINT_XFER_BULK:
1309		default:
1310			break;
1311		}
1312	}
1313	return limit;
1314}
1315
1316#define is_active(dum_hcd)	((dum_hcd->port_status & \
1317		(USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
1318			USB_PORT_STAT_SUSPEND)) \
1319		== (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
1320
1321static struct dummy_ep *find_endpoint (struct dummy *dum, u8 address)
1322{
1323	int		i;
1324
1325	if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
1326			dum->ss_hcd : dum->hs_hcd)))
1327		return NULL;
1328	if ((address & ~USB_DIR_IN) == 0)
1329		return &dum->ep [0];
1330	for (i = 1; i < DUMMY_ENDPOINTS; i++) {
1331		struct dummy_ep	*ep = &dum->ep [i];
1332
1333		if (!ep->desc)
1334			continue;
1335		if (ep->desc->bEndpointAddress == address)
1336			return ep;
1337	}
1338	return NULL;
1339}
1340
1341#undef is_active
1342
1343#define Dev_Request	(USB_TYPE_STANDARD | USB_RECIP_DEVICE)
1344#define Dev_InRequest	(Dev_Request | USB_DIR_IN)
1345#define Intf_Request	(USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
1346#define Intf_InRequest	(Intf_Request | USB_DIR_IN)
1347#define Ep_Request	(USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
1348#define Ep_InRequest	(Ep_Request | USB_DIR_IN)
1349
1350
1351/**
1352 * handle_control_request() - handles all control transfers
1353 * @dum: pointer to dummy (the_controller)
1354 * @urb: the urb request to handle
1355 * @setup: pointer to the setup data for a USB device control
1356 *	 request
1357 * @status: pointer to request handling status
1358 *
1359 * Return 0 - if the request was handled
1360 *	  1 - if the request wasn't handles
1361 *	  error code on error
1362 */
1363static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
1364				  struct usb_ctrlrequest *setup,
1365				  int *status)
1366{
1367	struct dummy_ep		*ep2;
1368	struct dummy		*dum = dum_hcd->dum;
1369	int			ret_val = 1;
1370	unsigned	w_index;
1371	unsigned	w_value;
1372
1373	w_index = le16_to_cpu(setup->wIndex);
1374	w_value = le16_to_cpu(setup->wValue);
1375	switch (setup->bRequest) {
1376	case USB_REQ_SET_ADDRESS:
1377		if (setup->bRequestType != Dev_Request)
1378			break;
1379		dum->address = w_value;
1380		*status = 0;
1381		dev_dbg(udc_dev(dum), "set_address = %d\n",
1382				w_value);
1383		ret_val = 0;
1384		break;
1385	case USB_REQ_SET_FEATURE:
1386		if (setup->bRequestType == Dev_Request) {
1387			ret_val = 0;
1388			switch (w_value) {
1389			case USB_DEVICE_REMOTE_WAKEUP:
1390				break;
1391			case USB_DEVICE_B_HNP_ENABLE:
1392				dum->gadget.b_hnp_enable = 1;
1393				break;
1394			case USB_DEVICE_A_HNP_SUPPORT:
1395				dum->gadget.a_hnp_support = 1;
1396				break;
1397			case USB_DEVICE_A_ALT_HNP_SUPPORT:
1398				dum->gadget.a_alt_hnp_support = 1;
1399				break;
1400			case USB_DEVICE_U1_ENABLE:
1401				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1402				    HCD_USB3)
1403					w_value = USB_DEV_STAT_U1_ENABLED;
1404				else
1405					ret_val = -EOPNOTSUPP;
1406				break;
1407			case USB_DEVICE_U2_ENABLE:
1408				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1409				    HCD_USB3)
1410					w_value = USB_DEV_STAT_U2_ENABLED;
1411				else
1412					ret_val = -EOPNOTSUPP;
1413				break;
1414			case USB_DEVICE_LTM_ENABLE:
1415				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1416				    HCD_USB3)
1417					w_value = USB_DEV_STAT_LTM_ENABLED;
1418				else
1419					ret_val = -EOPNOTSUPP;
1420				break;
1421			default:
1422				ret_val = -EOPNOTSUPP;
1423			}
1424			if (ret_val == 0) {
1425				dum->devstatus |= (1 << w_value);
1426				*status = 0;
1427			}
1428		} else if (setup->bRequestType == Ep_Request) {
1429			/* endpoint halt */
1430			ep2 = find_endpoint(dum, w_index);
1431			if (!ep2 || ep2->ep.name == ep0name) {
1432				ret_val = -EOPNOTSUPP;
1433				break;
1434			}
1435			ep2->halted = 1;
1436			ret_val = 0;
1437			*status = 0;
1438		}
1439		break;
1440	case USB_REQ_CLEAR_FEATURE:
1441		if (setup->bRequestType == Dev_Request) {
1442			ret_val = 0;
1443			switch (w_value) {
1444			case USB_DEVICE_REMOTE_WAKEUP:
1445				w_value = USB_DEVICE_REMOTE_WAKEUP;
1446				break;
1447			case USB_DEVICE_U1_ENABLE:
1448				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1449				    HCD_USB3)
1450					w_value = USB_DEV_STAT_U1_ENABLED;
1451				else
1452					ret_val = -EOPNOTSUPP;
1453				break;
1454			case USB_DEVICE_U2_ENABLE:
1455				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1456				    HCD_USB3)
1457					w_value = USB_DEV_STAT_U2_ENABLED;
1458				else
1459					ret_val = -EOPNOTSUPP;
1460				break;
1461			case USB_DEVICE_LTM_ENABLE:
1462				if (dummy_hcd_to_hcd(dum_hcd)->speed ==
1463				    HCD_USB3)
1464					w_value = USB_DEV_STAT_LTM_ENABLED;
1465				else
1466					ret_val = -EOPNOTSUPP;
1467				break;
1468			default:
1469				ret_val = -EOPNOTSUPP;
1470				break;
1471			}
1472			if (ret_val == 0) {
1473				dum->devstatus &= ~(1 << w_value);
1474				*status = 0;
1475			}
1476		} else if (setup->bRequestType == Ep_Request) {
1477			/* endpoint halt */
1478			ep2 = find_endpoint(dum, w_index);
1479			if (!ep2) {
1480				ret_val = -EOPNOTSUPP;
1481				break;
1482			}
1483			if (!ep2->wedged)
1484				ep2->halted = 0;
1485			ret_val = 0;
1486			*status = 0;
1487		}
1488		break;
1489	case USB_REQ_GET_STATUS:
1490		if (setup->bRequestType == Dev_InRequest
1491				|| setup->bRequestType == Intf_InRequest
1492				|| setup->bRequestType == Ep_InRequest) {
1493			char *buf;
1494			/*
1495			 * device: remote wakeup, selfpowered
1496			 * interface: nothing
1497			 * endpoint: halt
1498			 */
1499			buf = (char *)urb->transfer_buffer;
1500			if (urb->transfer_buffer_length > 0) {
1501				if (setup->bRequestType == Ep_InRequest) {
1502					ep2 = find_endpoint(dum, w_index);
1503					if (!ep2) {
1504						ret_val = -EOPNOTSUPP;
1505						break;
1506					}
1507					buf[0] = ep2->halted;
1508				} else if (setup->bRequestType ==
1509					   Dev_InRequest) {
1510					buf[0] = (u8)dum->devstatus;
1511				} else
1512					buf[0] = 0;
1513			}
1514			if (urb->transfer_buffer_length > 1)
1515				buf[1] = 0;
1516			urb->actual_length = min_t(u32, 2,
1517				urb->transfer_buffer_length);
1518			ret_val = 0;
1519			*status = 0;
1520		}
1521		break;
1522	}
1523	return ret_val;
1524}
1525
1526/* drive both sides of the transfers; looks like irq handlers to
1527 * both drivers except the callbacks aren't in_irq().
1528 */
1529static void dummy_timer(unsigned long _dum_hcd)
1530{
1531	struct dummy_hcd	*dum_hcd = (struct dummy_hcd *) _dum_hcd;
1532	struct dummy		*dum = dum_hcd->dum;
1533	struct urbp		*urbp, *tmp;
1534	unsigned long		flags;
1535	int			limit, total;
1536	int			i;
1537
1538	/* simplistic model for one frame's bandwidth */
1539	switch (dum->gadget.speed) {
1540	case USB_SPEED_LOW:
1541		total = 8/*bytes*/ * 12/*packets*/;
1542		break;
1543	case USB_SPEED_FULL:
1544		total = 64/*bytes*/ * 19/*packets*/;
1545		break;
1546	case USB_SPEED_HIGH:
1547		total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
1548		break;
1549	case USB_SPEED_SUPER:
1550		/* Bus speed is 500000 bytes/ms, so use a little less */
1551		total = 490000;
1552		break;
1553	default:
1554		dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
1555		return;
1556	}
1557
1558	/* FIXME if HZ != 1000 this will probably misbehave ... */
1559
1560	/* look at each urb queued by the host side driver */
1561	spin_lock_irqsave (&dum->lock, flags);
1562
1563	if (!dum_hcd->udev) {
1564		dev_err(dummy_dev(dum_hcd),
1565				"timer fired with no URBs pending?\n");
1566		spin_unlock_irqrestore (&dum->lock, flags);
1567		return;
1568	}
1569
1570	for (i = 0; i < DUMMY_ENDPOINTS; i++) {
1571		if (!ep_name [i])
1572			break;
1573		dum->ep [i].already_seen = 0;
1574	}
1575
1576restart:
1577	list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
1578		struct urb		*urb;
1579		struct dummy_request	*req;
1580		u8			address;
1581		struct dummy_ep		*ep = NULL;
1582		int			type;
1583		int			status = -EINPROGRESS;
1584
1585		urb = urbp->urb;
1586		if (urb->unlinked)
1587			goto return_urb;
1588		else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
1589			continue;
1590		type = usb_pipetype (urb->pipe);
1591
1592		/* used up this frame's non-periodic bandwidth?
1593		 * FIXME there's infinite bandwidth for control and
1594		 * periodic transfers ... unrealistic.
1595		 */
1596		if (total <= 0 && type == PIPE_BULK)
1597			continue;
1598
1599		/* find the gadget's ep for this request (if configured) */
1600		address = usb_pipeendpoint (urb->pipe);
1601		if (usb_pipein (urb->pipe))
1602			address |= USB_DIR_IN;
1603		ep = find_endpoint(dum, address);
1604		if (!ep) {
1605			/* set_configuration() disagreement */
1606			dev_dbg(dummy_dev(dum_hcd),
1607				"no ep configured for urb %p\n",
1608				urb);
1609			status = -EPROTO;
1610			goto return_urb;
1611		}
1612
1613		if (ep->already_seen)
1614			continue;
1615		ep->already_seen = 1;
1616		if (ep == &dum->ep [0] && urb->error_count) {
1617			ep->setup_stage = 1;	/* a new urb */
1618			urb->error_count = 0;
1619		}
1620		if (ep->halted && !ep->setup_stage) {
1621			/* NOTE: must not be iso! */
1622			dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
1623					ep->ep.name, urb);
1624			status = -EPIPE;
1625			goto return_urb;
1626		}
1627		/* FIXME make sure both ends agree on maxpacket */
1628
1629		/* handle control requests */
1630		if (ep == &dum->ep [0] && ep->setup_stage) {
1631			struct usb_ctrlrequest		setup;
1632			int				value = 1;
1633
1634			setup = *(struct usb_ctrlrequest*) urb->setup_packet;
1635			/* paranoia, in case of stale queued data */
1636			list_for_each_entry (req, &ep->queue, queue) {
1637				list_del_init (&req->queue);
1638				req->req.status = -EOVERFLOW;
1639				dev_dbg (udc_dev(dum), "stale req = %p\n",
1640						req);
1641
1642				spin_unlock (&dum->lock);
1643				req->req.complete (&ep->ep, &req->req);
1644				spin_lock (&dum->lock);
1645				ep->already_seen = 0;
1646				goto restart;
1647			}
1648
1649			/* gadget driver never sees set_address or operations
1650			 * on standard feature flags.  some hardware doesn't
1651			 * even expose them.
1652			 */
1653			ep->last_io = jiffies;
1654			ep->setup_stage = 0;
1655			ep->halted = 0;
1656
1657			value = handle_control_request(dum_hcd, urb, &setup,
1658						       &status);
1659
1660			/* gadget driver handles all other requests.  block
1661			 * until setup() returns; no reentrancy issues etc.
1662			 */
1663			if (value > 0) {
1664				spin_unlock (&dum->lock);
1665				value = dum->driver->setup (&dum->gadget,
1666						&setup);
1667				spin_lock (&dum->lock);
1668
1669				if (value >= 0) {
1670					/* no delays (max 64KB data stage) */
1671					limit = 64*1024;
1672					goto treat_control_like_bulk;
1673				}
1674				/* error, see below */
1675			}
1676
1677			if (value < 0) {
1678				if (value != -EOPNOTSUPP)
1679					dev_dbg (udc_dev(dum),
1680						"setup --> %d\n",
1681						value);
1682				status = -EPIPE;
1683				urb->actual_length = 0;
1684			}
1685
1686			goto return_urb;
1687		}
1688
1689		/* non-control requests */
1690		limit = total;
1691		switch (usb_pipetype (urb->pipe)) {
1692		case PIPE_ISOCHRONOUS:
1693			/* FIXME is it urb->interval since the last xfer?
1694			 * use urb->iso_frame_desc[i].
1695			 * complete whether or not ep has requests queued.
1696			 * report random errors, to debug drivers.
1697			 */
1698			limit = max (limit, periodic_bytes (dum, ep));
1699			status = -ENOSYS;
1700			break;
1701
1702		case PIPE_INTERRUPT:
1703			/* FIXME is it urb->interval since the last xfer?
1704			 * this almost certainly polls too fast.
1705			 */
1706			limit = max (limit, periodic_bytes (dum, ep));
1707			/* FALLTHROUGH */
1708
1709		// case PIPE_BULK:  case PIPE_CONTROL:
1710		default:
1711		treat_control_like_bulk:
1712			ep->last_io = jiffies;
1713			total = transfer(dum, urb, ep, limit, &status);
1714			break;
1715		}
1716
1717		/* incomplete transfer? */
1718		if (status == -EINPROGRESS)
1719			continue;
1720
1721return_urb:
1722		list_del (&urbp->urbp_list);
1723		kfree (urbp);
1724		if (ep)
1725			ep->already_seen = ep->setup_stage = 0;
1726
1727		usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
1728		spin_unlock (&dum->lock);
1729		usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
1730		spin_lock (&dum->lock);
1731
1732		goto restart;
1733	}
1734
1735	if (list_empty(&dum_hcd->urbp_list)) {
1736		usb_put_dev(dum_hcd->udev);
1737		dum_hcd->udev = NULL;
1738	} else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
1739		/* want a 1 msec delay here */
1740		mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
1741	}
1742
1743	spin_unlock_irqrestore (&dum->lock, flags);
1744}
1745
1746/*-------------------------------------------------------------------------*/
1747
1748#define PORT_C_MASK \
1749	((USB_PORT_STAT_C_CONNECTION \
1750	| USB_PORT_STAT_C_ENABLE \
1751	| USB_PORT_STAT_C_SUSPEND \
1752	| USB_PORT_STAT_C_OVERCURRENT \
1753	| USB_PORT_STAT_C_RESET) << 16)
1754
1755static int dummy_hub_status (struct usb_hcd *hcd, char *buf)
1756{
1757	struct dummy_hcd	*dum_hcd;
1758	unsigned long		flags;
1759	int			retval = 0;
1760
1761	dum_hcd = hcd_to_dummy_hcd(hcd);
1762
1763	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1764	if (!HCD_HW_ACCESSIBLE(hcd))
1765		goto done;
1766
1767	if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
1768		dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1769		dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1770		set_link_state(dum_hcd);
1771	}
1772
1773	if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
1774		*buf = (1 << 1);
1775		dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
1776				dum_hcd->port_status);
1777		retval = 1;
1778		if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
1779			usb_hcd_resume_root_hub (hcd);
1780	}
1781done:
1782	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
1783	return retval;
1784}
1785
1786static inline void
1787ss_hub_descriptor(struct usb_hub_descriptor *desc)
1788{
1789	memset(desc, 0, sizeof *desc);
1790	desc->bDescriptorType = 0x2a;
1791	desc->bDescLength = 12;
1792	desc->wHubCharacteristics = cpu_to_le16(0x0001);
1793	desc->bNbrPorts = 1;
1794	desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
1795	desc->u.ss.DeviceRemovable = 0xffff;
1796}
1797
1798static inline void
1799hub_descriptor (struct usb_hub_descriptor *desc)
1800{
1801	memset (desc, 0, sizeof *desc);
1802	desc->bDescriptorType = 0x29;
1803	desc->bDescLength = 9;
1804	desc->wHubCharacteristics = cpu_to_le16(0x0001);
1805	desc->bNbrPorts = 1;
1806	desc->u.hs.DeviceRemovable[0] = 0xff;
1807	desc->u.hs.DeviceRemovable[1] = 0xff;
1808}
1809
1810static int dummy_hub_control (
1811	struct usb_hcd	*hcd,
1812	u16		typeReq,
1813	u16		wValue,
1814	u16		wIndex,
1815	char		*buf,
1816	u16		wLength
1817) {
1818	struct dummy_hcd *dum_hcd;
1819	int		retval = 0;
1820	unsigned long	flags;
1821
1822	if (!HCD_HW_ACCESSIBLE(hcd))
1823		return -ETIMEDOUT;
1824
1825	dum_hcd = hcd_to_dummy_hcd(hcd);
1826
1827	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
1828	switch (typeReq) {
1829	case ClearHubFeature:
1830		break;
1831	case ClearPortFeature:
1832		switch (wValue) {
1833		case USB_PORT_FEAT_SUSPEND:
1834			if (hcd->speed == HCD_USB3) {
1835				dev_dbg(dummy_dev(dum_hcd),
1836					 "USB_PORT_FEAT_SUSPEND req not "
1837					 "supported for USB 3.0 roothub\n");
1838				goto error;
1839			}
1840			if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
1841				/* 20msec resume signaling */
1842				dum_hcd->resuming = 1;
1843				dum_hcd->re_timeout = jiffies +
1844						msecs_to_jiffies(20);
1845			}
1846			break;
1847		case USB_PORT_FEAT_POWER:
1848			if (hcd->speed == HCD_USB3) {
1849				if (dum_hcd->port_status & USB_PORT_STAT_POWER)
1850					dev_dbg(dummy_dev(dum_hcd),
1851						"power-off\n");
1852			} else
1853				if (dum_hcd->port_status &
1854							USB_SS_PORT_STAT_POWER)
1855					dev_dbg(dummy_dev(dum_hcd),
1856						"power-off\n");
1857			/* FALLS THROUGH */
1858		default:
1859			dum_hcd->port_status &= ~(1 << wValue);
1860			set_link_state(dum_hcd);
1861		}
1862		break;
1863	case GetHubDescriptor:
1864		if (hcd->speed == HCD_USB3 &&
1865				(wLength < USB_DT_SS_HUB_SIZE ||
1866				 wValue != (USB_DT_SS_HUB << 8))) {
1867			dev_dbg(dummy_dev(dum_hcd),
1868				"Wrong hub descriptor type for "
1869				"USB 3.0 roothub.\n");
1870			goto error;
1871		}
1872		if (hcd->speed == HCD_USB3)
1873			ss_hub_descriptor((struct usb_hub_descriptor *) buf);
1874		else
1875			hub_descriptor((struct usb_hub_descriptor *) buf);
1876		break;
1877	case GetHubStatus:
1878		*(__le32 *) buf = cpu_to_le32 (0);
1879		break;
1880	case GetPortStatus:
1881		if (wIndex != 1)
1882			retval = -EPIPE;
1883
1884		/* whoever resets or resumes must GetPortStatus to
1885		 * complete it!!
1886		 */
1887		if (dum_hcd->resuming &&
1888				time_after_eq(jiffies, dum_hcd->re_timeout)) {
1889			dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
1890			dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
1891		}
1892		if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
1893				time_after_eq(jiffies, dum_hcd->re_timeout)) {
1894			dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
1895			dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
1896			if (dum_hcd->dum->pullup) {
1897				dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
1898
1899				if (hcd->speed < HCD_USB3) {
1900					switch (dum_hcd->dum->gadget.speed) {
1901					case USB_SPEED_HIGH:
1902						dum_hcd->port_status |=
1903						      USB_PORT_STAT_HIGH_SPEED;
1904						break;
1905					case USB_SPEED_LOW:
1906						dum_hcd->dum->gadget.ep0->
1907							maxpacket = 8;
1908						dum_hcd->port_status |=
1909							USB_PORT_STAT_LOW_SPEED;
1910						break;
1911					default:
1912						dum_hcd->dum->gadget.speed =
1913							USB_SPEED_FULL;
1914						break;
1915					}
1916				}
1917			}
1918		}
1919		set_link_state(dum_hcd);
1920		((__le16 *) buf)[0] = cpu_to_le16 (dum_hcd->port_status);
1921		((__le16 *) buf)[1] = cpu_to_le16 (dum_hcd->port_status >> 16);
1922		break;
1923	case SetHubFeature:
1924		retval = -EPIPE;
1925		break;
1926	case SetPortFeature:
1927		switch (wValue) {
1928		case USB_PORT_FEAT_LINK_STATE:
1929			if (hcd->speed != HCD_USB3) {
1930				dev_dbg(dummy_dev(dum_hcd),
1931					 "USB_PORT_FEAT_LINK_STATE req not "
1932					 "supported for USB 2.0 roothub\n");
1933				goto error;
1934			}
1935			/*
1936			 * Since this is dummy we don't have an actual link so
1937			 * there is nothing to do for the SET_LINK_STATE cmd
1938			 */
1939			break;
1940		case USB_PORT_FEAT_U1_TIMEOUT:
1941		case USB_PORT_FEAT_U2_TIMEOUT:
1942			/* TODO: add suspend/resume support! */
1943			if (hcd->speed != HCD_USB3) {
1944				dev_dbg(dummy_dev(dum_hcd),
1945					 "USB_PORT_FEAT_U1/2_TIMEOUT req not "
1946					 "supported for USB 2.0 roothub\n");
1947				goto error;
1948			}
1949			break;
1950		case USB_PORT_FEAT_SUSPEND:
1951			/* Applicable only for USB2.0 hub */
1952			if (hcd->speed == HCD_USB3) {
1953				dev_dbg(dummy_dev(dum_hcd),
1954					 "USB_PORT_FEAT_SUSPEND req not "
1955					 "supported for USB 3.0 roothub\n");
1956				goto error;
1957			}
1958			if (dum_hcd->active) {
1959				dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
1960
1961				/* HNP would happen here; for now we
1962				 * assume b_bus_req is always true.
1963				 */
1964				set_link_state(dum_hcd);
1965				if (((1 << USB_DEVICE_B_HNP_ENABLE)
1966						& dum_hcd->dum->devstatus) != 0)
1967					dev_dbg(dummy_dev(dum_hcd),
1968							"no HNP yet!\n");
1969			}
1970			break;
1971		case USB_PORT_FEAT_POWER:
1972			if (hcd->speed == HCD_USB3)
1973				dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
1974			else
1975				dum_hcd->port_status |= USB_PORT_STAT_POWER;
1976			set_link_state(dum_hcd);
1977			break;
1978		case USB_PORT_FEAT_BH_PORT_RESET:
1979			/* Applicable only for USB3.0 hub */
1980			if (hcd->speed != HCD_USB3) {
1981				dev_dbg(dummy_dev(dum_hcd),
1982					 "USB_PORT_FEAT_BH_PORT_RESET req not "
1983					 "supported for USB 2.0 roothub\n");
1984				goto error;
1985			}
1986			/* FALLS THROUGH */
1987		case USB_PORT_FEAT_RESET:
1988			/* if it's already enabled, disable */
1989			if (hcd->speed == HCD_USB3) {
1990				dum_hcd->port_status = 0;
1991				dum_hcd->port_status =
1992					(USB_SS_PORT_STAT_POWER |
1993					 USB_PORT_STAT_CONNECTION |
1994					 USB_PORT_STAT_RESET);
1995			} else
1996				dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
1997					| USB_PORT_STAT_LOW_SPEED
1998					| USB_PORT_STAT_HIGH_SPEED);
1999			/*
2000			 * We want to reset device status. All but the
2001			 * Self powered feature
2002			 */
2003			dum_hcd->dum->devstatus &=
2004				(1 << USB_DEVICE_SELF_POWERED);
2005			/*
2006			 * FIXME USB3.0: what is the correct reset signaling
2007			 * interval? Is it still 50msec as for HS?
2008			 */
2009			dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
2010			/* FALLS THROUGH */
2011		default:
2012			if (hcd->speed == HCD_USB3) {
2013				if ((dum_hcd->port_status &
2014				     USB_SS_PORT_STAT_POWER) != 0) {
2015					dum_hcd->port_status |= (1 << wValue);
2016					set_link_state(dum_hcd);
2017				}
2018			} else
2019				if ((dum_hcd->port_status &
2020				     USB_PORT_STAT_POWER) != 0) {
2021					dum_hcd->port_status |= (1 << wValue);
2022					set_link_state(dum_hcd);
2023				}
2024		}
2025		break;
2026	case GetPortErrorCount:
2027		if (hcd->speed != HCD_USB3) {
2028			dev_dbg(dummy_dev(dum_hcd),
2029				 "GetPortErrorCount req not "
2030				 "supported for USB 2.0 roothub\n");
2031			goto error;
2032		}
2033		/* We'll always return 0 since this is a dummy hub */
2034		*(__le32 *) buf = cpu_to_le32(0);
2035		break;
2036	case SetHubDepth:
2037		if (hcd->speed != HCD_USB3) {
2038			dev_dbg(dummy_dev(dum_hcd),
2039				 "SetHubDepth req not supported for "
2040				 "USB 2.0 roothub\n");
2041			goto error;
2042		}
2043		break;
2044	default:
2045		dev_dbg(dummy_dev(dum_hcd),
2046			"hub control req%04x v%04x i%04x l%d\n",
2047			typeReq, wValue, wIndex, wLength);
2048error:
2049		/* "protocol stall" on error */
2050		retval = -EPIPE;
2051	}
2052	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2053
2054	if ((dum_hcd->port_status & PORT_C_MASK) != 0)
2055		usb_hcd_poll_rh_status (hcd);
2056	return retval;
2057}
2058
2059static int dummy_bus_suspend (struct usb_hcd *hcd)
2060{
2061	struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2062
2063	dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
2064
2065	spin_lock_irq(&dum_hcd->dum->lock);
2066	dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
2067	set_link_state(dum_hcd);
2068	hcd->state = HC_STATE_SUSPENDED;
2069	spin_unlock_irq(&dum_hcd->dum->lock);
2070	return 0;
2071}
2072
2073static int dummy_bus_resume (struct usb_hcd *hcd)
2074{
2075	struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
2076	int rc = 0;
2077
2078	dev_dbg (&hcd->self.root_hub->dev, "%s\n", __func__);
2079
2080	spin_lock_irq(&dum_hcd->dum->lock);
2081	if (!HCD_HW_ACCESSIBLE(hcd)) {
2082		rc = -ESHUTDOWN;
2083	} else {
2084		dum_hcd->rh_state = DUMMY_RH_RUNNING;
2085		set_link_state(dum_hcd);
2086		if (!list_empty(&dum_hcd->urbp_list))
2087			mod_timer(&dum_hcd->timer, jiffies);
2088		hcd->state = HC_STATE_RUNNING;
2089	}
2090	spin_unlock_irq(&dum_hcd->dum->lock);
2091	return rc;
2092}
2093
2094/*-------------------------------------------------------------------------*/
2095
2096static inline ssize_t
2097show_urb (char *buf, size_t size, struct urb *urb)
2098{
2099	int ep = usb_pipeendpoint (urb->pipe);
2100
2101	return snprintf (buf, size,
2102		"urb/%p %s ep%d%s%s len %d/%d\n",
2103		urb,
2104		({ char *s;
2105		 switch (urb->dev->speed) {
2106		 case USB_SPEED_LOW:
2107			s = "ls";
2108			break;
2109		 case USB_SPEED_FULL:
2110			s = "fs";
2111			break;
2112		 case USB_SPEED_HIGH:
2113			s = "hs";
2114			break;
2115		 case USB_SPEED_SUPER:
2116			s = "ss";
2117			break;
2118		 default:
2119			s = "?";
2120			break;
2121		 }; s; }),
2122		ep, ep ? (usb_pipein (urb->pipe) ? "in" : "out") : "",
2123		({ char *s; \
2124		 switch (usb_pipetype (urb->pipe)) { \
2125		 case PIPE_CONTROL: \
2126			s = ""; \
2127			break; \
2128		 case PIPE_BULK: \
2129			s = "-bulk"; \
2130			break; \
2131		 case PIPE_INTERRUPT: \
2132			s = "-int"; \
2133			break; \
2134		 default: \
2135			s = "-iso"; \
2136			break; \
2137		}; s;}),
2138		urb->actual_length, urb->transfer_buffer_length);
2139}
2140
2141static ssize_t
2142show_urbs (struct device *dev, struct device_attribute *attr, char *buf)
2143{
2144	struct usb_hcd		*hcd = dev_get_drvdata (dev);
2145	struct dummy_hcd	*dum_hcd = hcd_to_dummy_hcd(hcd);
2146	struct urbp		*urbp;
2147	size_t			size = 0;
2148	unsigned long		flags;
2149
2150	spin_lock_irqsave(&dum_hcd->dum->lock, flags);
2151	list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
2152		size_t		temp;
2153
2154		temp = show_urb (buf, PAGE_SIZE - size, urbp->urb);
2155		buf += temp;
2156		size += temp;
2157	}
2158	spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
2159
2160	return size;
2161}
2162static DEVICE_ATTR (urbs, S_IRUGO, show_urbs, NULL);
2163
2164static int dummy_start_ss(struct dummy_hcd *dum_hcd)
2165{
2166	init_timer(&dum_hcd->timer);
2167	dum_hcd->timer.function = dummy_timer;
2168	dum_hcd->timer.data = (unsigned long)dum_hcd;
2169	dum_hcd->rh_state = DUMMY_RH_RUNNING;
2170	INIT_LIST_HEAD(&dum_hcd->urbp_list);
2171	dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
2172	dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
2173	dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
2174#ifdef CONFIG_USB_OTG
2175	dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
2176#endif
2177	return 0;
2178
2179	/* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2180	return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2181}
2182
2183static int dummy_start(struct usb_hcd *hcd)
2184{
2185	struct dummy_hcd	*dum_hcd = hcd_to_dummy_hcd(hcd);
2186
2187	/*
2188	 * MASTER side init ... we emulate a root hub that'll only ever
2189	 * talk to one device (the slave side).  Also appears in sysfs,
2190	 * just like more familiar pci-based HCDs.
2191	 */
2192	if (!usb_hcd_is_primary_hcd(hcd))
2193		return dummy_start_ss(dum_hcd);
2194
2195	spin_lock_init(&dum_hcd->dum->lock);
2196	init_timer(&dum_hcd->timer);
2197	dum_hcd->timer.function = dummy_timer;
2198	dum_hcd->timer.data = (unsigned long)dum_hcd;
2199	dum_hcd->rh_state = DUMMY_RH_RUNNING;
2200
2201	INIT_LIST_HEAD(&dum_hcd->urbp_list);
2202
2203	hcd->power_budget = POWER_BUDGET;
2204	hcd->state = HC_STATE_RUNNING;
2205	hcd->uses_new_polling = 1;
2206
2207#ifdef CONFIG_USB_OTG
2208	hcd->self.otg_port = 1;
2209#endif
2210
2211	/* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
2212	return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
2213}
2214
2215static void dummy_stop (struct usb_hcd *hcd)
2216{
2217	struct dummy		*dum;
2218
2219	dum = (hcd_to_dummy_hcd(hcd))->dum;
2220	device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
2221	usb_gadget_unregister_driver(dum->driver);
2222	dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
2223}
2224
2225/*-------------------------------------------------------------------------*/
2226
2227static int dummy_h_get_frame (struct usb_hcd *hcd)
2228{
2229	return dummy_g_get_frame (NULL);
2230}
2231
2232static int dummy_setup(struct usb_hcd *hcd)
2233{
2234	if (usb_hcd_is_primary_hcd(hcd)) {
2235		the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
2236		the_controller.hs_hcd->dum = &the_controller;
2237		/*
2238		 * Mark the first roothub as being USB 2.0.
2239		 * The USB 3.0 roothub will be registered later by
2240		 * dummy_hcd_probe()
2241		 */
2242		hcd->speed = HCD_USB2;
2243		hcd->self.root_hub->speed = USB_SPEED_HIGH;
2244	} else {
2245		the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
2246		the_controller.ss_hcd->dum = &the_controller;
2247		hcd->speed = HCD_USB3;
2248		hcd->self.root_hub->speed = USB_SPEED_SUPER;
2249	}
2250	return 0;
2251}
2252
2253/* Change a group of bulk endpoints to support multiple stream IDs */
2254int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
2255	struct usb_host_endpoint **eps, unsigned int num_eps,
2256	unsigned int num_streams, gfp_t mem_flags)
2257{
2258	if (hcd->speed != HCD_USB3)
2259		dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2260			"%s() - ERROR! Not supported for USB2.0 roothub\n",
2261			__func__);
2262	return 0;
2263}
2264
2265/* Reverts a group of bulk endpoints back to not using stream IDs. */
2266int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2267	struct usb_host_endpoint **eps, unsigned int num_eps,
2268	gfp_t mem_flags)
2269{
2270	if (hcd->speed != HCD_USB3)
2271		dev_dbg(dummy_dev(hcd_to_dummy_hcd(hcd)),
2272			"%s() - ERROR! Not supported for USB2.0 roothub\n",
2273			__func__);
2274	return 0;
2275}
2276
2277static struct hc_driver dummy_hcd = {
2278	.description =		(char *) driver_name,
2279	.product_desc =		"Dummy host controller",
2280	.hcd_priv_size =	sizeof(struct dummy_hcd),
2281
2282	.flags =		HCD_USB3 | HCD_SHARED,
2283
2284	.reset =		dummy_setup,
2285	.start =		dummy_start,
2286	.stop =			dummy_stop,
2287
2288	.urb_enqueue = 		dummy_urb_enqueue,
2289	.urb_dequeue = 		dummy_urb_dequeue,
2290
2291	.get_frame_number = 	dummy_h_get_frame,
2292
2293	.hub_status_data = 	dummy_hub_status,
2294	.hub_control = 		dummy_hub_control,
2295	.bus_suspend =		dummy_bus_suspend,
2296	.bus_resume =		dummy_bus_resume,
2297
2298	.alloc_streams =	dummy_alloc_streams,
2299	.free_streams =		dummy_free_streams,
2300};
2301
2302static int dummy_hcd_probe(struct platform_device *pdev)
2303{
2304	struct usb_hcd		*hs_hcd;
2305	struct usb_hcd		*ss_hcd;
2306	int			retval;
2307
2308	dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
2309
2310	if (!mod_data.is_super_speed)
2311		dummy_hcd.flags = HCD_USB2;
2312	hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
2313	if (!hs_hcd)
2314		return -ENOMEM;
2315	hs_hcd->has_tt = 1;
2316
2317	retval = usb_add_hcd(hs_hcd, 0, 0);
2318	if (retval != 0) {
2319		usb_put_hcd(hs_hcd);
2320		return retval;
2321	}
2322
2323	if (mod_data.is_super_speed) {
2324		ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
2325					dev_name(&pdev->dev), hs_hcd);
2326		if (!ss_hcd) {
2327			retval = -ENOMEM;
2328			goto dealloc_usb2_hcd;
2329		}
2330
2331		retval = usb_add_hcd(ss_hcd, 0, 0);
2332		if (retval)
2333			goto put_usb3_hcd;
2334	}
2335	return 0;
2336
2337put_usb3_hcd:
2338	usb_put_hcd(ss_hcd);
2339dealloc_usb2_hcd:
2340	usb_put_hcd(hs_hcd);
2341	the_controller.hs_hcd = the_controller.ss_hcd = NULL;
2342	return retval;
2343}
2344
2345static int dummy_hcd_remove(struct platform_device *pdev)
2346{
2347	struct dummy		*dum;
2348
2349	dum = (hcd_to_dummy_hcd(platform_get_drvdata(pdev)))->dum;
2350
2351	if (dum->ss_hcd) {
2352		usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2353		usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
2354	}
2355
2356	usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2357	usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
2358
2359	the_controller.hs_hcd = NULL;
2360	the_controller.ss_hcd = NULL;
2361
2362	return 0;
2363}
2364
2365static int dummy_hcd_suspend (struct platform_device *pdev, pm_message_t state)
2366{
2367	struct usb_hcd		*hcd;
2368	struct dummy_hcd	*dum_hcd;
2369	int			rc = 0;
2370
2371	dev_dbg (&pdev->dev, "%s\n", __func__);
2372
2373	hcd = platform_get_drvdata (pdev);
2374	dum_hcd = hcd_to_dummy_hcd(hcd);
2375	if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
2376		dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
2377		rc = -EBUSY;
2378	} else
2379		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2380	return rc;
2381}
2382
2383static int dummy_hcd_resume (struct platform_device *pdev)
2384{
2385	struct usb_hcd		*hcd;
2386
2387	dev_dbg (&pdev->dev, "%s\n", __func__);
2388
2389	hcd = platform_get_drvdata (pdev);
2390	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2391	usb_hcd_poll_rh_status (hcd);
2392	return 0;
2393}
2394
2395static struct platform_driver dummy_hcd_driver = {
2396	.probe		= dummy_hcd_probe,
2397	.remove		= dummy_hcd_remove,
2398	.suspend	= dummy_hcd_suspend,
2399	.resume		= dummy_hcd_resume,
2400	.driver		= {
2401		.name	= (char *) driver_name,
2402		.owner	= THIS_MODULE,
2403	},
2404};
2405
2406/*-------------------------------------------------------------------------*/
2407
2408static struct platform_device *the_udc_pdev;
2409static struct platform_device *the_hcd_pdev;
2410
2411static int __init init (void)
2412{
2413	int	retval = -ENOMEM;
2414
2415	if (usb_disabled ())
2416		return -ENODEV;
2417
2418	if (!mod_data.is_high_speed && mod_data.is_super_speed)
2419		return -EINVAL;
2420
2421	the_hcd_pdev = platform_device_alloc(driver_name, -1);
2422	if (!the_hcd_pdev)
2423		return retval;
2424	the_udc_pdev = platform_device_alloc(gadget_name, -1);
2425	if (!the_udc_pdev)
2426		goto err_alloc_udc;
2427
2428	retval = platform_driver_register(&dummy_hcd_driver);
2429	if (retval < 0)
2430		goto err_register_hcd_driver;
2431	retval = platform_driver_register(&dummy_udc_driver);
2432	if (retval < 0)
2433		goto err_register_udc_driver;
2434
2435	retval = platform_device_add(the_hcd_pdev);
2436	if (retval < 0)
2437		goto err_add_hcd;
2438	if (!the_controller.hs_hcd ||
2439	    (!the_controller.ss_hcd && mod_data.is_super_speed)) {
2440		/*
2441		 * The hcd was added successfully but its probe function failed
2442		 * for some reason.
2443		 */
2444		retval = -EINVAL;
2445		goto err_add_udc;
2446	}
2447	retval = platform_device_add(the_udc_pdev);
2448	if (retval < 0)
2449		goto err_add_udc;
2450	if (!platform_get_drvdata(the_udc_pdev)) {
2451		/*
2452		 * The udc was added successfully but its probe function failed
2453		 * for some reason.
2454		 */
2455		retval = -EINVAL;
2456		goto err_probe_udc;
2457	}
2458	return retval;
2459
2460err_probe_udc:
2461	platform_device_del(the_udc_pdev);
2462err_add_udc:
2463	platform_device_del(the_hcd_pdev);
2464err_add_hcd:
2465	platform_driver_unregister(&dummy_udc_driver);
2466err_register_udc_driver:
2467	platform_driver_unregister(&dummy_hcd_driver);
2468err_register_hcd_driver:
2469	platform_device_put(the_udc_pdev);
2470err_alloc_udc:
2471	platform_device_put(the_hcd_pdev);
2472	return retval;
2473}
2474module_init (init);
2475
2476static void __exit cleanup (void)
2477{
2478	platform_device_unregister(the_udc_pdev);
2479	platform_device_unregister(the_hcd_pdev);
2480	platform_driver_unregister(&dummy_udc_driver);
2481	platform_driver_unregister(&dummy_hcd_driver);
2482}
2483module_exit (cleanup);
2484