xref: /openbmc/linux/drivers/usb/core/hub.c (revision d2e9b4d6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * USB hub driver.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * (C) Copyright 1999 Linus Torvalds
51da177e4SLinus Torvalds  * (C) Copyright 1999 Johannes Erdfelt
61da177e4SLinus Torvalds  * (C) Copyright 1999 Gregory P. Smith
71da177e4SLinus Torvalds  * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/errno.h>
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/moduleparam.h>
151da177e4SLinus Torvalds #include <linux/completion.h>
161da177e4SLinus Torvalds #include <linux/sched.h>
171da177e4SLinus Torvalds #include <linux/list.h>
181da177e4SLinus Torvalds #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/ioctl.h>
201da177e4SLinus Torvalds #include <linux/usb.h>
211da177e4SLinus Torvalds #include <linux/usbdevice_fs.h>
229c8d6178Sakpm@osdl.org #include <linux/kthread.h>
234186ecf8SArjan van de Ven #include <linux/mutex.h>
247dfb7103SNigel Cunningham #include <linux/freezer.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include <asm/uaccess.h>
271da177e4SLinus Torvalds #include <asm/byteorder.h>
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include "usb.h"
301da177e4SLinus Torvalds #include "hcd.h"
311da177e4SLinus Torvalds #include "hub.h"
321da177e4SLinus Torvalds 
33f2a383e4SGreg Kroah-Hartman /* if we are in debug mode, always announce new devices */
34f2a383e4SGreg Kroah-Hartman #ifdef DEBUG
35f2a383e4SGreg Kroah-Hartman #ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
36f2a383e4SGreg Kroah-Hartman #define CONFIG_USB_ANNOUNCE_NEW_DEVICES
37f2a383e4SGreg Kroah-Hartman #endif
38f2a383e4SGreg Kroah-Hartman #endif
39f2a383e4SGreg Kroah-Hartman 
401bb5f66bSAlan Stern struct usb_hub {
411bb5f66bSAlan Stern 	struct device		*intfdev;	/* the "interface" device */
421bb5f66bSAlan Stern 	struct usb_device	*hdev;
43e8054854SAlan Stern 	struct kref		kref;
441bb5f66bSAlan Stern 	struct urb		*urb;		/* for interrupt polling pipe */
451bb5f66bSAlan Stern 
461bb5f66bSAlan Stern 	/* buffer for urb ... with extra space in case of babble */
471bb5f66bSAlan Stern 	char			(*buffer)[8];
481bb5f66bSAlan Stern 	dma_addr_t		buffer_dma;	/* DMA address for buffer */
491bb5f66bSAlan Stern 	union {
501bb5f66bSAlan Stern 		struct usb_hub_status	hub;
511bb5f66bSAlan Stern 		struct usb_port_status	port;
521bb5f66bSAlan Stern 	}			*status;	/* buffer for status reports */
53db90e7a1SAlan Stern 	struct mutex		status_mutex;	/* for the status buffer */
541bb5f66bSAlan Stern 
551bb5f66bSAlan Stern 	int			error;		/* last reported error */
561bb5f66bSAlan Stern 	int			nerrors;	/* track consecutive errors */
571bb5f66bSAlan Stern 
581bb5f66bSAlan Stern 	struct list_head	event_list;	/* hubs w/data or errs ready */
591bb5f66bSAlan Stern 	unsigned long		event_bits[1];	/* status change bitmask */
601bb5f66bSAlan Stern 	unsigned long		change_bits[1];	/* ports with logical connect
611bb5f66bSAlan Stern 							status change */
621bb5f66bSAlan Stern 	unsigned long		busy_bits[1];	/* ports being reset or
631bb5f66bSAlan Stern 							resumed */
641bb5f66bSAlan Stern #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
651bb5f66bSAlan Stern #error event_bits[] is too short!
661bb5f66bSAlan Stern #endif
671bb5f66bSAlan Stern 
681bb5f66bSAlan Stern 	struct usb_hub_descriptor *descriptor;	/* class descriptor */
691bb5f66bSAlan Stern 	struct usb_tt		tt;		/* Transaction Translator */
701bb5f66bSAlan Stern 
711bb5f66bSAlan Stern 	unsigned		mA_per_port;	/* current for each child */
721bb5f66bSAlan Stern 
731bb5f66bSAlan Stern 	unsigned		limited_power:1;
741bb5f66bSAlan Stern 	unsigned		quiescing:1;
75e8054854SAlan Stern 	unsigned		disconnected:1;
761bb5f66bSAlan Stern 
771bb5f66bSAlan Stern 	unsigned		has_indicators:1;
781bb5f66bSAlan Stern 	u8			indicator[USB_MAXCHILDREN];
796d5aefb8SDavid Howells 	struct delayed_work	leds;
808520f380SAlan Stern 	struct delayed_work	init_work;
811bb5f66bSAlan Stern };
821bb5f66bSAlan Stern 
831bb5f66bSAlan Stern 
841da177e4SLinus Torvalds /* Protect struct usb_device->state and ->children members
859ad3d6ccSAlan Stern  * Note: Both are also protected by ->dev.sem, except that ->state can
861da177e4SLinus Torvalds  * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
871da177e4SLinus Torvalds static DEFINE_SPINLOCK(device_state_lock);
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds /* khubd's worklist and its lock */
901da177e4SLinus Torvalds static DEFINE_SPINLOCK(hub_event_lock);
911da177e4SLinus Torvalds static LIST_HEAD(hub_event_list);	/* List of hubs needing servicing */
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /* Wakes up khubd */
941da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
951da177e4SLinus Torvalds 
969c8d6178Sakpm@osdl.org static struct task_struct *khubd_task;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /* cycle leds on hubs that aren't blinking for attention */
991da177e4SLinus Torvalds static int blinkenlights = 0;
1001da177e4SLinus Torvalds module_param (blinkenlights, bool, S_IRUGO);
1011da177e4SLinus Torvalds MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds /*
104fd7c519dSJaroslav Kysela  * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
105fd7c519dSJaroslav Kysela  * 10 seconds to send reply for the initial 64-byte descriptor request.
106fd7c519dSJaroslav Kysela  */
107fd7c519dSJaroslav Kysela /* define initial 64-byte descriptor request timeout in milliseconds */
108fd7c519dSJaroslav Kysela static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
109fd7c519dSJaroslav Kysela module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
110b9cef6c3SWu Fengguang MODULE_PARM_DESC(initial_descriptor_timeout,
111b9cef6c3SWu Fengguang 		"initial 64-byte descriptor request timeout in milliseconds "
112b9cef6c3SWu Fengguang 		"(default 5000 - 5.0 seconds)");
113fd7c519dSJaroslav Kysela 
114fd7c519dSJaroslav Kysela /*
1151da177e4SLinus Torvalds  * As of 2.6.10 we introduce a new USB device initialization scheme which
1161da177e4SLinus Torvalds  * closely resembles the way Windows works.  Hopefully it will be compatible
1171da177e4SLinus Torvalds  * with a wider range of devices than the old scheme.  However some previously
1181da177e4SLinus Torvalds  * working devices may start giving rise to "device not accepting address"
1191da177e4SLinus Torvalds  * errors; if that happens the user can try the old scheme by adjusting the
1201da177e4SLinus Torvalds  * following module parameters.
1211da177e4SLinus Torvalds  *
1221da177e4SLinus Torvalds  * For maximum flexibility there are two boolean parameters to control the
1231da177e4SLinus Torvalds  * hub driver's behavior.  On the first initialization attempt, if the
1241da177e4SLinus Torvalds  * "old_scheme_first" parameter is set then the old scheme will be used,
1251da177e4SLinus Torvalds  * otherwise the new scheme is used.  If that fails and "use_both_schemes"
1261da177e4SLinus Torvalds  * is set, then the driver will make another attempt, using the other scheme.
1271da177e4SLinus Torvalds  */
1281da177e4SLinus Torvalds static int old_scheme_first = 0;
1291da177e4SLinus Torvalds module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
1301da177e4SLinus Torvalds MODULE_PARM_DESC(old_scheme_first,
1311da177e4SLinus Torvalds 		 "start with the old device initialization scheme");
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds static int use_both_schemes = 1;
1341da177e4SLinus Torvalds module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
1351da177e4SLinus Torvalds MODULE_PARM_DESC(use_both_schemes,
1361da177e4SLinus Torvalds 		"try the other device initialization scheme if the "
1371da177e4SLinus Torvalds 		"first one fails");
1381da177e4SLinus Torvalds 
13932fe0198SAlan Stern /* Mutual exclusion for EHCI CF initialization.  This interferes with
14032fe0198SAlan Stern  * port reset on some companion controllers.
14132fe0198SAlan Stern  */
14232fe0198SAlan Stern DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
14332fe0198SAlan Stern EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
14432fe0198SAlan Stern 
145948fea37SAlan Stern #define HUB_DEBOUNCE_TIMEOUT	1500
146948fea37SAlan Stern #define HUB_DEBOUNCE_STEP	  25
147948fea37SAlan Stern #define HUB_DEBOUNCE_STABLE	 100
148948fea37SAlan Stern 
1491da177e4SLinus Torvalds 
150742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev);
151742120c6SMing Lei 
1521da177e4SLinus Torvalds static inline char *portspeed(int portstatus)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
1551da177e4SLinus Torvalds     		return "480 Mb/s";
1561da177e4SLinus Torvalds 	else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
1571da177e4SLinus Torvalds 		return "1.5 Mb/s";
158d2e9b4d6SSarah Sharp 	else if (portstatus & (1 << USB_PORT_FEAT_SUPERSPEED))
159d2e9b4d6SSarah Sharp 		return "5.0 Gb/s";
1601da177e4SLinus Torvalds 	else
1611da177e4SLinus Torvalds 		return "12 Mb/s";
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /* Note that hdev or one of its children must be locked! */
1651da177e4SLinus Torvalds static inline struct usb_hub *hdev_to_hub(struct usb_device *hdev)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	return usb_get_intfdata(hdev->actconfig->interface[0]);
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.4.5 */
1711da177e4SLinus Torvalds static int get_hub_descriptor(struct usb_device *hdev, void *data, int size)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds 	int i, ret;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	for (i = 0; i < 3; i++) {
1761da177e4SLinus Torvalds 		ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
1771da177e4SLinus Torvalds 			USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
1781da177e4SLinus Torvalds 			USB_DT_HUB << 8, 0, data, size,
1791da177e4SLinus Torvalds 			USB_CTRL_GET_TIMEOUT);
1801da177e4SLinus Torvalds 		if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
1811da177e4SLinus Torvalds 			return ret;
1821da177e4SLinus Torvalds 	}
1831da177e4SLinus Torvalds 	return -EINVAL;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds /*
1871da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.1
1881da177e4SLinus Torvalds  */
1891da177e4SLinus Torvalds static int clear_hub_feature(struct usb_device *hdev, int feature)
1901da177e4SLinus Torvalds {
1911da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1921da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds /*
1961da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.2
1971da177e4SLinus Torvalds  */
1981da177e4SLinus Torvalds static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
2011da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
2021da177e4SLinus Torvalds 		NULL, 0, 1000);
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds /*
2061da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.13
2071da177e4SLinus Torvalds  */
2081da177e4SLinus Torvalds static int set_port_feature(struct usb_device *hdev, int port1, int feature)
2091da177e4SLinus Torvalds {
2101da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
2111da177e4SLinus Torvalds 		USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
2121da177e4SLinus Torvalds 		NULL, 0, 1000);
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds /*
2161da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
2171da177e4SLinus Torvalds  * for info about using port indicators
2181da177e4SLinus Torvalds  */
2191da177e4SLinus Torvalds static void set_port_led(
2201da177e4SLinus Torvalds 	struct usb_hub *hub,
2211da177e4SLinus Torvalds 	int port1,
2221da177e4SLinus Torvalds 	int selector
2231da177e4SLinus Torvalds )
2241da177e4SLinus Torvalds {
2251da177e4SLinus Torvalds 	int status = set_port_feature(hub->hdev, (selector << 8) | port1,
2261da177e4SLinus Torvalds 			USB_PORT_FEAT_INDICATOR);
2271da177e4SLinus Torvalds 	if (status < 0)
2281da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
2291da177e4SLinus Torvalds 			"port %d indicator %s status %d\n",
2301da177e4SLinus Torvalds 			port1,
2311da177e4SLinus Torvalds 			({ char *s; switch (selector) {
2321da177e4SLinus Torvalds 			case HUB_LED_AMBER: s = "amber"; break;
2331da177e4SLinus Torvalds 			case HUB_LED_GREEN: s = "green"; break;
2341da177e4SLinus Torvalds 			case HUB_LED_OFF: s = "off"; break;
2351da177e4SLinus Torvalds 			case HUB_LED_AUTO: s = "auto"; break;
2361da177e4SLinus Torvalds 			default: s = "??"; break;
2371da177e4SLinus Torvalds 			}; s; }),
2381da177e4SLinus Torvalds 			status);
2391da177e4SLinus Torvalds }
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds #define	LED_CYCLE_PERIOD	((2*HZ)/3)
2421da177e4SLinus Torvalds 
243c4028958SDavid Howells static void led_work (struct work_struct *work)
2441da177e4SLinus Torvalds {
245c4028958SDavid Howells 	struct usb_hub		*hub =
246c4028958SDavid Howells 		container_of(work, struct usb_hub, leds.work);
2471da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
2481da177e4SLinus Torvalds 	unsigned		i;
2491da177e4SLinus Torvalds 	unsigned		changed = 0;
2501da177e4SLinus Torvalds 	int			cursor = -1;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
2531da177e4SLinus Torvalds 		return;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
2561da177e4SLinus Torvalds 		unsigned	selector, mode;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 		/* 30%-50% duty cycle */
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 		switch (hub->indicator[i]) {
2611da177e4SLinus Torvalds 		/* cycle marker */
2621da177e4SLinus Torvalds 		case INDICATOR_CYCLE:
2631da177e4SLinus Torvalds 			cursor = i;
2641da177e4SLinus Torvalds 			selector = HUB_LED_AUTO;
2651da177e4SLinus Torvalds 			mode = INDICATOR_AUTO;
2661da177e4SLinus Torvalds 			break;
2671da177e4SLinus Torvalds 		/* blinking green = sw attention */
2681da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK:
2691da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
2701da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK_OFF;
2711da177e4SLinus Torvalds 			break;
2721da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK_OFF:
2731da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
2741da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK;
2751da177e4SLinus Torvalds 			break;
2761da177e4SLinus Torvalds 		/* blinking amber = hw attention */
2771da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK:
2781da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
2791da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK_OFF;
2801da177e4SLinus Torvalds 			break;
2811da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK_OFF:
2821da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
2831da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK;
2841da177e4SLinus Torvalds 			break;
2851da177e4SLinus Torvalds 		/* blink green/amber = reserved */
2861da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK:
2871da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
2881da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK_OFF;
2891da177e4SLinus Torvalds 			break;
2901da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK_OFF:
2911da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
2921da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK;
2931da177e4SLinus Torvalds 			break;
2941da177e4SLinus Torvalds 		default:
2951da177e4SLinus Torvalds 			continue;
2961da177e4SLinus Torvalds 		}
2971da177e4SLinus Torvalds 		if (selector != HUB_LED_AUTO)
2981da177e4SLinus Torvalds 			changed = 1;
2991da177e4SLinus Torvalds 		set_port_led(hub, i + 1, selector);
3001da177e4SLinus Torvalds 		hub->indicator[i] = mode;
3011da177e4SLinus Torvalds 	}
3021da177e4SLinus Torvalds 	if (!changed && blinkenlights) {
3031da177e4SLinus Torvalds 		cursor++;
3041da177e4SLinus Torvalds 		cursor %= hub->descriptor->bNbrPorts;
3051da177e4SLinus Torvalds 		set_port_led(hub, cursor + 1, HUB_LED_GREEN);
3061da177e4SLinus Torvalds 		hub->indicator[cursor] = INDICATOR_CYCLE;
3071da177e4SLinus Torvalds 		changed++;
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds 	if (changed)
3101da177e4SLinus Torvalds 		schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds /* use a short timeout for hub/port status fetches */
3141da177e4SLinus Torvalds #define	USB_STS_TIMEOUT		1000
3151da177e4SLinus Torvalds #define	USB_STS_RETRIES		5
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds /*
3181da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.6
3191da177e4SLinus Torvalds  */
3201da177e4SLinus Torvalds static int get_hub_status(struct usb_device *hdev,
3211da177e4SLinus Torvalds 		struct usb_hub_status *data)
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
3261da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
3271da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
3281da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
3291da177e4SLinus Torvalds 	}
3301da177e4SLinus Torvalds 	return status;
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds /*
3341da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7
3351da177e4SLinus Torvalds  */
3361da177e4SLinus Torvalds static int get_port_status(struct usb_device *hdev, int port1,
3371da177e4SLinus Torvalds 		struct usb_port_status *data)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
3421da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
3431da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
3441da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
3451da177e4SLinus Torvalds 	}
3461da177e4SLinus Torvalds 	return status;
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds 
3493eb14915SAlan Stern static int hub_port_status(struct usb_hub *hub, int port1,
3503eb14915SAlan Stern 		u16 *status, u16 *change)
3513eb14915SAlan Stern {
3523eb14915SAlan Stern 	int ret;
3533eb14915SAlan Stern 
3543eb14915SAlan Stern 	mutex_lock(&hub->status_mutex);
3553eb14915SAlan Stern 	ret = get_port_status(hub->hdev, port1, &hub->status->port);
3563eb14915SAlan Stern 	if (ret < 4) {
3573eb14915SAlan Stern 		dev_err(hub->intfdev,
3583eb14915SAlan Stern 			"%s failed (err = %d)\n", __func__, ret);
3593eb14915SAlan Stern 		if (ret >= 0)
3603eb14915SAlan Stern 			ret = -EIO;
3613eb14915SAlan Stern 	} else {
3623eb14915SAlan Stern 		*status = le16_to_cpu(hub->status->port.wPortStatus);
3633eb14915SAlan Stern 		*change = le16_to_cpu(hub->status->port.wPortChange);
3643eb14915SAlan Stern 		ret = 0;
3653eb14915SAlan Stern 	}
3663eb14915SAlan Stern 	mutex_unlock(&hub->status_mutex);
3673eb14915SAlan Stern 	return ret;
3683eb14915SAlan Stern }
3693eb14915SAlan Stern 
3701da177e4SLinus Torvalds static void kick_khubd(struct usb_hub *hub)
3711da177e4SLinus Torvalds {
3721da177e4SLinus Torvalds 	unsigned long	flags;
3731da177e4SLinus Torvalds 
37440f122f3SAlan Stern 	/* Suppress autosuspend until khubd runs */
37540f122f3SAlan Stern 	to_usb_interface(hub->intfdev)->pm_usage_cnt = 1;
37640f122f3SAlan Stern 
3771da177e4SLinus Torvalds 	spin_lock_irqsave(&hub_event_lock, flags);
3782e2c5eeaSRoel Kluin 	if (!hub->disconnected && list_empty(&hub->event_list)) {
3791da177e4SLinus Torvalds 		list_add_tail(&hub->event_list, &hub_event_list);
3801da177e4SLinus Torvalds 		wake_up(&khubd_wait);
3811da177e4SLinus Torvalds 	}
3821da177e4SLinus Torvalds 	spin_unlock_irqrestore(&hub_event_lock, flags);
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds void usb_kick_khubd(struct usb_device *hdev)
3861da177e4SLinus Torvalds {
387e8054854SAlan Stern 	/* FIXME: What if hdev isn't bound to the hub driver? */
3881da177e4SLinus Torvalds 	kick_khubd(hdev_to_hub(hdev));
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds /* completion function, fires on port status changes and various faults */
3937d12e780SDavid Howells static void hub_irq(struct urb *urb)
3941da177e4SLinus Torvalds {
395ec17cf1cSTobias Klauser 	struct usb_hub *hub = urb->context;
396e015268dSAlan Stern 	int status = urb->status;
39771d2718fSRoel Kluin 	unsigned i;
3981da177e4SLinus Torvalds 	unsigned long bits;
3991da177e4SLinus Torvalds 
400e015268dSAlan Stern 	switch (status) {
4011da177e4SLinus Torvalds 	case -ENOENT:		/* synchronous unlink */
4021da177e4SLinus Torvalds 	case -ECONNRESET:	/* async unlink */
4031da177e4SLinus Torvalds 	case -ESHUTDOWN:	/* hardware going away */
4041da177e4SLinus Torvalds 		return;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	default:		/* presumably an error */
4071da177e4SLinus Torvalds 		/* Cause a hub reset after 10 consecutive errors */
408e015268dSAlan Stern 		dev_dbg (hub->intfdev, "transfer --> %d\n", status);
4091da177e4SLinus Torvalds 		if ((++hub->nerrors < 10) || hub->error)
4101da177e4SLinus Torvalds 			goto resubmit;
411e015268dSAlan Stern 		hub->error = status;
4121da177e4SLinus Torvalds 		/* FALL THROUGH */
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	/* let khubd handle things */
4151da177e4SLinus Torvalds 	case 0:			/* we got data:  port status changed */
4161da177e4SLinus Torvalds 		bits = 0;
4171da177e4SLinus Torvalds 		for (i = 0; i < urb->actual_length; ++i)
4181da177e4SLinus Torvalds 			bits |= ((unsigned long) ((*hub->buffer)[i]))
4191da177e4SLinus Torvalds 					<< (i*8);
4201da177e4SLinus Torvalds 		hub->event_bits[0] = bits;
4211da177e4SLinus Torvalds 		break;
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	hub->nerrors = 0;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	/* Something happened, let khubd figure it out */
4271da177e4SLinus Torvalds 	kick_khubd(hub);
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds resubmit:
4301da177e4SLinus Torvalds 	if (hub->quiescing)
4311da177e4SLinus Torvalds 		return;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
4341da177e4SLinus Torvalds 			&& status != -ENODEV && status != -EPERM)
4351da177e4SLinus Torvalds 		dev_err (hub->intfdev, "resubmit --> %d\n", status);
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.2.3 */
4391da177e4SLinus Torvalds static inline int
4401da177e4SLinus Torvalds hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
4431da177e4SLinus Torvalds 			       HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
4441da177e4SLinus Torvalds 			       tt, NULL, 0, 1000);
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds /*
4481da177e4SLinus Torvalds  * enumeration blocks khubd for a long time. we use keventd instead, since
4491da177e4SLinus Torvalds  * long blocking there is the exception, not the rule.  accordingly, HCDs
4501da177e4SLinus Torvalds  * talking to TTs must queue control transfers (not just bulk and iso), so
4511da177e4SLinus Torvalds  * both can talk to the same hub concurrently.
4521da177e4SLinus Torvalds  */
453c4028958SDavid Howells static void hub_tt_kevent (struct work_struct *work)
4541da177e4SLinus Torvalds {
455c4028958SDavid Howells 	struct usb_hub		*hub =
456c4028958SDavid Howells 		container_of(work, struct usb_hub, tt.kevent);
4571da177e4SLinus Torvalds 	unsigned long		flags;
45855e5fdfaSMark Lord 	int			limit = 100;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	spin_lock_irqsave (&hub->tt.lock, flags);
46155e5fdfaSMark Lord 	while (--limit && !list_empty (&hub->tt.clear_list)) {
462d0f830d3SH Hartley Sweeten 		struct list_head	*next;
4631da177e4SLinus Torvalds 		struct usb_tt_clear	*clear;
4641da177e4SLinus Torvalds 		struct usb_device	*hdev = hub->hdev;
4651da177e4SLinus Torvalds 		int			status;
4661da177e4SLinus Torvalds 
467d0f830d3SH Hartley Sweeten 		next = hub->tt.clear_list.next;
468d0f830d3SH Hartley Sweeten 		clear = list_entry (next, struct usb_tt_clear, clear_list);
4691da177e4SLinus Torvalds 		list_del (&clear->clear_list);
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 		/* drop lock so HCD can concurrently report other TT errors */
4721da177e4SLinus Torvalds 		spin_unlock_irqrestore (&hub->tt.lock, flags);
4731da177e4SLinus Torvalds 		status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
4741da177e4SLinus Torvalds 		spin_lock_irqsave (&hub->tt.lock, flags);
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 		if (status)
4771da177e4SLinus Torvalds 			dev_err (&hdev->dev,
4781da177e4SLinus Torvalds 				"clear tt %d (%04x) error %d\n",
4791da177e4SLinus Torvalds 				clear->tt, clear->devinfo, status);
4801da177e4SLinus Torvalds 		kfree(clear);
4811da177e4SLinus Torvalds 	}
4821da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hub->tt.lock, flags);
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds /**
4861da177e4SLinus Torvalds  * usb_hub_tt_clear_buffer - clear control/bulk TT state in high speed hub
4871da177e4SLinus Torvalds  * @udev: the device whose split transaction failed
4881da177e4SLinus Torvalds  * @pipe: identifies the endpoint of the failed transaction
4891da177e4SLinus Torvalds  *
4901da177e4SLinus Torvalds  * High speed HCDs use this to tell the hub driver that some split control or
4911da177e4SLinus Torvalds  * bulk transaction failed in a way that requires clearing internal state of
4921da177e4SLinus Torvalds  * a transaction translator.  This is normally detected (and reported) from
4931da177e4SLinus Torvalds  * interrupt context.
4941da177e4SLinus Torvalds  *
4951da177e4SLinus Torvalds  * It may not be possible for that hub to handle additional full (or low)
4961da177e4SLinus Torvalds  * speed transactions until that state is fully cleared out.
4971da177e4SLinus Torvalds  */
4981da177e4SLinus Torvalds void usb_hub_tt_clear_buffer (struct usb_device *udev, int pipe)
4991da177e4SLinus Torvalds {
5001da177e4SLinus Torvalds 	struct usb_tt		*tt = udev->tt;
5011da177e4SLinus Torvalds 	unsigned long		flags;
5021da177e4SLinus Torvalds 	struct usb_tt_clear	*clear;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	/* we've got to cope with an arbitrary number of pending TT clears,
5051da177e4SLinus Torvalds 	 * since each TT has "at least two" buffers that can need it (and
5061da177e4SLinus Torvalds 	 * there can be many TTs per hub).  even if they're uncommon.
5071da177e4SLinus Torvalds 	 */
50854e6ecb2SChristoph Lameter 	if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
5091da177e4SLinus Torvalds 		dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
5101da177e4SLinus Torvalds 		/* FIXME recover somehow ... RESET_TT? */
5111da177e4SLinus Torvalds 		return;
5121da177e4SLinus Torvalds 	}
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	/* info that CLEAR_TT_BUFFER needs */
5151da177e4SLinus Torvalds 	clear->tt = tt->multi ? udev->ttport : 1;
5161da177e4SLinus Torvalds 	clear->devinfo = usb_pipeendpoint (pipe);
5171da177e4SLinus Torvalds 	clear->devinfo |= udev->devnum << 4;
5181da177e4SLinus Torvalds 	clear->devinfo |= usb_pipecontrol (pipe)
5191da177e4SLinus Torvalds 			? (USB_ENDPOINT_XFER_CONTROL << 11)
5201da177e4SLinus Torvalds 			: (USB_ENDPOINT_XFER_BULK << 11);
5211da177e4SLinus Torvalds 	if (usb_pipein (pipe))
5221da177e4SLinus Torvalds 		clear->devinfo |= 1 << 15;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	/* tell keventd to clear state for this TT */
5251da177e4SLinus Torvalds 	spin_lock_irqsave (&tt->lock, flags);
5261da177e4SLinus Torvalds 	list_add_tail (&clear->clear_list, &tt->clear_list);
5271da177e4SLinus Torvalds 	schedule_work (&tt->kevent);
5281da177e4SLinus Torvalds 	spin_unlock_irqrestore (&tt->lock, flags);
5291da177e4SLinus Torvalds }
530782e70c6SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(usb_hub_tt_clear_buffer);
5311da177e4SLinus Torvalds 
5328520f380SAlan Stern /* If do_delay is false, return the number of milliseconds the caller
5338520f380SAlan Stern  * needs to delay.
5348520f380SAlan Stern  */
5358520f380SAlan Stern static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
5361da177e4SLinus Torvalds {
5371da177e4SLinus Torvalds 	int port1;
538b789696aSDavid Brownell 	unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
5398520f380SAlan Stern 	unsigned delay;
5404489a571SAlan Stern 	u16 wHubCharacteristics =
5414489a571SAlan Stern 			le16_to_cpu(hub->descriptor->wHubCharacteristics);
5421da177e4SLinus Torvalds 
5434489a571SAlan Stern 	/* Enable power on each port.  Some hubs have reserved values
5444489a571SAlan Stern 	 * of LPSM (> 2) in their descriptors, even though they are
5454489a571SAlan Stern 	 * USB 2.0 hubs.  Some hubs do not implement port-power switching
5464489a571SAlan Stern 	 * but only emulate it.  In all cases, the ports won't work
5474489a571SAlan Stern 	 * unless we send these messages to the hub.
5484489a571SAlan Stern 	 */
5494489a571SAlan Stern 	if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
5501da177e4SLinus Torvalds 		dev_dbg(hub->intfdev, "enabling power on all ports\n");
5514489a571SAlan Stern 	else
5524489a571SAlan Stern 		dev_dbg(hub->intfdev, "trying to enable port power on "
5534489a571SAlan Stern 				"non-switchable hub\n");
5541da177e4SLinus Torvalds 	for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
5554489a571SAlan Stern 		set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
5561da177e4SLinus Torvalds 
557b789696aSDavid Brownell 	/* Wait at least 100 msec for power to become stable */
5588520f380SAlan Stern 	delay = max(pgood_delay, (unsigned) 100);
5598520f380SAlan Stern 	if (do_delay)
5608520f380SAlan Stern 		msleep(delay);
5618520f380SAlan Stern 	return delay;
5621da177e4SLinus Torvalds }
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds static int hub_hub_status(struct usb_hub *hub,
5651da177e4SLinus Torvalds 		u16 *status, u16 *change)
5661da177e4SLinus Torvalds {
5671da177e4SLinus Torvalds 	int ret;
5681da177e4SLinus Torvalds 
569db90e7a1SAlan Stern 	mutex_lock(&hub->status_mutex);
5701da177e4SLinus Torvalds 	ret = get_hub_status(hub->hdev, &hub->status->hub);
5711da177e4SLinus Torvalds 	if (ret < 0)
5721da177e4SLinus Torvalds 		dev_err (hub->intfdev,
573441b62c1SHarvey Harrison 			"%s failed (err = %d)\n", __func__, ret);
5741da177e4SLinus Torvalds 	else {
5751da177e4SLinus Torvalds 		*status = le16_to_cpu(hub->status->hub.wHubStatus);
5761da177e4SLinus Torvalds 		*change = le16_to_cpu(hub->status->hub.wHubChange);
5771da177e4SLinus Torvalds 		ret = 0;
5781da177e4SLinus Torvalds 	}
579db90e7a1SAlan Stern 	mutex_unlock(&hub->status_mutex);
5801da177e4SLinus Torvalds 	return ret;
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
5838b28c752SAlan Stern static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
5848b28c752SAlan Stern {
5858b28c752SAlan Stern 	struct usb_device *hdev = hub->hdev;
5860458d5b4SAlan Stern 	int ret = 0;
5878b28c752SAlan Stern 
5880458d5b4SAlan Stern 	if (hdev->children[port1-1] && set_state)
5898b28c752SAlan Stern 		usb_set_device_state(hdev->children[port1-1],
5908b28c752SAlan Stern 				USB_STATE_NOTATTACHED);
5910458d5b4SAlan Stern 	if (!hub->error)
5928b28c752SAlan Stern 		ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
5938b28c752SAlan Stern 	if (ret)
5948b28c752SAlan Stern 		dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
5958b28c752SAlan Stern 				port1, ret);
5968b28c752SAlan Stern 	return ret;
5978b28c752SAlan Stern }
5988b28c752SAlan Stern 
5990458d5b4SAlan Stern /*
6000458d5b4SAlan Stern  * Disable a port and mark a logical connnect-change event, so that some
6010458d5b4SAlan Stern  * time later khubd will disconnect() any existing usb_device on the port
6020458d5b4SAlan Stern  * and will re-enumerate if there actually is a device attached.
6030458d5b4SAlan Stern  */
6040458d5b4SAlan Stern static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
6057d069b7dSAlan Stern {
6060458d5b4SAlan Stern 	dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
6070458d5b4SAlan Stern 	hub_port_disable(hub, port1, 1);
6080458d5b4SAlan Stern 
6090458d5b4SAlan Stern 	/* FIXME let caller ask to power down the port:
6100458d5b4SAlan Stern 	 *  - some devices won't enumerate without a VBUS power cycle
6110458d5b4SAlan Stern 	 *  - SRP saves power that way
6120458d5b4SAlan Stern 	 *  - ... new call, TBD ...
6130458d5b4SAlan Stern 	 * That's easy if this hub can switch power per-port, and
6140458d5b4SAlan Stern 	 * khubd reactivates the port later (timer, SRP, etc).
6150458d5b4SAlan Stern 	 * Powerdown must be optional, because of reset/DFU.
6160458d5b4SAlan Stern 	 */
6170458d5b4SAlan Stern 
6180458d5b4SAlan Stern 	set_bit(port1, hub->change_bits);
6190458d5b4SAlan Stern  	kick_khubd(hub);
6200458d5b4SAlan Stern }
6210458d5b4SAlan Stern 
6226ee0b270SAlan Stern enum hub_activation_type {
6238520f380SAlan Stern 	HUB_INIT, HUB_INIT2, HUB_INIT3,
6248520f380SAlan Stern 	HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
6256ee0b270SAlan Stern };
6265e6effaeSAlan Stern 
6278520f380SAlan Stern static void hub_init_func2(struct work_struct *ws);
6288520f380SAlan Stern static void hub_init_func3(struct work_struct *ws);
6298520f380SAlan Stern 
630f2835219SAlan Stern static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
6315e6effaeSAlan Stern {
6325e6effaeSAlan Stern 	struct usb_device *hdev = hub->hdev;
6335e6effaeSAlan Stern 	int port1;
634f2835219SAlan Stern 	int status;
635948fea37SAlan Stern 	bool need_debounce_delay = false;
6368520f380SAlan Stern 	unsigned delay;
6378520f380SAlan Stern 
6388520f380SAlan Stern 	/* Continue a partial initialization */
6398520f380SAlan Stern 	if (type == HUB_INIT2)
6408520f380SAlan Stern 		goto init2;
6418520f380SAlan Stern 	if (type == HUB_INIT3)
6428520f380SAlan Stern 		goto init3;
6435e6effaeSAlan Stern 
644f2835219SAlan Stern 	/* After a resume, port power should still be on.
645f2835219SAlan Stern 	 * For any other type of activation, turn it on.
646f2835219SAlan Stern 	 */
6478520f380SAlan Stern 	if (type != HUB_RESUME) {
6488520f380SAlan Stern 
6498520f380SAlan Stern 		/* Speed up system boot by using a delayed_work for the
6508520f380SAlan Stern 		 * hub's initial power-up delays.  This is pretty awkward
6518520f380SAlan Stern 		 * and the implementation looks like a home-brewed sort of
6528520f380SAlan Stern 		 * setjmp/longjmp, but it saves at least 100 ms for each
6538520f380SAlan Stern 		 * root hub (assuming usbcore is compiled into the kernel
6548520f380SAlan Stern 		 * rather than as a module).  It adds up.
6558520f380SAlan Stern 		 *
6568520f380SAlan Stern 		 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
6578520f380SAlan Stern 		 * because for those activation types the ports have to be
6588520f380SAlan Stern 		 * operational when we return.  In theory this could be done
6598520f380SAlan Stern 		 * for HUB_POST_RESET, but it's easier not to.
6608520f380SAlan Stern 		 */
6618520f380SAlan Stern 		if (type == HUB_INIT) {
6628520f380SAlan Stern 			delay = hub_power_on(hub, false);
6638520f380SAlan Stern 			PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
6648520f380SAlan Stern 			schedule_delayed_work(&hub->init_work,
6658520f380SAlan Stern 					msecs_to_jiffies(delay));
66661fbeba1SAlan Stern 
66761fbeba1SAlan Stern 			/* Suppress autosuspend until init is done */
66861fbeba1SAlan Stern 			to_usb_interface(hub->intfdev)->pm_usage_cnt = 1;
6698520f380SAlan Stern 			return;		/* Continues at init2: below */
6708520f380SAlan Stern 		} else {
6718520f380SAlan Stern 			hub_power_on(hub, true);
6728520f380SAlan Stern 		}
6738520f380SAlan Stern 	}
6748520f380SAlan Stern  init2:
675f2835219SAlan Stern 
6766ee0b270SAlan Stern 	/* Check each port and set hub->change_bits to let khubd know
6776ee0b270SAlan Stern 	 * which ports need attention.
6785e6effaeSAlan Stern 	 */
6795e6effaeSAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
6805e6effaeSAlan Stern 		struct usb_device *udev = hdev->children[port1-1];
6815e6effaeSAlan Stern 		u16 portstatus, portchange;
6825e6effaeSAlan Stern 
6836ee0b270SAlan Stern 		portstatus = portchange = 0;
6846ee0b270SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
6856ee0b270SAlan Stern 		if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
6866ee0b270SAlan Stern 			dev_dbg(hub->intfdev,
6876ee0b270SAlan Stern 					"port %d: status %04x change %04x\n",
6886ee0b270SAlan Stern 					port1, portstatus, portchange);
6895e6effaeSAlan Stern 
6906ee0b270SAlan Stern 		/* After anything other than HUB_RESUME (i.e., initialization
6916ee0b270SAlan Stern 		 * or any sort of reset), every port should be disabled.
6926ee0b270SAlan Stern 		 * Unconnected ports should likewise be disabled (paranoia),
6936ee0b270SAlan Stern 		 * and so should ports for which we have no usb_device.
6945e6effaeSAlan Stern 		 */
6956ee0b270SAlan Stern 		if ((portstatus & USB_PORT_STAT_ENABLE) && (
6966ee0b270SAlan Stern 				type != HUB_RESUME ||
6976ee0b270SAlan Stern 				!(portstatus & USB_PORT_STAT_CONNECTION) ||
6986ee0b270SAlan Stern 				!udev ||
6996ee0b270SAlan Stern 				udev->state == USB_STATE_NOTATTACHED)) {
7006ee0b270SAlan Stern 			clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
7016ee0b270SAlan Stern 			portstatus &= ~USB_PORT_STAT_ENABLE;
7026ee0b270SAlan Stern 		}
7036ee0b270SAlan Stern 
704948fea37SAlan Stern 		/* Clear status-change flags; we'll debounce later */
705948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
706948fea37SAlan Stern 			need_debounce_delay = true;
707948fea37SAlan Stern 			clear_port_feature(hub->hdev, port1,
708948fea37SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
709948fea37SAlan Stern 		}
710948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE) {
711948fea37SAlan Stern 			need_debounce_delay = true;
712948fea37SAlan Stern 			clear_port_feature(hub->hdev, port1,
713948fea37SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
714948fea37SAlan Stern 		}
715948fea37SAlan Stern 
7166ee0b270SAlan Stern 		if (!udev || udev->state == USB_STATE_NOTATTACHED) {
7176ee0b270SAlan Stern 			/* Tell khubd to disconnect the device or
7186ee0b270SAlan Stern 			 * check for a new connection
7196ee0b270SAlan Stern 			 */
7206ee0b270SAlan Stern 			if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
7216ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
7226ee0b270SAlan Stern 
7236ee0b270SAlan Stern 		} else if (portstatus & USB_PORT_STAT_ENABLE) {
7246ee0b270SAlan Stern 			/* The power session apparently survived the resume.
7256ee0b270SAlan Stern 			 * If there was an overcurrent or suspend change
7266ee0b270SAlan Stern 			 * (i.e., remote wakeup request), have khubd
7276ee0b270SAlan Stern 			 * take care of it.
7286ee0b270SAlan Stern 			 */
7296ee0b270SAlan Stern 			if (portchange)
7306ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
7316ee0b270SAlan Stern 
7326ee0b270SAlan Stern 		} else if (udev->persist_enabled) {
7336ee0b270SAlan Stern #ifdef CONFIG_PM
7345e6effaeSAlan Stern 			udev->reset_resume = 1;
7356ee0b270SAlan Stern #endif
7368808f00cSAlan Stern 			set_bit(port1, hub->change_bits);
7378808f00cSAlan Stern 
7386ee0b270SAlan Stern 		} else {
7396ee0b270SAlan Stern 			/* The power session is gone; tell khubd */
7406ee0b270SAlan Stern 			usb_set_device_state(udev, USB_STATE_NOTATTACHED);
7416ee0b270SAlan Stern 			set_bit(port1, hub->change_bits);
7425e6effaeSAlan Stern 		}
7435e6effaeSAlan Stern 	}
7445e6effaeSAlan Stern 
745948fea37SAlan Stern 	/* If no port-status-change flags were set, we don't need any
746948fea37SAlan Stern 	 * debouncing.  If flags were set we can try to debounce the
747948fea37SAlan Stern 	 * ports all at once right now, instead of letting khubd do them
748948fea37SAlan Stern 	 * one at a time later on.
749948fea37SAlan Stern 	 *
750948fea37SAlan Stern 	 * If any port-status changes do occur during this delay, khubd
751948fea37SAlan Stern 	 * will see them later and handle them normally.
752948fea37SAlan Stern 	 */
7538520f380SAlan Stern 	if (need_debounce_delay) {
7548520f380SAlan Stern 		delay = HUB_DEBOUNCE_STABLE;
755f2835219SAlan Stern 
7568520f380SAlan Stern 		/* Don't do a long sleep inside a workqueue routine */
7578520f380SAlan Stern 		if (type == HUB_INIT2) {
7588520f380SAlan Stern 			PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
7598520f380SAlan Stern 			schedule_delayed_work(&hub->init_work,
7608520f380SAlan Stern 					msecs_to_jiffies(delay));
7618520f380SAlan Stern 			return;		/* Continues at init3: below */
7628520f380SAlan Stern 		} else {
7638520f380SAlan Stern 			msleep(delay);
7648520f380SAlan Stern 		}
7658520f380SAlan Stern 	}
7668520f380SAlan Stern  init3:
767f2835219SAlan Stern 	hub->quiescing = 0;
768f2835219SAlan Stern 
769f2835219SAlan Stern 	status = usb_submit_urb(hub->urb, GFP_NOIO);
770f2835219SAlan Stern 	if (status < 0)
771f2835219SAlan Stern 		dev_err(hub->intfdev, "activate --> %d\n", status);
772f2835219SAlan Stern 	if (hub->has_indicators && blinkenlights)
773f2835219SAlan Stern 		schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
774f2835219SAlan Stern 
775f2835219SAlan Stern 	/* Scan all ports that need attention */
776f2835219SAlan Stern 	kick_khubd(hub);
7775e6effaeSAlan Stern }
7785e6effaeSAlan Stern 
7798520f380SAlan Stern /* Implement the continuations for the delays above */
7808520f380SAlan Stern static void hub_init_func2(struct work_struct *ws)
7818520f380SAlan Stern {
7828520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
7838520f380SAlan Stern 
7848520f380SAlan Stern 	hub_activate(hub, HUB_INIT2);
7858520f380SAlan Stern }
7868520f380SAlan Stern 
7878520f380SAlan Stern static void hub_init_func3(struct work_struct *ws)
7888520f380SAlan Stern {
7898520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
7908520f380SAlan Stern 
7918520f380SAlan Stern 	hub_activate(hub, HUB_INIT3);
7928520f380SAlan Stern }
7938520f380SAlan Stern 
7944330354fSAlan Stern enum hub_quiescing_type {
7954330354fSAlan Stern 	HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
7964330354fSAlan Stern };
7974330354fSAlan Stern 
7984330354fSAlan Stern static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
7994330354fSAlan Stern {
8004330354fSAlan Stern 	struct usb_device *hdev = hub->hdev;
8014330354fSAlan Stern 	int i;
8024330354fSAlan Stern 
8038520f380SAlan Stern 	cancel_delayed_work_sync(&hub->init_work);
8048520f380SAlan Stern 
8054330354fSAlan Stern 	/* khubd and related activity won't re-trigger */
8064330354fSAlan Stern 	hub->quiescing = 1;
8074330354fSAlan Stern 
8084330354fSAlan Stern 	if (type != HUB_SUSPEND) {
8094330354fSAlan Stern 		/* Disconnect all the children */
8104330354fSAlan Stern 		for (i = 0; i < hdev->maxchild; ++i) {
8114330354fSAlan Stern 			if (hdev->children[i])
8124330354fSAlan Stern 				usb_disconnect(&hdev->children[i]);
8134330354fSAlan Stern 		}
8144330354fSAlan Stern 	}
8154330354fSAlan Stern 
8164330354fSAlan Stern 	/* Stop khubd and related activity */
8174330354fSAlan Stern 	usb_kill_urb(hub->urb);
8184330354fSAlan Stern 	if (hub->has_indicators)
8194330354fSAlan Stern 		cancel_delayed_work_sync(&hub->leds);
8204330354fSAlan Stern 	if (hub->tt.hub)
8214330354fSAlan Stern 		cancel_work_sync(&hub->tt.kevent);
8224330354fSAlan Stern }
8234330354fSAlan Stern 
8243eb14915SAlan Stern /* caller has locked the hub device */
8253eb14915SAlan Stern static int hub_pre_reset(struct usb_interface *intf)
8263eb14915SAlan Stern {
8273eb14915SAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
8283eb14915SAlan Stern 
8294330354fSAlan Stern 	hub_quiesce(hub, HUB_PRE_RESET);
830f07600cfSAlan Stern 	return 0;
8317d069b7dSAlan Stern }
8327d069b7dSAlan Stern 
8337d069b7dSAlan Stern /* caller has locked the hub device */
834f07600cfSAlan Stern static int hub_post_reset(struct usb_interface *intf)
8357d069b7dSAlan Stern {
8367de18d8bSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
8377de18d8bSAlan Stern 
838f2835219SAlan Stern 	hub_activate(hub, HUB_POST_RESET);
839f07600cfSAlan Stern 	return 0;
8407d069b7dSAlan Stern }
8417d069b7dSAlan Stern 
8421da177e4SLinus Torvalds static int hub_configure(struct usb_hub *hub,
8431da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint)
8441da177e4SLinus Torvalds {
8451da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
8461da177e4SLinus Torvalds 	struct device *hub_dev = hub->intfdev;
8471da177e4SLinus Torvalds 	u16 hubstatus, hubchange;
84874ad9bd2SGreg Kroah-Hartman 	u16 wHubCharacteristics;
8491da177e4SLinus Torvalds 	unsigned int pipe;
8501da177e4SLinus Torvalds 	int maxp, ret;
8511da177e4SLinus Torvalds 	char *message;
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	hub->buffer = usb_buffer_alloc(hdev, sizeof(*hub->buffer), GFP_KERNEL,
8541da177e4SLinus Torvalds 			&hub->buffer_dma);
8551da177e4SLinus Torvalds 	if (!hub->buffer) {
8561da177e4SLinus Torvalds 		message = "can't allocate hub irq buffer";
8571da177e4SLinus Torvalds 		ret = -ENOMEM;
8581da177e4SLinus Torvalds 		goto fail;
8591da177e4SLinus Torvalds 	}
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 	hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
8621da177e4SLinus Torvalds 	if (!hub->status) {
8631da177e4SLinus Torvalds 		message = "can't kmalloc hub status buffer";
8641da177e4SLinus Torvalds 		ret = -ENOMEM;
8651da177e4SLinus Torvalds 		goto fail;
8661da177e4SLinus Torvalds 	}
867db90e7a1SAlan Stern 	mutex_init(&hub->status_mutex);
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
8701da177e4SLinus Torvalds 	if (!hub->descriptor) {
8711da177e4SLinus Torvalds 		message = "can't kmalloc hub descriptor";
8721da177e4SLinus Torvalds 		ret = -ENOMEM;
8731da177e4SLinus Torvalds 		goto fail;
8741da177e4SLinus Torvalds 	}
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	/* Request the entire hub descriptor.
8771da177e4SLinus Torvalds 	 * hub->descriptor can handle USB_MAXCHILDREN ports,
8781da177e4SLinus Torvalds 	 * but the hub can/will return fewer bytes here.
8791da177e4SLinus Torvalds 	 */
8801da177e4SLinus Torvalds 	ret = get_hub_descriptor(hdev, hub->descriptor,
8811da177e4SLinus Torvalds 			sizeof(*hub->descriptor));
8821da177e4SLinus Torvalds 	if (ret < 0) {
8831da177e4SLinus Torvalds 		message = "can't read hub descriptor";
8841da177e4SLinus Torvalds 		goto fail;
8851da177e4SLinus Torvalds 	} else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
8861da177e4SLinus Torvalds 		message = "hub has too many ports!";
8871da177e4SLinus Torvalds 		ret = -ENODEV;
8881da177e4SLinus Torvalds 		goto fail;
8891da177e4SLinus Torvalds 	}
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	hdev->maxchild = hub->descriptor->bNbrPorts;
8921da177e4SLinus Torvalds 	dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
8931da177e4SLinus Torvalds 		(hdev->maxchild == 1) ? "" : "s");
8941da177e4SLinus Torvalds 
89574ad9bd2SGreg Kroah-Hartman 	wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
8961da177e4SLinus Torvalds 
89774ad9bd2SGreg Kroah-Hartman 	if (wHubCharacteristics & HUB_CHAR_COMPOUND) {
8981da177e4SLinus Torvalds 		int	i;
8991da177e4SLinus Torvalds 		char	portstr [USB_MAXCHILDREN + 1];
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 		for (i = 0; i < hdev->maxchild; i++)
9021da177e4SLinus Torvalds 			portstr[i] = hub->descriptor->DeviceRemovable
9031da177e4SLinus Torvalds 				    [((i + 1) / 8)] & (1 << ((i + 1) % 8))
9041da177e4SLinus Torvalds 				? 'F' : 'R';
9051da177e4SLinus Torvalds 		portstr[hdev->maxchild] = 0;
9061da177e4SLinus Torvalds 		dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
9071da177e4SLinus Torvalds 	} else
9081da177e4SLinus Torvalds 		dev_dbg(hub_dev, "standalone hub\n");
9091da177e4SLinus Torvalds 
91074ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_LPSM) {
9111da177e4SLinus Torvalds 		case 0x00:
9121da177e4SLinus Torvalds 			dev_dbg(hub_dev, "ganged power switching\n");
9131da177e4SLinus Torvalds 			break;
9141da177e4SLinus Torvalds 		case 0x01:
9151da177e4SLinus Torvalds 			dev_dbg(hub_dev, "individual port power switching\n");
9161da177e4SLinus Torvalds 			break;
9171da177e4SLinus Torvalds 		case 0x02:
9181da177e4SLinus Torvalds 		case 0x03:
9191da177e4SLinus Torvalds 			dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
9201da177e4SLinus Torvalds 			break;
9211da177e4SLinus Torvalds 	}
9221da177e4SLinus Torvalds 
92374ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_OCPM) {
9241da177e4SLinus Torvalds 		case 0x00:
9251da177e4SLinus Torvalds 			dev_dbg(hub_dev, "global over-current protection\n");
9261da177e4SLinus Torvalds 			break;
9271da177e4SLinus Torvalds 		case 0x08:
9281da177e4SLinus Torvalds 			dev_dbg(hub_dev, "individual port over-current protection\n");
9291da177e4SLinus Torvalds 			break;
9301da177e4SLinus Torvalds 		case 0x10:
9311da177e4SLinus Torvalds 		case 0x18:
9321da177e4SLinus Torvalds 			dev_dbg(hub_dev, "no over-current protection\n");
9331da177e4SLinus Torvalds                         break;
9341da177e4SLinus Torvalds 	}
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	spin_lock_init (&hub->tt.lock);
9371da177e4SLinus Torvalds 	INIT_LIST_HEAD (&hub->tt.clear_list);
938c4028958SDavid Howells 	INIT_WORK (&hub->tt.kevent, hub_tt_kevent);
9391da177e4SLinus Torvalds 	switch (hdev->descriptor.bDeviceProtocol) {
9401da177e4SLinus Torvalds 		case 0:
9411da177e4SLinus Torvalds 			break;
9421da177e4SLinus Torvalds 		case 1:
9431da177e4SLinus Torvalds 			dev_dbg(hub_dev, "Single TT\n");
9441da177e4SLinus Torvalds 			hub->tt.hub = hdev;
9451da177e4SLinus Torvalds 			break;
9461da177e4SLinus Torvalds 		case 2:
9471da177e4SLinus Torvalds 			ret = usb_set_interface(hdev, 0, 1);
9481da177e4SLinus Torvalds 			if (ret == 0) {
9491da177e4SLinus Torvalds 				dev_dbg(hub_dev, "TT per port\n");
9501da177e4SLinus Torvalds 				hub->tt.multi = 1;
9511da177e4SLinus Torvalds 			} else
9521da177e4SLinus Torvalds 				dev_err(hub_dev, "Using single TT (err %d)\n",
9531da177e4SLinus Torvalds 					ret);
9541da177e4SLinus Torvalds 			hub->tt.hub = hdev;
9551da177e4SLinus Torvalds 			break;
956d2e9b4d6SSarah Sharp 		case 3:
957d2e9b4d6SSarah Sharp 			/* USB 3.0 hubs don't have a TT */
958d2e9b4d6SSarah Sharp 			break;
9591da177e4SLinus Torvalds 		default:
9601da177e4SLinus Torvalds 			dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
9611da177e4SLinus Torvalds 				hdev->descriptor.bDeviceProtocol);
9621da177e4SLinus Torvalds 			break;
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds 
965e09711aeSdavid-b@pacbell.net 	/* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
96674ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_TTTT) {
967e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_8_BITS:
968e09711aeSdavid-b@pacbell.net 			if (hdev->descriptor.bDeviceProtocol != 0) {
969e09711aeSdavid-b@pacbell.net 				hub->tt.think_time = 666;
970e09711aeSdavid-b@pacbell.net 				dev_dbg(hub_dev, "TT requires at most %d "
971e09711aeSdavid-b@pacbell.net 						"FS bit times (%d ns)\n",
972e09711aeSdavid-b@pacbell.net 					8, hub->tt.think_time);
973e09711aeSdavid-b@pacbell.net 			}
9741da177e4SLinus Torvalds 			break;
975e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_16_BITS:
976e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 2;
977e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
978e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
979e09711aeSdavid-b@pacbell.net 				16, hub->tt.think_time);
9801da177e4SLinus Torvalds 			break;
981e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_24_BITS:
982e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 3;
983e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
984e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
985e09711aeSdavid-b@pacbell.net 				24, hub->tt.think_time);
9861da177e4SLinus Torvalds 			break;
987e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_32_BITS:
988e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 4;
989e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
990e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
991e09711aeSdavid-b@pacbell.net 				32, hub->tt.think_time);
9921da177e4SLinus Torvalds 			break;
9931da177e4SLinus Torvalds 	}
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	/* probe() zeroes hub->indicator[] */
99674ad9bd2SGreg Kroah-Hartman 	if (wHubCharacteristics & HUB_CHAR_PORTIND) {
9971da177e4SLinus Torvalds 		hub->has_indicators = 1;
9981da177e4SLinus Torvalds 		dev_dbg(hub_dev, "Port indicators are supported\n");
9991da177e4SLinus Torvalds 	}
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	dev_dbg(hub_dev, "power on to power good time: %dms\n",
10021da177e4SLinus Torvalds 		hub->descriptor->bPwrOn2PwrGood * 2);
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	/* power budgeting mostly matters with bus-powered hubs,
10051da177e4SLinus Torvalds 	 * and battery-powered root hubs (may provide just 8 mA).
10061da177e4SLinus Torvalds 	 */
10071da177e4SLinus Torvalds 	ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
100855c52718SAlan Stern 	if (ret < 2) {
10091da177e4SLinus Torvalds 		message = "can't get hub status";
10101da177e4SLinus Torvalds 		goto fail;
10111da177e4SLinus Torvalds 	}
10127d35b929SAlan Stern 	le16_to_cpus(&hubstatus);
10137d35b929SAlan Stern 	if (hdev == hdev->bus->root_hub) {
101455c52718SAlan Stern 		if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
101555c52718SAlan Stern 			hub->mA_per_port = 500;
101655c52718SAlan Stern 		else {
101755c52718SAlan Stern 			hub->mA_per_port = hdev->bus_mA;
101855c52718SAlan Stern 			hub->limited_power = 1;
101955c52718SAlan Stern 		}
10207d35b929SAlan Stern 	} else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
10211da177e4SLinus Torvalds 		dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
10221da177e4SLinus Torvalds 			hub->descriptor->bHubContrCurrent);
102355c52718SAlan Stern 		hub->limited_power = 1;
102455c52718SAlan Stern 		if (hdev->maxchild > 0) {
102555c52718SAlan Stern 			int remaining = hdev->bus_mA -
102655c52718SAlan Stern 					hub->descriptor->bHubContrCurrent;
10271da177e4SLinus Torvalds 
102855c52718SAlan Stern 			if (remaining < hdev->maxchild * 100)
102955c52718SAlan Stern 				dev_warn(hub_dev,
103055c52718SAlan Stern 					"insufficient power available "
103155c52718SAlan Stern 					"to use all downstream ports\n");
103255c52718SAlan Stern 			hub->mA_per_port = 100;		/* 7.2.1.1 */
103355c52718SAlan Stern 		}
103455c52718SAlan Stern 	} else {	/* Self-powered external hub */
103555c52718SAlan Stern 		/* FIXME: What about battery-powered external hubs that
103655c52718SAlan Stern 		 * provide less current per port? */
103755c52718SAlan Stern 		hub->mA_per_port = 500;
103855c52718SAlan Stern 	}
103955c52718SAlan Stern 	if (hub->mA_per_port < 500)
104055c52718SAlan Stern 		dev_dbg(hub_dev, "%umA bus power budget for each child\n",
104155c52718SAlan Stern 				hub->mA_per_port);
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	ret = hub_hub_status(hub, &hubstatus, &hubchange);
10441da177e4SLinus Torvalds 	if (ret < 0) {
10451da177e4SLinus Torvalds 		message = "can't get hub status";
10461da177e4SLinus Torvalds 		goto fail;
10471da177e4SLinus Torvalds 	}
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 	/* local power status reports aren't always correct */
10501da177e4SLinus Torvalds 	if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
10511da177e4SLinus Torvalds 		dev_dbg(hub_dev, "local power source is %s\n",
10521da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_LOCAL_POWER)
10531da177e4SLinus Torvalds 			? "lost (inactive)" : "good");
10541da177e4SLinus Torvalds 
105574ad9bd2SGreg Kroah-Hartman 	if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
10561da177e4SLinus Torvalds 		dev_dbg(hub_dev, "%sover-current condition exists\n",
10571da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
10581da177e4SLinus Torvalds 
105988fafff9Sinaky@linux.intel.com 	/* set up the interrupt endpoint
106088fafff9Sinaky@linux.intel.com 	 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
106188fafff9Sinaky@linux.intel.com 	 * bytes as USB2.0[11.12.3] says because some hubs are known
106288fafff9Sinaky@linux.intel.com 	 * to send more data (and thus cause overflow). For root hubs,
106388fafff9Sinaky@linux.intel.com 	 * maxpktsize is defined in hcd.c's fake endpoint descriptors
106488fafff9Sinaky@linux.intel.com 	 * to be big enough for at least USB_MAXCHILDREN ports. */
10651da177e4SLinus Torvalds 	pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
10661da177e4SLinus Torvalds 	maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	if (maxp > sizeof(*hub->buffer))
10691da177e4SLinus Torvalds 		maxp = sizeof(*hub->buffer);
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	hub->urb = usb_alloc_urb(0, GFP_KERNEL);
10721da177e4SLinus Torvalds 	if (!hub->urb) {
10731da177e4SLinus Torvalds 		message = "couldn't allocate interrupt urb";
10741da177e4SLinus Torvalds 		ret = -ENOMEM;
10751da177e4SLinus Torvalds 		goto fail;
10761da177e4SLinus Torvalds 	}
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
10791da177e4SLinus Torvalds 		hub, endpoint->bInterval);
10801da177e4SLinus Torvalds 	hub->urb->transfer_dma = hub->buffer_dma;
10811da177e4SLinus Torvalds 	hub->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	/* maybe cycle the hub leds */
10841da177e4SLinus Torvalds 	if (hub->has_indicators && blinkenlights)
10851da177e4SLinus Torvalds 		hub->indicator [0] = INDICATOR_CYCLE;
10861da177e4SLinus Torvalds 
1087f2835219SAlan Stern 	hub_activate(hub, HUB_INIT);
10881da177e4SLinus Torvalds 	return 0;
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds fail:
10911da177e4SLinus Torvalds 	dev_err (hub_dev, "config failed, %s (err %d)\n",
10921da177e4SLinus Torvalds 			message, ret);
10931da177e4SLinus Torvalds 	/* hub_disconnect() frees urb and descriptor */
10941da177e4SLinus Torvalds 	return ret;
10951da177e4SLinus Torvalds }
10961da177e4SLinus Torvalds 
1097e8054854SAlan Stern static void hub_release(struct kref *kref)
1098e8054854SAlan Stern {
1099e8054854SAlan Stern 	struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1100e8054854SAlan Stern 
1101e8054854SAlan Stern 	usb_put_intf(to_usb_interface(hub->intfdev));
1102e8054854SAlan Stern 	kfree(hub);
1103e8054854SAlan Stern }
1104e8054854SAlan Stern 
11051da177e4SLinus Torvalds static unsigned highspeed_hubs;
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds static void hub_disconnect(struct usb_interface *intf)
11081da177e4SLinus Torvalds {
11091da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata (intf);
1110e8054854SAlan Stern 
1111e8054854SAlan Stern 	/* Take the hub off the event list and don't let it be added again */
1112e8054854SAlan Stern 	spin_lock_irq(&hub_event_lock);
1113e8054854SAlan Stern 	list_del_init(&hub->event_list);
1114e8054854SAlan Stern 	hub->disconnected = 1;
1115e8054854SAlan Stern 	spin_unlock_irq(&hub_event_lock);
11161da177e4SLinus Torvalds 
11177de18d8bSAlan Stern 	/* Disconnect all children and quiesce the hub */
11187de18d8bSAlan Stern 	hub->error = 0;
11194330354fSAlan Stern 	hub_quiesce(hub, HUB_DISCONNECT);
11207de18d8bSAlan Stern 
11218b28c752SAlan Stern 	usb_set_intfdata (intf, NULL);
11221da177e4SLinus Torvalds 
1123e8054854SAlan Stern 	if (hub->hdev->speed == USB_SPEED_HIGH)
11241da177e4SLinus Torvalds 		highspeed_hubs--;
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 	usb_free_urb(hub->urb);
11271da177e4SLinus Torvalds 	kfree(hub->descriptor);
11281da177e4SLinus Torvalds 	kfree(hub->status);
1129e8054854SAlan Stern 	usb_buffer_free(hub->hdev, sizeof(*hub->buffer), hub->buffer,
11301da177e4SLinus Torvalds 			hub->buffer_dma);
11311da177e4SLinus Torvalds 
1132e8054854SAlan Stern 	kref_put(&hub->kref, hub_release);
11331da177e4SLinus Torvalds }
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
11361da177e4SLinus Torvalds {
11371da177e4SLinus Torvalds 	struct usb_host_interface *desc;
11381da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint;
11391da177e4SLinus Torvalds 	struct usb_device *hdev;
11401da177e4SLinus Torvalds 	struct usb_hub *hub;
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	desc = intf->cur_altsetting;
11431da177e4SLinus Torvalds 	hdev = interface_to_usbdev(intf);
11441da177e4SLinus Torvalds 
114538f3ad5eSFelipe Balbi 	if (hdev->level == MAX_TOPO_LEVEL) {
1146b9cef6c3SWu Fengguang 		dev_err(&intf->dev,
1147b9cef6c3SWu Fengguang 			"Unsupported bus topology: hub nested too deep\n");
114838f3ad5eSFelipe Balbi 		return -E2BIG;
114938f3ad5eSFelipe Balbi 	}
115038f3ad5eSFelipe Balbi 
115189ccbdc9SDavid Brownell #ifdef	CONFIG_USB_OTG_BLACKLIST_HUB
115289ccbdc9SDavid Brownell 	if (hdev->parent) {
115389ccbdc9SDavid Brownell 		dev_warn(&intf->dev, "ignoring external hub\n");
115489ccbdc9SDavid Brownell 		return -ENODEV;
115589ccbdc9SDavid Brownell 	}
115689ccbdc9SDavid Brownell #endif
115789ccbdc9SDavid Brownell 
11581da177e4SLinus Torvalds 	/* Some hubs have a subclass of 1, which AFAICT according to the */
11591da177e4SLinus Torvalds 	/*  specs is not defined, but it works */
11601da177e4SLinus Torvalds 	if ((desc->desc.bInterfaceSubClass != 0) &&
11611da177e4SLinus Torvalds 	    (desc->desc.bInterfaceSubClass != 1)) {
11621da177e4SLinus Torvalds descriptor_error:
11631da177e4SLinus Torvalds 		dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
11641da177e4SLinus Torvalds 		return -EIO;
11651da177e4SLinus Torvalds 	}
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	/* Multiple endpoints? What kind of mutant ninja-hub is this? */
11681da177e4SLinus Torvalds 	if (desc->desc.bNumEndpoints != 1)
11691da177e4SLinus Torvalds 		goto descriptor_error;
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	endpoint = &desc->endpoint[0].desc;
11721da177e4SLinus Torvalds 
1173fbf81c29SLuiz Fernando N. Capitulino 	/* If it's not an interrupt in endpoint, we'd better punt! */
1174fbf81c29SLuiz Fernando N. Capitulino 	if (!usb_endpoint_is_int_in(endpoint))
11751da177e4SLinus Torvalds 		goto descriptor_error;
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	/* We found a hub */
11781da177e4SLinus Torvalds 	dev_info (&intf->dev, "USB hub found\n");
11791da177e4SLinus Torvalds 
11800a1ef3b5SAlan Stern 	hub = kzalloc(sizeof(*hub), GFP_KERNEL);
11811da177e4SLinus Torvalds 	if (!hub) {
11821da177e4SLinus Torvalds 		dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
11831da177e4SLinus Torvalds 		return -ENOMEM;
11841da177e4SLinus Torvalds 	}
11851da177e4SLinus Torvalds 
1186e8054854SAlan Stern 	kref_init(&hub->kref);
11871da177e4SLinus Torvalds 	INIT_LIST_HEAD(&hub->event_list);
11881da177e4SLinus Torvalds 	hub->intfdev = &intf->dev;
11891da177e4SLinus Torvalds 	hub->hdev = hdev;
1190c4028958SDavid Howells 	INIT_DELAYED_WORK(&hub->leds, led_work);
11918520f380SAlan Stern 	INIT_DELAYED_WORK(&hub->init_work, NULL);
1192e8054854SAlan Stern 	usb_get_intf(intf);
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	usb_set_intfdata (intf, hub);
119540f122f3SAlan Stern 	intf->needs_remote_wakeup = 1;
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 	if (hdev->speed == USB_SPEED_HIGH)
11981da177e4SLinus Torvalds 		highspeed_hubs++;
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	if (hub_configure(hub, endpoint) >= 0)
12011da177e4SLinus Torvalds 		return 0;
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds 	hub_disconnect (intf);
12041da177e4SLinus Torvalds 	return -ENODEV;
12051da177e4SLinus Torvalds }
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds static int
12081da177e4SLinus Torvalds hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
12091da177e4SLinus Torvalds {
12101da177e4SLinus Torvalds 	struct usb_device *hdev = interface_to_usbdev (intf);
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 	/* assert ifno == 0 (part of hub spec) */
12131da177e4SLinus Torvalds 	switch (code) {
12141da177e4SLinus Torvalds 	case USBDEVFS_HUB_PORTINFO: {
12151da177e4SLinus Torvalds 		struct usbdevfs_hub_portinfo *info = user_data;
12161da177e4SLinus Torvalds 		int i;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
12191da177e4SLinus Torvalds 		if (hdev->devnum <= 0)
12201da177e4SLinus Torvalds 			info->nports = 0;
12211da177e4SLinus Torvalds 		else {
12221da177e4SLinus Torvalds 			info->nports = hdev->maxchild;
12231da177e4SLinus Torvalds 			for (i = 0; i < info->nports; i++) {
12241da177e4SLinus Torvalds 				if (hdev->children[i] == NULL)
12251da177e4SLinus Torvalds 					info->port[i] = 0;
12261da177e4SLinus Torvalds 				else
12271da177e4SLinus Torvalds 					info->port[i] =
12281da177e4SLinus Torvalds 						hdev->children[i]->devnum;
12291da177e4SLinus Torvalds 			}
12301da177e4SLinus Torvalds 		}
12311da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 		return info->nports + 1;
12341da177e4SLinus Torvalds 		}
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	default:
12371da177e4SLinus Torvalds 		return -ENOSYS;
12381da177e4SLinus Torvalds 	}
12391da177e4SLinus Torvalds }
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds static void recursively_mark_NOTATTACHED(struct usb_device *udev)
12431da177e4SLinus Torvalds {
12441da177e4SLinus Torvalds 	int i;
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	for (i = 0; i < udev->maxchild; ++i) {
12471da177e4SLinus Torvalds 		if (udev->children[i])
12481da177e4SLinus Torvalds 			recursively_mark_NOTATTACHED(udev->children[i]);
12491da177e4SLinus Torvalds 	}
125015123006SSarah Sharp 	if (udev->state == USB_STATE_SUSPENDED) {
1251ee49fb5dSAlan Stern 		udev->discon_suspended = 1;
125215123006SSarah Sharp 		udev->active_duration -= jiffies;
125315123006SSarah Sharp 	}
12541da177e4SLinus Torvalds 	udev->state = USB_STATE_NOTATTACHED;
12551da177e4SLinus Torvalds }
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds /**
12581da177e4SLinus Torvalds  * usb_set_device_state - change a device's current state (usbcore, hcds)
12591da177e4SLinus Torvalds  * @udev: pointer to device whose state should be changed
12601da177e4SLinus Torvalds  * @new_state: new state value to be stored
12611da177e4SLinus Torvalds  *
12621da177e4SLinus Torvalds  * udev->state is _not_ fully protected by the device lock.  Although
12631da177e4SLinus Torvalds  * most transitions are made only while holding the lock, the state can
12641da177e4SLinus Torvalds  * can change to USB_STATE_NOTATTACHED at almost any time.  This
12651da177e4SLinus Torvalds  * is so that devices can be marked as disconnected as soon as possible,
12661da177e4SLinus Torvalds  * without having to wait for any semaphores to be released.  As a result,
12671da177e4SLinus Torvalds  * all changes to any device's state must be protected by the
12681da177e4SLinus Torvalds  * device_state_lock spinlock.
12691da177e4SLinus Torvalds  *
12701da177e4SLinus Torvalds  * Once a device has been added to the device tree, all changes to its state
12711da177e4SLinus Torvalds  * should be made using this routine.  The state should _not_ be set directly.
12721da177e4SLinus Torvalds  *
12731da177e4SLinus Torvalds  * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
12741da177e4SLinus Torvalds  * Otherwise udev->state is set to new_state, and if new_state is
12751da177e4SLinus Torvalds  * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
12761da177e4SLinus Torvalds  * to USB_STATE_NOTATTACHED.
12771da177e4SLinus Torvalds  */
12781da177e4SLinus Torvalds void usb_set_device_state(struct usb_device *udev,
12791da177e4SLinus Torvalds 		enum usb_device_state new_state)
12801da177e4SLinus Torvalds {
12811da177e4SLinus Torvalds 	unsigned long flags;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	spin_lock_irqsave(&device_state_lock, flags);
12841da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED)
12851da177e4SLinus Torvalds 		;	/* do nothing */
1286b94dc6b5SDavid Brownell 	else if (new_state != USB_STATE_NOTATTACHED) {
1287fb669cc0SDavid Brownell 
1288fb669cc0SDavid Brownell 		/* root hub wakeup capabilities are managed out-of-band
1289fb669cc0SDavid Brownell 		 * and may involve silicon errata ... ignore them here.
1290fb669cc0SDavid Brownell 		 */
1291fb669cc0SDavid Brownell 		if (udev->parent) {
1292645daaabSAlan Stern 			if (udev->state == USB_STATE_SUSPENDED
1293645daaabSAlan Stern 					|| new_state == USB_STATE_SUSPENDED)
1294645daaabSAlan Stern 				;	/* No change to wakeup settings */
1295645daaabSAlan Stern 			else if (new_state == USB_STATE_CONFIGURED)
1296b94dc6b5SDavid Brownell 				device_init_wakeup(&udev->dev,
1297b94dc6b5SDavid Brownell 					(udev->actconfig->desc.bmAttributes
1298b94dc6b5SDavid Brownell 					 & USB_CONFIG_ATT_WAKEUP));
1299645daaabSAlan Stern 			else
1300b94dc6b5SDavid Brownell 				device_init_wakeup(&udev->dev, 0);
1301fb669cc0SDavid Brownell 		}
130215123006SSarah Sharp 		if (udev->state == USB_STATE_SUSPENDED &&
130315123006SSarah Sharp 			new_state != USB_STATE_SUSPENDED)
130415123006SSarah Sharp 			udev->active_duration -= jiffies;
130515123006SSarah Sharp 		else if (new_state == USB_STATE_SUSPENDED &&
130615123006SSarah Sharp 				udev->state != USB_STATE_SUSPENDED)
130715123006SSarah Sharp 			udev->active_duration += jiffies;
1308645daaabSAlan Stern 		udev->state = new_state;
1309b94dc6b5SDavid Brownell 	} else
13101da177e4SLinus Torvalds 		recursively_mark_NOTATTACHED(udev);
13111da177e4SLinus Torvalds 	spin_unlock_irqrestore(&device_state_lock, flags);
13121da177e4SLinus Torvalds }
13136da9c990SDavid Vrabel EXPORT_SYMBOL_GPL(usb_set_device_state);
13141da177e4SLinus Torvalds 
13158af548dcSInaky Perez-Gonzalez /*
13168af548dcSInaky Perez-Gonzalez  * WUSB devices are simple: they have no hubs behind, so the mapping
13178af548dcSInaky Perez-Gonzalez  * device <-> virtual port number becomes 1:1. Why? to simplify the
13188af548dcSInaky Perez-Gonzalez  * life of the device connection logic in
13198af548dcSInaky Perez-Gonzalez  * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
13208af548dcSInaky Perez-Gonzalez  * handshake we need to assign a temporary address in the unauthorized
13218af548dcSInaky Perez-Gonzalez  * space. For simplicity we use the first virtual port number found to
13228af548dcSInaky Perez-Gonzalez  * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
13238af548dcSInaky Perez-Gonzalez  * and that becomes it's address [X < 128] or its unauthorized address
13248af548dcSInaky Perez-Gonzalez  * [X | 0x80].
13258af548dcSInaky Perez-Gonzalez  *
13268af548dcSInaky Perez-Gonzalez  * We add 1 as an offset to the one-based USB-stack port number
13278af548dcSInaky Perez-Gonzalez  * (zero-based wusb virtual port index) for two reasons: (a) dev addr
13288af548dcSInaky Perez-Gonzalez  * 0 is reserved by USB for default address; (b) Linux's USB stack
13298af548dcSInaky Perez-Gonzalez  * uses always #1 for the root hub of the controller. So USB stack's
13308af548dcSInaky Perez-Gonzalez  * port #1, which is wusb virtual-port #0 has address #2.
13318af548dcSInaky Perez-Gonzalez  */
13321da177e4SLinus Torvalds static void choose_address(struct usb_device *udev)
13331da177e4SLinus Torvalds {
13341da177e4SLinus Torvalds 	int		devnum;
13351da177e4SLinus Torvalds 	struct usb_bus	*bus = udev->bus;
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 	/* If khubd ever becomes multithreaded, this will need a lock */
13388af548dcSInaky Perez-Gonzalez 	if (udev->wusb) {
13398af548dcSInaky Perez-Gonzalez 		devnum = udev->portnum + 1;
13408af548dcSInaky Perez-Gonzalez 		BUG_ON(test_bit(devnum, bus->devmap.devicemap));
13418af548dcSInaky Perez-Gonzalez 	} else {
13428af548dcSInaky Perez-Gonzalez 		/* Try to allocate the next devnum beginning at
13438af548dcSInaky Perez-Gonzalez 		 * bus->devnum_next. */
13441da177e4SLinus Torvalds 		devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
13451da177e4SLinus Torvalds 					    bus->devnum_next);
13461da177e4SLinus Torvalds 		if (devnum >= 128)
13478af548dcSInaky Perez-Gonzalez 			devnum = find_next_zero_bit(bus->devmap.devicemap,
13488af548dcSInaky Perez-Gonzalez 						    128, 1);
13491da177e4SLinus Torvalds 		bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
13508af548dcSInaky Perez-Gonzalez 	}
13511da177e4SLinus Torvalds 	if (devnum < 128) {
13521da177e4SLinus Torvalds 		set_bit(devnum, bus->devmap.devicemap);
13531da177e4SLinus Torvalds 		udev->devnum = devnum;
13541da177e4SLinus Torvalds 	}
13551da177e4SLinus Torvalds }
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds static void release_address(struct usb_device *udev)
13581da177e4SLinus Torvalds {
13591da177e4SLinus Torvalds 	if (udev->devnum > 0) {
13601da177e4SLinus Torvalds 		clear_bit(udev->devnum, udev->bus->devmap.devicemap);
13611da177e4SLinus Torvalds 		udev->devnum = -1;
13621da177e4SLinus Torvalds 	}
13631da177e4SLinus Torvalds }
13641da177e4SLinus Torvalds 
13654953d141SDavid Vrabel static void update_address(struct usb_device *udev, int devnum)
13664953d141SDavid Vrabel {
13674953d141SDavid Vrabel 	/* The address for a WUSB device is managed by wusbcore. */
13684953d141SDavid Vrabel 	if (!udev->wusb)
13694953d141SDavid Vrabel 		udev->devnum = devnum;
13704953d141SDavid Vrabel }
13714953d141SDavid Vrabel 
1372d5d4db70SAlan Stern #ifdef	CONFIG_USB_SUSPEND
1373d5d4db70SAlan Stern 
1374d5d4db70SAlan Stern static void usb_stop_pm(struct usb_device *udev)
1375d5d4db70SAlan Stern {
1376d5d4db70SAlan Stern 	/* Synchronize with the ksuspend thread to prevent any more
1377d5d4db70SAlan Stern 	 * autosuspend requests from being submitted, and decrement
1378d5d4db70SAlan Stern 	 * the parent's count of unsuspended children.
1379d5d4db70SAlan Stern 	 */
1380d5d4db70SAlan Stern 	usb_pm_lock(udev);
1381d5d4db70SAlan Stern 	if (udev->parent && !udev->discon_suspended)
1382d5d4db70SAlan Stern 		usb_autosuspend_device(udev->parent);
1383d5d4db70SAlan Stern 	usb_pm_unlock(udev);
1384d5d4db70SAlan Stern 
13859ac39f28SAlan Stern 	/* Stop any autosuspend or autoresume requests already submitted */
13869ac39f28SAlan Stern 	cancel_delayed_work_sync(&udev->autosuspend);
13879ac39f28SAlan Stern 	cancel_work_sync(&udev->autoresume);
1388d5d4db70SAlan Stern }
1389d5d4db70SAlan Stern 
1390d5d4db70SAlan Stern #else
1391d5d4db70SAlan Stern 
1392d5d4db70SAlan Stern static inline void usb_stop_pm(struct usb_device *udev)
1393d5d4db70SAlan Stern { }
1394d5d4db70SAlan Stern 
1395d5d4db70SAlan Stern #endif
1396d5d4db70SAlan Stern 
13971da177e4SLinus Torvalds /**
13981da177e4SLinus Torvalds  * usb_disconnect - disconnect a device (usbcore-internal)
13991da177e4SLinus Torvalds  * @pdev: pointer to device being disconnected
14001da177e4SLinus Torvalds  * Context: !in_interrupt ()
14011da177e4SLinus Torvalds  *
14021da177e4SLinus Torvalds  * Something got disconnected. Get rid of it and all of its children.
14031da177e4SLinus Torvalds  *
14041da177e4SLinus Torvalds  * If *pdev is a normal device then the parent hub must already be locked.
14051da177e4SLinus Torvalds  * If *pdev is a root hub then this routine will acquire the
14061da177e4SLinus Torvalds  * usb_bus_list_lock on behalf of the caller.
14071da177e4SLinus Torvalds  *
14081da177e4SLinus Torvalds  * Only hub drivers (including virtual root hub drivers for host
14091da177e4SLinus Torvalds  * controllers) should ever call this.
14101da177e4SLinus Torvalds  *
14111da177e4SLinus Torvalds  * This call is synchronous, and may not be used in an interrupt context.
14121da177e4SLinus Torvalds  */
14131da177e4SLinus Torvalds void usb_disconnect(struct usb_device **pdev)
14141da177e4SLinus Torvalds {
14151da177e4SLinus Torvalds 	struct usb_device	*udev = *pdev;
14161da177e4SLinus Torvalds 	int			i;
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 	if (!udev) {
1419441b62c1SHarvey Harrison 		pr_debug ("%s nodev\n", __func__);
14201da177e4SLinus Torvalds 		return;
14211da177e4SLinus Torvalds 	}
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 	/* mark the device as inactive, so any further urb submissions for
14241da177e4SLinus Torvalds 	 * this device (and any of its children) will fail immediately.
14251da177e4SLinus Torvalds 	 * this quiesces everyting except pending urbs.
14261da177e4SLinus Torvalds 	 */
14271da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
14281da177e4SLinus Torvalds 	dev_info (&udev->dev, "USB disconnect, address %d\n", udev->devnum);
14291da177e4SLinus Torvalds 
14309ad3d6ccSAlan Stern 	usb_lock_device(udev);
14319ad3d6ccSAlan Stern 
14321da177e4SLinus Torvalds 	/* Free up all the children before we remove this device */
14331da177e4SLinus Torvalds 	for (i = 0; i < USB_MAXCHILDREN; i++) {
14341da177e4SLinus Torvalds 		if (udev->children[i])
14351da177e4SLinus Torvalds 			usb_disconnect(&udev->children[i]);
14361da177e4SLinus Torvalds 	}
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	/* deallocate hcd/hardware state ... nuking all pending urbs and
14391da177e4SLinus Torvalds 	 * cleaning up all state associated with the current configuration
14401da177e4SLinus Torvalds 	 * so that the hardware is now fully quiesced.
14411da177e4SLinus Torvalds 	 */
1442782da727SAlan Stern 	dev_dbg (&udev->dev, "unregistering device\n");
14431da177e4SLinus Torvalds 	usb_disable_device(udev, 0);
1444cde217a5SAlan Stern 	usb_hcd_synchronize_unlinks(udev);
14451da177e4SLinus Torvalds 
14463b23dd6fSAlan Stern 	usb_remove_ep_devs(&udev->ep0);
1447782da727SAlan Stern 	usb_unlock_device(udev);
14483099e75aSGreg Kroah-Hartman 
1449782da727SAlan Stern 	/* Unregister the device.  The device driver is responsible
14503b23dd6fSAlan Stern 	 * for de-configuring the device and invoking the remove-device
14513b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
1452782da727SAlan Stern 	 */
1453782da727SAlan Stern 	device_del(&udev->dev);
1454782da727SAlan Stern 
1455782da727SAlan Stern 	/* Free the device number and delete the parent's children[]
14561da177e4SLinus Torvalds 	 * (or root_hub) pointer.
14571da177e4SLinus Torvalds 	 */
14581da177e4SLinus Torvalds 	release_address(udev);
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds 	/* Avoid races with recursively_mark_NOTATTACHED() */
14611da177e4SLinus Torvalds 	spin_lock_irq(&device_state_lock);
14621da177e4SLinus Torvalds 	*pdev = NULL;
14631da177e4SLinus Torvalds 	spin_unlock_irq(&device_state_lock);
14641da177e4SLinus Torvalds 
1465d5d4db70SAlan Stern 	usb_stop_pm(udev);
1466ee49fb5dSAlan Stern 
1467782da727SAlan Stern 	put_device(&udev->dev);
14681da177e4SLinus Torvalds }
14691da177e4SLinus Torvalds 
1470f2a383e4SGreg Kroah-Hartman #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
14711da177e4SLinus Torvalds static void show_string(struct usb_device *udev, char *id, char *string)
14721da177e4SLinus Torvalds {
14731da177e4SLinus Torvalds 	if (!string)
14741da177e4SLinus Torvalds 		return;
14751da177e4SLinus Torvalds 	dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
14761da177e4SLinus Torvalds }
14771da177e4SLinus Torvalds 
1478f2a383e4SGreg Kroah-Hartman static void announce_device(struct usb_device *udev)
1479f2a383e4SGreg Kroah-Hartman {
1480f2a383e4SGreg Kroah-Hartman 	dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
1481f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idVendor),
1482f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idProduct));
1483b9cef6c3SWu Fengguang 	dev_info(&udev->dev,
1484b9cef6c3SWu Fengguang 		"New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
1485f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iManufacturer,
1486f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iProduct,
1487f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iSerialNumber);
1488f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Product", udev->product);
1489f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Manufacturer", udev->manufacturer);
1490f2a383e4SGreg Kroah-Hartman 	show_string(udev, "SerialNumber", udev->serial);
1491f2a383e4SGreg Kroah-Hartman }
14921da177e4SLinus Torvalds #else
1493f2a383e4SGreg Kroah-Hartman static inline void announce_device(struct usb_device *udev) { }
14941da177e4SLinus Torvalds #endif
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
14971da177e4SLinus Torvalds #include "otg_whitelist.h"
14981da177e4SLinus Torvalds #endif
14991da177e4SLinus Torvalds 
15003ede760fSOliver Neukum /**
1501d9d16e8aSInaky Perez-Gonzalez  * usb_configure_device_otg - FIXME (usbcore-internal)
15023ede760fSOliver Neukum  * @udev: newly addressed device (in ADDRESS state)
15033ede760fSOliver Neukum  *
1504d9d16e8aSInaky Perez-Gonzalez  * Do configuration for On-The-Go devices
15053ede760fSOliver Neukum  */
1506d9d16e8aSInaky Perez-Gonzalez static int usb_configure_device_otg(struct usb_device *udev)
15071da177e4SLinus Torvalds {
1508d9d16e8aSInaky Perez-Gonzalez 	int err = 0;
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
15111da177e4SLinus Torvalds 	/*
15121da177e4SLinus Torvalds 	 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
15131da177e4SLinus Torvalds 	 * to wake us after we've powered off VBUS; and HNP, switching roles
15141da177e4SLinus Torvalds 	 * "host" to "peripheral".  The OTG descriptor helps figure this out.
15151da177e4SLinus Torvalds 	 */
15161da177e4SLinus Torvalds 	if (!udev->bus->is_b_host
15171da177e4SLinus Torvalds 			&& udev->config
15181da177e4SLinus Torvalds 			&& udev->parent == udev->bus->root_hub) {
15191da177e4SLinus Torvalds 		struct usb_otg_descriptor	*desc = 0;
15201da177e4SLinus Torvalds 		struct usb_bus			*bus = udev->bus;
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 		/* descriptor may appear anywhere in config */
15231da177e4SLinus Torvalds 		if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
15241da177e4SLinus Torvalds 					le16_to_cpu(udev->config[0].desc.wTotalLength),
15251da177e4SLinus Torvalds 					USB_DT_OTG, (void **) &desc) == 0) {
15261da177e4SLinus Torvalds 			if (desc->bmAttributes & USB_OTG_HNP) {
152712c3da34SAlan Stern 				unsigned		port1 = udev->portnum;
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds 				dev_info(&udev->dev,
15301da177e4SLinus Torvalds 					"Dual-Role OTG device on %sHNP port\n",
15311da177e4SLinus Torvalds 					(port1 == bus->otg_port)
15321da177e4SLinus Torvalds 						? "" : "non-");
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 				/* enable HNP before suspend, it's simpler */
15351da177e4SLinus Torvalds 				if (port1 == bus->otg_port)
15361da177e4SLinus Torvalds 					bus->b_hnp_enable = 1;
15371da177e4SLinus Torvalds 				err = usb_control_msg(udev,
15381da177e4SLinus Torvalds 					usb_sndctrlpipe(udev, 0),
15391da177e4SLinus Torvalds 					USB_REQ_SET_FEATURE, 0,
15401da177e4SLinus Torvalds 					bus->b_hnp_enable
15411da177e4SLinus Torvalds 						? USB_DEVICE_B_HNP_ENABLE
15421da177e4SLinus Torvalds 						: USB_DEVICE_A_ALT_HNP_SUPPORT,
15431da177e4SLinus Torvalds 					0, NULL, 0, USB_CTRL_SET_TIMEOUT);
15441da177e4SLinus Torvalds 				if (err < 0) {
15451da177e4SLinus Torvalds 					/* OTG MESSAGE: report errors here,
15461da177e4SLinus Torvalds 					 * customize to match your product.
15471da177e4SLinus Torvalds 					 */
15481da177e4SLinus Torvalds 					dev_info(&udev->dev,
1549b9cef6c3SWu Fengguang 						"can't set HNP mode: %d\n",
15501da177e4SLinus Torvalds 						err);
15511da177e4SLinus Torvalds 					bus->b_hnp_enable = 0;
15521da177e4SLinus Torvalds 				}
15531da177e4SLinus Torvalds 			}
15541da177e4SLinus Torvalds 		}
15551da177e4SLinus Torvalds 	}
15561da177e4SLinus Torvalds 
15571da177e4SLinus Torvalds 	if (!is_targeted(udev)) {
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds 		/* Maybe it can talk to us, though we can't talk to it.
15601da177e4SLinus Torvalds 		 * (Includes HNP test device.)
15611da177e4SLinus Torvalds 		 */
15621da177e4SLinus Torvalds 		if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
1563634a84f8SDavid Brownell 			err = usb_port_suspend(udev, PMSG_SUSPEND);
15641da177e4SLinus Torvalds 			if (err < 0)
15651da177e4SLinus Torvalds 				dev_dbg(&udev->dev, "HNP fail, %d\n", err);
15661da177e4SLinus Torvalds 		}
1567ffcdc18dSVikram Pandita 		err = -ENOTSUPP;
15681da177e4SLinus Torvalds 		goto fail;
15691da177e4SLinus Torvalds 	}
1570d9d16e8aSInaky Perez-Gonzalez fail:
15711da177e4SLinus Torvalds #endif
1572d9d16e8aSInaky Perez-Gonzalez 	return err;
1573d9d16e8aSInaky Perez-Gonzalez }
15741da177e4SLinus Torvalds 
1575d9d16e8aSInaky Perez-Gonzalez 
1576d9d16e8aSInaky Perez-Gonzalez /**
1577d9d16e8aSInaky Perez-Gonzalez  * usb_configure_device - Detect and probe device intfs/otg (usbcore-internal)
1578d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
1579d9d16e8aSInaky Perez-Gonzalez  *
1580d9d16e8aSInaky Perez-Gonzalez  * This is only called by usb_new_device() and usb_authorize_device()
1581d9d16e8aSInaky Perez-Gonzalez  * and FIXME -- all comments that apply to them apply here wrt to
1582d9d16e8aSInaky Perez-Gonzalez  * environment.
1583d9d16e8aSInaky Perez-Gonzalez  *
1584d9d16e8aSInaky Perez-Gonzalez  * If the device is WUSB and not authorized, we don't attempt to read
1585d9d16e8aSInaky Perez-Gonzalez  * the string descriptors, as they will be errored out by the device
1586d9d16e8aSInaky Perez-Gonzalez  * until it has been authorized.
1587d9d16e8aSInaky Perez-Gonzalez  */
1588d9d16e8aSInaky Perez-Gonzalez static int usb_configure_device(struct usb_device *udev)
1589d9d16e8aSInaky Perez-Gonzalez {
1590d9d16e8aSInaky Perez-Gonzalez 	int err;
1591d9d16e8aSInaky Perez-Gonzalez 
1592d9d16e8aSInaky Perez-Gonzalez 	if (udev->config == NULL) {
1593d9d16e8aSInaky Perez-Gonzalez 		err = usb_get_configuration(udev);
1594d9d16e8aSInaky Perez-Gonzalez 		if (err < 0) {
1595d9d16e8aSInaky Perez-Gonzalez 			dev_err(&udev->dev, "can't read configurations, error %d\n",
1596d9d16e8aSInaky Perez-Gonzalez 				err);
1597d9d16e8aSInaky Perez-Gonzalez 			goto fail;
1598d9d16e8aSInaky Perez-Gonzalez 		}
1599d9d16e8aSInaky Perez-Gonzalez 	}
1600d9d16e8aSInaky Perez-Gonzalez 	if (udev->wusb == 1 && udev->authorized == 0) {
1601d9d16e8aSInaky Perez-Gonzalez 		udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1602d9d16e8aSInaky Perez-Gonzalez 		udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1603d9d16e8aSInaky Perez-Gonzalez 		udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1604d9d16e8aSInaky Perez-Gonzalez 	}
1605d9d16e8aSInaky Perez-Gonzalez 	else {
1606d9d16e8aSInaky Perez-Gonzalez 		/* read the standard strings and cache them if present */
1607d9d16e8aSInaky Perez-Gonzalez 		udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
1608d9d16e8aSInaky Perez-Gonzalez 		udev->manufacturer = usb_cache_string(udev,
1609d9d16e8aSInaky Perez-Gonzalez 						      udev->descriptor.iManufacturer);
1610d9d16e8aSInaky Perez-Gonzalez 		udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
1611d9d16e8aSInaky Perez-Gonzalez 	}
1612d9d16e8aSInaky Perez-Gonzalez 	err = usb_configure_device_otg(udev);
1613d9d16e8aSInaky Perez-Gonzalez fail:
1614d9d16e8aSInaky Perez-Gonzalez 	return err;
1615d9d16e8aSInaky Perez-Gonzalez }
1616d9d16e8aSInaky Perez-Gonzalez 
1617d9d16e8aSInaky Perez-Gonzalez 
1618d9d16e8aSInaky Perez-Gonzalez /**
1619d9d16e8aSInaky Perez-Gonzalez  * usb_new_device - perform initial device setup (usbcore-internal)
1620d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
1621d9d16e8aSInaky Perez-Gonzalez  *
1622d9d16e8aSInaky Perez-Gonzalez  * This is called with devices which have been enumerated, but not yet
1623d9d16e8aSInaky Perez-Gonzalez  * configured.  The device descriptor is available, but not descriptors
1624d9d16e8aSInaky Perez-Gonzalez  * for any device configuration.  The caller must have locked either
1625d9d16e8aSInaky Perez-Gonzalez  * the parent hub (if udev is a normal device) or else the
1626d9d16e8aSInaky Perez-Gonzalez  * usb_bus_list_lock (if udev is a root hub).  The parent's pointer to
1627d9d16e8aSInaky Perez-Gonzalez  * udev has already been installed, but udev is not yet visible through
1628d9d16e8aSInaky Perez-Gonzalez  * sysfs or other filesystem code.
1629d9d16e8aSInaky Perez-Gonzalez  *
1630d9d16e8aSInaky Perez-Gonzalez  * It will return if the device is configured properly or not.  Zero if
1631d9d16e8aSInaky Perez-Gonzalez  * the interface was registered with the driver core; else a negative
1632d9d16e8aSInaky Perez-Gonzalez  * errno value.
1633d9d16e8aSInaky Perez-Gonzalez  *
1634d9d16e8aSInaky Perez-Gonzalez  * This call is synchronous, and may not be used in an interrupt context.
1635d9d16e8aSInaky Perez-Gonzalez  *
1636d9d16e8aSInaky Perez-Gonzalez  * Only the hub driver or root-hub registrar should ever call this.
1637d9d16e8aSInaky Perez-Gonzalez  */
1638d9d16e8aSInaky Perez-Gonzalez int usb_new_device(struct usb_device *udev)
1639d9d16e8aSInaky Perez-Gonzalez {
1640d9d16e8aSInaky Perez-Gonzalez 	int err;
1641d9d16e8aSInaky Perez-Gonzalez 
16426cd13201SAlan Stern 	/* Increment the parent's count of unsuspended children */
16436cd13201SAlan Stern 	if (udev->parent)
16446cd13201SAlan Stern 		usb_autoresume_device(udev->parent);
16456cd13201SAlan Stern 
1646d9d16e8aSInaky Perez-Gonzalez 	usb_detect_quirks(udev);		/* Determine quirks */
1647d9d16e8aSInaky Perez-Gonzalez 	err = usb_configure_device(udev);	/* detect & probe dev/intfs */
1648d9d16e8aSInaky Perez-Gonzalez 	if (err < 0)
1649d9d16e8aSInaky Perez-Gonzalez 		goto fail;
16509f8b17e6SKay Sievers 	/* export the usbdev device-node for libusb */
16519f8b17e6SKay Sievers 	udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
16529f8b17e6SKay Sievers 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
16539f8b17e6SKay Sievers 
16546cd13201SAlan Stern 	/* Tell the world! */
16556cd13201SAlan Stern 	announce_device(udev);
1656195af2ccSAlan Stern 
1657782da727SAlan Stern 	/* Register the device.  The device driver is responsible
16583b23dd6fSAlan Stern 	 * for configuring the device and invoking the add-device
16593b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
1660782da727SAlan Stern 	 */
16611da177e4SLinus Torvalds 	err = device_add(&udev->dev);
16621da177e4SLinus Torvalds 	if (err) {
16631da177e4SLinus Torvalds 		dev_err(&udev->dev, "can't device_add, error %d\n", err);
16641da177e4SLinus Torvalds 		goto fail;
16651da177e4SLinus Torvalds 	}
16669ad3d6ccSAlan Stern 
16673b23dd6fSAlan Stern 	(void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
1668c066475eSGreg Kroah-Hartman 	return err;
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds fail:
16711da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
16726cd13201SAlan Stern 	usb_stop_pm(udev);
1673d9d16e8aSInaky Perez-Gonzalez 	return err;
16741da177e4SLinus Torvalds }
16751da177e4SLinus Torvalds 
167693993a0aSInaky Perez-Gonzalez 
167793993a0aSInaky Perez-Gonzalez /**
1678fd39c86bSRandy Dunlap  * usb_deauthorize_device - deauthorize a device (usbcore-internal)
1679fd39c86bSRandy Dunlap  * @usb_dev: USB device
1680fd39c86bSRandy Dunlap  *
1681fd39c86bSRandy Dunlap  * Move the USB device to a very basic state where interfaces are disabled
1682fd39c86bSRandy Dunlap  * and the device is in fact unconfigured and unusable.
168393993a0aSInaky Perez-Gonzalez  *
168493993a0aSInaky Perez-Gonzalez  * We share a lock (that we have) with device_del(), so we need to
168593993a0aSInaky Perez-Gonzalez  * defer its call.
168693993a0aSInaky Perez-Gonzalez  */
168793993a0aSInaky Perez-Gonzalez int usb_deauthorize_device(struct usb_device *usb_dev)
168893993a0aSInaky Perez-Gonzalez {
168993993a0aSInaky Perez-Gonzalez 	unsigned cnt;
169093993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
169193993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 0)
169293993a0aSInaky Perez-Gonzalez 		goto out_unauthorized;
169393993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 0;
169493993a0aSInaky Perez-Gonzalez 	usb_set_configuration(usb_dev, -1);
169593993a0aSInaky Perez-Gonzalez 	usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
169693993a0aSInaky Perez-Gonzalez 	usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
169793993a0aSInaky Perez-Gonzalez 	usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
169893993a0aSInaky Perez-Gonzalez 	kfree(usb_dev->config);
169993993a0aSInaky Perez-Gonzalez 	usb_dev->config = NULL;
170093993a0aSInaky Perez-Gonzalez 	for (cnt = 0; cnt < usb_dev->descriptor.bNumConfigurations; cnt++)
170193993a0aSInaky Perez-Gonzalez 		kfree(usb_dev->rawdescriptors[cnt]);
170293993a0aSInaky Perez-Gonzalez 	usb_dev->descriptor.bNumConfigurations = 0;
170393993a0aSInaky Perez-Gonzalez 	kfree(usb_dev->rawdescriptors);
170493993a0aSInaky Perez-Gonzalez out_unauthorized:
170593993a0aSInaky Perez-Gonzalez 	usb_unlock_device(usb_dev);
170693993a0aSInaky Perez-Gonzalez 	return 0;
170793993a0aSInaky Perez-Gonzalez }
170893993a0aSInaky Perez-Gonzalez 
170993993a0aSInaky Perez-Gonzalez 
171093993a0aSInaky Perez-Gonzalez int usb_authorize_device(struct usb_device *usb_dev)
171193993a0aSInaky Perez-Gonzalez {
171293993a0aSInaky Perez-Gonzalez 	int result = 0, c;
171393993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
171493993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 1)
171593993a0aSInaky Perez-Gonzalez 		goto out_authorized;
171693993a0aSInaky Perez-Gonzalez 	kfree(usb_dev->product);
171793993a0aSInaky Perez-Gonzalez 	usb_dev->product = NULL;
171893993a0aSInaky Perez-Gonzalez 	kfree(usb_dev->manufacturer);
171993993a0aSInaky Perez-Gonzalez 	usb_dev->manufacturer = NULL;
172093993a0aSInaky Perez-Gonzalez 	kfree(usb_dev->serial);
172193993a0aSInaky Perez-Gonzalez 	usb_dev->serial = NULL;
172293993a0aSInaky Perez-Gonzalez 	result = usb_autoresume_device(usb_dev);
172393993a0aSInaky Perez-Gonzalez 	if (result < 0) {
172493993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev,
172593993a0aSInaky Perez-Gonzalez 			"can't autoresume for authorization: %d\n", result);
172693993a0aSInaky Perez-Gonzalez 		goto error_autoresume;
172793993a0aSInaky Perez-Gonzalez 	}
172893993a0aSInaky Perez-Gonzalez 	result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
172993993a0aSInaky Perez-Gonzalez 	if (result < 0) {
173093993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev, "can't re-read device descriptor for "
173193993a0aSInaky Perez-Gonzalez 			"authorization: %d\n", result);
173293993a0aSInaky Perez-Gonzalez 		goto error_device_descriptor;
173393993a0aSInaky Perez-Gonzalez 	}
173493993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 1;
173593993a0aSInaky Perez-Gonzalez 	result = usb_configure_device(usb_dev);
173693993a0aSInaky Perez-Gonzalez 	if (result < 0)
173793993a0aSInaky Perez-Gonzalez 		goto error_configure;
173893993a0aSInaky Perez-Gonzalez 	/* Choose and set the configuration.  This registers the interfaces
173993993a0aSInaky Perez-Gonzalez 	 * with the driver core and lets interface drivers bind to them.
174093993a0aSInaky Perez-Gonzalez 	 */
1741b5ea060fSGreg Kroah-Hartman 	c = usb_choose_configuration(usb_dev);
174293993a0aSInaky Perez-Gonzalez 	if (c >= 0) {
174393993a0aSInaky Perez-Gonzalez 		result = usb_set_configuration(usb_dev, c);
174493993a0aSInaky Perez-Gonzalez 		if (result) {
174593993a0aSInaky Perez-Gonzalez 			dev_err(&usb_dev->dev,
174693993a0aSInaky Perez-Gonzalez 				"can't set config #%d, error %d\n", c, result);
174793993a0aSInaky Perez-Gonzalez 			/* This need not be fatal.  The user can try to
174893993a0aSInaky Perez-Gonzalez 			 * set other configurations. */
174993993a0aSInaky Perez-Gonzalez 		}
175093993a0aSInaky Perez-Gonzalez 	}
175193993a0aSInaky Perez-Gonzalez 	dev_info(&usb_dev->dev, "authorized to connect\n");
175293993a0aSInaky Perez-Gonzalez error_configure:
175393993a0aSInaky Perez-Gonzalez error_device_descriptor:
175493993a0aSInaky Perez-Gonzalez error_autoresume:
175593993a0aSInaky Perez-Gonzalez out_authorized:
175693993a0aSInaky Perez-Gonzalez 	usb_unlock_device(usb_dev);	// complements locktree
175793993a0aSInaky Perez-Gonzalez 	return result;
175893993a0aSInaky Perez-Gonzalez }
175993993a0aSInaky Perez-Gonzalez 
176093993a0aSInaky Perez-Gonzalez 
17610165de09SInaky Perez-Gonzalez /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
17620165de09SInaky Perez-Gonzalez static unsigned hub_is_wusb(struct usb_hub *hub)
17630165de09SInaky Perez-Gonzalez {
17640165de09SInaky Perez-Gonzalez 	struct usb_hcd *hcd;
17650165de09SInaky Perez-Gonzalez 	if (hub->hdev->parent != NULL)  /* not a root hub? */
17660165de09SInaky Perez-Gonzalez 		return 0;
17670165de09SInaky Perez-Gonzalez 	hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
17680165de09SInaky Perez-Gonzalez 	return hcd->wireless;
17690165de09SInaky Perez-Gonzalez }
17700165de09SInaky Perez-Gonzalez 
17710165de09SInaky Perez-Gonzalez 
17721da177e4SLinus Torvalds #define PORT_RESET_TRIES	5
17731da177e4SLinus Torvalds #define SET_ADDRESS_TRIES	2
17741da177e4SLinus Torvalds #define GET_DESCRIPTOR_TRIES	2
17751da177e4SLinus Torvalds #define SET_CONFIG_TRIES	(2 * (use_both_schemes + 1))
17761da177e4SLinus Torvalds #define USE_NEW_SCHEME(i)	((i) / 2 == old_scheme_first)
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds #define HUB_ROOT_RESET_TIME	50	/* times are in msec */
17791da177e4SLinus Torvalds #define HUB_SHORT_RESET_TIME	10
17801da177e4SLinus Torvalds #define HUB_LONG_RESET_TIME	200
17811da177e4SLinus Torvalds #define HUB_RESET_TIMEOUT	500
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds static int hub_port_wait_reset(struct usb_hub *hub, int port1,
17841da177e4SLinus Torvalds 				struct usb_device *udev, unsigned int delay)
17851da177e4SLinus Torvalds {
17861da177e4SLinus Torvalds 	int delay_time, ret;
17871da177e4SLinus Torvalds 	u16 portstatus;
17881da177e4SLinus Torvalds 	u16 portchange;
17891da177e4SLinus Torvalds 
17901da177e4SLinus Torvalds 	for (delay_time = 0;
17911da177e4SLinus Torvalds 			delay_time < HUB_RESET_TIMEOUT;
17921da177e4SLinus Torvalds 			delay_time += delay) {
17931da177e4SLinus Torvalds 		/* wait to give the device a chance to reset */
17941da177e4SLinus Torvalds 		msleep(delay);
17951da177e4SLinus Torvalds 
17961da177e4SLinus Torvalds 		/* read and decode port status */
17971da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
17981da177e4SLinus Torvalds 		if (ret < 0)
17991da177e4SLinus Torvalds 			return ret;
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds 		/* Device went away? */
18021da177e4SLinus Torvalds 		if (!(portstatus & USB_PORT_STAT_CONNECTION))
18031da177e4SLinus Torvalds 			return -ENOTCONN;
18041da177e4SLinus Torvalds 
1805dd4dd19eSAlan Stern 		/* bomb out completely if the connection bounced */
18061da177e4SLinus Torvalds 		if ((portchange & USB_PORT_STAT_C_CONNECTION))
1807dd4dd19eSAlan Stern 			return -ENOTCONN;
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds 		/* if we`ve finished resetting, then break out of the loop */
18101da177e4SLinus Torvalds 		if (!(portstatus & USB_PORT_STAT_RESET) &&
18111da177e4SLinus Torvalds 		    (portstatus & USB_PORT_STAT_ENABLE)) {
18120165de09SInaky Perez-Gonzalez 			if (hub_is_wusb(hub))
18130165de09SInaky Perez-Gonzalez 				udev->speed = USB_SPEED_VARIABLE;
18140165de09SInaky Perez-Gonzalez 			else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
18151da177e4SLinus Torvalds 				udev->speed = USB_SPEED_HIGH;
18161da177e4SLinus Torvalds 			else if (portstatus & USB_PORT_STAT_LOW_SPEED)
18171da177e4SLinus Torvalds 				udev->speed = USB_SPEED_LOW;
18181da177e4SLinus Torvalds 			else
18191da177e4SLinus Torvalds 				udev->speed = USB_SPEED_FULL;
18201da177e4SLinus Torvalds 			return 0;
18211da177e4SLinus Torvalds 		}
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds 		/* switch to the long delay after two short delay failures */
18241da177e4SLinus Torvalds 		if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
18251da177e4SLinus Torvalds 			delay = HUB_LONG_RESET_TIME;
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
18281da177e4SLinus Torvalds 			"port %d not reset yet, waiting %dms\n",
18291da177e4SLinus Torvalds 			port1, delay);
18301da177e4SLinus Torvalds 	}
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds 	return -EBUSY;
18331da177e4SLinus Torvalds }
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds static int hub_port_reset(struct usb_hub *hub, int port1,
18361da177e4SLinus Torvalds 				struct usb_device *udev, unsigned int delay)
18371da177e4SLinus Torvalds {
18381da177e4SLinus Torvalds 	int i, status;
18391da177e4SLinus Torvalds 
184032fe0198SAlan Stern 	/* Block EHCI CF initialization during the port reset.
184132fe0198SAlan Stern 	 * Some companion controllers don't like it when they mix.
184232fe0198SAlan Stern 	 */
184332fe0198SAlan Stern 	down_read(&ehci_cf_port_reset_rwsem);
184432fe0198SAlan Stern 
18451da177e4SLinus Torvalds 	/* Reset the port */
18461da177e4SLinus Torvalds 	for (i = 0; i < PORT_RESET_TRIES; i++) {
18471da177e4SLinus Torvalds 		status = set_port_feature(hub->hdev,
18481da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_RESET);
18491da177e4SLinus Torvalds 		if (status)
18501da177e4SLinus Torvalds 			dev_err(hub->intfdev,
18511da177e4SLinus Torvalds 					"cannot reset port %d (err = %d)\n",
18521da177e4SLinus Torvalds 					port1, status);
18531da177e4SLinus Torvalds 		else {
18541da177e4SLinus Torvalds 			status = hub_port_wait_reset(hub, port1, udev, delay);
1855dd16525bSDavid Brownell 			if (status && status != -ENOTCONN)
18561da177e4SLinus Torvalds 				dev_dbg(hub->intfdev,
18571da177e4SLinus Torvalds 						"port_wait_reset: err = %d\n",
18581da177e4SLinus Torvalds 						status);
18591da177e4SLinus Torvalds 		}
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 		/* return on disconnect or reset */
18621da177e4SLinus Torvalds 		switch (status) {
18631da177e4SLinus Torvalds 		case 0:
1864b789696aSDavid Brownell 			/* TRSTRCY = 10 ms; plus some extra */
1865b789696aSDavid Brownell 			msleep(10 + 40);
18664953d141SDavid Vrabel 			update_address(udev, 0);
18671da177e4SLinus Torvalds 			/* FALL THROUGH */
18681da177e4SLinus Torvalds 		case -ENOTCONN:
18691da177e4SLinus Torvalds 		case -ENODEV:
18701da177e4SLinus Torvalds 			clear_port_feature(hub->hdev,
18711da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_C_RESET);
18721da177e4SLinus Torvalds 			/* FIXME need disconnect() for NOTATTACHED device */
18731da177e4SLinus Torvalds 			usb_set_device_state(udev, status
18741da177e4SLinus Torvalds 					? USB_STATE_NOTATTACHED
18751da177e4SLinus Torvalds 					: USB_STATE_DEFAULT);
187632fe0198SAlan Stern 			goto done;
18771da177e4SLinus Torvalds 		}
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
18801da177e4SLinus Torvalds 			"port %d not enabled, trying reset again...\n",
18811da177e4SLinus Torvalds 			port1);
18821da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
18831da177e4SLinus Torvalds 	}
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds 	dev_err (hub->intfdev,
18861da177e4SLinus Torvalds 		"Cannot enable port %i.  Maybe the USB cable is bad?\n",
18871da177e4SLinus Torvalds 		port1);
18881da177e4SLinus Torvalds 
188932fe0198SAlan Stern  done:
189032fe0198SAlan Stern 	up_read(&ehci_cf_port_reset_rwsem);
18911da177e4SLinus Torvalds 	return status;
18921da177e4SLinus Torvalds }
18931da177e4SLinus Torvalds 
1894d388dab7SAlan Stern #ifdef	CONFIG_PM
18951da177e4SLinus Torvalds 
1896b01b03f3SAlan Stern #define MASK_BITS	(USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION | \
1897b01b03f3SAlan Stern 				USB_PORT_STAT_SUSPEND)
1898b01b03f3SAlan Stern #define WANT_BITS	(USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION)
1899b01b03f3SAlan Stern 
1900b01b03f3SAlan Stern /* Determine whether the device on a port is ready for a normal resume,
1901b01b03f3SAlan Stern  * is ready for a reset-resume, or should be disconnected.
1902b01b03f3SAlan Stern  */
1903b01b03f3SAlan Stern static int check_port_resume_type(struct usb_device *udev,
1904b01b03f3SAlan Stern 		struct usb_hub *hub, int port1,
1905b01b03f3SAlan Stern 		int status, unsigned portchange, unsigned portstatus)
1906b01b03f3SAlan Stern {
1907b01b03f3SAlan Stern 	/* Is the device still present? */
1908b01b03f3SAlan Stern 	if (status || (portstatus & MASK_BITS) != WANT_BITS) {
1909b01b03f3SAlan Stern 		if (status >= 0)
1910b01b03f3SAlan Stern 			status = -ENODEV;
1911b01b03f3SAlan Stern 	}
1912b01b03f3SAlan Stern 
191386c57edfSAlan Stern 	/* Can't do a normal resume if the port isn't enabled,
191486c57edfSAlan Stern 	 * so try a reset-resume instead.
191586c57edfSAlan Stern 	 */
191686c57edfSAlan Stern 	else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
191786c57edfSAlan Stern 		if (udev->persist_enabled)
191886c57edfSAlan Stern 			udev->reset_resume = 1;
191986c57edfSAlan Stern 		else
1920b01b03f3SAlan Stern 			status = -ENODEV;
192186c57edfSAlan Stern 	}
1922b01b03f3SAlan Stern 
1923b01b03f3SAlan Stern 	if (status) {
1924b01b03f3SAlan Stern 		dev_dbg(hub->intfdev,
1925b01b03f3SAlan Stern 				"port %d status %04x.%04x after resume, %d\n",
1926b01b03f3SAlan Stern 				port1, portchange, portstatus, status);
1927b01b03f3SAlan Stern 	} else if (udev->reset_resume) {
1928b01b03f3SAlan Stern 
1929b01b03f3SAlan Stern 		/* Late port handoff can set status-change bits */
1930b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION)
1931b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
1932b01b03f3SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
1933b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE)
1934b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
1935b01b03f3SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
1936b01b03f3SAlan Stern 	}
1937b01b03f3SAlan Stern 
1938b01b03f3SAlan Stern 	return status;
1939b01b03f3SAlan Stern }
1940b01b03f3SAlan Stern 
19411da177e4SLinus Torvalds #ifdef	CONFIG_USB_SUSPEND
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds /*
1944140d8f68SAlan Stern  * usb_port_suspend - suspend a usb device's upstream port
1945624d6c07SAlan Stern  * @udev: device that's no longer in active use, not a root hub
19465edbfb7cSDavid Brownell  * Context: must be able to sleep; device not locked; pm locks held
19471da177e4SLinus Torvalds  *
19481da177e4SLinus Torvalds  * Suspends a USB device that isn't in active use, conserving power.
19491da177e4SLinus Torvalds  * Devices may wake out of a suspend, if anything important happens,
19501da177e4SLinus Torvalds  * using the remote wakeup mechanism.  They may also be taken out of
1951140d8f68SAlan Stern  * suspend by the host, using usb_port_resume().  It's also routine
19521da177e4SLinus Torvalds  * to disconnect devices while they are suspended.
19531da177e4SLinus Torvalds  *
1954390a8c34SDavid Brownell  * This only affects the USB hardware for a device; its interfaces
1955390a8c34SDavid Brownell  * (and, for hubs, child devices) must already have been suspended.
1956390a8c34SDavid Brownell  *
1957624d6c07SAlan Stern  * Selective port suspend reduces power; most suspended devices draw
1958624d6c07SAlan Stern  * less than 500 uA.  It's also used in OTG, along with remote wakeup.
1959624d6c07SAlan Stern  * All devices below the suspended port are also suspended.
1960624d6c07SAlan Stern  *
1961624d6c07SAlan Stern  * Devices leave suspend state when the host wakes them up.  Some devices
1962624d6c07SAlan Stern  * also support "remote wakeup", where the device can activate the USB
1963624d6c07SAlan Stern  * tree above them to deliver data, such as a keypress or packet.  In
1964624d6c07SAlan Stern  * some cases, this wakes the USB host.
1965624d6c07SAlan Stern  *
19661da177e4SLinus Torvalds  * Suspending OTG devices may trigger HNP, if that's been enabled
19671da177e4SLinus Torvalds  * between a pair of dual-role devices.  That will change roles, such
19681da177e4SLinus Torvalds  * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
19691da177e4SLinus Torvalds  *
19704956eccdSAlan Stern  * Devices on USB hub ports have only one "suspend" state, corresponding
19714956eccdSAlan Stern  * to ACPI D2, "may cause the device to lose some context".
19724956eccdSAlan Stern  * State transitions include:
19734956eccdSAlan Stern  *
19744956eccdSAlan Stern  *   - suspend, resume ... when the VBUS power link stays live
19754956eccdSAlan Stern  *   - suspend, disconnect ... VBUS lost
19764956eccdSAlan Stern  *
19774956eccdSAlan Stern  * Once VBUS drop breaks the circuit, the port it's using has to go through
19784956eccdSAlan Stern  * normal re-enumeration procedures, starting with enabling VBUS power.
19794956eccdSAlan Stern  * Other than re-initializing the hub (plug/unplug, except for root hubs),
19804956eccdSAlan Stern  * Linux (2.6) currently has NO mechanisms to initiate that:  no khubd
19814956eccdSAlan Stern  * timer, no SRP, no requests through sysfs.
19824956eccdSAlan Stern  *
19834956eccdSAlan Stern  * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
19844956eccdSAlan Stern  * the root hub for their bus goes into global suspend ... so we don't
19854956eccdSAlan Stern  * (falsely) update the device power state to say it suspended.
19864956eccdSAlan Stern  *
19871da177e4SLinus Torvalds  * Returns 0 on success, else negative errno.
19881da177e4SLinus Torvalds  */
198965bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
19901da177e4SLinus Torvalds {
1991624d6c07SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
1992624d6c07SAlan Stern 	int		port1 = udev->portnum;
1993624d6c07SAlan Stern 	int		status;
19944956eccdSAlan Stern 
1995624d6c07SAlan Stern 	// dev_dbg(hub->intfdev, "suspend port %d\n", port1);
1996624d6c07SAlan Stern 
1997624d6c07SAlan Stern 	/* enable remote wakeup when appropriate; this lets the device
1998624d6c07SAlan Stern 	 * wake up the upstream hub (including maybe the root hub).
1999624d6c07SAlan Stern 	 *
2000624d6c07SAlan Stern 	 * NOTE:  OTG devices may issue remote wakeup (or SRP) even when
2001624d6c07SAlan Stern 	 * we don't explicitly enable it here.
2002624d6c07SAlan Stern 	 */
2003624d6c07SAlan Stern 	if (udev->do_remote_wakeup) {
2004624d6c07SAlan Stern 		status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2005624d6c07SAlan Stern 				USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2006624d6c07SAlan Stern 				USB_DEVICE_REMOTE_WAKEUP, 0,
2007624d6c07SAlan Stern 				NULL, 0,
2008624d6c07SAlan Stern 				USB_CTRL_SET_TIMEOUT);
2009624d6c07SAlan Stern 		if (status)
2010624d6c07SAlan Stern 			dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2011624d6c07SAlan Stern 					status);
2012624d6c07SAlan Stern 	}
2013624d6c07SAlan Stern 
2014624d6c07SAlan Stern 	/* see 7.1.7.6 */
2015624d6c07SAlan Stern 	status = set_port_feature(hub->hdev, port1, USB_PORT_FEAT_SUSPEND);
2016624d6c07SAlan Stern 	if (status) {
2017624d6c07SAlan Stern 		dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2018624d6c07SAlan Stern 				port1, status);
2019624d6c07SAlan Stern 		/* paranoia:  "should not happen" */
2020624d6c07SAlan Stern 		(void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2021624d6c07SAlan Stern 				USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2022624d6c07SAlan Stern 				USB_DEVICE_REMOTE_WAKEUP, 0,
2023624d6c07SAlan Stern 				NULL, 0,
2024624d6c07SAlan Stern 				USB_CTRL_SET_TIMEOUT);
2025624d6c07SAlan Stern 	} else {
2026624d6c07SAlan Stern 		/* device has up to 10 msec to fully suspend */
2027624d6c07SAlan Stern 		dev_dbg(&udev->dev, "usb %ssuspend\n",
202865bfd296SAlan Stern 				(msg.event & PM_EVENT_AUTO ? "auto-" : ""));
2029624d6c07SAlan Stern 		usb_set_device_state(udev, USB_STATE_SUSPENDED);
2030624d6c07SAlan Stern 		msleep(10);
2031624d6c07SAlan Stern 	}
20324956eccdSAlan Stern 	return status;
20331da177e4SLinus Torvalds }
2034f3f3253dSDavid Brownell 
20351da177e4SLinus Torvalds /*
2036390a8c34SDavid Brownell  * If the USB "suspend" state is in use (rather than "global suspend"),
2037390a8c34SDavid Brownell  * many devices will be individually taken out of suspend state using
203854515fe5SAlan Stern  * special "resume" signaling.  This routine kicks in shortly after
20391da177e4SLinus Torvalds  * hardware resume signaling is finished, either because of selective
20401da177e4SLinus Torvalds  * resume (by host) or remote wakeup (by device) ... now see what changed
20411da177e4SLinus Torvalds  * in the tree that's rooted at this device.
204254515fe5SAlan Stern  *
204354515fe5SAlan Stern  * If @udev->reset_resume is set then the device is reset before the
204454515fe5SAlan Stern  * status check is done.
20451da177e4SLinus Torvalds  */
2046140d8f68SAlan Stern static int finish_port_resume(struct usb_device *udev)
20471da177e4SLinus Torvalds {
204854515fe5SAlan Stern 	int	status = 0;
20491da177e4SLinus Torvalds 	u16	devstatus;
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds 	/* caller owns the udev device lock */
2052b9cef6c3SWu Fengguang 	dev_dbg(&udev->dev, "%s\n",
2053b9cef6c3SWu Fengguang 		udev->reset_resume ? "finish reset-resume" : "finish resume");
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 	/* usb ch9 identifies four variants of SUSPENDED, based on what
20561da177e4SLinus Torvalds 	 * state the device resumes to.  Linux currently won't see the
20571da177e4SLinus Torvalds 	 * first two on the host side; they'd be inside hub_port_init()
20581da177e4SLinus Torvalds 	 * during many timeouts, but khubd can't suspend until later.
20591da177e4SLinus Torvalds 	 */
20601da177e4SLinus Torvalds 	usb_set_device_state(udev, udev->actconfig
20611da177e4SLinus Torvalds 			? USB_STATE_CONFIGURED
20621da177e4SLinus Torvalds 			: USB_STATE_ADDRESS);
20631da177e4SLinus Torvalds 
206454515fe5SAlan Stern 	/* 10.5.4.5 says not to reset a suspended port if the attached
206554515fe5SAlan Stern 	 * device is enabled for remote wakeup.  Hence the reset
206654515fe5SAlan Stern 	 * operation is carried out here, after the port has been
206754515fe5SAlan Stern 	 * resumed.
206854515fe5SAlan Stern 	 */
206954515fe5SAlan Stern 	if (udev->reset_resume)
207086c57edfSAlan Stern  retry_reset_resume:
2071742120c6SMing Lei 		status = usb_reset_and_verify_device(udev);
207254515fe5SAlan Stern 
20731da177e4SLinus Torvalds  	/* 10.5.4.5 says be sure devices in the tree are still there.
20741da177e4SLinus Torvalds  	 * For now let's assume the device didn't go crazy on resume,
20751da177e4SLinus Torvalds 	 * and device drivers will know about any resume quirks.
20761da177e4SLinus Torvalds 	 */
207754515fe5SAlan Stern 	if (status == 0) {
207846dede46SAlan Stern 		devstatus = 0;
20791da177e4SLinus Torvalds 		status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2080b40b7a90SAlan Stern 		if (status >= 0)
208146dede46SAlan Stern 			status = (status > 0 ? 0 : -ENODEV);
208286c57edfSAlan Stern 
208386c57edfSAlan Stern 		/* If a normal resume failed, try doing a reset-resume */
208486c57edfSAlan Stern 		if (status && !udev->reset_resume && udev->persist_enabled) {
208586c57edfSAlan Stern 			dev_dbg(&udev->dev, "retry with reset-resume\n");
208686c57edfSAlan Stern 			udev->reset_resume = 1;
208786c57edfSAlan Stern 			goto retry_reset_resume;
208886c57edfSAlan Stern 		}
208954515fe5SAlan Stern 	}
2090b40b7a90SAlan Stern 
2091624d6c07SAlan Stern 	if (status) {
2092624d6c07SAlan Stern 		dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
20931da177e4SLinus Torvalds 				status);
2094624d6c07SAlan Stern 	} else if (udev->actconfig) {
20951da177e4SLinus Torvalds 		le16_to_cpus(&devstatus);
2096686314cfSAlan Stern 		if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
20971da177e4SLinus Torvalds 			status = usb_control_msg(udev,
20981da177e4SLinus Torvalds 					usb_sndctrlpipe(udev, 0),
20991da177e4SLinus Torvalds 					USB_REQ_CLEAR_FEATURE,
21001da177e4SLinus Torvalds 						USB_RECIP_DEVICE,
21011da177e4SLinus Torvalds 					USB_DEVICE_REMOTE_WAKEUP, 0,
21021da177e4SLinus Torvalds 					NULL, 0,
21031da177e4SLinus Torvalds 					USB_CTRL_SET_TIMEOUT);
2104a8e7c565SAlan Stern 			if (status)
2105b9cef6c3SWu Fengguang 				dev_dbg(&udev->dev,
2106b9cef6c3SWu Fengguang 					"disable remote wakeup, status %d\n",
2107b9cef6c3SWu Fengguang 					status);
21084bf0ba86SAlan Stern 		}
21091da177e4SLinus Torvalds 		status = 0;
21101da177e4SLinus Torvalds 	}
21111da177e4SLinus Torvalds 	return status;
21121da177e4SLinus Torvalds }
21131da177e4SLinus Torvalds 
2114624d6c07SAlan Stern /*
2115624d6c07SAlan Stern  * usb_port_resume - re-activate a suspended usb device's upstream port
2116624d6c07SAlan Stern  * @udev: device to re-activate, not a root hub
2117624d6c07SAlan Stern  * Context: must be able to sleep; device not locked; pm locks held
2118624d6c07SAlan Stern  *
2119624d6c07SAlan Stern  * This will re-activate the suspended device, increasing power usage
2120624d6c07SAlan Stern  * while letting drivers communicate again with its endpoints.
2121624d6c07SAlan Stern  * USB resume explicitly guarantees that the power session between
2122624d6c07SAlan Stern  * the host and the device is the same as it was when the device
2123624d6c07SAlan Stern  * suspended.
2124624d6c07SAlan Stern  *
2125feccc30dSAlan Stern  * If @udev->reset_resume is set then this routine won't check that the
2126feccc30dSAlan Stern  * port is still enabled.  Furthermore, finish_port_resume() above will
212754515fe5SAlan Stern  * reset @udev.  The end result is that a broken power session can be
212854515fe5SAlan Stern  * recovered and @udev will appear to persist across a loss of VBUS power.
212954515fe5SAlan Stern  *
213054515fe5SAlan Stern  * For example, if a host controller doesn't maintain VBUS suspend current
213154515fe5SAlan Stern  * during a system sleep or is reset when the system wakes up, all the USB
213254515fe5SAlan Stern  * power sessions below it will be broken.  This is especially troublesome
213354515fe5SAlan Stern  * for mass-storage devices containing mounted filesystems, since the
213454515fe5SAlan Stern  * device will appear to have disconnected and all the memory mappings
213554515fe5SAlan Stern  * to it will be lost.  Using the USB_PERSIST facility, the device can be
213654515fe5SAlan Stern  * made to appear as if it had not disconnected.
213754515fe5SAlan Stern  *
2138742120c6SMing Lei  * This facility can be dangerous.  Although usb_reset_and_verify_device() makes
2139feccc30dSAlan Stern  * every effort to insure that the same device is present after the
214054515fe5SAlan Stern  * reset as before, it cannot provide a 100% guarantee.  Furthermore it's
214154515fe5SAlan Stern  * quite possible for a device to remain unaltered but its media to be
214254515fe5SAlan Stern  * changed.  If the user replaces a flash memory card while the system is
214354515fe5SAlan Stern  * asleep, he will have only himself to blame when the filesystem on the
214454515fe5SAlan Stern  * new card is corrupted and the system crashes.
214554515fe5SAlan Stern  *
2146624d6c07SAlan Stern  * Returns 0 on success, else negative errno.
2147624d6c07SAlan Stern  */
214865bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
21491da177e4SLinus Torvalds {
2150624d6c07SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2151624d6c07SAlan Stern 	int		port1 = udev->portnum;
21521da177e4SLinus Torvalds 	int		status;
2153d25450c6SAlan Stern 	u16		portchange, portstatus;
2154d25450c6SAlan Stern 
2155d25450c6SAlan Stern 	/* Skip the initial Clear-Suspend step for a remote wakeup */
2156d25450c6SAlan Stern 	status = hub_port_status(hub, port1, &portstatus, &portchange);
2157d25450c6SAlan Stern 	if (status == 0 && !(portstatus & USB_PORT_STAT_SUSPEND))
2158d25450c6SAlan Stern 		goto SuspendCleared;
21591da177e4SLinus Torvalds 
21601da177e4SLinus Torvalds 	// dev_dbg(hub->intfdev, "resume port %d\n", port1);
21611da177e4SLinus Torvalds 
2162d5cbad4bSAlan Stern 	set_bit(port1, hub->busy_bits);
2163d5cbad4bSAlan Stern 
21641da177e4SLinus Torvalds 	/* see 7.1.7.7; affects power usage, but not budgeting */
21651da177e4SLinus Torvalds 	status = clear_port_feature(hub->hdev,
21661da177e4SLinus Torvalds 			port1, USB_PORT_FEAT_SUSPEND);
21671da177e4SLinus Torvalds 	if (status) {
2168624d6c07SAlan Stern 		dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
21691da177e4SLinus Torvalds 				port1, status);
21701da177e4SLinus Torvalds 	} else {
21711da177e4SLinus Torvalds 		/* drive resume for at least 20 msec */
2172645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n",
217365bfd296SAlan Stern 				(msg.event & PM_EVENT_AUTO ? "auto-" : ""));
21741da177e4SLinus Torvalds 		msleep(25);
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds 		/* Virtual root hubs can trigger on GET_PORT_STATUS to
21771da177e4SLinus Torvalds 		 * stop resume signaling.  Then finish the resume
21781da177e4SLinus Torvalds 		 * sequence.
21791da177e4SLinus Torvalds 		 */
2180d25450c6SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
218154515fe5SAlan Stern 
21821da177e4SLinus Torvalds 		/* TRSMRCY = 10 msec */
21831da177e4SLinus Torvalds 		msleep(10);
21841da177e4SLinus Torvalds 	}
2185b01b03f3SAlan Stern 
2186b01b03f3SAlan Stern  SuspendCleared:
2187b01b03f3SAlan Stern 	if (status == 0) {
2188b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_SUSPEND)
2189b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2190b01b03f3SAlan Stern 					USB_PORT_FEAT_C_SUSPEND);
21911da177e4SLinus Torvalds 	}
21921da177e4SLinus Torvalds 
2193d5cbad4bSAlan Stern 	clear_bit(port1, hub->busy_bits);
2194d5cbad4bSAlan Stern 
2195b01b03f3SAlan Stern 	status = check_port_resume_type(udev,
2196b01b03f3SAlan Stern 			hub, port1, status, portchange, portstatus);
219754515fe5SAlan Stern 	if (status == 0)
219854515fe5SAlan Stern 		status = finish_port_resume(udev);
219954515fe5SAlan Stern 	if (status < 0) {
220054515fe5SAlan Stern 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
220154515fe5SAlan Stern 		hub_port_logical_disconnect(hub, port1);
220254515fe5SAlan Stern 	}
22031da177e4SLinus Torvalds 	return status;
22041da177e4SLinus Torvalds }
22051da177e4SLinus Torvalds 
22068808f00cSAlan Stern /* caller has locked udev */
22071da177e4SLinus Torvalds static int remote_wakeup(struct usb_device *udev)
22081da177e4SLinus Torvalds {
22091da177e4SLinus Torvalds 	int	status = 0;
22101da177e4SLinus Torvalds 
22111da177e4SLinus Torvalds 	if (udev->state == USB_STATE_SUSPENDED) {
2212645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
22131941044aSAlan Stern 		usb_mark_last_busy(udev);
221465bfd296SAlan Stern 		status = usb_external_resume_device(udev, PMSG_REMOTE_RESUME);
2215d25450c6SAlan Stern 	}
22161da177e4SLinus Torvalds 	return status;
22171da177e4SLinus Torvalds }
22181da177e4SLinus Torvalds 
2219d388dab7SAlan Stern #else	/* CONFIG_USB_SUSPEND */
2220d388dab7SAlan Stern 
2221d388dab7SAlan Stern /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
2222d388dab7SAlan Stern 
222365bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
2224d388dab7SAlan Stern {
2225d388dab7SAlan Stern 	return 0;
2226d388dab7SAlan Stern }
2227d388dab7SAlan Stern 
2228b01b03f3SAlan Stern /* However we may need to do a reset-resume */
2229b01b03f3SAlan Stern 
223065bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
2231d388dab7SAlan Stern {
2232b01b03f3SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2233b01b03f3SAlan Stern 	int		port1 = udev->portnum;
2234b01b03f3SAlan Stern 	int		status;
2235b01b03f3SAlan Stern 	u16		portchange, portstatus;
223654515fe5SAlan Stern 
2237b01b03f3SAlan Stern 	status = hub_port_status(hub, port1, &portstatus, &portchange);
2238b01b03f3SAlan Stern 	status = check_port_resume_type(udev,
2239b01b03f3SAlan Stern 			hub, port1, status, portchange, portstatus);
2240b01b03f3SAlan Stern 
2241b01b03f3SAlan Stern 	if (status) {
2242b01b03f3SAlan Stern 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2243b01b03f3SAlan Stern 		hub_port_logical_disconnect(hub, port1);
2244b01b03f3SAlan Stern 	} else if (udev->reset_resume) {
224554515fe5SAlan Stern 		dev_dbg(&udev->dev, "reset-resume\n");
2246742120c6SMing Lei 		status = usb_reset_and_verify_device(udev);
224754515fe5SAlan Stern 	}
224854515fe5SAlan Stern 	return status;
2249d388dab7SAlan Stern }
2250d388dab7SAlan Stern 
2251d388dab7SAlan Stern static inline int remote_wakeup(struct usb_device *udev)
2252d388dab7SAlan Stern {
2253d388dab7SAlan Stern 	return 0;
2254d388dab7SAlan Stern }
2255d388dab7SAlan Stern 
2256d388dab7SAlan Stern #endif
2257d388dab7SAlan Stern 
2258db690874SDavid Brownell static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
22591da177e4SLinus Torvalds {
22601da177e4SLinus Torvalds 	struct usb_hub		*hub = usb_get_intfdata (intf);
22611da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
22621da177e4SLinus Torvalds 	unsigned		port1;
22631da177e4SLinus Torvalds 
2264c9f89fa4SDavid Brownell 	/* fail if children aren't already suspended */
22651da177e4SLinus Torvalds 	for (port1 = 1; port1 <= hdev->maxchild; port1++) {
22661da177e4SLinus Torvalds 		struct usb_device	*udev;
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds 		udev = hdev->children [port1-1];
22696840d255SAlan Stern 		if (udev && udev->can_submit) {
227065bfd296SAlan Stern 			if (!(msg.event & PM_EVENT_AUTO))
2271645daaabSAlan Stern 				dev_dbg(&intf->dev, "port %d nyet suspended\n",
2272645daaabSAlan Stern 						port1);
2273c9f89fa4SDavid Brownell 			return -EBUSY;
2274c9f89fa4SDavid Brownell 		}
22751da177e4SLinus Torvalds 	}
22761da177e4SLinus Torvalds 
2277441b62c1SHarvey Harrison 	dev_dbg(&intf->dev, "%s\n", __func__);
227840f122f3SAlan Stern 
2279c9f89fa4SDavid Brownell 	/* stop khubd and related activity */
22804330354fSAlan Stern 	hub_quiesce(hub, HUB_SUSPEND);
2281b6f6436dSAlan Stern 	return 0;
22821da177e4SLinus Torvalds }
22831da177e4SLinus Torvalds 
22841da177e4SLinus Torvalds static int hub_resume(struct usb_interface *intf)
22851da177e4SLinus Torvalds {
22861da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata(intf);
22871da177e4SLinus Torvalds 
22885e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
2289f2835219SAlan Stern 	hub_activate(hub, HUB_RESUME);
22901da177e4SLinus Torvalds 	return 0;
22911da177e4SLinus Torvalds }
22921da177e4SLinus Torvalds 
2293b41a60ecSAlan Stern static int hub_reset_resume(struct usb_interface *intf)
2294f07600cfSAlan Stern {
2295b41a60ecSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
2296f07600cfSAlan Stern 
22975e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
2298f2835219SAlan Stern 	hub_activate(hub, HUB_RESET_RESUME);
2299f07600cfSAlan Stern 	return 0;
2300f07600cfSAlan Stern }
2301f07600cfSAlan Stern 
230254515fe5SAlan Stern /**
230354515fe5SAlan Stern  * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
230454515fe5SAlan Stern  * @rhdev: struct usb_device for the root hub
230554515fe5SAlan Stern  *
230654515fe5SAlan Stern  * The USB host controller driver calls this function when its root hub
230754515fe5SAlan Stern  * is resumed and Vbus power has been interrupted or the controller
2308feccc30dSAlan Stern  * has been reset.  The routine marks @rhdev as having lost power.
2309feccc30dSAlan Stern  * When the hub driver is resumed it will take notice and carry out
2310feccc30dSAlan Stern  * power-session recovery for all the "USB-PERSIST"-enabled child devices;
2311feccc30dSAlan Stern  * the others will be disconnected.
231254515fe5SAlan Stern  */
231354515fe5SAlan Stern void usb_root_hub_lost_power(struct usb_device *rhdev)
231454515fe5SAlan Stern {
231554515fe5SAlan Stern 	dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
231654515fe5SAlan Stern 	rhdev->reset_resume = 1;
231754515fe5SAlan Stern }
231854515fe5SAlan Stern EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
231954515fe5SAlan Stern 
2320d388dab7SAlan Stern #else	/* CONFIG_PM */
2321d388dab7SAlan Stern 
2322d388dab7SAlan Stern static inline int remote_wakeup(struct usb_device *udev)
2323d388dab7SAlan Stern {
2324d388dab7SAlan Stern 	return 0;
2325d388dab7SAlan Stern }
2326d388dab7SAlan Stern 
2327511366daSAndrew Morton #define hub_suspend		NULL
2328511366daSAndrew Morton #define hub_resume		NULL
2329f07600cfSAlan Stern #define hub_reset_resume	NULL
2330d388dab7SAlan Stern #endif
2331d388dab7SAlan Stern 
23321da177e4SLinus Torvalds 
23331da177e4SLinus Torvalds /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
23341da177e4SLinus Torvalds  *
23351da177e4SLinus Torvalds  * Between connect detection and reset signaling there must be a delay
23361da177e4SLinus Torvalds  * of 100ms at least for debounce and power-settling.  The corresponding
23371da177e4SLinus Torvalds  * timer shall restart whenever the downstream port detects a disconnect.
23381da177e4SLinus Torvalds  *
23391da177e4SLinus Torvalds  * Apparently there are some bluetooth and irda-dongles and a number of
23401da177e4SLinus Torvalds  * low-speed devices for which this debounce period may last over a second.
23411da177e4SLinus Torvalds  * Not covered by the spec - but easy to deal with.
23421da177e4SLinus Torvalds  *
23431da177e4SLinus Torvalds  * This implementation uses a 1500ms total debounce timeout; if the
23441da177e4SLinus Torvalds  * connection isn't stable by then it returns -ETIMEDOUT.  It checks
23451da177e4SLinus Torvalds  * every 25ms for transient disconnects.  When the port status has been
23461da177e4SLinus Torvalds  * unchanged for 100ms it returns the port status.
23471da177e4SLinus Torvalds  */
23481da177e4SLinus Torvalds static int hub_port_debounce(struct usb_hub *hub, int port1)
23491da177e4SLinus Torvalds {
23501da177e4SLinus Torvalds 	int ret;
23511da177e4SLinus Torvalds 	int total_time, stable_time = 0;
23521da177e4SLinus Torvalds 	u16 portchange, portstatus;
23531da177e4SLinus Torvalds 	unsigned connection = 0xffff;
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds 	for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
23561da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
23571da177e4SLinus Torvalds 		if (ret < 0)
23581da177e4SLinus Torvalds 			return ret;
23591da177e4SLinus Torvalds 
23601da177e4SLinus Torvalds 		if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
23611da177e4SLinus Torvalds 		     (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
23621da177e4SLinus Torvalds 			stable_time += HUB_DEBOUNCE_STEP;
23631da177e4SLinus Torvalds 			if (stable_time >= HUB_DEBOUNCE_STABLE)
23641da177e4SLinus Torvalds 				break;
23651da177e4SLinus Torvalds 		} else {
23661da177e4SLinus Torvalds 			stable_time = 0;
23671da177e4SLinus Torvalds 			connection = portstatus & USB_PORT_STAT_CONNECTION;
23681da177e4SLinus Torvalds 		}
23691da177e4SLinus Torvalds 
23701da177e4SLinus Torvalds 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
23711da177e4SLinus Torvalds 			clear_port_feature(hub->hdev, port1,
23721da177e4SLinus Torvalds 					USB_PORT_FEAT_C_CONNECTION);
23731da177e4SLinus Torvalds 		}
23741da177e4SLinus Torvalds 
23751da177e4SLinus Torvalds 		if (total_time >= HUB_DEBOUNCE_TIMEOUT)
23761da177e4SLinus Torvalds 			break;
23771da177e4SLinus Torvalds 		msleep(HUB_DEBOUNCE_STEP);
23781da177e4SLinus Torvalds 	}
23791da177e4SLinus Torvalds 
23801da177e4SLinus Torvalds 	dev_dbg (hub->intfdev,
23811da177e4SLinus Torvalds 		"debounce: port %d: total %dms stable %dms status 0x%x\n",
23821da177e4SLinus Torvalds 		port1, total_time, stable_time, portstatus);
23831da177e4SLinus Torvalds 
23841da177e4SLinus Torvalds 	if (stable_time < HUB_DEBOUNCE_STABLE)
23851da177e4SLinus Torvalds 		return -ETIMEDOUT;
23861da177e4SLinus Torvalds 	return portstatus;
23871da177e4SLinus Torvalds }
23881da177e4SLinus Torvalds 
2389fc721f51SInaky Perez-Gonzalez void usb_ep0_reinit(struct usb_device *udev)
23901da177e4SLinus Torvalds {
2391ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
2392ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
23932caf7fcdSAlan Stern 	usb_enable_endpoint(udev, &udev->ep0, true);
23941da177e4SLinus Torvalds }
2395fc721f51SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_ep0_reinit);
23961da177e4SLinus Torvalds 
23971da177e4SLinus Torvalds #define usb_sndaddr0pipe()	(PIPE_CONTROL << 30)
23981da177e4SLinus Torvalds #define usb_rcvaddr0pipe()	((PIPE_CONTROL << 30) | USB_DIR_IN)
23991da177e4SLinus Torvalds 
24004326ed0bSAlan Stern static int hub_set_address(struct usb_device *udev, int devnum)
24011da177e4SLinus Torvalds {
24021da177e4SLinus Torvalds 	int retval;
24031da177e4SLinus Torvalds 
24044326ed0bSAlan Stern 	if (devnum <= 1)
24051da177e4SLinus Torvalds 		return -EINVAL;
24061da177e4SLinus Torvalds 	if (udev->state == USB_STATE_ADDRESS)
24071da177e4SLinus Torvalds 		return 0;
24081da177e4SLinus Torvalds 	if (udev->state != USB_STATE_DEFAULT)
24091da177e4SLinus Torvalds 		return -EINVAL;
24101da177e4SLinus Torvalds 	retval = usb_control_msg(udev, usb_sndaddr0pipe(),
24114326ed0bSAlan Stern 		USB_REQ_SET_ADDRESS, 0, devnum, 0,
24121da177e4SLinus Torvalds 		NULL, 0, USB_CTRL_SET_TIMEOUT);
24131da177e4SLinus Torvalds 	if (retval == 0) {
24144953d141SDavid Vrabel 		/* Device now using proper address. */
24154953d141SDavid Vrabel 		update_address(udev, devnum);
24161da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_ADDRESS);
2417fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
24181da177e4SLinus Torvalds 	}
24191da177e4SLinus Torvalds 	return retval;
24201da177e4SLinus Torvalds }
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds /* Reset device, (re)assign address, get device descriptor.
24231da177e4SLinus Torvalds  * Device connection must be stable, no more debouncing needed.
24241da177e4SLinus Torvalds  * Returns device in USB_STATE_ADDRESS, except on error.
24251da177e4SLinus Torvalds  *
24261da177e4SLinus Torvalds  * If this is called for an already-existing device (as part of
2427742120c6SMing Lei  * usb_reset_and_verify_device), the caller must own the device lock.  For a
24281da177e4SLinus Torvalds  * newly detected device that is not accessible through any global
24291da177e4SLinus Torvalds  * pointers, it's not necessary to lock the device.
24301da177e4SLinus Torvalds  */
24311da177e4SLinus Torvalds static int
24321da177e4SLinus Torvalds hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
24331da177e4SLinus Torvalds 		int retry_counter)
24341da177e4SLinus Torvalds {
24354186ecf8SArjan van de Ven 	static DEFINE_MUTEX(usb_address0_mutex);
24361da177e4SLinus Torvalds 
24371da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
24381da177e4SLinus Torvalds 	int			i, j, retval;
24391da177e4SLinus Torvalds 	unsigned		delay = HUB_SHORT_RESET_TIME;
24401da177e4SLinus Torvalds 	enum usb_device_speed	oldspeed = udev->speed;
244183a07196SInaky Perez-Gonzalez 	char 			*speed, *type;
24424326ed0bSAlan Stern 	int			devnum = udev->devnum;
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds 	/* root hub ports have a slightly longer reset period
24451da177e4SLinus Torvalds 	 * (from USB 2.0 spec, section 7.1.7.5)
24461da177e4SLinus Torvalds 	 */
24471da177e4SLinus Torvalds 	if (!hdev->parent) {
24481da177e4SLinus Torvalds 		delay = HUB_ROOT_RESET_TIME;
24491da177e4SLinus Torvalds 		if (port1 == hdev->bus->otg_port)
24501da177e4SLinus Torvalds 			hdev->bus->b_hnp_enable = 0;
24511da177e4SLinus Torvalds 	}
24521da177e4SLinus Torvalds 
24531da177e4SLinus Torvalds 	/* Some low speed devices have problems with the quick delay, so */
24541da177e4SLinus Torvalds 	/*  be a bit pessimistic with those devices. RHbug #23670 */
24551da177e4SLinus Torvalds 	if (oldspeed == USB_SPEED_LOW)
24561da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
24571da177e4SLinus Torvalds 
24584186ecf8SArjan van de Ven 	mutex_lock(&usb_address0_mutex);
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds 	/* Reset the device; full speed may morph to high speed */
24611da177e4SLinus Torvalds 	retval = hub_port_reset(hub, port1, udev, delay);
24621da177e4SLinus Torvalds 	if (retval < 0)		/* error or disconnect */
24631da177e4SLinus Torvalds 		goto fail;
24641da177e4SLinus Torvalds 				/* success, speed is known */
24651da177e4SLinus Torvalds 	retval = -ENODEV;
24661da177e4SLinus Torvalds 
24671da177e4SLinus Torvalds 	if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
24681da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset changed speed!\n");
24691da177e4SLinus Torvalds 		goto fail;
24701da177e4SLinus Torvalds 	}
24711da177e4SLinus Torvalds 	oldspeed = udev->speed;
24721da177e4SLinus Torvalds 
24731da177e4SLinus Torvalds 	/* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
24741da177e4SLinus Torvalds 	 * it's fixed size except for full speed devices.
24755bb6e0aeSInaky Perez-Gonzalez 	 * For Wireless USB devices, ep0 max packet is always 512 (tho
24765bb6e0aeSInaky Perez-Gonzalez 	 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
24771da177e4SLinus Torvalds 	 */
24781da177e4SLinus Torvalds 	switch (udev->speed) {
24796b403b02SSarah Sharp 	case USB_SPEED_SUPER:
24805bb6e0aeSInaky Perez-Gonzalez 	case USB_SPEED_VARIABLE:	/* fixed at 512 */
2481551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
24825bb6e0aeSInaky Perez-Gonzalez 		break;
24831da177e4SLinus Torvalds 	case USB_SPEED_HIGH:		/* fixed at 64 */
2484551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
24851da177e4SLinus Torvalds 		break;
24861da177e4SLinus Torvalds 	case USB_SPEED_FULL:		/* 8, 16, 32, or 64 */
24871da177e4SLinus Torvalds 		/* to determine the ep0 maxpacket size, try to read
24881da177e4SLinus Torvalds 		 * the device descriptor to get bMaxPacketSize0 and
24891da177e4SLinus Torvalds 		 * then correct our initial guess.
24901da177e4SLinus Torvalds 		 */
2491551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
24921da177e4SLinus Torvalds 		break;
24931da177e4SLinus Torvalds 	case USB_SPEED_LOW:		/* fixed at 8 */
2494551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
24951da177e4SLinus Torvalds 		break;
24961da177e4SLinus Torvalds 	default:
24971da177e4SLinus Torvalds 		goto fail;
24981da177e4SLinus Torvalds 	}
24991da177e4SLinus Torvalds 
250083a07196SInaky Perez-Gonzalez 	type = "";
250183a07196SInaky Perez-Gonzalez 	switch (udev->speed) {
25021da177e4SLinus Torvalds 	case USB_SPEED_LOW:	speed = "low";	break;
25031da177e4SLinus Torvalds 	case USB_SPEED_FULL:	speed = "full";	break;
25041da177e4SLinus Torvalds 	case USB_SPEED_HIGH:	speed = "high";	break;
25056b403b02SSarah Sharp 	case USB_SPEED_SUPER:
25066b403b02SSarah Sharp 				speed = "super";
25076b403b02SSarah Sharp 				break;
250883a07196SInaky Perez-Gonzalez 	case USB_SPEED_VARIABLE:
250983a07196SInaky Perez-Gonzalez 				speed = "variable";
251083a07196SInaky Perez-Gonzalez 				type = "Wireless ";
251183a07196SInaky Perez-Gonzalez 				break;
25121da177e4SLinus Torvalds 	default: 		speed = "?";	break;
251383a07196SInaky Perez-Gonzalez 	}
251483a07196SInaky Perez-Gonzalez 	dev_info (&udev->dev,
251583a07196SInaky Perez-Gonzalez 		  "%s %s speed %sUSB device using %s and address %d\n",
251683a07196SInaky Perez-Gonzalez 		  (udev->config) ? "reset" : "new", speed, type,
25174326ed0bSAlan Stern 		  udev->bus->controller->driver->name, devnum);
25181da177e4SLinus Torvalds 
25191da177e4SLinus Torvalds 	/* Set up TT records, if needed  */
25201da177e4SLinus Torvalds 	if (hdev->tt) {
25211da177e4SLinus Torvalds 		udev->tt = hdev->tt;
25221da177e4SLinus Torvalds 		udev->ttport = hdev->ttport;
25231da177e4SLinus Torvalds 	} else if (udev->speed != USB_SPEED_HIGH
25241da177e4SLinus Torvalds 			&& hdev->speed == USB_SPEED_HIGH) {
25251da177e4SLinus Torvalds 		udev->tt = &hub->tt;
25261da177e4SLinus Torvalds 		udev->ttport = port1;
25271da177e4SLinus Torvalds 	}
25281da177e4SLinus Torvalds 
25291da177e4SLinus Torvalds 	/* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
25301da177e4SLinus Torvalds 	 * Because device hardware and firmware is sometimes buggy in
25311da177e4SLinus Torvalds 	 * this area, and this is how Linux has done it for ages.
25321da177e4SLinus Torvalds 	 * Change it cautiously.
25331da177e4SLinus Torvalds 	 *
25341da177e4SLinus Torvalds 	 * NOTE:  If USE_NEW_SCHEME() is true we will start by issuing
25351da177e4SLinus Torvalds 	 * a 64-byte GET_DESCRIPTOR request.  This is what Windows does,
25361da177e4SLinus Torvalds 	 * so it may help with some non-standards-compliant devices.
25371da177e4SLinus Torvalds 	 * Otherwise we start with SET_ADDRESS and then try to read the
25381da177e4SLinus Torvalds 	 * first 8 bytes of the device descriptor to get the ep0 maxpacket
25391da177e4SLinus Torvalds 	 * value.
25401da177e4SLinus Torvalds 	 */
25411da177e4SLinus Torvalds 	for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
25421da177e4SLinus Torvalds 		if (USE_NEW_SCHEME(retry_counter)) {
25431da177e4SLinus Torvalds 			struct usb_device_descriptor *buf;
25441da177e4SLinus Torvalds 			int r = 0;
25451da177e4SLinus Torvalds 
25461da177e4SLinus Torvalds #define GET_DESCRIPTOR_BUFSIZE	64
25471da177e4SLinus Torvalds 			buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
25481da177e4SLinus Torvalds 			if (!buf) {
25491da177e4SLinus Torvalds 				retval = -ENOMEM;
25501da177e4SLinus Torvalds 				continue;
25511da177e4SLinus Torvalds 			}
25521da177e4SLinus Torvalds 
2553b89ee19aSAlan Stern 			/* Retry on all errors; some devices are flakey.
2554b89ee19aSAlan Stern 			 * 255 is for WUSB devices, we actually need to use
2555b89ee19aSAlan Stern 			 * 512 (WUSB1.0[4.8.1]).
25561da177e4SLinus Torvalds 			 */
25571da177e4SLinus Torvalds 			for (j = 0; j < 3; ++j) {
25581da177e4SLinus Torvalds 				buf->bMaxPacketSize0 = 0;
25591da177e4SLinus Torvalds 				r = usb_control_msg(udev, usb_rcvaddr0pipe(),
25601da177e4SLinus Torvalds 					USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
25611da177e4SLinus Torvalds 					USB_DT_DEVICE << 8, 0,
25621da177e4SLinus Torvalds 					buf, GET_DESCRIPTOR_BUFSIZE,
2563fd7c519dSJaroslav Kysela 					initial_descriptor_timeout);
25641da177e4SLinus Torvalds 				switch (buf->bMaxPacketSize0) {
25655bb6e0aeSInaky Perez-Gonzalez 				case 8: case 16: case 32: case 64: case 255:
25661da177e4SLinus Torvalds 					if (buf->bDescriptorType ==
25671da177e4SLinus Torvalds 							USB_DT_DEVICE) {
25681da177e4SLinus Torvalds 						r = 0;
25691da177e4SLinus Torvalds 						break;
25701da177e4SLinus Torvalds 					}
25711da177e4SLinus Torvalds 					/* FALL THROUGH */
25721da177e4SLinus Torvalds 				default:
25731da177e4SLinus Torvalds 					if (r == 0)
25741da177e4SLinus Torvalds 						r = -EPROTO;
25751da177e4SLinus Torvalds 					break;
25761da177e4SLinus Torvalds 				}
25771da177e4SLinus Torvalds 				if (r == 0)
25781da177e4SLinus Torvalds 					break;
25791da177e4SLinus Torvalds 			}
25801da177e4SLinus Torvalds 			udev->descriptor.bMaxPacketSize0 =
25811da177e4SLinus Torvalds 					buf->bMaxPacketSize0;
25821da177e4SLinus Torvalds 			kfree(buf);
25831da177e4SLinus Torvalds 
25841da177e4SLinus Torvalds 			retval = hub_port_reset(hub, port1, udev, delay);
25851da177e4SLinus Torvalds 			if (retval < 0)		/* error or disconnect */
25861da177e4SLinus Torvalds 				goto fail;
25871da177e4SLinus Torvalds 			if (oldspeed != udev->speed) {
25881da177e4SLinus Torvalds 				dev_dbg(&udev->dev,
25891da177e4SLinus Torvalds 					"device reset changed speed!\n");
25901da177e4SLinus Torvalds 				retval = -ENODEV;
25911da177e4SLinus Torvalds 				goto fail;
25921da177e4SLinus Torvalds 			}
25931da177e4SLinus Torvalds 			if (r) {
2594b9cef6c3SWu Fengguang 				dev_err(&udev->dev,
2595b9cef6c3SWu Fengguang 					"device descriptor read/64, error %d\n",
2596b9cef6c3SWu Fengguang 					r);
25971da177e4SLinus Torvalds 				retval = -EMSGSIZE;
25981da177e4SLinus Torvalds 				continue;
25991da177e4SLinus Torvalds 			}
26001da177e4SLinus Torvalds #undef GET_DESCRIPTOR_BUFSIZE
26011da177e4SLinus Torvalds 		}
26021da177e4SLinus Torvalds 
26036c529cdcSInaky Perez-Gonzalez  		/*
26046c529cdcSInaky Perez-Gonzalez  		 * If device is WUSB, we already assigned an
26056c529cdcSInaky Perez-Gonzalez  		 * unauthorized address in the Connect Ack sequence;
26066c529cdcSInaky Perez-Gonzalez  		 * authorization will assign the final address.
26076c529cdcSInaky Perez-Gonzalez  		 */
26086c529cdcSInaky Perez-Gonzalez  		if (udev->wusb == 0) {
26091da177e4SLinus Torvalds 			for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
26104326ed0bSAlan Stern 				retval = hub_set_address(udev, devnum);
26111da177e4SLinus Torvalds 				if (retval >= 0)
26121da177e4SLinus Torvalds 					break;
26131da177e4SLinus Torvalds 				msleep(200);
26141da177e4SLinus Torvalds 			}
26151da177e4SLinus Torvalds 			if (retval < 0) {
26161da177e4SLinus Torvalds 				dev_err(&udev->dev,
26171da177e4SLinus Torvalds 					"device not accepting address %d, error %d\n",
26184326ed0bSAlan Stern 					devnum, retval);
26191da177e4SLinus Torvalds 				goto fail;
26201da177e4SLinus Torvalds 			}
26211da177e4SLinus Torvalds 
26221da177e4SLinus Torvalds 			/* cope with hardware quirkiness:
26231da177e4SLinus Torvalds 			 *  - let SET_ADDRESS settle, some device hardware wants it
26241da177e4SLinus Torvalds 			 *  - read ep0 maxpacket even for high and low speed,
26251da177e4SLinus Torvalds 			 */
26261da177e4SLinus Torvalds 			msleep(10);
26271da177e4SLinus Torvalds 			if (USE_NEW_SCHEME(retry_counter))
26281da177e4SLinus Torvalds 				break;
26296c529cdcSInaky Perez-Gonzalez   		}
26301da177e4SLinus Torvalds 
26311da177e4SLinus Torvalds 		retval = usb_get_device_descriptor(udev, 8);
26321da177e4SLinus Torvalds 		if (retval < 8) {
2633b9cef6c3SWu Fengguang 			dev_err(&udev->dev,
2634b9cef6c3SWu Fengguang 					"device descriptor read/8, error %d\n",
2635b9cef6c3SWu Fengguang 					retval);
26361da177e4SLinus Torvalds 			if (retval >= 0)
26371da177e4SLinus Torvalds 				retval = -EMSGSIZE;
26381da177e4SLinus Torvalds 		} else {
26391da177e4SLinus Torvalds 			retval = 0;
26401da177e4SLinus Torvalds 			break;
26411da177e4SLinus Torvalds 		}
26421da177e4SLinus Torvalds 	}
26431da177e4SLinus Torvalds 	if (retval)
26441da177e4SLinus Torvalds 		goto fail;
26451da177e4SLinus Torvalds 
26466b403b02SSarah Sharp 	if (udev->descriptor.bMaxPacketSize0 == 0xff ||
26476b403b02SSarah Sharp 			udev->speed == USB_SPEED_SUPER)
26486b403b02SSarah Sharp 		i = 512;
26496b403b02SSarah Sharp 	else
26506b403b02SSarah Sharp 		i = udev->descriptor.bMaxPacketSize0;
26511da177e4SLinus Torvalds 	if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) {
26521da177e4SLinus Torvalds 		if (udev->speed != USB_SPEED_FULL ||
26531da177e4SLinus Torvalds 				!(i == 8 || i == 16 || i == 32 || i == 64)) {
26541da177e4SLinus Torvalds 			dev_err(&udev->dev, "ep0 maxpacket = %d\n", i);
26551da177e4SLinus Torvalds 			retval = -EMSGSIZE;
26561da177e4SLinus Torvalds 			goto fail;
26571da177e4SLinus Torvalds 		}
26581da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
26591da177e4SLinus Torvalds 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
2660fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
26611da177e4SLinus Torvalds 	}
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds 	retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
26641da177e4SLinus Torvalds 	if (retval < (signed)sizeof(udev->descriptor)) {
2665b9cef6c3SWu Fengguang 		dev_err(&udev->dev, "device descriptor read/all, error %d\n",
2666b9cef6c3SWu Fengguang 			retval);
26671da177e4SLinus Torvalds 		if (retval >= 0)
26681da177e4SLinus Torvalds 			retval = -ENOMSG;
26691da177e4SLinus Torvalds 		goto fail;
26701da177e4SLinus Torvalds 	}
26711da177e4SLinus Torvalds 
26721da177e4SLinus Torvalds 	retval = 0;
26731da177e4SLinus Torvalds 
26741da177e4SLinus Torvalds fail:
26754326ed0bSAlan Stern 	if (retval) {
26761da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 0);
26774953d141SDavid Vrabel 		update_address(udev, devnum);	/* for disconnect processing */
26784326ed0bSAlan Stern 	}
26794186ecf8SArjan van de Ven 	mutex_unlock(&usb_address0_mutex);
26801da177e4SLinus Torvalds 	return retval;
26811da177e4SLinus Torvalds }
26821da177e4SLinus Torvalds 
26831da177e4SLinus Torvalds static void
26841da177e4SLinus Torvalds check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
26851da177e4SLinus Torvalds {
26861da177e4SLinus Torvalds 	struct usb_qualifier_descriptor	*qual;
26871da177e4SLinus Torvalds 	int				status;
26881da177e4SLinus Torvalds 
2689e94b1766SChristoph Lameter 	qual = kmalloc (sizeof *qual, GFP_KERNEL);
26901da177e4SLinus Torvalds 	if (qual == NULL)
26911da177e4SLinus Torvalds 		return;
26921da177e4SLinus Torvalds 
26931da177e4SLinus Torvalds 	status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
26941da177e4SLinus Torvalds 			qual, sizeof *qual);
26951da177e4SLinus Torvalds 	if (status == sizeof *qual) {
26961da177e4SLinus Torvalds 		dev_info(&udev->dev, "not running at top speed; "
26971da177e4SLinus Torvalds 			"connect to a high speed hub\n");
26981da177e4SLinus Torvalds 		/* hub LEDs are probably harder to miss than syslog */
26991da177e4SLinus Torvalds 		if (hub->has_indicators) {
27001da177e4SLinus Torvalds 			hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
2701c4028958SDavid Howells 			schedule_delayed_work (&hub->leds, 0);
27021da177e4SLinus Torvalds 		}
27031da177e4SLinus Torvalds 	}
27041da177e4SLinus Torvalds 	kfree(qual);
27051da177e4SLinus Torvalds }
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds static unsigned
27081da177e4SLinus Torvalds hub_power_remaining (struct usb_hub *hub)
27091da177e4SLinus Torvalds {
27101da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
27111da177e4SLinus Torvalds 	int remaining;
271255c52718SAlan Stern 	int port1;
27131da177e4SLinus Torvalds 
271455c52718SAlan Stern 	if (!hub->limited_power)
27151da177e4SLinus Torvalds 		return 0;
27161da177e4SLinus Torvalds 
271755c52718SAlan Stern 	remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
271855c52718SAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
271955c52718SAlan Stern 		struct usb_device	*udev = hdev->children[port1 - 1];
272055c52718SAlan Stern 		int			delta;
27211da177e4SLinus Torvalds 
27221da177e4SLinus Torvalds 		if (!udev)
27231da177e4SLinus Torvalds 			continue;
27241da177e4SLinus Torvalds 
272555c52718SAlan Stern 		/* Unconfigured devices may not use more than 100mA,
272655c52718SAlan Stern 		 * or 8mA for OTG ports */
27271da177e4SLinus Torvalds 		if (udev->actconfig)
272855c52718SAlan Stern 			delta = udev->actconfig->desc.bMaxPower * 2;
272955c52718SAlan Stern 		else if (port1 != udev->bus->otg_port || hdev->parent)
273055c52718SAlan Stern 			delta = 100;
27311da177e4SLinus Torvalds 		else
273255c52718SAlan Stern 			delta = 8;
273355c52718SAlan Stern 		if (delta > hub->mA_per_port)
2734b9cef6c3SWu Fengguang 			dev_warn(&udev->dev,
2735b9cef6c3SWu Fengguang 				 "%dmA is over %umA budget for port %d!\n",
273655c52718SAlan Stern 				 delta, hub->mA_per_port, port1);
27371da177e4SLinus Torvalds 		remaining -= delta;
27381da177e4SLinus Torvalds 	}
27391da177e4SLinus Torvalds 	if (remaining < 0) {
274055c52718SAlan Stern 		dev_warn(hub->intfdev, "%dmA over power budget!\n",
274155c52718SAlan Stern 			- remaining);
27421da177e4SLinus Torvalds 		remaining = 0;
27431da177e4SLinus Torvalds 	}
27441da177e4SLinus Torvalds 	return remaining;
27451da177e4SLinus Torvalds }
27461da177e4SLinus Torvalds 
27471da177e4SLinus Torvalds /* Handle physical or logical connection change events.
27481da177e4SLinus Torvalds  * This routine is called when:
27491da177e4SLinus Torvalds  * 	a port connection-change occurs;
27501da177e4SLinus Torvalds  *	a port enable-change occurs (often caused by EMI);
2751742120c6SMing Lei  *	usb_reset_and_verify_device() encounters changed descriptors (as from
27521da177e4SLinus Torvalds  *		a firmware download)
27531da177e4SLinus Torvalds  * caller already locked the hub
27541da177e4SLinus Torvalds  */
27551da177e4SLinus Torvalds static void hub_port_connect_change(struct usb_hub *hub, int port1,
27561da177e4SLinus Torvalds 					u16 portstatus, u16 portchange)
27571da177e4SLinus Torvalds {
27581da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
27591da177e4SLinus Torvalds 	struct device *hub_dev = hub->intfdev;
276090da096eSBalaji Rao 	struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
276124618b0cSAlan Stern 	unsigned wHubCharacteristics =
276224618b0cSAlan Stern 			le16_to_cpu(hub->descriptor->wHubCharacteristics);
27638808f00cSAlan Stern 	struct usb_device *udev;
27641da177e4SLinus Torvalds 	int status, i;
27651da177e4SLinus Torvalds 
27661da177e4SLinus Torvalds 	dev_dbg (hub_dev,
27671da177e4SLinus Torvalds 		"port %d, status %04x, change %04x, %s\n",
27681da177e4SLinus Torvalds 		port1, portstatus, portchange, portspeed (portstatus));
27691da177e4SLinus Torvalds 
27701da177e4SLinus Torvalds 	if (hub->has_indicators) {
27711da177e4SLinus Torvalds 		set_port_led(hub, port1, HUB_LED_AUTO);
27721da177e4SLinus Torvalds 		hub->indicator[port1-1] = INDICATOR_AUTO;
27731da177e4SLinus Torvalds 	}
27741da177e4SLinus Torvalds 
27751da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
27761da177e4SLinus Torvalds 	/* during HNP, don't repeat the debounce */
27771da177e4SLinus Torvalds 	if (hdev->bus->is_b_host)
277824618b0cSAlan Stern 		portchange &= ~(USB_PORT_STAT_C_CONNECTION |
277924618b0cSAlan Stern 				USB_PORT_STAT_C_ENABLE);
27801da177e4SLinus Torvalds #endif
27811da177e4SLinus Torvalds 
27828808f00cSAlan Stern 	/* Try to resuscitate an existing device */
27838808f00cSAlan Stern 	udev = hdev->children[port1-1];
27848808f00cSAlan Stern 	if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
27858808f00cSAlan Stern 			udev->state != USB_STATE_NOTATTACHED) {
27868808f00cSAlan Stern 		usb_lock_device(udev);
27878808f00cSAlan Stern 		if (portstatus & USB_PORT_STAT_ENABLE) {
27888808f00cSAlan Stern 			status = 0;		/* Nothing to do */
27898808f00cSAlan Stern 
27908808f00cSAlan Stern #ifdef CONFIG_USB_SUSPEND
27915257d97aSAlan Stern 		} else if (udev->state == USB_STATE_SUSPENDED &&
27925257d97aSAlan Stern 				udev->persist_enabled) {
27938808f00cSAlan Stern 			/* For a suspended device, treat this as a
27948808f00cSAlan Stern 			 * remote wakeup event.
27958808f00cSAlan Stern 			 */
27968808f00cSAlan Stern 			if (udev->do_remote_wakeup)
27978808f00cSAlan Stern 				status = remote_wakeup(udev);
27988808f00cSAlan Stern 
27998808f00cSAlan Stern 			/* Otherwise leave it be; devices can't tell the
28008808f00cSAlan Stern 			 * difference between suspended and disabled.
28018808f00cSAlan Stern 			 */
28028808f00cSAlan Stern 			else
28038808f00cSAlan Stern 				status = 0;
28048808f00cSAlan Stern #endif
28058808f00cSAlan Stern 
28068808f00cSAlan Stern 		} else {
28075257d97aSAlan Stern 			status = -ENODEV;	/* Don't resuscitate */
28088808f00cSAlan Stern 		}
28098808f00cSAlan Stern 		usb_unlock_device(udev);
28108808f00cSAlan Stern 
28118808f00cSAlan Stern 		if (status == 0) {
28128808f00cSAlan Stern 			clear_bit(port1, hub->change_bits);
28138808f00cSAlan Stern 			return;
28148808f00cSAlan Stern 		}
28158808f00cSAlan Stern 	}
28168808f00cSAlan Stern 
281724618b0cSAlan Stern 	/* Disconnect any existing devices under this port */
28188808f00cSAlan Stern 	if (udev)
281924618b0cSAlan Stern 		usb_disconnect(&hdev->children[port1-1]);
282024618b0cSAlan Stern 	clear_bit(port1, hub->change_bits);
282124618b0cSAlan Stern 
28225257d97aSAlan Stern 	if (portchange & (USB_PORT_STAT_C_CONNECTION |
28235257d97aSAlan Stern 				USB_PORT_STAT_C_ENABLE)) {
28245257d97aSAlan Stern 		status = hub_port_debounce(hub, port1);
28255257d97aSAlan Stern 		if (status < 0) {
28265257d97aSAlan Stern 			if (printk_ratelimit())
28275257d97aSAlan Stern 				dev_err(hub_dev, "connect-debounce failed, "
28285257d97aSAlan Stern 						"port %d disabled\n", port1);
28295257d97aSAlan Stern 			portstatus &= ~USB_PORT_STAT_CONNECTION;
28305257d97aSAlan Stern 		} else {
28315257d97aSAlan Stern 			portstatus = status;
28325257d97aSAlan Stern 		}
28335257d97aSAlan Stern 	}
28345257d97aSAlan Stern 
283524618b0cSAlan Stern 	/* Return now if debouncing failed or nothing is connected */
28361da177e4SLinus Torvalds 	if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
28371da177e4SLinus Torvalds 
28381da177e4SLinus Torvalds 		/* maybe switch power back on (e.g. root hub was reset) */
283974ad9bd2SGreg Kroah-Hartman 		if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
28401da177e4SLinus Torvalds 				&& !(portstatus & (1 << USB_PORT_FEAT_POWER)))
28411da177e4SLinus Torvalds 			set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
28421da177e4SLinus Torvalds 
28431da177e4SLinus Torvalds 		if (portstatus & USB_PORT_STAT_ENABLE)
28441da177e4SLinus Torvalds   			goto done;
28451da177e4SLinus Torvalds 		return;
28461da177e4SLinus Torvalds 	}
28471da177e4SLinus Torvalds 
28481da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; i++) {
28491da177e4SLinus Torvalds 
28501da177e4SLinus Torvalds 		/* reallocate for each attempt, since references
28511da177e4SLinus Torvalds 		 * to the previous one can escape in various ways
28521da177e4SLinus Torvalds 		 */
28531da177e4SLinus Torvalds 		udev = usb_alloc_dev(hdev, hdev->bus, port1);
28541da177e4SLinus Torvalds 		if (!udev) {
28551da177e4SLinus Torvalds 			dev_err (hub_dev,
28561da177e4SLinus Torvalds 				"couldn't allocate port %d usb_device\n",
28571da177e4SLinus Torvalds 				port1);
28581da177e4SLinus Torvalds 			goto done;
28591da177e4SLinus Torvalds 		}
28601da177e4SLinus Torvalds 
28611da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_POWERED);
28621da177e4SLinus Torvalds 		udev->speed = USB_SPEED_UNKNOWN;
286355c52718SAlan Stern  		udev->bus_mA = hub->mA_per_port;
2864b6956ffaSAlan Stern 		udev->level = hdev->level + 1;
28658af548dcSInaky Perez-Gonzalez 		udev->wusb = hub_is_wusb(hub);
28661da177e4SLinus Torvalds 
28671da177e4SLinus Torvalds 		/* set the address */
28681da177e4SLinus Torvalds 		choose_address(udev);
28691da177e4SLinus Torvalds 		if (udev->devnum <= 0) {
28701da177e4SLinus Torvalds 			status = -ENOTCONN;	/* Don't retry */
28711da177e4SLinus Torvalds 			goto loop;
28721da177e4SLinus Torvalds 		}
28731da177e4SLinus Torvalds 
28741da177e4SLinus Torvalds 		/* reset and get descriptor */
28751da177e4SLinus Torvalds 		status = hub_port_init(hub, udev, port1, i);
28761da177e4SLinus Torvalds 		if (status < 0)
28771da177e4SLinus Torvalds 			goto loop;
28781da177e4SLinus Torvalds 
28791da177e4SLinus Torvalds 		/* consecutive bus-powered hubs aren't reliable; they can
28801da177e4SLinus Torvalds 		 * violate the voltage drop budget.  if the new child has
28811da177e4SLinus Torvalds 		 * a "powered" LED, users should notice we didn't enable it
28821da177e4SLinus Torvalds 		 * (without reading syslog), even without per-port LEDs
28831da177e4SLinus Torvalds 		 * on the parent.
28841da177e4SLinus Torvalds 		 */
28851da177e4SLinus Torvalds 		if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
288655c52718SAlan Stern 				&& udev->bus_mA <= 100) {
28871da177e4SLinus Torvalds 			u16	devstat;
28881da177e4SLinus Torvalds 
28891da177e4SLinus Torvalds 			status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
28901da177e4SLinus Torvalds 					&devstat);
289155c52718SAlan Stern 			if (status < 2) {
28921da177e4SLinus Torvalds 				dev_dbg(&udev->dev, "get status %d ?\n", status);
28931da177e4SLinus Torvalds 				goto loop_disable;
28941da177e4SLinus Torvalds 			}
289555c52718SAlan Stern 			le16_to_cpus(&devstat);
28961da177e4SLinus Torvalds 			if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
28971da177e4SLinus Torvalds 				dev_err(&udev->dev,
28981da177e4SLinus Torvalds 					"can't connect bus-powered hub "
28991da177e4SLinus Torvalds 					"to this port\n");
29001da177e4SLinus Torvalds 				if (hub->has_indicators) {
29011da177e4SLinus Torvalds 					hub->indicator[port1-1] =
29021da177e4SLinus Torvalds 						INDICATOR_AMBER_BLINK;
2903c4028958SDavid Howells 					schedule_delayed_work (&hub->leds, 0);
29041da177e4SLinus Torvalds 				}
29051da177e4SLinus Torvalds 				status = -ENOTCONN;	/* Don't retry */
29061da177e4SLinus Torvalds 				goto loop_disable;
29071da177e4SLinus Torvalds 			}
29081da177e4SLinus Torvalds 		}
29091da177e4SLinus Torvalds 
29101da177e4SLinus Torvalds 		/* check for devices running slower than they could */
29111da177e4SLinus Torvalds 		if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
29121da177e4SLinus Torvalds 				&& udev->speed == USB_SPEED_FULL
29131da177e4SLinus Torvalds 				&& highspeed_hubs != 0)
29141da177e4SLinus Torvalds 			check_highspeed (hub, udev, port1);
29151da177e4SLinus Torvalds 
29161da177e4SLinus Torvalds 		/* Store the parent's children[] pointer.  At this point
29171da177e4SLinus Torvalds 		 * udev becomes globally accessible, although presumably
29181da177e4SLinus Torvalds 		 * no one will look at it until hdev is unlocked.
29191da177e4SLinus Torvalds 		 */
29201da177e4SLinus Torvalds 		status = 0;
29211da177e4SLinus Torvalds 
29221da177e4SLinus Torvalds 		/* We mustn't add new devices if the parent hub has
29231da177e4SLinus Torvalds 		 * been disconnected; we would race with the
29241da177e4SLinus Torvalds 		 * recursively_mark_NOTATTACHED() routine.
29251da177e4SLinus Torvalds 		 */
29261da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
29271da177e4SLinus Torvalds 		if (hdev->state == USB_STATE_NOTATTACHED)
29281da177e4SLinus Torvalds 			status = -ENOTCONN;
29291da177e4SLinus Torvalds 		else
29301da177e4SLinus Torvalds 			hdev->children[port1-1] = udev;
29311da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
29321da177e4SLinus Torvalds 
29331da177e4SLinus Torvalds 		/* Run it through the hoops (find a driver, etc) */
29341da177e4SLinus Torvalds 		if (!status) {
29351da177e4SLinus Torvalds 			status = usb_new_device(udev);
29361da177e4SLinus Torvalds 			if (status) {
29371da177e4SLinus Torvalds 				spin_lock_irq(&device_state_lock);
29381da177e4SLinus Torvalds 				hdev->children[port1-1] = NULL;
29391da177e4SLinus Torvalds 				spin_unlock_irq(&device_state_lock);
29401da177e4SLinus Torvalds 			}
29411da177e4SLinus Torvalds 		}
29421da177e4SLinus Torvalds 
29431da177e4SLinus Torvalds 		if (status)
29441da177e4SLinus Torvalds 			goto loop_disable;
29451da177e4SLinus Torvalds 
29461da177e4SLinus Torvalds 		status = hub_power_remaining(hub);
29471da177e4SLinus Torvalds 		if (status)
294855c52718SAlan Stern 			dev_dbg(hub_dev, "%dmA power budget left\n", status);
29491da177e4SLinus Torvalds 
29501da177e4SLinus Torvalds 		return;
29511da177e4SLinus Torvalds 
29521da177e4SLinus Torvalds loop_disable:
29531da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 1);
29541da177e4SLinus Torvalds loop:
2955fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
29561da177e4SLinus Torvalds 		release_address(udev);
29571da177e4SLinus Torvalds 		usb_put_dev(udev);
2958ffcdc18dSVikram Pandita 		if ((status == -ENOTCONN) || (status == -ENOTSUPP))
29591da177e4SLinus Torvalds 			break;
29601da177e4SLinus Torvalds 	}
29613a31155cSAlan Stern 	if (hub->hdev->parent ||
29623a31155cSAlan Stern 			!hcd->driver->port_handed_over ||
29633a31155cSAlan Stern 			!(hcd->driver->port_handed_over)(hcd, port1))
29643a31155cSAlan Stern 		dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
29653a31155cSAlan Stern 				port1);
29661da177e4SLinus Torvalds 
29671da177e4SLinus Torvalds done:
29681da177e4SLinus Torvalds 	hub_port_disable(hub, port1, 1);
296990da096eSBalaji Rao 	if (hcd->driver->relinquish_port && !hub->hdev->parent)
297090da096eSBalaji Rao 		hcd->driver->relinquish_port(hcd, port1);
29711da177e4SLinus Torvalds }
29721da177e4SLinus Torvalds 
29731da177e4SLinus Torvalds static void hub_events(void)
29741da177e4SLinus Torvalds {
29751da177e4SLinus Torvalds 	struct list_head *tmp;
29761da177e4SLinus Torvalds 	struct usb_device *hdev;
29771da177e4SLinus Torvalds 	struct usb_interface *intf;
29781da177e4SLinus Torvalds 	struct usb_hub *hub;
29791da177e4SLinus Torvalds 	struct device *hub_dev;
29801da177e4SLinus Torvalds 	u16 hubstatus;
29811da177e4SLinus Torvalds 	u16 hubchange;
29821da177e4SLinus Torvalds 	u16 portstatus;
29831da177e4SLinus Torvalds 	u16 portchange;
29841da177e4SLinus Torvalds 	int i, ret;
29851da177e4SLinus Torvalds 	int connect_change;
29861da177e4SLinus Torvalds 
29871da177e4SLinus Torvalds 	/*
29881da177e4SLinus Torvalds 	 *  We restart the list every time to avoid a deadlock with
29891da177e4SLinus Torvalds 	 * deleting hubs downstream from this one. This should be
29901da177e4SLinus Torvalds 	 * safe since we delete the hub from the event list.
29911da177e4SLinus Torvalds 	 * Not the most efficient, but avoids deadlocks.
29921da177e4SLinus Torvalds 	 */
29931da177e4SLinus Torvalds 	while (1) {
29941da177e4SLinus Torvalds 
29951da177e4SLinus Torvalds 		/* Grab the first entry at the beginning of the list */
29961da177e4SLinus Torvalds 		spin_lock_irq(&hub_event_lock);
29971da177e4SLinus Torvalds 		if (list_empty(&hub_event_list)) {
29981da177e4SLinus Torvalds 			spin_unlock_irq(&hub_event_lock);
29991da177e4SLinus Torvalds 			break;
30001da177e4SLinus Torvalds 		}
30011da177e4SLinus Torvalds 
30021da177e4SLinus Torvalds 		tmp = hub_event_list.next;
30031da177e4SLinus Torvalds 		list_del_init(tmp);
30041da177e4SLinus Torvalds 
30051da177e4SLinus Torvalds 		hub = list_entry(tmp, struct usb_hub, event_list);
3006e8054854SAlan Stern 		kref_get(&hub->kref);
3007e8054854SAlan Stern 		spin_unlock_irq(&hub_event_lock);
30081da177e4SLinus Torvalds 
3009e8054854SAlan Stern 		hdev = hub->hdev;
3010e8054854SAlan Stern 		hub_dev = hub->intfdev;
3011e8054854SAlan Stern 		intf = to_usb_interface(hub_dev);
301240f122f3SAlan Stern 		dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
30131da177e4SLinus Torvalds 				hdev->state, hub->descriptor
30141da177e4SLinus Torvalds 					? hub->descriptor->bNbrPorts
30151da177e4SLinus Torvalds 					: 0,
30161da177e4SLinus Torvalds 				/* NOTE: expects max 15 ports... */
30171da177e4SLinus Torvalds 				(u16) hub->change_bits[0],
301840f122f3SAlan Stern 				(u16) hub->event_bits[0]);
30191da177e4SLinus Torvalds 
30201da177e4SLinus Torvalds 		/* Lock the device, then check to see if we were
30211da177e4SLinus Torvalds 		 * disconnected while waiting for the lock to succeed. */
302206b84e8aSAlan Stern 		usb_lock_device(hdev);
3023e8054854SAlan Stern 		if (unlikely(hub->disconnected))
30241da177e4SLinus Torvalds 			goto loop;
30251da177e4SLinus Torvalds 
30261da177e4SLinus Torvalds 		/* If the hub has died, clean up after it */
30271da177e4SLinus Torvalds 		if (hdev->state == USB_STATE_NOTATTACHED) {
30287de18d8bSAlan Stern 			hub->error = -ENODEV;
30294330354fSAlan Stern 			hub_quiesce(hub, HUB_DISCONNECT);
30301da177e4SLinus Torvalds 			goto loop;
30311da177e4SLinus Torvalds 		}
30321da177e4SLinus Torvalds 
303340f122f3SAlan Stern 		/* Autoresume */
303440f122f3SAlan Stern 		ret = usb_autopm_get_interface(intf);
303540f122f3SAlan Stern 		if (ret) {
303640f122f3SAlan Stern 			dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
30371da177e4SLinus Torvalds 			goto loop;
303840f122f3SAlan Stern 		}
303940f122f3SAlan Stern 
304040f122f3SAlan Stern 		/* If this is an inactive hub, do nothing */
304140f122f3SAlan Stern 		if (hub->quiescing)
304240f122f3SAlan Stern 			goto loop_autopm;
30431da177e4SLinus Torvalds 
30441da177e4SLinus Torvalds 		if (hub->error) {
30451da177e4SLinus Torvalds 			dev_dbg (hub_dev, "resetting for error %d\n",
30461da177e4SLinus Torvalds 				hub->error);
30471da177e4SLinus Torvalds 
3048742120c6SMing Lei 			ret = usb_reset_device(hdev);
30491da177e4SLinus Torvalds 			if (ret) {
30501da177e4SLinus Torvalds 				dev_dbg (hub_dev,
30511da177e4SLinus Torvalds 					"error resetting hub: %d\n", ret);
305240f122f3SAlan Stern 				goto loop_autopm;
30531da177e4SLinus Torvalds 			}
30541da177e4SLinus Torvalds 
30551da177e4SLinus Torvalds 			hub->nerrors = 0;
30561da177e4SLinus Torvalds 			hub->error = 0;
30571da177e4SLinus Torvalds 		}
30581da177e4SLinus Torvalds 
30591da177e4SLinus Torvalds 		/* deal with port status changes */
30601da177e4SLinus Torvalds 		for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
30611da177e4SLinus Torvalds 			if (test_bit(i, hub->busy_bits))
30621da177e4SLinus Torvalds 				continue;
30631da177e4SLinus Torvalds 			connect_change = test_bit(i, hub->change_bits);
30641da177e4SLinus Torvalds 			if (!test_and_clear_bit(i, hub->event_bits) &&
30656ee0b270SAlan Stern 					!connect_change)
30661da177e4SLinus Torvalds 				continue;
30671da177e4SLinus Torvalds 
30681da177e4SLinus Torvalds 			ret = hub_port_status(hub, i,
30691da177e4SLinus Torvalds 					&portstatus, &portchange);
30701da177e4SLinus Torvalds 			if (ret < 0)
30711da177e4SLinus Torvalds 				continue;
30721da177e4SLinus Torvalds 
30731da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_CONNECTION) {
30741da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
30751da177e4SLinus Torvalds 					USB_PORT_FEAT_C_CONNECTION);
30761da177e4SLinus Torvalds 				connect_change = 1;
30771da177e4SLinus Torvalds 			}
30781da177e4SLinus Torvalds 
30791da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_ENABLE) {
30801da177e4SLinus Torvalds 				if (!connect_change)
30811da177e4SLinus Torvalds 					dev_dbg (hub_dev,
30821da177e4SLinus Torvalds 						"port %d enable change, "
30831da177e4SLinus Torvalds 						"status %08x\n",
30841da177e4SLinus Torvalds 						i, portstatus);
30851da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
30861da177e4SLinus Torvalds 					USB_PORT_FEAT_C_ENABLE);
30871da177e4SLinus Torvalds 
30881da177e4SLinus Torvalds 				/*
30891da177e4SLinus Torvalds 				 * EM interference sometimes causes badly
30901da177e4SLinus Torvalds 				 * shielded USB devices to be shutdown by
30911da177e4SLinus Torvalds 				 * the hub, this hack enables them again.
30921da177e4SLinus Torvalds 				 * Works at least with mouse driver.
30931da177e4SLinus Torvalds 				 */
30941da177e4SLinus Torvalds 				if (!(portstatus & USB_PORT_STAT_ENABLE)
30951da177e4SLinus Torvalds 				    && !connect_change
30961da177e4SLinus Torvalds 				    && hdev->children[i-1]) {
30971da177e4SLinus Torvalds 					dev_err (hub_dev,
30981da177e4SLinus Torvalds 					    "port %i "
30991da177e4SLinus Torvalds 					    "disabled by hub (EMI?), "
31001da177e4SLinus Torvalds 					    "re-enabling...\n",
31011da177e4SLinus Torvalds 						i);
31021da177e4SLinus Torvalds 					connect_change = 1;
31031da177e4SLinus Torvalds 				}
31041da177e4SLinus Torvalds 			}
31051da177e4SLinus Torvalds 
31061da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_SUSPEND) {
31078808f00cSAlan Stern 				struct usb_device *udev;
31088808f00cSAlan Stern 
31091da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
31101da177e4SLinus Torvalds 					USB_PORT_FEAT_C_SUSPEND);
31118808f00cSAlan Stern 				udev = hdev->children[i-1];
31128808f00cSAlan Stern 				if (udev) {
31138808f00cSAlan Stern 					usb_lock_device(udev);
31141da177e4SLinus Torvalds 					ret = remote_wakeup(hdev->
31151da177e4SLinus Torvalds 							children[i-1]);
31168808f00cSAlan Stern 					usb_unlock_device(udev);
31171da177e4SLinus Torvalds 					if (ret < 0)
31181da177e4SLinus Torvalds 						connect_change = 1;
31191da177e4SLinus Torvalds 				} else {
31201da177e4SLinus Torvalds 					ret = -ENODEV;
31211da177e4SLinus Torvalds 					hub_port_disable(hub, i, 1);
31221da177e4SLinus Torvalds 				}
31231da177e4SLinus Torvalds 				dev_dbg (hub_dev,
31241da177e4SLinus Torvalds 					"resume on port %d, status %d\n",
31251da177e4SLinus Torvalds 					i, ret);
31261da177e4SLinus Torvalds 			}
31271da177e4SLinus Torvalds 
31281da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
31291da177e4SLinus Torvalds 				dev_err (hub_dev,
31301da177e4SLinus Torvalds 					"over-current change on port %d\n",
31311da177e4SLinus Torvalds 					i);
31321da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
31331da177e4SLinus Torvalds 					USB_PORT_FEAT_C_OVER_CURRENT);
31348520f380SAlan Stern 				hub_power_on(hub, true);
31351da177e4SLinus Torvalds 			}
31361da177e4SLinus Torvalds 
31371da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_RESET) {
31381da177e4SLinus Torvalds 				dev_dbg (hub_dev,
31391da177e4SLinus Torvalds 					"reset change on port %d\n",
31401da177e4SLinus Torvalds 					i);
31411da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
31421da177e4SLinus Torvalds 					USB_PORT_FEAT_C_RESET);
31431da177e4SLinus Torvalds 			}
31441da177e4SLinus Torvalds 
31451da177e4SLinus Torvalds 			if (connect_change)
31461da177e4SLinus Torvalds 				hub_port_connect_change(hub, i,
31471da177e4SLinus Torvalds 						portstatus, portchange);
31481da177e4SLinus Torvalds 		} /* end for i */
31491da177e4SLinus Torvalds 
31501da177e4SLinus Torvalds 		/* deal with hub status changes */
31511da177e4SLinus Torvalds 		if (test_and_clear_bit(0, hub->event_bits) == 0)
31521da177e4SLinus Torvalds 			;	/* do nothing */
31531da177e4SLinus Torvalds 		else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
31541da177e4SLinus Torvalds 			dev_err (hub_dev, "get_hub_status failed\n");
31551da177e4SLinus Torvalds 		else {
31561da177e4SLinus Torvalds 			if (hubchange & HUB_CHANGE_LOCAL_POWER) {
31571da177e4SLinus Torvalds 				dev_dbg (hub_dev, "power change\n");
31581da177e4SLinus Torvalds 				clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
315955c52718SAlan Stern 				if (hubstatus & HUB_STATUS_LOCAL_POWER)
316055c52718SAlan Stern 					/* FIXME: Is this always true? */
316155c52718SAlan Stern 					hub->limited_power = 1;
3162403fae78Sjidong xiao 				else
3163403fae78Sjidong xiao 					hub->limited_power = 0;
31641da177e4SLinus Torvalds 			}
31651da177e4SLinus Torvalds 			if (hubchange & HUB_CHANGE_OVERCURRENT) {
31661da177e4SLinus Torvalds 				dev_dbg (hub_dev, "overcurrent change\n");
31671da177e4SLinus Torvalds 				msleep(500);	/* Cool down */
31681da177e4SLinus Torvalds 				clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
31698520f380SAlan Stern                         	hub_power_on(hub, true);
31701da177e4SLinus Torvalds 			}
31711da177e4SLinus Torvalds 		}
31721da177e4SLinus Torvalds 
317340f122f3SAlan Stern loop_autopm:
317440f122f3SAlan Stern 		/* Allow autosuspend if we're not going to run again */
317540f122f3SAlan Stern 		if (list_empty(&hub->event_list))
317640f122f3SAlan Stern 			usb_autopm_enable(intf);
31771da177e4SLinus Torvalds loop:
31781da177e4SLinus Torvalds 		usb_unlock_device(hdev);
3179e8054854SAlan Stern 		kref_put(&hub->kref, hub_release);
31801da177e4SLinus Torvalds 
31811da177e4SLinus Torvalds         } /* end while (1) */
31821da177e4SLinus Torvalds }
31831da177e4SLinus Torvalds 
31841da177e4SLinus Torvalds static int hub_thread(void *__unused)
31851da177e4SLinus Torvalds {
31863bb1af52SAlan Stern 	/* khubd needs to be freezable to avoid intefering with USB-PERSIST
31873bb1af52SAlan Stern 	 * port handover.  Otherwise it might see that a full-speed device
31883bb1af52SAlan Stern 	 * was gone before the EHCI controller had handed its port over to
31893bb1af52SAlan Stern 	 * the companion full-speed controller.
31903bb1af52SAlan Stern 	 */
319183144186SRafael J. Wysocki 	set_freezable();
31923bb1af52SAlan Stern 
31931da177e4SLinus Torvalds 	do {
31941da177e4SLinus Torvalds 		hub_events();
3195e42837bcSRafael J. Wysocki 		wait_event_freezable(khubd_wait,
31969c8d6178Sakpm@osdl.org 				!list_empty(&hub_event_list) ||
31979c8d6178Sakpm@osdl.org 				kthread_should_stop());
31989c8d6178Sakpm@osdl.org 	} while (!kthread_should_stop() || !list_empty(&hub_event_list));
31991da177e4SLinus Torvalds 
32001da177e4SLinus Torvalds 	pr_debug("%s: khubd exiting\n", usbcore_name);
32019c8d6178Sakpm@osdl.org 	return 0;
32021da177e4SLinus Torvalds }
32031da177e4SLinus Torvalds 
32041da177e4SLinus Torvalds static struct usb_device_id hub_id_table [] = {
32051da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
32061da177e4SLinus Torvalds       .bDeviceClass = USB_CLASS_HUB},
32071da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
32081da177e4SLinus Torvalds       .bInterfaceClass = USB_CLASS_HUB},
32091da177e4SLinus Torvalds     { }						/* Terminating entry */
32101da177e4SLinus Torvalds };
32111da177e4SLinus Torvalds 
32121da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, hub_id_table);
32131da177e4SLinus Torvalds 
32141da177e4SLinus Torvalds static struct usb_driver hub_driver = {
32151da177e4SLinus Torvalds 	.name =		"hub",
32161da177e4SLinus Torvalds 	.probe =	hub_probe,
32171da177e4SLinus Torvalds 	.disconnect =	hub_disconnect,
32181da177e4SLinus Torvalds 	.suspend =	hub_suspend,
32191da177e4SLinus Torvalds 	.resume =	hub_resume,
3220f07600cfSAlan Stern 	.reset_resume =	hub_reset_resume,
32217de18d8bSAlan Stern 	.pre_reset =	hub_pre_reset,
32227de18d8bSAlan Stern 	.post_reset =	hub_post_reset,
32231da177e4SLinus Torvalds 	.ioctl =	hub_ioctl,
32241da177e4SLinus Torvalds 	.id_table =	hub_id_table,
322540f122f3SAlan Stern 	.supports_autosuspend =	1,
32261da177e4SLinus Torvalds };
32271da177e4SLinus Torvalds 
32281da177e4SLinus Torvalds int usb_hub_init(void)
32291da177e4SLinus Torvalds {
32301da177e4SLinus Torvalds 	if (usb_register(&hub_driver) < 0) {
32311da177e4SLinus Torvalds 		printk(KERN_ERR "%s: can't register hub driver\n",
32321da177e4SLinus Torvalds 			usbcore_name);
32331da177e4SLinus Torvalds 		return -1;
32341da177e4SLinus Torvalds 	}
32351da177e4SLinus Torvalds 
32369c8d6178Sakpm@osdl.org 	khubd_task = kthread_run(hub_thread, NULL, "khubd");
32379c8d6178Sakpm@osdl.org 	if (!IS_ERR(khubd_task))
32381da177e4SLinus Torvalds 		return 0;
32391da177e4SLinus Torvalds 
32401da177e4SLinus Torvalds 	/* Fall through if kernel_thread failed */
32411da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
32421da177e4SLinus Torvalds 	printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
32431da177e4SLinus Torvalds 
32441da177e4SLinus Torvalds 	return -1;
32451da177e4SLinus Torvalds }
32461da177e4SLinus Torvalds 
32471da177e4SLinus Torvalds void usb_hub_cleanup(void)
32481da177e4SLinus Torvalds {
32499c8d6178Sakpm@osdl.org 	kthread_stop(khubd_task);
32501da177e4SLinus Torvalds 
32511da177e4SLinus Torvalds 	/*
32521da177e4SLinus Torvalds 	 * Hub resources are freed for us by usb_deregister. It calls
32531da177e4SLinus Torvalds 	 * usb_driver_purge on every device which in turn calls that
32541da177e4SLinus Torvalds 	 * devices disconnect function if it is using this driver.
32551da177e4SLinus Torvalds 	 * The hub_disconnect function takes care of releasing the
32561da177e4SLinus Torvalds 	 * individual hub resources. -greg
32571da177e4SLinus Torvalds 	 */
32581da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
32591da177e4SLinus Torvalds } /* usb_hub_cleanup() */
32601da177e4SLinus Torvalds 
3261eb764c4bSAlan Stern static int descriptors_changed(struct usb_device *udev,
3262eb764c4bSAlan Stern 		struct usb_device_descriptor *old_device_descriptor)
32631da177e4SLinus Torvalds {
3264eb764c4bSAlan Stern 	int		changed = 0;
32651da177e4SLinus Torvalds 	unsigned	index;
3266eb764c4bSAlan Stern 	unsigned	serial_len = 0;
3267eb764c4bSAlan Stern 	unsigned	len;
3268eb764c4bSAlan Stern 	unsigned	old_length;
3269eb764c4bSAlan Stern 	int		length;
3270eb764c4bSAlan Stern 	char		*buf;
32711da177e4SLinus Torvalds 
3272eb764c4bSAlan Stern 	if (memcmp(&udev->descriptor, old_device_descriptor,
3273eb764c4bSAlan Stern 			sizeof(*old_device_descriptor)) != 0)
3274eb764c4bSAlan Stern 		return 1;
3275eb764c4bSAlan Stern 
3276eb764c4bSAlan Stern 	/* Since the idVendor, idProduct, and bcdDevice values in the
3277eb764c4bSAlan Stern 	 * device descriptor haven't changed, we will assume the
3278eb764c4bSAlan Stern 	 * Manufacturer and Product strings haven't changed either.
3279eb764c4bSAlan Stern 	 * But the SerialNumber string could be different (e.g., a
3280eb764c4bSAlan Stern 	 * different flash card of the same brand).
3281eb764c4bSAlan Stern 	 */
3282eb764c4bSAlan Stern 	if (udev->serial)
3283eb764c4bSAlan Stern 		serial_len = strlen(udev->serial) + 1;
3284eb764c4bSAlan Stern 
3285eb764c4bSAlan Stern 	len = serial_len;
32861da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
3287eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
3288eb764c4bSAlan Stern 		len = max(len, old_length);
32891da177e4SLinus Torvalds 	}
3290eb764c4bSAlan Stern 
32910cc1a51fSOliver Neukum 	buf = kmalloc(len, GFP_NOIO);
32921da177e4SLinus Torvalds 	if (buf == NULL) {
32931da177e4SLinus Torvalds 		dev_err(&udev->dev, "no mem to re-read configs after reset\n");
32941da177e4SLinus Torvalds 		/* assume the worst */
32951da177e4SLinus Torvalds 		return 1;
32961da177e4SLinus Torvalds 	}
32971da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
3298eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
32991da177e4SLinus Torvalds 		length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
33001da177e4SLinus Torvalds 				old_length);
3301eb764c4bSAlan Stern 		if (length != old_length) {
33021da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d, error %d\n",
33031da177e4SLinus Torvalds 					index, length);
3304eb764c4bSAlan Stern 			changed = 1;
33051da177e4SLinus Torvalds 			break;
33061da177e4SLinus Torvalds 		}
33071da177e4SLinus Torvalds 		if (memcmp (buf, udev->rawdescriptors[index], old_length)
33081da177e4SLinus Torvalds 				!= 0) {
33091da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
3310eb764c4bSAlan Stern 				index,
3311eb764c4bSAlan Stern 				((struct usb_config_descriptor *) buf)->
3312eb764c4bSAlan Stern 					bConfigurationValue);
3313eb764c4bSAlan Stern 			changed = 1;
33141da177e4SLinus Torvalds 			break;
33151da177e4SLinus Torvalds 		}
33161da177e4SLinus Torvalds 	}
3317eb764c4bSAlan Stern 
3318eb764c4bSAlan Stern 	if (!changed && serial_len) {
3319eb764c4bSAlan Stern 		length = usb_string(udev, udev->descriptor.iSerialNumber,
3320eb764c4bSAlan Stern 				buf, serial_len);
3321eb764c4bSAlan Stern 		if (length + 1 != serial_len) {
3322eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string error %d\n",
3323eb764c4bSAlan Stern 					length);
3324eb764c4bSAlan Stern 			changed = 1;
3325eb764c4bSAlan Stern 		} else if (memcmp(buf, udev->serial, length) != 0) {
3326eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string changed\n");
3327eb764c4bSAlan Stern 			changed = 1;
3328eb764c4bSAlan Stern 		}
3329eb764c4bSAlan Stern 	}
3330eb764c4bSAlan Stern 
33311da177e4SLinus Torvalds 	kfree(buf);
3332eb764c4bSAlan Stern 	return changed;
33331da177e4SLinus Torvalds }
33341da177e4SLinus Torvalds 
33351da177e4SLinus Torvalds /**
3336742120c6SMing Lei  * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
33371da177e4SLinus Torvalds  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
33381da177e4SLinus Torvalds  *
333979efa097SAlan Stern  * WARNING - don't use this routine to reset a composite device
334079efa097SAlan Stern  * (one with multiple interfaces owned by separate drivers)!
3341742120c6SMing Lei  * Use usb_reset_device() instead.
33421da177e4SLinus Torvalds  *
33431da177e4SLinus Torvalds  * Do a port reset, reassign the device's address, and establish its
33441da177e4SLinus Torvalds  * former operating configuration.  If the reset fails, or the device's
33451da177e4SLinus Torvalds  * descriptors change from their values before the reset, or the original
33461da177e4SLinus Torvalds  * configuration and altsettings cannot be restored, a flag will be set
33471da177e4SLinus Torvalds  * telling khubd to pretend the device has been disconnected and then
33481da177e4SLinus Torvalds  * re-connected.  All drivers will be unbound, and the device will be
33491da177e4SLinus Torvalds  * re-enumerated and probed all over again.
33501da177e4SLinus Torvalds  *
33511da177e4SLinus Torvalds  * Returns 0 if the reset succeeded, -ENODEV if the device has been
33521da177e4SLinus Torvalds  * flagged for logical disconnection, or some other negative error code
33531da177e4SLinus Torvalds  * if the reset wasn't even attempted.
33541da177e4SLinus Torvalds  *
33551da177e4SLinus Torvalds  * The caller must own the device lock.  For example, it's safe to use
33561da177e4SLinus Torvalds  * this from a driver probe() routine after downloading new firmware.
33571da177e4SLinus Torvalds  * For calls that might not occur during probe(), drivers should lock
33581da177e4SLinus Torvalds  * the device using usb_lock_device_for_reset().
33596bc6cff5SAlan Stern  *
33606bc6cff5SAlan Stern  * Locking exception: This routine may also be called from within an
33616bc6cff5SAlan Stern  * autoresume handler.  Such usage won't conflict with other tasks
33626bc6cff5SAlan Stern  * holding the device lock because these tasks should always call
33636bc6cff5SAlan Stern  * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
33641da177e4SLinus Torvalds  */
3365742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev)
33661da177e4SLinus Torvalds {
33671da177e4SLinus Torvalds 	struct usb_device		*parent_hdev = udev->parent;
33681da177e4SLinus Torvalds 	struct usb_hub			*parent_hub;
33691da177e4SLinus Torvalds 	struct usb_device_descriptor	descriptor = udev->descriptor;
337012c3da34SAlan Stern 	int 				i, ret = 0;
337112c3da34SAlan Stern 	int				port1 = udev->portnum;
33721da177e4SLinus Torvalds 
33731da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED ||
33741da177e4SLinus Torvalds 			udev->state == USB_STATE_SUSPENDED) {
33751da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
33761da177e4SLinus Torvalds 				udev->state);
33771da177e4SLinus Torvalds 		return -EINVAL;
33781da177e4SLinus Torvalds 	}
33791da177e4SLinus Torvalds 
33801da177e4SLinus Torvalds 	if (!parent_hdev) {
33811da177e4SLinus Torvalds 		/* this requires hcd-specific logic; see OHCI hc_restart() */
3382441b62c1SHarvey Harrison 		dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
33831da177e4SLinus Torvalds 		return -EISDIR;
33841da177e4SLinus Torvalds 	}
33851da177e4SLinus Torvalds 	parent_hub = hdev_to_hub(parent_hdev);
33861da177e4SLinus Torvalds 
33871da177e4SLinus Torvalds 	set_bit(port1, parent_hub->busy_bits);
33881da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; ++i) {
33891da177e4SLinus Torvalds 
33901da177e4SLinus Torvalds 		/* ep0 maxpacket size may change; let the HCD know about it.
33911da177e4SLinus Torvalds 		 * Other endpoints will be handled by re-enumeration. */
3392fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
33931da177e4SLinus Torvalds 		ret = hub_port_init(parent_hub, udev, port1, i);
3394dd4dd19eSAlan Stern 		if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
33951da177e4SLinus Torvalds 			break;
33961da177e4SLinus Torvalds 	}
33971da177e4SLinus Torvalds 	clear_bit(port1, parent_hub->busy_bits);
3398d5cbad4bSAlan Stern 
33991da177e4SLinus Torvalds 	if (ret < 0)
34001da177e4SLinus Torvalds 		goto re_enumerate;
34011da177e4SLinus Torvalds 
34021da177e4SLinus Torvalds 	/* Device might have changed firmware (DFU or similar) */
3403eb764c4bSAlan Stern 	if (descriptors_changed(udev, &descriptor)) {
34041da177e4SLinus Torvalds 		dev_info(&udev->dev, "device firmware changed\n");
34051da177e4SLinus Torvalds 		udev->descriptor = descriptor;	/* for disconnect() calls */
34061da177e4SLinus Torvalds 		goto re_enumerate;
34071da177e4SLinus Torvalds   	}
34081da177e4SLinus Torvalds 
34094fe0387aSAlan Stern 	/* Restore the device's previous configuration */
34101da177e4SLinus Torvalds 	if (!udev->actconfig)
34111da177e4SLinus Torvalds 		goto done;
34121da177e4SLinus Torvalds 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
34131da177e4SLinus Torvalds 			USB_REQ_SET_CONFIGURATION, 0,
34141da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, 0,
34151da177e4SLinus Torvalds 			NULL, 0, USB_CTRL_SET_TIMEOUT);
34161da177e4SLinus Torvalds 	if (ret < 0) {
34171da177e4SLinus Torvalds 		dev_err(&udev->dev,
34181da177e4SLinus Torvalds 			"can't restore configuration #%d (error=%d)\n",
34191da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, ret);
34201da177e4SLinus Torvalds 		goto re_enumerate;
34211da177e4SLinus Torvalds   	}
34221da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_CONFIGURED);
34231da177e4SLinus Torvalds 
34244fe0387aSAlan Stern 	/* Put interfaces back into the same altsettings as before.
34254fe0387aSAlan Stern 	 * Don't bother to send the Set-Interface request for interfaces
34264fe0387aSAlan Stern 	 * that were already in altsetting 0; besides being unnecessary,
34274fe0387aSAlan Stern 	 * many devices can't handle it.  Instead just reset the host-side
34284fe0387aSAlan Stern 	 * endpoint state.
34294fe0387aSAlan Stern 	 */
34301da177e4SLinus Torvalds 	for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
34311da177e4SLinus Torvalds 		struct usb_interface *intf = udev->actconfig->interface[i];
34321da177e4SLinus Torvalds 		struct usb_interface_descriptor *desc;
34331da177e4SLinus Torvalds 
34341da177e4SLinus Torvalds 		desc = &intf->cur_altsetting->desc;
34354fe0387aSAlan Stern 		if (desc->bAlternateSetting == 0) {
34364fe0387aSAlan Stern 			usb_disable_interface(udev, intf, true);
34374fe0387aSAlan Stern 			usb_enable_interface(udev, intf, true);
34384fe0387aSAlan Stern 			ret = 0;
34394fe0387aSAlan Stern 		} else {
34401da177e4SLinus Torvalds 			ret = usb_set_interface(udev, desc->bInterfaceNumber,
34411da177e4SLinus Torvalds 					desc->bAlternateSetting);
34424fe0387aSAlan Stern 		}
34431da177e4SLinus Torvalds 		if (ret < 0) {
34441da177e4SLinus Torvalds 			dev_err(&udev->dev, "failed to restore interface %d "
34451da177e4SLinus Torvalds 				"altsetting %d (error=%d)\n",
34461da177e4SLinus Torvalds 				desc->bInterfaceNumber,
34471da177e4SLinus Torvalds 				desc->bAlternateSetting,
34481da177e4SLinus Torvalds 				ret);
34491da177e4SLinus Torvalds 			goto re_enumerate;
34501da177e4SLinus Torvalds 		}
34511da177e4SLinus Torvalds 	}
34521da177e4SLinus Torvalds 
34531da177e4SLinus Torvalds done:
34541da177e4SLinus Torvalds 	return 0;
34551da177e4SLinus Torvalds 
34561da177e4SLinus Torvalds re_enumerate:
34571da177e4SLinus Torvalds 	hub_port_logical_disconnect(parent_hub, port1);
34581da177e4SLinus Torvalds 	return -ENODEV;
34591da177e4SLinus Torvalds }
346079efa097SAlan Stern 
346179efa097SAlan Stern /**
3462742120c6SMing Lei  * usb_reset_device - warn interface drivers and perform a USB port reset
346379efa097SAlan Stern  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
346479efa097SAlan Stern  *
346579efa097SAlan Stern  * Warns all drivers bound to registered interfaces (using their pre_reset
346679efa097SAlan Stern  * method), performs the port reset, and then lets the drivers know that
346779efa097SAlan Stern  * the reset is over (using their post_reset method).
346879efa097SAlan Stern  *
3469742120c6SMing Lei  * Return value is the same as for usb_reset_and_verify_device().
347079efa097SAlan Stern  *
347179efa097SAlan Stern  * The caller must own the device lock.  For example, it's safe to use
347279efa097SAlan Stern  * this from a driver probe() routine after downloading new firmware.
347379efa097SAlan Stern  * For calls that might not occur during probe(), drivers should lock
347479efa097SAlan Stern  * the device using usb_lock_device_for_reset().
347578d9a487SAlan Stern  *
347678d9a487SAlan Stern  * If an interface is currently being probed or disconnected, we assume
347778d9a487SAlan Stern  * its driver knows how to handle resets.  For all other interfaces,
347878d9a487SAlan Stern  * if the driver doesn't have pre_reset and post_reset methods then
347978d9a487SAlan Stern  * we attempt to unbind it and rebind afterward.
348079efa097SAlan Stern  */
3481742120c6SMing Lei int usb_reset_device(struct usb_device *udev)
348279efa097SAlan Stern {
348379efa097SAlan Stern 	int ret;
3484852c4b43SAlan Stern 	int i;
348579efa097SAlan Stern 	struct usb_host_config *config = udev->actconfig;
348679efa097SAlan Stern 
348779efa097SAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED ||
348879efa097SAlan Stern 			udev->state == USB_STATE_SUSPENDED) {
348979efa097SAlan Stern 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
349079efa097SAlan Stern 				udev->state);
349179efa097SAlan Stern 		return -EINVAL;
349279efa097SAlan Stern 	}
349379efa097SAlan Stern 
3494645daaabSAlan Stern 	/* Prevent autosuspend during the reset */
349594fcda1fSAlan Stern 	usb_autoresume_device(udev);
3496645daaabSAlan Stern 
349779efa097SAlan Stern 	if (config) {
3498852c4b43SAlan Stern 		for (i = 0; i < config->desc.bNumInterfaces; ++i) {
3499852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
350079efa097SAlan Stern 			struct usb_driver *drv;
350178d9a487SAlan Stern 			int unbind = 0;
350279efa097SAlan Stern 
3503852c4b43SAlan Stern 			if (cintf->dev.driver) {
350479efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
350578d9a487SAlan Stern 				if (drv->pre_reset && drv->post_reset)
350678d9a487SAlan Stern 					unbind = (drv->pre_reset)(cintf);
350778d9a487SAlan Stern 				else if (cintf->condition ==
350878d9a487SAlan Stern 						USB_INTERFACE_BOUND)
350978d9a487SAlan Stern 					unbind = 1;
351078d9a487SAlan Stern 				if (unbind)
351178d9a487SAlan Stern 					usb_forced_unbind_intf(cintf);
351279efa097SAlan Stern 			}
351379efa097SAlan Stern 		}
351479efa097SAlan Stern 	}
351579efa097SAlan Stern 
3516742120c6SMing Lei 	ret = usb_reset_and_verify_device(udev);
351779efa097SAlan Stern 
351879efa097SAlan Stern 	if (config) {
3519852c4b43SAlan Stern 		for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
3520852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
352179efa097SAlan Stern 			struct usb_driver *drv;
352278d9a487SAlan Stern 			int rebind = cintf->needs_binding;
352379efa097SAlan Stern 
352478d9a487SAlan Stern 			if (!rebind && cintf->dev.driver) {
352579efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
352679efa097SAlan Stern 				if (drv->post_reset)
352778d9a487SAlan Stern 					rebind = (drv->post_reset)(cintf);
352878d9a487SAlan Stern 				else if (cintf->condition ==
352978d9a487SAlan Stern 						USB_INTERFACE_BOUND)
353078d9a487SAlan Stern 					rebind = 1;
353179efa097SAlan Stern 			}
35326c640945SAlan Stern 			if (ret == 0 && rebind)
353378d9a487SAlan Stern 				usb_rebind_intf(cintf);
353479efa097SAlan Stern 		}
353579efa097SAlan Stern 	}
353679efa097SAlan Stern 
353794fcda1fSAlan Stern 	usb_autosuspend_device(udev);
353879efa097SAlan Stern 	return ret;
353979efa097SAlan Stern }
3540742120c6SMing Lei EXPORT_SYMBOL_GPL(usb_reset_device);
3541dc023dceSInaky Perez-Gonzalez 
3542dc023dceSInaky Perez-Gonzalez 
3543dc023dceSInaky Perez-Gonzalez /**
3544dc023dceSInaky Perez-Gonzalez  * usb_queue_reset_device - Reset a USB device from an atomic context
3545dc023dceSInaky Perez-Gonzalez  * @iface: USB interface belonging to the device to reset
3546dc023dceSInaky Perez-Gonzalez  *
3547dc023dceSInaky Perez-Gonzalez  * This function can be used to reset a USB device from an atomic
3548dc023dceSInaky Perez-Gonzalez  * context, where usb_reset_device() won't work (as it blocks).
3549dc023dceSInaky Perez-Gonzalez  *
3550dc023dceSInaky Perez-Gonzalez  * Doing a reset via this method is functionally equivalent to calling
3551dc023dceSInaky Perez-Gonzalez  * usb_reset_device(), except for the fact that it is delayed to a
3552dc023dceSInaky Perez-Gonzalez  * workqueue. This means that any drivers bound to other interfaces
3553dc023dceSInaky Perez-Gonzalez  * might be unbound, as well as users from usbfs in user space.
3554dc023dceSInaky Perez-Gonzalez  *
3555dc023dceSInaky Perez-Gonzalez  * Corner cases:
3556dc023dceSInaky Perez-Gonzalez  *
3557dc023dceSInaky Perez-Gonzalez  * - Scheduling two resets at the same time from two different drivers
3558dc023dceSInaky Perez-Gonzalez  *   attached to two different interfaces of the same device is
3559dc023dceSInaky Perez-Gonzalez  *   possible; depending on how the driver attached to each interface
3560dc023dceSInaky Perez-Gonzalez  *   handles ->pre_reset(), the second reset might happen or not.
3561dc023dceSInaky Perez-Gonzalez  *
3562dc023dceSInaky Perez-Gonzalez  * - If a driver is unbound and it had a pending reset, the reset will
3563dc023dceSInaky Perez-Gonzalez  *   be cancelled.
3564dc023dceSInaky Perez-Gonzalez  *
3565dc023dceSInaky Perez-Gonzalez  * - This function can be called during .probe() or .disconnect()
3566dc023dceSInaky Perez-Gonzalez  *   times. On return from .disconnect(), any pending resets will be
3567dc023dceSInaky Perez-Gonzalez  *   cancelled.
3568dc023dceSInaky Perez-Gonzalez  *
3569dc023dceSInaky Perez-Gonzalez  * There is no no need to lock/unlock the @reset_ws as schedule_work()
3570dc023dceSInaky Perez-Gonzalez  * does its own.
3571dc023dceSInaky Perez-Gonzalez  *
3572dc023dceSInaky Perez-Gonzalez  * NOTE: We don't do any reference count tracking because it is not
3573dc023dceSInaky Perez-Gonzalez  *     needed. The lifecycle of the work_struct is tied to the
3574dc023dceSInaky Perez-Gonzalez  *     usb_interface. Before destroying the interface we cancel the
3575dc023dceSInaky Perez-Gonzalez  *     work_struct, so the fact that work_struct is queued and or
3576dc023dceSInaky Perez-Gonzalez  *     running means the interface (and thus, the device) exist and
3577dc023dceSInaky Perez-Gonzalez  *     are referenced.
3578dc023dceSInaky Perez-Gonzalez  */
3579dc023dceSInaky Perez-Gonzalez void usb_queue_reset_device(struct usb_interface *iface)
3580dc023dceSInaky Perez-Gonzalez {
3581dc023dceSInaky Perez-Gonzalez 	schedule_work(&iface->reset_ws);
3582dc023dceSInaky Perez-Gonzalez }
3583dc023dceSInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_queue_reset_device);
3584