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