xref: /openbmc/linux/drivers/usb/core/hub.c (revision 16985408)
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 	union {
491bb5f66bSAlan Stern 		struct usb_hub_status	hub;
501bb5f66bSAlan Stern 		struct usb_port_status	port;
511bb5f66bSAlan Stern 	}			*status;	/* buffer for status reports */
52db90e7a1SAlan Stern 	struct mutex		status_mutex;	/* for the status buffer */
531bb5f66bSAlan Stern 
541bb5f66bSAlan Stern 	int			error;		/* last reported error */
551bb5f66bSAlan Stern 	int			nerrors;	/* track consecutive errors */
561bb5f66bSAlan Stern 
571bb5f66bSAlan Stern 	struct list_head	event_list;	/* hubs w/data or errs ready */
581bb5f66bSAlan Stern 	unsigned long		event_bits[1];	/* status change bitmask */
591bb5f66bSAlan Stern 	unsigned long		change_bits[1];	/* ports with logical connect
601bb5f66bSAlan Stern 							status change */
611bb5f66bSAlan Stern 	unsigned long		busy_bits[1];	/* ports being reset or
621bb5f66bSAlan Stern 							resumed */
63253e0572SAlan Stern 	unsigned long		removed_bits[1]; /* ports with a "removed"
64253e0572SAlan Stern 							device present */
651bb5f66bSAlan Stern #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
661bb5f66bSAlan Stern #error event_bits[] is too short!
671bb5f66bSAlan Stern #endif
681bb5f66bSAlan Stern 
691bb5f66bSAlan Stern 	struct usb_hub_descriptor *descriptor;	/* class descriptor */
701bb5f66bSAlan Stern 	struct usb_tt		tt;		/* Transaction Translator */
711bb5f66bSAlan Stern 
721bb5f66bSAlan Stern 	unsigned		mA_per_port;	/* current for each child */
731bb5f66bSAlan Stern 
748e4ceb38SAlan Stern 	unsigned		init_done:1;
751bb5f66bSAlan Stern 	unsigned		limited_power:1;
761bb5f66bSAlan Stern 	unsigned		quiescing:1;
77e8054854SAlan Stern 	unsigned		disconnected:1;
781bb5f66bSAlan Stern 
791bb5f66bSAlan Stern 	unsigned		has_indicators:1;
801bb5f66bSAlan Stern 	u8			indicator[USB_MAXCHILDREN];
816d5aefb8SDavid Howells 	struct delayed_work	leds;
828520f380SAlan Stern 	struct delayed_work	init_work;
837cbe5dcaSAlan Stern 	void			**port_owners;
841bb5f66bSAlan Stern };
851bb5f66bSAlan Stern 
861bb5f66bSAlan Stern 
871da177e4SLinus Torvalds /* Protect struct usb_device->state and ->children members
889ad3d6ccSAlan Stern  * Note: Both are also protected by ->dev.sem, except that ->state can
891da177e4SLinus Torvalds  * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
901da177e4SLinus Torvalds static DEFINE_SPINLOCK(device_state_lock);
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /* khubd's worklist and its lock */
931da177e4SLinus Torvalds static DEFINE_SPINLOCK(hub_event_lock);
941da177e4SLinus Torvalds static LIST_HEAD(hub_event_list);	/* List of hubs needing servicing */
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /* Wakes up khubd */
971da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
981da177e4SLinus Torvalds 
999c8d6178Sakpm@osdl.org static struct task_struct *khubd_task;
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds /* cycle leds on hubs that aren't blinking for attention */
1021da177e4SLinus Torvalds static int blinkenlights = 0;
1031da177e4SLinus Torvalds module_param (blinkenlights, bool, S_IRUGO);
1041da177e4SLinus Torvalds MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /*
107fd7c519dSJaroslav Kysela  * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
108fd7c519dSJaroslav Kysela  * 10 seconds to send reply for the initial 64-byte descriptor request.
109fd7c519dSJaroslav Kysela  */
110fd7c519dSJaroslav Kysela /* define initial 64-byte descriptor request timeout in milliseconds */
111fd7c519dSJaroslav Kysela static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
112fd7c519dSJaroslav Kysela module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
113b9cef6c3SWu Fengguang MODULE_PARM_DESC(initial_descriptor_timeout,
114b9cef6c3SWu Fengguang 		"initial 64-byte descriptor request timeout in milliseconds "
115b9cef6c3SWu Fengguang 		"(default 5000 - 5.0 seconds)");
116fd7c519dSJaroslav Kysela 
117fd7c519dSJaroslav Kysela /*
1181da177e4SLinus Torvalds  * As of 2.6.10 we introduce a new USB device initialization scheme which
1191da177e4SLinus Torvalds  * closely resembles the way Windows works.  Hopefully it will be compatible
1201da177e4SLinus Torvalds  * with a wider range of devices than the old scheme.  However some previously
1211da177e4SLinus Torvalds  * working devices may start giving rise to "device not accepting address"
1221da177e4SLinus Torvalds  * errors; if that happens the user can try the old scheme by adjusting the
1231da177e4SLinus Torvalds  * following module parameters.
1241da177e4SLinus Torvalds  *
1251da177e4SLinus Torvalds  * For maximum flexibility there are two boolean parameters to control the
1261da177e4SLinus Torvalds  * hub driver's behavior.  On the first initialization attempt, if the
1271da177e4SLinus Torvalds  * "old_scheme_first" parameter is set then the old scheme will be used,
1281da177e4SLinus Torvalds  * otherwise the new scheme is used.  If that fails and "use_both_schemes"
1291da177e4SLinus Torvalds  * is set, then the driver will make another attempt, using the other scheme.
1301da177e4SLinus Torvalds  */
1311da177e4SLinus Torvalds static int old_scheme_first = 0;
1321da177e4SLinus Torvalds module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
1331da177e4SLinus Torvalds MODULE_PARM_DESC(old_scheme_first,
1341da177e4SLinus Torvalds 		 "start with the old device initialization scheme");
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds static int use_both_schemes = 1;
1371da177e4SLinus Torvalds module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
1381da177e4SLinus Torvalds MODULE_PARM_DESC(use_both_schemes,
1391da177e4SLinus Torvalds 		"try the other device initialization scheme if the "
1401da177e4SLinus Torvalds 		"first one fails");
1411da177e4SLinus Torvalds 
14232fe0198SAlan Stern /* Mutual exclusion for EHCI CF initialization.  This interferes with
14332fe0198SAlan Stern  * port reset on some companion controllers.
14432fe0198SAlan Stern  */
14532fe0198SAlan Stern DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
14632fe0198SAlan Stern EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
14732fe0198SAlan Stern 
148948fea37SAlan Stern #define HUB_DEBOUNCE_TIMEOUT	1500
149948fea37SAlan Stern #define HUB_DEBOUNCE_STEP	  25
150948fea37SAlan Stern #define HUB_DEBOUNCE_STABLE	 100
151948fea37SAlan Stern 
1521da177e4SLinus Torvalds 
153742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev);
154742120c6SMing Lei 
1551da177e4SLinus Torvalds static inline char *portspeed(int portstatus)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
1581da177e4SLinus Torvalds     		return "480 Mb/s";
1591da177e4SLinus Torvalds 	else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
1601da177e4SLinus Torvalds 		return "1.5 Mb/s";
161d2e9b4d6SSarah Sharp 	else if (portstatus & (1 << USB_PORT_FEAT_SUPERSPEED))
162d2e9b4d6SSarah Sharp 		return "5.0 Gb/s";
1631da177e4SLinus Torvalds 	else
1641da177e4SLinus Torvalds 		return "12 Mb/s";
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds /* Note that hdev or one of its children must be locked! */
16825118084SAlan Stern static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
1691da177e4SLinus Torvalds {
17025118084SAlan Stern 	if (!hdev || !hdev->actconfig)
17125118084SAlan Stern 		return NULL;
1721da177e4SLinus Torvalds 	return usb_get_intfdata(hdev->actconfig->interface[0]);
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.4.5 */
1761da177e4SLinus Torvalds static int get_hub_descriptor(struct usb_device *hdev, void *data, int size)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	int i, ret;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	for (i = 0; i < 3; i++) {
1811da177e4SLinus Torvalds 		ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
1821da177e4SLinus Torvalds 			USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
1831da177e4SLinus Torvalds 			USB_DT_HUB << 8, 0, data, size,
1841da177e4SLinus Torvalds 			USB_CTRL_GET_TIMEOUT);
1851da177e4SLinus Torvalds 		if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
1861da177e4SLinus Torvalds 			return ret;
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds 	return -EINVAL;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds /*
1921da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.1
1931da177e4SLinus Torvalds  */
1941da177e4SLinus Torvalds static int clear_hub_feature(struct usb_device *hdev, int feature)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1971da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds /*
2011da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.2
2021da177e4SLinus Torvalds  */
2031da177e4SLinus Torvalds static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
2061da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
2071da177e4SLinus Torvalds 		NULL, 0, 1000);
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds /*
2111da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.13
2121da177e4SLinus Torvalds  */
2131da177e4SLinus Torvalds static int set_port_feature(struct usb_device *hdev, int port1, int feature)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
2161da177e4SLinus Torvalds 		USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
2171da177e4SLinus Torvalds 		NULL, 0, 1000);
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds /*
2211da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
2221da177e4SLinus Torvalds  * for info about using port indicators
2231da177e4SLinus Torvalds  */
2241da177e4SLinus Torvalds static void set_port_led(
2251da177e4SLinus Torvalds 	struct usb_hub *hub,
2261da177e4SLinus Torvalds 	int port1,
2271da177e4SLinus Torvalds 	int selector
2281da177e4SLinus Torvalds )
2291da177e4SLinus Torvalds {
2301da177e4SLinus Torvalds 	int status = set_port_feature(hub->hdev, (selector << 8) | port1,
2311da177e4SLinus Torvalds 			USB_PORT_FEAT_INDICATOR);
2321da177e4SLinus Torvalds 	if (status < 0)
2331da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
2341da177e4SLinus Torvalds 			"port %d indicator %s status %d\n",
2351da177e4SLinus Torvalds 			port1,
2361da177e4SLinus Torvalds 			({ char *s; switch (selector) {
2371da177e4SLinus Torvalds 			case HUB_LED_AMBER: s = "amber"; break;
2381da177e4SLinus Torvalds 			case HUB_LED_GREEN: s = "green"; break;
2391da177e4SLinus Torvalds 			case HUB_LED_OFF: s = "off"; break;
2401da177e4SLinus Torvalds 			case HUB_LED_AUTO: s = "auto"; break;
2411da177e4SLinus Torvalds 			default: s = "??"; break;
2421da177e4SLinus Torvalds 			}; s; }),
2431da177e4SLinus Torvalds 			status);
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds #define	LED_CYCLE_PERIOD	((2*HZ)/3)
2471da177e4SLinus Torvalds 
248c4028958SDavid Howells static void led_work (struct work_struct *work)
2491da177e4SLinus Torvalds {
250c4028958SDavid Howells 	struct usb_hub		*hub =
251c4028958SDavid Howells 		container_of(work, struct usb_hub, leds.work);
2521da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
2531da177e4SLinus Torvalds 	unsigned		i;
2541da177e4SLinus Torvalds 	unsigned		changed = 0;
2551da177e4SLinus Torvalds 	int			cursor = -1;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
2581da177e4SLinus Torvalds 		return;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
2611da177e4SLinus Torvalds 		unsigned	selector, mode;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 		/* 30%-50% duty cycle */
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 		switch (hub->indicator[i]) {
2661da177e4SLinus Torvalds 		/* cycle marker */
2671da177e4SLinus Torvalds 		case INDICATOR_CYCLE:
2681da177e4SLinus Torvalds 			cursor = i;
2691da177e4SLinus Torvalds 			selector = HUB_LED_AUTO;
2701da177e4SLinus Torvalds 			mode = INDICATOR_AUTO;
2711da177e4SLinus Torvalds 			break;
2721da177e4SLinus Torvalds 		/* blinking green = sw attention */
2731da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK:
2741da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
2751da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK_OFF;
2761da177e4SLinus Torvalds 			break;
2771da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK_OFF:
2781da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
2791da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK;
2801da177e4SLinus Torvalds 			break;
2811da177e4SLinus Torvalds 		/* blinking amber = hw attention */
2821da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK:
2831da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
2841da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK_OFF;
2851da177e4SLinus Torvalds 			break;
2861da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK_OFF:
2871da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
2881da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK;
2891da177e4SLinus Torvalds 			break;
2901da177e4SLinus Torvalds 		/* blink green/amber = reserved */
2911da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK:
2921da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
2931da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK_OFF;
2941da177e4SLinus Torvalds 			break;
2951da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK_OFF:
2961da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
2971da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK;
2981da177e4SLinus Torvalds 			break;
2991da177e4SLinus Torvalds 		default:
3001da177e4SLinus Torvalds 			continue;
3011da177e4SLinus Torvalds 		}
3021da177e4SLinus Torvalds 		if (selector != HUB_LED_AUTO)
3031da177e4SLinus Torvalds 			changed = 1;
3041da177e4SLinus Torvalds 		set_port_led(hub, i + 1, selector);
3051da177e4SLinus Torvalds 		hub->indicator[i] = mode;
3061da177e4SLinus Torvalds 	}
3071da177e4SLinus Torvalds 	if (!changed && blinkenlights) {
3081da177e4SLinus Torvalds 		cursor++;
3091da177e4SLinus Torvalds 		cursor %= hub->descriptor->bNbrPorts;
3101da177e4SLinus Torvalds 		set_port_led(hub, cursor + 1, HUB_LED_GREEN);
3111da177e4SLinus Torvalds 		hub->indicator[cursor] = INDICATOR_CYCLE;
3121da177e4SLinus Torvalds 		changed++;
3131da177e4SLinus Torvalds 	}
3141da177e4SLinus Torvalds 	if (changed)
3151da177e4SLinus Torvalds 		schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds /* use a short timeout for hub/port status fetches */
3191da177e4SLinus Torvalds #define	USB_STS_TIMEOUT		1000
3201da177e4SLinus Torvalds #define	USB_STS_RETRIES		5
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds /*
3231da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.6
3241da177e4SLinus Torvalds  */
3251da177e4SLinus Torvalds static int get_hub_status(struct usb_device *hdev,
3261da177e4SLinus Torvalds 		struct usb_hub_status *data)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
3311da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
3321da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
3331da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
3341da177e4SLinus Torvalds 	}
3351da177e4SLinus Torvalds 	return status;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds /*
3391da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7
3401da177e4SLinus Torvalds  */
3411da177e4SLinus Torvalds static int get_port_status(struct usb_device *hdev, int port1,
3421da177e4SLinus Torvalds 		struct usb_port_status *data)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
3471da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
3481da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
3491da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
3501da177e4SLinus Torvalds 	}
3511da177e4SLinus Torvalds 	return status;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3543eb14915SAlan Stern static int hub_port_status(struct usb_hub *hub, int port1,
3553eb14915SAlan Stern 		u16 *status, u16 *change)
3563eb14915SAlan Stern {
3573eb14915SAlan Stern 	int ret;
3583eb14915SAlan Stern 
3593eb14915SAlan Stern 	mutex_lock(&hub->status_mutex);
3603eb14915SAlan Stern 	ret = get_port_status(hub->hdev, port1, &hub->status->port);
3613eb14915SAlan Stern 	if (ret < 4) {
3623eb14915SAlan Stern 		dev_err(hub->intfdev,
3633eb14915SAlan Stern 			"%s failed (err = %d)\n", __func__, ret);
3643eb14915SAlan Stern 		if (ret >= 0)
3653eb14915SAlan Stern 			ret = -EIO;
3663eb14915SAlan Stern 	} else {
3673eb14915SAlan Stern 		*status = le16_to_cpu(hub->status->port.wPortStatus);
3683eb14915SAlan Stern 		*change = le16_to_cpu(hub->status->port.wPortChange);
3693eb14915SAlan Stern 		ret = 0;
3703eb14915SAlan Stern 	}
3713eb14915SAlan Stern 	mutex_unlock(&hub->status_mutex);
3723eb14915SAlan Stern 	return ret;
3733eb14915SAlan Stern }
3743eb14915SAlan Stern 
3751da177e4SLinus Torvalds static void kick_khubd(struct usb_hub *hub)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds 	unsigned long	flags;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	spin_lock_irqsave(&hub_event_lock, flags);
3802e2c5eeaSRoel Kluin 	if (!hub->disconnected && list_empty(&hub->event_list)) {
3811da177e4SLinus Torvalds 		list_add_tail(&hub->event_list, &hub_event_list);
3828e4ceb38SAlan Stern 
3838e4ceb38SAlan Stern 		/* Suppress autosuspend until khubd runs */
3848e4ceb38SAlan Stern 		usb_autopm_get_interface_no_resume(
3858e4ceb38SAlan Stern 				to_usb_interface(hub->intfdev));
3861da177e4SLinus Torvalds 		wake_up(&khubd_wait);
3871da177e4SLinus Torvalds 	}
3881da177e4SLinus Torvalds 	spin_unlock_irqrestore(&hub_event_lock, flags);
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds void usb_kick_khubd(struct usb_device *hdev)
3921da177e4SLinus Torvalds {
39325118084SAlan Stern 	struct usb_hub *hub = hdev_to_hub(hdev);
39425118084SAlan Stern 
39525118084SAlan Stern 	if (hub)
39625118084SAlan Stern 		kick_khubd(hub);
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds /* completion function, fires on port status changes and various faults */
4017d12e780SDavid Howells static void hub_irq(struct urb *urb)
4021da177e4SLinus Torvalds {
403ec17cf1cSTobias Klauser 	struct usb_hub *hub = urb->context;
404e015268dSAlan Stern 	int status = urb->status;
40571d2718fSRoel Kluin 	unsigned i;
4061da177e4SLinus Torvalds 	unsigned long bits;
4071da177e4SLinus Torvalds 
408e015268dSAlan Stern 	switch (status) {
4091da177e4SLinus Torvalds 	case -ENOENT:		/* synchronous unlink */
4101da177e4SLinus Torvalds 	case -ECONNRESET:	/* async unlink */
4111da177e4SLinus Torvalds 	case -ESHUTDOWN:	/* hardware going away */
4121da177e4SLinus Torvalds 		return;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	default:		/* presumably an error */
4151da177e4SLinus Torvalds 		/* Cause a hub reset after 10 consecutive errors */
416e015268dSAlan Stern 		dev_dbg (hub->intfdev, "transfer --> %d\n", status);
4171da177e4SLinus Torvalds 		if ((++hub->nerrors < 10) || hub->error)
4181da177e4SLinus Torvalds 			goto resubmit;
419e015268dSAlan Stern 		hub->error = status;
4201da177e4SLinus Torvalds 		/* FALL THROUGH */
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	/* let khubd handle things */
4231da177e4SLinus Torvalds 	case 0:			/* we got data:  port status changed */
4241da177e4SLinus Torvalds 		bits = 0;
4251da177e4SLinus Torvalds 		for (i = 0; i < urb->actual_length; ++i)
4261da177e4SLinus Torvalds 			bits |= ((unsigned long) ((*hub->buffer)[i]))
4271da177e4SLinus Torvalds 					<< (i*8);
4281da177e4SLinus Torvalds 		hub->event_bits[0] = bits;
4291da177e4SLinus Torvalds 		break;
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	hub->nerrors = 0;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	/* Something happened, let khubd figure it out */
4351da177e4SLinus Torvalds 	kick_khubd(hub);
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds resubmit:
4381da177e4SLinus Torvalds 	if (hub->quiescing)
4391da177e4SLinus Torvalds 		return;
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
4421da177e4SLinus Torvalds 			&& status != -ENODEV && status != -EPERM)
4431da177e4SLinus Torvalds 		dev_err (hub->intfdev, "resubmit --> %d\n", status);
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.2.3 */
4471da177e4SLinus Torvalds static inline int
4481da177e4SLinus Torvalds hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
4491da177e4SLinus Torvalds {
450c2f6595fSAlan Stern 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
4511da177e4SLinus Torvalds 			       HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
4521da177e4SLinus Torvalds 			       tt, NULL, 0, 1000);
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds /*
4561da177e4SLinus Torvalds  * enumeration blocks khubd for a long time. we use keventd instead, since
4571da177e4SLinus Torvalds  * long blocking there is the exception, not the rule.  accordingly, HCDs
4581da177e4SLinus Torvalds  * talking to TTs must queue control transfers (not just bulk and iso), so
4591da177e4SLinus Torvalds  * both can talk to the same hub concurrently.
4601da177e4SLinus Torvalds  */
461cb88a1b8SAlan Stern static void hub_tt_work(struct work_struct *work)
4621da177e4SLinus Torvalds {
463c4028958SDavid Howells 	struct usb_hub		*hub =
464cb88a1b8SAlan Stern 		container_of(work, struct usb_hub, tt.clear_work);
4651da177e4SLinus Torvalds 	unsigned long		flags;
46655e5fdfaSMark Lord 	int			limit = 100;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	spin_lock_irqsave (&hub->tt.lock, flags);
46955e5fdfaSMark Lord 	while (--limit && !list_empty (&hub->tt.clear_list)) {
470d0f830d3SH Hartley Sweeten 		struct list_head	*next;
4711da177e4SLinus Torvalds 		struct usb_tt_clear	*clear;
4721da177e4SLinus Torvalds 		struct usb_device	*hdev = hub->hdev;
473cb88a1b8SAlan Stern 		const struct hc_driver	*drv;
4741da177e4SLinus Torvalds 		int			status;
4751da177e4SLinus Torvalds 
476d0f830d3SH Hartley Sweeten 		next = hub->tt.clear_list.next;
477d0f830d3SH Hartley Sweeten 		clear = list_entry (next, struct usb_tt_clear, clear_list);
4781da177e4SLinus Torvalds 		list_del (&clear->clear_list);
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 		/* drop lock so HCD can concurrently report other TT errors */
4811da177e4SLinus Torvalds 		spin_unlock_irqrestore (&hub->tt.lock, flags);
4821da177e4SLinus Torvalds 		status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
4831da177e4SLinus Torvalds 		if (status)
4841da177e4SLinus Torvalds 			dev_err (&hdev->dev,
4851da177e4SLinus Torvalds 				"clear tt %d (%04x) error %d\n",
4861da177e4SLinus Torvalds 				clear->tt, clear->devinfo, status);
487cb88a1b8SAlan Stern 
488cb88a1b8SAlan Stern 		/* Tell the HCD, even if the operation failed */
489cb88a1b8SAlan Stern 		drv = clear->hcd->driver;
490cb88a1b8SAlan Stern 		if (drv->clear_tt_buffer_complete)
491cb88a1b8SAlan Stern 			(drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
492cb88a1b8SAlan Stern 
4931da177e4SLinus Torvalds 		kfree(clear);
494cb88a1b8SAlan Stern 		spin_lock_irqsave(&hub->tt.lock, flags);
4951da177e4SLinus Torvalds 	}
4961da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hub->tt.lock, flags);
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds /**
500cb88a1b8SAlan Stern  * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
501cb88a1b8SAlan Stern  * @urb: an URB associated with the failed or incomplete split transaction
5021da177e4SLinus Torvalds  *
5031da177e4SLinus Torvalds  * High speed HCDs use this to tell the hub driver that some split control or
5041da177e4SLinus Torvalds  * bulk transaction failed in a way that requires clearing internal state of
5051da177e4SLinus Torvalds  * a transaction translator.  This is normally detected (and reported) from
5061da177e4SLinus Torvalds  * interrupt context.
5071da177e4SLinus Torvalds  *
5081da177e4SLinus Torvalds  * It may not be possible for that hub to handle additional full (or low)
5091da177e4SLinus Torvalds  * speed transactions until that state is fully cleared out.
5101da177e4SLinus Torvalds  */
511cb88a1b8SAlan Stern int usb_hub_clear_tt_buffer(struct urb *urb)
5121da177e4SLinus Torvalds {
513cb88a1b8SAlan Stern 	struct usb_device	*udev = urb->dev;
514cb88a1b8SAlan Stern 	int			pipe = urb->pipe;
5151da177e4SLinus Torvalds 	struct usb_tt		*tt = udev->tt;
5161da177e4SLinus Torvalds 	unsigned long		flags;
5171da177e4SLinus Torvalds 	struct usb_tt_clear	*clear;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	/* we've got to cope with an arbitrary number of pending TT clears,
5201da177e4SLinus Torvalds 	 * since each TT has "at least two" buffers that can need it (and
5211da177e4SLinus Torvalds 	 * there can be many TTs per hub).  even if they're uncommon.
5221da177e4SLinus Torvalds 	 */
52354e6ecb2SChristoph Lameter 	if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
5241da177e4SLinus Torvalds 		dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
5251da177e4SLinus Torvalds 		/* FIXME recover somehow ... RESET_TT? */
526cb88a1b8SAlan Stern 		return -ENOMEM;
5271da177e4SLinus Torvalds 	}
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	/* info that CLEAR_TT_BUFFER needs */
5301da177e4SLinus Torvalds 	clear->tt = tt->multi ? udev->ttport : 1;
5311da177e4SLinus Torvalds 	clear->devinfo = usb_pipeendpoint (pipe);
5321da177e4SLinus Torvalds 	clear->devinfo |= udev->devnum << 4;
5331da177e4SLinus Torvalds 	clear->devinfo |= usb_pipecontrol (pipe)
5341da177e4SLinus Torvalds 			? (USB_ENDPOINT_XFER_CONTROL << 11)
5351da177e4SLinus Torvalds 			: (USB_ENDPOINT_XFER_BULK << 11);
5361da177e4SLinus Torvalds 	if (usb_pipein (pipe))
5371da177e4SLinus Torvalds 		clear->devinfo |= 1 << 15;
5381da177e4SLinus Torvalds 
539cb88a1b8SAlan Stern 	/* info for completion callback */
540cb88a1b8SAlan Stern 	clear->hcd = bus_to_hcd(udev->bus);
541cb88a1b8SAlan Stern 	clear->ep = urb->ep;
542cb88a1b8SAlan Stern 
5431da177e4SLinus Torvalds 	/* tell keventd to clear state for this TT */
5441da177e4SLinus Torvalds 	spin_lock_irqsave (&tt->lock, flags);
5451da177e4SLinus Torvalds 	list_add_tail (&clear->clear_list, &tt->clear_list);
546cb88a1b8SAlan Stern 	schedule_work(&tt->clear_work);
5471da177e4SLinus Torvalds 	spin_unlock_irqrestore (&tt->lock, flags);
548cb88a1b8SAlan Stern 	return 0;
5491da177e4SLinus Torvalds }
550cb88a1b8SAlan Stern EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
5511da177e4SLinus Torvalds 
5528520f380SAlan Stern /* If do_delay is false, return the number of milliseconds the caller
5538520f380SAlan Stern  * needs to delay.
5548520f380SAlan Stern  */
5558520f380SAlan Stern static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	int port1;
558b789696aSDavid Brownell 	unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
5598520f380SAlan Stern 	unsigned delay;
5604489a571SAlan Stern 	u16 wHubCharacteristics =
5614489a571SAlan Stern 			le16_to_cpu(hub->descriptor->wHubCharacteristics);
5621da177e4SLinus Torvalds 
5634489a571SAlan Stern 	/* Enable power on each port.  Some hubs have reserved values
5644489a571SAlan Stern 	 * of LPSM (> 2) in their descriptors, even though they are
5654489a571SAlan Stern 	 * USB 2.0 hubs.  Some hubs do not implement port-power switching
5664489a571SAlan Stern 	 * but only emulate it.  In all cases, the ports won't work
5674489a571SAlan Stern 	 * unless we send these messages to the hub.
5684489a571SAlan Stern 	 */
5694489a571SAlan Stern 	if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
5701da177e4SLinus Torvalds 		dev_dbg(hub->intfdev, "enabling power on all ports\n");
5714489a571SAlan Stern 	else
5724489a571SAlan Stern 		dev_dbg(hub->intfdev, "trying to enable port power on "
5734489a571SAlan Stern 				"non-switchable hub\n");
5741da177e4SLinus Torvalds 	for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
5754489a571SAlan Stern 		set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
5761da177e4SLinus Torvalds 
577b789696aSDavid Brownell 	/* Wait at least 100 msec for power to become stable */
5788520f380SAlan Stern 	delay = max(pgood_delay, (unsigned) 100);
5798520f380SAlan Stern 	if (do_delay)
5808520f380SAlan Stern 		msleep(delay);
5818520f380SAlan Stern 	return delay;
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds static int hub_hub_status(struct usb_hub *hub,
5851da177e4SLinus Torvalds 		u16 *status, u16 *change)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	int ret;
5881da177e4SLinus Torvalds 
589db90e7a1SAlan Stern 	mutex_lock(&hub->status_mutex);
5901da177e4SLinus Torvalds 	ret = get_hub_status(hub->hdev, &hub->status->hub);
5911da177e4SLinus Torvalds 	if (ret < 0)
5921da177e4SLinus Torvalds 		dev_err (hub->intfdev,
593441b62c1SHarvey Harrison 			"%s failed (err = %d)\n", __func__, ret);
5941da177e4SLinus Torvalds 	else {
5951da177e4SLinus Torvalds 		*status = le16_to_cpu(hub->status->hub.wHubStatus);
5961da177e4SLinus Torvalds 		*change = le16_to_cpu(hub->status->hub.wHubChange);
5971da177e4SLinus Torvalds 		ret = 0;
5981da177e4SLinus Torvalds 	}
599db90e7a1SAlan Stern 	mutex_unlock(&hub->status_mutex);
6001da177e4SLinus Torvalds 	return ret;
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
6038b28c752SAlan Stern static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
6048b28c752SAlan Stern {
6058b28c752SAlan Stern 	struct usb_device *hdev = hub->hdev;
6060458d5b4SAlan Stern 	int ret = 0;
6078b28c752SAlan Stern 
6080458d5b4SAlan Stern 	if (hdev->children[port1-1] && set_state)
6098b28c752SAlan Stern 		usb_set_device_state(hdev->children[port1-1],
6108b28c752SAlan Stern 				USB_STATE_NOTATTACHED);
6110458d5b4SAlan Stern 	if (!hub->error)
6128b28c752SAlan Stern 		ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
6138b28c752SAlan Stern 	if (ret)
6148b28c752SAlan Stern 		dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
6158b28c752SAlan Stern 				port1, ret);
6168b28c752SAlan Stern 	return ret;
6178b28c752SAlan Stern }
6188b28c752SAlan Stern 
6190458d5b4SAlan Stern /*
6200458d5b4SAlan Stern  * Disable a port and mark a logical connnect-change event, so that some
6210458d5b4SAlan Stern  * time later khubd will disconnect() any existing usb_device on the port
6220458d5b4SAlan Stern  * and will re-enumerate if there actually is a device attached.
6230458d5b4SAlan Stern  */
6240458d5b4SAlan Stern static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
6257d069b7dSAlan Stern {
6260458d5b4SAlan Stern 	dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
6270458d5b4SAlan Stern 	hub_port_disable(hub, port1, 1);
6280458d5b4SAlan Stern 
6290458d5b4SAlan Stern 	/* FIXME let caller ask to power down the port:
6300458d5b4SAlan Stern 	 *  - some devices won't enumerate without a VBUS power cycle
6310458d5b4SAlan Stern 	 *  - SRP saves power that way
6320458d5b4SAlan Stern 	 *  - ... new call, TBD ...
6330458d5b4SAlan Stern 	 * That's easy if this hub can switch power per-port, and
6340458d5b4SAlan Stern 	 * khubd reactivates the port later (timer, SRP, etc).
6350458d5b4SAlan Stern 	 * Powerdown must be optional, because of reset/DFU.
6360458d5b4SAlan Stern 	 */
6370458d5b4SAlan Stern 
6380458d5b4SAlan Stern 	set_bit(port1, hub->change_bits);
6390458d5b4SAlan Stern  	kick_khubd(hub);
6400458d5b4SAlan Stern }
6410458d5b4SAlan Stern 
642253e0572SAlan Stern /**
643253e0572SAlan Stern  * usb_remove_device - disable a device's port on its parent hub
644253e0572SAlan Stern  * @udev: device to be disabled and removed
645253e0572SAlan Stern  * Context: @udev locked, must be able to sleep.
646253e0572SAlan Stern  *
647253e0572SAlan Stern  * After @udev's port has been disabled, khubd is notified and it will
648253e0572SAlan Stern  * see that the device has been disconnected.  When the device is
649253e0572SAlan Stern  * physically unplugged and something is plugged in, the events will
650253e0572SAlan Stern  * be received and processed normally.
651253e0572SAlan Stern  */
652253e0572SAlan Stern int usb_remove_device(struct usb_device *udev)
653253e0572SAlan Stern {
654253e0572SAlan Stern 	struct usb_hub *hub;
655253e0572SAlan Stern 	struct usb_interface *intf;
656253e0572SAlan Stern 
657253e0572SAlan Stern 	if (!udev->parent)	/* Can't remove a root hub */
658253e0572SAlan Stern 		return -EINVAL;
659253e0572SAlan Stern 	hub = hdev_to_hub(udev->parent);
660253e0572SAlan Stern 	intf = to_usb_interface(hub->intfdev);
661253e0572SAlan Stern 
662253e0572SAlan Stern 	usb_autopm_get_interface(intf);
663253e0572SAlan Stern 	set_bit(udev->portnum, hub->removed_bits);
664253e0572SAlan Stern 	hub_port_logical_disconnect(hub, udev->portnum);
665253e0572SAlan Stern 	usb_autopm_put_interface(intf);
666253e0572SAlan Stern 	return 0;
667253e0572SAlan Stern }
668253e0572SAlan Stern 
6696ee0b270SAlan Stern enum hub_activation_type {
6708e4ceb38SAlan Stern 	HUB_INIT, HUB_INIT2, HUB_INIT3,		/* INITs must come first */
6718520f380SAlan Stern 	HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
6726ee0b270SAlan Stern };
6735e6effaeSAlan Stern 
6748520f380SAlan Stern static void hub_init_func2(struct work_struct *ws);
6758520f380SAlan Stern static void hub_init_func3(struct work_struct *ws);
6768520f380SAlan Stern 
677f2835219SAlan Stern static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
6785e6effaeSAlan Stern {
6795e6effaeSAlan Stern 	struct usb_device *hdev = hub->hdev;
6805e6effaeSAlan Stern 	int port1;
681f2835219SAlan Stern 	int status;
682948fea37SAlan Stern 	bool need_debounce_delay = false;
6838520f380SAlan Stern 	unsigned delay;
6848520f380SAlan Stern 
6858520f380SAlan Stern 	/* Continue a partial initialization */
6868520f380SAlan Stern 	if (type == HUB_INIT2)
6878520f380SAlan Stern 		goto init2;
6888520f380SAlan Stern 	if (type == HUB_INIT3)
6898520f380SAlan Stern 		goto init3;
6905e6effaeSAlan Stern 
691f2835219SAlan Stern 	/* After a resume, port power should still be on.
692f2835219SAlan Stern 	 * For any other type of activation, turn it on.
693f2835219SAlan Stern 	 */
6948520f380SAlan Stern 	if (type != HUB_RESUME) {
6958520f380SAlan Stern 
6968520f380SAlan Stern 		/* Speed up system boot by using a delayed_work for the
6978520f380SAlan Stern 		 * hub's initial power-up delays.  This is pretty awkward
6988520f380SAlan Stern 		 * and the implementation looks like a home-brewed sort of
6998520f380SAlan Stern 		 * setjmp/longjmp, but it saves at least 100 ms for each
7008520f380SAlan Stern 		 * root hub (assuming usbcore is compiled into the kernel
7018520f380SAlan Stern 		 * rather than as a module).  It adds up.
7028520f380SAlan Stern 		 *
7038520f380SAlan Stern 		 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
7048520f380SAlan Stern 		 * because for those activation types the ports have to be
7058520f380SAlan Stern 		 * operational when we return.  In theory this could be done
7068520f380SAlan Stern 		 * for HUB_POST_RESET, but it's easier not to.
7078520f380SAlan Stern 		 */
7088520f380SAlan Stern 		if (type == HUB_INIT) {
7098520f380SAlan Stern 			delay = hub_power_on(hub, false);
7108520f380SAlan Stern 			PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
7118520f380SAlan Stern 			schedule_delayed_work(&hub->init_work,
7128520f380SAlan Stern 					msecs_to_jiffies(delay));
71361fbeba1SAlan Stern 
71461fbeba1SAlan Stern 			/* Suppress autosuspend until init is done */
7158e4ceb38SAlan Stern 			usb_autopm_get_interface_no_resume(
7168e4ceb38SAlan Stern 					to_usb_interface(hub->intfdev));
7178520f380SAlan Stern 			return;		/* Continues at init2: below */
7188520f380SAlan Stern 		} else {
7198520f380SAlan Stern 			hub_power_on(hub, true);
7208520f380SAlan Stern 		}
7218520f380SAlan Stern 	}
7228520f380SAlan Stern  init2:
723f2835219SAlan Stern 
7246ee0b270SAlan Stern 	/* Check each port and set hub->change_bits to let khubd know
7256ee0b270SAlan Stern 	 * which ports need attention.
7265e6effaeSAlan Stern 	 */
7275e6effaeSAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
7285e6effaeSAlan Stern 		struct usb_device *udev = hdev->children[port1-1];
7295e6effaeSAlan Stern 		u16 portstatus, portchange;
7305e6effaeSAlan Stern 
7316ee0b270SAlan Stern 		portstatus = portchange = 0;
7326ee0b270SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
7336ee0b270SAlan Stern 		if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
7346ee0b270SAlan Stern 			dev_dbg(hub->intfdev,
7356ee0b270SAlan Stern 					"port %d: status %04x change %04x\n",
7366ee0b270SAlan Stern 					port1, portstatus, portchange);
7375e6effaeSAlan Stern 
7386ee0b270SAlan Stern 		/* After anything other than HUB_RESUME (i.e., initialization
7396ee0b270SAlan Stern 		 * or any sort of reset), every port should be disabled.
7406ee0b270SAlan Stern 		 * Unconnected ports should likewise be disabled (paranoia),
7416ee0b270SAlan Stern 		 * and so should ports for which we have no usb_device.
7425e6effaeSAlan Stern 		 */
7436ee0b270SAlan Stern 		if ((portstatus & USB_PORT_STAT_ENABLE) && (
7446ee0b270SAlan Stern 				type != HUB_RESUME ||
7456ee0b270SAlan Stern 				!(portstatus & USB_PORT_STAT_CONNECTION) ||
7466ee0b270SAlan Stern 				!udev ||
7476ee0b270SAlan Stern 				udev->state == USB_STATE_NOTATTACHED)) {
7486ee0b270SAlan Stern 			clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
7496ee0b270SAlan Stern 			portstatus &= ~USB_PORT_STAT_ENABLE;
7506ee0b270SAlan Stern 		}
7516ee0b270SAlan Stern 
752948fea37SAlan Stern 		/* Clear status-change flags; we'll debounce later */
753948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
754948fea37SAlan Stern 			need_debounce_delay = true;
755948fea37SAlan Stern 			clear_port_feature(hub->hdev, port1,
756948fea37SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
757948fea37SAlan Stern 		}
758948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE) {
759948fea37SAlan Stern 			need_debounce_delay = true;
760948fea37SAlan Stern 			clear_port_feature(hub->hdev, port1,
761948fea37SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
762948fea37SAlan Stern 		}
763948fea37SAlan Stern 
764253e0572SAlan Stern 		/* We can forget about a "removed" device when there's a
765253e0572SAlan Stern 		 * physical disconnect or the connect status changes.
766253e0572SAlan Stern 		 */
767253e0572SAlan Stern 		if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
768253e0572SAlan Stern 				(portchange & USB_PORT_STAT_C_CONNECTION))
769253e0572SAlan Stern 			clear_bit(port1, hub->removed_bits);
770253e0572SAlan Stern 
7716ee0b270SAlan Stern 		if (!udev || udev->state == USB_STATE_NOTATTACHED) {
7726ee0b270SAlan Stern 			/* Tell khubd to disconnect the device or
7736ee0b270SAlan Stern 			 * check for a new connection
7746ee0b270SAlan Stern 			 */
7756ee0b270SAlan Stern 			if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
7766ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
7776ee0b270SAlan Stern 
7786ee0b270SAlan Stern 		} else if (portstatus & USB_PORT_STAT_ENABLE) {
7796ee0b270SAlan Stern 			/* The power session apparently survived the resume.
7806ee0b270SAlan Stern 			 * If there was an overcurrent or suspend change
7816ee0b270SAlan Stern 			 * (i.e., remote wakeup request), have khubd
7826ee0b270SAlan Stern 			 * take care of it.
7836ee0b270SAlan Stern 			 */
7846ee0b270SAlan Stern 			if (portchange)
7856ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
7866ee0b270SAlan Stern 
7876ee0b270SAlan Stern 		} else if (udev->persist_enabled) {
7886ee0b270SAlan Stern #ifdef CONFIG_PM
7895e6effaeSAlan Stern 			udev->reset_resume = 1;
7906ee0b270SAlan Stern #endif
7918808f00cSAlan Stern 			set_bit(port1, hub->change_bits);
7928808f00cSAlan Stern 
7936ee0b270SAlan Stern 		} else {
7946ee0b270SAlan Stern 			/* The power session is gone; tell khubd */
7956ee0b270SAlan Stern 			usb_set_device_state(udev, USB_STATE_NOTATTACHED);
7966ee0b270SAlan Stern 			set_bit(port1, hub->change_bits);
7975e6effaeSAlan Stern 		}
7985e6effaeSAlan Stern 	}
7995e6effaeSAlan Stern 
800948fea37SAlan Stern 	/* If no port-status-change flags were set, we don't need any
801948fea37SAlan Stern 	 * debouncing.  If flags were set we can try to debounce the
802948fea37SAlan Stern 	 * ports all at once right now, instead of letting khubd do them
803948fea37SAlan Stern 	 * one at a time later on.
804948fea37SAlan Stern 	 *
805948fea37SAlan Stern 	 * If any port-status changes do occur during this delay, khubd
806948fea37SAlan Stern 	 * will see them later and handle them normally.
807948fea37SAlan Stern 	 */
8088520f380SAlan Stern 	if (need_debounce_delay) {
8098520f380SAlan Stern 		delay = HUB_DEBOUNCE_STABLE;
810f2835219SAlan Stern 
8118520f380SAlan Stern 		/* Don't do a long sleep inside a workqueue routine */
8128520f380SAlan Stern 		if (type == HUB_INIT2) {
8138520f380SAlan Stern 			PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
8148520f380SAlan Stern 			schedule_delayed_work(&hub->init_work,
8158520f380SAlan Stern 					msecs_to_jiffies(delay));
8168520f380SAlan Stern 			return;		/* Continues at init3: below */
8178520f380SAlan Stern 		} else {
8188520f380SAlan Stern 			msleep(delay);
8198520f380SAlan Stern 		}
8208520f380SAlan Stern 	}
8218520f380SAlan Stern  init3:
822f2835219SAlan Stern 	hub->quiescing = 0;
8238e4ceb38SAlan Stern 	hub->init_done = 1;
824f2835219SAlan Stern 
825f2835219SAlan Stern 	status = usb_submit_urb(hub->urb, GFP_NOIO);
826f2835219SAlan Stern 	if (status < 0)
827f2835219SAlan Stern 		dev_err(hub->intfdev, "activate --> %d\n", status);
828f2835219SAlan Stern 	if (hub->has_indicators && blinkenlights)
829f2835219SAlan Stern 		schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
830f2835219SAlan Stern 
831f2835219SAlan Stern 	/* Scan all ports that need attention */
832f2835219SAlan Stern 	kick_khubd(hub);
8338e4ceb38SAlan Stern 
8348e4ceb38SAlan Stern 	/* Allow autosuspend if it was suppressed */
8358e4ceb38SAlan Stern 	if (type <= HUB_INIT3)
8368e4ceb38SAlan Stern 		usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
8375e6effaeSAlan Stern }
8385e6effaeSAlan Stern 
8398520f380SAlan Stern /* Implement the continuations for the delays above */
8408520f380SAlan Stern static void hub_init_func2(struct work_struct *ws)
8418520f380SAlan Stern {
8428520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
8438520f380SAlan Stern 
8448520f380SAlan Stern 	hub_activate(hub, HUB_INIT2);
8458520f380SAlan Stern }
8468520f380SAlan Stern 
8478520f380SAlan Stern static void hub_init_func3(struct work_struct *ws)
8488520f380SAlan Stern {
8498520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
8508520f380SAlan Stern 
8518520f380SAlan Stern 	hub_activate(hub, HUB_INIT3);
8528520f380SAlan Stern }
8538520f380SAlan Stern 
8544330354fSAlan Stern enum hub_quiescing_type {
8554330354fSAlan Stern 	HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
8564330354fSAlan Stern };
8574330354fSAlan Stern 
8584330354fSAlan Stern static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
8594330354fSAlan Stern {
8604330354fSAlan Stern 	struct usb_device *hdev = hub->hdev;
8614330354fSAlan Stern 	int i;
8624330354fSAlan Stern 
8638520f380SAlan Stern 	cancel_delayed_work_sync(&hub->init_work);
8648e4ceb38SAlan Stern 	if (!hub->init_done) {
8658e4ceb38SAlan Stern 		hub->init_done = 1;
8668e4ceb38SAlan Stern 		usb_autopm_put_interface_no_suspend(
8678e4ceb38SAlan Stern 				to_usb_interface(hub->intfdev));
8688e4ceb38SAlan Stern 	}
8698520f380SAlan Stern 
8704330354fSAlan Stern 	/* khubd and related activity won't re-trigger */
8714330354fSAlan Stern 	hub->quiescing = 1;
8724330354fSAlan Stern 
8734330354fSAlan Stern 	if (type != HUB_SUSPEND) {
8744330354fSAlan Stern 		/* Disconnect all the children */
8754330354fSAlan Stern 		for (i = 0; i < hdev->maxchild; ++i) {
8764330354fSAlan Stern 			if (hdev->children[i])
8774330354fSAlan Stern 				usb_disconnect(&hdev->children[i]);
8784330354fSAlan Stern 		}
8794330354fSAlan Stern 	}
8804330354fSAlan Stern 
8814330354fSAlan Stern 	/* Stop khubd and related activity */
8824330354fSAlan Stern 	usb_kill_urb(hub->urb);
8834330354fSAlan Stern 	if (hub->has_indicators)
8844330354fSAlan Stern 		cancel_delayed_work_sync(&hub->leds);
8854330354fSAlan Stern 	if (hub->tt.hub)
886cb88a1b8SAlan Stern 		cancel_work_sync(&hub->tt.clear_work);
8874330354fSAlan Stern }
8884330354fSAlan Stern 
8893eb14915SAlan Stern /* caller has locked the hub device */
8903eb14915SAlan Stern static int hub_pre_reset(struct usb_interface *intf)
8913eb14915SAlan Stern {
8923eb14915SAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
8933eb14915SAlan Stern 
8944330354fSAlan Stern 	hub_quiesce(hub, HUB_PRE_RESET);
895f07600cfSAlan Stern 	return 0;
8967d069b7dSAlan Stern }
8977d069b7dSAlan Stern 
8987d069b7dSAlan Stern /* caller has locked the hub device */
899f07600cfSAlan Stern static int hub_post_reset(struct usb_interface *intf)
9007d069b7dSAlan Stern {
9017de18d8bSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
9027de18d8bSAlan Stern 
903f2835219SAlan Stern 	hub_activate(hub, HUB_POST_RESET);
904f07600cfSAlan Stern 	return 0;
9057d069b7dSAlan Stern }
9067d069b7dSAlan Stern 
9071da177e4SLinus Torvalds static int hub_configure(struct usb_hub *hub,
9081da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint)
9091da177e4SLinus Torvalds {
910b356b7c7SSarah Sharp 	struct usb_hcd *hcd;
9111da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
9121da177e4SLinus Torvalds 	struct device *hub_dev = hub->intfdev;
9131da177e4SLinus Torvalds 	u16 hubstatus, hubchange;
91474ad9bd2SGreg Kroah-Hartman 	u16 wHubCharacteristics;
9151da177e4SLinus Torvalds 	unsigned int pipe;
9161da177e4SLinus Torvalds 	int maxp, ret;
9177cbe5dcaSAlan Stern 	char *message = "out of memory";
9181da177e4SLinus Torvalds 
919d697cddaSAlan Stern 	hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
9201da177e4SLinus Torvalds 	if (!hub->buffer) {
9211da177e4SLinus Torvalds 		ret = -ENOMEM;
9221da177e4SLinus Torvalds 		goto fail;
9231da177e4SLinus Torvalds 	}
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
9261da177e4SLinus Torvalds 	if (!hub->status) {
9271da177e4SLinus Torvalds 		ret = -ENOMEM;
9281da177e4SLinus Torvalds 		goto fail;
9291da177e4SLinus Torvalds 	}
930db90e7a1SAlan Stern 	mutex_init(&hub->status_mutex);
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
9331da177e4SLinus Torvalds 	if (!hub->descriptor) {
9341da177e4SLinus Torvalds 		ret = -ENOMEM;
9351da177e4SLinus Torvalds 		goto fail;
9361da177e4SLinus Torvalds 	}
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	/* Request the entire hub descriptor.
9391da177e4SLinus Torvalds 	 * hub->descriptor can handle USB_MAXCHILDREN ports,
9401da177e4SLinus Torvalds 	 * but the hub can/will return fewer bytes here.
9411da177e4SLinus Torvalds 	 */
9421da177e4SLinus Torvalds 	ret = get_hub_descriptor(hdev, hub->descriptor,
9431da177e4SLinus Torvalds 			sizeof(*hub->descriptor));
9441da177e4SLinus Torvalds 	if (ret < 0) {
9451da177e4SLinus Torvalds 		message = "can't read hub descriptor";
9461da177e4SLinus Torvalds 		goto fail;
9471da177e4SLinus Torvalds 	} else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
9481da177e4SLinus Torvalds 		message = "hub has too many ports!";
9491da177e4SLinus Torvalds 		ret = -ENODEV;
9501da177e4SLinus Torvalds 		goto fail;
9511da177e4SLinus Torvalds 	}
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	hdev->maxchild = hub->descriptor->bNbrPorts;
9541da177e4SLinus Torvalds 	dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
9551da177e4SLinus Torvalds 		(hdev->maxchild == 1) ? "" : "s");
9561da177e4SLinus Torvalds 
9577cbe5dcaSAlan Stern 	hub->port_owners = kzalloc(hdev->maxchild * sizeof(void *), GFP_KERNEL);
9587cbe5dcaSAlan Stern 	if (!hub->port_owners) {
9597cbe5dcaSAlan Stern 		ret = -ENOMEM;
9607cbe5dcaSAlan Stern 		goto fail;
9617cbe5dcaSAlan Stern 	}
9627cbe5dcaSAlan Stern 
96374ad9bd2SGreg Kroah-Hartman 	wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
9641da177e4SLinus Torvalds 
96574ad9bd2SGreg Kroah-Hartman 	if (wHubCharacteristics & HUB_CHAR_COMPOUND) {
9661da177e4SLinus Torvalds 		int	i;
9671da177e4SLinus Torvalds 		char	portstr [USB_MAXCHILDREN + 1];
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 		for (i = 0; i < hdev->maxchild; i++)
9701da177e4SLinus Torvalds 			portstr[i] = hub->descriptor->DeviceRemovable
9711da177e4SLinus Torvalds 				    [((i + 1) / 8)] & (1 << ((i + 1) % 8))
9721da177e4SLinus Torvalds 				? 'F' : 'R';
9731da177e4SLinus Torvalds 		portstr[hdev->maxchild] = 0;
9741da177e4SLinus Torvalds 		dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
9751da177e4SLinus Torvalds 	} else
9761da177e4SLinus Torvalds 		dev_dbg(hub_dev, "standalone hub\n");
9771da177e4SLinus Torvalds 
97874ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_LPSM) {
9791da177e4SLinus Torvalds 		case 0x00:
9801da177e4SLinus Torvalds 			dev_dbg(hub_dev, "ganged power switching\n");
9811da177e4SLinus Torvalds 			break;
9821da177e4SLinus Torvalds 		case 0x01:
9831da177e4SLinus Torvalds 			dev_dbg(hub_dev, "individual port power switching\n");
9841da177e4SLinus Torvalds 			break;
9851da177e4SLinus Torvalds 		case 0x02:
9861da177e4SLinus Torvalds 		case 0x03:
9871da177e4SLinus Torvalds 			dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
9881da177e4SLinus Torvalds 			break;
9891da177e4SLinus Torvalds 	}
9901da177e4SLinus Torvalds 
99174ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_OCPM) {
9921da177e4SLinus Torvalds 		case 0x00:
9931da177e4SLinus Torvalds 			dev_dbg(hub_dev, "global over-current protection\n");
9941da177e4SLinus Torvalds 			break;
9951da177e4SLinus Torvalds 		case 0x08:
9961da177e4SLinus Torvalds 			dev_dbg(hub_dev, "individual port over-current protection\n");
9971da177e4SLinus Torvalds 			break;
9981da177e4SLinus Torvalds 		case 0x10:
9991da177e4SLinus Torvalds 		case 0x18:
10001da177e4SLinus Torvalds 			dev_dbg(hub_dev, "no over-current protection\n");
10011da177e4SLinus Torvalds                         break;
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	spin_lock_init (&hub->tt.lock);
10051da177e4SLinus Torvalds 	INIT_LIST_HEAD (&hub->tt.clear_list);
1006cb88a1b8SAlan Stern 	INIT_WORK(&hub->tt.clear_work, hub_tt_work);
10071da177e4SLinus Torvalds 	switch (hdev->descriptor.bDeviceProtocol) {
10081da177e4SLinus Torvalds 		case 0:
10091da177e4SLinus Torvalds 			break;
10101da177e4SLinus Torvalds 		case 1:
10111da177e4SLinus Torvalds 			dev_dbg(hub_dev, "Single TT\n");
10121da177e4SLinus Torvalds 			hub->tt.hub = hdev;
10131da177e4SLinus Torvalds 			break;
10141da177e4SLinus Torvalds 		case 2:
10151da177e4SLinus Torvalds 			ret = usb_set_interface(hdev, 0, 1);
10161da177e4SLinus Torvalds 			if (ret == 0) {
10171da177e4SLinus Torvalds 				dev_dbg(hub_dev, "TT per port\n");
10181da177e4SLinus Torvalds 				hub->tt.multi = 1;
10191da177e4SLinus Torvalds 			} else
10201da177e4SLinus Torvalds 				dev_err(hub_dev, "Using single TT (err %d)\n",
10211da177e4SLinus Torvalds 					ret);
10221da177e4SLinus Torvalds 			hub->tt.hub = hdev;
10231da177e4SLinus Torvalds 			break;
1024d2e9b4d6SSarah Sharp 		case 3:
1025d2e9b4d6SSarah Sharp 			/* USB 3.0 hubs don't have a TT */
1026d2e9b4d6SSarah Sharp 			break;
10271da177e4SLinus Torvalds 		default:
10281da177e4SLinus Torvalds 			dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
10291da177e4SLinus Torvalds 				hdev->descriptor.bDeviceProtocol);
10301da177e4SLinus Torvalds 			break;
10311da177e4SLinus Torvalds 	}
10321da177e4SLinus Torvalds 
1033e09711aeSdavid-b@pacbell.net 	/* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
103474ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_TTTT) {
1035e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_8_BITS:
1036e09711aeSdavid-b@pacbell.net 			if (hdev->descriptor.bDeviceProtocol != 0) {
1037e09711aeSdavid-b@pacbell.net 				hub->tt.think_time = 666;
1038e09711aeSdavid-b@pacbell.net 				dev_dbg(hub_dev, "TT requires at most %d "
1039e09711aeSdavid-b@pacbell.net 						"FS bit times (%d ns)\n",
1040e09711aeSdavid-b@pacbell.net 					8, hub->tt.think_time);
1041e09711aeSdavid-b@pacbell.net 			}
10421da177e4SLinus Torvalds 			break;
1043e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_16_BITS:
1044e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 2;
1045e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1046e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1047e09711aeSdavid-b@pacbell.net 				16, hub->tt.think_time);
10481da177e4SLinus Torvalds 			break;
1049e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_24_BITS:
1050e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 3;
1051e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1052e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1053e09711aeSdavid-b@pacbell.net 				24, hub->tt.think_time);
10541da177e4SLinus Torvalds 			break;
1055e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_32_BITS:
1056e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 4;
1057e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1058e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1059e09711aeSdavid-b@pacbell.net 				32, hub->tt.think_time);
10601da177e4SLinus Torvalds 			break;
10611da177e4SLinus Torvalds 	}
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	/* probe() zeroes hub->indicator[] */
106474ad9bd2SGreg Kroah-Hartman 	if (wHubCharacteristics & HUB_CHAR_PORTIND) {
10651da177e4SLinus Torvalds 		hub->has_indicators = 1;
10661da177e4SLinus Torvalds 		dev_dbg(hub_dev, "Port indicators are supported\n");
10671da177e4SLinus Torvalds 	}
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	dev_dbg(hub_dev, "power on to power good time: %dms\n",
10701da177e4SLinus Torvalds 		hub->descriptor->bPwrOn2PwrGood * 2);
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds 	/* power budgeting mostly matters with bus-powered hubs,
10731da177e4SLinus Torvalds 	 * and battery-powered root hubs (may provide just 8 mA).
10741da177e4SLinus Torvalds 	 */
10751da177e4SLinus Torvalds 	ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
107655c52718SAlan Stern 	if (ret < 2) {
10771da177e4SLinus Torvalds 		message = "can't get hub status";
10781da177e4SLinus Torvalds 		goto fail;
10791da177e4SLinus Torvalds 	}
10807d35b929SAlan Stern 	le16_to_cpus(&hubstatus);
10817d35b929SAlan Stern 	if (hdev == hdev->bus->root_hub) {
108255c52718SAlan Stern 		if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
108355c52718SAlan Stern 			hub->mA_per_port = 500;
108455c52718SAlan Stern 		else {
108555c52718SAlan Stern 			hub->mA_per_port = hdev->bus_mA;
108655c52718SAlan Stern 			hub->limited_power = 1;
108755c52718SAlan Stern 		}
10887d35b929SAlan Stern 	} else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
10891da177e4SLinus Torvalds 		dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
10901da177e4SLinus Torvalds 			hub->descriptor->bHubContrCurrent);
109155c52718SAlan Stern 		hub->limited_power = 1;
109255c52718SAlan Stern 		if (hdev->maxchild > 0) {
109355c52718SAlan Stern 			int remaining = hdev->bus_mA -
109455c52718SAlan Stern 					hub->descriptor->bHubContrCurrent;
10951da177e4SLinus Torvalds 
109655c52718SAlan Stern 			if (remaining < hdev->maxchild * 100)
109755c52718SAlan Stern 				dev_warn(hub_dev,
109855c52718SAlan Stern 					"insufficient power available "
109955c52718SAlan Stern 					"to use all downstream ports\n");
110055c52718SAlan Stern 			hub->mA_per_port = 100;		/* 7.2.1.1 */
110155c52718SAlan Stern 		}
110255c52718SAlan Stern 	} else {	/* Self-powered external hub */
110355c52718SAlan Stern 		/* FIXME: What about battery-powered external hubs that
110455c52718SAlan Stern 		 * provide less current per port? */
110555c52718SAlan Stern 		hub->mA_per_port = 500;
110655c52718SAlan Stern 	}
110755c52718SAlan Stern 	if (hub->mA_per_port < 500)
110855c52718SAlan Stern 		dev_dbg(hub_dev, "%umA bus power budget for each child\n",
110955c52718SAlan Stern 				hub->mA_per_port);
11101da177e4SLinus Torvalds 
1111b356b7c7SSarah Sharp 	/* Update the HCD's internal representation of this hub before khubd
1112b356b7c7SSarah Sharp 	 * starts getting port status changes for devices under the hub.
1113b356b7c7SSarah Sharp 	 */
1114b356b7c7SSarah Sharp 	hcd = bus_to_hcd(hdev->bus);
1115b356b7c7SSarah Sharp 	if (hcd->driver->update_hub_device) {
1116b356b7c7SSarah Sharp 		ret = hcd->driver->update_hub_device(hcd, hdev,
1117b356b7c7SSarah Sharp 				&hub->tt, GFP_KERNEL);
1118b356b7c7SSarah Sharp 		if (ret < 0) {
1119b356b7c7SSarah Sharp 			message = "can't update HCD hub info";
1120b356b7c7SSarah Sharp 			goto fail;
1121b356b7c7SSarah Sharp 		}
1122b356b7c7SSarah Sharp 	}
1123b356b7c7SSarah Sharp 
11241da177e4SLinus Torvalds 	ret = hub_hub_status(hub, &hubstatus, &hubchange);
11251da177e4SLinus Torvalds 	if (ret < 0) {
11261da177e4SLinus Torvalds 		message = "can't get hub status";
11271da177e4SLinus Torvalds 		goto fail;
11281da177e4SLinus Torvalds 	}
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 	/* local power status reports aren't always correct */
11311da177e4SLinus Torvalds 	if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
11321da177e4SLinus Torvalds 		dev_dbg(hub_dev, "local power source is %s\n",
11331da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_LOCAL_POWER)
11341da177e4SLinus Torvalds 			? "lost (inactive)" : "good");
11351da177e4SLinus Torvalds 
113674ad9bd2SGreg Kroah-Hartman 	if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
11371da177e4SLinus Torvalds 		dev_dbg(hub_dev, "%sover-current condition exists\n",
11381da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
11391da177e4SLinus Torvalds 
114088fafff9Sinaky@linux.intel.com 	/* set up the interrupt endpoint
114188fafff9Sinaky@linux.intel.com 	 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
114288fafff9Sinaky@linux.intel.com 	 * bytes as USB2.0[11.12.3] says because some hubs are known
114388fafff9Sinaky@linux.intel.com 	 * to send more data (and thus cause overflow). For root hubs,
114488fafff9Sinaky@linux.intel.com 	 * maxpktsize is defined in hcd.c's fake endpoint descriptors
114588fafff9Sinaky@linux.intel.com 	 * to be big enough for at least USB_MAXCHILDREN ports. */
11461da177e4SLinus Torvalds 	pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
11471da177e4SLinus Torvalds 	maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	if (maxp > sizeof(*hub->buffer))
11501da177e4SLinus Torvalds 		maxp = sizeof(*hub->buffer);
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 	hub->urb = usb_alloc_urb(0, GFP_KERNEL);
11531da177e4SLinus Torvalds 	if (!hub->urb) {
11541da177e4SLinus Torvalds 		ret = -ENOMEM;
11551da177e4SLinus Torvalds 		goto fail;
11561da177e4SLinus Torvalds 	}
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
11591da177e4SLinus Torvalds 		hub, endpoint->bInterval);
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	/* maybe cycle the hub leds */
11621da177e4SLinus Torvalds 	if (hub->has_indicators && blinkenlights)
11631da177e4SLinus Torvalds 		hub->indicator [0] = INDICATOR_CYCLE;
11641da177e4SLinus Torvalds 
1165f2835219SAlan Stern 	hub_activate(hub, HUB_INIT);
11661da177e4SLinus Torvalds 	return 0;
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds fail:
11691da177e4SLinus Torvalds 	dev_err (hub_dev, "config failed, %s (err %d)\n",
11701da177e4SLinus Torvalds 			message, ret);
11711da177e4SLinus Torvalds 	/* hub_disconnect() frees urb and descriptor */
11721da177e4SLinus Torvalds 	return ret;
11731da177e4SLinus Torvalds }
11741da177e4SLinus Torvalds 
1175e8054854SAlan Stern static void hub_release(struct kref *kref)
1176e8054854SAlan Stern {
1177e8054854SAlan Stern 	struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1178e8054854SAlan Stern 
1179e8054854SAlan Stern 	usb_put_intf(to_usb_interface(hub->intfdev));
1180e8054854SAlan Stern 	kfree(hub);
1181e8054854SAlan Stern }
1182e8054854SAlan Stern 
11831da177e4SLinus Torvalds static unsigned highspeed_hubs;
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds static void hub_disconnect(struct usb_interface *intf)
11861da177e4SLinus Torvalds {
11871da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata (intf);
1188e8054854SAlan Stern 
1189e8054854SAlan Stern 	/* Take the hub off the event list and don't let it be added again */
1190e8054854SAlan Stern 	spin_lock_irq(&hub_event_lock);
11918e4ceb38SAlan Stern 	if (!list_empty(&hub->event_list)) {
1192e8054854SAlan Stern 		list_del_init(&hub->event_list);
11938e4ceb38SAlan Stern 		usb_autopm_put_interface_no_suspend(intf);
11948e4ceb38SAlan Stern 	}
1195e8054854SAlan Stern 	hub->disconnected = 1;
1196e8054854SAlan Stern 	spin_unlock_irq(&hub_event_lock);
11971da177e4SLinus Torvalds 
11987de18d8bSAlan Stern 	/* Disconnect all children and quiesce the hub */
11997de18d8bSAlan Stern 	hub->error = 0;
12004330354fSAlan Stern 	hub_quiesce(hub, HUB_DISCONNECT);
12017de18d8bSAlan Stern 
12028b28c752SAlan Stern 	usb_set_intfdata (intf, NULL);
12037cbe5dcaSAlan Stern 	hub->hdev->maxchild = 0;
12041da177e4SLinus Torvalds 
1205e8054854SAlan Stern 	if (hub->hdev->speed == USB_SPEED_HIGH)
12061da177e4SLinus Torvalds 		highspeed_hubs--;
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	usb_free_urb(hub->urb);
12097cbe5dcaSAlan Stern 	kfree(hub->port_owners);
12101da177e4SLinus Torvalds 	kfree(hub->descriptor);
12111da177e4SLinus Torvalds 	kfree(hub->status);
1212d697cddaSAlan Stern 	kfree(hub->buffer);
12131da177e4SLinus Torvalds 
1214e8054854SAlan Stern 	kref_put(&hub->kref, hub_release);
12151da177e4SLinus Torvalds }
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
12181da177e4SLinus Torvalds {
12191da177e4SLinus Torvalds 	struct usb_host_interface *desc;
12201da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint;
12211da177e4SLinus Torvalds 	struct usb_device *hdev;
12221da177e4SLinus Torvalds 	struct usb_hub *hub;
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	desc = intf->cur_altsetting;
12251da177e4SLinus Torvalds 	hdev = interface_to_usbdev(intf);
12261da177e4SLinus Torvalds 
122738f3ad5eSFelipe Balbi 	if (hdev->level == MAX_TOPO_LEVEL) {
1228b9cef6c3SWu Fengguang 		dev_err(&intf->dev,
1229b9cef6c3SWu Fengguang 			"Unsupported bus topology: hub nested too deep\n");
123038f3ad5eSFelipe Balbi 		return -E2BIG;
123138f3ad5eSFelipe Balbi 	}
123238f3ad5eSFelipe Balbi 
123389ccbdc9SDavid Brownell #ifdef	CONFIG_USB_OTG_BLACKLIST_HUB
123489ccbdc9SDavid Brownell 	if (hdev->parent) {
123589ccbdc9SDavid Brownell 		dev_warn(&intf->dev, "ignoring external hub\n");
123689ccbdc9SDavid Brownell 		return -ENODEV;
123789ccbdc9SDavid Brownell 	}
123889ccbdc9SDavid Brownell #endif
123989ccbdc9SDavid Brownell 
12401da177e4SLinus Torvalds 	/* Some hubs have a subclass of 1, which AFAICT according to the */
12411da177e4SLinus Torvalds 	/*  specs is not defined, but it works */
12421da177e4SLinus Torvalds 	if ((desc->desc.bInterfaceSubClass != 0) &&
12431da177e4SLinus Torvalds 	    (desc->desc.bInterfaceSubClass != 1)) {
12441da177e4SLinus Torvalds descriptor_error:
12451da177e4SLinus Torvalds 		dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
12461da177e4SLinus Torvalds 		return -EIO;
12471da177e4SLinus Torvalds 	}
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	/* Multiple endpoints? What kind of mutant ninja-hub is this? */
12501da177e4SLinus Torvalds 	if (desc->desc.bNumEndpoints != 1)
12511da177e4SLinus Torvalds 		goto descriptor_error;
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	endpoint = &desc->endpoint[0].desc;
12541da177e4SLinus Torvalds 
1255fbf81c29SLuiz Fernando N. Capitulino 	/* If it's not an interrupt in endpoint, we'd better punt! */
1256fbf81c29SLuiz Fernando N. Capitulino 	if (!usb_endpoint_is_int_in(endpoint))
12571da177e4SLinus Torvalds 		goto descriptor_error;
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds 	/* We found a hub */
12601da177e4SLinus Torvalds 	dev_info (&intf->dev, "USB hub found\n");
12611da177e4SLinus Torvalds 
12620a1ef3b5SAlan Stern 	hub = kzalloc(sizeof(*hub), GFP_KERNEL);
12631da177e4SLinus Torvalds 	if (!hub) {
12641da177e4SLinus Torvalds 		dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
12651da177e4SLinus Torvalds 		return -ENOMEM;
12661da177e4SLinus Torvalds 	}
12671da177e4SLinus Torvalds 
1268e8054854SAlan Stern 	kref_init(&hub->kref);
12691da177e4SLinus Torvalds 	INIT_LIST_HEAD(&hub->event_list);
12701da177e4SLinus Torvalds 	hub->intfdev = &intf->dev;
12711da177e4SLinus Torvalds 	hub->hdev = hdev;
1272c4028958SDavid Howells 	INIT_DELAYED_WORK(&hub->leds, led_work);
12738520f380SAlan Stern 	INIT_DELAYED_WORK(&hub->init_work, NULL);
1274e8054854SAlan Stern 	usb_get_intf(intf);
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 	usb_set_intfdata (intf, hub);
127740f122f3SAlan Stern 	intf->needs_remote_wakeup = 1;
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	if (hdev->speed == USB_SPEED_HIGH)
12801da177e4SLinus Torvalds 		highspeed_hubs++;
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 	if (hub_configure(hub, endpoint) >= 0)
12831da177e4SLinus Torvalds 		return 0;
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	hub_disconnect (intf);
12861da177e4SLinus Torvalds 	return -ENODEV;
12871da177e4SLinus Torvalds }
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds static int
12901da177e4SLinus Torvalds hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
12911da177e4SLinus Torvalds {
12921da177e4SLinus Torvalds 	struct usb_device *hdev = interface_to_usbdev (intf);
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 	/* assert ifno == 0 (part of hub spec) */
12951da177e4SLinus Torvalds 	switch (code) {
12961da177e4SLinus Torvalds 	case USBDEVFS_HUB_PORTINFO: {
12971da177e4SLinus Torvalds 		struct usbdevfs_hub_portinfo *info = user_data;
12981da177e4SLinus Torvalds 		int i;
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
13011da177e4SLinus Torvalds 		if (hdev->devnum <= 0)
13021da177e4SLinus Torvalds 			info->nports = 0;
13031da177e4SLinus Torvalds 		else {
13041da177e4SLinus Torvalds 			info->nports = hdev->maxchild;
13051da177e4SLinus Torvalds 			for (i = 0; i < info->nports; i++) {
13061da177e4SLinus Torvalds 				if (hdev->children[i] == NULL)
13071da177e4SLinus Torvalds 					info->port[i] = 0;
13081da177e4SLinus Torvalds 				else
13091da177e4SLinus Torvalds 					info->port[i] =
13101da177e4SLinus Torvalds 						hdev->children[i]->devnum;
13111da177e4SLinus Torvalds 			}
13121da177e4SLinus Torvalds 		}
13131da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 		return info->nports + 1;
13161da177e4SLinus Torvalds 		}
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	default:
13191da177e4SLinus Torvalds 		return -ENOSYS;
13201da177e4SLinus Torvalds 	}
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds 
13237cbe5dcaSAlan Stern /*
13247cbe5dcaSAlan Stern  * Allow user programs to claim ports on a hub.  When a device is attached
13257cbe5dcaSAlan Stern  * to one of these "claimed" ports, the program will "own" the device.
13267cbe5dcaSAlan Stern  */
13277cbe5dcaSAlan Stern static int find_port_owner(struct usb_device *hdev, unsigned port1,
13287cbe5dcaSAlan Stern 		void ***ppowner)
13297cbe5dcaSAlan Stern {
13307cbe5dcaSAlan Stern 	if (hdev->state == USB_STATE_NOTATTACHED)
13317cbe5dcaSAlan Stern 		return -ENODEV;
13327cbe5dcaSAlan Stern 	if (port1 == 0 || port1 > hdev->maxchild)
13337cbe5dcaSAlan Stern 		return -EINVAL;
13347cbe5dcaSAlan Stern 
13357cbe5dcaSAlan Stern 	/* This assumes that devices not managed by the hub driver
13367cbe5dcaSAlan Stern 	 * will always have maxchild equal to 0.
13377cbe5dcaSAlan Stern 	 */
13387cbe5dcaSAlan Stern 	*ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]);
13397cbe5dcaSAlan Stern 	return 0;
13407cbe5dcaSAlan Stern }
13417cbe5dcaSAlan Stern 
13427cbe5dcaSAlan Stern /* In the following three functions, the caller must hold hdev's lock */
13437cbe5dcaSAlan Stern int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, void *owner)
13447cbe5dcaSAlan Stern {
13457cbe5dcaSAlan Stern 	int rc;
13467cbe5dcaSAlan Stern 	void **powner;
13477cbe5dcaSAlan Stern 
13487cbe5dcaSAlan Stern 	rc = find_port_owner(hdev, port1, &powner);
13497cbe5dcaSAlan Stern 	if (rc)
13507cbe5dcaSAlan Stern 		return rc;
13517cbe5dcaSAlan Stern 	if (*powner)
13527cbe5dcaSAlan Stern 		return -EBUSY;
13537cbe5dcaSAlan Stern 	*powner = owner;
13547cbe5dcaSAlan Stern 	return rc;
13557cbe5dcaSAlan Stern }
13567cbe5dcaSAlan Stern 
13577cbe5dcaSAlan Stern int usb_hub_release_port(struct usb_device *hdev, unsigned port1, void *owner)
13587cbe5dcaSAlan Stern {
13597cbe5dcaSAlan Stern 	int rc;
13607cbe5dcaSAlan Stern 	void **powner;
13617cbe5dcaSAlan Stern 
13627cbe5dcaSAlan Stern 	rc = find_port_owner(hdev, port1, &powner);
13637cbe5dcaSAlan Stern 	if (rc)
13647cbe5dcaSAlan Stern 		return rc;
13657cbe5dcaSAlan Stern 	if (*powner != owner)
13667cbe5dcaSAlan Stern 		return -ENOENT;
13677cbe5dcaSAlan Stern 	*powner = NULL;
13687cbe5dcaSAlan Stern 	return rc;
13697cbe5dcaSAlan Stern }
13707cbe5dcaSAlan Stern 
13717cbe5dcaSAlan Stern void usb_hub_release_all_ports(struct usb_device *hdev, void *owner)
13727cbe5dcaSAlan Stern {
13737cbe5dcaSAlan Stern 	int n;
13747cbe5dcaSAlan Stern 	void **powner;
13757cbe5dcaSAlan Stern 
13767cbe5dcaSAlan Stern 	n = find_port_owner(hdev, 1, &powner);
13777cbe5dcaSAlan Stern 	if (n == 0) {
13787cbe5dcaSAlan Stern 		for (; n < hdev->maxchild; (++n, ++powner)) {
13797cbe5dcaSAlan Stern 			if (*powner == owner)
13807cbe5dcaSAlan Stern 				*powner = NULL;
13817cbe5dcaSAlan Stern 		}
13827cbe5dcaSAlan Stern 	}
13837cbe5dcaSAlan Stern }
13847cbe5dcaSAlan Stern 
13857cbe5dcaSAlan Stern /* The caller must hold udev's lock */
13867cbe5dcaSAlan Stern bool usb_device_is_owned(struct usb_device *udev)
13877cbe5dcaSAlan Stern {
13887cbe5dcaSAlan Stern 	struct usb_hub *hub;
13897cbe5dcaSAlan Stern 
13907cbe5dcaSAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
13917cbe5dcaSAlan Stern 		return false;
13927cbe5dcaSAlan Stern 	hub = hdev_to_hub(udev->parent);
13937cbe5dcaSAlan Stern 	return !!hub->port_owners[udev->portnum - 1];
13947cbe5dcaSAlan Stern }
13957cbe5dcaSAlan Stern 
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds static void recursively_mark_NOTATTACHED(struct usb_device *udev)
13981da177e4SLinus Torvalds {
13991da177e4SLinus Torvalds 	int i;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	for (i = 0; i < udev->maxchild; ++i) {
14021da177e4SLinus Torvalds 		if (udev->children[i])
14031da177e4SLinus Torvalds 			recursively_mark_NOTATTACHED(udev->children[i]);
14041da177e4SLinus Torvalds 	}
140515123006SSarah Sharp 	if (udev->state == USB_STATE_SUSPENDED) {
1406ee49fb5dSAlan Stern 		udev->discon_suspended = 1;
140715123006SSarah Sharp 		udev->active_duration -= jiffies;
140815123006SSarah Sharp 	}
14091da177e4SLinus Torvalds 	udev->state = USB_STATE_NOTATTACHED;
14101da177e4SLinus Torvalds }
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds /**
14131da177e4SLinus Torvalds  * usb_set_device_state - change a device's current state (usbcore, hcds)
14141da177e4SLinus Torvalds  * @udev: pointer to device whose state should be changed
14151da177e4SLinus Torvalds  * @new_state: new state value to be stored
14161da177e4SLinus Torvalds  *
14171da177e4SLinus Torvalds  * udev->state is _not_ fully protected by the device lock.  Although
14181da177e4SLinus Torvalds  * most transitions are made only while holding the lock, the state can
14191da177e4SLinus Torvalds  * can change to USB_STATE_NOTATTACHED at almost any time.  This
14201da177e4SLinus Torvalds  * is so that devices can be marked as disconnected as soon as possible,
14211da177e4SLinus Torvalds  * without having to wait for any semaphores to be released.  As a result,
14221da177e4SLinus Torvalds  * all changes to any device's state must be protected by the
14231da177e4SLinus Torvalds  * device_state_lock spinlock.
14241da177e4SLinus Torvalds  *
14251da177e4SLinus Torvalds  * Once a device has been added to the device tree, all changes to its state
14261da177e4SLinus Torvalds  * should be made using this routine.  The state should _not_ be set directly.
14271da177e4SLinus Torvalds  *
14281da177e4SLinus Torvalds  * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
14291da177e4SLinus Torvalds  * Otherwise udev->state is set to new_state, and if new_state is
14301da177e4SLinus Torvalds  * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
14311da177e4SLinus Torvalds  * to USB_STATE_NOTATTACHED.
14321da177e4SLinus Torvalds  */
14331da177e4SLinus Torvalds void usb_set_device_state(struct usb_device *udev,
14341da177e4SLinus Torvalds 		enum usb_device_state new_state)
14351da177e4SLinus Torvalds {
14361da177e4SLinus Torvalds 	unsigned long flags;
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	spin_lock_irqsave(&device_state_lock, flags);
14391da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED)
14401da177e4SLinus Torvalds 		;	/* do nothing */
1441b94dc6b5SDavid Brownell 	else if (new_state != USB_STATE_NOTATTACHED) {
1442fb669cc0SDavid Brownell 
1443fb669cc0SDavid Brownell 		/* root hub wakeup capabilities are managed out-of-band
1444fb669cc0SDavid Brownell 		 * and may involve silicon errata ... ignore them here.
1445fb669cc0SDavid Brownell 		 */
1446fb669cc0SDavid Brownell 		if (udev->parent) {
1447645daaabSAlan Stern 			if (udev->state == USB_STATE_SUSPENDED
1448645daaabSAlan Stern 					|| new_state == USB_STATE_SUSPENDED)
1449645daaabSAlan Stern 				;	/* No change to wakeup settings */
1450645daaabSAlan Stern 			else if (new_state == USB_STATE_CONFIGURED)
145116985408SDan Streetman 				device_set_wakeup_capable(&udev->dev,
1452b94dc6b5SDavid Brownell 					(udev->actconfig->desc.bmAttributes
1453b94dc6b5SDavid Brownell 					 & USB_CONFIG_ATT_WAKEUP));
1454645daaabSAlan Stern 			else
145516985408SDan Streetman 				device_set_wakeup_capable(&udev->dev, 0);
1456fb669cc0SDavid Brownell 		}
145715123006SSarah Sharp 		if (udev->state == USB_STATE_SUSPENDED &&
145815123006SSarah Sharp 			new_state != USB_STATE_SUSPENDED)
145915123006SSarah Sharp 			udev->active_duration -= jiffies;
146015123006SSarah Sharp 		else if (new_state == USB_STATE_SUSPENDED &&
146115123006SSarah Sharp 				udev->state != USB_STATE_SUSPENDED)
146215123006SSarah Sharp 			udev->active_duration += jiffies;
1463645daaabSAlan Stern 		udev->state = new_state;
1464b94dc6b5SDavid Brownell 	} else
14651da177e4SLinus Torvalds 		recursively_mark_NOTATTACHED(udev);
14661da177e4SLinus Torvalds 	spin_unlock_irqrestore(&device_state_lock, flags);
14671da177e4SLinus Torvalds }
14686da9c990SDavid Vrabel EXPORT_SYMBOL_GPL(usb_set_device_state);
14691da177e4SLinus Torvalds 
14708af548dcSInaky Perez-Gonzalez /*
14718af548dcSInaky Perez-Gonzalez  * WUSB devices are simple: they have no hubs behind, so the mapping
14728af548dcSInaky Perez-Gonzalez  * device <-> virtual port number becomes 1:1. Why? to simplify the
14738af548dcSInaky Perez-Gonzalez  * life of the device connection logic in
14748af548dcSInaky Perez-Gonzalez  * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
14758af548dcSInaky Perez-Gonzalez  * handshake we need to assign a temporary address in the unauthorized
14768af548dcSInaky Perez-Gonzalez  * space. For simplicity we use the first virtual port number found to
14778af548dcSInaky Perez-Gonzalez  * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
14788af548dcSInaky Perez-Gonzalez  * and that becomes it's address [X < 128] or its unauthorized address
14798af548dcSInaky Perez-Gonzalez  * [X | 0x80].
14808af548dcSInaky Perez-Gonzalez  *
14818af548dcSInaky Perez-Gonzalez  * We add 1 as an offset to the one-based USB-stack port number
14828af548dcSInaky Perez-Gonzalez  * (zero-based wusb virtual port index) for two reasons: (a) dev addr
14838af548dcSInaky Perez-Gonzalez  * 0 is reserved by USB for default address; (b) Linux's USB stack
14848af548dcSInaky Perez-Gonzalez  * uses always #1 for the root hub of the controller. So USB stack's
14858af548dcSInaky Perez-Gonzalez  * port #1, which is wusb virtual-port #0 has address #2.
1486c6515272SSarah Sharp  *
1487c6515272SSarah Sharp  * Devices connected under xHCI are not as simple.  The host controller
1488c6515272SSarah Sharp  * supports virtualization, so the hardware assigns device addresses and
1489c6515272SSarah Sharp  * the HCD must setup data structures before issuing a set address
1490c6515272SSarah Sharp  * command to the hardware.
14918af548dcSInaky Perez-Gonzalez  */
14921da177e4SLinus Torvalds static void choose_address(struct usb_device *udev)
14931da177e4SLinus Torvalds {
14941da177e4SLinus Torvalds 	int		devnum;
14951da177e4SLinus Torvalds 	struct usb_bus	*bus = udev->bus;
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 	/* If khubd ever becomes multithreaded, this will need a lock */
14988af548dcSInaky Perez-Gonzalez 	if (udev->wusb) {
14998af548dcSInaky Perez-Gonzalez 		devnum = udev->portnum + 1;
15008af548dcSInaky Perez-Gonzalez 		BUG_ON(test_bit(devnum, bus->devmap.devicemap));
15018af548dcSInaky Perez-Gonzalez 	} else {
15028af548dcSInaky Perez-Gonzalez 		/* Try to allocate the next devnum beginning at
15038af548dcSInaky Perez-Gonzalez 		 * bus->devnum_next. */
15041da177e4SLinus Torvalds 		devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
15051da177e4SLinus Torvalds 					    bus->devnum_next);
15061da177e4SLinus Torvalds 		if (devnum >= 128)
15078af548dcSInaky Perez-Gonzalez 			devnum = find_next_zero_bit(bus->devmap.devicemap,
15088af548dcSInaky Perez-Gonzalez 						    128, 1);
15091da177e4SLinus Torvalds 		bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
15108af548dcSInaky Perez-Gonzalez 	}
15111da177e4SLinus Torvalds 	if (devnum < 128) {
15121da177e4SLinus Torvalds 		set_bit(devnum, bus->devmap.devicemap);
15131da177e4SLinus Torvalds 		udev->devnum = devnum;
15141da177e4SLinus Torvalds 	}
15151da177e4SLinus Torvalds }
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds static void release_address(struct usb_device *udev)
15181da177e4SLinus Torvalds {
15191da177e4SLinus Torvalds 	if (udev->devnum > 0) {
15201da177e4SLinus Torvalds 		clear_bit(udev->devnum, udev->bus->devmap.devicemap);
15211da177e4SLinus Torvalds 		udev->devnum = -1;
15221da177e4SLinus Torvalds 	}
15231da177e4SLinus Torvalds }
15241da177e4SLinus Torvalds 
15254953d141SDavid Vrabel static void update_address(struct usb_device *udev, int devnum)
15264953d141SDavid Vrabel {
15274953d141SDavid Vrabel 	/* The address for a WUSB device is managed by wusbcore. */
15284953d141SDavid Vrabel 	if (!udev->wusb)
15294953d141SDavid Vrabel 		udev->devnum = devnum;
15304953d141SDavid Vrabel }
15314953d141SDavid Vrabel 
1532d5d4db70SAlan Stern #ifdef	CONFIG_USB_SUSPEND
1533d5d4db70SAlan Stern 
1534d5d4db70SAlan Stern static void usb_stop_pm(struct usb_device *udev)
1535d5d4db70SAlan Stern {
1536d5d4db70SAlan Stern 	/* Synchronize with the ksuspend thread to prevent any more
1537d5d4db70SAlan Stern 	 * autosuspend requests from being submitted, and decrement
1538d5d4db70SAlan Stern 	 * the parent's count of unsuspended children.
1539d5d4db70SAlan Stern 	 */
1540d5d4db70SAlan Stern 	usb_pm_lock(udev);
1541d5d4db70SAlan Stern 	if (udev->parent && !udev->discon_suspended)
1542d5d4db70SAlan Stern 		usb_autosuspend_device(udev->parent);
1543d5d4db70SAlan Stern 	usb_pm_unlock(udev);
1544d5d4db70SAlan Stern 
15459ac39f28SAlan Stern 	/* Stop any autosuspend or autoresume requests already submitted */
15469ac39f28SAlan Stern 	cancel_delayed_work_sync(&udev->autosuspend);
15479ac39f28SAlan Stern 	cancel_work_sync(&udev->autoresume);
1548d5d4db70SAlan Stern }
1549d5d4db70SAlan Stern 
1550d5d4db70SAlan Stern #else
1551d5d4db70SAlan Stern 
1552d5d4db70SAlan Stern static inline void usb_stop_pm(struct usb_device *udev)
1553d5d4db70SAlan Stern { }
1554d5d4db70SAlan Stern 
1555d5d4db70SAlan Stern #endif
1556d5d4db70SAlan Stern 
15571da177e4SLinus Torvalds /**
15581da177e4SLinus Torvalds  * usb_disconnect - disconnect a device (usbcore-internal)
15591da177e4SLinus Torvalds  * @pdev: pointer to device being disconnected
15601da177e4SLinus Torvalds  * Context: !in_interrupt ()
15611da177e4SLinus Torvalds  *
15621da177e4SLinus Torvalds  * Something got disconnected. Get rid of it and all of its children.
15631da177e4SLinus Torvalds  *
15641da177e4SLinus Torvalds  * If *pdev is a normal device then the parent hub must already be locked.
15651da177e4SLinus Torvalds  * If *pdev is a root hub then this routine will acquire the
15661da177e4SLinus Torvalds  * usb_bus_list_lock on behalf of the caller.
15671da177e4SLinus Torvalds  *
15681da177e4SLinus Torvalds  * Only hub drivers (including virtual root hub drivers for host
15691da177e4SLinus Torvalds  * controllers) should ever call this.
15701da177e4SLinus Torvalds  *
15711da177e4SLinus Torvalds  * This call is synchronous, and may not be used in an interrupt context.
15721da177e4SLinus Torvalds  */
15731da177e4SLinus Torvalds void usb_disconnect(struct usb_device **pdev)
15741da177e4SLinus Torvalds {
15751da177e4SLinus Torvalds 	struct usb_device	*udev = *pdev;
15761da177e4SLinus Torvalds 	int			i;
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	if (!udev) {
1579441b62c1SHarvey Harrison 		pr_debug ("%s nodev\n", __func__);
15801da177e4SLinus Torvalds 		return;
15811da177e4SLinus Torvalds 	}
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	/* mark the device as inactive, so any further urb submissions for
15841da177e4SLinus Torvalds 	 * this device (and any of its children) will fail immediately.
15851da177e4SLinus Torvalds 	 * this quiesces everyting except pending urbs.
15861da177e4SLinus Torvalds 	 */
15871da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
15881da177e4SLinus Torvalds 	dev_info (&udev->dev, "USB disconnect, address %d\n", udev->devnum);
15891da177e4SLinus Torvalds 
15909ad3d6ccSAlan Stern 	usb_lock_device(udev);
15919ad3d6ccSAlan Stern 
15921da177e4SLinus Torvalds 	/* Free up all the children before we remove this device */
15931da177e4SLinus Torvalds 	for (i = 0; i < USB_MAXCHILDREN; i++) {
15941da177e4SLinus Torvalds 		if (udev->children[i])
15951da177e4SLinus Torvalds 			usb_disconnect(&udev->children[i]);
15961da177e4SLinus Torvalds 	}
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	/* deallocate hcd/hardware state ... nuking all pending urbs and
15991da177e4SLinus Torvalds 	 * cleaning up all state associated with the current configuration
16001da177e4SLinus Torvalds 	 * so that the hardware is now fully quiesced.
16011da177e4SLinus Torvalds 	 */
1602782da727SAlan Stern 	dev_dbg (&udev->dev, "unregistering device\n");
16031da177e4SLinus Torvalds 	usb_disable_device(udev, 0);
1604cde217a5SAlan Stern 	usb_hcd_synchronize_unlinks(udev);
16051da177e4SLinus Torvalds 
16063b23dd6fSAlan Stern 	usb_remove_ep_devs(&udev->ep0);
1607782da727SAlan Stern 	usb_unlock_device(udev);
16083099e75aSGreg Kroah-Hartman 
1609782da727SAlan Stern 	/* Unregister the device.  The device driver is responsible
16103b23dd6fSAlan Stern 	 * for de-configuring the device and invoking the remove-device
16113b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
1612782da727SAlan Stern 	 */
1613782da727SAlan Stern 	device_del(&udev->dev);
1614782da727SAlan Stern 
1615782da727SAlan Stern 	/* Free the device number and delete the parent's children[]
16161da177e4SLinus Torvalds 	 * (or root_hub) pointer.
16171da177e4SLinus Torvalds 	 */
16181da177e4SLinus Torvalds 	release_address(udev);
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 	/* Avoid races with recursively_mark_NOTATTACHED() */
16211da177e4SLinus Torvalds 	spin_lock_irq(&device_state_lock);
16221da177e4SLinus Torvalds 	*pdev = NULL;
16231da177e4SLinus Torvalds 	spin_unlock_irq(&device_state_lock);
16241da177e4SLinus Torvalds 
1625d5d4db70SAlan Stern 	usb_stop_pm(udev);
1626ee49fb5dSAlan Stern 
1627782da727SAlan Stern 	put_device(&udev->dev);
16281da177e4SLinus Torvalds }
16291da177e4SLinus Torvalds 
1630f2a383e4SGreg Kroah-Hartman #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
16311da177e4SLinus Torvalds static void show_string(struct usb_device *udev, char *id, char *string)
16321da177e4SLinus Torvalds {
16331da177e4SLinus Torvalds 	if (!string)
16341da177e4SLinus Torvalds 		return;
16351da177e4SLinus Torvalds 	dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
16361da177e4SLinus Torvalds }
16371da177e4SLinus Torvalds 
1638f2a383e4SGreg Kroah-Hartman static void announce_device(struct usb_device *udev)
1639f2a383e4SGreg Kroah-Hartman {
1640f2a383e4SGreg Kroah-Hartman 	dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
1641f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idVendor),
1642f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idProduct));
1643b9cef6c3SWu Fengguang 	dev_info(&udev->dev,
1644b9cef6c3SWu Fengguang 		"New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
1645f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iManufacturer,
1646f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iProduct,
1647f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iSerialNumber);
1648f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Product", udev->product);
1649f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Manufacturer", udev->manufacturer);
1650f2a383e4SGreg Kroah-Hartman 	show_string(udev, "SerialNumber", udev->serial);
1651f2a383e4SGreg Kroah-Hartman }
16521da177e4SLinus Torvalds #else
1653f2a383e4SGreg Kroah-Hartman static inline void announce_device(struct usb_device *udev) { }
16541da177e4SLinus Torvalds #endif
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
16571da177e4SLinus Torvalds #include "otg_whitelist.h"
16581da177e4SLinus Torvalds #endif
16591da177e4SLinus Torvalds 
16603ede760fSOliver Neukum /**
16618d8558d1SAlan Stern  * usb_enumerate_device_otg - FIXME (usbcore-internal)
16623ede760fSOliver Neukum  * @udev: newly addressed device (in ADDRESS state)
16633ede760fSOliver Neukum  *
16648d8558d1SAlan Stern  * Finish enumeration for On-The-Go devices
16653ede760fSOliver Neukum  */
16668d8558d1SAlan Stern static int usb_enumerate_device_otg(struct usb_device *udev)
16671da177e4SLinus Torvalds {
1668d9d16e8aSInaky Perez-Gonzalez 	int err = 0;
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
16711da177e4SLinus Torvalds 	/*
16721da177e4SLinus Torvalds 	 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
16731da177e4SLinus Torvalds 	 * to wake us after we've powered off VBUS; and HNP, switching roles
16741da177e4SLinus Torvalds 	 * "host" to "peripheral".  The OTG descriptor helps figure this out.
16751da177e4SLinus Torvalds 	 */
16761da177e4SLinus Torvalds 	if (!udev->bus->is_b_host
16771da177e4SLinus Torvalds 			&& udev->config
16781da177e4SLinus Torvalds 			&& udev->parent == udev->bus->root_hub) {
16792eb5052eSFelipe Balbi 		struct usb_otg_descriptor	*desc = NULL;
16801da177e4SLinus Torvalds 		struct usb_bus			*bus = udev->bus;
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds 		/* descriptor may appear anywhere in config */
16831da177e4SLinus Torvalds 		if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
16841da177e4SLinus Torvalds 					le16_to_cpu(udev->config[0].desc.wTotalLength),
16851da177e4SLinus Torvalds 					USB_DT_OTG, (void **) &desc) == 0) {
16861da177e4SLinus Torvalds 			if (desc->bmAttributes & USB_OTG_HNP) {
168712c3da34SAlan Stern 				unsigned		port1 = udev->portnum;
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 				dev_info(&udev->dev,
16901da177e4SLinus Torvalds 					"Dual-Role OTG device on %sHNP port\n",
16911da177e4SLinus Torvalds 					(port1 == bus->otg_port)
16921da177e4SLinus Torvalds 						? "" : "non-");
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 				/* enable HNP before suspend, it's simpler */
16951da177e4SLinus Torvalds 				if (port1 == bus->otg_port)
16961da177e4SLinus Torvalds 					bus->b_hnp_enable = 1;
16971da177e4SLinus Torvalds 				err = usb_control_msg(udev,
16981da177e4SLinus Torvalds 					usb_sndctrlpipe(udev, 0),
16991da177e4SLinus Torvalds 					USB_REQ_SET_FEATURE, 0,
17001da177e4SLinus Torvalds 					bus->b_hnp_enable
17011da177e4SLinus Torvalds 						? USB_DEVICE_B_HNP_ENABLE
17021da177e4SLinus Torvalds 						: USB_DEVICE_A_ALT_HNP_SUPPORT,
17031da177e4SLinus Torvalds 					0, NULL, 0, USB_CTRL_SET_TIMEOUT);
17041da177e4SLinus Torvalds 				if (err < 0) {
17051da177e4SLinus Torvalds 					/* OTG MESSAGE: report errors here,
17061da177e4SLinus Torvalds 					 * customize to match your product.
17071da177e4SLinus Torvalds 					 */
17081da177e4SLinus Torvalds 					dev_info(&udev->dev,
1709b9cef6c3SWu Fengguang 						"can't set HNP mode: %d\n",
17101da177e4SLinus Torvalds 						err);
17111da177e4SLinus Torvalds 					bus->b_hnp_enable = 0;
17121da177e4SLinus Torvalds 				}
17131da177e4SLinus Torvalds 			}
17141da177e4SLinus Torvalds 		}
17151da177e4SLinus Torvalds 	}
17161da177e4SLinus Torvalds 
17171da177e4SLinus Torvalds 	if (!is_targeted(udev)) {
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 		/* Maybe it can talk to us, though we can't talk to it.
17201da177e4SLinus Torvalds 		 * (Includes HNP test device.)
17211da177e4SLinus Torvalds 		 */
17221da177e4SLinus Torvalds 		if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
1723634a84f8SDavid Brownell 			err = usb_port_suspend(udev, PMSG_SUSPEND);
17241da177e4SLinus Torvalds 			if (err < 0)
17251da177e4SLinus Torvalds 				dev_dbg(&udev->dev, "HNP fail, %d\n", err);
17261da177e4SLinus Torvalds 		}
1727ffcdc18dSVikram Pandita 		err = -ENOTSUPP;
17281da177e4SLinus Torvalds 		goto fail;
17291da177e4SLinus Torvalds 	}
1730d9d16e8aSInaky Perez-Gonzalez fail:
17311da177e4SLinus Torvalds #endif
1732d9d16e8aSInaky Perez-Gonzalez 	return err;
1733d9d16e8aSInaky Perez-Gonzalez }
17341da177e4SLinus Torvalds 
1735d9d16e8aSInaky Perez-Gonzalez 
1736d9d16e8aSInaky Perez-Gonzalez /**
17378d8558d1SAlan Stern  * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
1738d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
1739d9d16e8aSInaky Perez-Gonzalez  *
1740d9d16e8aSInaky Perez-Gonzalez  * This is only called by usb_new_device() and usb_authorize_device()
1741d9d16e8aSInaky Perez-Gonzalez  * and FIXME -- all comments that apply to them apply here wrt to
1742d9d16e8aSInaky Perez-Gonzalez  * environment.
1743d9d16e8aSInaky Perez-Gonzalez  *
1744d9d16e8aSInaky Perez-Gonzalez  * If the device is WUSB and not authorized, we don't attempt to read
1745d9d16e8aSInaky Perez-Gonzalez  * the string descriptors, as they will be errored out by the device
1746d9d16e8aSInaky Perez-Gonzalez  * until it has been authorized.
1747d9d16e8aSInaky Perez-Gonzalez  */
17488d8558d1SAlan Stern static int usb_enumerate_device(struct usb_device *udev)
1749d9d16e8aSInaky Perez-Gonzalez {
1750d9d16e8aSInaky Perez-Gonzalez 	int err;
1751d9d16e8aSInaky Perez-Gonzalez 
1752d9d16e8aSInaky Perez-Gonzalez 	if (udev->config == NULL) {
1753d9d16e8aSInaky Perez-Gonzalez 		err = usb_get_configuration(udev);
1754d9d16e8aSInaky Perez-Gonzalez 		if (err < 0) {
1755d9d16e8aSInaky Perez-Gonzalez 			dev_err(&udev->dev, "can't read configurations, error %d\n",
1756d9d16e8aSInaky Perez-Gonzalez 				err);
1757d9d16e8aSInaky Perez-Gonzalez 			goto fail;
1758d9d16e8aSInaky Perez-Gonzalez 		}
1759d9d16e8aSInaky Perez-Gonzalez 	}
1760d9d16e8aSInaky Perez-Gonzalez 	if (udev->wusb == 1 && udev->authorized == 0) {
1761d9d16e8aSInaky Perez-Gonzalez 		udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1762d9d16e8aSInaky Perez-Gonzalez 		udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1763d9d16e8aSInaky Perez-Gonzalez 		udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1764d9d16e8aSInaky Perez-Gonzalez 	}
1765d9d16e8aSInaky Perez-Gonzalez 	else {
1766d9d16e8aSInaky Perez-Gonzalez 		/* read the standard strings and cache them if present */
1767d9d16e8aSInaky Perez-Gonzalez 		udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
1768d9d16e8aSInaky Perez-Gonzalez 		udev->manufacturer = usb_cache_string(udev,
1769d9d16e8aSInaky Perez-Gonzalez 						      udev->descriptor.iManufacturer);
1770d9d16e8aSInaky Perez-Gonzalez 		udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
1771d9d16e8aSInaky Perez-Gonzalez 	}
17728d8558d1SAlan Stern 	err = usb_enumerate_device_otg(udev);
1773d9d16e8aSInaky Perez-Gonzalez fail:
1774d9d16e8aSInaky Perez-Gonzalez 	return err;
1775d9d16e8aSInaky Perez-Gonzalez }
1776d9d16e8aSInaky Perez-Gonzalez 
1777d9d16e8aSInaky Perez-Gonzalez 
1778d9d16e8aSInaky Perez-Gonzalez /**
1779d9d16e8aSInaky Perez-Gonzalez  * usb_new_device - perform initial device setup (usbcore-internal)
1780d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
1781d9d16e8aSInaky Perez-Gonzalez  *
17828d8558d1SAlan Stern  * This is called with devices which have been detected but not fully
17838d8558d1SAlan Stern  * enumerated.  The device descriptor is available, but not descriptors
1784d9d16e8aSInaky Perez-Gonzalez  * for any device configuration.  The caller must have locked either
1785d9d16e8aSInaky Perez-Gonzalez  * the parent hub (if udev is a normal device) or else the
1786d9d16e8aSInaky Perez-Gonzalez  * usb_bus_list_lock (if udev is a root hub).  The parent's pointer to
1787d9d16e8aSInaky Perez-Gonzalez  * udev has already been installed, but udev is not yet visible through
1788d9d16e8aSInaky Perez-Gonzalez  * sysfs or other filesystem code.
1789d9d16e8aSInaky Perez-Gonzalez  *
1790d9d16e8aSInaky Perez-Gonzalez  * It will return if the device is configured properly or not.  Zero if
1791d9d16e8aSInaky Perez-Gonzalez  * the interface was registered with the driver core; else a negative
1792d9d16e8aSInaky Perez-Gonzalez  * errno value.
1793d9d16e8aSInaky Perez-Gonzalez  *
1794d9d16e8aSInaky Perez-Gonzalez  * This call is synchronous, and may not be used in an interrupt context.
1795d9d16e8aSInaky Perez-Gonzalez  *
1796d9d16e8aSInaky Perez-Gonzalez  * Only the hub driver or root-hub registrar should ever call this.
1797d9d16e8aSInaky Perez-Gonzalez  */
1798d9d16e8aSInaky Perez-Gonzalez int usb_new_device(struct usb_device *udev)
1799d9d16e8aSInaky Perez-Gonzalez {
1800d9d16e8aSInaky Perez-Gonzalez 	int err;
1801d9d16e8aSInaky Perez-Gonzalez 
180216985408SDan Streetman 	if (udev->parent) {
18036cd13201SAlan Stern 		/* Increment the parent's count of unsuspended children */
18046cd13201SAlan Stern 		usb_autoresume_device(udev->parent);
18056cd13201SAlan Stern 
180616985408SDan Streetman 		/* Initialize non-root-hub device wakeup to disabled;
180716985408SDan Streetman 		 * device (un)configuration controls wakeup capable
180816985408SDan Streetman 		 * sysfs power/wakeup controls wakeup enabled/disabled
180916985408SDan Streetman 		 */
181016985408SDan Streetman 		device_init_wakeup(&udev->dev, 0);
181116985408SDan Streetman 		device_set_wakeup_enable(&udev->dev, 1);
181216985408SDan Streetman 	}
181316985408SDan Streetman 
18148d8558d1SAlan Stern 	usb_detect_quirks(udev);
18158d8558d1SAlan Stern 	err = usb_enumerate_device(udev);	/* Read descriptors */
1816d9d16e8aSInaky Perez-Gonzalez 	if (err < 0)
1817d9d16e8aSInaky Perez-Gonzalez 		goto fail;
1818c6515272SSarah Sharp 	dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
1819c6515272SSarah Sharp 			udev->devnum, udev->bus->busnum,
1820c6515272SSarah Sharp 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
18219f8b17e6SKay Sievers 	/* export the usbdev device-node for libusb */
18229f8b17e6SKay Sievers 	udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
18239f8b17e6SKay Sievers 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
18249f8b17e6SKay Sievers 
18256cd13201SAlan Stern 	/* Tell the world! */
18266cd13201SAlan Stern 	announce_device(udev);
1827195af2ccSAlan Stern 
1828927bc916SRafael J. Wysocki 	device_enable_async_suspend(&udev->dev);
1829782da727SAlan Stern 	/* Register the device.  The device driver is responsible
18303b23dd6fSAlan Stern 	 * for configuring the device and invoking the add-device
18313b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
1832782da727SAlan Stern 	 */
18331da177e4SLinus Torvalds 	err = device_add(&udev->dev);
18341da177e4SLinus Torvalds 	if (err) {
18351da177e4SLinus Torvalds 		dev_err(&udev->dev, "can't device_add, error %d\n", err);
18361da177e4SLinus Torvalds 		goto fail;
18371da177e4SLinus Torvalds 	}
18389ad3d6ccSAlan Stern 
18393b23dd6fSAlan Stern 	(void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
1840c066475eSGreg Kroah-Hartman 	return err;
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds fail:
18431da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
18446cd13201SAlan Stern 	usb_stop_pm(udev);
1845d9d16e8aSInaky Perez-Gonzalez 	return err;
18461da177e4SLinus Torvalds }
18471da177e4SLinus Torvalds 
184893993a0aSInaky Perez-Gonzalez 
184993993a0aSInaky Perez-Gonzalez /**
1850fd39c86bSRandy Dunlap  * usb_deauthorize_device - deauthorize a device (usbcore-internal)
1851fd39c86bSRandy Dunlap  * @usb_dev: USB device
1852fd39c86bSRandy Dunlap  *
1853fd39c86bSRandy Dunlap  * Move the USB device to a very basic state where interfaces are disabled
1854fd39c86bSRandy Dunlap  * and the device is in fact unconfigured and unusable.
185593993a0aSInaky Perez-Gonzalez  *
185693993a0aSInaky Perez-Gonzalez  * We share a lock (that we have) with device_del(), so we need to
185793993a0aSInaky Perez-Gonzalez  * defer its call.
185893993a0aSInaky Perez-Gonzalez  */
185993993a0aSInaky Perez-Gonzalez int usb_deauthorize_device(struct usb_device *usb_dev)
186093993a0aSInaky Perez-Gonzalez {
186193993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
186293993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 0)
186393993a0aSInaky Perez-Gonzalez 		goto out_unauthorized;
1864da307123SAlan Stern 
186593993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 0;
186693993a0aSInaky Perez-Gonzalez 	usb_set_configuration(usb_dev, -1);
1867da307123SAlan Stern 
1868da307123SAlan Stern 	kfree(usb_dev->product);
186993993a0aSInaky Perez-Gonzalez 	usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1870da307123SAlan Stern 	kfree(usb_dev->manufacturer);
187193993a0aSInaky Perez-Gonzalez 	usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1872da307123SAlan Stern 	kfree(usb_dev->serial);
187393993a0aSInaky Perez-Gonzalez 	usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1874da307123SAlan Stern 
1875da307123SAlan Stern 	usb_destroy_configuration(usb_dev);
187693993a0aSInaky Perez-Gonzalez 	usb_dev->descriptor.bNumConfigurations = 0;
1877da307123SAlan Stern 
187893993a0aSInaky Perez-Gonzalez out_unauthorized:
187993993a0aSInaky Perez-Gonzalez 	usb_unlock_device(usb_dev);
188093993a0aSInaky Perez-Gonzalez 	return 0;
188193993a0aSInaky Perez-Gonzalez }
188293993a0aSInaky Perez-Gonzalez 
188393993a0aSInaky Perez-Gonzalez 
188493993a0aSInaky Perez-Gonzalez int usb_authorize_device(struct usb_device *usb_dev)
188593993a0aSInaky Perez-Gonzalez {
188693993a0aSInaky Perez-Gonzalez 	int result = 0, c;
1887da307123SAlan Stern 
188893993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
188993993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 1)
189093993a0aSInaky Perez-Gonzalez 		goto out_authorized;
1891da307123SAlan Stern 
189293993a0aSInaky Perez-Gonzalez 	result = usb_autoresume_device(usb_dev);
189393993a0aSInaky Perez-Gonzalez 	if (result < 0) {
189493993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev,
189593993a0aSInaky Perez-Gonzalez 			"can't autoresume for authorization: %d\n", result);
189693993a0aSInaky Perez-Gonzalez 		goto error_autoresume;
189793993a0aSInaky Perez-Gonzalez 	}
189893993a0aSInaky Perez-Gonzalez 	result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
189993993a0aSInaky Perez-Gonzalez 	if (result < 0) {
190093993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev, "can't re-read device descriptor for "
190193993a0aSInaky Perez-Gonzalez 			"authorization: %d\n", result);
190293993a0aSInaky Perez-Gonzalez 		goto error_device_descriptor;
190393993a0aSInaky Perez-Gonzalez 	}
1904da307123SAlan Stern 
1905da307123SAlan Stern 	kfree(usb_dev->product);
1906da307123SAlan Stern 	usb_dev->product = NULL;
1907da307123SAlan Stern 	kfree(usb_dev->manufacturer);
1908da307123SAlan Stern 	usb_dev->manufacturer = NULL;
1909da307123SAlan Stern 	kfree(usb_dev->serial);
1910da307123SAlan Stern 	usb_dev->serial = NULL;
1911da307123SAlan Stern 
191293993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 1;
19138d8558d1SAlan Stern 	result = usb_enumerate_device(usb_dev);
191493993a0aSInaky Perez-Gonzalez 	if (result < 0)
19158d8558d1SAlan Stern 		goto error_enumerate;
191693993a0aSInaky Perez-Gonzalez 	/* Choose and set the configuration.  This registers the interfaces
191793993a0aSInaky Perez-Gonzalez 	 * with the driver core and lets interface drivers bind to them.
191893993a0aSInaky Perez-Gonzalez 	 */
1919b5ea060fSGreg Kroah-Hartman 	c = usb_choose_configuration(usb_dev);
192093993a0aSInaky Perez-Gonzalez 	if (c >= 0) {
192193993a0aSInaky Perez-Gonzalez 		result = usb_set_configuration(usb_dev, c);
192293993a0aSInaky Perez-Gonzalez 		if (result) {
192393993a0aSInaky Perez-Gonzalez 			dev_err(&usb_dev->dev,
192493993a0aSInaky Perez-Gonzalez 				"can't set config #%d, error %d\n", c, result);
192593993a0aSInaky Perez-Gonzalez 			/* This need not be fatal.  The user can try to
192693993a0aSInaky Perez-Gonzalez 			 * set other configurations. */
192793993a0aSInaky Perez-Gonzalez 		}
192893993a0aSInaky Perez-Gonzalez 	}
192993993a0aSInaky Perez-Gonzalez 	dev_info(&usb_dev->dev, "authorized to connect\n");
1930da307123SAlan Stern 
19318d8558d1SAlan Stern error_enumerate:
193293993a0aSInaky Perez-Gonzalez error_device_descriptor:
1933da307123SAlan Stern 	usb_autosuspend_device(usb_dev);
193493993a0aSInaky Perez-Gonzalez error_autoresume:
193593993a0aSInaky Perez-Gonzalez out_authorized:
193693993a0aSInaky Perez-Gonzalez 	usb_unlock_device(usb_dev);	// complements locktree
193793993a0aSInaky Perez-Gonzalez 	return result;
193893993a0aSInaky Perez-Gonzalez }
193993993a0aSInaky Perez-Gonzalez 
194093993a0aSInaky Perez-Gonzalez 
19410165de09SInaky Perez-Gonzalez /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
19420165de09SInaky Perez-Gonzalez static unsigned hub_is_wusb(struct usb_hub *hub)
19430165de09SInaky Perez-Gonzalez {
19440165de09SInaky Perez-Gonzalez 	struct usb_hcd *hcd;
19450165de09SInaky Perez-Gonzalez 	if (hub->hdev->parent != NULL)  /* not a root hub? */
19460165de09SInaky Perez-Gonzalez 		return 0;
19470165de09SInaky Perez-Gonzalez 	hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
19480165de09SInaky Perez-Gonzalez 	return hcd->wireless;
19490165de09SInaky Perez-Gonzalez }
19500165de09SInaky Perez-Gonzalez 
19510165de09SInaky Perez-Gonzalez 
19521da177e4SLinus Torvalds #define PORT_RESET_TRIES	5
19531da177e4SLinus Torvalds #define SET_ADDRESS_TRIES	2
19541da177e4SLinus Torvalds #define GET_DESCRIPTOR_TRIES	2
19551da177e4SLinus Torvalds #define SET_CONFIG_TRIES	(2 * (use_both_schemes + 1))
19561da177e4SLinus Torvalds #define USE_NEW_SCHEME(i)	((i) / 2 == old_scheme_first)
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds #define HUB_ROOT_RESET_TIME	50	/* times are in msec */
19591da177e4SLinus Torvalds #define HUB_SHORT_RESET_TIME	10
19601da177e4SLinus Torvalds #define HUB_LONG_RESET_TIME	200
19611da177e4SLinus Torvalds #define HUB_RESET_TIMEOUT	500
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds static int hub_port_wait_reset(struct usb_hub *hub, int port1,
19641da177e4SLinus Torvalds 				struct usb_device *udev, unsigned int delay)
19651da177e4SLinus Torvalds {
19661da177e4SLinus Torvalds 	int delay_time, ret;
19671da177e4SLinus Torvalds 	u16 portstatus;
19681da177e4SLinus Torvalds 	u16 portchange;
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds 	for (delay_time = 0;
19711da177e4SLinus Torvalds 			delay_time < HUB_RESET_TIMEOUT;
19721da177e4SLinus Torvalds 			delay_time += delay) {
19731da177e4SLinus Torvalds 		/* wait to give the device a chance to reset */
19741da177e4SLinus Torvalds 		msleep(delay);
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 		/* read and decode port status */
19771da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
19781da177e4SLinus Torvalds 		if (ret < 0)
19791da177e4SLinus Torvalds 			return ret;
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 		/* Device went away? */
19821da177e4SLinus Torvalds 		if (!(portstatus & USB_PORT_STAT_CONNECTION))
19831da177e4SLinus Torvalds 			return -ENOTCONN;
19841da177e4SLinus Torvalds 
1985dd4dd19eSAlan Stern 		/* bomb out completely if the connection bounced */
19861da177e4SLinus Torvalds 		if ((portchange & USB_PORT_STAT_C_CONNECTION))
1987dd4dd19eSAlan Stern 			return -ENOTCONN;
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds 		/* if we`ve finished resetting, then break out of the loop */
19901da177e4SLinus Torvalds 		if (!(portstatus & USB_PORT_STAT_RESET) &&
19911da177e4SLinus Torvalds 		    (portstatus & USB_PORT_STAT_ENABLE)) {
19920165de09SInaky Perez-Gonzalez 			if (hub_is_wusb(hub))
19930165de09SInaky Perez-Gonzalez 				udev->speed = USB_SPEED_VARIABLE;
19940165de09SInaky Perez-Gonzalez 			else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
19951da177e4SLinus Torvalds 				udev->speed = USB_SPEED_HIGH;
19961da177e4SLinus Torvalds 			else if (portstatus & USB_PORT_STAT_LOW_SPEED)
19971da177e4SLinus Torvalds 				udev->speed = USB_SPEED_LOW;
19981da177e4SLinus Torvalds 			else
19991da177e4SLinus Torvalds 				udev->speed = USB_SPEED_FULL;
20001da177e4SLinus Torvalds 			return 0;
20011da177e4SLinus Torvalds 		}
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds 		/* switch to the long delay after two short delay failures */
20041da177e4SLinus Torvalds 		if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
20051da177e4SLinus Torvalds 			delay = HUB_LONG_RESET_TIME;
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
20081da177e4SLinus Torvalds 			"port %d not reset yet, waiting %dms\n",
20091da177e4SLinus Torvalds 			port1, delay);
20101da177e4SLinus Torvalds 	}
20111da177e4SLinus Torvalds 
20121da177e4SLinus Torvalds 	return -EBUSY;
20131da177e4SLinus Torvalds }
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds static int hub_port_reset(struct usb_hub *hub, int port1,
20161da177e4SLinus Torvalds 				struct usb_device *udev, unsigned int delay)
20171da177e4SLinus Torvalds {
20181da177e4SLinus Torvalds 	int i, status;
2019a5f0efabSSarah Sharp 	struct usb_hcd *hcd;
20201da177e4SLinus Torvalds 
2021a5f0efabSSarah Sharp 	hcd = bus_to_hcd(udev->bus);
202232fe0198SAlan Stern 	/* Block EHCI CF initialization during the port reset.
202332fe0198SAlan Stern 	 * Some companion controllers don't like it when they mix.
202432fe0198SAlan Stern 	 */
202532fe0198SAlan Stern 	down_read(&ehci_cf_port_reset_rwsem);
202632fe0198SAlan Stern 
20271da177e4SLinus Torvalds 	/* Reset the port */
20281da177e4SLinus Torvalds 	for (i = 0; i < PORT_RESET_TRIES; i++) {
20291da177e4SLinus Torvalds 		status = set_port_feature(hub->hdev,
20301da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_RESET);
20311da177e4SLinus Torvalds 		if (status)
20321da177e4SLinus Torvalds 			dev_err(hub->intfdev,
20331da177e4SLinus Torvalds 					"cannot reset port %d (err = %d)\n",
20341da177e4SLinus Torvalds 					port1, status);
20351da177e4SLinus Torvalds 		else {
20361da177e4SLinus Torvalds 			status = hub_port_wait_reset(hub, port1, udev, delay);
2037dd16525bSDavid Brownell 			if (status && status != -ENOTCONN)
20381da177e4SLinus Torvalds 				dev_dbg(hub->intfdev,
20391da177e4SLinus Torvalds 						"port_wait_reset: err = %d\n",
20401da177e4SLinus Torvalds 						status);
20411da177e4SLinus Torvalds 		}
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 		/* return on disconnect or reset */
20441da177e4SLinus Torvalds 		switch (status) {
20451da177e4SLinus Torvalds 		case 0:
2046b789696aSDavid Brownell 			/* TRSTRCY = 10 ms; plus some extra */
2047b789696aSDavid Brownell 			msleep(10 + 40);
20484953d141SDavid Vrabel 			update_address(udev, 0);
2049a5f0efabSSarah Sharp 			if (hcd->driver->reset_device) {
2050a5f0efabSSarah Sharp 				status = hcd->driver->reset_device(hcd, udev);
2051a5f0efabSSarah Sharp 				if (status < 0) {
2052a5f0efabSSarah Sharp 					dev_err(&udev->dev, "Cannot reset "
2053a5f0efabSSarah Sharp 							"HCD device state\n");
2054a5f0efabSSarah Sharp 					break;
2055a5f0efabSSarah Sharp 				}
2056a5f0efabSSarah Sharp 			}
20571da177e4SLinus Torvalds 			/* FALL THROUGH */
20581da177e4SLinus Torvalds 		case -ENOTCONN:
20591da177e4SLinus Torvalds 		case -ENODEV:
20601da177e4SLinus Torvalds 			clear_port_feature(hub->hdev,
20611da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_C_RESET);
20621da177e4SLinus Torvalds 			/* FIXME need disconnect() for NOTATTACHED device */
20631da177e4SLinus Torvalds 			usb_set_device_state(udev, status
20641da177e4SLinus Torvalds 					? USB_STATE_NOTATTACHED
20651da177e4SLinus Torvalds 					: USB_STATE_DEFAULT);
206632fe0198SAlan Stern 			goto done;
20671da177e4SLinus Torvalds 		}
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
20701da177e4SLinus Torvalds 			"port %d not enabled, trying reset again...\n",
20711da177e4SLinus Torvalds 			port1);
20721da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
20731da177e4SLinus Torvalds 	}
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds 	dev_err (hub->intfdev,
20761da177e4SLinus Torvalds 		"Cannot enable port %i.  Maybe the USB cable is bad?\n",
20771da177e4SLinus Torvalds 		port1);
20781da177e4SLinus Torvalds 
207932fe0198SAlan Stern  done:
208032fe0198SAlan Stern 	up_read(&ehci_cf_port_reset_rwsem);
20811da177e4SLinus Torvalds 	return status;
20821da177e4SLinus Torvalds }
20831da177e4SLinus Torvalds 
2084d388dab7SAlan Stern #ifdef	CONFIG_PM
20851da177e4SLinus Torvalds 
2086b01b03f3SAlan Stern #define MASK_BITS	(USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION | \
2087b01b03f3SAlan Stern 				USB_PORT_STAT_SUSPEND)
2088b01b03f3SAlan Stern #define WANT_BITS	(USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION)
2089b01b03f3SAlan Stern 
2090b01b03f3SAlan Stern /* Determine whether the device on a port is ready for a normal resume,
2091b01b03f3SAlan Stern  * is ready for a reset-resume, or should be disconnected.
2092b01b03f3SAlan Stern  */
2093b01b03f3SAlan Stern static int check_port_resume_type(struct usb_device *udev,
2094b01b03f3SAlan Stern 		struct usb_hub *hub, int port1,
2095b01b03f3SAlan Stern 		int status, unsigned portchange, unsigned portstatus)
2096b01b03f3SAlan Stern {
2097b01b03f3SAlan Stern 	/* Is the device still present? */
2098b01b03f3SAlan Stern 	if (status || (portstatus & MASK_BITS) != WANT_BITS) {
2099b01b03f3SAlan Stern 		if (status >= 0)
2100b01b03f3SAlan Stern 			status = -ENODEV;
2101b01b03f3SAlan Stern 	}
2102b01b03f3SAlan Stern 
210386c57edfSAlan Stern 	/* Can't do a normal resume if the port isn't enabled,
210486c57edfSAlan Stern 	 * so try a reset-resume instead.
210586c57edfSAlan Stern 	 */
210686c57edfSAlan Stern 	else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
210786c57edfSAlan Stern 		if (udev->persist_enabled)
210886c57edfSAlan Stern 			udev->reset_resume = 1;
210986c57edfSAlan Stern 		else
2110b01b03f3SAlan Stern 			status = -ENODEV;
211186c57edfSAlan Stern 	}
2112b01b03f3SAlan Stern 
2113b01b03f3SAlan Stern 	if (status) {
2114b01b03f3SAlan Stern 		dev_dbg(hub->intfdev,
2115b01b03f3SAlan Stern 				"port %d status %04x.%04x after resume, %d\n",
2116b01b03f3SAlan Stern 				port1, portchange, portstatus, status);
2117b01b03f3SAlan Stern 	} else if (udev->reset_resume) {
2118b01b03f3SAlan Stern 
2119b01b03f3SAlan Stern 		/* Late port handoff can set status-change bits */
2120b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION)
2121b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2122b01b03f3SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
2123b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE)
2124b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2125b01b03f3SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
2126b01b03f3SAlan Stern 	}
2127b01b03f3SAlan Stern 
2128b01b03f3SAlan Stern 	return status;
2129b01b03f3SAlan Stern }
2130b01b03f3SAlan Stern 
21311da177e4SLinus Torvalds #ifdef	CONFIG_USB_SUSPEND
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds /*
2134140d8f68SAlan Stern  * usb_port_suspend - suspend a usb device's upstream port
2135624d6c07SAlan Stern  * @udev: device that's no longer in active use, not a root hub
21365edbfb7cSDavid Brownell  * Context: must be able to sleep; device not locked; pm locks held
21371da177e4SLinus Torvalds  *
21381da177e4SLinus Torvalds  * Suspends a USB device that isn't in active use, conserving power.
21391da177e4SLinus Torvalds  * Devices may wake out of a suspend, if anything important happens,
21401da177e4SLinus Torvalds  * using the remote wakeup mechanism.  They may also be taken out of
2141140d8f68SAlan Stern  * suspend by the host, using usb_port_resume().  It's also routine
21421da177e4SLinus Torvalds  * to disconnect devices while they are suspended.
21431da177e4SLinus Torvalds  *
2144390a8c34SDavid Brownell  * This only affects the USB hardware for a device; its interfaces
2145390a8c34SDavid Brownell  * (and, for hubs, child devices) must already have been suspended.
2146390a8c34SDavid Brownell  *
2147624d6c07SAlan Stern  * Selective port suspend reduces power; most suspended devices draw
2148624d6c07SAlan Stern  * less than 500 uA.  It's also used in OTG, along with remote wakeup.
2149624d6c07SAlan Stern  * All devices below the suspended port are also suspended.
2150624d6c07SAlan Stern  *
2151624d6c07SAlan Stern  * Devices leave suspend state when the host wakes them up.  Some devices
2152624d6c07SAlan Stern  * also support "remote wakeup", where the device can activate the USB
2153624d6c07SAlan Stern  * tree above them to deliver data, such as a keypress or packet.  In
2154624d6c07SAlan Stern  * some cases, this wakes the USB host.
2155624d6c07SAlan Stern  *
21561da177e4SLinus Torvalds  * Suspending OTG devices may trigger HNP, if that's been enabled
21571da177e4SLinus Torvalds  * between a pair of dual-role devices.  That will change roles, such
21581da177e4SLinus Torvalds  * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
21591da177e4SLinus Torvalds  *
21604956eccdSAlan Stern  * Devices on USB hub ports have only one "suspend" state, corresponding
21614956eccdSAlan Stern  * to ACPI D2, "may cause the device to lose some context".
21624956eccdSAlan Stern  * State transitions include:
21634956eccdSAlan Stern  *
21644956eccdSAlan Stern  *   - suspend, resume ... when the VBUS power link stays live
21654956eccdSAlan Stern  *   - suspend, disconnect ... VBUS lost
21664956eccdSAlan Stern  *
21674956eccdSAlan Stern  * Once VBUS drop breaks the circuit, the port it's using has to go through
21684956eccdSAlan Stern  * normal re-enumeration procedures, starting with enabling VBUS power.
21694956eccdSAlan Stern  * Other than re-initializing the hub (plug/unplug, except for root hubs),
21704956eccdSAlan Stern  * Linux (2.6) currently has NO mechanisms to initiate that:  no khubd
21714956eccdSAlan Stern  * timer, no SRP, no requests through sysfs.
21724956eccdSAlan Stern  *
21734956eccdSAlan Stern  * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
21744956eccdSAlan Stern  * the root hub for their bus goes into global suspend ... so we don't
21754956eccdSAlan Stern  * (falsely) update the device power state to say it suspended.
21764956eccdSAlan Stern  *
21771da177e4SLinus Torvalds  * Returns 0 on success, else negative errno.
21781da177e4SLinus Torvalds  */
217965bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
21801da177e4SLinus Torvalds {
2181624d6c07SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2182624d6c07SAlan Stern 	int		port1 = udev->portnum;
2183624d6c07SAlan Stern 	int		status;
21844956eccdSAlan Stern 
2185624d6c07SAlan Stern 	// dev_dbg(hub->intfdev, "suspend port %d\n", port1);
2186624d6c07SAlan Stern 
2187624d6c07SAlan Stern 	/* enable remote wakeup when appropriate; this lets the device
2188624d6c07SAlan Stern 	 * wake up the upstream hub (including maybe the root hub).
2189624d6c07SAlan Stern 	 *
2190624d6c07SAlan Stern 	 * NOTE:  OTG devices may issue remote wakeup (or SRP) even when
2191624d6c07SAlan Stern 	 * we don't explicitly enable it here.
2192624d6c07SAlan Stern 	 */
2193624d6c07SAlan Stern 	if (udev->do_remote_wakeup) {
2194624d6c07SAlan Stern 		status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2195624d6c07SAlan Stern 				USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2196624d6c07SAlan Stern 				USB_DEVICE_REMOTE_WAKEUP, 0,
2197624d6c07SAlan Stern 				NULL, 0,
2198624d6c07SAlan Stern 				USB_CTRL_SET_TIMEOUT);
21990c487206SOliver Neukum 		if (status) {
2200624d6c07SAlan Stern 			dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2201624d6c07SAlan Stern 					status);
22020c487206SOliver Neukum 			/* bail if autosuspend is requested */
22030c487206SOliver Neukum 			if (msg.event & PM_EVENT_AUTO)
22040c487206SOliver Neukum 				return status;
22050c487206SOliver Neukum 		}
2206624d6c07SAlan Stern 	}
2207624d6c07SAlan Stern 
2208624d6c07SAlan Stern 	/* see 7.1.7.6 */
2209624d6c07SAlan Stern 	status = set_port_feature(hub->hdev, port1, USB_PORT_FEAT_SUSPEND);
2210624d6c07SAlan Stern 	if (status) {
2211624d6c07SAlan Stern 		dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2212624d6c07SAlan Stern 				port1, status);
2213624d6c07SAlan Stern 		/* paranoia:  "should not happen" */
22140c487206SOliver Neukum 		if (udev->do_remote_wakeup)
2215624d6c07SAlan Stern 			(void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2216624d6c07SAlan Stern 				USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2217624d6c07SAlan Stern 				USB_DEVICE_REMOTE_WAKEUP, 0,
2218624d6c07SAlan Stern 				NULL, 0,
2219624d6c07SAlan Stern 				USB_CTRL_SET_TIMEOUT);
2220624d6c07SAlan Stern 	} else {
2221624d6c07SAlan Stern 		/* device has up to 10 msec to fully suspend */
2222624d6c07SAlan Stern 		dev_dbg(&udev->dev, "usb %ssuspend\n",
222365bfd296SAlan Stern 				(msg.event & PM_EVENT_AUTO ? "auto-" : ""));
2224624d6c07SAlan Stern 		usb_set_device_state(udev, USB_STATE_SUSPENDED);
2225624d6c07SAlan Stern 		msleep(10);
2226624d6c07SAlan Stern 	}
22274956eccdSAlan Stern 	return status;
22281da177e4SLinus Torvalds }
2229f3f3253dSDavid Brownell 
22301da177e4SLinus Torvalds /*
2231390a8c34SDavid Brownell  * If the USB "suspend" state is in use (rather than "global suspend"),
2232390a8c34SDavid Brownell  * many devices will be individually taken out of suspend state using
223354515fe5SAlan Stern  * special "resume" signaling.  This routine kicks in shortly after
22341da177e4SLinus Torvalds  * hardware resume signaling is finished, either because of selective
22351da177e4SLinus Torvalds  * resume (by host) or remote wakeup (by device) ... now see what changed
22361da177e4SLinus Torvalds  * in the tree that's rooted at this device.
223754515fe5SAlan Stern  *
223854515fe5SAlan Stern  * If @udev->reset_resume is set then the device is reset before the
223954515fe5SAlan Stern  * status check is done.
22401da177e4SLinus Torvalds  */
2241140d8f68SAlan Stern static int finish_port_resume(struct usb_device *udev)
22421da177e4SLinus Torvalds {
224354515fe5SAlan Stern 	int	status = 0;
22441da177e4SLinus Torvalds 	u16	devstatus;
22451da177e4SLinus Torvalds 
22461da177e4SLinus Torvalds 	/* caller owns the udev device lock */
2247b9cef6c3SWu Fengguang 	dev_dbg(&udev->dev, "%s\n",
2248b9cef6c3SWu Fengguang 		udev->reset_resume ? "finish reset-resume" : "finish resume");
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds 	/* usb ch9 identifies four variants of SUSPENDED, based on what
22511da177e4SLinus Torvalds 	 * state the device resumes to.  Linux currently won't see the
22521da177e4SLinus Torvalds 	 * first two on the host side; they'd be inside hub_port_init()
22531da177e4SLinus Torvalds 	 * during many timeouts, but khubd can't suspend until later.
22541da177e4SLinus Torvalds 	 */
22551da177e4SLinus Torvalds 	usb_set_device_state(udev, udev->actconfig
22561da177e4SLinus Torvalds 			? USB_STATE_CONFIGURED
22571da177e4SLinus Torvalds 			: USB_STATE_ADDRESS);
22581da177e4SLinus Torvalds 
225954515fe5SAlan Stern 	/* 10.5.4.5 says not to reset a suspended port if the attached
226054515fe5SAlan Stern 	 * device is enabled for remote wakeup.  Hence the reset
226154515fe5SAlan Stern 	 * operation is carried out here, after the port has been
226254515fe5SAlan Stern 	 * resumed.
226354515fe5SAlan Stern 	 */
226454515fe5SAlan Stern 	if (udev->reset_resume)
226586c57edfSAlan Stern  retry_reset_resume:
2266742120c6SMing Lei 		status = usb_reset_and_verify_device(udev);
226754515fe5SAlan Stern 
22681da177e4SLinus Torvalds  	/* 10.5.4.5 says be sure devices in the tree are still there.
22691da177e4SLinus Torvalds  	 * For now let's assume the device didn't go crazy on resume,
22701da177e4SLinus Torvalds 	 * and device drivers will know about any resume quirks.
22711da177e4SLinus Torvalds 	 */
227254515fe5SAlan Stern 	if (status == 0) {
227346dede46SAlan Stern 		devstatus = 0;
22741da177e4SLinus Torvalds 		status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2275b40b7a90SAlan Stern 		if (status >= 0)
227646dede46SAlan Stern 			status = (status > 0 ? 0 : -ENODEV);
227786c57edfSAlan Stern 
227886c57edfSAlan Stern 		/* If a normal resume failed, try doing a reset-resume */
227986c57edfSAlan Stern 		if (status && !udev->reset_resume && udev->persist_enabled) {
228086c57edfSAlan Stern 			dev_dbg(&udev->dev, "retry with reset-resume\n");
228186c57edfSAlan Stern 			udev->reset_resume = 1;
228286c57edfSAlan Stern 			goto retry_reset_resume;
228386c57edfSAlan Stern 		}
228454515fe5SAlan Stern 	}
2285b40b7a90SAlan Stern 
2286624d6c07SAlan Stern 	if (status) {
2287624d6c07SAlan Stern 		dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
22881da177e4SLinus Torvalds 				status);
2289624d6c07SAlan Stern 	} else if (udev->actconfig) {
22901da177e4SLinus Torvalds 		le16_to_cpus(&devstatus);
2291686314cfSAlan Stern 		if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
22921da177e4SLinus Torvalds 			status = usb_control_msg(udev,
22931da177e4SLinus Torvalds 					usb_sndctrlpipe(udev, 0),
22941da177e4SLinus Torvalds 					USB_REQ_CLEAR_FEATURE,
22951da177e4SLinus Torvalds 						USB_RECIP_DEVICE,
22961da177e4SLinus Torvalds 					USB_DEVICE_REMOTE_WAKEUP, 0,
22971da177e4SLinus Torvalds 					NULL, 0,
22981da177e4SLinus Torvalds 					USB_CTRL_SET_TIMEOUT);
2299a8e7c565SAlan Stern 			if (status)
2300b9cef6c3SWu Fengguang 				dev_dbg(&udev->dev,
2301b9cef6c3SWu Fengguang 					"disable remote wakeup, status %d\n",
2302b9cef6c3SWu Fengguang 					status);
23034bf0ba86SAlan Stern 		}
23041da177e4SLinus Torvalds 		status = 0;
23051da177e4SLinus Torvalds 	}
23061da177e4SLinus Torvalds 	return status;
23071da177e4SLinus Torvalds }
23081da177e4SLinus Torvalds 
2309624d6c07SAlan Stern /*
2310624d6c07SAlan Stern  * usb_port_resume - re-activate a suspended usb device's upstream port
2311624d6c07SAlan Stern  * @udev: device to re-activate, not a root hub
2312624d6c07SAlan Stern  * Context: must be able to sleep; device not locked; pm locks held
2313624d6c07SAlan Stern  *
2314624d6c07SAlan Stern  * This will re-activate the suspended device, increasing power usage
2315624d6c07SAlan Stern  * while letting drivers communicate again with its endpoints.
2316624d6c07SAlan Stern  * USB resume explicitly guarantees that the power session between
2317624d6c07SAlan Stern  * the host and the device is the same as it was when the device
2318624d6c07SAlan Stern  * suspended.
2319624d6c07SAlan Stern  *
2320feccc30dSAlan Stern  * If @udev->reset_resume is set then this routine won't check that the
2321feccc30dSAlan Stern  * port is still enabled.  Furthermore, finish_port_resume() above will
232254515fe5SAlan Stern  * reset @udev.  The end result is that a broken power session can be
232354515fe5SAlan Stern  * recovered and @udev will appear to persist across a loss of VBUS power.
232454515fe5SAlan Stern  *
232554515fe5SAlan Stern  * For example, if a host controller doesn't maintain VBUS suspend current
232654515fe5SAlan Stern  * during a system sleep or is reset when the system wakes up, all the USB
232754515fe5SAlan Stern  * power sessions below it will be broken.  This is especially troublesome
232854515fe5SAlan Stern  * for mass-storage devices containing mounted filesystems, since the
232954515fe5SAlan Stern  * device will appear to have disconnected and all the memory mappings
233054515fe5SAlan Stern  * to it will be lost.  Using the USB_PERSIST facility, the device can be
233154515fe5SAlan Stern  * made to appear as if it had not disconnected.
233254515fe5SAlan Stern  *
2333742120c6SMing Lei  * This facility can be dangerous.  Although usb_reset_and_verify_device() makes
2334feccc30dSAlan Stern  * every effort to insure that the same device is present after the
233554515fe5SAlan Stern  * reset as before, it cannot provide a 100% guarantee.  Furthermore it's
233654515fe5SAlan Stern  * quite possible for a device to remain unaltered but its media to be
233754515fe5SAlan Stern  * changed.  If the user replaces a flash memory card while the system is
233854515fe5SAlan Stern  * asleep, he will have only himself to blame when the filesystem on the
233954515fe5SAlan Stern  * new card is corrupted and the system crashes.
234054515fe5SAlan Stern  *
2341624d6c07SAlan Stern  * Returns 0 on success, else negative errno.
2342624d6c07SAlan Stern  */
234365bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
23441da177e4SLinus Torvalds {
2345624d6c07SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2346624d6c07SAlan Stern 	int		port1 = udev->portnum;
23471da177e4SLinus Torvalds 	int		status;
2348d25450c6SAlan Stern 	u16		portchange, portstatus;
2349d25450c6SAlan Stern 
2350d25450c6SAlan Stern 	/* Skip the initial Clear-Suspend step for a remote wakeup */
2351d25450c6SAlan Stern 	status = hub_port_status(hub, port1, &portstatus, &portchange);
2352d25450c6SAlan Stern 	if (status == 0 && !(portstatus & USB_PORT_STAT_SUSPEND))
2353d25450c6SAlan Stern 		goto SuspendCleared;
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds 	// dev_dbg(hub->intfdev, "resume port %d\n", port1);
23561da177e4SLinus Torvalds 
2357d5cbad4bSAlan Stern 	set_bit(port1, hub->busy_bits);
2358d5cbad4bSAlan Stern 
23591da177e4SLinus Torvalds 	/* see 7.1.7.7; affects power usage, but not budgeting */
23601da177e4SLinus Torvalds 	status = clear_port_feature(hub->hdev,
23611da177e4SLinus Torvalds 			port1, USB_PORT_FEAT_SUSPEND);
23621da177e4SLinus Torvalds 	if (status) {
2363624d6c07SAlan Stern 		dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
23641da177e4SLinus Torvalds 				port1, status);
23651da177e4SLinus Torvalds 	} else {
23661da177e4SLinus Torvalds 		/* drive resume for at least 20 msec */
2367645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n",
236865bfd296SAlan Stern 				(msg.event & PM_EVENT_AUTO ? "auto-" : ""));
23691da177e4SLinus Torvalds 		msleep(25);
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds 		/* Virtual root hubs can trigger on GET_PORT_STATUS to
23721da177e4SLinus Torvalds 		 * stop resume signaling.  Then finish the resume
23731da177e4SLinus Torvalds 		 * sequence.
23741da177e4SLinus Torvalds 		 */
2375d25450c6SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
237654515fe5SAlan Stern 
23771da177e4SLinus Torvalds 		/* TRSMRCY = 10 msec */
23781da177e4SLinus Torvalds 		msleep(10);
23791da177e4SLinus Torvalds 	}
2380b01b03f3SAlan Stern 
2381b01b03f3SAlan Stern  SuspendCleared:
2382b01b03f3SAlan Stern 	if (status == 0) {
2383b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_SUSPEND)
2384b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2385b01b03f3SAlan Stern 					USB_PORT_FEAT_C_SUSPEND);
23861da177e4SLinus Torvalds 	}
23871da177e4SLinus Torvalds 
2388d5cbad4bSAlan Stern 	clear_bit(port1, hub->busy_bits);
2389d5cbad4bSAlan Stern 
2390b01b03f3SAlan Stern 	status = check_port_resume_type(udev,
2391b01b03f3SAlan Stern 			hub, port1, status, portchange, portstatus);
239254515fe5SAlan Stern 	if (status == 0)
239354515fe5SAlan Stern 		status = finish_port_resume(udev);
239454515fe5SAlan Stern 	if (status < 0) {
239554515fe5SAlan Stern 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
239654515fe5SAlan Stern 		hub_port_logical_disconnect(hub, port1);
239754515fe5SAlan Stern 	}
23981da177e4SLinus Torvalds 	return status;
23991da177e4SLinus Torvalds }
24001da177e4SLinus Torvalds 
24018808f00cSAlan Stern /* caller has locked udev */
24021da177e4SLinus Torvalds static int remote_wakeup(struct usb_device *udev)
24031da177e4SLinus Torvalds {
24041da177e4SLinus Torvalds 	int	status = 0;
24051da177e4SLinus Torvalds 
24061da177e4SLinus Torvalds 	if (udev->state == USB_STATE_SUSPENDED) {
2407645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
24081941044aSAlan Stern 		usb_mark_last_busy(udev);
240965bfd296SAlan Stern 		status = usb_external_resume_device(udev, PMSG_REMOTE_RESUME);
2410d25450c6SAlan Stern 	}
24111da177e4SLinus Torvalds 	return status;
24121da177e4SLinus Torvalds }
24131da177e4SLinus Torvalds 
2414d388dab7SAlan Stern #else	/* CONFIG_USB_SUSPEND */
2415d388dab7SAlan Stern 
2416d388dab7SAlan Stern /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
2417d388dab7SAlan Stern 
241865bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
2419d388dab7SAlan Stern {
2420d388dab7SAlan Stern 	return 0;
2421d388dab7SAlan Stern }
2422d388dab7SAlan Stern 
2423b01b03f3SAlan Stern /* However we may need to do a reset-resume */
2424b01b03f3SAlan Stern 
242565bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
2426d388dab7SAlan Stern {
2427b01b03f3SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2428b01b03f3SAlan Stern 	int		port1 = udev->portnum;
2429b01b03f3SAlan Stern 	int		status;
2430b01b03f3SAlan Stern 	u16		portchange, portstatus;
243154515fe5SAlan Stern 
2432b01b03f3SAlan Stern 	status = hub_port_status(hub, port1, &portstatus, &portchange);
2433b01b03f3SAlan Stern 	status = check_port_resume_type(udev,
2434b01b03f3SAlan Stern 			hub, port1, status, portchange, portstatus);
2435b01b03f3SAlan Stern 
2436b01b03f3SAlan Stern 	if (status) {
2437b01b03f3SAlan Stern 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2438b01b03f3SAlan Stern 		hub_port_logical_disconnect(hub, port1);
2439b01b03f3SAlan Stern 	} else if (udev->reset_resume) {
244054515fe5SAlan Stern 		dev_dbg(&udev->dev, "reset-resume\n");
2441742120c6SMing Lei 		status = usb_reset_and_verify_device(udev);
244254515fe5SAlan Stern 	}
244354515fe5SAlan Stern 	return status;
2444d388dab7SAlan Stern }
2445d388dab7SAlan Stern 
2446d388dab7SAlan Stern static inline int remote_wakeup(struct usb_device *udev)
2447d388dab7SAlan Stern {
2448d388dab7SAlan Stern 	return 0;
2449d388dab7SAlan Stern }
2450d388dab7SAlan Stern 
2451d388dab7SAlan Stern #endif
2452d388dab7SAlan Stern 
2453db690874SDavid Brownell static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
24541da177e4SLinus Torvalds {
24551da177e4SLinus Torvalds 	struct usb_hub		*hub = usb_get_intfdata (intf);
24561da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
24571da177e4SLinus Torvalds 	unsigned		port1;
24581da177e4SLinus Torvalds 
2459c9f89fa4SDavid Brownell 	/* fail if children aren't already suspended */
24601da177e4SLinus Torvalds 	for (port1 = 1; port1 <= hdev->maxchild; port1++) {
24611da177e4SLinus Torvalds 		struct usb_device	*udev;
24621da177e4SLinus Torvalds 
24631da177e4SLinus Torvalds 		udev = hdev->children [port1-1];
24646840d255SAlan Stern 		if (udev && udev->can_submit) {
246565bfd296SAlan Stern 			if (!(msg.event & PM_EVENT_AUTO))
2466645daaabSAlan Stern 				dev_dbg(&intf->dev, "port %d nyet suspended\n",
2467645daaabSAlan Stern 						port1);
2468c9f89fa4SDavid Brownell 			return -EBUSY;
2469c9f89fa4SDavid Brownell 		}
24701da177e4SLinus Torvalds 	}
24711da177e4SLinus Torvalds 
2472441b62c1SHarvey Harrison 	dev_dbg(&intf->dev, "%s\n", __func__);
247340f122f3SAlan Stern 
2474c9f89fa4SDavid Brownell 	/* stop khubd and related activity */
24754330354fSAlan Stern 	hub_quiesce(hub, HUB_SUSPEND);
2476b6f6436dSAlan Stern 	return 0;
24771da177e4SLinus Torvalds }
24781da177e4SLinus Torvalds 
24791da177e4SLinus Torvalds static int hub_resume(struct usb_interface *intf)
24801da177e4SLinus Torvalds {
24811da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata(intf);
24821da177e4SLinus Torvalds 
24835e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
2484f2835219SAlan Stern 	hub_activate(hub, HUB_RESUME);
24851da177e4SLinus Torvalds 	return 0;
24861da177e4SLinus Torvalds }
24871da177e4SLinus Torvalds 
2488b41a60ecSAlan Stern static int hub_reset_resume(struct usb_interface *intf)
2489f07600cfSAlan Stern {
2490b41a60ecSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
2491f07600cfSAlan Stern 
24925e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
2493f2835219SAlan Stern 	hub_activate(hub, HUB_RESET_RESUME);
2494f07600cfSAlan Stern 	return 0;
2495f07600cfSAlan Stern }
2496f07600cfSAlan Stern 
249754515fe5SAlan Stern /**
249854515fe5SAlan Stern  * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
249954515fe5SAlan Stern  * @rhdev: struct usb_device for the root hub
250054515fe5SAlan Stern  *
250154515fe5SAlan Stern  * The USB host controller driver calls this function when its root hub
250254515fe5SAlan Stern  * is resumed and Vbus power has been interrupted or the controller
2503feccc30dSAlan Stern  * has been reset.  The routine marks @rhdev as having lost power.
2504feccc30dSAlan Stern  * When the hub driver is resumed it will take notice and carry out
2505feccc30dSAlan Stern  * power-session recovery for all the "USB-PERSIST"-enabled child devices;
2506feccc30dSAlan Stern  * the others will be disconnected.
250754515fe5SAlan Stern  */
250854515fe5SAlan Stern void usb_root_hub_lost_power(struct usb_device *rhdev)
250954515fe5SAlan Stern {
251054515fe5SAlan Stern 	dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
251154515fe5SAlan Stern 	rhdev->reset_resume = 1;
251254515fe5SAlan Stern }
251354515fe5SAlan Stern EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
251454515fe5SAlan Stern 
2515d388dab7SAlan Stern #else	/* CONFIG_PM */
2516d388dab7SAlan Stern 
2517d388dab7SAlan Stern static inline int remote_wakeup(struct usb_device *udev)
2518d388dab7SAlan Stern {
2519d388dab7SAlan Stern 	return 0;
2520d388dab7SAlan Stern }
2521d388dab7SAlan Stern 
2522511366daSAndrew Morton #define hub_suspend		NULL
2523511366daSAndrew Morton #define hub_resume		NULL
2524f07600cfSAlan Stern #define hub_reset_resume	NULL
2525d388dab7SAlan Stern #endif
2526d388dab7SAlan Stern 
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
25291da177e4SLinus Torvalds  *
25301da177e4SLinus Torvalds  * Between connect detection and reset signaling there must be a delay
25311da177e4SLinus Torvalds  * of 100ms at least for debounce and power-settling.  The corresponding
25321da177e4SLinus Torvalds  * timer shall restart whenever the downstream port detects a disconnect.
25331da177e4SLinus Torvalds  *
25341da177e4SLinus Torvalds  * Apparently there are some bluetooth and irda-dongles and a number of
25351da177e4SLinus Torvalds  * low-speed devices for which this debounce period may last over a second.
25361da177e4SLinus Torvalds  * Not covered by the spec - but easy to deal with.
25371da177e4SLinus Torvalds  *
25381da177e4SLinus Torvalds  * This implementation uses a 1500ms total debounce timeout; if the
25391da177e4SLinus Torvalds  * connection isn't stable by then it returns -ETIMEDOUT.  It checks
25401da177e4SLinus Torvalds  * every 25ms for transient disconnects.  When the port status has been
25411da177e4SLinus Torvalds  * unchanged for 100ms it returns the port status.
25421da177e4SLinus Torvalds  */
25431da177e4SLinus Torvalds static int hub_port_debounce(struct usb_hub *hub, int port1)
25441da177e4SLinus Torvalds {
25451da177e4SLinus Torvalds 	int ret;
25461da177e4SLinus Torvalds 	int total_time, stable_time = 0;
25471da177e4SLinus Torvalds 	u16 portchange, portstatus;
25481da177e4SLinus Torvalds 	unsigned connection = 0xffff;
25491da177e4SLinus Torvalds 
25501da177e4SLinus Torvalds 	for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
25511da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
25521da177e4SLinus Torvalds 		if (ret < 0)
25531da177e4SLinus Torvalds 			return ret;
25541da177e4SLinus Torvalds 
25551da177e4SLinus Torvalds 		if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
25561da177e4SLinus Torvalds 		     (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
25571da177e4SLinus Torvalds 			stable_time += HUB_DEBOUNCE_STEP;
25581da177e4SLinus Torvalds 			if (stable_time >= HUB_DEBOUNCE_STABLE)
25591da177e4SLinus Torvalds 				break;
25601da177e4SLinus Torvalds 		} else {
25611da177e4SLinus Torvalds 			stable_time = 0;
25621da177e4SLinus Torvalds 			connection = portstatus & USB_PORT_STAT_CONNECTION;
25631da177e4SLinus Torvalds 		}
25641da177e4SLinus Torvalds 
25651da177e4SLinus Torvalds 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
25661da177e4SLinus Torvalds 			clear_port_feature(hub->hdev, port1,
25671da177e4SLinus Torvalds 					USB_PORT_FEAT_C_CONNECTION);
25681da177e4SLinus Torvalds 		}
25691da177e4SLinus Torvalds 
25701da177e4SLinus Torvalds 		if (total_time >= HUB_DEBOUNCE_TIMEOUT)
25711da177e4SLinus Torvalds 			break;
25721da177e4SLinus Torvalds 		msleep(HUB_DEBOUNCE_STEP);
25731da177e4SLinus Torvalds 	}
25741da177e4SLinus Torvalds 
25751da177e4SLinus Torvalds 	dev_dbg (hub->intfdev,
25761da177e4SLinus Torvalds 		"debounce: port %d: total %dms stable %dms status 0x%x\n",
25771da177e4SLinus Torvalds 		port1, total_time, stable_time, portstatus);
25781da177e4SLinus Torvalds 
25791da177e4SLinus Torvalds 	if (stable_time < HUB_DEBOUNCE_STABLE)
25801da177e4SLinus Torvalds 		return -ETIMEDOUT;
25811da177e4SLinus Torvalds 	return portstatus;
25821da177e4SLinus Torvalds }
25831da177e4SLinus Torvalds 
2584fc721f51SInaky Perez-Gonzalez void usb_ep0_reinit(struct usb_device *udev)
25851da177e4SLinus Torvalds {
2586ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
2587ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
25882caf7fcdSAlan Stern 	usb_enable_endpoint(udev, &udev->ep0, true);
25891da177e4SLinus Torvalds }
2590fc721f51SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_ep0_reinit);
25911da177e4SLinus Torvalds 
25921da177e4SLinus Torvalds #define usb_sndaddr0pipe()	(PIPE_CONTROL << 30)
25931da177e4SLinus Torvalds #define usb_rcvaddr0pipe()	((PIPE_CONTROL << 30) | USB_DIR_IN)
25941da177e4SLinus Torvalds 
25954326ed0bSAlan Stern static int hub_set_address(struct usb_device *udev, int devnum)
25961da177e4SLinus Torvalds {
25971da177e4SLinus Torvalds 	int retval;
2598c6515272SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
25991da177e4SLinus Torvalds 
2600c6515272SSarah Sharp 	/*
2601c6515272SSarah Sharp 	 * The host controller will choose the device address,
2602c6515272SSarah Sharp 	 * instead of the core having chosen it earlier
2603c6515272SSarah Sharp 	 */
2604c6515272SSarah Sharp 	if (!hcd->driver->address_device && devnum <= 1)
26051da177e4SLinus Torvalds 		return -EINVAL;
26061da177e4SLinus Torvalds 	if (udev->state == USB_STATE_ADDRESS)
26071da177e4SLinus Torvalds 		return 0;
26081da177e4SLinus Torvalds 	if (udev->state != USB_STATE_DEFAULT)
26091da177e4SLinus Torvalds 		return -EINVAL;
2610c6515272SSarah Sharp 	if (hcd->driver->address_device) {
2611c6515272SSarah Sharp 		retval = hcd->driver->address_device(hcd, udev);
2612c6515272SSarah Sharp 	} else {
26131da177e4SLinus Torvalds 		retval = usb_control_msg(udev, usb_sndaddr0pipe(),
26144326ed0bSAlan Stern 				USB_REQ_SET_ADDRESS, 0, devnum, 0,
26151da177e4SLinus Torvalds 				NULL, 0, USB_CTRL_SET_TIMEOUT);
2616c6515272SSarah Sharp 		if (retval == 0)
2617c6515272SSarah Sharp 			update_address(udev, devnum);
2618c6515272SSarah Sharp 	}
26191da177e4SLinus Torvalds 	if (retval == 0) {
26204953d141SDavid Vrabel 		/* Device now using proper address. */
26211da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_ADDRESS);
2622fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
26231da177e4SLinus Torvalds 	}
26241da177e4SLinus Torvalds 	return retval;
26251da177e4SLinus Torvalds }
26261da177e4SLinus Torvalds 
26271da177e4SLinus Torvalds /* Reset device, (re)assign address, get device descriptor.
26281da177e4SLinus Torvalds  * Device connection must be stable, no more debouncing needed.
26291da177e4SLinus Torvalds  * Returns device in USB_STATE_ADDRESS, except on error.
26301da177e4SLinus Torvalds  *
26311da177e4SLinus Torvalds  * If this is called for an already-existing device (as part of
2632742120c6SMing Lei  * usb_reset_and_verify_device), the caller must own the device lock.  For a
26331da177e4SLinus Torvalds  * newly detected device that is not accessible through any global
26341da177e4SLinus Torvalds  * pointers, it's not necessary to lock the device.
26351da177e4SLinus Torvalds  */
26361da177e4SLinus Torvalds static int
26371da177e4SLinus Torvalds hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
26381da177e4SLinus Torvalds 		int retry_counter)
26391da177e4SLinus Torvalds {
26404186ecf8SArjan van de Ven 	static DEFINE_MUTEX(usb_address0_mutex);
26411da177e4SLinus Torvalds 
26421da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
2643e7b77172SSarah Sharp 	struct usb_hcd		*hcd = bus_to_hcd(hdev->bus);
26441da177e4SLinus Torvalds 	int			i, j, retval;
26451da177e4SLinus Torvalds 	unsigned		delay = HUB_SHORT_RESET_TIME;
26461da177e4SLinus Torvalds 	enum usb_device_speed	oldspeed = udev->speed;
264783a07196SInaky Perez-Gonzalez 	char 			*speed, *type;
26484326ed0bSAlan Stern 	int			devnum = udev->devnum;
26491da177e4SLinus Torvalds 
26501da177e4SLinus Torvalds 	/* root hub ports have a slightly longer reset period
26511da177e4SLinus Torvalds 	 * (from USB 2.0 spec, section 7.1.7.5)
26521da177e4SLinus Torvalds 	 */
26531da177e4SLinus Torvalds 	if (!hdev->parent) {
26541da177e4SLinus Torvalds 		delay = HUB_ROOT_RESET_TIME;
26551da177e4SLinus Torvalds 		if (port1 == hdev->bus->otg_port)
26561da177e4SLinus Torvalds 			hdev->bus->b_hnp_enable = 0;
26571da177e4SLinus Torvalds 	}
26581da177e4SLinus Torvalds 
26591da177e4SLinus Torvalds 	/* Some low speed devices have problems with the quick delay, so */
26601da177e4SLinus Torvalds 	/*  be a bit pessimistic with those devices. RHbug #23670 */
26611da177e4SLinus Torvalds 	if (oldspeed == USB_SPEED_LOW)
26621da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
26631da177e4SLinus Torvalds 
26644186ecf8SArjan van de Ven 	mutex_lock(&usb_address0_mutex);
26651da177e4SLinus Torvalds 
2666a5f0efabSSarah Sharp 	if (!udev->config && oldspeed == USB_SPEED_SUPER) {
2667e7b77172SSarah Sharp 		/* Don't reset USB 3.0 devices during an initial setup */
2668e7b77172SSarah Sharp 		usb_set_device_state(udev, USB_STATE_DEFAULT);
2669e7b77172SSarah Sharp 	} else {
26701da177e4SLinus Torvalds 		/* Reset the device; full speed may morph to high speed */
2671e7b77172SSarah Sharp 		/* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
26721da177e4SLinus Torvalds 		retval = hub_port_reset(hub, port1, udev, delay);
26731da177e4SLinus Torvalds 		if (retval < 0)		/* error or disconnect */
26741da177e4SLinus Torvalds 			goto fail;
26751da177e4SLinus Torvalds 		/* success, speed is known */
2676e7b77172SSarah Sharp 	}
26771da177e4SLinus Torvalds 	retval = -ENODEV;
26781da177e4SLinus Torvalds 
26791da177e4SLinus Torvalds 	if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
26801da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset changed speed!\n");
26811da177e4SLinus Torvalds 		goto fail;
26821da177e4SLinus Torvalds 	}
26831da177e4SLinus Torvalds 	oldspeed = udev->speed;
26841da177e4SLinus Torvalds 
26851da177e4SLinus Torvalds 	/* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
26861da177e4SLinus Torvalds 	 * it's fixed size except for full speed devices.
26875bb6e0aeSInaky Perez-Gonzalez 	 * For Wireless USB devices, ep0 max packet is always 512 (tho
26885bb6e0aeSInaky Perez-Gonzalez 	 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
26891da177e4SLinus Torvalds 	 */
26901da177e4SLinus Torvalds 	switch (udev->speed) {
26916b403b02SSarah Sharp 	case USB_SPEED_SUPER:
26925bb6e0aeSInaky Perez-Gonzalez 	case USB_SPEED_VARIABLE:	/* fixed at 512 */
2693551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
26945bb6e0aeSInaky Perez-Gonzalez 		break;
26951da177e4SLinus Torvalds 	case USB_SPEED_HIGH:		/* fixed at 64 */
2696551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
26971da177e4SLinus Torvalds 		break;
26981da177e4SLinus Torvalds 	case USB_SPEED_FULL:		/* 8, 16, 32, or 64 */
26991da177e4SLinus Torvalds 		/* to determine the ep0 maxpacket size, try to read
27001da177e4SLinus Torvalds 		 * the device descriptor to get bMaxPacketSize0 and
27011da177e4SLinus Torvalds 		 * then correct our initial guess.
27021da177e4SLinus Torvalds 		 */
2703551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
27041da177e4SLinus Torvalds 		break;
27051da177e4SLinus Torvalds 	case USB_SPEED_LOW:		/* fixed at 8 */
2706551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
27071da177e4SLinus Torvalds 		break;
27081da177e4SLinus Torvalds 	default:
27091da177e4SLinus Torvalds 		goto fail;
27101da177e4SLinus Torvalds 	}
27111da177e4SLinus Torvalds 
271283a07196SInaky Perez-Gonzalez 	type = "";
271383a07196SInaky Perez-Gonzalez 	switch (udev->speed) {
27141da177e4SLinus Torvalds 	case USB_SPEED_LOW:	speed = "low";	break;
27151da177e4SLinus Torvalds 	case USB_SPEED_FULL:	speed = "full";	break;
27161da177e4SLinus Torvalds 	case USB_SPEED_HIGH:	speed = "high";	break;
27176b403b02SSarah Sharp 	case USB_SPEED_SUPER:
27186b403b02SSarah Sharp 				speed = "super";
27196b403b02SSarah Sharp 				break;
272083a07196SInaky Perez-Gonzalez 	case USB_SPEED_VARIABLE:
272183a07196SInaky Perez-Gonzalez 				speed = "variable";
272283a07196SInaky Perez-Gonzalez 				type = "Wireless ";
272383a07196SInaky Perez-Gonzalez 				break;
27241da177e4SLinus Torvalds 	default: 		speed = "?";	break;
272583a07196SInaky Perez-Gonzalez 	}
2726c6515272SSarah Sharp 	if (udev->speed != USB_SPEED_SUPER)
272783a07196SInaky Perez-Gonzalez 		dev_info(&udev->dev,
272883a07196SInaky Perez-Gonzalez 				"%s %s speed %sUSB device using %s and address %d\n",
272983a07196SInaky Perez-Gonzalez 				(udev->config) ? "reset" : "new", speed, type,
27304326ed0bSAlan Stern 				udev->bus->controller->driver->name, devnum);
27311da177e4SLinus Torvalds 
27321da177e4SLinus Torvalds 	/* Set up TT records, if needed  */
27331da177e4SLinus Torvalds 	if (hdev->tt) {
27341da177e4SLinus Torvalds 		udev->tt = hdev->tt;
27351da177e4SLinus Torvalds 		udev->ttport = hdev->ttport;
27361da177e4SLinus Torvalds 	} else if (udev->speed != USB_SPEED_HIGH
27371da177e4SLinus Torvalds 			&& hdev->speed == USB_SPEED_HIGH) {
27381da177e4SLinus Torvalds 		udev->tt = &hub->tt;
27391da177e4SLinus Torvalds 		udev->ttport = port1;
27401da177e4SLinus Torvalds 	}
27411da177e4SLinus Torvalds 
27421da177e4SLinus Torvalds 	/* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
27431da177e4SLinus Torvalds 	 * Because device hardware and firmware is sometimes buggy in
27441da177e4SLinus Torvalds 	 * this area, and this is how Linux has done it for ages.
27451da177e4SLinus Torvalds 	 * Change it cautiously.
27461da177e4SLinus Torvalds 	 *
27471da177e4SLinus Torvalds 	 * NOTE:  If USE_NEW_SCHEME() is true we will start by issuing
27481da177e4SLinus Torvalds 	 * a 64-byte GET_DESCRIPTOR request.  This is what Windows does,
27491da177e4SLinus Torvalds 	 * so it may help with some non-standards-compliant devices.
27501da177e4SLinus Torvalds 	 * Otherwise we start with SET_ADDRESS and then try to read the
27511da177e4SLinus Torvalds 	 * first 8 bytes of the device descriptor to get the ep0 maxpacket
27521da177e4SLinus Torvalds 	 * value.
27531da177e4SLinus Torvalds 	 */
27541da177e4SLinus Torvalds 	for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
2755c6515272SSarah Sharp 		/*
2756c6515272SSarah Sharp 		 * An xHCI controller cannot send any packets to a device until
2757c6515272SSarah Sharp 		 * a set address command successfully completes.
2758c6515272SSarah Sharp 		 */
2759c6515272SSarah Sharp 		if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
27601da177e4SLinus Torvalds 			struct usb_device_descriptor *buf;
27611da177e4SLinus Torvalds 			int r = 0;
27621da177e4SLinus Torvalds 
27631da177e4SLinus Torvalds #define GET_DESCRIPTOR_BUFSIZE	64
27641da177e4SLinus Torvalds 			buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
27651da177e4SLinus Torvalds 			if (!buf) {
27661da177e4SLinus Torvalds 				retval = -ENOMEM;
27671da177e4SLinus Torvalds 				continue;
27681da177e4SLinus Torvalds 			}
27691da177e4SLinus Torvalds 
2770b89ee19aSAlan Stern 			/* Retry on all errors; some devices are flakey.
2771b89ee19aSAlan Stern 			 * 255 is for WUSB devices, we actually need to use
2772b89ee19aSAlan Stern 			 * 512 (WUSB1.0[4.8.1]).
27731da177e4SLinus Torvalds 			 */
27741da177e4SLinus Torvalds 			for (j = 0; j < 3; ++j) {
27751da177e4SLinus Torvalds 				buf->bMaxPacketSize0 = 0;
27761da177e4SLinus Torvalds 				r = usb_control_msg(udev, usb_rcvaddr0pipe(),
27771da177e4SLinus Torvalds 					USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
27781da177e4SLinus Torvalds 					USB_DT_DEVICE << 8, 0,
27791da177e4SLinus Torvalds 					buf, GET_DESCRIPTOR_BUFSIZE,
2780fd7c519dSJaroslav Kysela 					initial_descriptor_timeout);
27811da177e4SLinus Torvalds 				switch (buf->bMaxPacketSize0) {
27825bb6e0aeSInaky Perez-Gonzalez 				case 8: case 16: case 32: case 64: case 255:
27831da177e4SLinus Torvalds 					if (buf->bDescriptorType ==
27841da177e4SLinus Torvalds 							USB_DT_DEVICE) {
27851da177e4SLinus Torvalds 						r = 0;
27861da177e4SLinus Torvalds 						break;
27871da177e4SLinus Torvalds 					}
27881da177e4SLinus Torvalds 					/* FALL THROUGH */
27891da177e4SLinus Torvalds 				default:
27901da177e4SLinus Torvalds 					if (r == 0)
27911da177e4SLinus Torvalds 						r = -EPROTO;
27921da177e4SLinus Torvalds 					break;
27931da177e4SLinus Torvalds 				}
27941da177e4SLinus Torvalds 				if (r == 0)
27951da177e4SLinus Torvalds 					break;
27961da177e4SLinus Torvalds 			}
27971da177e4SLinus Torvalds 			udev->descriptor.bMaxPacketSize0 =
27981da177e4SLinus Torvalds 					buf->bMaxPacketSize0;
27991da177e4SLinus Torvalds 			kfree(buf);
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds 			retval = hub_port_reset(hub, port1, udev, delay);
28021da177e4SLinus Torvalds 			if (retval < 0)		/* error or disconnect */
28031da177e4SLinus Torvalds 				goto fail;
28041da177e4SLinus Torvalds 			if (oldspeed != udev->speed) {
28051da177e4SLinus Torvalds 				dev_dbg(&udev->dev,
28061da177e4SLinus Torvalds 					"device reset changed speed!\n");
28071da177e4SLinus Torvalds 				retval = -ENODEV;
28081da177e4SLinus Torvalds 				goto fail;
28091da177e4SLinus Torvalds 			}
28101da177e4SLinus Torvalds 			if (r) {
2811b9cef6c3SWu Fengguang 				dev_err(&udev->dev,
2812b9cef6c3SWu Fengguang 					"device descriptor read/64, error %d\n",
2813b9cef6c3SWu Fengguang 					r);
28141da177e4SLinus Torvalds 				retval = -EMSGSIZE;
28151da177e4SLinus Torvalds 				continue;
28161da177e4SLinus Torvalds 			}
28171da177e4SLinus Torvalds #undef GET_DESCRIPTOR_BUFSIZE
28181da177e4SLinus Torvalds 		}
28191da177e4SLinus Torvalds 
28206c529cdcSInaky Perez-Gonzalez  		/*
28216c529cdcSInaky Perez-Gonzalez  		 * If device is WUSB, we already assigned an
28226c529cdcSInaky Perez-Gonzalez  		 * unauthorized address in the Connect Ack sequence;
28236c529cdcSInaky Perez-Gonzalez  		 * authorization will assign the final address.
28246c529cdcSInaky Perez-Gonzalez  		 */
28256c529cdcSInaky Perez-Gonzalez 		if (udev->wusb == 0) {
28261da177e4SLinus Torvalds 			for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
28274326ed0bSAlan Stern 				retval = hub_set_address(udev, devnum);
28281da177e4SLinus Torvalds 				if (retval >= 0)
28291da177e4SLinus Torvalds 					break;
28301da177e4SLinus Torvalds 				msleep(200);
28311da177e4SLinus Torvalds 			}
28321da177e4SLinus Torvalds 			if (retval < 0) {
28331da177e4SLinus Torvalds 				dev_err(&udev->dev,
28341da177e4SLinus Torvalds 					"device not accepting address %d, error %d\n",
28354326ed0bSAlan Stern 					devnum, retval);
28361da177e4SLinus Torvalds 				goto fail;
28371da177e4SLinus Torvalds 			}
2838c6515272SSarah Sharp 			if (udev->speed == USB_SPEED_SUPER) {
2839c6515272SSarah Sharp 				devnum = udev->devnum;
2840c6515272SSarah Sharp 				dev_info(&udev->dev,
2841c6515272SSarah Sharp 						"%s SuperSpeed USB device using %s and address %d\n",
2842c6515272SSarah Sharp 						(udev->config) ? "reset" : "new",
2843c6515272SSarah Sharp 						udev->bus->controller->driver->name, devnum);
2844c6515272SSarah Sharp 			}
28451da177e4SLinus Torvalds 
28461da177e4SLinus Torvalds 			/* cope with hardware quirkiness:
28471da177e4SLinus Torvalds 			 *  - let SET_ADDRESS settle, some device hardware wants it
28481da177e4SLinus Torvalds 			 *  - read ep0 maxpacket even for high and low speed,
28491da177e4SLinus Torvalds 			 */
28501da177e4SLinus Torvalds 			msleep(10);
2851c6515272SSarah Sharp 			if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
28521da177e4SLinus Torvalds 				break;
28536c529cdcSInaky Perez-Gonzalez   		}
28541da177e4SLinus Torvalds 
28551da177e4SLinus Torvalds 		retval = usb_get_device_descriptor(udev, 8);
28561da177e4SLinus Torvalds 		if (retval < 8) {
2857b9cef6c3SWu Fengguang 			dev_err(&udev->dev,
2858b9cef6c3SWu Fengguang 					"device descriptor read/8, error %d\n",
2859b9cef6c3SWu Fengguang 					retval);
28601da177e4SLinus Torvalds 			if (retval >= 0)
28611da177e4SLinus Torvalds 				retval = -EMSGSIZE;
28621da177e4SLinus Torvalds 		} else {
28631da177e4SLinus Torvalds 			retval = 0;
28641da177e4SLinus Torvalds 			break;
28651da177e4SLinus Torvalds 		}
28661da177e4SLinus Torvalds 	}
28671da177e4SLinus Torvalds 	if (retval)
28681da177e4SLinus Torvalds 		goto fail;
28691da177e4SLinus Torvalds 
28706b403b02SSarah Sharp 	if (udev->descriptor.bMaxPacketSize0 == 0xff ||
28716b403b02SSarah Sharp 			udev->speed == USB_SPEED_SUPER)
28726b403b02SSarah Sharp 		i = 512;
28736b403b02SSarah Sharp 	else
28746b403b02SSarah Sharp 		i = udev->descriptor.bMaxPacketSize0;
28751da177e4SLinus Torvalds 	if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) {
28761da177e4SLinus Torvalds 		if (udev->speed != USB_SPEED_FULL ||
28771da177e4SLinus Torvalds 				!(i == 8 || i == 16 || i == 32 || i == 64)) {
28781da177e4SLinus Torvalds 			dev_err(&udev->dev, "ep0 maxpacket = %d\n", i);
28791da177e4SLinus Torvalds 			retval = -EMSGSIZE;
28801da177e4SLinus Torvalds 			goto fail;
28811da177e4SLinus Torvalds 		}
28821da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
28831da177e4SLinus Torvalds 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
2884fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
28851da177e4SLinus Torvalds 	}
28861da177e4SLinus Torvalds 
28871da177e4SLinus Torvalds 	retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
28881da177e4SLinus Torvalds 	if (retval < (signed)sizeof(udev->descriptor)) {
2889b9cef6c3SWu Fengguang 		dev_err(&udev->dev, "device descriptor read/all, error %d\n",
2890b9cef6c3SWu Fengguang 			retval);
28911da177e4SLinus Torvalds 		if (retval >= 0)
28921da177e4SLinus Torvalds 			retval = -ENOMSG;
28931da177e4SLinus Torvalds 		goto fail;
28941da177e4SLinus Torvalds 	}
28951da177e4SLinus Torvalds 
28961da177e4SLinus Torvalds 	retval = 0;
28971da177e4SLinus Torvalds 
28981da177e4SLinus Torvalds fail:
28994326ed0bSAlan Stern 	if (retval) {
29001da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 0);
29014953d141SDavid Vrabel 		update_address(udev, devnum);	/* for disconnect processing */
29024326ed0bSAlan Stern 	}
29034186ecf8SArjan van de Ven 	mutex_unlock(&usb_address0_mutex);
29041da177e4SLinus Torvalds 	return retval;
29051da177e4SLinus Torvalds }
29061da177e4SLinus Torvalds 
29071da177e4SLinus Torvalds static void
29081da177e4SLinus Torvalds check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
29091da177e4SLinus Torvalds {
29101da177e4SLinus Torvalds 	struct usb_qualifier_descriptor	*qual;
29111da177e4SLinus Torvalds 	int				status;
29121da177e4SLinus Torvalds 
2913e94b1766SChristoph Lameter 	qual = kmalloc (sizeof *qual, GFP_KERNEL);
29141da177e4SLinus Torvalds 	if (qual == NULL)
29151da177e4SLinus Torvalds 		return;
29161da177e4SLinus Torvalds 
29171da177e4SLinus Torvalds 	status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
29181da177e4SLinus Torvalds 			qual, sizeof *qual);
29191da177e4SLinus Torvalds 	if (status == sizeof *qual) {
29201da177e4SLinus Torvalds 		dev_info(&udev->dev, "not running at top speed; "
29211da177e4SLinus Torvalds 			"connect to a high speed hub\n");
29221da177e4SLinus Torvalds 		/* hub LEDs are probably harder to miss than syslog */
29231da177e4SLinus Torvalds 		if (hub->has_indicators) {
29241da177e4SLinus Torvalds 			hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
2925c4028958SDavid Howells 			schedule_delayed_work (&hub->leds, 0);
29261da177e4SLinus Torvalds 		}
29271da177e4SLinus Torvalds 	}
29281da177e4SLinus Torvalds 	kfree(qual);
29291da177e4SLinus Torvalds }
29301da177e4SLinus Torvalds 
29311da177e4SLinus Torvalds static unsigned
29321da177e4SLinus Torvalds hub_power_remaining (struct usb_hub *hub)
29331da177e4SLinus Torvalds {
29341da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
29351da177e4SLinus Torvalds 	int remaining;
293655c52718SAlan Stern 	int port1;
29371da177e4SLinus Torvalds 
293855c52718SAlan Stern 	if (!hub->limited_power)
29391da177e4SLinus Torvalds 		return 0;
29401da177e4SLinus Torvalds 
294155c52718SAlan Stern 	remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
294255c52718SAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
294355c52718SAlan Stern 		struct usb_device	*udev = hdev->children[port1 - 1];
294455c52718SAlan Stern 		int			delta;
29451da177e4SLinus Torvalds 
29461da177e4SLinus Torvalds 		if (!udev)
29471da177e4SLinus Torvalds 			continue;
29481da177e4SLinus Torvalds 
294955c52718SAlan Stern 		/* Unconfigured devices may not use more than 100mA,
295055c52718SAlan Stern 		 * or 8mA for OTG ports */
29511da177e4SLinus Torvalds 		if (udev->actconfig)
295255c52718SAlan Stern 			delta = udev->actconfig->desc.bMaxPower * 2;
295355c52718SAlan Stern 		else if (port1 != udev->bus->otg_port || hdev->parent)
295455c52718SAlan Stern 			delta = 100;
29551da177e4SLinus Torvalds 		else
295655c52718SAlan Stern 			delta = 8;
295755c52718SAlan Stern 		if (delta > hub->mA_per_port)
2958b9cef6c3SWu Fengguang 			dev_warn(&udev->dev,
2959b9cef6c3SWu Fengguang 				 "%dmA is over %umA budget for port %d!\n",
296055c52718SAlan Stern 				 delta, hub->mA_per_port, port1);
29611da177e4SLinus Torvalds 		remaining -= delta;
29621da177e4SLinus Torvalds 	}
29631da177e4SLinus Torvalds 	if (remaining < 0) {
296455c52718SAlan Stern 		dev_warn(hub->intfdev, "%dmA over power budget!\n",
296555c52718SAlan Stern 			- remaining);
29661da177e4SLinus Torvalds 		remaining = 0;
29671da177e4SLinus Torvalds 	}
29681da177e4SLinus Torvalds 	return remaining;
29691da177e4SLinus Torvalds }
29701da177e4SLinus Torvalds 
29711da177e4SLinus Torvalds /* Handle physical or logical connection change events.
29721da177e4SLinus Torvalds  * This routine is called when:
29731da177e4SLinus Torvalds  * 	a port connection-change occurs;
29741da177e4SLinus Torvalds  *	a port enable-change occurs (often caused by EMI);
2975742120c6SMing Lei  *	usb_reset_and_verify_device() encounters changed descriptors (as from
29761da177e4SLinus Torvalds  *		a firmware download)
29771da177e4SLinus Torvalds  * caller already locked the hub
29781da177e4SLinus Torvalds  */
29791da177e4SLinus Torvalds static void hub_port_connect_change(struct usb_hub *hub, int port1,
29801da177e4SLinus Torvalds 					u16 portstatus, u16 portchange)
29811da177e4SLinus Torvalds {
29821da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
29831da177e4SLinus Torvalds 	struct device *hub_dev = hub->intfdev;
298490da096eSBalaji Rao 	struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
298524618b0cSAlan Stern 	unsigned wHubCharacteristics =
298624618b0cSAlan Stern 			le16_to_cpu(hub->descriptor->wHubCharacteristics);
29878808f00cSAlan Stern 	struct usb_device *udev;
29881da177e4SLinus Torvalds 	int status, i;
29891da177e4SLinus Torvalds 
29901da177e4SLinus Torvalds 	dev_dbg (hub_dev,
29911da177e4SLinus Torvalds 		"port %d, status %04x, change %04x, %s\n",
29921da177e4SLinus Torvalds 		port1, portstatus, portchange, portspeed (portstatus));
29931da177e4SLinus Torvalds 
29941da177e4SLinus Torvalds 	if (hub->has_indicators) {
29951da177e4SLinus Torvalds 		set_port_led(hub, port1, HUB_LED_AUTO);
29961da177e4SLinus Torvalds 		hub->indicator[port1-1] = INDICATOR_AUTO;
29971da177e4SLinus Torvalds 	}
29981da177e4SLinus Torvalds 
29991da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
30001da177e4SLinus Torvalds 	/* during HNP, don't repeat the debounce */
30011da177e4SLinus Torvalds 	if (hdev->bus->is_b_host)
300224618b0cSAlan Stern 		portchange &= ~(USB_PORT_STAT_C_CONNECTION |
300324618b0cSAlan Stern 				USB_PORT_STAT_C_ENABLE);
30041da177e4SLinus Torvalds #endif
30051da177e4SLinus Torvalds 
30068808f00cSAlan Stern 	/* Try to resuscitate an existing device */
30078808f00cSAlan Stern 	udev = hdev->children[port1-1];
30088808f00cSAlan Stern 	if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
30098808f00cSAlan Stern 			udev->state != USB_STATE_NOTATTACHED) {
30108808f00cSAlan Stern 		usb_lock_device(udev);
30118808f00cSAlan Stern 		if (portstatus & USB_PORT_STAT_ENABLE) {
30128808f00cSAlan Stern 			status = 0;		/* Nothing to do */
30138808f00cSAlan Stern 
30148808f00cSAlan Stern #ifdef CONFIG_USB_SUSPEND
30155257d97aSAlan Stern 		} else if (udev->state == USB_STATE_SUSPENDED &&
30165257d97aSAlan Stern 				udev->persist_enabled) {
30178808f00cSAlan Stern 			/* For a suspended device, treat this as a
30188808f00cSAlan Stern 			 * remote wakeup event.
30198808f00cSAlan Stern 			 */
30208808f00cSAlan Stern 			status = remote_wakeup(udev);
30218808f00cSAlan Stern #endif
30228808f00cSAlan Stern 
30238808f00cSAlan Stern 		} else {
30245257d97aSAlan Stern 			status = -ENODEV;	/* Don't resuscitate */
30258808f00cSAlan Stern 		}
30268808f00cSAlan Stern 		usb_unlock_device(udev);
30278808f00cSAlan Stern 
30288808f00cSAlan Stern 		if (status == 0) {
30298808f00cSAlan Stern 			clear_bit(port1, hub->change_bits);
30308808f00cSAlan Stern 			return;
30318808f00cSAlan Stern 		}
30328808f00cSAlan Stern 	}
30338808f00cSAlan Stern 
303424618b0cSAlan Stern 	/* Disconnect any existing devices under this port */
30358808f00cSAlan Stern 	if (udev)
303624618b0cSAlan Stern 		usb_disconnect(&hdev->children[port1-1]);
303724618b0cSAlan Stern 	clear_bit(port1, hub->change_bits);
303824618b0cSAlan Stern 
3039253e0572SAlan Stern 	/* We can forget about a "removed" device when there's a physical
3040253e0572SAlan Stern 	 * disconnect or the connect status changes.
3041253e0572SAlan Stern 	 */
3042253e0572SAlan Stern 	if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3043253e0572SAlan Stern 			(portchange & USB_PORT_STAT_C_CONNECTION))
3044253e0572SAlan Stern 		clear_bit(port1, hub->removed_bits);
3045253e0572SAlan Stern 
30465257d97aSAlan Stern 	if (portchange & (USB_PORT_STAT_C_CONNECTION |
30475257d97aSAlan Stern 				USB_PORT_STAT_C_ENABLE)) {
30485257d97aSAlan Stern 		status = hub_port_debounce(hub, port1);
30495257d97aSAlan Stern 		if (status < 0) {
30505257d97aSAlan Stern 			if (printk_ratelimit())
30515257d97aSAlan Stern 				dev_err(hub_dev, "connect-debounce failed, "
30525257d97aSAlan Stern 						"port %d disabled\n", port1);
30535257d97aSAlan Stern 			portstatus &= ~USB_PORT_STAT_CONNECTION;
30545257d97aSAlan Stern 		} else {
30555257d97aSAlan Stern 			portstatus = status;
30565257d97aSAlan Stern 		}
30575257d97aSAlan Stern 	}
30585257d97aSAlan Stern 
3059253e0572SAlan Stern 	/* Return now if debouncing failed or nothing is connected or
3060253e0572SAlan Stern 	 * the device was "removed".
3061253e0572SAlan Stern 	 */
3062253e0572SAlan Stern 	if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3063253e0572SAlan Stern 			test_bit(port1, hub->removed_bits)) {
30641da177e4SLinus Torvalds 
30651da177e4SLinus Torvalds 		/* maybe switch power back on (e.g. root hub was reset) */
306674ad9bd2SGreg Kroah-Hartman 		if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
30671da177e4SLinus Torvalds 				&& !(portstatus & (1 << USB_PORT_FEAT_POWER)))
30681da177e4SLinus Torvalds 			set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
30691da177e4SLinus Torvalds 
30701da177e4SLinus Torvalds 		if (portstatus & USB_PORT_STAT_ENABLE)
30711da177e4SLinus Torvalds   			goto done;
30721da177e4SLinus Torvalds 		return;
30731da177e4SLinus Torvalds 	}
30741da177e4SLinus Torvalds 
30751da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; i++) {
30761da177e4SLinus Torvalds 
30771da177e4SLinus Torvalds 		/* reallocate for each attempt, since references
30781da177e4SLinus Torvalds 		 * to the previous one can escape in various ways
30791da177e4SLinus Torvalds 		 */
30801da177e4SLinus Torvalds 		udev = usb_alloc_dev(hdev, hdev->bus, port1);
30811da177e4SLinus Torvalds 		if (!udev) {
30821da177e4SLinus Torvalds 			dev_err (hub_dev,
30831da177e4SLinus Torvalds 				"couldn't allocate port %d usb_device\n",
30841da177e4SLinus Torvalds 				port1);
30851da177e4SLinus Torvalds 			goto done;
30861da177e4SLinus Torvalds 		}
30871da177e4SLinus Torvalds 
30881da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_POWERED);
308955c52718SAlan Stern  		udev->bus_mA = hub->mA_per_port;
3090b6956ffaSAlan Stern 		udev->level = hdev->level + 1;
30918af548dcSInaky Perez-Gonzalez 		udev->wusb = hub_is_wusb(hub);
30921da177e4SLinus Torvalds 
3093e7b77172SSarah Sharp 		/*
3094e7b77172SSarah Sharp 		 * USB 3.0 devices are reset automatically before the connect
3095e7b77172SSarah Sharp 		 * port status change appears, and the root hub port status
3096e7b77172SSarah Sharp 		 * shows the correct speed.  We also get port change
3097e7b77172SSarah Sharp 		 * notifications for USB 3.0 devices from the USB 3.0 portion of
3098e7b77172SSarah Sharp 		 * an external USB 3.0 hub, but this isn't handled correctly yet
3099e7b77172SSarah Sharp 		 * FIXME.
3100e7b77172SSarah Sharp 		 */
3101e7b77172SSarah Sharp 
3102e7b77172SSarah Sharp 		if (!(hcd->driver->flags & HCD_USB3))
3103e7b77172SSarah Sharp 			udev->speed = USB_SPEED_UNKNOWN;
3104e7b77172SSarah Sharp 		else if ((hdev->parent == NULL) &&
3105e7b77172SSarah Sharp 				(portstatus & (1 << USB_PORT_FEAT_SUPERSPEED)))
3106e7b77172SSarah Sharp 			udev->speed = USB_SPEED_SUPER;
3107e7b77172SSarah Sharp 		else
3108e7b77172SSarah Sharp 			udev->speed = USB_SPEED_UNKNOWN;
3109e7b77172SSarah Sharp 
3110c6515272SSarah Sharp 		/*
3111c6515272SSarah Sharp 		 * xHCI needs to issue an address device command later
3112c6515272SSarah Sharp 		 * in the hub_port_init sequence for SS/HS/FS/LS devices.
3113c6515272SSarah Sharp 		 */
3114c6515272SSarah Sharp 		if (!(hcd->driver->flags & HCD_USB3)) {
3115c6515272SSarah Sharp 			/* set the address */
3116c6515272SSarah Sharp 			choose_address(udev);
3117c6515272SSarah Sharp 			if (udev->devnum <= 0) {
3118c6515272SSarah Sharp 				status = -ENOTCONN;	/* Don't retry */
3119c6515272SSarah Sharp 				goto loop;
3120c6515272SSarah Sharp 			}
3121c6515272SSarah Sharp 		}
3122c6515272SSarah Sharp 
3123e7b77172SSarah Sharp 		/* reset (non-USB 3.0 devices) and get descriptor */
31241da177e4SLinus Torvalds 		status = hub_port_init(hub, udev, port1, i);
31251da177e4SLinus Torvalds 		if (status < 0)
31261da177e4SLinus Torvalds 			goto loop;
31271da177e4SLinus Torvalds 
31281da177e4SLinus Torvalds 		/* consecutive bus-powered hubs aren't reliable; they can
31291da177e4SLinus Torvalds 		 * violate the voltage drop budget.  if the new child has
31301da177e4SLinus Torvalds 		 * a "powered" LED, users should notice we didn't enable it
31311da177e4SLinus Torvalds 		 * (without reading syslog), even without per-port LEDs
31321da177e4SLinus Torvalds 		 * on the parent.
31331da177e4SLinus Torvalds 		 */
31341da177e4SLinus Torvalds 		if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
313555c52718SAlan Stern 				&& udev->bus_mA <= 100) {
31361da177e4SLinus Torvalds 			u16	devstat;
31371da177e4SLinus Torvalds 
31381da177e4SLinus Torvalds 			status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
31391da177e4SLinus Torvalds 					&devstat);
314055c52718SAlan Stern 			if (status < 2) {
31411da177e4SLinus Torvalds 				dev_dbg(&udev->dev, "get status %d ?\n", status);
31421da177e4SLinus Torvalds 				goto loop_disable;
31431da177e4SLinus Torvalds 			}
314455c52718SAlan Stern 			le16_to_cpus(&devstat);
31451da177e4SLinus Torvalds 			if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
31461da177e4SLinus Torvalds 				dev_err(&udev->dev,
31471da177e4SLinus Torvalds 					"can't connect bus-powered hub "
31481da177e4SLinus Torvalds 					"to this port\n");
31491da177e4SLinus Torvalds 				if (hub->has_indicators) {
31501da177e4SLinus Torvalds 					hub->indicator[port1-1] =
31511da177e4SLinus Torvalds 						INDICATOR_AMBER_BLINK;
3152c4028958SDavid Howells 					schedule_delayed_work (&hub->leds, 0);
31531da177e4SLinus Torvalds 				}
31541da177e4SLinus Torvalds 				status = -ENOTCONN;	/* Don't retry */
31551da177e4SLinus Torvalds 				goto loop_disable;
31561da177e4SLinus Torvalds 			}
31571da177e4SLinus Torvalds 		}
31581da177e4SLinus Torvalds 
31591da177e4SLinus Torvalds 		/* check for devices running slower than they could */
31601da177e4SLinus Torvalds 		if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
31611da177e4SLinus Torvalds 				&& udev->speed == USB_SPEED_FULL
31621da177e4SLinus Torvalds 				&& highspeed_hubs != 0)
31631da177e4SLinus Torvalds 			check_highspeed (hub, udev, port1);
31641da177e4SLinus Torvalds 
31651da177e4SLinus Torvalds 		/* Store the parent's children[] pointer.  At this point
31661da177e4SLinus Torvalds 		 * udev becomes globally accessible, although presumably
31671da177e4SLinus Torvalds 		 * no one will look at it until hdev is unlocked.
31681da177e4SLinus Torvalds 		 */
31691da177e4SLinus Torvalds 		status = 0;
31701da177e4SLinus Torvalds 
31711da177e4SLinus Torvalds 		/* We mustn't add new devices if the parent hub has
31721da177e4SLinus Torvalds 		 * been disconnected; we would race with the
31731da177e4SLinus Torvalds 		 * recursively_mark_NOTATTACHED() routine.
31741da177e4SLinus Torvalds 		 */
31751da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
31761da177e4SLinus Torvalds 		if (hdev->state == USB_STATE_NOTATTACHED)
31771da177e4SLinus Torvalds 			status = -ENOTCONN;
31781da177e4SLinus Torvalds 		else
31791da177e4SLinus Torvalds 			hdev->children[port1-1] = udev;
31801da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
31811da177e4SLinus Torvalds 
31821da177e4SLinus Torvalds 		/* Run it through the hoops (find a driver, etc) */
31831da177e4SLinus Torvalds 		if (!status) {
31841da177e4SLinus Torvalds 			status = usb_new_device(udev);
31851da177e4SLinus Torvalds 			if (status) {
31861da177e4SLinus Torvalds 				spin_lock_irq(&device_state_lock);
31871da177e4SLinus Torvalds 				hdev->children[port1-1] = NULL;
31881da177e4SLinus Torvalds 				spin_unlock_irq(&device_state_lock);
31891da177e4SLinus Torvalds 			}
31901da177e4SLinus Torvalds 		}
31911da177e4SLinus Torvalds 
31921da177e4SLinus Torvalds 		if (status)
31931da177e4SLinus Torvalds 			goto loop_disable;
31941da177e4SLinus Torvalds 
31951da177e4SLinus Torvalds 		status = hub_power_remaining(hub);
31961da177e4SLinus Torvalds 		if (status)
319755c52718SAlan Stern 			dev_dbg(hub_dev, "%dmA power budget left\n", status);
31981da177e4SLinus Torvalds 
31991da177e4SLinus Torvalds 		return;
32001da177e4SLinus Torvalds 
32011da177e4SLinus Torvalds loop_disable:
32021da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 1);
32031da177e4SLinus Torvalds loop:
3204fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
32051da177e4SLinus Torvalds 		release_address(udev);
32061da177e4SLinus Torvalds 		usb_put_dev(udev);
3207ffcdc18dSVikram Pandita 		if ((status == -ENOTCONN) || (status == -ENOTSUPP))
32081da177e4SLinus Torvalds 			break;
32091da177e4SLinus Torvalds 	}
32103a31155cSAlan Stern 	if (hub->hdev->parent ||
32113a31155cSAlan Stern 			!hcd->driver->port_handed_over ||
32123a31155cSAlan Stern 			!(hcd->driver->port_handed_over)(hcd, port1))
32133a31155cSAlan Stern 		dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
32143a31155cSAlan Stern 				port1);
32151da177e4SLinus Torvalds 
32161da177e4SLinus Torvalds done:
32171da177e4SLinus Torvalds 	hub_port_disable(hub, port1, 1);
321890da096eSBalaji Rao 	if (hcd->driver->relinquish_port && !hub->hdev->parent)
321990da096eSBalaji Rao 		hcd->driver->relinquish_port(hcd, port1);
32201da177e4SLinus Torvalds }
32211da177e4SLinus Torvalds 
32221da177e4SLinus Torvalds static void hub_events(void)
32231da177e4SLinus Torvalds {
32241da177e4SLinus Torvalds 	struct list_head *tmp;
32251da177e4SLinus Torvalds 	struct usb_device *hdev;
32261da177e4SLinus Torvalds 	struct usb_interface *intf;
32271da177e4SLinus Torvalds 	struct usb_hub *hub;
32281da177e4SLinus Torvalds 	struct device *hub_dev;
32291da177e4SLinus Torvalds 	u16 hubstatus;
32301da177e4SLinus Torvalds 	u16 hubchange;
32311da177e4SLinus Torvalds 	u16 portstatus;
32321da177e4SLinus Torvalds 	u16 portchange;
32331da177e4SLinus Torvalds 	int i, ret;
32341da177e4SLinus Torvalds 	int connect_change;
32351da177e4SLinus Torvalds 
32361da177e4SLinus Torvalds 	/*
32371da177e4SLinus Torvalds 	 *  We restart the list every time to avoid a deadlock with
32381da177e4SLinus Torvalds 	 * deleting hubs downstream from this one. This should be
32391da177e4SLinus Torvalds 	 * safe since we delete the hub from the event list.
32401da177e4SLinus Torvalds 	 * Not the most efficient, but avoids deadlocks.
32411da177e4SLinus Torvalds 	 */
32421da177e4SLinus Torvalds 	while (1) {
32431da177e4SLinus Torvalds 
32441da177e4SLinus Torvalds 		/* Grab the first entry at the beginning of the list */
32451da177e4SLinus Torvalds 		spin_lock_irq(&hub_event_lock);
32461da177e4SLinus Torvalds 		if (list_empty(&hub_event_list)) {
32471da177e4SLinus Torvalds 			spin_unlock_irq(&hub_event_lock);
32481da177e4SLinus Torvalds 			break;
32491da177e4SLinus Torvalds 		}
32501da177e4SLinus Torvalds 
32511da177e4SLinus Torvalds 		tmp = hub_event_list.next;
32521da177e4SLinus Torvalds 		list_del_init(tmp);
32531da177e4SLinus Torvalds 
32541da177e4SLinus Torvalds 		hub = list_entry(tmp, struct usb_hub, event_list);
3255e8054854SAlan Stern 		kref_get(&hub->kref);
3256e8054854SAlan Stern 		spin_unlock_irq(&hub_event_lock);
32571da177e4SLinus Torvalds 
3258e8054854SAlan Stern 		hdev = hub->hdev;
3259e8054854SAlan Stern 		hub_dev = hub->intfdev;
3260e8054854SAlan Stern 		intf = to_usb_interface(hub_dev);
326140f122f3SAlan Stern 		dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
32621da177e4SLinus Torvalds 				hdev->state, hub->descriptor
32631da177e4SLinus Torvalds 					? hub->descriptor->bNbrPorts
32641da177e4SLinus Torvalds 					: 0,
32651da177e4SLinus Torvalds 				/* NOTE: expects max 15 ports... */
32661da177e4SLinus Torvalds 				(u16) hub->change_bits[0],
326740f122f3SAlan Stern 				(u16) hub->event_bits[0]);
32681da177e4SLinus Torvalds 
32691da177e4SLinus Torvalds 		/* Lock the device, then check to see if we were
32701da177e4SLinus Torvalds 		 * disconnected while waiting for the lock to succeed. */
327106b84e8aSAlan Stern 		usb_lock_device(hdev);
3272e8054854SAlan Stern 		if (unlikely(hub->disconnected))
32738e4ceb38SAlan Stern 			goto loop2;
32741da177e4SLinus Torvalds 
32751da177e4SLinus Torvalds 		/* If the hub has died, clean up after it */
32761da177e4SLinus Torvalds 		if (hdev->state == USB_STATE_NOTATTACHED) {
32777de18d8bSAlan Stern 			hub->error = -ENODEV;
32784330354fSAlan Stern 			hub_quiesce(hub, HUB_DISCONNECT);
32791da177e4SLinus Torvalds 			goto loop;
32801da177e4SLinus Torvalds 		}
32811da177e4SLinus Torvalds 
328240f122f3SAlan Stern 		/* Autoresume */
328340f122f3SAlan Stern 		ret = usb_autopm_get_interface(intf);
328440f122f3SAlan Stern 		if (ret) {
328540f122f3SAlan Stern 			dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
32861da177e4SLinus Torvalds 			goto loop;
328740f122f3SAlan Stern 		}
328840f122f3SAlan Stern 
328940f122f3SAlan Stern 		/* If this is an inactive hub, do nothing */
329040f122f3SAlan Stern 		if (hub->quiescing)
329140f122f3SAlan Stern 			goto loop_autopm;
32921da177e4SLinus Torvalds 
32931da177e4SLinus Torvalds 		if (hub->error) {
32941da177e4SLinus Torvalds 			dev_dbg (hub_dev, "resetting for error %d\n",
32951da177e4SLinus Torvalds 				hub->error);
32961da177e4SLinus Torvalds 
3297742120c6SMing Lei 			ret = usb_reset_device(hdev);
32981da177e4SLinus Torvalds 			if (ret) {
32991da177e4SLinus Torvalds 				dev_dbg (hub_dev,
33001da177e4SLinus Torvalds 					"error resetting hub: %d\n", ret);
330140f122f3SAlan Stern 				goto loop_autopm;
33021da177e4SLinus Torvalds 			}
33031da177e4SLinus Torvalds 
33041da177e4SLinus Torvalds 			hub->nerrors = 0;
33051da177e4SLinus Torvalds 			hub->error = 0;
33061da177e4SLinus Torvalds 		}
33071da177e4SLinus Torvalds 
33081da177e4SLinus Torvalds 		/* deal with port status changes */
33091da177e4SLinus Torvalds 		for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
33101da177e4SLinus Torvalds 			if (test_bit(i, hub->busy_bits))
33111da177e4SLinus Torvalds 				continue;
33121da177e4SLinus Torvalds 			connect_change = test_bit(i, hub->change_bits);
33131da177e4SLinus Torvalds 			if (!test_and_clear_bit(i, hub->event_bits) &&
33146ee0b270SAlan Stern 					!connect_change)
33151da177e4SLinus Torvalds 				continue;
33161da177e4SLinus Torvalds 
33171da177e4SLinus Torvalds 			ret = hub_port_status(hub, i,
33181da177e4SLinus Torvalds 					&portstatus, &portchange);
33191da177e4SLinus Torvalds 			if (ret < 0)
33201da177e4SLinus Torvalds 				continue;
33211da177e4SLinus Torvalds 
33221da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_CONNECTION) {
33231da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
33241da177e4SLinus Torvalds 					USB_PORT_FEAT_C_CONNECTION);
33251da177e4SLinus Torvalds 				connect_change = 1;
33261da177e4SLinus Torvalds 			}
33271da177e4SLinus Torvalds 
33281da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_ENABLE) {
33291da177e4SLinus Torvalds 				if (!connect_change)
33301da177e4SLinus Torvalds 					dev_dbg (hub_dev,
33311da177e4SLinus Torvalds 						"port %d enable change, "
33321da177e4SLinus Torvalds 						"status %08x\n",
33331da177e4SLinus Torvalds 						i, portstatus);
33341da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
33351da177e4SLinus Torvalds 					USB_PORT_FEAT_C_ENABLE);
33361da177e4SLinus Torvalds 
33371da177e4SLinus Torvalds 				/*
33381da177e4SLinus Torvalds 				 * EM interference sometimes causes badly
33391da177e4SLinus Torvalds 				 * shielded USB devices to be shutdown by
33401da177e4SLinus Torvalds 				 * the hub, this hack enables them again.
33411da177e4SLinus Torvalds 				 * Works at least with mouse driver.
33421da177e4SLinus Torvalds 				 */
33431da177e4SLinus Torvalds 				if (!(portstatus & USB_PORT_STAT_ENABLE)
33441da177e4SLinus Torvalds 				    && !connect_change
33451da177e4SLinus Torvalds 				    && hdev->children[i-1]) {
33461da177e4SLinus Torvalds 					dev_err (hub_dev,
33471da177e4SLinus Torvalds 					    "port %i "
33481da177e4SLinus Torvalds 					    "disabled by hub (EMI?), "
33491da177e4SLinus Torvalds 					    "re-enabling...\n",
33501da177e4SLinus Torvalds 						i);
33511da177e4SLinus Torvalds 					connect_change = 1;
33521da177e4SLinus Torvalds 				}
33531da177e4SLinus Torvalds 			}
33541da177e4SLinus Torvalds 
33551da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_SUSPEND) {
33568808f00cSAlan Stern 				struct usb_device *udev;
33578808f00cSAlan Stern 
33581da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
33591da177e4SLinus Torvalds 					USB_PORT_FEAT_C_SUSPEND);
33608808f00cSAlan Stern 				udev = hdev->children[i-1];
33618808f00cSAlan Stern 				if (udev) {
336249d0f078SAlan Stern 					/* TRSMRCY = 10 msec */
336349d0f078SAlan Stern 					msleep(10);
336449d0f078SAlan Stern 
33658808f00cSAlan Stern 					usb_lock_device(udev);
33661da177e4SLinus Torvalds 					ret = remote_wakeup(hdev->
33671da177e4SLinus Torvalds 							children[i-1]);
33688808f00cSAlan Stern 					usb_unlock_device(udev);
33691da177e4SLinus Torvalds 					if (ret < 0)
33701da177e4SLinus Torvalds 						connect_change = 1;
33711da177e4SLinus Torvalds 				} else {
33721da177e4SLinus Torvalds 					ret = -ENODEV;
33731da177e4SLinus Torvalds 					hub_port_disable(hub, i, 1);
33741da177e4SLinus Torvalds 				}
33751da177e4SLinus Torvalds 				dev_dbg (hub_dev,
33761da177e4SLinus Torvalds 					"resume on port %d, status %d\n",
33771da177e4SLinus Torvalds 					i, ret);
33781da177e4SLinus Torvalds 			}
33791da177e4SLinus Torvalds 
33801da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
33811da177e4SLinus Torvalds 				dev_err (hub_dev,
33821da177e4SLinus Torvalds 					"over-current change on port %d\n",
33831da177e4SLinus Torvalds 					i);
33841da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
33851da177e4SLinus Torvalds 					USB_PORT_FEAT_C_OVER_CURRENT);
33868520f380SAlan Stern 				hub_power_on(hub, true);
33871da177e4SLinus Torvalds 			}
33881da177e4SLinus Torvalds 
33891da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_RESET) {
33901da177e4SLinus Torvalds 				dev_dbg (hub_dev,
33911da177e4SLinus Torvalds 					"reset change on port %d\n",
33921da177e4SLinus Torvalds 					i);
33931da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
33941da177e4SLinus Torvalds 					USB_PORT_FEAT_C_RESET);
33951da177e4SLinus Torvalds 			}
33961da177e4SLinus Torvalds 
33971da177e4SLinus Torvalds 			if (connect_change)
33981da177e4SLinus Torvalds 				hub_port_connect_change(hub, i,
33991da177e4SLinus Torvalds 						portstatus, portchange);
34001da177e4SLinus Torvalds 		} /* end for i */
34011da177e4SLinus Torvalds 
34021da177e4SLinus Torvalds 		/* deal with hub status changes */
34031da177e4SLinus Torvalds 		if (test_and_clear_bit(0, hub->event_bits) == 0)
34041da177e4SLinus Torvalds 			;	/* do nothing */
34051da177e4SLinus Torvalds 		else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
34061da177e4SLinus Torvalds 			dev_err (hub_dev, "get_hub_status failed\n");
34071da177e4SLinus Torvalds 		else {
34081da177e4SLinus Torvalds 			if (hubchange & HUB_CHANGE_LOCAL_POWER) {
34091da177e4SLinus Torvalds 				dev_dbg (hub_dev, "power change\n");
34101da177e4SLinus Torvalds 				clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
341155c52718SAlan Stern 				if (hubstatus & HUB_STATUS_LOCAL_POWER)
341255c52718SAlan Stern 					/* FIXME: Is this always true? */
341355c52718SAlan Stern 					hub->limited_power = 1;
3414403fae78Sjidong xiao 				else
3415403fae78Sjidong xiao 					hub->limited_power = 0;
34161da177e4SLinus Torvalds 			}
34171da177e4SLinus Torvalds 			if (hubchange & HUB_CHANGE_OVERCURRENT) {
34181da177e4SLinus Torvalds 				dev_dbg (hub_dev, "overcurrent change\n");
34191da177e4SLinus Torvalds 				msleep(500);	/* Cool down */
34201da177e4SLinus Torvalds 				clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
34218520f380SAlan Stern                         	hub_power_on(hub, true);
34221da177e4SLinus Torvalds 			}
34231da177e4SLinus Torvalds 		}
34241da177e4SLinus Torvalds 
342540f122f3SAlan Stern  loop_autopm:
34268e4ceb38SAlan Stern 		/* Balance the usb_autopm_get_interface() above */
34278e4ceb38SAlan Stern 		usb_autopm_put_interface_no_suspend(intf);
34281da177e4SLinus Torvalds  loop:
34298e4ceb38SAlan Stern 		/* Balance the usb_autopm_get_interface_no_resume() in
34308e4ceb38SAlan Stern 		 * kick_khubd() and allow autosuspend.
34318e4ceb38SAlan Stern 		 */
34328e4ceb38SAlan Stern 		usb_autopm_put_interface(intf);
34338e4ceb38SAlan Stern  loop2:
34341da177e4SLinus Torvalds 		usb_unlock_device(hdev);
3435e8054854SAlan Stern 		kref_put(&hub->kref, hub_release);
34361da177e4SLinus Torvalds 
34371da177e4SLinus Torvalds         } /* end while (1) */
34381da177e4SLinus Torvalds }
34391da177e4SLinus Torvalds 
34401da177e4SLinus Torvalds static int hub_thread(void *__unused)
34411da177e4SLinus Torvalds {
34423bb1af52SAlan Stern 	/* khubd needs to be freezable to avoid intefering with USB-PERSIST
34433bb1af52SAlan Stern 	 * port handover.  Otherwise it might see that a full-speed device
34443bb1af52SAlan Stern 	 * was gone before the EHCI controller had handed its port over to
34453bb1af52SAlan Stern 	 * the companion full-speed controller.
34463bb1af52SAlan Stern 	 */
344783144186SRafael J. Wysocki 	set_freezable();
34483bb1af52SAlan Stern 
34491da177e4SLinus Torvalds 	do {
34501da177e4SLinus Torvalds 		hub_events();
3451e42837bcSRafael J. Wysocki 		wait_event_freezable(khubd_wait,
34529c8d6178Sakpm@osdl.org 				!list_empty(&hub_event_list) ||
34539c8d6178Sakpm@osdl.org 				kthread_should_stop());
34549c8d6178Sakpm@osdl.org 	} while (!kthread_should_stop() || !list_empty(&hub_event_list));
34551da177e4SLinus Torvalds 
34561da177e4SLinus Torvalds 	pr_debug("%s: khubd exiting\n", usbcore_name);
34579c8d6178Sakpm@osdl.org 	return 0;
34581da177e4SLinus Torvalds }
34591da177e4SLinus Torvalds 
34601da177e4SLinus Torvalds static struct usb_device_id hub_id_table [] = {
34611da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
34621da177e4SLinus Torvalds       .bDeviceClass = USB_CLASS_HUB},
34631da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
34641da177e4SLinus Torvalds       .bInterfaceClass = USB_CLASS_HUB},
34651da177e4SLinus Torvalds     { }						/* Terminating entry */
34661da177e4SLinus Torvalds };
34671da177e4SLinus Torvalds 
34681da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, hub_id_table);
34691da177e4SLinus Torvalds 
34701da177e4SLinus Torvalds static struct usb_driver hub_driver = {
34711da177e4SLinus Torvalds 	.name =		"hub",
34721da177e4SLinus Torvalds 	.probe =	hub_probe,
34731da177e4SLinus Torvalds 	.disconnect =	hub_disconnect,
34741da177e4SLinus Torvalds 	.suspend =	hub_suspend,
34751da177e4SLinus Torvalds 	.resume =	hub_resume,
3476f07600cfSAlan Stern 	.reset_resume =	hub_reset_resume,
34777de18d8bSAlan Stern 	.pre_reset =	hub_pre_reset,
34787de18d8bSAlan Stern 	.post_reset =	hub_post_reset,
34791da177e4SLinus Torvalds 	.ioctl =	hub_ioctl,
34801da177e4SLinus Torvalds 	.id_table =	hub_id_table,
348140f122f3SAlan Stern 	.supports_autosuspend =	1,
34821da177e4SLinus Torvalds };
34831da177e4SLinus Torvalds 
34841da177e4SLinus Torvalds int usb_hub_init(void)
34851da177e4SLinus Torvalds {
34861da177e4SLinus Torvalds 	if (usb_register(&hub_driver) < 0) {
34871da177e4SLinus Torvalds 		printk(KERN_ERR "%s: can't register hub driver\n",
34881da177e4SLinus Torvalds 			usbcore_name);
34891da177e4SLinus Torvalds 		return -1;
34901da177e4SLinus Torvalds 	}
34911da177e4SLinus Torvalds 
34929c8d6178Sakpm@osdl.org 	khubd_task = kthread_run(hub_thread, NULL, "khubd");
34939c8d6178Sakpm@osdl.org 	if (!IS_ERR(khubd_task))
34941da177e4SLinus Torvalds 		return 0;
34951da177e4SLinus Torvalds 
34961da177e4SLinus Torvalds 	/* Fall through if kernel_thread failed */
34971da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
34981da177e4SLinus Torvalds 	printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
34991da177e4SLinus Torvalds 
35001da177e4SLinus Torvalds 	return -1;
35011da177e4SLinus Torvalds }
35021da177e4SLinus Torvalds 
35031da177e4SLinus Torvalds void usb_hub_cleanup(void)
35041da177e4SLinus Torvalds {
35059c8d6178Sakpm@osdl.org 	kthread_stop(khubd_task);
35061da177e4SLinus Torvalds 
35071da177e4SLinus Torvalds 	/*
35081da177e4SLinus Torvalds 	 * Hub resources are freed for us by usb_deregister. It calls
35091da177e4SLinus Torvalds 	 * usb_driver_purge on every device which in turn calls that
35101da177e4SLinus Torvalds 	 * devices disconnect function if it is using this driver.
35111da177e4SLinus Torvalds 	 * The hub_disconnect function takes care of releasing the
35121da177e4SLinus Torvalds 	 * individual hub resources. -greg
35131da177e4SLinus Torvalds 	 */
35141da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
35151da177e4SLinus Torvalds } /* usb_hub_cleanup() */
35161da177e4SLinus Torvalds 
3517eb764c4bSAlan Stern static int descriptors_changed(struct usb_device *udev,
3518eb764c4bSAlan Stern 		struct usb_device_descriptor *old_device_descriptor)
35191da177e4SLinus Torvalds {
3520eb764c4bSAlan Stern 	int		changed = 0;
35211da177e4SLinus Torvalds 	unsigned	index;
3522eb764c4bSAlan Stern 	unsigned	serial_len = 0;
3523eb764c4bSAlan Stern 	unsigned	len;
3524eb764c4bSAlan Stern 	unsigned	old_length;
3525eb764c4bSAlan Stern 	int		length;
3526eb764c4bSAlan Stern 	char		*buf;
35271da177e4SLinus Torvalds 
3528eb764c4bSAlan Stern 	if (memcmp(&udev->descriptor, old_device_descriptor,
3529eb764c4bSAlan Stern 			sizeof(*old_device_descriptor)) != 0)
3530eb764c4bSAlan Stern 		return 1;
3531eb764c4bSAlan Stern 
3532eb764c4bSAlan Stern 	/* Since the idVendor, idProduct, and bcdDevice values in the
3533eb764c4bSAlan Stern 	 * device descriptor haven't changed, we will assume the
3534eb764c4bSAlan Stern 	 * Manufacturer and Product strings haven't changed either.
3535eb764c4bSAlan Stern 	 * But the SerialNumber string could be different (e.g., a
3536eb764c4bSAlan Stern 	 * different flash card of the same brand).
3537eb764c4bSAlan Stern 	 */
3538eb764c4bSAlan Stern 	if (udev->serial)
3539eb764c4bSAlan Stern 		serial_len = strlen(udev->serial) + 1;
3540eb764c4bSAlan Stern 
3541eb764c4bSAlan Stern 	len = serial_len;
35421da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
3543eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
3544eb764c4bSAlan Stern 		len = max(len, old_length);
35451da177e4SLinus Torvalds 	}
3546eb764c4bSAlan Stern 
35470cc1a51fSOliver Neukum 	buf = kmalloc(len, GFP_NOIO);
35481da177e4SLinus Torvalds 	if (buf == NULL) {
35491da177e4SLinus Torvalds 		dev_err(&udev->dev, "no mem to re-read configs after reset\n");
35501da177e4SLinus Torvalds 		/* assume the worst */
35511da177e4SLinus Torvalds 		return 1;
35521da177e4SLinus Torvalds 	}
35531da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
3554eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
35551da177e4SLinus Torvalds 		length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
35561da177e4SLinus Torvalds 				old_length);
3557eb764c4bSAlan Stern 		if (length != old_length) {
35581da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d, error %d\n",
35591da177e4SLinus Torvalds 					index, length);
3560eb764c4bSAlan Stern 			changed = 1;
35611da177e4SLinus Torvalds 			break;
35621da177e4SLinus Torvalds 		}
35631da177e4SLinus Torvalds 		if (memcmp (buf, udev->rawdescriptors[index], old_length)
35641da177e4SLinus Torvalds 				!= 0) {
35651da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
3566eb764c4bSAlan Stern 				index,
3567eb764c4bSAlan Stern 				((struct usb_config_descriptor *) buf)->
3568eb764c4bSAlan Stern 					bConfigurationValue);
3569eb764c4bSAlan Stern 			changed = 1;
35701da177e4SLinus Torvalds 			break;
35711da177e4SLinus Torvalds 		}
35721da177e4SLinus Torvalds 	}
3573eb764c4bSAlan Stern 
3574eb764c4bSAlan Stern 	if (!changed && serial_len) {
3575eb764c4bSAlan Stern 		length = usb_string(udev, udev->descriptor.iSerialNumber,
3576eb764c4bSAlan Stern 				buf, serial_len);
3577eb764c4bSAlan Stern 		if (length + 1 != serial_len) {
3578eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string error %d\n",
3579eb764c4bSAlan Stern 					length);
3580eb764c4bSAlan Stern 			changed = 1;
3581eb764c4bSAlan Stern 		} else if (memcmp(buf, udev->serial, length) != 0) {
3582eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string changed\n");
3583eb764c4bSAlan Stern 			changed = 1;
3584eb764c4bSAlan Stern 		}
3585eb764c4bSAlan Stern 	}
3586eb764c4bSAlan Stern 
35871da177e4SLinus Torvalds 	kfree(buf);
3588eb764c4bSAlan Stern 	return changed;
35891da177e4SLinus Torvalds }
35901da177e4SLinus Torvalds 
35911da177e4SLinus Torvalds /**
3592742120c6SMing Lei  * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
35931da177e4SLinus Torvalds  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
35941da177e4SLinus Torvalds  *
359579efa097SAlan Stern  * WARNING - don't use this routine to reset a composite device
359679efa097SAlan Stern  * (one with multiple interfaces owned by separate drivers)!
3597742120c6SMing Lei  * Use usb_reset_device() instead.
35981da177e4SLinus Torvalds  *
35991da177e4SLinus Torvalds  * Do a port reset, reassign the device's address, and establish its
36001da177e4SLinus Torvalds  * former operating configuration.  If the reset fails, or the device's
36011da177e4SLinus Torvalds  * descriptors change from their values before the reset, or the original
36021da177e4SLinus Torvalds  * configuration and altsettings cannot be restored, a flag will be set
36031da177e4SLinus Torvalds  * telling khubd to pretend the device has been disconnected and then
36041da177e4SLinus Torvalds  * re-connected.  All drivers will be unbound, and the device will be
36051da177e4SLinus Torvalds  * re-enumerated and probed all over again.
36061da177e4SLinus Torvalds  *
36071da177e4SLinus Torvalds  * Returns 0 if the reset succeeded, -ENODEV if the device has been
36081da177e4SLinus Torvalds  * flagged for logical disconnection, or some other negative error code
36091da177e4SLinus Torvalds  * if the reset wasn't even attempted.
36101da177e4SLinus Torvalds  *
36111da177e4SLinus Torvalds  * The caller must own the device lock.  For example, it's safe to use
36121da177e4SLinus Torvalds  * this from a driver probe() routine after downloading new firmware.
36131da177e4SLinus Torvalds  * For calls that might not occur during probe(), drivers should lock
36141da177e4SLinus Torvalds  * the device using usb_lock_device_for_reset().
36156bc6cff5SAlan Stern  *
36166bc6cff5SAlan Stern  * Locking exception: This routine may also be called from within an
36176bc6cff5SAlan Stern  * autoresume handler.  Such usage won't conflict with other tasks
36186bc6cff5SAlan Stern  * holding the device lock because these tasks should always call
36196bc6cff5SAlan Stern  * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
36201da177e4SLinus Torvalds  */
3621742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev)
36221da177e4SLinus Torvalds {
36231da177e4SLinus Torvalds 	struct usb_device		*parent_hdev = udev->parent;
36241da177e4SLinus Torvalds 	struct usb_hub			*parent_hub;
36253f0479e0SSarah Sharp 	struct usb_hcd			*hcd = bus_to_hcd(udev->bus);
36261da177e4SLinus Torvalds 	struct usb_device_descriptor	descriptor = udev->descriptor;
362712c3da34SAlan Stern 	int 				i, ret = 0;
362812c3da34SAlan Stern 	int				port1 = udev->portnum;
36291da177e4SLinus Torvalds 
36301da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED ||
36311da177e4SLinus Torvalds 			udev->state == USB_STATE_SUSPENDED) {
36321da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
36331da177e4SLinus Torvalds 				udev->state);
36341da177e4SLinus Torvalds 		return -EINVAL;
36351da177e4SLinus Torvalds 	}
36361da177e4SLinus Torvalds 
36371da177e4SLinus Torvalds 	if (!parent_hdev) {
36381da177e4SLinus Torvalds 		/* this requires hcd-specific logic; see OHCI hc_restart() */
3639441b62c1SHarvey Harrison 		dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
36401da177e4SLinus Torvalds 		return -EISDIR;
36411da177e4SLinus Torvalds 	}
36421da177e4SLinus Torvalds 	parent_hub = hdev_to_hub(parent_hdev);
36431da177e4SLinus Torvalds 
36441da177e4SLinus Torvalds 	set_bit(port1, parent_hub->busy_bits);
36451da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; ++i) {
36461da177e4SLinus Torvalds 
36471da177e4SLinus Torvalds 		/* ep0 maxpacket size may change; let the HCD know about it.
36481da177e4SLinus Torvalds 		 * Other endpoints will be handled by re-enumeration. */
3649fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
36501da177e4SLinus Torvalds 		ret = hub_port_init(parent_hub, udev, port1, i);
3651dd4dd19eSAlan Stern 		if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
36521da177e4SLinus Torvalds 			break;
36531da177e4SLinus Torvalds 	}
36541da177e4SLinus Torvalds 	clear_bit(port1, parent_hub->busy_bits);
3655d5cbad4bSAlan Stern 
36561da177e4SLinus Torvalds 	if (ret < 0)
36571da177e4SLinus Torvalds 		goto re_enumerate;
36581da177e4SLinus Torvalds 
36591da177e4SLinus Torvalds 	/* Device might have changed firmware (DFU or similar) */
3660eb764c4bSAlan Stern 	if (descriptors_changed(udev, &descriptor)) {
36611da177e4SLinus Torvalds 		dev_info(&udev->dev, "device firmware changed\n");
36621da177e4SLinus Torvalds 		udev->descriptor = descriptor;	/* for disconnect() calls */
36631da177e4SLinus Torvalds 		goto re_enumerate;
36641da177e4SLinus Torvalds   	}
36651da177e4SLinus Torvalds 
36664fe0387aSAlan Stern 	/* Restore the device's previous configuration */
36671da177e4SLinus Torvalds 	if (!udev->actconfig)
36681da177e4SLinus Torvalds 		goto done;
36693f0479e0SSarah Sharp 
36703f0479e0SSarah Sharp 	mutex_lock(&hcd->bandwidth_mutex);
36713f0479e0SSarah Sharp 	ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
36723f0479e0SSarah Sharp 	if (ret < 0) {
36733f0479e0SSarah Sharp 		dev_warn(&udev->dev,
36743f0479e0SSarah Sharp 				"Busted HC?  Not enough HCD resources for "
36753f0479e0SSarah Sharp 				"old configuration.\n");
36763f0479e0SSarah Sharp 		mutex_unlock(&hcd->bandwidth_mutex);
36773f0479e0SSarah Sharp 		goto re_enumerate;
36783f0479e0SSarah Sharp 	}
36791da177e4SLinus Torvalds 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
36801da177e4SLinus Torvalds 			USB_REQ_SET_CONFIGURATION, 0,
36811da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, 0,
36821da177e4SLinus Torvalds 			NULL, 0, USB_CTRL_SET_TIMEOUT);
36831da177e4SLinus Torvalds 	if (ret < 0) {
36841da177e4SLinus Torvalds 		dev_err(&udev->dev,
36851da177e4SLinus Torvalds 			"can't restore configuration #%d (error=%d)\n",
36861da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, ret);
36873f0479e0SSarah Sharp 		mutex_unlock(&hcd->bandwidth_mutex);
36881da177e4SLinus Torvalds 		goto re_enumerate;
36891da177e4SLinus Torvalds   	}
36903f0479e0SSarah Sharp 	mutex_unlock(&hcd->bandwidth_mutex);
36911da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_CONFIGURED);
36921da177e4SLinus Torvalds 
36934fe0387aSAlan Stern 	/* Put interfaces back into the same altsettings as before.
36944fe0387aSAlan Stern 	 * Don't bother to send the Set-Interface request for interfaces
36954fe0387aSAlan Stern 	 * that were already in altsetting 0; besides being unnecessary,
36964fe0387aSAlan Stern 	 * many devices can't handle it.  Instead just reset the host-side
36974fe0387aSAlan Stern 	 * endpoint state.
36984fe0387aSAlan Stern 	 */
36991da177e4SLinus Torvalds 	for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
37003f0479e0SSarah Sharp 		struct usb_host_config *config = udev->actconfig;
37013f0479e0SSarah Sharp 		struct usb_interface *intf = config->interface[i];
37021da177e4SLinus Torvalds 		struct usb_interface_descriptor *desc;
37031da177e4SLinus Torvalds 
37041da177e4SLinus Torvalds 		desc = &intf->cur_altsetting->desc;
37054fe0387aSAlan Stern 		if (desc->bAlternateSetting == 0) {
37064fe0387aSAlan Stern 			usb_disable_interface(udev, intf, true);
37074fe0387aSAlan Stern 			usb_enable_interface(udev, intf, true);
37084fe0387aSAlan Stern 			ret = 0;
37094fe0387aSAlan Stern 		} else {
371004a723eaSSarah Sharp 			/* Let the bandwidth allocation function know that this
371104a723eaSSarah Sharp 			 * device has been reset, and it will have to use
371204a723eaSSarah Sharp 			 * alternate setting 0 as the current alternate setting.
37133f0479e0SSarah Sharp 			 */
371404a723eaSSarah Sharp 			intf->resetting_device = 1;
37151da177e4SLinus Torvalds 			ret = usb_set_interface(udev, desc->bInterfaceNumber,
37161da177e4SLinus Torvalds 					desc->bAlternateSetting);
371704a723eaSSarah Sharp 			intf->resetting_device = 0;
37184fe0387aSAlan Stern 		}
37191da177e4SLinus Torvalds 		if (ret < 0) {
37201da177e4SLinus Torvalds 			dev_err(&udev->dev, "failed to restore interface %d "
37211da177e4SLinus Torvalds 				"altsetting %d (error=%d)\n",
37221da177e4SLinus Torvalds 				desc->bInterfaceNumber,
37231da177e4SLinus Torvalds 				desc->bAlternateSetting,
37241da177e4SLinus Torvalds 				ret);
37251da177e4SLinus Torvalds 			goto re_enumerate;
37261da177e4SLinus Torvalds 		}
37271da177e4SLinus Torvalds 	}
37281da177e4SLinus Torvalds 
37291da177e4SLinus Torvalds done:
37301da177e4SLinus Torvalds 	return 0;
37311da177e4SLinus Torvalds 
37321da177e4SLinus Torvalds re_enumerate:
37331da177e4SLinus Torvalds 	hub_port_logical_disconnect(parent_hub, port1);
37341da177e4SLinus Torvalds 	return -ENODEV;
37351da177e4SLinus Torvalds }
373679efa097SAlan Stern 
373779efa097SAlan Stern /**
3738742120c6SMing Lei  * usb_reset_device - warn interface drivers and perform a USB port reset
373979efa097SAlan Stern  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
374079efa097SAlan Stern  *
374179efa097SAlan Stern  * Warns all drivers bound to registered interfaces (using their pre_reset
374279efa097SAlan Stern  * method), performs the port reset, and then lets the drivers know that
374379efa097SAlan Stern  * the reset is over (using their post_reset method).
374479efa097SAlan Stern  *
3745742120c6SMing Lei  * Return value is the same as for usb_reset_and_verify_device().
374679efa097SAlan Stern  *
374779efa097SAlan Stern  * The caller must own the device lock.  For example, it's safe to use
374879efa097SAlan Stern  * this from a driver probe() routine after downloading new firmware.
374979efa097SAlan Stern  * For calls that might not occur during probe(), drivers should lock
375079efa097SAlan Stern  * the device using usb_lock_device_for_reset().
375178d9a487SAlan Stern  *
375278d9a487SAlan Stern  * If an interface is currently being probed or disconnected, we assume
375378d9a487SAlan Stern  * its driver knows how to handle resets.  For all other interfaces,
375478d9a487SAlan Stern  * if the driver doesn't have pre_reset and post_reset methods then
375578d9a487SAlan Stern  * we attempt to unbind it and rebind afterward.
375679efa097SAlan Stern  */
3757742120c6SMing Lei int usb_reset_device(struct usb_device *udev)
375879efa097SAlan Stern {
375979efa097SAlan Stern 	int ret;
3760852c4b43SAlan Stern 	int i;
376179efa097SAlan Stern 	struct usb_host_config *config = udev->actconfig;
376279efa097SAlan Stern 
376379efa097SAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED ||
376479efa097SAlan Stern 			udev->state == USB_STATE_SUSPENDED) {
376579efa097SAlan Stern 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
376679efa097SAlan Stern 				udev->state);
376779efa097SAlan Stern 		return -EINVAL;
376879efa097SAlan Stern 	}
376979efa097SAlan Stern 
3770645daaabSAlan Stern 	/* Prevent autosuspend during the reset */
377194fcda1fSAlan Stern 	usb_autoresume_device(udev);
3772645daaabSAlan Stern 
377379efa097SAlan Stern 	if (config) {
3774852c4b43SAlan Stern 		for (i = 0; i < config->desc.bNumInterfaces; ++i) {
3775852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
377679efa097SAlan Stern 			struct usb_driver *drv;
377778d9a487SAlan Stern 			int unbind = 0;
377879efa097SAlan Stern 
3779852c4b43SAlan Stern 			if (cintf->dev.driver) {
378079efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
378178d9a487SAlan Stern 				if (drv->pre_reset && drv->post_reset)
378278d9a487SAlan Stern 					unbind = (drv->pre_reset)(cintf);
378378d9a487SAlan Stern 				else if (cintf->condition ==
378478d9a487SAlan Stern 						USB_INTERFACE_BOUND)
378578d9a487SAlan Stern 					unbind = 1;
378678d9a487SAlan Stern 				if (unbind)
378778d9a487SAlan Stern 					usb_forced_unbind_intf(cintf);
378879efa097SAlan Stern 			}
378979efa097SAlan Stern 		}
379079efa097SAlan Stern 	}
379179efa097SAlan Stern 
3792742120c6SMing Lei 	ret = usb_reset_and_verify_device(udev);
379379efa097SAlan Stern 
379479efa097SAlan Stern 	if (config) {
3795852c4b43SAlan Stern 		for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
3796852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
379779efa097SAlan Stern 			struct usb_driver *drv;
379878d9a487SAlan Stern 			int rebind = cintf->needs_binding;
379979efa097SAlan Stern 
380078d9a487SAlan Stern 			if (!rebind && cintf->dev.driver) {
380179efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
380279efa097SAlan Stern 				if (drv->post_reset)
380378d9a487SAlan Stern 					rebind = (drv->post_reset)(cintf);
380478d9a487SAlan Stern 				else if (cintf->condition ==
380578d9a487SAlan Stern 						USB_INTERFACE_BOUND)
380678d9a487SAlan Stern 					rebind = 1;
380779efa097SAlan Stern 			}
38086c640945SAlan Stern 			if (ret == 0 && rebind)
380978d9a487SAlan Stern 				usb_rebind_intf(cintf);
381079efa097SAlan Stern 		}
381179efa097SAlan Stern 	}
381279efa097SAlan Stern 
381394fcda1fSAlan Stern 	usb_autosuspend_device(udev);
381479efa097SAlan Stern 	return ret;
381579efa097SAlan Stern }
3816742120c6SMing Lei EXPORT_SYMBOL_GPL(usb_reset_device);
3817dc023dceSInaky Perez-Gonzalez 
3818dc023dceSInaky Perez-Gonzalez 
3819dc023dceSInaky Perez-Gonzalez /**
3820dc023dceSInaky Perez-Gonzalez  * usb_queue_reset_device - Reset a USB device from an atomic context
3821dc023dceSInaky Perez-Gonzalez  * @iface: USB interface belonging to the device to reset
3822dc023dceSInaky Perez-Gonzalez  *
3823dc023dceSInaky Perez-Gonzalez  * This function can be used to reset a USB device from an atomic
3824dc023dceSInaky Perez-Gonzalez  * context, where usb_reset_device() won't work (as it blocks).
3825dc023dceSInaky Perez-Gonzalez  *
3826dc023dceSInaky Perez-Gonzalez  * Doing a reset via this method is functionally equivalent to calling
3827dc023dceSInaky Perez-Gonzalez  * usb_reset_device(), except for the fact that it is delayed to a
3828dc023dceSInaky Perez-Gonzalez  * workqueue. This means that any drivers bound to other interfaces
3829dc023dceSInaky Perez-Gonzalez  * might be unbound, as well as users from usbfs in user space.
3830dc023dceSInaky Perez-Gonzalez  *
3831dc023dceSInaky Perez-Gonzalez  * Corner cases:
3832dc023dceSInaky Perez-Gonzalez  *
3833dc023dceSInaky Perez-Gonzalez  * - Scheduling two resets at the same time from two different drivers
3834dc023dceSInaky Perez-Gonzalez  *   attached to two different interfaces of the same device is
3835dc023dceSInaky Perez-Gonzalez  *   possible; depending on how the driver attached to each interface
3836dc023dceSInaky Perez-Gonzalez  *   handles ->pre_reset(), the second reset might happen or not.
3837dc023dceSInaky Perez-Gonzalez  *
3838dc023dceSInaky Perez-Gonzalez  * - If a driver is unbound and it had a pending reset, the reset will
3839dc023dceSInaky Perez-Gonzalez  *   be cancelled.
3840dc023dceSInaky Perez-Gonzalez  *
3841dc023dceSInaky Perez-Gonzalez  * - This function can be called during .probe() or .disconnect()
3842dc023dceSInaky Perez-Gonzalez  *   times. On return from .disconnect(), any pending resets will be
3843dc023dceSInaky Perez-Gonzalez  *   cancelled.
3844dc023dceSInaky Perez-Gonzalez  *
3845dc023dceSInaky Perez-Gonzalez  * There is no no need to lock/unlock the @reset_ws as schedule_work()
3846dc023dceSInaky Perez-Gonzalez  * does its own.
3847dc023dceSInaky Perez-Gonzalez  *
3848dc023dceSInaky Perez-Gonzalez  * NOTE: We don't do any reference count tracking because it is not
3849dc023dceSInaky Perez-Gonzalez  *     needed. The lifecycle of the work_struct is tied to the
3850dc023dceSInaky Perez-Gonzalez  *     usb_interface. Before destroying the interface we cancel the
3851dc023dceSInaky Perez-Gonzalez  *     work_struct, so the fact that work_struct is queued and or
3852dc023dceSInaky Perez-Gonzalez  *     running means the interface (and thus, the device) exist and
3853dc023dceSInaky Perez-Gonzalez  *     are referenced.
3854dc023dceSInaky Perez-Gonzalez  */
3855dc023dceSInaky Perez-Gonzalez void usb_queue_reset_device(struct usb_interface *iface)
3856dc023dceSInaky Perez-Gonzalez {
3857dc023dceSInaky Perez-Gonzalez 	schedule_work(&iface->reset_ws);
3858dc023dceSInaky Perez-Gonzalez }
3859dc023dceSInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_queue_reset_device);
3860