xref: /linux/drivers/usb/core/hcd.c (revision dacf1d7a78bf8a131346c47bfba7fe1f3ff44beb)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright Linus Torvalds 1999
4  * (C) Copyright Johannes Erdfelt 1999-2001
5  * (C) Copyright Andreas Gal 1999
6  * (C) Copyright Gregory P. Smith 1999
7  * (C) Copyright Deti Fliegl 1999
8  * (C) Copyright Randy Dunlap 2000
9  * (C) Copyright David Brownell 2000-2002
10  */
11 
12 #include <linux/bcd.h>
13 #include <linux/module.h>
14 #include <linux/version.h>
15 #include <linux/kernel.h>
16 #include <linux/sched/task_stack.h>
17 #include <linux/slab.h>
18 #include <linux/completion.h>
19 #include <linux/utsname.h>
20 #include <linux/mm.h>
21 #include <asm/io.h>
22 #include <linux/device.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/mutex.h>
25 #include <asm/irq.h>
26 #include <asm/byteorder.h>
27 #include <asm/unaligned.h>
28 #include <linux/platform_device.h>
29 #include <linux/workqueue.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/types.h>
32 #include <linux/genalloc.h>
33 #include <linux/io.h>
34 #include <linux/kcov.h>
35 
36 #include <linux/phy/phy.h>
37 #include <linux/usb.h>
38 #include <linux/usb/hcd.h>
39 #include <linux/usb/otg.h>
40 
41 #include "usb.h"
42 #include "phy.h"
43 
44 
45 /*-------------------------------------------------------------------------*/
46 
47 /*
48  * USB Host Controller Driver framework
49  *
50  * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing
51  * HCD-specific behaviors/bugs.
52  *
53  * This does error checks, tracks devices and urbs, and delegates to a
54  * "hc_driver" only for code (and data) that really needs to know about
55  * hardware differences.  That includes root hub registers, i/o queues,
56  * and so on ... but as little else as possible.
57  *
58  * Shared code includes most of the "root hub" code (these are emulated,
59  * though each HC's hardware works differently) and PCI glue, plus request
60  * tracking overhead.  The HCD code should only block on spinlocks or on
61  * hardware handshaking; blocking on software events (such as other kernel
62  * threads releasing resources, or completing actions) is all generic.
63  *
64  * Happens the USB 2.0 spec says this would be invisible inside the "USBD",
65  * and includes mostly a "HCDI" (HCD Interface) along with some APIs used
66  * only by the hub driver ... and that neither should be seen or used by
67  * usb client device drivers.
68  *
69  * Contributors of ideas or unattributed patches include: David Brownell,
70  * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ...
71  *
72  * HISTORY:
73  * 2002-02-21	Pull in most of the usb_bus support from usb.c; some
74  *		associated cleanup.  "usb_hcd" still != "usb_bus".
75  * 2001-12-12	Initial patch version for Linux 2.5.1 kernel.
76  */
77 
78 /*-------------------------------------------------------------------------*/
79 
80 /* Keep track of which host controller drivers are loaded */
81 unsigned long usb_hcds_loaded;
82 EXPORT_SYMBOL_GPL(usb_hcds_loaded);
83 
84 /* host controllers we manage */
85 DEFINE_IDR (usb_bus_idr);
86 EXPORT_SYMBOL_GPL (usb_bus_idr);
87 
88 /* used when allocating bus numbers */
89 #define USB_MAXBUS		64
90 
91 /* used when updating list of hcds */
92 DEFINE_MUTEX(usb_bus_idr_lock);	/* exported only for usbfs */
93 EXPORT_SYMBOL_GPL (usb_bus_idr_lock);
94 
95 /* used for controlling access to virtual root hubs */
96 static DEFINE_SPINLOCK(hcd_root_hub_lock);
97 
98 /* used when updating an endpoint's URB list */
99 static DEFINE_SPINLOCK(hcd_urb_list_lock);
100 
101 /* used to protect against unlinking URBs after the device is gone */
102 static DEFINE_SPINLOCK(hcd_urb_unlink_lock);
103 
104 /* wait queue for synchronous unlinks */
105 DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue);
106 
107 /*-------------------------------------------------------------------------*/
108 
109 /*
110  * Sharable chunks of root hub code.
111  */
112 
113 /*-------------------------------------------------------------------------*/
114 #define KERNEL_REL	bin2bcd(LINUX_VERSION_MAJOR)
115 #define KERNEL_VER	bin2bcd(LINUX_VERSION_PATCHLEVEL)
116 
117 /* usb 3.1 root hub device descriptor */
118 static const u8 usb31_rh_dev_descriptor[18] = {
119 	0x12,       /*  __u8  bLength; */
120 	USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
121 	0x10, 0x03, /*  __le16 bcdUSB; v3.1 */
122 
123 	0x09,	    /*  __u8  bDeviceClass; HUB_CLASSCODE */
124 	0x00,	    /*  __u8  bDeviceSubClass; */
125 	0x03,       /*  __u8  bDeviceProtocol; USB 3 hub */
126 	0x09,       /*  __u8  bMaxPacketSize0; 2^9 = 512 Bytes */
127 
128 	0x6b, 0x1d, /*  __le16 idVendor; Linux Foundation 0x1d6b */
129 	0x03, 0x00, /*  __le16 idProduct; device 0x0003 */
130 	KERNEL_VER, KERNEL_REL, /*  __le16 bcdDevice */
131 
132 	0x03,       /*  __u8  iManufacturer; */
133 	0x02,       /*  __u8  iProduct; */
134 	0x01,       /*  __u8  iSerialNumber; */
135 	0x01        /*  __u8  bNumConfigurations; */
136 };
137 
138 /* usb 3.0 root hub device descriptor */
139 static const u8 usb3_rh_dev_descriptor[18] = {
140 	0x12,       /*  __u8  bLength; */
141 	USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
142 	0x00, 0x03, /*  __le16 bcdUSB; v3.0 */
143 
144 	0x09,	    /*  __u8  bDeviceClass; HUB_CLASSCODE */
145 	0x00,	    /*  __u8  bDeviceSubClass; */
146 	0x03,       /*  __u8  bDeviceProtocol; USB 3.0 hub */
147 	0x09,       /*  __u8  bMaxPacketSize0; 2^9 = 512 Bytes */
148 
149 	0x6b, 0x1d, /*  __le16 idVendor; Linux Foundation 0x1d6b */
150 	0x03, 0x00, /*  __le16 idProduct; device 0x0003 */
151 	KERNEL_VER, KERNEL_REL, /*  __le16 bcdDevice */
152 
153 	0x03,       /*  __u8  iManufacturer; */
154 	0x02,       /*  __u8  iProduct; */
155 	0x01,       /*  __u8  iSerialNumber; */
156 	0x01        /*  __u8  bNumConfigurations; */
157 };
158 
159 /* usb 2.0 root hub device descriptor */
160 static const u8 usb2_rh_dev_descriptor[18] = {
161 	0x12,       /*  __u8  bLength; */
162 	USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
163 	0x00, 0x02, /*  __le16 bcdUSB; v2.0 */
164 
165 	0x09,	    /*  __u8  bDeviceClass; HUB_CLASSCODE */
166 	0x00,	    /*  __u8  bDeviceSubClass; */
167 	0x00,       /*  __u8  bDeviceProtocol; [ usb 2.0 no TT ] */
168 	0x40,       /*  __u8  bMaxPacketSize0; 64 Bytes */
169 
170 	0x6b, 0x1d, /*  __le16 idVendor; Linux Foundation 0x1d6b */
171 	0x02, 0x00, /*  __le16 idProduct; device 0x0002 */
172 	KERNEL_VER, KERNEL_REL, /*  __le16 bcdDevice */
173 
174 	0x03,       /*  __u8  iManufacturer; */
175 	0x02,       /*  __u8  iProduct; */
176 	0x01,       /*  __u8  iSerialNumber; */
177 	0x01        /*  __u8  bNumConfigurations; */
178 };
179 
180 /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
181 
182 /* usb 1.1 root hub device descriptor */
183 static const u8 usb11_rh_dev_descriptor[18] = {
184 	0x12,       /*  __u8  bLength; */
185 	USB_DT_DEVICE, /* __u8 bDescriptorType; Device */
186 	0x10, 0x01, /*  __le16 bcdUSB; v1.1 */
187 
188 	0x09,	    /*  __u8  bDeviceClass; HUB_CLASSCODE */
189 	0x00,	    /*  __u8  bDeviceSubClass; */
190 	0x00,       /*  __u8  bDeviceProtocol; [ low/full speeds only ] */
191 	0x40,       /*  __u8  bMaxPacketSize0; 64 Bytes */
192 
193 	0x6b, 0x1d, /*  __le16 idVendor; Linux Foundation 0x1d6b */
194 	0x01, 0x00, /*  __le16 idProduct; device 0x0001 */
195 	KERNEL_VER, KERNEL_REL, /*  __le16 bcdDevice */
196 
197 	0x03,       /*  __u8  iManufacturer; */
198 	0x02,       /*  __u8  iProduct; */
199 	0x01,       /*  __u8  iSerialNumber; */
200 	0x01        /*  __u8  bNumConfigurations; */
201 };
202 
203 
204 /*-------------------------------------------------------------------------*/
205 
206 /* Configuration descriptors for our root hubs */
207 
208 static const u8 fs_rh_config_descriptor[] = {
209 
210 	/* one configuration */
211 	0x09,       /*  __u8  bLength; */
212 	USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */
213 	0x19, 0x00, /*  __le16 wTotalLength; */
214 	0x01,       /*  __u8  bNumInterfaces; (1) */
215 	0x01,       /*  __u8  bConfigurationValue; */
216 	0x00,       /*  __u8  iConfiguration; */
217 	0xc0,       /*  __u8  bmAttributes;
218 				 Bit 7: must be set,
219 				     6: Self-powered,
220 				     5: Remote wakeup,
221 				     4..0: resvd */
222 	0x00,       /*  __u8  MaxPower; */
223 
224 	/* USB 1.1:
225 	 * USB 2.0, single TT organization (mandatory):
226 	 *	one interface, protocol 0
227 	 *
228 	 * USB 2.0, multiple TT organization (optional):
229 	 *	two interfaces, protocols 1 (like single TT)
230 	 *	and 2 (multiple TT mode) ... config is
231 	 *	sometimes settable
232 	 *	NOT IMPLEMENTED
233 	 */
234 
235 	/* one interface */
236 	0x09,       /*  __u8  if_bLength; */
237 	USB_DT_INTERFACE,  /* __u8 if_bDescriptorType; Interface */
238 	0x00,       /*  __u8  if_bInterfaceNumber; */
239 	0x00,       /*  __u8  if_bAlternateSetting; */
240 	0x01,       /*  __u8  if_bNumEndpoints; */
241 	0x09,       /*  __u8  if_bInterfaceClass; HUB_CLASSCODE */
242 	0x00,       /*  __u8  if_bInterfaceSubClass; */
243 	0x00,       /*  __u8  if_bInterfaceProtocol; [usb1.1 or single tt] */
244 	0x00,       /*  __u8  if_iInterface; */
245 
246 	/* one endpoint (status change endpoint) */
247 	0x07,       /*  __u8  ep_bLength; */
248 	USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */
249 	0x81,       /*  __u8  ep_bEndpointAddress; IN Endpoint 1 */
250 	0x03,       /*  __u8  ep_bmAttributes; Interrupt */
251 	0x02, 0x00, /*  __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
252 	0xff        /*  __u8  ep_bInterval; (255ms -- usb 2.0 spec) */
253 };
254 
255 static const u8 hs_rh_config_descriptor[] = {
256 
257 	/* one configuration */
258 	0x09,       /*  __u8  bLength; */
259 	USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */
260 	0x19, 0x00, /*  __le16 wTotalLength; */
261 	0x01,       /*  __u8  bNumInterfaces; (1) */
262 	0x01,       /*  __u8  bConfigurationValue; */
263 	0x00,       /*  __u8  iConfiguration; */
264 	0xc0,       /*  __u8  bmAttributes;
265 				 Bit 7: must be set,
266 				     6: Self-powered,
267 				     5: Remote wakeup,
268 				     4..0: resvd */
269 	0x00,       /*  __u8  MaxPower; */
270 
271 	/* USB 1.1:
272 	 * USB 2.0, single TT organization (mandatory):
273 	 *	one interface, protocol 0
274 	 *
275 	 * USB 2.0, multiple TT organization (optional):
276 	 *	two interfaces, protocols 1 (like single TT)
277 	 *	and 2 (multiple TT mode) ... config is
278 	 *	sometimes settable
279 	 *	NOT IMPLEMENTED
280 	 */
281 
282 	/* one interface */
283 	0x09,       /*  __u8  if_bLength; */
284 	USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */
285 	0x00,       /*  __u8  if_bInterfaceNumber; */
286 	0x00,       /*  __u8  if_bAlternateSetting; */
287 	0x01,       /*  __u8  if_bNumEndpoints; */
288 	0x09,       /*  __u8  if_bInterfaceClass; HUB_CLASSCODE */
289 	0x00,       /*  __u8  if_bInterfaceSubClass; */
290 	0x00,       /*  __u8  if_bInterfaceProtocol; [usb1.1 or single tt] */
291 	0x00,       /*  __u8  if_iInterface; */
292 
293 	/* one endpoint (status change endpoint) */
294 	0x07,       /*  __u8  ep_bLength; */
295 	USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */
296 	0x81,       /*  __u8  ep_bEndpointAddress; IN Endpoint 1 */
297 	0x03,       /*  __u8  ep_bmAttributes; Interrupt */
298 		    /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
299 		     * see hub.c:hub_configure() for details. */
300 	(USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
301 	0x0c        /*  __u8  ep_bInterval; (256ms -- usb 2.0 spec) */
302 };
303 
304 static const u8 ss_rh_config_descriptor[] = {
305 	/* one configuration */
306 	0x09,       /*  __u8  bLength; */
307 	USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */
308 	0x1f, 0x00, /*  __le16 wTotalLength; */
309 	0x01,       /*  __u8  bNumInterfaces; (1) */
310 	0x01,       /*  __u8  bConfigurationValue; */
311 	0x00,       /*  __u8  iConfiguration; */
312 	0xc0,       /*  __u8  bmAttributes;
313 				 Bit 7: must be set,
314 				     6: Self-powered,
315 				     5: Remote wakeup,
316 				     4..0: resvd */
317 	0x00,       /*  __u8  MaxPower; */
318 
319 	/* one interface */
320 	0x09,       /*  __u8  if_bLength; */
321 	USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */
322 	0x00,       /*  __u8  if_bInterfaceNumber; */
323 	0x00,       /*  __u8  if_bAlternateSetting; */
324 	0x01,       /*  __u8  if_bNumEndpoints; */
325 	0x09,       /*  __u8  if_bInterfaceClass; HUB_CLASSCODE */
326 	0x00,       /*  __u8  if_bInterfaceSubClass; */
327 	0x00,       /*  __u8  if_bInterfaceProtocol; */
328 	0x00,       /*  __u8  if_iInterface; */
329 
330 	/* one endpoint (status change endpoint) */
331 	0x07,       /*  __u8  ep_bLength; */
332 	USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */
333 	0x81,       /*  __u8  ep_bEndpointAddress; IN Endpoint 1 */
334 	0x03,       /*  __u8  ep_bmAttributes; Interrupt */
335 		    /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
336 		     * see hub.c:hub_configure() for details. */
337 	(USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
338 	0x0c,       /*  __u8  ep_bInterval; (256ms -- usb 2.0 spec) */
339 
340 	/* one SuperSpeed endpoint companion descriptor */
341 	0x06,        /* __u8 ss_bLength */
342 	USB_DT_SS_ENDPOINT_COMP, /* __u8 ss_bDescriptorType; SuperSpeed EP */
343 		     /* Companion */
344 	0x00,        /* __u8 ss_bMaxBurst; allows 1 TX between ACKs */
345 	0x00,        /* __u8 ss_bmAttributes; 1 packet per service interval */
346 	0x02, 0x00   /* __le16 ss_wBytesPerInterval; 15 bits for max 15 ports */
347 };
348 
349 /* authorized_default behaviour:
350  * -1 is authorized for all devices (leftover from wireless USB)
351  * 0 is unauthorized for all devices
352  * 1 is authorized for all devices
353  * 2 is authorized for internal devices
354  */
355 #define USB_AUTHORIZE_WIRED	-1
356 #define USB_AUTHORIZE_NONE	0
357 #define USB_AUTHORIZE_ALL	1
358 #define USB_AUTHORIZE_INTERNAL	2
359 
360 static int authorized_default = CONFIG_USB_DEFAULT_AUTHORIZATION_MODE;
361 module_param(authorized_default, int, S_IRUGO|S_IWUSR);
362 MODULE_PARM_DESC(authorized_default,
363 		"Default USB device authorization: 0 is not authorized, 1 is authorized (default), 2 is authorized for internal devices, -1 is authorized (same as 1)");
364 /*-------------------------------------------------------------------------*/
365 
366 /**
367  * ascii2desc() - Helper routine for producing UTF-16LE string descriptors
368  * @s: Null-terminated ASCII (actually ISO-8859-1) string
369  * @buf: Buffer for USB string descriptor (header + UTF-16LE)
370  * @len: Length (in bytes; may be odd) of descriptor buffer.
371  *
372  * Return: The number of bytes filled in: 2 + 2*strlen(s) or @len,
373  * whichever is less.
374  *
375  * Note:
376  * USB String descriptors can contain at most 126 characters; input
377  * strings longer than that are truncated.
378  */
379 static unsigned
380 ascii2desc(char const *s, u8 *buf, unsigned len)
381 {
382 	unsigned n, t = 2 + 2*strlen(s);
383 
384 	if (t > 254)
385 		t = 254;	/* Longest possible UTF string descriptor */
386 	if (len > t)
387 		len = t;
388 
389 	t += USB_DT_STRING << 8;	/* Now t is first 16 bits to store */
390 
391 	n = len;
392 	while (n--) {
393 		*buf++ = t;
394 		if (!n--)
395 			break;
396 		*buf++ = t >> 8;
397 		t = (unsigned char)*s++;
398 	}
399 	return len;
400 }
401 
402 /**
403  * rh_string() - provides string descriptors for root hub
404  * @id: the string ID number (0: langids, 1: serial #, 2: product, 3: vendor)
405  * @hcd: the host controller for this root hub
406  * @data: buffer for output packet
407  * @len: length of the provided buffer
408  *
409  * Produces either a manufacturer, product or serial number string for the
410  * virtual root hub device.
411  *
412  * Return: The number of bytes filled in: the length of the descriptor or
413  * of the provided buffer, whichever is less.
414  */
415 static unsigned
416 rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len)
417 {
418 	char buf[100];
419 	char const *s;
420 	static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};
421 
422 	/* language ids */
423 	switch (id) {
424 	case 0:
425 		/* Array of LANGID codes (0x0409 is MSFT-speak for "en-us") */
426 		/* See http://www.usb.org/developers/docs/USB_LANGIDs.pdf */
427 		if (len > 4)
428 			len = 4;
429 		memcpy(data, langids, len);
430 		return len;
431 	case 1:
432 		/* Serial number */
433 		s = hcd->self.bus_name;
434 		break;
435 	case 2:
436 		/* Product name */
437 		s = hcd->product_desc;
438 		break;
439 	case 3:
440 		/* Manufacturer */
441 		snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
442 			init_utsname()->release, hcd->driver->description);
443 		s = buf;
444 		break;
445 	default:
446 		/* Can't happen; caller guarantees it */
447 		return 0;
448 	}
449 
450 	return ascii2desc(s, data, len);
451 }
452 
453 
454 /* Root hub control transfers execute synchronously */
455 static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
456 {
457 	struct usb_ctrlrequest *cmd;
458 	u16		typeReq, wValue, wIndex, wLength;
459 	u8		*ubuf = urb->transfer_buffer;
460 	unsigned	len = 0;
461 	int		status;
462 	u8		patch_wakeup = 0;
463 	u8		patch_protocol = 0;
464 	u16		tbuf_size;
465 	u8		*tbuf = NULL;
466 	const u8	*bufp;
467 
468 	might_sleep();
469 
470 	spin_lock_irq(&hcd_root_hub_lock);
471 	status = usb_hcd_link_urb_to_ep(hcd, urb);
472 	spin_unlock_irq(&hcd_root_hub_lock);
473 	if (status)
474 		return status;
475 	urb->hcpriv = hcd;	/* Indicate it's queued */
476 
477 	cmd = (struct usb_ctrlrequest *) urb->setup_packet;
478 	typeReq  = (cmd->bRequestType << 8) | cmd->bRequest;
479 	wValue   = le16_to_cpu (cmd->wValue);
480 	wIndex   = le16_to_cpu (cmd->wIndex);
481 	wLength  = le16_to_cpu (cmd->wLength);
482 
483 	if (wLength > urb->transfer_buffer_length)
484 		goto error;
485 
486 	/*
487 	 * tbuf should be at least as big as the
488 	 * USB hub descriptor.
489 	 */
490 	tbuf_size =  max_t(u16, sizeof(struct usb_hub_descriptor), wLength);
491 	tbuf = kzalloc(tbuf_size, GFP_KERNEL);
492 	if (!tbuf) {
493 		status = -ENOMEM;
494 		goto err_alloc;
495 	}
496 
497 	bufp = tbuf;
498 
499 
500 	urb->actual_length = 0;
501 	switch (typeReq) {
502 
503 	/* DEVICE REQUESTS */
504 
505 	/* The root hub's remote wakeup enable bit is implemented using
506 	 * driver model wakeup flags.  If this system supports wakeup
507 	 * through USB, userspace may change the default "allow wakeup"
508 	 * policy through sysfs or these calls.
509 	 *
510 	 * Most root hubs support wakeup from downstream devices, for
511 	 * runtime power management (disabling USB clocks and reducing
512 	 * VBUS power usage).  However, not all of them do so; silicon,
513 	 * board, and BIOS bugs here are not uncommon, so these can't
514 	 * be treated quite like external hubs.
515 	 *
516 	 * Likewise, not all root hubs will pass wakeup events upstream,
517 	 * to wake up the whole system.  So don't assume root hub and
518 	 * controller capabilities are identical.
519 	 */
520 
521 	case DeviceRequest | USB_REQ_GET_STATUS:
522 		tbuf[0] = (device_may_wakeup(&hcd->self.root_hub->dev)
523 					<< USB_DEVICE_REMOTE_WAKEUP)
524 				| (1 << USB_DEVICE_SELF_POWERED);
525 		tbuf[1] = 0;
526 		len = 2;
527 		break;
528 	case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
529 		if (wValue == USB_DEVICE_REMOTE_WAKEUP)
530 			device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
531 		else
532 			goto error;
533 		break;
534 	case DeviceOutRequest | USB_REQ_SET_FEATURE:
535 		if (device_can_wakeup(&hcd->self.root_hub->dev)
536 				&& wValue == USB_DEVICE_REMOTE_WAKEUP)
537 			device_set_wakeup_enable(&hcd->self.root_hub->dev, 1);
538 		else
539 			goto error;
540 		break;
541 	case DeviceRequest | USB_REQ_GET_CONFIGURATION:
542 		tbuf[0] = 1;
543 		len = 1;
544 		fallthrough;
545 	case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
546 		break;
547 	case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
548 		switch (wValue & 0xff00) {
549 		case USB_DT_DEVICE << 8:
550 			switch (hcd->speed) {
551 			case HCD_USB32:
552 			case HCD_USB31:
553 				bufp = usb31_rh_dev_descriptor;
554 				break;
555 			case HCD_USB3:
556 				bufp = usb3_rh_dev_descriptor;
557 				break;
558 			case HCD_USB2:
559 				bufp = usb2_rh_dev_descriptor;
560 				break;
561 			case HCD_USB11:
562 				bufp = usb11_rh_dev_descriptor;
563 				break;
564 			default:
565 				goto error;
566 			}
567 			len = 18;
568 			if (hcd->has_tt)
569 				patch_protocol = 1;
570 			break;
571 		case USB_DT_CONFIG << 8:
572 			switch (hcd->speed) {
573 			case HCD_USB32:
574 			case HCD_USB31:
575 			case HCD_USB3:
576 				bufp = ss_rh_config_descriptor;
577 				len = sizeof ss_rh_config_descriptor;
578 				break;
579 			case HCD_USB2:
580 				bufp = hs_rh_config_descriptor;
581 				len = sizeof hs_rh_config_descriptor;
582 				break;
583 			case HCD_USB11:
584 				bufp = fs_rh_config_descriptor;
585 				len = sizeof fs_rh_config_descriptor;
586 				break;
587 			default:
588 				goto error;
589 			}
590 			if (device_can_wakeup(&hcd->self.root_hub->dev))
591 				patch_wakeup = 1;
592 			break;
593 		case USB_DT_STRING << 8:
594 			if ((wValue & 0xff) < 4)
595 				urb->actual_length = rh_string(wValue & 0xff,
596 						hcd, ubuf, wLength);
597 			else /* unsupported IDs --> "protocol stall" */
598 				goto error;
599 			break;
600 		case USB_DT_BOS << 8:
601 			goto nongeneric;
602 		default:
603 			goto error;
604 		}
605 		break;
606 	case DeviceRequest | USB_REQ_GET_INTERFACE:
607 		tbuf[0] = 0;
608 		len = 1;
609 		fallthrough;
610 	case DeviceOutRequest | USB_REQ_SET_INTERFACE:
611 		break;
612 	case DeviceOutRequest | USB_REQ_SET_ADDRESS:
613 		/* wValue == urb->dev->devaddr */
614 		dev_dbg (hcd->self.controller, "root hub device address %d\n",
615 			wValue);
616 		break;
617 
618 	/* INTERFACE REQUESTS (no defined feature/status flags) */
619 
620 	/* ENDPOINT REQUESTS */
621 
622 	case EndpointRequest | USB_REQ_GET_STATUS:
623 		/* ENDPOINT_HALT flag */
624 		tbuf[0] = 0;
625 		tbuf[1] = 0;
626 		len = 2;
627 		fallthrough;
628 	case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
629 	case EndpointOutRequest | USB_REQ_SET_FEATURE:
630 		dev_dbg (hcd->self.controller, "no endpoint features yet\n");
631 		break;
632 
633 	/* CLASS REQUESTS (and errors) */
634 
635 	default:
636 nongeneric:
637 		/* non-generic request */
638 		switch (typeReq) {
639 		case GetHubStatus:
640 			len = 4;
641 			break;
642 		case GetPortStatus:
643 			if (wValue == HUB_PORT_STATUS)
644 				len = 4;
645 			else
646 				/* other port status types return 8 bytes */
647 				len = 8;
648 			break;
649 		case GetHubDescriptor:
650 			len = sizeof (struct usb_hub_descriptor);
651 			break;
652 		case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
653 			/* len is returned by hub_control */
654 			break;
655 		}
656 		status = hcd->driver->hub_control (hcd,
657 			typeReq, wValue, wIndex,
658 			tbuf, wLength);
659 
660 		if (typeReq == GetHubDescriptor)
661 			usb_hub_adjust_deviceremovable(hcd->self.root_hub,
662 				(struct usb_hub_descriptor *)tbuf);
663 		break;
664 error:
665 		/* "protocol stall" on error */
666 		status = -EPIPE;
667 	}
668 
669 	if (status < 0) {
670 		len = 0;
671 		if (status != -EPIPE) {
672 			dev_dbg (hcd->self.controller,
673 				"CTRL: TypeReq=0x%x val=0x%x "
674 				"idx=0x%x len=%d ==> %d\n",
675 				typeReq, wValue, wIndex,
676 				wLength, status);
677 		}
678 	} else if (status > 0) {
679 		/* hub_control may return the length of data copied. */
680 		len = status;
681 		status = 0;
682 	}
683 	if (len) {
684 		if (urb->transfer_buffer_length < len)
685 			len = urb->transfer_buffer_length;
686 		urb->actual_length = len;
687 		/* always USB_DIR_IN, toward host */
688 		memcpy (ubuf, bufp, len);
689 
690 		/* report whether RH hardware supports remote wakeup */
691 		if (patch_wakeup &&
692 				len > offsetof (struct usb_config_descriptor,
693 						bmAttributes))
694 			((struct usb_config_descriptor *)ubuf)->bmAttributes
695 				|= USB_CONFIG_ATT_WAKEUP;
696 
697 		/* report whether RH hardware has an integrated TT */
698 		if (patch_protocol &&
699 				len > offsetof(struct usb_device_descriptor,
700 						bDeviceProtocol))
701 			((struct usb_device_descriptor *) ubuf)->
702 				bDeviceProtocol = USB_HUB_PR_HS_SINGLE_TT;
703 	}
704 
705 	kfree(tbuf);
706  err_alloc:
707 
708 	/* any errors get returned through the urb completion */
709 	spin_lock_irq(&hcd_root_hub_lock);
710 	usb_hcd_unlink_urb_from_ep(hcd, urb);
711 	usb_hcd_giveback_urb(hcd, urb, status);
712 	spin_unlock_irq(&hcd_root_hub_lock);
713 	return 0;
714 }
715 
716 /*-------------------------------------------------------------------------*/
717 
718 /*
719  * Root Hub interrupt transfers are polled using a timer if the
720  * driver requests it; otherwise the driver is responsible for
721  * calling usb_hcd_poll_rh_status() when an event occurs.
722  *
723  * Completion handler may not sleep. See usb_hcd_giveback_urb() for details.
724  */
725 void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
726 {
727 	struct urb	*urb;
728 	int		length;
729 	int		status;
730 	unsigned long	flags;
731 	char		buffer[6];	/* Any root hubs with > 31 ports? */
732 
733 	if (unlikely(!hcd->rh_pollable))
734 		return;
735 	if (!hcd->uses_new_polling && !hcd->status_urb)
736 		return;
737 
738 	length = hcd->driver->hub_status_data(hcd, buffer);
739 	if (length > 0) {
740 
741 		/* try to complete the status urb */
742 		spin_lock_irqsave(&hcd_root_hub_lock, flags);
743 		urb = hcd->status_urb;
744 		if (urb) {
745 			clear_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
746 			hcd->status_urb = NULL;
747 			if (urb->transfer_buffer_length >= length) {
748 				status = 0;
749 			} else {
750 				status = -EOVERFLOW;
751 				length = urb->transfer_buffer_length;
752 			}
753 			urb->actual_length = length;
754 			memcpy(urb->transfer_buffer, buffer, length);
755 
756 			usb_hcd_unlink_urb_from_ep(hcd, urb);
757 			usb_hcd_giveback_urb(hcd, urb, status);
758 		} else {
759 			length = 0;
760 			set_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
761 		}
762 		spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
763 	}
764 
765 	/* The USB 2.0 spec says 256 ms.  This is close enough and won't
766 	 * exceed that limit if HZ is 100. The math is more clunky than
767 	 * maybe expected, this is to make sure that all timers for USB devices
768 	 * fire at the same time to give the CPU a break in between */
769 	if (hcd->uses_new_polling ? HCD_POLL_RH(hcd) :
770 			(length == 0 && hcd->status_urb != NULL))
771 		mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
772 }
773 EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
774 
775 /* timer callback */
776 static void rh_timer_func (struct timer_list *t)
777 {
778 	struct usb_hcd *_hcd = from_timer(_hcd, t, rh_timer);
779 
780 	usb_hcd_poll_rh_status(_hcd);
781 }
782 
783 /*-------------------------------------------------------------------------*/
784 
785 static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
786 {
787 	int		retval;
788 	unsigned long	flags;
789 	unsigned	len = 1 + (urb->dev->maxchild / 8);
790 
791 	spin_lock_irqsave (&hcd_root_hub_lock, flags);
792 	if (hcd->status_urb || urb->transfer_buffer_length < len) {
793 		dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
794 		retval = -EINVAL;
795 		goto done;
796 	}
797 
798 	retval = usb_hcd_link_urb_to_ep(hcd, urb);
799 	if (retval)
800 		goto done;
801 
802 	hcd->status_urb = urb;
803 	urb->hcpriv = hcd;	/* indicate it's queued */
804 	if (!hcd->uses_new_polling)
805 		mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
806 
807 	/* If a status change has already occurred, report it ASAP */
808 	else if (HCD_POLL_PENDING(hcd))
809 		mod_timer(&hcd->rh_timer, jiffies);
810 	retval = 0;
811  done:
812 	spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
813 	return retval;
814 }
815 
816 static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
817 {
818 	if (usb_endpoint_xfer_int(&urb->ep->desc))
819 		return rh_queue_status (hcd, urb);
820 	if (usb_endpoint_xfer_control(&urb->ep->desc))
821 		return rh_call_control (hcd, urb);
822 	return -EINVAL;
823 }
824 
825 /*-------------------------------------------------------------------------*/
826 
827 /* Unlinks of root-hub control URBs are legal, but they don't do anything
828  * since these URBs always execute synchronously.
829  */
830 static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
831 {
832 	unsigned long	flags;
833 	int		rc;
834 
835 	spin_lock_irqsave(&hcd_root_hub_lock, flags);
836 	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
837 	if (rc)
838 		goto done;
839 
840 	if (usb_endpoint_num(&urb->ep->desc) == 0) {	/* Control URB */
841 		;	/* Do nothing */
842 
843 	} else {				/* Status URB */
844 		if (!hcd->uses_new_polling)
845 			del_timer (&hcd->rh_timer);
846 		if (urb == hcd->status_urb) {
847 			hcd->status_urb = NULL;
848 			usb_hcd_unlink_urb_from_ep(hcd, urb);
849 			usb_hcd_giveback_urb(hcd, urb, status);
850 		}
851 	}
852  done:
853 	spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
854 	return rc;
855 }
856 
857 
858 /*-------------------------------------------------------------------------*/
859 
860 /**
861  * usb_bus_init - shared initialization code
862  * @bus: the bus structure being initialized
863  *
864  * This code is used to initialize a usb_bus structure, memory for which is
865  * separately managed.
866  */
867 static void usb_bus_init (struct usb_bus *bus)
868 {
869 	memset (&bus->devmap, 0, sizeof(struct usb_devmap));
870 
871 	bus->devnum_next = 1;
872 
873 	bus->root_hub = NULL;
874 	bus->busnum = -1;
875 	bus->bandwidth_allocated = 0;
876 	bus->bandwidth_int_reqs  = 0;
877 	bus->bandwidth_isoc_reqs = 0;
878 	mutex_init(&bus->devnum_next_mutex);
879 }
880 
881 /*-------------------------------------------------------------------------*/
882 
883 /**
884  * usb_register_bus - registers the USB host controller with the usb core
885  * @bus: pointer to the bus to register
886  *
887  * Context: task context, might sleep.
888  *
889  * Assigns a bus number, and links the controller into usbcore data
890  * structures so that it can be seen by scanning the bus list.
891  *
892  * Return: 0 if successful. A negative error code otherwise.
893  */
894 static int usb_register_bus(struct usb_bus *bus)
895 {
896 	int result = -E2BIG;
897 	int busnum;
898 
899 	mutex_lock(&usb_bus_idr_lock);
900 	busnum = idr_alloc(&usb_bus_idr, bus, 1, USB_MAXBUS, GFP_KERNEL);
901 	if (busnum < 0) {
902 		pr_err("%s: failed to get bus number\n", usbcore_name);
903 		goto error_find_busnum;
904 	}
905 	bus->busnum = busnum;
906 	mutex_unlock(&usb_bus_idr_lock);
907 
908 	usb_notify_add_bus(bus);
909 
910 	dev_info (bus->controller, "new USB bus registered, assigned bus "
911 		  "number %d\n", bus->busnum);
912 	return 0;
913 
914 error_find_busnum:
915 	mutex_unlock(&usb_bus_idr_lock);
916 	return result;
917 }
918 
919 /**
920  * usb_deregister_bus - deregisters the USB host controller
921  * @bus: pointer to the bus to deregister
922  *
923  * Context: task context, might sleep.
924  *
925  * Recycles the bus number, and unlinks the controller from usbcore data
926  * structures so that it won't be seen by scanning the bus list.
927  */
928 static void usb_deregister_bus (struct usb_bus *bus)
929 {
930 	dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
931 
932 	/*
933 	 * NOTE: make sure that all the devices are removed by the
934 	 * controller code, as well as having it call this when cleaning
935 	 * itself up
936 	 */
937 	mutex_lock(&usb_bus_idr_lock);
938 	idr_remove(&usb_bus_idr, bus->busnum);
939 	mutex_unlock(&usb_bus_idr_lock);
940 
941 	usb_notify_remove_bus(bus);
942 }
943 
944 /**
945  * register_root_hub - called by usb_add_hcd() to register a root hub
946  * @hcd: host controller for this root hub
947  *
948  * This function registers the root hub with the USB subsystem.  It sets up
949  * the device properly in the device tree and then calls usb_new_device()
950  * to register the usb device.  It also assigns the root hub's USB address
951  * (always 1).
952  *
953  * Return: 0 if successful. A negative error code otherwise.
954  */
955 static int register_root_hub(struct usb_hcd *hcd)
956 {
957 	struct device *parent_dev = hcd->self.controller;
958 	struct usb_device *usb_dev = hcd->self.root_hub;
959 	struct usb_device_descriptor *descr;
960 	const int devnum = 1;
961 	int retval;
962 
963 	usb_dev->devnum = devnum;
964 	usb_dev->bus->devnum_next = devnum + 1;
965 	set_bit (devnum, usb_dev->bus->devmap.devicemap);
966 	usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
967 
968 	mutex_lock(&usb_bus_idr_lock);
969 
970 	usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
971 	descr = usb_get_device_descriptor(usb_dev);
972 	if (IS_ERR(descr)) {
973 		retval = PTR_ERR(descr);
974 		mutex_unlock(&usb_bus_idr_lock);
975 		dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
976 				dev_name(&usb_dev->dev), retval);
977 		return retval;
978 	}
979 	usb_dev->descriptor = *descr;
980 	kfree(descr);
981 
982 	if (le16_to_cpu(usb_dev->descriptor.bcdUSB) >= 0x0201) {
983 		retval = usb_get_bos_descriptor(usb_dev);
984 		if (!retval) {
985 			usb_dev->lpm_capable = usb_device_supports_lpm(usb_dev);
986 		} else if (usb_dev->speed >= USB_SPEED_SUPER) {
987 			mutex_unlock(&usb_bus_idr_lock);
988 			dev_dbg(parent_dev, "can't read %s bos descriptor %d\n",
989 					dev_name(&usb_dev->dev), retval);
990 			return retval;
991 		}
992 	}
993 
994 	retval = usb_new_device (usb_dev);
995 	if (retval) {
996 		dev_err (parent_dev, "can't register root hub for %s, %d\n",
997 				dev_name(&usb_dev->dev), retval);
998 	} else {
999 		spin_lock_irq (&hcd_root_hub_lock);
1000 		hcd->rh_registered = 1;
1001 		spin_unlock_irq (&hcd_root_hub_lock);
1002 
1003 		/* Did the HC die before the root hub was registered? */
1004 		if (HCD_DEAD(hcd))
1005 			usb_hc_died (hcd);	/* This time clean up */
1006 	}
1007 	mutex_unlock(&usb_bus_idr_lock);
1008 
1009 	return retval;
1010 }
1011 
1012 /*
1013  * usb_hcd_start_port_resume - a root-hub port is sending a resume signal
1014  * @bus: the bus which the root hub belongs to
1015  * @portnum: the port which is being resumed
1016  *
1017  * HCDs should call this function when they know that a resume signal is
1018  * being sent to a root-hub port.  The root hub will be prevented from
1019  * going into autosuspend until usb_hcd_end_port_resume() is called.
1020  *
1021  * The bus's private lock must be held by the caller.
1022  */
1023 void usb_hcd_start_port_resume(struct usb_bus *bus, int portnum)
1024 {
1025 	unsigned bit = 1 << portnum;
1026 
1027 	if (!(bus->resuming_ports & bit)) {
1028 		bus->resuming_ports |= bit;
1029 		pm_runtime_get_noresume(&bus->root_hub->dev);
1030 	}
1031 }
1032 EXPORT_SYMBOL_GPL(usb_hcd_start_port_resume);
1033 
1034 /*
1035  * usb_hcd_end_port_resume - a root-hub port has stopped sending a resume signal
1036  * @bus: the bus which the root hub belongs to
1037  * @portnum: the port which is being resumed
1038  *
1039  * HCDs should call this function when they know that a resume signal has
1040  * stopped being sent to a root-hub port.  The root hub will be allowed to
1041  * autosuspend again.
1042  *
1043  * The bus's private lock must be held by the caller.
1044  */
1045 void usb_hcd_end_port_resume(struct usb_bus *bus, int portnum)
1046 {
1047 	unsigned bit = 1 << portnum;
1048 
1049 	if (bus->resuming_ports & bit) {
1050 		bus->resuming_ports &= ~bit;
1051 		pm_runtime_put_noidle(&bus->root_hub->dev);
1052 	}
1053 }
1054 EXPORT_SYMBOL_GPL(usb_hcd_end_port_resume);
1055 
1056 /*-------------------------------------------------------------------------*/
1057 
1058 /**
1059  * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
1060  * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
1061  * @is_input: true iff the transaction sends data to the host
1062  * @isoc: true for isochronous transactions, false for interrupt ones
1063  * @bytecount: how many bytes in the transaction.
1064  *
1065  * Return: Approximate bus time in nanoseconds for a periodic transaction.
1066  *
1067  * Note:
1068  * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
1069  * scheduled in software, this function is only used for such scheduling.
1070  */
1071 long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
1072 {
1073 	unsigned long	tmp;
1074 
1075 	switch (speed) {
1076 	case USB_SPEED_LOW: 	/* INTR only */
1077 		if (is_input) {
1078 			tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
1079 			return 64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
1080 		} else {
1081 			tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
1082 			return 64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
1083 		}
1084 	case USB_SPEED_FULL:	/* ISOC or INTR */
1085 		if (isoc) {
1086 			tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
1087 			return ((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp;
1088 		} else {
1089 			tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
1090 			return 9107L + BW_HOST_DELAY + tmp;
1091 		}
1092 	case USB_SPEED_HIGH:	/* ISOC or INTR */
1093 		/* FIXME adjust for input vs output */
1094 		if (isoc)
1095 			tmp = HS_NSECS_ISO (bytecount);
1096 		else
1097 			tmp = HS_NSECS (bytecount);
1098 		return tmp;
1099 	default:
1100 		pr_debug ("%s: bogus device speed!\n", usbcore_name);
1101 		return -1;
1102 	}
1103 }
1104 EXPORT_SYMBOL_GPL(usb_calc_bus_time);
1105 
1106 
1107 /*-------------------------------------------------------------------------*/
1108 
1109 /*
1110  * Generic HC operations.
1111  */
1112 
1113 /*-------------------------------------------------------------------------*/
1114 
1115 /**
1116  * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue
1117  * @hcd: host controller to which @urb was submitted
1118  * @urb: URB being submitted
1119  *
1120  * Host controller drivers should call this routine in their enqueue()
1121  * method.  The HCD's private spinlock must be held and interrupts must
1122  * be disabled.  The actions carried out here are required for URB
1123  * submission, as well as for endpoint shutdown and for usb_kill_urb.
1124  *
1125  * Return: 0 for no error, otherwise a negative error code (in which case
1126  * the enqueue() method must fail).  If no error occurs but enqueue() fails
1127  * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing
1128  * the private spinlock and returning.
1129  */
1130 int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb)
1131 {
1132 	int		rc = 0;
1133 
1134 	spin_lock(&hcd_urb_list_lock);
1135 
1136 	/* Check that the URB isn't being killed */
1137 	if (unlikely(atomic_read(&urb->reject))) {
1138 		rc = -EPERM;
1139 		goto done;
1140 	}
1141 
1142 	if (unlikely(!urb->ep->enabled)) {
1143 		rc = -ENOENT;
1144 		goto done;
1145 	}
1146 
1147 	if (unlikely(!urb->dev->can_submit)) {
1148 		rc = -EHOSTUNREACH;
1149 		goto done;
1150 	}
1151 
1152 	/*
1153 	 * Check the host controller's state and add the URB to the
1154 	 * endpoint's queue.
1155 	 */
1156 	if (HCD_RH_RUNNING(hcd)) {
1157 		urb->unlinked = 0;
1158 		list_add_tail(&urb->urb_list, &urb->ep->urb_list);
1159 	} else {
1160 		rc = -ESHUTDOWN;
1161 		goto done;
1162 	}
1163  done:
1164 	spin_unlock(&hcd_urb_list_lock);
1165 	return rc;
1166 }
1167 EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep);
1168 
1169 /**
1170  * usb_hcd_check_unlink_urb - check whether an URB may be unlinked
1171  * @hcd: host controller to which @urb was submitted
1172  * @urb: URB being checked for unlinkability
1173  * @status: error code to store in @urb if the unlink succeeds
1174  *
1175  * Host controller drivers should call this routine in their dequeue()
1176  * method.  The HCD's private spinlock must be held and interrupts must
1177  * be disabled.  The actions carried out here are required for making
1178  * sure than an unlink is valid.
1179  *
1180  * Return: 0 for no error, otherwise a negative error code (in which case
1181  * the dequeue() method must fail).  The possible error codes are:
1182  *
1183  *	-EIDRM: @urb was not submitted or has already completed.
1184  *		The completion function may not have been called yet.
1185  *
1186  *	-EBUSY: @urb has already been unlinked.
1187  */
1188 int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
1189 		int status)
1190 {
1191 	struct list_head	*tmp;
1192 
1193 	/* insist the urb is still queued */
1194 	list_for_each(tmp, &urb->ep->urb_list) {
1195 		if (tmp == &urb->urb_list)
1196 			break;
1197 	}
1198 	if (tmp != &urb->urb_list)
1199 		return -EIDRM;
1200 
1201 	/* Any status except -EINPROGRESS means something already started to
1202 	 * unlink this URB from the hardware.  So there's no more work to do.
1203 	 */
1204 	if (urb->unlinked)
1205 		return -EBUSY;
1206 	urb->unlinked = status;
1207 	return 0;
1208 }
1209 EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb);
1210 
1211 /**
1212  * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
1213  * @hcd: host controller to which @urb was submitted
1214  * @urb: URB being unlinked
1215  *
1216  * Host controller drivers should call this routine before calling
1217  * usb_hcd_giveback_urb().  The HCD's private spinlock must be held and
1218  * interrupts must be disabled.  The actions carried out here are required
1219  * for URB completion.
1220  */
1221 void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb)
1222 {
1223 	/* clear all state linking urb to this dev (and hcd) */
1224 	spin_lock(&hcd_urb_list_lock);
1225 	list_del_init(&urb->urb_list);
1226 	spin_unlock(&hcd_urb_list_lock);
1227 }
1228 EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep);
1229 
1230 /*
1231  * Some usb host controllers can only perform dma using a small SRAM area,
1232  * or have restrictions on addressable DRAM.
1233  * The usb core itself is however optimized for host controllers that can dma
1234  * using regular system memory - like pci devices doing bus mastering.
1235  *
1236  * To support host controllers with limited dma capabilities we provide dma
1237  * bounce buffers. This feature can be enabled by initializing
1238  * hcd->localmem_pool using usb_hcd_setup_local_mem().
1239  *
1240  * The initialized hcd->localmem_pool then tells the usb code to allocate all
1241  * data for dma using the genalloc API.
1242  *
1243  * So, to summarize...
1244  *
1245  * - We need "local" memory, canonical example being
1246  *   a small SRAM on a discrete controller being the
1247  *   only memory that the controller can read ...
1248  *   (a) "normal" kernel memory is no good, and
1249  *   (b) there's not enough to share
1250  *
1251  * - So we use that, even though the primary requirement
1252  *   is that the memory be "local" (hence addressable
1253  *   by that device), not "coherent".
1254  *
1255  */
1256 
1257 static int hcd_alloc_coherent(struct usb_bus *bus,
1258 			      gfp_t mem_flags, dma_addr_t *dma_handle,
1259 			      void **vaddr_handle, size_t size,
1260 			      enum dma_data_direction dir)
1261 {
1262 	unsigned char *vaddr;
1263 
1264 	if (*vaddr_handle == NULL) {
1265 		WARN_ON_ONCE(1);
1266 		return -EFAULT;
1267 	}
1268 
1269 	vaddr = hcd_buffer_alloc(bus, size + sizeof(unsigned long),
1270 				 mem_flags, dma_handle);
1271 	if (!vaddr)
1272 		return -ENOMEM;
1273 
1274 	/*
1275 	 * Store the virtual address of the buffer at the end
1276 	 * of the allocated dma buffer. The size of the buffer
1277 	 * may be uneven so use unaligned functions instead
1278 	 * of just rounding up. It makes sense to optimize for
1279 	 * memory footprint over access speed since the amount
1280 	 * of memory available for dma may be limited.
1281 	 */
1282 	put_unaligned((unsigned long)*vaddr_handle,
1283 		      (unsigned long *)(vaddr + size));
1284 
1285 	if (dir == DMA_TO_DEVICE)
1286 		memcpy(vaddr, *vaddr_handle, size);
1287 
1288 	*vaddr_handle = vaddr;
1289 	return 0;
1290 }
1291 
1292 static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
1293 			      void **vaddr_handle, size_t size,
1294 			      enum dma_data_direction dir)
1295 {
1296 	unsigned char *vaddr = *vaddr_handle;
1297 
1298 	vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size));
1299 
1300 	if (dir == DMA_FROM_DEVICE)
1301 		memcpy(vaddr, *vaddr_handle, size);
1302 
1303 	hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle);
1304 
1305 	*vaddr_handle = vaddr;
1306 	*dma_handle = 0;
1307 }
1308 
1309 void usb_hcd_unmap_urb_setup_for_dma(struct usb_hcd *hcd, struct urb *urb)
1310 {
1311 	if (IS_ENABLED(CONFIG_HAS_DMA) &&
1312 	    (urb->transfer_flags & URB_SETUP_MAP_SINGLE))
1313 		dma_unmap_single(hcd->self.sysdev,
1314 				urb->setup_dma,
1315 				sizeof(struct usb_ctrlrequest),
1316 				DMA_TO_DEVICE);
1317 	else if (urb->transfer_flags & URB_SETUP_MAP_LOCAL)
1318 		hcd_free_coherent(urb->dev->bus,
1319 				&urb->setup_dma,
1320 				(void **) &urb->setup_packet,
1321 				sizeof(struct usb_ctrlrequest),
1322 				DMA_TO_DEVICE);
1323 
1324 	/* Make it safe to call this routine more than once */
1325 	urb->transfer_flags &= ~(URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL);
1326 }
1327 EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_setup_for_dma);
1328 
1329 static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
1330 {
1331 	if (hcd->driver->unmap_urb_for_dma)
1332 		hcd->driver->unmap_urb_for_dma(hcd, urb);
1333 	else
1334 		usb_hcd_unmap_urb_for_dma(hcd, urb);
1335 }
1336 
1337 void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
1338 {
1339 	enum dma_data_direction dir;
1340 
1341 	usb_hcd_unmap_urb_setup_for_dma(hcd, urb);
1342 
1343 	dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1344 	if (IS_ENABLED(CONFIG_HAS_DMA) &&
1345 	    (urb->transfer_flags & URB_DMA_MAP_SG))
1346 		dma_unmap_sg(hcd->self.sysdev,
1347 				urb->sg,
1348 				urb->num_sgs,
1349 				dir);
1350 	else if (IS_ENABLED(CONFIG_HAS_DMA) &&
1351 		 (urb->transfer_flags & URB_DMA_MAP_PAGE))
1352 		dma_unmap_page(hcd->self.sysdev,
1353 				urb->transfer_dma,
1354 				urb->transfer_buffer_length,
1355 				dir);
1356 	else if (IS_ENABLED(CONFIG_HAS_DMA) &&
1357 		 (urb->transfer_flags & URB_DMA_MAP_SINGLE))
1358 		dma_unmap_single(hcd->self.sysdev,
1359 				urb->transfer_dma,
1360 				urb->transfer_buffer_length,
1361 				dir);
1362 	else if (urb->transfer_flags & URB_MAP_LOCAL)
1363 		hcd_free_coherent(urb->dev->bus,
1364 				&urb->transfer_dma,
1365 				&urb->transfer_buffer,
1366 				urb->transfer_buffer_length,
1367 				dir);
1368 
1369 	/* Make it safe to call this routine more than once */
1370 	urb->transfer_flags &= ~(URB_DMA_MAP_SG | URB_DMA_MAP_PAGE |
1371 			URB_DMA_MAP_SINGLE | URB_MAP_LOCAL);
1372 }
1373 EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_for_dma);
1374 
1375 static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
1376 			   gfp_t mem_flags)
1377 {
1378 	if (hcd->driver->map_urb_for_dma)
1379 		return hcd->driver->map_urb_for_dma(hcd, urb, mem_flags);
1380 	else
1381 		return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
1382 }
1383 
1384 int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
1385 			    gfp_t mem_flags)
1386 {
1387 	enum dma_data_direction dir;
1388 	int ret = 0;
1389 
1390 	/* Map the URB's buffers for DMA access.
1391 	 * Lower level HCD code should use *_dma exclusively,
1392 	 * unless it uses pio or talks to another transport,
1393 	 * or uses the provided scatter gather list for bulk.
1394 	 */
1395 
1396 	if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1397 		if (hcd->self.uses_pio_for_control)
1398 			return ret;
1399 		if (hcd->localmem_pool) {
1400 			ret = hcd_alloc_coherent(
1401 					urb->dev->bus, mem_flags,
1402 					&urb->setup_dma,
1403 					(void **)&urb->setup_packet,
1404 					sizeof(struct usb_ctrlrequest),
1405 					DMA_TO_DEVICE);
1406 			if (ret)
1407 				return ret;
1408 			urb->transfer_flags |= URB_SETUP_MAP_LOCAL;
1409 		} else if (hcd_uses_dma(hcd)) {
1410 			if (object_is_on_stack(urb->setup_packet)) {
1411 				WARN_ONCE(1, "setup packet is on stack\n");
1412 				return -EAGAIN;
1413 			}
1414 
1415 			urb->setup_dma = dma_map_single(
1416 					hcd->self.sysdev,
1417 					urb->setup_packet,
1418 					sizeof(struct usb_ctrlrequest),
1419 					DMA_TO_DEVICE);
1420 			if (dma_mapping_error(hcd->self.sysdev,
1421 						urb->setup_dma))
1422 				return -EAGAIN;
1423 			urb->transfer_flags |= URB_SETUP_MAP_SINGLE;
1424 		}
1425 	}
1426 
1427 	dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1428 	if (urb->transfer_buffer_length != 0
1429 	    && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
1430 		if (hcd->localmem_pool) {
1431 			ret = hcd_alloc_coherent(
1432 					urb->dev->bus, mem_flags,
1433 					&urb->transfer_dma,
1434 					&urb->transfer_buffer,
1435 					urb->transfer_buffer_length,
1436 					dir);
1437 			if (ret == 0)
1438 				urb->transfer_flags |= URB_MAP_LOCAL;
1439 		} else if (hcd_uses_dma(hcd)) {
1440 			if (urb->num_sgs) {
1441 				int n;
1442 
1443 				/* We don't support sg for isoc transfers ! */
1444 				if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1445 					WARN_ON(1);
1446 					return -EINVAL;
1447 				}
1448 
1449 				n = dma_map_sg(
1450 						hcd->self.sysdev,
1451 						urb->sg,
1452 						urb->num_sgs,
1453 						dir);
1454 				if (!n)
1455 					ret = -EAGAIN;
1456 				else
1457 					urb->transfer_flags |= URB_DMA_MAP_SG;
1458 				urb->num_mapped_sgs = n;
1459 				if (n != urb->num_sgs)
1460 					urb->transfer_flags |=
1461 							URB_DMA_SG_COMBINED;
1462 			} else if (urb->sg) {
1463 				struct scatterlist *sg = urb->sg;
1464 				urb->transfer_dma = dma_map_page(
1465 						hcd->self.sysdev,
1466 						sg_page(sg),
1467 						sg->offset,
1468 						urb->transfer_buffer_length,
1469 						dir);
1470 				if (dma_mapping_error(hcd->self.sysdev,
1471 						urb->transfer_dma))
1472 					ret = -EAGAIN;
1473 				else
1474 					urb->transfer_flags |= URB_DMA_MAP_PAGE;
1475 			} else if (object_is_on_stack(urb->transfer_buffer)) {
1476 				WARN_ONCE(1, "transfer buffer is on stack\n");
1477 				ret = -EAGAIN;
1478 			} else {
1479 				urb->transfer_dma = dma_map_single(
1480 						hcd->self.sysdev,
1481 						urb->transfer_buffer,
1482 						urb->transfer_buffer_length,
1483 						dir);
1484 				if (dma_mapping_error(hcd->self.sysdev,
1485 						urb->transfer_dma))
1486 					ret = -EAGAIN;
1487 				else
1488 					urb->transfer_flags |= URB_DMA_MAP_SINGLE;
1489 			}
1490 		}
1491 		if (ret && (urb->transfer_flags & (URB_SETUP_MAP_SINGLE |
1492 				URB_SETUP_MAP_LOCAL)))
1493 			usb_hcd_unmap_urb_for_dma(hcd, urb);
1494 	}
1495 	return ret;
1496 }
1497 EXPORT_SYMBOL_GPL(usb_hcd_map_urb_for_dma);
1498 
1499 /*-------------------------------------------------------------------------*/
1500 
1501 /* may be called in any context with a valid urb->dev usecount
1502  * caller surrenders "ownership" of urb
1503  * expects usb_submit_urb() to have sanity checked and conditioned all
1504  * inputs in the urb
1505  */
1506 int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
1507 {
1508 	int			status;
1509 	struct usb_hcd		*hcd = bus_to_hcd(urb->dev->bus);
1510 
1511 	/* increment urb's reference count as part of giving it to the HCD
1512 	 * (which will control it).  HCD guarantees that it either returns
1513 	 * an error or calls giveback(), but not both.
1514 	 */
1515 	usb_get_urb(urb);
1516 	atomic_inc(&urb->use_count);
1517 	atomic_inc(&urb->dev->urbnum);
1518 	usbmon_urb_submit(&hcd->self, urb);
1519 
1520 	/* NOTE requirements on root-hub callers (usbfs and the hub
1521 	 * driver, for now):  URBs' urb->transfer_buffer must be
1522 	 * valid and usb_buffer_{sync,unmap}() not be needed, since
1523 	 * they could clobber root hub response data.  Also, control
1524 	 * URBs must be submitted in process context with interrupts
1525 	 * enabled.
1526 	 */
1527 
1528 	if (is_root_hub(urb->dev)) {
1529 		status = rh_urb_enqueue(hcd, urb);
1530 	} else {
1531 		status = map_urb_for_dma(hcd, urb, mem_flags);
1532 		if (likely(status == 0)) {
1533 			status = hcd->driver->urb_enqueue(hcd, urb, mem_flags);
1534 			if (unlikely(status))
1535 				unmap_urb_for_dma(hcd, urb);
1536 		}
1537 	}
1538 
1539 	if (unlikely(status)) {
1540 		usbmon_urb_submit_error(&hcd->self, urb, status);
1541 		urb->hcpriv = NULL;
1542 		INIT_LIST_HEAD(&urb->urb_list);
1543 		atomic_dec(&urb->use_count);
1544 		/*
1545 		 * Order the write of urb->use_count above before the read
1546 		 * of urb->reject below.  Pairs with the memory barriers in
1547 		 * usb_kill_urb() and usb_poison_urb().
1548 		 */
1549 		smp_mb__after_atomic();
1550 
1551 		atomic_dec(&urb->dev->urbnum);
1552 		if (atomic_read(&urb->reject))
1553 			wake_up(&usb_kill_urb_queue);
1554 		usb_put_urb(urb);
1555 	}
1556 	return status;
1557 }
1558 
1559 /*-------------------------------------------------------------------------*/
1560 
1561 /* this makes the hcd giveback() the urb more quickly, by kicking it
1562  * off hardware queues (which may take a while) and returning it as
1563  * soon as practical.  we've already set up the urb's return status,
1564  * but we can't know if the callback completed already.
1565  */
1566 static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status)
1567 {
1568 	int		value;
1569 
1570 	if (is_root_hub(urb->dev))
1571 		value = usb_rh_urb_dequeue(hcd, urb, status);
1572 	else {
1573 
1574 		/* The only reason an HCD might fail this call is if
1575 		 * it has not yet fully queued the urb to begin with.
1576 		 * Such failures should be harmless. */
1577 		value = hcd->driver->urb_dequeue(hcd, urb, status);
1578 	}
1579 	return value;
1580 }
1581 
1582 /*
1583  * called in any context
1584  *
1585  * caller guarantees urb won't be recycled till both unlink()
1586  * and the urb's completion function return
1587  */
1588 int usb_hcd_unlink_urb (struct urb *urb, int status)
1589 {
1590 	struct usb_hcd		*hcd;
1591 	struct usb_device	*udev = urb->dev;
1592 	int			retval = -EIDRM;
1593 	unsigned long		flags;
1594 
1595 	/* Prevent the device and bus from going away while
1596 	 * the unlink is carried out.  If they are already gone
1597 	 * then urb->use_count must be 0, since disconnected
1598 	 * devices can't have any active URBs.
1599 	 */
1600 	spin_lock_irqsave(&hcd_urb_unlink_lock, flags);
1601 	if (atomic_read(&urb->use_count) > 0) {
1602 		retval = 0;
1603 		usb_get_dev(udev);
1604 	}
1605 	spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags);
1606 	if (retval == 0) {
1607 		hcd = bus_to_hcd(urb->dev->bus);
1608 		retval = unlink1(hcd, urb, status);
1609 		if (retval == 0)
1610 			retval = -EINPROGRESS;
1611 		else if (retval != -EIDRM && retval != -EBUSY)
1612 			dev_dbg(&udev->dev, "hcd_unlink_urb %pK fail %d\n",
1613 					urb, retval);
1614 		usb_put_dev(udev);
1615 	}
1616 	return retval;
1617 }
1618 
1619 /*-------------------------------------------------------------------------*/
1620 
1621 static void __usb_hcd_giveback_urb(struct urb *urb)
1622 {
1623 	struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
1624 	struct usb_anchor *anchor = urb->anchor;
1625 	int status = urb->unlinked;
1626 
1627 	urb->hcpriv = NULL;
1628 	if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
1629 	    urb->actual_length < urb->transfer_buffer_length &&
1630 	    !status))
1631 		status = -EREMOTEIO;
1632 
1633 	unmap_urb_for_dma(hcd, urb);
1634 	usbmon_urb_complete(&hcd->self, urb, status);
1635 	usb_anchor_suspend_wakeups(anchor);
1636 	usb_unanchor_urb(urb);
1637 	if (likely(status == 0))
1638 		usb_led_activity(USB_LED_EVENT_HOST);
1639 
1640 	/* pass ownership to the completion handler */
1641 	urb->status = status;
1642 	/*
1643 	 * This function can be called in task context inside another remote
1644 	 * coverage collection section, but kcov doesn't support that kind of
1645 	 * recursion yet. Only collect coverage in softirq context for now.
1646 	 */
1647 	kcov_remote_start_usb_softirq((u64)urb->dev->bus->busnum);
1648 	urb->complete(urb);
1649 	kcov_remote_stop_softirq();
1650 
1651 	usb_anchor_resume_wakeups(anchor);
1652 	atomic_dec(&urb->use_count);
1653 	/*
1654 	 * Order the write of urb->use_count above before the read
1655 	 * of urb->reject below.  Pairs with the memory barriers in
1656 	 * usb_kill_urb() and usb_poison_urb().
1657 	 */
1658 	smp_mb__after_atomic();
1659 
1660 	if (unlikely(atomic_read(&urb->reject)))
1661 		wake_up(&usb_kill_urb_queue);
1662 	usb_put_urb(urb);
1663 }
1664 
1665 static void usb_giveback_urb_bh(struct tasklet_struct *t)
1666 {
1667 	struct giveback_urb_bh *bh = from_tasklet(bh, t, bh);
1668 	struct list_head local_list;
1669 
1670 	spin_lock_irq(&bh->lock);
1671 	bh->running = true;
1672 	list_replace_init(&bh->head, &local_list);
1673 	spin_unlock_irq(&bh->lock);
1674 
1675 	while (!list_empty(&local_list)) {
1676 		struct urb *urb;
1677 
1678 		urb = list_entry(local_list.next, struct urb, urb_list);
1679 		list_del_init(&urb->urb_list);
1680 		bh->completing_ep = urb->ep;
1681 		__usb_hcd_giveback_urb(urb);
1682 		bh->completing_ep = NULL;
1683 	}
1684 
1685 	/*
1686 	 * giveback new URBs next time to prevent this function
1687 	 * from not exiting for a long time.
1688 	 */
1689 	spin_lock_irq(&bh->lock);
1690 	if (!list_empty(&bh->head)) {
1691 		if (bh->high_prio)
1692 			tasklet_hi_schedule(&bh->bh);
1693 		else
1694 			tasklet_schedule(&bh->bh);
1695 	}
1696 	bh->running = false;
1697 	spin_unlock_irq(&bh->lock);
1698 }
1699 
1700 /**
1701  * usb_hcd_giveback_urb - return URB from HCD to device driver
1702  * @hcd: host controller returning the URB
1703  * @urb: urb being returned to the USB device driver.
1704  * @status: completion status code for the URB.
1705  *
1706  * Context: atomic. The completion callback is invoked in caller's context.
1707  * For HCDs with HCD_BH flag set, the completion callback is invoked in tasklet
1708  * context (except for URBs submitted to the root hub which always complete in
1709  * caller's context).
1710  *
1711  * This hands the URB from HCD to its USB device driver, using its
1712  * completion function.  The HCD has freed all per-urb resources
1713  * (and is done using urb->hcpriv).  It also released all HCD locks;
1714  * the device driver won't cause problems if it frees, modifies,
1715  * or resubmits this URB.
1716  *
1717  * If @urb was unlinked, the value of @status will be overridden by
1718  * @urb->unlinked.  Erroneous short transfers are detected in case
1719  * the HCD hasn't checked for them.
1720  */
1721 void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status)
1722 {
1723 	struct giveback_urb_bh *bh;
1724 	bool running;
1725 
1726 	/* pass status to tasklet via unlinked */
1727 	if (likely(!urb->unlinked))
1728 		urb->unlinked = status;
1729 
1730 	if (!hcd_giveback_urb_in_bh(hcd) && !is_root_hub(urb->dev)) {
1731 		__usb_hcd_giveback_urb(urb);
1732 		return;
1733 	}
1734 
1735 	if (usb_pipeisoc(urb->pipe) || usb_pipeint(urb->pipe))
1736 		bh = &hcd->high_prio_bh;
1737 	else
1738 		bh = &hcd->low_prio_bh;
1739 
1740 	spin_lock(&bh->lock);
1741 	list_add_tail(&urb->urb_list, &bh->head);
1742 	running = bh->running;
1743 	spin_unlock(&bh->lock);
1744 
1745 	if (running)
1746 		;
1747 	else if (bh->high_prio)
1748 		tasklet_hi_schedule(&bh->bh);
1749 	else
1750 		tasklet_schedule(&bh->bh);
1751 }
1752 EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb);
1753 
1754 /*-------------------------------------------------------------------------*/
1755 
1756 /* Cancel all URBs pending on this endpoint and wait for the endpoint's
1757  * queue to drain completely.  The caller must first insure that no more
1758  * URBs can be submitted for this endpoint.
1759  */
1760 void usb_hcd_flush_endpoint(struct usb_device *udev,
1761 		struct usb_host_endpoint *ep)
1762 {
1763 	struct usb_hcd		*hcd;
1764 	struct urb		*urb;
1765 
1766 	if (!ep)
1767 		return;
1768 	might_sleep();
1769 	hcd = bus_to_hcd(udev->bus);
1770 
1771 	/* No more submits can occur */
1772 	spin_lock_irq(&hcd_urb_list_lock);
1773 rescan:
1774 	list_for_each_entry_reverse(urb, &ep->urb_list, urb_list) {
1775 		int	is_in;
1776 
1777 		if (urb->unlinked)
1778 			continue;
1779 		usb_get_urb (urb);
1780 		is_in = usb_urb_dir_in(urb);
1781 		spin_unlock(&hcd_urb_list_lock);
1782 
1783 		/* kick hcd */
1784 		unlink1(hcd, urb, -ESHUTDOWN);
1785 		dev_dbg (hcd->self.controller,
1786 			"shutdown urb %pK ep%d%s-%s\n",
1787 			urb, usb_endpoint_num(&ep->desc),
1788 			is_in ? "in" : "out",
1789 			usb_ep_type_string(usb_endpoint_type(&ep->desc)));
1790 		usb_put_urb (urb);
1791 
1792 		/* list contents may have changed */
1793 		spin_lock(&hcd_urb_list_lock);
1794 		goto rescan;
1795 	}
1796 	spin_unlock_irq(&hcd_urb_list_lock);
1797 
1798 	/* Wait until the endpoint queue is completely empty */
1799 	while (!list_empty (&ep->urb_list)) {
1800 		spin_lock_irq(&hcd_urb_list_lock);
1801 
1802 		/* The list may have changed while we acquired the spinlock */
1803 		urb = NULL;
1804 		if (!list_empty (&ep->urb_list)) {
1805 			urb = list_entry (ep->urb_list.prev, struct urb,
1806 					urb_list);
1807 			usb_get_urb (urb);
1808 		}
1809 		spin_unlock_irq(&hcd_urb_list_lock);
1810 
1811 		if (urb) {
1812 			usb_kill_urb (urb);
1813 			usb_put_urb (urb);
1814 		}
1815 	}
1816 }
1817 
1818 /**
1819  * usb_hcd_alloc_bandwidth - check whether a new bandwidth setting exceeds
1820  *				the bus bandwidth
1821  * @udev: target &usb_device
1822  * @new_config: new configuration to install
1823  * @cur_alt: the current alternate interface setting
1824  * @new_alt: alternate interface setting that is being installed
1825  *
1826  * To change configurations, pass in the new configuration in new_config,
1827  * and pass NULL for cur_alt and new_alt.
1828  *
1829  * To reset a device's configuration (put the device in the ADDRESSED state),
1830  * pass in NULL for new_config, cur_alt, and new_alt.
1831  *
1832  * To change alternate interface settings, pass in NULL for new_config,
1833  * pass in the current alternate interface setting in cur_alt,
1834  * and pass in the new alternate interface setting in new_alt.
1835  *
1836  * Return: An error if the requested bandwidth change exceeds the
1837  * bus bandwidth or host controller internal resources.
1838  */
1839 int usb_hcd_alloc_bandwidth(struct usb_device *udev,
1840 		struct usb_host_config *new_config,
1841 		struct usb_host_interface *cur_alt,
1842 		struct usb_host_interface *new_alt)
1843 {
1844 	int num_intfs, i, j;
1845 	struct usb_host_interface *alt = NULL;
1846 	int ret = 0;
1847 	struct usb_hcd *hcd;
1848 	struct usb_host_endpoint *ep;
1849 
1850 	hcd = bus_to_hcd(udev->bus);
1851 	if (!hcd->driver->check_bandwidth)
1852 		return 0;
1853 
1854 	/* Configuration is being removed - set configuration 0 */
1855 	if (!new_config && !cur_alt) {
1856 		for (i = 1; i < 16; ++i) {
1857 			ep = udev->ep_out[i];
1858 			if (ep)
1859 				hcd->driver->drop_endpoint(hcd, udev, ep);
1860 			ep = udev->ep_in[i];
1861 			if (ep)
1862 				hcd->driver->drop_endpoint(hcd, udev, ep);
1863 		}
1864 		hcd->driver->check_bandwidth(hcd, udev);
1865 		return 0;
1866 	}
1867 	/* Check if the HCD says there's enough bandwidth.  Enable all endpoints
1868 	 * each interface's alt setting 0 and ask the HCD to check the bandwidth
1869 	 * of the bus.  There will always be bandwidth for endpoint 0, so it's
1870 	 * ok to exclude it.
1871 	 */
1872 	if (new_config) {
1873 		num_intfs = new_config->desc.bNumInterfaces;
1874 		/* Remove endpoints (except endpoint 0, which is always on the
1875 		 * schedule) from the old config from the schedule
1876 		 */
1877 		for (i = 1; i < 16; ++i) {
1878 			ep = udev->ep_out[i];
1879 			if (ep) {
1880 				ret = hcd->driver->drop_endpoint(hcd, udev, ep);
1881 				if (ret < 0)
1882 					goto reset;
1883 			}
1884 			ep = udev->ep_in[i];
1885 			if (ep) {
1886 				ret = hcd->driver->drop_endpoint(hcd, udev, ep);
1887 				if (ret < 0)
1888 					goto reset;
1889 			}
1890 		}
1891 		for (i = 0; i < num_intfs; ++i) {
1892 			struct usb_host_interface *first_alt;
1893 			int iface_num;
1894 
1895 			first_alt = &new_config->intf_cache[i]->altsetting[0];
1896 			iface_num = first_alt->desc.bInterfaceNumber;
1897 			/* Set up endpoints for alternate interface setting 0 */
1898 			alt = usb_find_alt_setting(new_config, iface_num, 0);
1899 			if (!alt)
1900 				/* No alt setting 0? Pick the first setting. */
1901 				alt = first_alt;
1902 
1903 			for (j = 0; j < alt->desc.bNumEndpoints; j++) {
1904 				ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]);
1905 				if (ret < 0)
1906 					goto reset;
1907 			}
1908 		}
1909 	}
1910 	if (cur_alt && new_alt) {
1911 		struct usb_interface *iface = usb_ifnum_to_if(udev,
1912 				cur_alt->desc.bInterfaceNumber);
1913 
1914 		if (!iface)
1915 			return -EINVAL;
1916 		if (iface->resetting_device) {
1917 			/*
1918 			 * The USB core just reset the device, so the xHCI host
1919 			 * and the device will think alt setting 0 is installed.
1920 			 * However, the USB core will pass in the alternate
1921 			 * setting installed before the reset as cur_alt.  Dig
1922 			 * out the alternate setting 0 structure, or the first
1923 			 * alternate setting if a broken device doesn't have alt
1924 			 * setting 0.
1925 			 */
1926 			cur_alt = usb_altnum_to_altsetting(iface, 0);
1927 			if (!cur_alt)
1928 				cur_alt = &iface->altsetting[0];
1929 		}
1930 
1931 		/* Drop all the endpoints in the current alt setting */
1932 		for (i = 0; i < cur_alt->desc.bNumEndpoints; i++) {
1933 			ret = hcd->driver->drop_endpoint(hcd, udev,
1934 					&cur_alt->endpoint[i]);
1935 			if (ret < 0)
1936 				goto reset;
1937 		}
1938 		/* Add all the endpoints in the new alt setting */
1939 		for (i = 0; i < new_alt->desc.bNumEndpoints; i++) {
1940 			ret = hcd->driver->add_endpoint(hcd, udev,
1941 					&new_alt->endpoint[i]);
1942 			if (ret < 0)
1943 				goto reset;
1944 		}
1945 	}
1946 	ret = hcd->driver->check_bandwidth(hcd, udev);
1947 reset:
1948 	if (ret < 0)
1949 		hcd->driver->reset_bandwidth(hcd, udev);
1950 	return ret;
1951 }
1952 
1953 /* Disables the endpoint: synchronizes with the hcd to make sure all
1954  * endpoint state is gone from hardware.  usb_hcd_flush_endpoint() must
1955  * have been called previously.  Use for set_configuration, set_interface,
1956  * driver removal, physical disconnect.
1957  *
1958  * example:  a qh stored in ep->hcpriv, holding state related to endpoint
1959  * type, maxpacket size, toggle, halt status, and scheduling.
1960  */
1961 void usb_hcd_disable_endpoint(struct usb_device *udev,
1962 		struct usb_host_endpoint *ep)
1963 {
1964 	struct usb_hcd		*hcd;
1965 
1966 	might_sleep();
1967 	hcd = bus_to_hcd(udev->bus);
1968 	if (hcd->driver->endpoint_disable)
1969 		hcd->driver->endpoint_disable(hcd, ep);
1970 }
1971 
1972 /**
1973  * usb_hcd_reset_endpoint - reset host endpoint state
1974  * @udev: USB device.
1975  * @ep:   the endpoint to reset.
1976  *
1977  * Resets any host endpoint state such as the toggle bit, sequence
1978  * number and current window.
1979  */
1980 void usb_hcd_reset_endpoint(struct usb_device *udev,
1981 			    struct usb_host_endpoint *ep)
1982 {
1983 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1984 
1985 	if (hcd->driver->endpoint_reset)
1986 		hcd->driver->endpoint_reset(hcd, ep);
1987 	else {
1988 		int epnum = usb_endpoint_num(&ep->desc);
1989 		int is_out = usb_endpoint_dir_out(&ep->desc);
1990 		int is_control = usb_endpoint_xfer_control(&ep->desc);
1991 
1992 		usb_settoggle(udev, epnum, is_out, 0);
1993 		if (is_control)
1994 			usb_settoggle(udev, epnum, !is_out, 0);
1995 	}
1996 }
1997 
1998 /**
1999  * usb_alloc_streams - allocate bulk endpoint stream IDs.
2000  * @interface:		alternate setting that includes all endpoints.
2001  * @eps:		array of endpoints that need streams.
2002  * @num_eps:		number of endpoints in the array.
2003  * @num_streams:	number of streams to allocate.
2004  * @mem_flags:		flags hcd should use to allocate memory.
2005  *
2006  * Sets up a group of bulk endpoints to have @num_streams stream IDs available.
2007  * Drivers may queue multiple transfers to different stream IDs, which may
2008  * complete in a different order than they were queued.
2009  *
2010  * Return: On success, the number of allocated streams. On failure, a negative
2011  * error code.
2012  */
2013 int usb_alloc_streams(struct usb_interface *interface,
2014 		struct usb_host_endpoint **eps, unsigned int num_eps,
2015 		unsigned int num_streams, gfp_t mem_flags)
2016 {
2017 	struct usb_hcd *hcd;
2018 	struct usb_device *dev;
2019 	int i, ret;
2020 
2021 	dev = interface_to_usbdev(interface);
2022 	hcd = bus_to_hcd(dev->bus);
2023 	if (!hcd->driver->alloc_streams || !hcd->driver->free_streams)
2024 		return -EINVAL;
2025 	if (dev->speed < USB_SPEED_SUPER)
2026 		return -EINVAL;
2027 	if (dev->state < USB_STATE_CONFIGURED)
2028 		return -ENODEV;
2029 
2030 	for (i = 0; i < num_eps; i++) {
2031 		/* Streams only apply to bulk endpoints. */
2032 		if (!usb_endpoint_xfer_bulk(&eps[i]->desc))
2033 			return -EINVAL;
2034 		/* Re-alloc is not allowed */
2035 		if (eps[i]->streams)
2036 			return -EINVAL;
2037 	}
2038 
2039 	ret = hcd->driver->alloc_streams(hcd, dev, eps, num_eps,
2040 			num_streams, mem_flags);
2041 	if (ret < 0)
2042 		return ret;
2043 
2044 	for (i = 0; i < num_eps; i++)
2045 		eps[i]->streams = ret;
2046 
2047 	return ret;
2048 }
2049 EXPORT_SYMBOL_GPL(usb_alloc_streams);
2050 
2051 /**
2052  * usb_free_streams - free bulk endpoint stream IDs.
2053  * @interface:	alternate setting that includes all endpoints.
2054  * @eps:	array of endpoints to remove streams from.
2055  * @num_eps:	number of endpoints in the array.
2056  * @mem_flags:	flags hcd should use to allocate memory.
2057  *
2058  * Reverts a group of bulk endpoints back to not using stream IDs.
2059  * Can fail if we are given bad arguments, or HCD is broken.
2060  *
2061  * Return: 0 on success. On failure, a negative error code.
2062  */
2063 int usb_free_streams(struct usb_interface *interface,
2064 		struct usb_host_endpoint **eps, unsigned int num_eps,
2065 		gfp_t mem_flags)
2066 {
2067 	struct usb_hcd *hcd;
2068 	struct usb_device *dev;
2069 	int i, ret;
2070 
2071 	dev = interface_to_usbdev(interface);
2072 	hcd = bus_to_hcd(dev->bus);
2073 	if (dev->speed < USB_SPEED_SUPER)
2074 		return -EINVAL;
2075 
2076 	/* Double-free is not allowed */
2077 	for (i = 0; i < num_eps; i++)
2078 		if (!eps[i] || !eps[i]->streams)
2079 			return -EINVAL;
2080 
2081 	ret = hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags);
2082 	if (ret < 0)
2083 		return ret;
2084 
2085 	for (i = 0; i < num_eps; i++)
2086 		eps[i]->streams = 0;
2087 
2088 	return ret;
2089 }
2090 EXPORT_SYMBOL_GPL(usb_free_streams);
2091 
2092 /* Protect against drivers that try to unlink URBs after the device
2093  * is gone, by waiting until all unlinks for @udev are finished.
2094  * Since we don't currently track URBs by device, simply wait until
2095  * nothing is running in the locked region of usb_hcd_unlink_urb().
2096  */
2097 void usb_hcd_synchronize_unlinks(struct usb_device *udev)
2098 {
2099 	spin_lock_irq(&hcd_urb_unlink_lock);
2100 	spin_unlock_irq(&hcd_urb_unlink_lock);
2101 }
2102 
2103 /*-------------------------------------------------------------------------*/
2104 
2105 /* called in any context */
2106 int usb_hcd_get_frame_number (struct usb_device *udev)
2107 {
2108 	struct usb_hcd	*hcd = bus_to_hcd(udev->bus);
2109 
2110 	if (!HCD_RH_RUNNING(hcd))
2111 		return -ESHUTDOWN;
2112 	return hcd->driver->get_frame_number (hcd);
2113 }
2114 
2115 /*-------------------------------------------------------------------------*/
2116 #ifdef CONFIG_USB_HCD_TEST_MODE
2117 
2118 static void usb_ehset_completion(struct urb *urb)
2119 {
2120 	struct completion  *done = urb->context;
2121 
2122 	complete(done);
2123 }
2124 /*
2125  * Allocate and initialize a control URB. This request will be used by the
2126  * EHSET SINGLE_STEP_SET_FEATURE test in which the DATA and STATUS stages
2127  * of the GetDescriptor request are sent 15 seconds after the SETUP stage.
2128  * Return NULL if failed.
2129  */
2130 static struct urb *request_single_step_set_feature_urb(
2131 	struct usb_device	*udev,
2132 	void			*dr,
2133 	void			*buf,
2134 	struct completion	*done)
2135 {
2136 	struct urb *urb;
2137 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2138 
2139 	urb = usb_alloc_urb(0, GFP_KERNEL);
2140 	if (!urb)
2141 		return NULL;
2142 
2143 	urb->pipe = usb_rcvctrlpipe(udev, 0);
2144 
2145 	urb->ep = &udev->ep0;
2146 	urb->dev = udev;
2147 	urb->setup_packet = (void *)dr;
2148 	urb->transfer_buffer = buf;
2149 	urb->transfer_buffer_length = USB_DT_DEVICE_SIZE;
2150 	urb->complete = usb_ehset_completion;
2151 	urb->status = -EINPROGRESS;
2152 	urb->actual_length = 0;
2153 	urb->transfer_flags = URB_DIR_IN;
2154 	usb_get_urb(urb);
2155 	atomic_inc(&urb->use_count);
2156 	atomic_inc(&urb->dev->urbnum);
2157 	if (map_urb_for_dma(hcd, urb, GFP_KERNEL)) {
2158 		usb_put_urb(urb);
2159 		usb_free_urb(urb);
2160 		return NULL;
2161 	}
2162 
2163 	urb->context = done;
2164 	return urb;
2165 }
2166 
2167 int ehset_single_step_set_feature(struct usb_hcd *hcd, int port)
2168 {
2169 	int retval = -ENOMEM;
2170 	struct usb_ctrlrequest *dr;
2171 	struct urb *urb;
2172 	struct usb_device *udev;
2173 	struct usb_device_descriptor *buf;
2174 	DECLARE_COMPLETION_ONSTACK(done);
2175 
2176 	/* Obtain udev of the rhub's child port */
2177 	udev = usb_hub_find_child(hcd->self.root_hub, port);
2178 	if (!udev) {
2179 		dev_err(hcd->self.controller, "No device attached to the RootHub\n");
2180 		return -ENODEV;
2181 	}
2182 	buf = kmalloc(USB_DT_DEVICE_SIZE, GFP_KERNEL);
2183 	if (!buf)
2184 		return -ENOMEM;
2185 
2186 	dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
2187 	if (!dr) {
2188 		kfree(buf);
2189 		return -ENOMEM;
2190 	}
2191 
2192 	/* Fill Setup packet for GetDescriptor */
2193 	dr->bRequestType = USB_DIR_IN;
2194 	dr->bRequest = USB_REQ_GET_DESCRIPTOR;
2195 	dr->wValue = cpu_to_le16(USB_DT_DEVICE << 8);
2196 	dr->wIndex = 0;
2197 	dr->wLength = cpu_to_le16(USB_DT_DEVICE_SIZE);
2198 	urb = request_single_step_set_feature_urb(udev, dr, buf, &done);
2199 	if (!urb)
2200 		goto cleanup;
2201 
2202 	/* Submit just the SETUP stage */
2203 	retval = hcd->driver->submit_single_step_set_feature(hcd, urb, 1);
2204 	if (retval)
2205 		goto out1;
2206 	if (!wait_for_completion_timeout(&done, msecs_to_jiffies(2000))) {
2207 		usb_kill_urb(urb);
2208 		retval = -ETIMEDOUT;
2209 		dev_err(hcd->self.controller,
2210 			"%s SETUP stage timed out on ep0\n", __func__);
2211 		goto out1;
2212 	}
2213 	msleep(15 * 1000);
2214 
2215 	/* Complete remaining DATA and STATUS stages using the same URB */
2216 	urb->status = -EINPROGRESS;
2217 	usb_get_urb(urb);
2218 	atomic_inc(&urb->use_count);
2219 	atomic_inc(&urb->dev->urbnum);
2220 	retval = hcd->driver->submit_single_step_set_feature(hcd, urb, 0);
2221 	if (!retval && !wait_for_completion_timeout(&done,
2222 						msecs_to_jiffies(2000))) {
2223 		usb_kill_urb(urb);
2224 		retval = -ETIMEDOUT;
2225 		dev_err(hcd->self.controller,
2226 			"%s IN stage timed out on ep0\n", __func__);
2227 	}
2228 out1:
2229 	usb_free_urb(urb);
2230 cleanup:
2231 	kfree(dr);
2232 	kfree(buf);
2233 	return retval;
2234 }
2235 EXPORT_SYMBOL_GPL(ehset_single_step_set_feature);
2236 #endif /* CONFIG_USB_HCD_TEST_MODE */
2237 
2238 /*-------------------------------------------------------------------------*/
2239 
2240 #ifdef	CONFIG_PM
2241 
2242 int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
2243 {
2244 	struct usb_hcd	*hcd = bus_to_hcd(rhdev->bus);
2245 	int		status;
2246 	int		old_state = hcd->state;
2247 
2248 	dev_dbg(&rhdev->dev, "bus %ssuspend, wakeup %d\n",
2249 			(PMSG_IS_AUTO(msg) ? "auto-" : ""),
2250 			rhdev->do_remote_wakeup);
2251 	if (HCD_DEAD(hcd)) {
2252 		dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "suspend");
2253 		return 0;
2254 	}
2255 
2256 	if (!hcd->driver->bus_suspend) {
2257 		status = -ENOENT;
2258 	} else {
2259 		clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2260 		hcd->state = HC_STATE_QUIESCING;
2261 		status = hcd->driver->bus_suspend(hcd);
2262 	}
2263 	if (status == 0) {
2264 		usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
2265 		hcd->state = HC_STATE_SUSPENDED;
2266 
2267 		if (!PMSG_IS_AUTO(msg))
2268 			usb_phy_roothub_suspend(hcd->self.sysdev,
2269 						hcd->phy_roothub);
2270 
2271 		/* Did we race with a root-hub wakeup event? */
2272 		if (rhdev->do_remote_wakeup) {
2273 			char	buffer[6];
2274 
2275 			status = hcd->driver->hub_status_data(hcd, buffer);
2276 			if (status != 0) {
2277 				dev_dbg(&rhdev->dev, "suspend raced with wakeup event\n");
2278 				hcd_bus_resume(rhdev, PMSG_AUTO_RESUME);
2279 				status = -EBUSY;
2280 			}
2281 		}
2282 	} else {
2283 		spin_lock_irq(&hcd_root_hub_lock);
2284 		if (!HCD_DEAD(hcd)) {
2285 			set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2286 			hcd->state = old_state;
2287 		}
2288 		spin_unlock_irq(&hcd_root_hub_lock);
2289 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
2290 				"suspend", status);
2291 	}
2292 	return status;
2293 }
2294 
2295 int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
2296 {
2297 	struct usb_hcd	*hcd = bus_to_hcd(rhdev->bus);
2298 	int		status;
2299 	int		old_state = hcd->state;
2300 
2301 	dev_dbg(&rhdev->dev, "usb %sresume\n",
2302 			(PMSG_IS_AUTO(msg) ? "auto-" : ""));
2303 	if (HCD_DEAD(hcd)) {
2304 		dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume");
2305 		return 0;
2306 	}
2307 
2308 	if (!PMSG_IS_AUTO(msg)) {
2309 		status = usb_phy_roothub_resume(hcd->self.sysdev,
2310 						hcd->phy_roothub);
2311 		if (status)
2312 			return status;
2313 	}
2314 
2315 	if (!hcd->driver->bus_resume)
2316 		return -ENOENT;
2317 	if (HCD_RH_RUNNING(hcd))
2318 		return 0;
2319 
2320 	hcd->state = HC_STATE_RESUMING;
2321 	status = hcd->driver->bus_resume(hcd);
2322 	clear_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
2323 	if (status == 0)
2324 		status = usb_phy_roothub_calibrate(hcd->phy_roothub);
2325 
2326 	if (status == 0) {
2327 		struct usb_device *udev;
2328 		int port1;
2329 
2330 		spin_lock_irq(&hcd_root_hub_lock);
2331 		if (!HCD_DEAD(hcd)) {
2332 			usb_set_device_state(rhdev, rhdev->actconfig
2333 					? USB_STATE_CONFIGURED
2334 					: USB_STATE_ADDRESS);
2335 			set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2336 			hcd->state = HC_STATE_RUNNING;
2337 		}
2338 		spin_unlock_irq(&hcd_root_hub_lock);
2339 
2340 		/*
2341 		 * Check whether any of the enabled ports on the root hub are
2342 		 * unsuspended.  If they are then a TRSMRCY delay is needed
2343 		 * (this is what the USB-2 spec calls a "global resume").
2344 		 * Otherwise we can skip the delay.
2345 		 */
2346 		usb_hub_for_each_child(rhdev, port1, udev) {
2347 			if (udev->state != USB_STATE_NOTATTACHED &&
2348 					!udev->port_is_suspended) {
2349 				usleep_range(10000, 11000);	/* TRSMRCY */
2350 				break;
2351 			}
2352 		}
2353 	} else {
2354 		hcd->state = old_state;
2355 		usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
2356 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
2357 				"resume", status);
2358 		if (status != -ESHUTDOWN)
2359 			usb_hc_died(hcd);
2360 	}
2361 	return status;
2362 }
2363 
2364 /* Workqueue routine for root-hub remote wakeup */
2365 static void hcd_resume_work(struct work_struct *work)
2366 {
2367 	struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
2368 	struct usb_device *udev = hcd->self.root_hub;
2369 
2370 	usb_remote_wakeup(udev);
2371 }
2372 
2373 /**
2374  * usb_hcd_resume_root_hub - called by HCD to resume its root hub
2375  * @hcd: host controller for this root hub
2376  *
2377  * The USB host controller calls this function when its root hub is
2378  * suspended (with the remote wakeup feature enabled) and a remote
2379  * wakeup request is received.  The routine submits a workqueue request
2380  * to resume the root hub (that is, manage its downstream ports again).
2381  */
2382 void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
2383 {
2384 	unsigned long flags;
2385 
2386 	spin_lock_irqsave (&hcd_root_hub_lock, flags);
2387 	if (hcd->rh_registered) {
2388 		pm_wakeup_event(&hcd->self.root_hub->dev, 0);
2389 		set_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
2390 		queue_work(pm_wq, &hcd->wakeup_work);
2391 	}
2392 	spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
2393 }
2394 EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
2395 
2396 #endif	/* CONFIG_PM */
2397 
2398 /*-------------------------------------------------------------------------*/
2399 
2400 #ifdef	CONFIG_USB_OTG
2401 
2402 /**
2403  * usb_bus_start_enum - start immediate enumeration (for OTG)
2404  * @bus: the bus (must use hcd framework)
2405  * @port_num: 1-based number of port; usually bus->otg_port
2406  * Context: atomic
2407  *
2408  * Starts enumeration, with an immediate reset followed later by
2409  * hub_wq identifying and possibly configuring the device.
2410  * This is needed by OTG controller drivers, where it helps meet
2411  * HNP protocol timing requirements for starting a port reset.
2412  *
2413  * Return: 0 if successful.
2414  */
2415 int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
2416 {
2417 	struct usb_hcd		*hcd;
2418 	int			status = -EOPNOTSUPP;
2419 
2420 	/* NOTE: since HNP can't start by grabbing the bus's address0_sem,
2421 	 * boards with root hubs hooked up to internal devices (instead of
2422 	 * just the OTG port) may need more attention to resetting...
2423 	 */
2424 	hcd = bus_to_hcd(bus);
2425 	if (port_num && hcd->driver->start_port_reset)
2426 		status = hcd->driver->start_port_reset(hcd, port_num);
2427 
2428 	/* allocate hub_wq shortly after (first) root port reset finishes;
2429 	 * it may issue others, until at least 50 msecs have passed.
2430 	 */
2431 	if (status == 0)
2432 		mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
2433 	return status;
2434 }
2435 EXPORT_SYMBOL_GPL(usb_bus_start_enum);
2436 
2437 #endif
2438 
2439 /*-------------------------------------------------------------------------*/
2440 
2441 /**
2442  * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
2443  * @irq: the IRQ being raised
2444  * @__hcd: pointer to the HCD whose IRQ is being signaled
2445  *
2446  * If the controller isn't HALTed, calls the driver's irq handler.
2447  * Checks whether the controller is now dead.
2448  *
2449  * Return: %IRQ_HANDLED if the IRQ was handled. %IRQ_NONE otherwise.
2450  */
2451 irqreturn_t usb_hcd_irq (int irq, void *__hcd)
2452 {
2453 	struct usb_hcd		*hcd = __hcd;
2454 	irqreturn_t		rc;
2455 
2456 	if (unlikely(HCD_DEAD(hcd) || !HCD_HW_ACCESSIBLE(hcd)))
2457 		rc = IRQ_NONE;
2458 	else if (hcd->driver->irq(hcd) == IRQ_NONE)
2459 		rc = IRQ_NONE;
2460 	else
2461 		rc = IRQ_HANDLED;
2462 
2463 	return rc;
2464 }
2465 EXPORT_SYMBOL_GPL(usb_hcd_irq);
2466 
2467 /*-------------------------------------------------------------------------*/
2468 
2469 /* Workqueue routine for when the root-hub has died. */
2470 static void hcd_died_work(struct work_struct *work)
2471 {
2472 	struct usb_hcd *hcd = container_of(work, struct usb_hcd, died_work);
2473 	static char *env[] = {
2474 		"ERROR=DEAD",
2475 		NULL
2476 	};
2477 
2478 	/* Notify user space that the host controller has died */
2479 	kobject_uevent_env(&hcd->self.root_hub->dev.kobj, KOBJ_OFFLINE, env);
2480 }
2481 
2482 /**
2483  * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
2484  * @hcd: pointer to the HCD representing the controller
2485  *
2486  * This is called by bus glue to report a USB host controller that died
2487  * while operations may still have been pending.  It's called automatically
2488  * by the PCI glue, so only glue for non-PCI busses should need to call it.
2489  *
2490  * Only call this function with the primary HCD.
2491  */
2492 void usb_hc_died (struct usb_hcd *hcd)
2493 {
2494 	unsigned long flags;
2495 
2496 	dev_err (hcd->self.controller, "HC died; cleaning up\n");
2497 
2498 	spin_lock_irqsave (&hcd_root_hub_lock, flags);
2499 	clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2500 	set_bit(HCD_FLAG_DEAD, &hcd->flags);
2501 	if (hcd->rh_registered) {
2502 		clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2503 
2504 		/* make hub_wq clean up old urbs and devices */
2505 		usb_set_device_state (hcd->self.root_hub,
2506 				USB_STATE_NOTATTACHED);
2507 		usb_kick_hub_wq(hcd->self.root_hub);
2508 	}
2509 	if (usb_hcd_is_primary_hcd(hcd) && hcd->shared_hcd) {
2510 		hcd = hcd->shared_hcd;
2511 		clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2512 		set_bit(HCD_FLAG_DEAD, &hcd->flags);
2513 		if (hcd->rh_registered) {
2514 			clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2515 
2516 			/* make hub_wq clean up old urbs and devices */
2517 			usb_set_device_state(hcd->self.root_hub,
2518 					USB_STATE_NOTATTACHED);
2519 			usb_kick_hub_wq(hcd->self.root_hub);
2520 		}
2521 	}
2522 
2523 	/* Handle the case where this function gets called with a shared HCD */
2524 	if (usb_hcd_is_primary_hcd(hcd))
2525 		schedule_work(&hcd->died_work);
2526 	else
2527 		schedule_work(&hcd->primary_hcd->died_work);
2528 
2529 	spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
2530 	/* Make sure that the other roothub is also deallocated. */
2531 }
2532 EXPORT_SYMBOL_GPL (usb_hc_died);
2533 
2534 /*-------------------------------------------------------------------------*/
2535 
2536 static void init_giveback_urb_bh(struct giveback_urb_bh *bh)
2537 {
2538 
2539 	spin_lock_init(&bh->lock);
2540 	INIT_LIST_HEAD(&bh->head);
2541 	tasklet_setup(&bh->bh, usb_giveback_urb_bh);
2542 }
2543 
2544 struct usb_hcd *__usb_create_hcd(const struct hc_driver *driver,
2545 		struct device *sysdev, struct device *dev, const char *bus_name,
2546 		struct usb_hcd *primary_hcd)
2547 {
2548 	struct usb_hcd *hcd;
2549 
2550 	hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
2551 	if (!hcd)
2552 		return NULL;
2553 	if (primary_hcd == NULL) {
2554 		hcd->address0_mutex = kmalloc(sizeof(*hcd->address0_mutex),
2555 				GFP_KERNEL);
2556 		if (!hcd->address0_mutex) {
2557 			kfree(hcd);
2558 			dev_dbg(dev, "hcd address0 mutex alloc failed\n");
2559 			return NULL;
2560 		}
2561 		mutex_init(hcd->address0_mutex);
2562 		hcd->bandwidth_mutex = kmalloc(sizeof(*hcd->bandwidth_mutex),
2563 				GFP_KERNEL);
2564 		if (!hcd->bandwidth_mutex) {
2565 			kfree(hcd->address0_mutex);
2566 			kfree(hcd);
2567 			dev_dbg(dev, "hcd bandwidth mutex alloc failed\n");
2568 			return NULL;
2569 		}
2570 		mutex_init(hcd->bandwidth_mutex);
2571 		dev_set_drvdata(dev, hcd);
2572 	} else {
2573 		mutex_lock(&usb_port_peer_mutex);
2574 		hcd->address0_mutex = primary_hcd->address0_mutex;
2575 		hcd->bandwidth_mutex = primary_hcd->bandwidth_mutex;
2576 		hcd->primary_hcd = primary_hcd;
2577 		primary_hcd->primary_hcd = primary_hcd;
2578 		hcd->shared_hcd = primary_hcd;
2579 		primary_hcd->shared_hcd = hcd;
2580 		mutex_unlock(&usb_port_peer_mutex);
2581 	}
2582 
2583 	kref_init(&hcd->kref);
2584 
2585 	usb_bus_init(&hcd->self);
2586 	hcd->self.controller = dev;
2587 	hcd->self.sysdev = sysdev;
2588 	hcd->self.bus_name = bus_name;
2589 
2590 	timer_setup(&hcd->rh_timer, rh_timer_func, 0);
2591 #ifdef CONFIG_PM
2592 	INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
2593 #endif
2594 
2595 	INIT_WORK(&hcd->died_work, hcd_died_work);
2596 
2597 	hcd->driver = driver;
2598 	hcd->speed = driver->flags & HCD_MASK;
2599 	hcd->product_desc = (driver->product_desc) ? driver->product_desc :
2600 			"USB Host Controller";
2601 	return hcd;
2602 }
2603 EXPORT_SYMBOL_GPL(__usb_create_hcd);
2604 
2605 /**
2606  * usb_create_shared_hcd - create and initialize an HCD structure
2607  * @driver: HC driver that will use this hcd
2608  * @dev: device for this HC, stored in hcd->self.controller
2609  * @bus_name: value to store in hcd->self.bus_name
2610  * @primary_hcd: a pointer to the usb_hcd structure that is sharing the
2611  *              PCI device.  Only allocate certain resources for the primary HCD
2612  *
2613  * Context: task context, might sleep.
2614  *
2615  * Allocate a struct usb_hcd, with extra space at the end for the
2616  * HC driver's private data.  Initialize the generic members of the
2617  * hcd structure.
2618  *
2619  * Return: On success, a pointer to the created and initialized HCD structure.
2620  * On failure (e.g. if memory is unavailable), %NULL.
2621  */
2622 struct usb_hcd *usb_create_shared_hcd(const struct hc_driver *driver,
2623 		struct device *dev, const char *bus_name,
2624 		struct usb_hcd *primary_hcd)
2625 {
2626 	return __usb_create_hcd(driver, dev, dev, bus_name, primary_hcd);
2627 }
2628 EXPORT_SYMBOL_GPL(usb_create_shared_hcd);
2629 
2630 /**
2631  * usb_create_hcd - create and initialize an HCD structure
2632  * @driver: HC driver that will use this hcd
2633  * @dev: device for this HC, stored in hcd->self.controller
2634  * @bus_name: value to store in hcd->self.bus_name
2635  *
2636  * Context: task context, might sleep.
2637  *
2638  * Allocate a struct usb_hcd, with extra space at the end for the
2639  * HC driver's private data.  Initialize the generic members of the
2640  * hcd structure.
2641  *
2642  * Return: On success, a pointer to the created and initialized HCD
2643  * structure. On failure (e.g. if memory is unavailable), %NULL.
2644  */
2645 struct usb_hcd *usb_create_hcd(const struct hc_driver *driver,
2646 		struct device *dev, const char *bus_name)
2647 {
2648 	return __usb_create_hcd(driver, dev, dev, bus_name, NULL);
2649 }
2650 EXPORT_SYMBOL_GPL(usb_create_hcd);
2651 
2652 /*
2653  * Roothubs that share one PCI device must also share the bandwidth mutex.
2654  * Don't deallocate the bandwidth_mutex until the last shared usb_hcd is
2655  * deallocated.
2656  *
2657  * Make sure to deallocate the bandwidth_mutex only when the last HCD is
2658  * freed.  When hcd_release() is called for either hcd in a peer set,
2659  * invalidate the peer's ->shared_hcd and ->primary_hcd pointers.
2660  */
2661 static void hcd_release(struct kref *kref)
2662 {
2663 	struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
2664 
2665 	mutex_lock(&usb_port_peer_mutex);
2666 	if (hcd->shared_hcd) {
2667 		struct usb_hcd *peer = hcd->shared_hcd;
2668 
2669 		peer->shared_hcd = NULL;
2670 		peer->primary_hcd = NULL;
2671 	} else {
2672 		kfree(hcd->address0_mutex);
2673 		kfree(hcd->bandwidth_mutex);
2674 	}
2675 	mutex_unlock(&usb_port_peer_mutex);
2676 	kfree(hcd);
2677 }
2678 
2679 struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
2680 {
2681 	if (hcd)
2682 		kref_get (&hcd->kref);
2683 	return hcd;
2684 }
2685 EXPORT_SYMBOL_GPL(usb_get_hcd);
2686 
2687 void usb_put_hcd (struct usb_hcd *hcd)
2688 {
2689 	if (hcd)
2690 		kref_put (&hcd->kref, hcd_release);
2691 }
2692 EXPORT_SYMBOL_GPL(usb_put_hcd);
2693 
2694 int usb_hcd_is_primary_hcd(struct usb_hcd *hcd)
2695 {
2696 	if (!hcd->primary_hcd)
2697 		return 1;
2698 	return hcd == hcd->primary_hcd;
2699 }
2700 EXPORT_SYMBOL_GPL(usb_hcd_is_primary_hcd);
2701 
2702 int usb_hcd_find_raw_port_number(struct usb_hcd *hcd, int port1)
2703 {
2704 	if (!hcd->driver->find_raw_port_number)
2705 		return port1;
2706 
2707 	return hcd->driver->find_raw_port_number(hcd, port1);
2708 }
2709 
2710 static int usb_hcd_request_irqs(struct usb_hcd *hcd,
2711 		unsigned int irqnum, unsigned long irqflags)
2712 {
2713 	int retval;
2714 
2715 	if (hcd->driver->irq) {
2716 
2717 		snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
2718 				hcd->driver->description, hcd->self.busnum);
2719 		retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
2720 				hcd->irq_descr, hcd);
2721 		if (retval != 0) {
2722 			dev_err(hcd->self.controller,
2723 					"request interrupt %d failed\n",
2724 					irqnum);
2725 			return retval;
2726 		}
2727 		hcd->irq = irqnum;
2728 		dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
2729 				(hcd->driver->flags & HCD_MEMORY) ?
2730 					"io mem" : "io port",
2731 				(unsigned long long)hcd->rsrc_start);
2732 	} else {
2733 		hcd->irq = 0;
2734 		if (hcd->rsrc_start)
2735 			dev_info(hcd->self.controller, "%s 0x%08llx\n",
2736 					(hcd->driver->flags & HCD_MEMORY) ?
2737 						"io mem" : "io port",
2738 					(unsigned long long)hcd->rsrc_start);
2739 	}
2740 	return 0;
2741 }
2742 
2743 /*
2744  * Before we free this root hub, flush in-flight peering attempts
2745  * and disable peer lookups
2746  */
2747 static void usb_put_invalidate_rhdev(struct usb_hcd *hcd)
2748 {
2749 	struct usb_device *rhdev;
2750 
2751 	mutex_lock(&usb_port_peer_mutex);
2752 	rhdev = hcd->self.root_hub;
2753 	hcd->self.root_hub = NULL;
2754 	mutex_unlock(&usb_port_peer_mutex);
2755 	usb_put_dev(rhdev);
2756 }
2757 
2758 /**
2759  * usb_stop_hcd - Halt the HCD
2760  * @hcd: the usb_hcd that has to be halted
2761  *
2762  * Stop the root-hub polling timer and invoke the HCD's ->stop callback.
2763  */
2764 static void usb_stop_hcd(struct usb_hcd *hcd)
2765 {
2766 	hcd->rh_pollable = 0;
2767 	clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2768 	del_timer_sync(&hcd->rh_timer);
2769 
2770 	hcd->driver->stop(hcd);
2771 	hcd->state = HC_STATE_HALT;
2772 
2773 	/* In case the HCD restarted the timer, stop it again. */
2774 	clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
2775 	del_timer_sync(&hcd->rh_timer);
2776 }
2777 
2778 /**
2779  * usb_add_hcd - finish generic HCD structure initialization and register
2780  * @hcd: the usb_hcd structure to initialize
2781  * @irqnum: Interrupt line to allocate
2782  * @irqflags: Interrupt type flags
2783  *
2784  * Finish the remaining parts of generic HCD initialization: allocate the
2785  * buffers of consistent memory, register the bus, request the IRQ line,
2786  * and call the driver's reset() and start() routines.
2787  */
2788 int usb_add_hcd(struct usb_hcd *hcd,
2789 		unsigned int irqnum, unsigned long irqflags)
2790 {
2791 	int retval;
2792 	struct usb_device *rhdev;
2793 	struct usb_hcd *shared_hcd;
2794 
2795 	if (!hcd->skip_phy_initialization) {
2796 		if (usb_hcd_is_primary_hcd(hcd)) {
2797 			hcd->phy_roothub = usb_phy_roothub_alloc(hcd->self.sysdev);
2798 			if (IS_ERR(hcd->phy_roothub))
2799 				return PTR_ERR(hcd->phy_roothub);
2800 		} else {
2801 			hcd->phy_roothub = usb_phy_roothub_alloc_usb3_phy(hcd->self.sysdev);
2802 			if (IS_ERR(hcd->phy_roothub))
2803 				return PTR_ERR(hcd->phy_roothub);
2804 		}
2805 
2806 		retval = usb_phy_roothub_init(hcd->phy_roothub);
2807 		if (retval)
2808 			return retval;
2809 
2810 		retval = usb_phy_roothub_set_mode(hcd->phy_roothub,
2811 						  PHY_MODE_USB_HOST_SS);
2812 		if (retval)
2813 			retval = usb_phy_roothub_set_mode(hcd->phy_roothub,
2814 							  PHY_MODE_USB_HOST);
2815 		if (retval)
2816 			goto err_usb_phy_roothub_power_on;
2817 
2818 		retval = usb_phy_roothub_power_on(hcd->phy_roothub);
2819 		if (retval)
2820 			goto err_usb_phy_roothub_power_on;
2821 	}
2822 
2823 	dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
2824 
2825 	switch (authorized_default) {
2826 	case USB_AUTHORIZE_NONE:
2827 		hcd->dev_policy = USB_DEVICE_AUTHORIZE_NONE;
2828 		break;
2829 
2830 	case USB_AUTHORIZE_INTERNAL:
2831 		hcd->dev_policy = USB_DEVICE_AUTHORIZE_INTERNAL;
2832 		break;
2833 
2834 	case USB_AUTHORIZE_ALL:
2835 	case USB_AUTHORIZE_WIRED:
2836 	default:
2837 		hcd->dev_policy = USB_DEVICE_AUTHORIZE_ALL;
2838 		break;
2839 	}
2840 
2841 	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2842 
2843 	/* per default all interfaces are authorized */
2844 	set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
2845 
2846 	/* HC is in reset state, but accessible.  Now do the one-time init,
2847 	 * bottom up so that hcds can customize the root hubs before hub_wq
2848 	 * starts talking to them.  (Note, bus id is assigned early too.)
2849 	 */
2850 	retval = hcd_buffer_create(hcd);
2851 	if (retval != 0) {
2852 		dev_dbg(hcd->self.sysdev, "pool alloc failed\n");
2853 		goto err_create_buf;
2854 	}
2855 
2856 	retval = usb_register_bus(&hcd->self);
2857 	if (retval < 0)
2858 		goto err_register_bus;
2859 
2860 	rhdev = usb_alloc_dev(NULL, &hcd->self, 0);
2861 	if (rhdev == NULL) {
2862 		dev_err(hcd->self.sysdev, "unable to allocate root hub\n");
2863 		retval = -ENOMEM;
2864 		goto err_allocate_root_hub;
2865 	}
2866 	mutex_lock(&usb_port_peer_mutex);
2867 	hcd->self.root_hub = rhdev;
2868 	mutex_unlock(&usb_port_peer_mutex);
2869 
2870 	rhdev->rx_lanes = 1;
2871 	rhdev->tx_lanes = 1;
2872 	rhdev->ssp_rate = USB_SSP_GEN_UNKNOWN;
2873 
2874 	switch (hcd->speed) {
2875 	case HCD_USB11:
2876 		rhdev->speed = USB_SPEED_FULL;
2877 		break;
2878 	case HCD_USB2:
2879 		rhdev->speed = USB_SPEED_HIGH;
2880 		break;
2881 	case HCD_USB3:
2882 		rhdev->speed = USB_SPEED_SUPER;
2883 		break;
2884 	case HCD_USB32:
2885 		rhdev->rx_lanes = 2;
2886 		rhdev->tx_lanes = 2;
2887 		rhdev->ssp_rate = USB_SSP_GEN_2x2;
2888 		rhdev->speed = USB_SPEED_SUPER_PLUS;
2889 		break;
2890 	case HCD_USB31:
2891 		rhdev->ssp_rate = USB_SSP_GEN_2x1;
2892 		rhdev->speed = USB_SPEED_SUPER_PLUS;
2893 		break;
2894 	default:
2895 		retval = -EINVAL;
2896 		goto err_set_rh_speed;
2897 	}
2898 
2899 	/* wakeup flag init defaults to "everything works" for root hubs,
2900 	 * but drivers can override it in reset() if needed, along with
2901 	 * recording the overall controller's system wakeup capability.
2902 	 */
2903 	device_set_wakeup_capable(&rhdev->dev, 1);
2904 
2905 	/* HCD_FLAG_RH_RUNNING doesn't matter until the root hub is
2906 	 * registered.  But since the controller can die at any time,
2907 	 * let's initialize the flag before touching the hardware.
2908 	 */
2909 	set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
2910 
2911 	/* "reset" is misnamed; its role is now one-time init. the controller
2912 	 * should already have been reset (and boot firmware kicked off etc).
2913 	 */
2914 	if (hcd->driver->reset) {
2915 		retval = hcd->driver->reset(hcd);
2916 		if (retval < 0) {
2917 			dev_err(hcd->self.controller, "can't setup: %d\n",
2918 					retval);
2919 			goto err_hcd_driver_setup;
2920 		}
2921 	}
2922 	hcd->rh_pollable = 1;
2923 
2924 	retval = usb_phy_roothub_calibrate(hcd->phy_roothub);
2925 	if (retval)
2926 		goto err_hcd_driver_setup;
2927 
2928 	/* NOTE: root hub and controller capabilities may not be the same */
2929 	if (device_can_wakeup(hcd->self.controller)
2930 			&& device_can_wakeup(&hcd->self.root_hub->dev))
2931 		dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
2932 
2933 	/* initialize tasklets */
2934 	init_giveback_urb_bh(&hcd->high_prio_bh);
2935 	hcd->high_prio_bh.high_prio = true;
2936 	init_giveback_urb_bh(&hcd->low_prio_bh);
2937 
2938 	/* enable irqs just before we start the controller,
2939 	 * if the BIOS provides legacy PCI irqs.
2940 	 */
2941 	if (usb_hcd_is_primary_hcd(hcd) && irqnum) {
2942 		retval = usb_hcd_request_irqs(hcd, irqnum, irqflags);
2943 		if (retval)
2944 			goto err_request_irq;
2945 	}
2946 
2947 	hcd->state = HC_STATE_RUNNING;
2948 	retval = hcd->driver->start(hcd);
2949 	if (retval < 0) {
2950 		dev_err(hcd->self.controller, "startup error %d\n", retval);
2951 		goto err_hcd_driver_start;
2952 	}
2953 
2954 	/* starting here, usbcore will pay attention to the shared HCD roothub */
2955 	shared_hcd = hcd->shared_hcd;
2956 	if (!usb_hcd_is_primary_hcd(hcd) && shared_hcd && HCD_DEFER_RH_REGISTER(shared_hcd)) {
2957 		retval = register_root_hub(shared_hcd);
2958 		if (retval != 0)
2959 			goto err_register_root_hub;
2960 
2961 		if (shared_hcd->uses_new_polling && HCD_POLL_RH(shared_hcd))
2962 			usb_hcd_poll_rh_status(shared_hcd);
2963 	}
2964 
2965 	/* starting here, usbcore will pay attention to this root hub */
2966 	if (!HCD_DEFER_RH_REGISTER(hcd)) {
2967 		retval = register_root_hub(hcd);
2968 		if (retval != 0)
2969 			goto err_register_root_hub;
2970 
2971 		if (hcd->uses_new_polling && HCD_POLL_RH(hcd))
2972 			usb_hcd_poll_rh_status(hcd);
2973 	}
2974 
2975 	return retval;
2976 
2977 err_register_root_hub:
2978 	usb_stop_hcd(hcd);
2979 err_hcd_driver_start:
2980 	if (usb_hcd_is_primary_hcd(hcd) && hcd->irq > 0)
2981 		free_irq(irqnum, hcd);
2982 err_request_irq:
2983 err_hcd_driver_setup:
2984 err_set_rh_speed:
2985 	usb_put_invalidate_rhdev(hcd);
2986 err_allocate_root_hub:
2987 	usb_deregister_bus(&hcd->self);
2988 err_register_bus:
2989 	hcd_buffer_destroy(hcd);
2990 err_create_buf:
2991 	usb_phy_roothub_power_off(hcd->phy_roothub);
2992 err_usb_phy_roothub_power_on:
2993 	usb_phy_roothub_exit(hcd->phy_roothub);
2994 
2995 	return retval;
2996 }
2997 EXPORT_SYMBOL_GPL(usb_add_hcd);
2998 
2999 /**
3000  * usb_remove_hcd - shutdown processing for generic HCDs
3001  * @hcd: the usb_hcd structure to remove
3002  *
3003  * Context: task context, might sleep.
3004  *
3005  * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
3006  * invoking the HCD's stop() method.
3007  */
3008 void usb_remove_hcd(struct usb_hcd *hcd)
3009 {
3010 	struct usb_device *rhdev;
3011 	bool rh_registered;
3012 
3013 	if (!hcd) {
3014 		pr_debug("%s: hcd is NULL\n", __func__);
3015 		return;
3016 	}
3017 	rhdev = hcd->self.root_hub;
3018 
3019 	dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
3020 
3021 	usb_get_dev(rhdev);
3022 	clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags);
3023 	if (HC_IS_RUNNING (hcd->state))
3024 		hcd->state = HC_STATE_QUIESCING;
3025 
3026 	dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
3027 	spin_lock_irq (&hcd_root_hub_lock);
3028 	rh_registered = hcd->rh_registered;
3029 	hcd->rh_registered = 0;
3030 	spin_unlock_irq (&hcd_root_hub_lock);
3031 
3032 #ifdef CONFIG_PM
3033 	cancel_work_sync(&hcd->wakeup_work);
3034 #endif
3035 	cancel_work_sync(&hcd->died_work);
3036 
3037 	mutex_lock(&usb_bus_idr_lock);
3038 	if (rh_registered)
3039 		usb_disconnect(&rhdev);		/* Sets rhdev to NULL */
3040 	mutex_unlock(&usb_bus_idr_lock);
3041 
3042 	/*
3043 	 * tasklet_kill() isn't needed here because:
3044 	 * - driver's disconnect() called from usb_disconnect() should
3045 	 *   make sure its URBs are completed during the disconnect()
3046 	 *   callback
3047 	 *
3048 	 * - it is too late to run complete() here since driver may have
3049 	 *   been removed already now
3050 	 */
3051 
3052 	/* Prevent any more root-hub status calls from the timer.
3053 	 * The HCD might still restart the timer (if a port status change
3054 	 * interrupt occurs), but usb_hcd_poll_rh_status() won't invoke
3055 	 * the hub_status_data() callback.
3056 	 */
3057 	usb_stop_hcd(hcd);
3058 
3059 	if (usb_hcd_is_primary_hcd(hcd)) {
3060 		if (hcd->irq > 0)
3061 			free_irq(hcd->irq, hcd);
3062 	}
3063 
3064 	usb_deregister_bus(&hcd->self);
3065 	hcd_buffer_destroy(hcd);
3066 
3067 	usb_phy_roothub_power_off(hcd->phy_roothub);
3068 	usb_phy_roothub_exit(hcd->phy_roothub);
3069 
3070 	usb_put_invalidate_rhdev(hcd);
3071 	hcd->flags = 0;
3072 }
3073 EXPORT_SYMBOL_GPL(usb_remove_hcd);
3074 
3075 void
3076 usb_hcd_platform_shutdown(struct platform_device *dev)
3077 {
3078 	struct usb_hcd *hcd = platform_get_drvdata(dev);
3079 
3080 	/* No need for pm_runtime_put(), we're shutting down */
3081 	pm_runtime_get_sync(&dev->dev);
3082 
3083 	if (hcd->driver->shutdown)
3084 		hcd->driver->shutdown(hcd);
3085 }
3086 EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown);
3087 
3088 int usb_hcd_setup_local_mem(struct usb_hcd *hcd, phys_addr_t phys_addr,
3089 			    dma_addr_t dma, size_t size)
3090 {
3091 	int err;
3092 	void *local_mem;
3093 
3094 	hcd->localmem_pool = devm_gen_pool_create(hcd->self.sysdev, 4,
3095 						  dev_to_node(hcd->self.sysdev),
3096 						  dev_name(hcd->self.sysdev));
3097 	if (IS_ERR(hcd->localmem_pool))
3098 		return PTR_ERR(hcd->localmem_pool);
3099 
3100 	/*
3101 	 * if a physical SRAM address was passed, map it, otherwise
3102 	 * allocate system memory as a buffer.
3103 	 */
3104 	if (phys_addr)
3105 		local_mem = devm_memremap(hcd->self.sysdev, phys_addr,
3106 					  size, MEMREMAP_WC);
3107 	else
3108 		local_mem = dmam_alloc_attrs(hcd->self.sysdev, size, &dma,
3109 					     GFP_KERNEL,
3110 					     DMA_ATTR_WRITE_COMBINE);
3111 
3112 	if (IS_ERR_OR_NULL(local_mem)) {
3113 		if (!local_mem)
3114 			return -ENOMEM;
3115 
3116 		return PTR_ERR(local_mem);
3117 	}
3118 
3119 	/*
3120 	 * Here we pass a dma_addr_t but the arg type is a phys_addr_t.
3121 	 * It's not backed by system memory and thus there's no kernel mapping
3122 	 * for it.
3123 	 */
3124 	err = gen_pool_add_virt(hcd->localmem_pool, (unsigned long)local_mem,
3125 				dma, size, dev_to_node(hcd->self.sysdev));
3126 	if (err < 0) {
3127 		dev_err(hcd->self.sysdev, "gen_pool_add_virt failed with %d\n",
3128 			err);
3129 		return err;
3130 	}
3131 
3132 	return 0;
3133 }
3134 EXPORT_SYMBOL_GPL(usb_hcd_setup_local_mem);
3135 
3136 /*-------------------------------------------------------------------------*/
3137 
3138 #if IS_ENABLED(CONFIG_USB_MON)
3139 
3140 const struct usb_mon_operations *mon_ops;
3141 
3142 /*
3143  * The registration is unlocked.
3144  * We do it this way because we do not want to lock in hot paths.
3145  *
3146  * Notice that the code is minimally error-proof. Because usbmon needs
3147  * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
3148  */
3149 
3150 int usb_mon_register(const struct usb_mon_operations *ops)
3151 {
3152 
3153 	if (mon_ops)
3154 		return -EBUSY;
3155 
3156 	mon_ops = ops;
3157 	mb();
3158 	return 0;
3159 }
3160 EXPORT_SYMBOL_GPL (usb_mon_register);
3161 
3162 void usb_mon_deregister (void)
3163 {
3164 
3165 	if (mon_ops == NULL) {
3166 		printk(KERN_ERR "USB: monitor was not registered\n");
3167 		return;
3168 	}
3169 	mon_ops = NULL;
3170 	mb();
3171 }
3172 EXPORT_SYMBOL_GPL (usb_mon_deregister);
3173 
3174 #endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */
3175