xref: /openbmc/linux/drivers/usb/core/hub.c (revision 749da5f8)
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>
2227729aadSEric Lescouet #include <linux/usb/hcd.h>
239c8d6178Sakpm@osdl.org #include <linux/kthread.h>
244186ecf8SArjan van de Ven #include <linux/mutex.h>
257dfb7103SNigel Cunningham #include <linux/freezer.h>
269bbdf1e0SAlan Stern #include <linux/pm_runtime.h>
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #include <asm/uaccess.h>
291da177e4SLinus Torvalds #include <asm/byteorder.h>
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include "usb.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 
741bb5f66bSAlan Stern 	unsigned		limited_power:1;
751bb5f66bSAlan Stern 	unsigned		quiescing:1;
76e8054854SAlan Stern 	unsigned		disconnected:1;
771bb5f66bSAlan Stern 
781bb5f66bSAlan Stern 	unsigned		has_indicators:1;
791bb5f66bSAlan Stern 	u8			indicator[USB_MAXCHILDREN];
806d5aefb8SDavid Howells 	struct delayed_work	leds;
818520f380SAlan Stern 	struct delayed_work	init_work;
827cbe5dcaSAlan Stern 	void			**port_owners;
831bb5f66bSAlan Stern };
841bb5f66bSAlan Stern 
851bb5f66bSAlan Stern 
861da177e4SLinus Torvalds /* Protect struct usb_device->state and ->children members
879ad3d6ccSAlan Stern  * Note: Both are also protected by ->dev.sem, except that ->state can
881da177e4SLinus Torvalds  * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
891da177e4SLinus Torvalds static DEFINE_SPINLOCK(device_state_lock);
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /* khubd's worklist and its lock */
921da177e4SLinus Torvalds static DEFINE_SPINLOCK(hub_event_lock);
931da177e4SLinus Torvalds static LIST_HEAD(hub_event_list);	/* List of hubs needing servicing */
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds /* Wakes up khubd */
961da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
971da177e4SLinus Torvalds 
989c8d6178Sakpm@osdl.org static struct task_struct *khubd_task;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds /* cycle leds on hubs that aren't blinking for attention */
1011da177e4SLinus Torvalds static int blinkenlights = 0;
1021da177e4SLinus Torvalds module_param (blinkenlights, bool, S_IRUGO);
1031da177e4SLinus Torvalds MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds /*
106fd7c519dSJaroslav Kysela  * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
107fd7c519dSJaroslav Kysela  * 10 seconds to send reply for the initial 64-byte descriptor request.
108fd7c519dSJaroslav Kysela  */
109fd7c519dSJaroslav Kysela /* define initial 64-byte descriptor request timeout in milliseconds */
110fd7c519dSJaroslav Kysela static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
111fd7c519dSJaroslav Kysela module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
112b9cef6c3SWu Fengguang MODULE_PARM_DESC(initial_descriptor_timeout,
113b9cef6c3SWu Fengguang 		"initial 64-byte descriptor request timeout in milliseconds "
114b9cef6c3SWu Fengguang 		"(default 5000 - 5.0 seconds)");
115fd7c519dSJaroslav Kysela 
116fd7c519dSJaroslav Kysela /*
1171da177e4SLinus Torvalds  * As of 2.6.10 we introduce a new USB device initialization scheme which
1181da177e4SLinus Torvalds  * closely resembles the way Windows works.  Hopefully it will be compatible
1191da177e4SLinus Torvalds  * with a wider range of devices than the old scheme.  However some previously
1201da177e4SLinus Torvalds  * working devices may start giving rise to "device not accepting address"
1211da177e4SLinus Torvalds  * errors; if that happens the user can try the old scheme by adjusting the
1221da177e4SLinus Torvalds  * following module parameters.
1231da177e4SLinus Torvalds  *
1241da177e4SLinus Torvalds  * For maximum flexibility there are two boolean parameters to control the
1251da177e4SLinus Torvalds  * hub driver's behavior.  On the first initialization attempt, if the
1261da177e4SLinus Torvalds  * "old_scheme_first" parameter is set then the old scheme will be used,
1271da177e4SLinus Torvalds  * otherwise the new scheme is used.  If that fails and "use_both_schemes"
1281da177e4SLinus Torvalds  * is set, then the driver will make another attempt, using the other scheme.
1291da177e4SLinus Torvalds  */
1301da177e4SLinus Torvalds static int old_scheme_first = 0;
1311da177e4SLinus Torvalds module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
1321da177e4SLinus Torvalds MODULE_PARM_DESC(old_scheme_first,
1331da177e4SLinus Torvalds 		 "start with the old device initialization scheme");
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds static int use_both_schemes = 1;
1361da177e4SLinus Torvalds module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
1371da177e4SLinus Torvalds MODULE_PARM_DESC(use_both_schemes,
1381da177e4SLinus Torvalds 		"try the other device initialization scheme if the "
1391da177e4SLinus Torvalds 		"first one fails");
1401da177e4SLinus Torvalds 
14132fe0198SAlan Stern /* Mutual exclusion for EHCI CF initialization.  This interferes with
14232fe0198SAlan Stern  * port reset on some companion controllers.
14332fe0198SAlan Stern  */
14432fe0198SAlan Stern DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
14532fe0198SAlan Stern EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
14632fe0198SAlan Stern 
147948fea37SAlan Stern #define HUB_DEBOUNCE_TIMEOUT	1500
148948fea37SAlan Stern #define HUB_DEBOUNCE_STEP	  25
149948fea37SAlan Stern #define HUB_DEBOUNCE_STABLE	 100
150948fea37SAlan Stern 
1511da177e4SLinus Torvalds 
152742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev);
153742120c6SMing Lei 
1541da177e4SLinus Torvalds static inline char *portspeed(int portstatus)
1551da177e4SLinus Torvalds {
156288ead45SAlan Stern 	if (portstatus & USB_PORT_STAT_HIGH_SPEED)
1571da177e4SLinus Torvalds     		return "480 Mb/s";
158288ead45SAlan Stern 	else if (portstatus & USB_PORT_STAT_LOW_SPEED)
1591da177e4SLinus Torvalds 		return "1.5 Mb/s";
160288ead45SAlan Stern 	else if (portstatus & USB_PORT_STAT_SUPER_SPEED)
161d2e9b4d6SSarah Sharp 		return "5.0 Gb/s";
1621da177e4SLinus Torvalds 	else
1631da177e4SLinus Torvalds 		return "12 Mb/s";
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds /* Note that hdev or one of its children must be locked! */
16725118084SAlan Stern static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
1681da177e4SLinus Torvalds {
16925118084SAlan Stern 	if (!hdev || !hdev->actconfig)
17025118084SAlan Stern 		return NULL;
1711da177e4SLinus Torvalds 	return usb_get_intfdata(hdev->actconfig->interface[0]);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.4.5 */
1751da177e4SLinus Torvalds static int get_hub_descriptor(struct usb_device *hdev, void *data, int size)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	int i, ret;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	for (i = 0; i < 3; i++) {
1801da177e4SLinus Torvalds 		ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
1811da177e4SLinus Torvalds 			USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
1821da177e4SLinus Torvalds 			USB_DT_HUB << 8, 0, data, size,
1831da177e4SLinus Torvalds 			USB_CTRL_GET_TIMEOUT);
1841da177e4SLinus Torvalds 		if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
1851da177e4SLinus Torvalds 			return ret;
1861da177e4SLinus Torvalds 	}
1871da177e4SLinus Torvalds 	return -EINVAL;
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds /*
1911da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.1
1921da177e4SLinus Torvalds  */
1931da177e4SLinus Torvalds static int clear_hub_feature(struct usb_device *hdev, int feature)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1961da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds /*
2001da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.2
2011da177e4SLinus Torvalds  */
2021da177e4SLinus Torvalds static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
2051da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
2061da177e4SLinus Torvalds 		NULL, 0, 1000);
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds /*
2101da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.13
2111da177e4SLinus Torvalds  */
2121da177e4SLinus Torvalds static int set_port_feature(struct usb_device *hdev, int port1, int feature)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
2151da177e4SLinus Torvalds 		USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
2161da177e4SLinus Torvalds 		NULL, 0, 1000);
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds /*
2201da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
2211da177e4SLinus Torvalds  * for info about using port indicators
2221da177e4SLinus Torvalds  */
2231da177e4SLinus Torvalds static void set_port_led(
2241da177e4SLinus Torvalds 	struct usb_hub *hub,
2251da177e4SLinus Torvalds 	int port1,
2261da177e4SLinus Torvalds 	int selector
2271da177e4SLinus Torvalds )
2281da177e4SLinus Torvalds {
2291da177e4SLinus Torvalds 	int status = set_port_feature(hub->hdev, (selector << 8) | port1,
2301da177e4SLinus Torvalds 			USB_PORT_FEAT_INDICATOR);
2311da177e4SLinus Torvalds 	if (status < 0)
2321da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
2331da177e4SLinus Torvalds 			"port %d indicator %s status %d\n",
2341da177e4SLinus Torvalds 			port1,
2351da177e4SLinus Torvalds 			({ char *s; switch (selector) {
2361da177e4SLinus Torvalds 			case HUB_LED_AMBER: s = "amber"; break;
2371da177e4SLinus Torvalds 			case HUB_LED_GREEN: s = "green"; break;
2381da177e4SLinus Torvalds 			case HUB_LED_OFF: s = "off"; break;
2391da177e4SLinus Torvalds 			case HUB_LED_AUTO: s = "auto"; break;
2401da177e4SLinus Torvalds 			default: s = "??"; break;
2411da177e4SLinus Torvalds 			}; s; }),
2421da177e4SLinus Torvalds 			status);
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds #define	LED_CYCLE_PERIOD	((2*HZ)/3)
2461da177e4SLinus Torvalds 
247c4028958SDavid Howells static void led_work (struct work_struct *work)
2481da177e4SLinus Torvalds {
249c4028958SDavid Howells 	struct usb_hub		*hub =
250c4028958SDavid Howells 		container_of(work, struct usb_hub, leds.work);
2511da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
2521da177e4SLinus Torvalds 	unsigned		i;
2531da177e4SLinus Torvalds 	unsigned		changed = 0;
2541da177e4SLinus Torvalds 	int			cursor = -1;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
2571da177e4SLinus Torvalds 		return;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
2601da177e4SLinus Torvalds 		unsigned	selector, mode;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 		/* 30%-50% duty cycle */
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 		switch (hub->indicator[i]) {
2651da177e4SLinus Torvalds 		/* cycle marker */
2661da177e4SLinus Torvalds 		case INDICATOR_CYCLE:
2671da177e4SLinus Torvalds 			cursor = i;
2681da177e4SLinus Torvalds 			selector = HUB_LED_AUTO;
2691da177e4SLinus Torvalds 			mode = INDICATOR_AUTO;
2701da177e4SLinus Torvalds 			break;
2711da177e4SLinus Torvalds 		/* blinking green = sw attention */
2721da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK:
2731da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
2741da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK_OFF;
2751da177e4SLinus Torvalds 			break;
2761da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK_OFF:
2771da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
2781da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK;
2791da177e4SLinus Torvalds 			break;
2801da177e4SLinus Torvalds 		/* blinking amber = hw attention */
2811da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK:
2821da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
2831da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK_OFF;
2841da177e4SLinus Torvalds 			break;
2851da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK_OFF:
2861da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
2871da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK;
2881da177e4SLinus Torvalds 			break;
2891da177e4SLinus Torvalds 		/* blink green/amber = reserved */
2901da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK:
2911da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
2921da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK_OFF;
2931da177e4SLinus Torvalds 			break;
2941da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK_OFF:
2951da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
2961da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK;
2971da177e4SLinus Torvalds 			break;
2981da177e4SLinus Torvalds 		default:
2991da177e4SLinus Torvalds 			continue;
3001da177e4SLinus Torvalds 		}
3011da177e4SLinus Torvalds 		if (selector != HUB_LED_AUTO)
3021da177e4SLinus Torvalds 			changed = 1;
3031da177e4SLinus Torvalds 		set_port_led(hub, i + 1, selector);
3041da177e4SLinus Torvalds 		hub->indicator[i] = mode;
3051da177e4SLinus Torvalds 	}
3061da177e4SLinus Torvalds 	if (!changed && blinkenlights) {
3071da177e4SLinus Torvalds 		cursor++;
3081da177e4SLinus Torvalds 		cursor %= hub->descriptor->bNbrPorts;
3091da177e4SLinus Torvalds 		set_port_led(hub, cursor + 1, HUB_LED_GREEN);
3101da177e4SLinus Torvalds 		hub->indicator[cursor] = INDICATOR_CYCLE;
3111da177e4SLinus Torvalds 		changed++;
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 	if (changed)
3141da177e4SLinus Torvalds 		schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds /* use a short timeout for hub/port status fetches */
3181da177e4SLinus Torvalds #define	USB_STS_TIMEOUT		1000
3191da177e4SLinus Torvalds #define	USB_STS_RETRIES		5
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds /*
3221da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.6
3231da177e4SLinus Torvalds  */
3241da177e4SLinus Torvalds static int get_hub_status(struct usb_device *hdev,
3251da177e4SLinus Torvalds 		struct usb_hub_status *data)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
3301da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
3311da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
3321da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
3331da177e4SLinus Torvalds 	}
3341da177e4SLinus Torvalds 	return status;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds /*
3381da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7
3391da177e4SLinus Torvalds  */
3401da177e4SLinus Torvalds static int get_port_status(struct usb_device *hdev, int port1,
3411da177e4SLinus Torvalds 		struct usb_port_status *data)
3421da177e4SLinus Torvalds {
3431da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
3461da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
3471da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
3481da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
3491da177e4SLinus Torvalds 	}
3501da177e4SLinus Torvalds 	return status;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3533eb14915SAlan Stern static int hub_port_status(struct usb_hub *hub, int port1,
3543eb14915SAlan Stern 		u16 *status, u16 *change)
3553eb14915SAlan Stern {
3563eb14915SAlan Stern 	int ret;
3573eb14915SAlan Stern 
3583eb14915SAlan Stern 	mutex_lock(&hub->status_mutex);
3593eb14915SAlan Stern 	ret = get_port_status(hub->hdev, port1, &hub->status->port);
3603eb14915SAlan Stern 	if (ret < 4) {
3613eb14915SAlan Stern 		dev_err(hub->intfdev,
3623eb14915SAlan Stern 			"%s failed (err = %d)\n", __func__, ret);
3633eb14915SAlan Stern 		if (ret >= 0)
3643eb14915SAlan Stern 			ret = -EIO;
3653eb14915SAlan Stern 	} else {
3663eb14915SAlan Stern 		*status = le16_to_cpu(hub->status->port.wPortStatus);
3673eb14915SAlan Stern 		*change = le16_to_cpu(hub->status->port.wPortChange);
3683eb14915SAlan Stern 		ret = 0;
3693eb14915SAlan Stern 	}
3703eb14915SAlan Stern 	mutex_unlock(&hub->status_mutex);
3713eb14915SAlan Stern 	return ret;
3723eb14915SAlan Stern }
3733eb14915SAlan Stern 
3741da177e4SLinus Torvalds static void kick_khubd(struct usb_hub *hub)
3751da177e4SLinus Torvalds {
3761da177e4SLinus Torvalds 	unsigned long	flags;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	spin_lock_irqsave(&hub_event_lock, flags);
3792e2c5eeaSRoel Kluin 	if (!hub->disconnected && list_empty(&hub->event_list)) {
3801da177e4SLinus Torvalds 		list_add_tail(&hub->event_list, &hub_event_list);
3818e4ceb38SAlan Stern 
3828e4ceb38SAlan Stern 		/* Suppress autosuspend until khubd runs */
3838e4ceb38SAlan Stern 		usb_autopm_get_interface_no_resume(
3848e4ceb38SAlan Stern 				to_usb_interface(hub->intfdev));
3851da177e4SLinus Torvalds 		wake_up(&khubd_wait);
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds 	spin_unlock_irqrestore(&hub_event_lock, flags);
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds void usb_kick_khubd(struct usb_device *hdev)
3911da177e4SLinus Torvalds {
39225118084SAlan Stern 	struct usb_hub *hub = hdev_to_hub(hdev);
39325118084SAlan Stern 
39425118084SAlan Stern 	if (hub)
39525118084SAlan Stern 		kick_khubd(hub);
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds /* completion function, fires on port status changes and various faults */
4007d12e780SDavid Howells static void hub_irq(struct urb *urb)
4011da177e4SLinus Torvalds {
402ec17cf1cSTobias Klauser 	struct usb_hub *hub = urb->context;
403e015268dSAlan Stern 	int status = urb->status;
40471d2718fSRoel Kluin 	unsigned i;
4051da177e4SLinus Torvalds 	unsigned long bits;
4061da177e4SLinus Torvalds 
407e015268dSAlan Stern 	switch (status) {
4081da177e4SLinus Torvalds 	case -ENOENT:		/* synchronous unlink */
4091da177e4SLinus Torvalds 	case -ECONNRESET:	/* async unlink */
4101da177e4SLinus Torvalds 	case -ESHUTDOWN:	/* hardware going away */
4111da177e4SLinus Torvalds 		return;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	default:		/* presumably an error */
4141da177e4SLinus Torvalds 		/* Cause a hub reset after 10 consecutive errors */
415e015268dSAlan Stern 		dev_dbg (hub->intfdev, "transfer --> %d\n", status);
4161da177e4SLinus Torvalds 		if ((++hub->nerrors < 10) || hub->error)
4171da177e4SLinus Torvalds 			goto resubmit;
418e015268dSAlan Stern 		hub->error = status;
4191da177e4SLinus Torvalds 		/* FALL THROUGH */
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	/* let khubd handle things */
4221da177e4SLinus Torvalds 	case 0:			/* we got data:  port status changed */
4231da177e4SLinus Torvalds 		bits = 0;
4241da177e4SLinus Torvalds 		for (i = 0; i < urb->actual_length; ++i)
4251da177e4SLinus Torvalds 			bits |= ((unsigned long) ((*hub->buffer)[i]))
4261da177e4SLinus Torvalds 					<< (i*8);
4271da177e4SLinus Torvalds 		hub->event_bits[0] = bits;
4281da177e4SLinus Torvalds 		break;
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	hub->nerrors = 0;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	/* Something happened, let khubd figure it out */
4341da177e4SLinus Torvalds 	kick_khubd(hub);
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds resubmit:
4371da177e4SLinus Torvalds 	if (hub->quiescing)
4381da177e4SLinus Torvalds 		return;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
4411da177e4SLinus Torvalds 			&& status != -ENODEV && status != -EPERM)
4421da177e4SLinus Torvalds 		dev_err (hub->intfdev, "resubmit --> %d\n", status);
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.2.3 */
4461da177e4SLinus Torvalds static inline int
4471da177e4SLinus Torvalds hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
4481da177e4SLinus Torvalds {
449c2f6595fSAlan Stern 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
4501da177e4SLinus Torvalds 			       HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
4511da177e4SLinus Torvalds 			       tt, NULL, 0, 1000);
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds /*
4551da177e4SLinus Torvalds  * enumeration blocks khubd for a long time. we use keventd instead, since
4561da177e4SLinus Torvalds  * long blocking there is the exception, not the rule.  accordingly, HCDs
4571da177e4SLinus Torvalds  * talking to TTs must queue control transfers (not just bulk and iso), so
4581da177e4SLinus Torvalds  * both can talk to the same hub concurrently.
4591da177e4SLinus Torvalds  */
460cb88a1b8SAlan Stern static void hub_tt_work(struct work_struct *work)
4611da177e4SLinus Torvalds {
462c4028958SDavid Howells 	struct usb_hub		*hub =
463cb88a1b8SAlan Stern 		container_of(work, struct usb_hub, tt.clear_work);
4641da177e4SLinus Torvalds 	unsigned long		flags;
46555e5fdfaSMark Lord 	int			limit = 100;
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	spin_lock_irqsave (&hub->tt.lock, flags);
46855e5fdfaSMark Lord 	while (--limit && !list_empty (&hub->tt.clear_list)) {
469d0f830d3SH Hartley Sweeten 		struct list_head	*next;
4701da177e4SLinus Torvalds 		struct usb_tt_clear	*clear;
4711da177e4SLinus Torvalds 		struct usb_device	*hdev = hub->hdev;
472cb88a1b8SAlan Stern 		const struct hc_driver	*drv;
4731da177e4SLinus Torvalds 		int			status;
4741da177e4SLinus Torvalds 
475d0f830d3SH Hartley Sweeten 		next = hub->tt.clear_list.next;
476d0f830d3SH Hartley Sweeten 		clear = list_entry (next, struct usb_tt_clear, clear_list);
4771da177e4SLinus Torvalds 		list_del (&clear->clear_list);
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 		/* drop lock so HCD can concurrently report other TT errors */
4801da177e4SLinus Torvalds 		spin_unlock_irqrestore (&hub->tt.lock, flags);
4811da177e4SLinus Torvalds 		status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
4821da177e4SLinus Torvalds 		if (status)
4831da177e4SLinus Torvalds 			dev_err (&hdev->dev,
4841da177e4SLinus Torvalds 				"clear tt %d (%04x) error %d\n",
4851da177e4SLinus Torvalds 				clear->tt, clear->devinfo, status);
486cb88a1b8SAlan Stern 
487cb88a1b8SAlan Stern 		/* Tell the HCD, even if the operation failed */
488cb88a1b8SAlan Stern 		drv = clear->hcd->driver;
489cb88a1b8SAlan Stern 		if (drv->clear_tt_buffer_complete)
490cb88a1b8SAlan Stern 			(drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
491cb88a1b8SAlan Stern 
4921da177e4SLinus Torvalds 		kfree(clear);
493cb88a1b8SAlan Stern 		spin_lock_irqsave(&hub->tt.lock, flags);
4941da177e4SLinus Torvalds 	}
4951da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hub->tt.lock, flags);
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds /**
499cb88a1b8SAlan Stern  * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
500cb88a1b8SAlan Stern  * @urb: an URB associated with the failed or incomplete split transaction
5011da177e4SLinus Torvalds  *
5021da177e4SLinus Torvalds  * High speed HCDs use this to tell the hub driver that some split control or
5031da177e4SLinus Torvalds  * bulk transaction failed in a way that requires clearing internal state of
5041da177e4SLinus Torvalds  * a transaction translator.  This is normally detected (and reported) from
5051da177e4SLinus Torvalds  * interrupt context.
5061da177e4SLinus Torvalds  *
5071da177e4SLinus Torvalds  * It may not be possible for that hub to handle additional full (or low)
5081da177e4SLinus Torvalds  * speed transactions until that state is fully cleared out.
5091da177e4SLinus Torvalds  */
510cb88a1b8SAlan Stern int usb_hub_clear_tt_buffer(struct urb *urb)
5111da177e4SLinus Torvalds {
512cb88a1b8SAlan Stern 	struct usb_device	*udev = urb->dev;
513cb88a1b8SAlan Stern 	int			pipe = urb->pipe;
5141da177e4SLinus Torvalds 	struct usb_tt		*tt = udev->tt;
5151da177e4SLinus Torvalds 	unsigned long		flags;
5161da177e4SLinus Torvalds 	struct usb_tt_clear	*clear;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	/* we've got to cope with an arbitrary number of pending TT clears,
5191da177e4SLinus Torvalds 	 * since each TT has "at least two" buffers that can need it (and
5201da177e4SLinus Torvalds 	 * there can be many TTs per hub).  even if they're uncommon.
5211da177e4SLinus Torvalds 	 */
52254e6ecb2SChristoph Lameter 	if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
5231da177e4SLinus Torvalds 		dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
5241da177e4SLinus Torvalds 		/* FIXME recover somehow ... RESET_TT? */
525cb88a1b8SAlan Stern 		return -ENOMEM;
5261da177e4SLinus Torvalds 	}
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	/* info that CLEAR_TT_BUFFER needs */
5291da177e4SLinus Torvalds 	clear->tt = tt->multi ? udev->ttport : 1;
5301da177e4SLinus Torvalds 	clear->devinfo = usb_pipeendpoint (pipe);
5311da177e4SLinus Torvalds 	clear->devinfo |= udev->devnum << 4;
5321da177e4SLinus Torvalds 	clear->devinfo |= usb_pipecontrol (pipe)
5331da177e4SLinus Torvalds 			? (USB_ENDPOINT_XFER_CONTROL << 11)
5341da177e4SLinus Torvalds 			: (USB_ENDPOINT_XFER_BULK << 11);
5351da177e4SLinus Torvalds 	if (usb_pipein (pipe))
5361da177e4SLinus Torvalds 		clear->devinfo |= 1 << 15;
5371da177e4SLinus Torvalds 
538cb88a1b8SAlan Stern 	/* info for completion callback */
539cb88a1b8SAlan Stern 	clear->hcd = bus_to_hcd(udev->bus);
540cb88a1b8SAlan Stern 	clear->ep = urb->ep;
541cb88a1b8SAlan Stern 
5421da177e4SLinus Torvalds 	/* tell keventd to clear state for this TT */
5431da177e4SLinus Torvalds 	spin_lock_irqsave (&tt->lock, flags);
5441da177e4SLinus Torvalds 	list_add_tail (&clear->clear_list, &tt->clear_list);
545cb88a1b8SAlan Stern 	schedule_work(&tt->clear_work);
5461da177e4SLinus Torvalds 	spin_unlock_irqrestore (&tt->lock, flags);
547cb88a1b8SAlan Stern 	return 0;
5481da177e4SLinus Torvalds }
549cb88a1b8SAlan Stern EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
5501da177e4SLinus Torvalds 
5518520f380SAlan Stern /* If do_delay is false, return the number of milliseconds the caller
5528520f380SAlan Stern  * needs to delay.
5538520f380SAlan Stern  */
5548520f380SAlan Stern static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
5551da177e4SLinus Torvalds {
5561da177e4SLinus Torvalds 	int port1;
557b789696aSDavid Brownell 	unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
5588520f380SAlan Stern 	unsigned delay;
5594489a571SAlan Stern 	u16 wHubCharacteristics =
5604489a571SAlan Stern 			le16_to_cpu(hub->descriptor->wHubCharacteristics);
5611da177e4SLinus Torvalds 
5624489a571SAlan Stern 	/* Enable power on each port.  Some hubs have reserved values
5634489a571SAlan Stern 	 * of LPSM (> 2) in their descriptors, even though they are
5644489a571SAlan Stern 	 * USB 2.0 hubs.  Some hubs do not implement port-power switching
5654489a571SAlan Stern 	 * but only emulate it.  In all cases, the ports won't work
5664489a571SAlan Stern 	 * unless we send these messages to the hub.
5674489a571SAlan Stern 	 */
5684489a571SAlan Stern 	if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
5691da177e4SLinus Torvalds 		dev_dbg(hub->intfdev, "enabling power on all ports\n");
5704489a571SAlan Stern 	else
5714489a571SAlan Stern 		dev_dbg(hub->intfdev, "trying to enable port power on "
5724489a571SAlan Stern 				"non-switchable hub\n");
5731da177e4SLinus Torvalds 	for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
5744489a571SAlan Stern 		set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
5751da177e4SLinus Torvalds 
576b789696aSDavid Brownell 	/* Wait at least 100 msec for power to become stable */
5778520f380SAlan Stern 	delay = max(pgood_delay, (unsigned) 100);
5788520f380SAlan Stern 	if (do_delay)
5798520f380SAlan Stern 		msleep(delay);
5808520f380SAlan Stern 	return delay;
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds static int hub_hub_status(struct usb_hub *hub,
5841da177e4SLinus Torvalds 		u16 *status, u16 *change)
5851da177e4SLinus Torvalds {
5861da177e4SLinus Torvalds 	int ret;
5871da177e4SLinus Torvalds 
588db90e7a1SAlan Stern 	mutex_lock(&hub->status_mutex);
5891da177e4SLinus Torvalds 	ret = get_hub_status(hub->hdev, &hub->status->hub);
5901da177e4SLinus Torvalds 	if (ret < 0)
5911da177e4SLinus Torvalds 		dev_err (hub->intfdev,
592441b62c1SHarvey Harrison 			"%s failed (err = %d)\n", __func__, ret);
5931da177e4SLinus Torvalds 	else {
5941da177e4SLinus Torvalds 		*status = le16_to_cpu(hub->status->hub.wHubStatus);
5951da177e4SLinus Torvalds 		*change = le16_to_cpu(hub->status->hub.wHubChange);
5961da177e4SLinus Torvalds 		ret = 0;
5971da177e4SLinus Torvalds 	}
598db90e7a1SAlan Stern 	mutex_unlock(&hub->status_mutex);
5991da177e4SLinus Torvalds 	return ret;
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
6028b28c752SAlan Stern static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
6038b28c752SAlan Stern {
6048b28c752SAlan Stern 	struct usb_device *hdev = hub->hdev;
6050458d5b4SAlan Stern 	int ret = 0;
6068b28c752SAlan Stern 
6070458d5b4SAlan Stern 	if (hdev->children[port1-1] && set_state)
6088b28c752SAlan Stern 		usb_set_device_state(hdev->children[port1-1],
6098b28c752SAlan Stern 				USB_STATE_NOTATTACHED);
6100458d5b4SAlan Stern 	if (!hub->error)
6118b28c752SAlan Stern 		ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
6128b28c752SAlan Stern 	if (ret)
6138b28c752SAlan Stern 		dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
6148b28c752SAlan Stern 				port1, ret);
6158b28c752SAlan Stern 	return ret;
6168b28c752SAlan Stern }
6178b28c752SAlan Stern 
6180458d5b4SAlan Stern /*
6190458d5b4SAlan Stern  * Disable a port and mark a logical connnect-change event, so that some
6200458d5b4SAlan Stern  * time later khubd will disconnect() any existing usb_device on the port
6210458d5b4SAlan Stern  * and will re-enumerate if there actually is a device attached.
6220458d5b4SAlan Stern  */
6230458d5b4SAlan Stern static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
6247d069b7dSAlan Stern {
6250458d5b4SAlan Stern 	dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
6260458d5b4SAlan Stern 	hub_port_disable(hub, port1, 1);
6270458d5b4SAlan Stern 
6280458d5b4SAlan Stern 	/* FIXME let caller ask to power down the port:
6290458d5b4SAlan Stern 	 *  - some devices won't enumerate without a VBUS power cycle
6300458d5b4SAlan Stern 	 *  - SRP saves power that way
6310458d5b4SAlan Stern 	 *  - ... new call, TBD ...
6320458d5b4SAlan Stern 	 * That's easy if this hub can switch power per-port, and
6330458d5b4SAlan Stern 	 * khubd reactivates the port later (timer, SRP, etc).
6340458d5b4SAlan Stern 	 * Powerdown must be optional, because of reset/DFU.
6350458d5b4SAlan Stern 	 */
6360458d5b4SAlan Stern 
6370458d5b4SAlan Stern 	set_bit(port1, hub->change_bits);
6380458d5b4SAlan Stern  	kick_khubd(hub);
6390458d5b4SAlan Stern }
6400458d5b4SAlan Stern 
641253e0572SAlan Stern /**
642253e0572SAlan Stern  * usb_remove_device - disable a device's port on its parent hub
643253e0572SAlan Stern  * @udev: device to be disabled and removed
644253e0572SAlan Stern  * Context: @udev locked, must be able to sleep.
645253e0572SAlan Stern  *
646253e0572SAlan Stern  * After @udev's port has been disabled, khubd is notified and it will
647253e0572SAlan Stern  * see that the device has been disconnected.  When the device is
648253e0572SAlan Stern  * physically unplugged and something is plugged in, the events will
649253e0572SAlan Stern  * be received and processed normally.
650253e0572SAlan Stern  */
651253e0572SAlan Stern int usb_remove_device(struct usb_device *udev)
652253e0572SAlan Stern {
653253e0572SAlan Stern 	struct usb_hub *hub;
654253e0572SAlan Stern 	struct usb_interface *intf;
655253e0572SAlan Stern 
656253e0572SAlan Stern 	if (!udev->parent)	/* Can't remove a root hub */
657253e0572SAlan Stern 		return -EINVAL;
658253e0572SAlan Stern 	hub = hdev_to_hub(udev->parent);
659253e0572SAlan Stern 	intf = to_usb_interface(hub->intfdev);
660253e0572SAlan Stern 
661253e0572SAlan Stern 	usb_autopm_get_interface(intf);
662253e0572SAlan Stern 	set_bit(udev->portnum, hub->removed_bits);
663253e0572SAlan Stern 	hub_port_logical_disconnect(hub, udev->portnum);
664253e0572SAlan Stern 	usb_autopm_put_interface(intf);
665253e0572SAlan Stern 	return 0;
666253e0572SAlan Stern }
667253e0572SAlan Stern 
6686ee0b270SAlan Stern enum hub_activation_type {
6698e4ceb38SAlan Stern 	HUB_INIT, HUB_INIT2, HUB_INIT3,		/* INITs must come first */
6708520f380SAlan Stern 	HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
6716ee0b270SAlan Stern };
6725e6effaeSAlan Stern 
6738520f380SAlan Stern static void hub_init_func2(struct work_struct *ws);
6748520f380SAlan Stern static void hub_init_func3(struct work_struct *ws);
6758520f380SAlan Stern 
676f2835219SAlan Stern static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
6775e6effaeSAlan Stern {
6785e6effaeSAlan Stern 	struct usb_device *hdev = hub->hdev;
6795e6effaeSAlan Stern 	int port1;
680f2835219SAlan Stern 	int status;
681948fea37SAlan Stern 	bool need_debounce_delay = false;
6828520f380SAlan Stern 	unsigned delay;
6838520f380SAlan Stern 
6848520f380SAlan Stern 	/* Continue a partial initialization */
6858520f380SAlan Stern 	if (type == HUB_INIT2)
6868520f380SAlan Stern 		goto init2;
6878520f380SAlan Stern 	if (type == HUB_INIT3)
6888520f380SAlan Stern 		goto init3;
6895e6effaeSAlan Stern 
690f2835219SAlan Stern 	/* After a resume, port power should still be on.
691f2835219SAlan Stern 	 * For any other type of activation, turn it on.
692f2835219SAlan Stern 	 */
6938520f380SAlan Stern 	if (type != HUB_RESUME) {
6948520f380SAlan Stern 
6958520f380SAlan Stern 		/* Speed up system boot by using a delayed_work for the
6968520f380SAlan Stern 		 * hub's initial power-up delays.  This is pretty awkward
6978520f380SAlan Stern 		 * and the implementation looks like a home-brewed sort of
6988520f380SAlan Stern 		 * setjmp/longjmp, but it saves at least 100 ms for each
6998520f380SAlan Stern 		 * root hub (assuming usbcore is compiled into the kernel
7008520f380SAlan Stern 		 * rather than as a module).  It adds up.
7018520f380SAlan Stern 		 *
7028520f380SAlan Stern 		 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
7038520f380SAlan Stern 		 * because for those activation types the ports have to be
7048520f380SAlan Stern 		 * operational when we return.  In theory this could be done
7058520f380SAlan Stern 		 * for HUB_POST_RESET, but it's easier not to.
7068520f380SAlan Stern 		 */
7078520f380SAlan Stern 		if (type == HUB_INIT) {
7088520f380SAlan Stern 			delay = hub_power_on(hub, false);
7098520f380SAlan Stern 			PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
7108520f380SAlan Stern 			schedule_delayed_work(&hub->init_work,
7118520f380SAlan Stern 					msecs_to_jiffies(delay));
71261fbeba1SAlan Stern 
71361fbeba1SAlan Stern 			/* Suppress autosuspend until init is done */
7148e4ceb38SAlan Stern 			usb_autopm_get_interface_no_resume(
7158e4ceb38SAlan Stern 					to_usb_interface(hub->intfdev));
7168520f380SAlan Stern 			return;		/* Continues at init2: below */
7178520f380SAlan Stern 		} else {
7188520f380SAlan Stern 			hub_power_on(hub, true);
7198520f380SAlan Stern 		}
7208520f380SAlan Stern 	}
7218520f380SAlan Stern  init2:
722f2835219SAlan Stern 
7236ee0b270SAlan Stern 	/* Check each port and set hub->change_bits to let khubd know
7246ee0b270SAlan Stern 	 * which ports need attention.
7255e6effaeSAlan Stern 	 */
7265e6effaeSAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
7275e6effaeSAlan Stern 		struct usb_device *udev = hdev->children[port1-1];
7285e6effaeSAlan Stern 		u16 portstatus, portchange;
7295e6effaeSAlan Stern 
7306ee0b270SAlan Stern 		portstatus = portchange = 0;
7316ee0b270SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
7326ee0b270SAlan Stern 		if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
7336ee0b270SAlan Stern 			dev_dbg(hub->intfdev,
7346ee0b270SAlan Stern 					"port %d: status %04x change %04x\n",
7356ee0b270SAlan Stern 					port1, portstatus, portchange);
7365e6effaeSAlan Stern 
7376ee0b270SAlan Stern 		/* After anything other than HUB_RESUME (i.e., initialization
7386ee0b270SAlan Stern 		 * or any sort of reset), every port should be disabled.
7396ee0b270SAlan Stern 		 * Unconnected ports should likewise be disabled (paranoia),
7406ee0b270SAlan Stern 		 * and so should ports for which we have no usb_device.
7415e6effaeSAlan Stern 		 */
7426ee0b270SAlan Stern 		if ((portstatus & USB_PORT_STAT_ENABLE) && (
7436ee0b270SAlan Stern 				type != HUB_RESUME ||
7446ee0b270SAlan Stern 				!(portstatus & USB_PORT_STAT_CONNECTION) ||
7456ee0b270SAlan Stern 				!udev ||
7466ee0b270SAlan Stern 				udev->state == USB_STATE_NOTATTACHED)) {
7476ee0b270SAlan Stern 			clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
7486ee0b270SAlan Stern 			portstatus &= ~USB_PORT_STAT_ENABLE;
7496ee0b270SAlan Stern 		}
7506ee0b270SAlan Stern 
751948fea37SAlan Stern 		/* Clear status-change flags; we'll debounce later */
752948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
753948fea37SAlan Stern 			need_debounce_delay = true;
754948fea37SAlan Stern 			clear_port_feature(hub->hdev, port1,
755948fea37SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
756948fea37SAlan Stern 		}
757948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE) {
758948fea37SAlan Stern 			need_debounce_delay = true;
759948fea37SAlan Stern 			clear_port_feature(hub->hdev, port1,
760948fea37SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
761948fea37SAlan Stern 		}
762948fea37SAlan Stern 
763253e0572SAlan Stern 		/* We can forget about a "removed" device when there's a
764253e0572SAlan Stern 		 * physical disconnect or the connect status changes.
765253e0572SAlan Stern 		 */
766253e0572SAlan Stern 		if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
767253e0572SAlan Stern 				(portchange & USB_PORT_STAT_C_CONNECTION))
768253e0572SAlan Stern 			clear_bit(port1, hub->removed_bits);
769253e0572SAlan Stern 
7706ee0b270SAlan Stern 		if (!udev || udev->state == USB_STATE_NOTATTACHED) {
7716ee0b270SAlan Stern 			/* Tell khubd to disconnect the device or
7726ee0b270SAlan Stern 			 * check for a new connection
7736ee0b270SAlan Stern 			 */
7746ee0b270SAlan Stern 			if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
7756ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
7766ee0b270SAlan Stern 
7776ee0b270SAlan Stern 		} else if (portstatus & USB_PORT_STAT_ENABLE) {
7786ee0b270SAlan Stern 			/* The power session apparently survived the resume.
7796ee0b270SAlan Stern 			 * If there was an overcurrent or suspend change
7806ee0b270SAlan Stern 			 * (i.e., remote wakeup request), have khubd
7816ee0b270SAlan Stern 			 * take care of it.
7826ee0b270SAlan Stern 			 */
7836ee0b270SAlan Stern 			if (portchange)
7846ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
7856ee0b270SAlan Stern 
7866ee0b270SAlan Stern 		} else if (udev->persist_enabled) {
7876ee0b270SAlan Stern #ifdef CONFIG_PM
7885e6effaeSAlan Stern 			udev->reset_resume = 1;
7896ee0b270SAlan Stern #endif
7908808f00cSAlan Stern 			set_bit(port1, hub->change_bits);
7918808f00cSAlan Stern 
7926ee0b270SAlan Stern 		} else {
7936ee0b270SAlan Stern 			/* The power session is gone; tell khubd */
7946ee0b270SAlan Stern 			usb_set_device_state(udev, USB_STATE_NOTATTACHED);
7956ee0b270SAlan Stern 			set_bit(port1, hub->change_bits);
7965e6effaeSAlan Stern 		}
7975e6effaeSAlan Stern 	}
7985e6effaeSAlan Stern 
799948fea37SAlan Stern 	/* If no port-status-change flags were set, we don't need any
800948fea37SAlan Stern 	 * debouncing.  If flags were set we can try to debounce the
801948fea37SAlan Stern 	 * ports all at once right now, instead of letting khubd do them
802948fea37SAlan Stern 	 * one at a time later on.
803948fea37SAlan Stern 	 *
804948fea37SAlan Stern 	 * If any port-status changes do occur during this delay, khubd
805948fea37SAlan Stern 	 * will see them later and handle them normally.
806948fea37SAlan Stern 	 */
8078520f380SAlan Stern 	if (need_debounce_delay) {
8088520f380SAlan Stern 		delay = HUB_DEBOUNCE_STABLE;
809f2835219SAlan Stern 
8108520f380SAlan Stern 		/* Don't do a long sleep inside a workqueue routine */
8118520f380SAlan Stern 		if (type == HUB_INIT2) {
8128520f380SAlan Stern 			PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
8138520f380SAlan Stern 			schedule_delayed_work(&hub->init_work,
8148520f380SAlan Stern 					msecs_to_jiffies(delay));
8158520f380SAlan Stern 			return;		/* Continues at init3: below */
8168520f380SAlan Stern 		} else {
8178520f380SAlan Stern 			msleep(delay);
8188520f380SAlan Stern 		}
8198520f380SAlan Stern 	}
8208520f380SAlan Stern  init3:
821f2835219SAlan Stern 	hub->quiescing = 0;
822f2835219SAlan Stern 
823f2835219SAlan Stern 	status = usb_submit_urb(hub->urb, GFP_NOIO);
824f2835219SAlan Stern 	if (status < 0)
825f2835219SAlan Stern 		dev_err(hub->intfdev, "activate --> %d\n", status);
826f2835219SAlan Stern 	if (hub->has_indicators && blinkenlights)
827f2835219SAlan Stern 		schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
828f2835219SAlan Stern 
829f2835219SAlan Stern 	/* Scan all ports that need attention */
830f2835219SAlan Stern 	kick_khubd(hub);
8318e4ceb38SAlan Stern 
8328e4ceb38SAlan Stern 	/* Allow autosuspend if it was suppressed */
8338e4ceb38SAlan Stern 	if (type <= HUB_INIT3)
8348e4ceb38SAlan Stern 		usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
8355e6effaeSAlan Stern }
8365e6effaeSAlan Stern 
8378520f380SAlan Stern /* Implement the continuations for the delays above */
8388520f380SAlan Stern static void hub_init_func2(struct work_struct *ws)
8398520f380SAlan Stern {
8408520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
8418520f380SAlan Stern 
8428520f380SAlan Stern 	hub_activate(hub, HUB_INIT2);
8438520f380SAlan Stern }
8448520f380SAlan Stern 
8458520f380SAlan Stern static void hub_init_func3(struct work_struct *ws)
8468520f380SAlan Stern {
8478520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
8488520f380SAlan Stern 
8498520f380SAlan Stern 	hub_activate(hub, HUB_INIT3);
8508520f380SAlan Stern }
8518520f380SAlan Stern 
8524330354fSAlan Stern enum hub_quiescing_type {
8534330354fSAlan Stern 	HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
8544330354fSAlan Stern };
8554330354fSAlan Stern 
8564330354fSAlan Stern static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
8574330354fSAlan Stern {
8584330354fSAlan Stern 	struct usb_device *hdev = hub->hdev;
8594330354fSAlan Stern 	int i;
8604330354fSAlan Stern 
8618520f380SAlan Stern 	cancel_delayed_work_sync(&hub->init_work);
8628520f380SAlan Stern 
8634330354fSAlan Stern 	/* khubd and related activity won't re-trigger */
8644330354fSAlan Stern 	hub->quiescing = 1;
8654330354fSAlan Stern 
8664330354fSAlan Stern 	if (type != HUB_SUSPEND) {
8674330354fSAlan Stern 		/* Disconnect all the children */
8684330354fSAlan Stern 		for (i = 0; i < hdev->maxchild; ++i) {
8694330354fSAlan Stern 			if (hdev->children[i])
8704330354fSAlan Stern 				usb_disconnect(&hdev->children[i]);
8714330354fSAlan Stern 		}
8724330354fSAlan Stern 	}
8734330354fSAlan Stern 
8744330354fSAlan Stern 	/* Stop khubd and related activity */
8754330354fSAlan Stern 	usb_kill_urb(hub->urb);
8764330354fSAlan Stern 	if (hub->has_indicators)
8774330354fSAlan Stern 		cancel_delayed_work_sync(&hub->leds);
8784330354fSAlan Stern 	if (hub->tt.hub)
879cb88a1b8SAlan Stern 		cancel_work_sync(&hub->tt.clear_work);
8804330354fSAlan Stern }
8814330354fSAlan Stern 
8823eb14915SAlan Stern /* caller has locked the hub device */
8833eb14915SAlan Stern static int hub_pre_reset(struct usb_interface *intf)
8843eb14915SAlan Stern {
8853eb14915SAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
8863eb14915SAlan Stern 
8874330354fSAlan Stern 	hub_quiesce(hub, HUB_PRE_RESET);
888f07600cfSAlan Stern 	return 0;
8897d069b7dSAlan Stern }
8907d069b7dSAlan Stern 
8917d069b7dSAlan Stern /* caller has locked the hub device */
892f07600cfSAlan Stern static int hub_post_reset(struct usb_interface *intf)
8937d069b7dSAlan Stern {
8947de18d8bSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
8957de18d8bSAlan Stern 
896f2835219SAlan Stern 	hub_activate(hub, HUB_POST_RESET);
897f07600cfSAlan Stern 	return 0;
8987d069b7dSAlan Stern }
8997d069b7dSAlan Stern 
9001da177e4SLinus Torvalds static int hub_configure(struct usb_hub *hub,
9011da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint)
9021da177e4SLinus Torvalds {
903b356b7c7SSarah Sharp 	struct usb_hcd *hcd;
9041da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
9051da177e4SLinus Torvalds 	struct device *hub_dev = hub->intfdev;
9061da177e4SLinus Torvalds 	u16 hubstatus, hubchange;
90774ad9bd2SGreg Kroah-Hartman 	u16 wHubCharacteristics;
9081da177e4SLinus Torvalds 	unsigned int pipe;
9091da177e4SLinus Torvalds 	int maxp, ret;
9107cbe5dcaSAlan Stern 	char *message = "out of memory";
9111da177e4SLinus Torvalds 
912d697cddaSAlan Stern 	hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
9131da177e4SLinus Torvalds 	if (!hub->buffer) {
9141da177e4SLinus Torvalds 		ret = -ENOMEM;
9151da177e4SLinus Torvalds 		goto fail;
9161da177e4SLinus Torvalds 	}
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
9191da177e4SLinus Torvalds 	if (!hub->status) {
9201da177e4SLinus Torvalds 		ret = -ENOMEM;
9211da177e4SLinus Torvalds 		goto fail;
9221da177e4SLinus Torvalds 	}
923db90e7a1SAlan Stern 	mutex_init(&hub->status_mutex);
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
9261da177e4SLinus Torvalds 	if (!hub->descriptor) {
9271da177e4SLinus Torvalds 		ret = -ENOMEM;
9281da177e4SLinus Torvalds 		goto fail;
9291da177e4SLinus Torvalds 	}
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	/* Request the entire hub descriptor.
9321da177e4SLinus Torvalds 	 * hub->descriptor can handle USB_MAXCHILDREN ports,
9331da177e4SLinus Torvalds 	 * but the hub can/will return fewer bytes here.
9341da177e4SLinus Torvalds 	 */
9351da177e4SLinus Torvalds 	ret = get_hub_descriptor(hdev, hub->descriptor,
9361da177e4SLinus Torvalds 			sizeof(*hub->descriptor));
9371da177e4SLinus Torvalds 	if (ret < 0) {
9381da177e4SLinus Torvalds 		message = "can't read hub descriptor";
9391da177e4SLinus Torvalds 		goto fail;
9401da177e4SLinus Torvalds 	} else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
9411da177e4SLinus Torvalds 		message = "hub has too many ports!";
9421da177e4SLinus Torvalds 		ret = -ENODEV;
9431da177e4SLinus Torvalds 		goto fail;
9441da177e4SLinus Torvalds 	}
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	hdev->maxchild = hub->descriptor->bNbrPorts;
9471da177e4SLinus Torvalds 	dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
9481da177e4SLinus Torvalds 		(hdev->maxchild == 1) ? "" : "s");
9491da177e4SLinus Torvalds 
9507cbe5dcaSAlan Stern 	hub->port_owners = kzalloc(hdev->maxchild * sizeof(void *), GFP_KERNEL);
9517cbe5dcaSAlan Stern 	if (!hub->port_owners) {
9527cbe5dcaSAlan Stern 		ret = -ENOMEM;
9537cbe5dcaSAlan Stern 		goto fail;
9547cbe5dcaSAlan Stern 	}
9557cbe5dcaSAlan Stern 
95674ad9bd2SGreg Kroah-Hartman 	wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
9571da177e4SLinus Torvalds 
95874ad9bd2SGreg Kroah-Hartman 	if (wHubCharacteristics & HUB_CHAR_COMPOUND) {
9591da177e4SLinus Torvalds 		int	i;
9601da177e4SLinus Torvalds 		char	portstr [USB_MAXCHILDREN + 1];
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 		for (i = 0; i < hdev->maxchild; i++)
9631da177e4SLinus Torvalds 			portstr[i] = hub->descriptor->DeviceRemovable
9641da177e4SLinus Torvalds 				    [((i + 1) / 8)] & (1 << ((i + 1) % 8))
9651da177e4SLinus Torvalds 				? 'F' : 'R';
9661da177e4SLinus Torvalds 		portstr[hdev->maxchild] = 0;
9671da177e4SLinus Torvalds 		dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
9681da177e4SLinus Torvalds 	} else
9691da177e4SLinus Torvalds 		dev_dbg(hub_dev, "standalone hub\n");
9701da177e4SLinus Torvalds 
97174ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_LPSM) {
9721da177e4SLinus Torvalds 		case 0x00:
9731da177e4SLinus Torvalds 			dev_dbg(hub_dev, "ganged power switching\n");
9741da177e4SLinus Torvalds 			break;
9751da177e4SLinus Torvalds 		case 0x01:
9761da177e4SLinus Torvalds 			dev_dbg(hub_dev, "individual port power switching\n");
9771da177e4SLinus Torvalds 			break;
9781da177e4SLinus Torvalds 		case 0x02:
9791da177e4SLinus Torvalds 		case 0x03:
9801da177e4SLinus Torvalds 			dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
9811da177e4SLinus Torvalds 			break;
9821da177e4SLinus Torvalds 	}
9831da177e4SLinus Torvalds 
98474ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_OCPM) {
9851da177e4SLinus Torvalds 		case 0x00:
9861da177e4SLinus Torvalds 			dev_dbg(hub_dev, "global over-current protection\n");
9871da177e4SLinus Torvalds 			break;
9881da177e4SLinus Torvalds 		case 0x08:
9891da177e4SLinus Torvalds 			dev_dbg(hub_dev, "individual port over-current protection\n");
9901da177e4SLinus Torvalds 			break;
9911da177e4SLinus Torvalds 		case 0x10:
9921da177e4SLinus Torvalds 		case 0x18:
9931da177e4SLinus Torvalds 			dev_dbg(hub_dev, "no over-current protection\n");
9941da177e4SLinus Torvalds                         break;
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 	spin_lock_init (&hub->tt.lock);
9981da177e4SLinus Torvalds 	INIT_LIST_HEAD (&hub->tt.clear_list);
999cb88a1b8SAlan Stern 	INIT_WORK(&hub->tt.clear_work, hub_tt_work);
10001da177e4SLinus Torvalds 	switch (hdev->descriptor.bDeviceProtocol) {
10011da177e4SLinus Torvalds 		case 0:
10021da177e4SLinus Torvalds 			break;
10031da177e4SLinus Torvalds 		case 1:
10041da177e4SLinus Torvalds 			dev_dbg(hub_dev, "Single TT\n");
10051da177e4SLinus Torvalds 			hub->tt.hub = hdev;
10061da177e4SLinus Torvalds 			break;
10071da177e4SLinus Torvalds 		case 2:
10081da177e4SLinus Torvalds 			ret = usb_set_interface(hdev, 0, 1);
10091da177e4SLinus Torvalds 			if (ret == 0) {
10101da177e4SLinus Torvalds 				dev_dbg(hub_dev, "TT per port\n");
10111da177e4SLinus Torvalds 				hub->tt.multi = 1;
10121da177e4SLinus Torvalds 			} else
10131da177e4SLinus Torvalds 				dev_err(hub_dev, "Using single TT (err %d)\n",
10141da177e4SLinus Torvalds 					ret);
10151da177e4SLinus Torvalds 			hub->tt.hub = hdev;
10161da177e4SLinus Torvalds 			break;
1017d2e9b4d6SSarah Sharp 		case 3:
1018d2e9b4d6SSarah Sharp 			/* USB 3.0 hubs don't have a TT */
1019d2e9b4d6SSarah Sharp 			break;
10201da177e4SLinus Torvalds 		default:
10211da177e4SLinus Torvalds 			dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
10221da177e4SLinus Torvalds 				hdev->descriptor.bDeviceProtocol);
10231da177e4SLinus Torvalds 			break;
10241da177e4SLinus Torvalds 	}
10251da177e4SLinus Torvalds 
1026e09711aeSdavid-b@pacbell.net 	/* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
102774ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_TTTT) {
1028e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_8_BITS:
1029e09711aeSdavid-b@pacbell.net 			if (hdev->descriptor.bDeviceProtocol != 0) {
1030e09711aeSdavid-b@pacbell.net 				hub->tt.think_time = 666;
1031e09711aeSdavid-b@pacbell.net 				dev_dbg(hub_dev, "TT requires at most %d "
1032e09711aeSdavid-b@pacbell.net 						"FS bit times (%d ns)\n",
1033e09711aeSdavid-b@pacbell.net 					8, hub->tt.think_time);
1034e09711aeSdavid-b@pacbell.net 			}
10351da177e4SLinus Torvalds 			break;
1036e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_16_BITS:
1037e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 2;
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 				16, hub->tt.think_time);
10411da177e4SLinus Torvalds 			break;
1042e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_24_BITS:
1043e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 3;
1044e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1045e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1046e09711aeSdavid-b@pacbell.net 				24, hub->tt.think_time);
10471da177e4SLinus Torvalds 			break;
1048e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_32_BITS:
1049e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 4;
1050e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1051e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1052e09711aeSdavid-b@pacbell.net 				32, hub->tt.think_time);
10531da177e4SLinus Torvalds 			break;
10541da177e4SLinus Torvalds 	}
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	/* probe() zeroes hub->indicator[] */
105774ad9bd2SGreg Kroah-Hartman 	if (wHubCharacteristics & HUB_CHAR_PORTIND) {
10581da177e4SLinus Torvalds 		hub->has_indicators = 1;
10591da177e4SLinus Torvalds 		dev_dbg(hub_dev, "Port indicators are supported\n");
10601da177e4SLinus Torvalds 	}
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	dev_dbg(hub_dev, "power on to power good time: %dms\n",
10631da177e4SLinus Torvalds 		hub->descriptor->bPwrOn2PwrGood * 2);
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	/* power budgeting mostly matters with bus-powered hubs,
10661da177e4SLinus Torvalds 	 * and battery-powered root hubs (may provide just 8 mA).
10671da177e4SLinus Torvalds 	 */
10681da177e4SLinus Torvalds 	ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
106955c52718SAlan Stern 	if (ret < 2) {
10701da177e4SLinus Torvalds 		message = "can't get hub status";
10711da177e4SLinus Torvalds 		goto fail;
10721da177e4SLinus Torvalds 	}
10737d35b929SAlan Stern 	le16_to_cpus(&hubstatus);
10747d35b929SAlan Stern 	if (hdev == hdev->bus->root_hub) {
107555c52718SAlan Stern 		if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
107655c52718SAlan Stern 			hub->mA_per_port = 500;
107755c52718SAlan Stern 		else {
107855c52718SAlan Stern 			hub->mA_per_port = hdev->bus_mA;
107955c52718SAlan Stern 			hub->limited_power = 1;
108055c52718SAlan Stern 		}
10817d35b929SAlan Stern 	} else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
10821da177e4SLinus Torvalds 		dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
10831da177e4SLinus Torvalds 			hub->descriptor->bHubContrCurrent);
108455c52718SAlan Stern 		hub->limited_power = 1;
108555c52718SAlan Stern 		if (hdev->maxchild > 0) {
108655c52718SAlan Stern 			int remaining = hdev->bus_mA -
108755c52718SAlan Stern 					hub->descriptor->bHubContrCurrent;
10881da177e4SLinus Torvalds 
108955c52718SAlan Stern 			if (remaining < hdev->maxchild * 100)
109055c52718SAlan Stern 				dev_warn(hub_dev,
109155c52718SAlan Stern 					"insufficient power available "
109255c52718SAlan Stern 					"to use all downstream ports\n");
109355c52718SAlan Stern 			hub->mA_per_port = 100;		/* 7.2.1.1 */
109455c52718SAlan Stern 		}
109555c52718SAlan Stern 	} else {	/* Self-powered external hub */
109655c52718SAlan Stern 		/* FIXME: What about battery-powered external hubs that
109755c52718SAlan Stern 		 * provide less current per port? */
109855c52718SAlan Stern 		hub->mA_per_port = 500;
109955c52718SAlan Stern 	}
110055c52718SAlan Stern 	if (hub->mA_per_port < 500)
110155c52718SAlan Stern 		dev_dbg(hub_dev, "%umA bus power budget for each child\n",
110255c52718SAlan Stern 				hub->mA_per_port);
11031da177e4SLinus Torvalds 
1104b356b7c7SSarah Sharp 	/* Update the HCD's internal representation of this hub before khubd
1105b356b7c7SSarah Sharp 	 * starts getting port status changes for devices under the hub.
1106b356b7c7SSarah Sharp 	 */
1107b356b7c7SSarah Sharp 	hcd = bus_to_hcd(hdev->bus);
1108b356b7c7SSarah Sharp 	if (hcd->driver->update_hub_device) {
1109b356b7c7SSarah Sharp 		ret = hcd->driver->update_hub_device(hcd, hdev,
1110b356b7c7SSarah Sharp 				&hub->tt, GFP_KERNEL);
1111b356b7c7SSarah Sharp 		if (ret < 0) {
1112b356b7c7SSarah Sharp 			message = "can't update HCD hub info";
1113b356b7c7SSarah Sharp 			goto fail;
1114b356b7c7SSarah Sharp 		}
1115b356b7c7SSarah Sharp 	}
1116b356b7c7SSarah Sharp 
11171da177e4SLinus Torvalds 	ret = hub_hub_status(hub, &hubstatus, &hubchange);
11181da177e4SLinus Torvalds 	if (ret < 0) {
11191da177e4SLinus Torvalds 		message = "can't get hub status";
11201da177e4SLinus Torvalds 		goto fail;
11211da177e4SLinus Torvalds 	}
11221da177e4SLinus Torvalds 
11231da177e4SLinus Torvalds 	/* local power status reports aren't always correct */
11241da177e4SLinus Torvalds 	if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
11251da177e4SLinus Torvalds 		dev_dbg(hub_dev, "local power source is %s\n",
11261da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_LOCAL_POWER)
11271da177e4SLinus Torvalds 			? "lost (inactive)" : "good");
11281da177e4SLinus Torvalds 
112974ad9bd2SGreg Kroah-Hartman 	if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
11301da177e4SLinus Torvalds 		dev_dbg(hub_dev, "%sover-current condition exists\n",
11311da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
11321da177e4SLinus Torvalds 
113388fafff9Sinaky@linux.intel.com 	/* set up the interrupt endpoint
113488fafff9Sinaky@linux.intel.com 	 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
113588fafff9Sinaky@linux.intel.com 	 * bytes as USB2.0[11.12.3] says because some hubs are known
113688fafff9Sinaky@linux.intel.com 	 * to send more data (and thus cause overflow). For root hubs,
113788fafff9Sinaky@linux.intel.com 	 * maxpktsize is defined in hcd.c's fake endpoint descriptors
113888fafff9Sinaky@linux.intel.com 	 * to be big enough for at least USB_MAXCHILDREN ports. */
11391da177e4SLinus Torvalds 	pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
11401da177e4SLinus Torvalds 	maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	if (maxp > sizeof(*hub->buffer))
11431da177e4SLinus Torvalds 		maxp = sizeof(*hub->buffer);
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	hub->urb = usb_alloc_urb(0, GFP_KERNEL);
11461da177e4SLinus Torvalds 	if (!hub->urb) {
11471da177e4SLinus Torvalds 		ret = -ENOMEM;
11481da177e4SLinus Torvalds 		goto fail;
11491da177e4SLinus Torvalds 	}
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 	usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
11521da177e4SLinus Torvalds 		hub, endpoint->bInterval);
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	/* maybe cycle the hub leds */
11551da177e4SLinus Torvalds 	if (hub->has_indicators && blinkenlights)
11561da177e4SLinus Torvalds 		hub->indicator [0] = INDICATOR_CYCLE;
11571da177e4SLinus Torvalds 
1158f2835219SAlan Stern 	hub_activate(hub, HUB_INIT);
11591da177e4SLinus Torvalds 	return 0;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds fail:
11621da177e4SLinus Torvalds 	dev_err (hub_dev, "config failed, %s (err %d)\n",
11631da177e4SLinus Torvalds 			message, ret);
11641da177e4SLinus Torvalds 	/* hub_disconnect() frees urb and descriptor */
11651da177e4SLinus Torvalds 	return ret;
11661da177e4SLinus Torvalds }
11671da177e4SLinus Torvalds 
1168e8054854SAlan Stern static void hub_release(struct kref *kref)
1169e8054854SAlan Stern {
1170e8054854SAlan Stern 	struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1171e8054854SAlan Stern 
1172e8054854SAlan Stern 	usb_put_intf(to_usb_interface(hub->intfdev));
1173e8054854SAlan Stern 	kfree(hub);
1174e8054854SAlan Stern }
1175e8054854SAlan Stern 
11761da177e4SLinus Torvalds static unsigned highspeed_hubs;
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds static void hub_disconnect(struct usb_interface *intf)
11791da177e4SLinus Torvalds {
11801da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata (intf);
1181e8054854SAlan Stern 
1182e8054854SAlan Stern 	/* Take the hub off the event list and don't let it be added again */
1183e8054854SAlan Stern 	spin_lock_irq(&hub_event_lock);
11848e4ceb38SAlan Stern 	if (!list_empty(&hub->event_list)) {
1185e8054854SAlan Stern 		list_del_init(&hub->event_list);
11868e4ceb38SAlan Stern 		usb_autopm_put_interface_no_suspend(intf);
11878e4ceb38SAlan Stern 	}
1188e8054854SAlan Stern 	hub->disconnected = 1;
1189e8054854SAlan Stern 	spin_unlock_irq(&hub_event_lock);
11901da177e4SLinus Torvalds 
11917de18d8bSAlan Stern 	/* Disconnect all children and quiesce the hub */
11927de18d8bSAlan Stern 	hub->error = 0;
11934330354fSAlan Stern 	hub_quiesce(hub, HUB_DISCONNECT);
11947de18d8bSAlan Stern 
11958b28c752SAlan Stern 	usb_set_intfdata (intf, NULL);
11967cbe5dcaSAlan Stern 	hub->hdev->maxchild = 0;
11971da177e4SLinus Torvalds 
1198e8054854SAlan Stern 	if (hub->hdev->speed == USB_SPEED_HIGH)
11991da177e4SLinus Torvalds 		highspeed_hubs--;
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 	usb_free_urb(hub->urb);
12027cbe5dcaSAlan Stern 	kfree(hub->port_owners);
12031da177e4SLinus Torvalds 	kfree(hub->descriptor);
12041da177e4SLinus Torvalds 	kfree(hub->status);
1205d697cddaSAlan Stern 	kfree(hub->buffer);
12061da177e4SLinus Torvalds 
1207e8054854SAlan Stern 	kref_put(&hub->kref, hub_release);
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
12111da177e4SLinus Torvalds {
12121da177e4SLinus Torvalds 	struct usb_host_interface *desc;
12131da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint;
12141da177e4SLinus Torvalds 	struct usb_device *hdev;
12151da177e4SLinus Torvalds 	struct usb_hub *hub;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	desc = intf->cur_altsetting;
12181da177e4SLinus Torvalds 	hdev = interface_to_usbdev(intf);
12191da177e4SLinus Torvalds 
1220088f7fecSAlan Stern 	/* Hubs have proper suspend/resume support */
1221088f7fecSAlan Stern 	usb_enable_autosuspend(hdev);
1222088f7fecSAlan Stern 
122338f3ad5eSFelipe Balbi 	if (hdev->level == MAX_TOPO_LEVEL) {
1224b9cef6c3SWu Fengguang 		dev_err(&intf->dev,
1225b9cef6c3SWu Fengguang 			"Unsupported bus topology: hub nested too deep\n");
122638f3ad5eSFelipe Balbi 		return -E2BIG;
122738f3ad5eSFelipe Balbi 	}
122838f3ad5eSFelipe Balbi 
122989ccbdc9SDavid Brownell #ifdef	CONFIG_USB_OTG_BLACKLIST_HUB
123089ccbdc9SDavid Brownell 	if (hdev->parent) {
123189ccbdc9SDavid Brownell 		dev_warn(&intf->dev, "ignoring external hub\n");
123289ccbdc9SDavid Brownell 		return -ENODEV;
123389ccbdc9SDavid Brownell 	}
123489ccbdc9SDavid Brownell #endif
123589ccbdc9SDavid Brownell 
12361da177e4SLinus Torvalds 	/* Some hubs have a subclass of 1, which AFAICT according to the */
12371da177e4SLinus Torvalds 	/*  specs is not defined, but it works */
12381da177e4SLinus Torvalds 	if ((desc->desc.bInterfaceSubClass != 0) &&
12391da177e4SLinus Torvalds 	    (desc->desc.bInterfaceSubClass != 1)) {
12401da177e4SLinus Torvalds descriptor_error:
12411da177e4SLinus Torvalds 		dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
12421da177e4SLinus Torvalds 		return -EIO;
12431da177e4SLinus Torvalds 	}
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	/* Multiple endpoints? What kind of mutant ninja-hub is this? */
12461da177e4SLinus Torvalds 	if (desc->desc.bNumEndpoints != 1)
12471da177e4SLinus Torvalds 		goto descriptor_error;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	endpoint = &desc->endpoint[0].desc;
12501da177e4SLinus Torvalds 
1251fbf81c29SLuiz Fernando N. Capitulino 	/* If it's not an interrupt in endpoint, we'd better punt! */
1252fbf81c29SLuiz Fernando N. Capitulino 	if (!usb_endpoint_is_int_in(endpoint))
12531da177e4SLinus Torvalds 		goto descriptor_error;
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 	/* We found a hub */
12561da177e4SLinus Torvalds 	dev_info (&intf->dev, "USB hub found\n");
12571da177e4SLinus Torvalds 
12580a1ef3b5SAlan Stern 	hub = kzalloc(sizeof(*hub), GFP_KERNEL);
12591da177e4SLinus Torvalds 	if (!hub) {
12601da177e4SLinus Torvalds 		dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
12611da177e4SLinus Torvalds 		return -ENOMEM;
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds 
1264e8054854SAlan Stern 	kref_init(&hub->kref);
12651da177e4SLinus Torvalds 	INIT_LIST_HEAD(&hub->event_list);
12661da177e4SLinus Torvalds 	hub->intfdev = &intf->dev;
12671da177e4SLinus Torvalds 	hub->hdev = hdev;
1268c4028958SDavid Howells 	INIT_DELAYED_WORK(&hub->leds, led_work);
12698520f380SAlan Stern 	INIT_DELAYED_WORK(&hub->init_work, NULL);
1270e8054854SAlan Stern 	usb_get_intf(intf);
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	usb_set_intfdata (intf, hub);
127340f122f3SAlan Stern 	intf->needs_remote_wakeup = 1;
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	if (hdev->speed == USB_SPEED_HIGH)
12761da177e4SLinus Torvalds 		highspeed_hubs++;
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	if (hub_configure(hub, endpoint) >= 0)
12791da177e4SLinus Torvalds 		return 0;
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 	hub_disconnect (intf);
12821da177e4SLinus Torvalds 	return -ENODEV;
12831da177e4SLinus Torvalds }
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds static int
12861da177e4SLinus Torvalds hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
12871da177e4SLinus Torvalds {
12881da177e4SLinus Torvalds 	struct usb_device *hdev = interface_to_usbdev (intf);
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	/* assert ifno == 0 (part of hub spec) */
12911da177e4SLinus Torvalds 	switch (code) {
12921da177e4SLinus Torvalds 	case USBDEVFS_HUB_PORTINFO: {
12931da177e4SLinus Torvalds 		struct usbdevfs_hub_portinfo *info = user_data;
12941da177e4SLinus Torvalds 		int i;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
12971da177e4SLinus Torvalds 		if (hdev->devnum <= 0)
12981da177e4SLinus Torvalds 			info->nports = 0;
12991da177e4SLinus Torvalds 		else {
13001da177e4SLinus Torvalds 			info->nports = hdev->maxchild;
13011da177e4SLinus Torvalds 			for (i = 0; i < info->nports; i++) {
13021da177e4SLinus Torvalds 				if (hdev->children[i] == NULL)
13031da177e4SLinus Torvalds 					info->port[i] = 0;
13041da177e4SLinus Torvalds 				else
13051da177e4SLinus Torvalds 					info->port[i] =
13061da177e4SLinus Torvalds 						hdev->children[i]->devnum;
13071da177e4SLinus Torvalds 			}
13081da177e4SLinus Torvalds 		}
13091da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 		return info->nports + 1;
13121da177e4SLinus Torvalds 		}
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	default:
13151da177e4SLinus Torvalds 		return -ENOSYS;
13161da177e4SLinus Torvalds 	}
13171da177e4SLinus Torvalds }
13181da177e4SLinus Torvalds 
13197cbe5dcaSAlan Stern /*
13207cbe5dcaSAlan Stern  * Allow user programs to claim ports on a hub.  When a device is attached
13217cbe5dcaSAlan Stern  * to one of these "claimed" ports, the program will "own" the device.
13227cbe5dcaSAlan Stern  */
13237cbe5dcaSAlan Stern static int find_port_owner(struct usb_device *hdev, unsigned port1,
13247cbe5dcaSAlan Stern 		void ***ppowner)
13257cbe5dcaSAlan Stern {
13267cbe5dcaSAlan Stern 	if (hdev->state == USB_STATE_NOTATTACHED)
13277cbe5dcaSAlan Stern 		return -ENODEV;
13287cbe5dcaSAlan Stern 	if (port1 == 0 || port1 > hdev->maxchild)
13297cbe5dcaSAlan Stern 		return -EINVAL;
13307cbe5dcaSAlan Stern 
13317cbe5dcaSAlan Stern 	/* This assumes that devices not managed by the hub driver
13327cbe5dcaSAlan Stern 	 * will always have maxchild equal to 0.
13337cbe5dcaSAlan Stern 	 */
13347cbe5dcaSAlan Stern 	*ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]);
13357cbe5dcaSAlan Stern 	return 0;
13367cbe5dcaSAlan Stern }
13377cbe5dcaSAlan Stern 
13387cbe5dcaSAlan Stern /* In the following three functions, the caller must hold hdev's lock */
13397cbe5dcaSAlan Stern int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, void *owner)
13407cbe5dcaSAlan Stern {
13417cbe5dcaSAlan Stern 	int rc;
13427cbe5dcaSAlan Stern 	void **powner;
13437cbe5dcaSAlan Stern 
13447cbe5dcaSAlan Stern 	rc = find_port_owner(hdev, port1, &powner);
13457cbe5dcaSAlan Stern 	if (rc)
13467cbe5dcaSAlan Stern 		return rc;
13477cbe5dcaSAlan Stern 	if (*powner)
13487cbe5dcaSAlan Stern 		return -EBUSY;
13497cbe5dcaSAlan Stern 	*powner = owner;
13507cbe5dcaSAlan Stern 	return rc;
13517cbe5dcaSAlan Stern }
13527cbe5dcaSAlan Stern 
13537cbe5dcaSAlan Stern int usb_hub_release_port(struct usb_device *hdev, unsigned port1, void *owner)
13547cbe5dcaSAlan Stern {
13557cbe5dcaSAlan Stern 	int rc;
13567cbe5dcaSAlan Stern 	void **powner;
13577cbe5dcaSAlan Stern 
13587cbe5dcaSAlan Stern 	rc = find_port_owner(hdev, port1, &powner);
13597cbe5dcaSAlan Stern 	if (rc)
13607cbe5dcaSAlan Stern 		return rc;
13617cbe5dcaSAlan Stern 	if (*powner != owner)
13627cbe5dcaSAlan Stern 		return -ENOENT;
13637cbe5dcaSAlan Stern 	*powner = NULL;
13647cbe5dcaSAlan Stern 	return rc;
13657cbe5dcaSAlan Stern }
13667cbe5dcaSAlan Stern 
13677cbe5dcaSAlan Stern void usb_hub_release_all_ports(struct usb_device *hdev, void *owner)
13687cbe5dcaSAlan Stern {
13697cbe5dcaSAlan Stern 	int n;
13707cbe5dcaSAlan Stern 	void **powner;
13717cbe5dcaSAlan Stern 
13727cbe5dcaSAlan Stern 	n = find_port_owner(hdev, 1, &powner);
13737cbe5dcaSAlan Stern 	if (n == 0) {
13747cbe5dcaSAlan Stern 		for (; n < hdev->maxchild; (++n, ++powner)) {
13757cbe5dcaSAlan Stern 			if (*powner == owner)
13767cbe5dcaSAlan Stern 				*powner = NULL;
13777cbe5dcaSAlan Stern 		}
13787cbe5dcaSAlan Stern 	}
13797cbe5dcaSAlan Stern }
13807cbe5dcaSAlan Stern 
13817cbe5dcaSAlan Stern /* The caller must hold udev's lock */
13827cbe5dcaSAlan Stern bool usb_device_is_owned(struct usb_device *udev)
13837cbe5dcaSAlan Stern {
13847cbe5dcaSAlan Stern 	struct usb_hub *hub;
13857cbe5dcaSAlan Stern 
13867cbe5dcaSAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
13877cbe5dcaSAlan Stern 		return false;
13887cbe5dcaSAlan Stern 	hub = hdev_to_hub(udev->parent);
13897cbe5dcaSAlan Stern 	return !!hub->port_owners[udev->portnum - 1];
13907cbe5dcaSAlan Stern }
13917cbe5dcaSAlan Stern 
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds static void recursively_mark_NOTATTACHED(struct usb_device *udev)
13941da177e4SLinus Torvalds {
13951da177e4SLinus Torvalds 	int i;
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds 	for (i = 0; i < udev->maxchild; ++i) {
13981da177e4SLinus Torvalds 		if (udev->children[i])
13991da177e4SLinus Torvalds 			recursively_mark_NOTATTACHED(udev->children[i]);
14001da177e4SLinus Torvalds 	}
14019bbdf1e0SAlan Stern 	if (udev->state == USB_STATE_SUSPENDED)
140215123006SSarah Sharp 		udev->active_duration -= jiffies;
14031da177e4SLinus Torvalds 	udev->state = USB_STATE_NOTATTACHED;
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds /**
14071da177e4SLinus Torvalds  * usb_set_device_state - change a device's current state (usbcore, hcds)
14081da177e4SLinus Torvalds  * @udev: pointer to device whose state should be changed
14091da177e4SLinus Torvalds  * @new_state: new state value to be stored
14101da177e4SLinus Torvalds  *
14111da177e4SLinus Torvalds  * udev->state is _not_ fully protected by the device lock.  Although
14121da177e4SLinus Torvalds  * most transitions are made only while holding the lock, the state can
14131da177e4SLinus Torvalds  * can change to USB_STATE_NOTATTACHED at almost any time.  This
14141da177e4SLinus Torvalds  * is so that devices can be marked as disconnected as soon as possible,
14151da177e4SLinus Torvalds  * without having to wait for any semaphores to be released.  As a result,
14161da177e4SLinus Torvalds  * all changes to any device's state must be protected by the
14171da177e4SLinus Torvalds  * device_state_lock spinlock.
14181da177e4SLinus Torvalds  *
14191da177e4SLinus Torvalds  * Once a device has been added to the device tree, all changes to its state
14201da177e4SLinus Torvalds  * should be made using this routine.  The state should _not_ be set directly.
14211da177e4SLinus Torvalds  *
14221da177e4SLinus Torvalds  * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
14231da177e4SLinus Torvalds  * Otherwise udev->state is set to new_state, and if new_state is
14241da177e4SLinus Torvalds  * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
14251da177e4SLinus Torvalds  * to USB_STATE_NOTATTACHED.
14261da177e4SLinus Torvalds  */
14271da177e4SLinus Torvalds void usb_set_device_state(struct usb_device *udev,
14281da177e4SLinus Torvalds 		enum usb_device_state new_state)
14291da177e4SLinus Torvalds {
14301da177e4SLinus Torvalds 	unsigned long flags;
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 	spin_lock_irqsave(&device_state_lock, flags);
14331da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED)
14341da177e4SLinus Torvalds 		;	/* do nothing */
1435b94dc6b5SDavid Brownell 	else if (new_state != USB_STATE_NOTATTACHED) {
1436fb669cc0SDavid Brownell 
1437fb669cc0SDavid Brownell 		/* root hub wakeup capabilities are managed out-of-band
1438fb669cc0SDavid Brownell 		 * and may involve silicon errata ... ignore them here.
1439fb669cc0SDavid Brownell 		 */
1440fb669cc0SDavid Brownell 		if (udev->parent) {
1441645daaabSAlan Stern 			if (udev->state == USB_STATE_SUSPENDED
1442645daaabSAlan Stern 					|| new_state == USB_STATE_SUSPENDED)
1443645daaabSAlan Stern 				;	/* No change to wakeup settings */
1444645daaabSAlan Stern 			else if (new_state == USB_STATE_CONFIGURED)
144516985408SDan Streetman 				device_set_wakeup_capable(&udev->dev,
1446b94dc6b5SDavid Brownell 					(udev->actconfig->desc.bmAttributes
1447b94dc6b5SDavid Brownell 					 & USB_CONFIG_ATT_WAKEUP));
1448645daaabSAlan Stern 			else
144916985408SDan Streetman 				device_set_wakeup_capable(&udev->dev, 0);
1450fb669cc0SDavid Brownell 		}
145115123006SSarah Sharp 		if (udev->state == USB_STATE_SUSPENDED &&
145215123006SSarah Sharp 			new_state != USB_STATE_SUSPENDED)
145315123006SSarah Sharp 			udev->active_duration -= jiffies;
145415123006SSarah Sharp 		else if (new_state == USB_STATE_SUSPENDED &&
145515123006SSarah Sharp 				udev->state != USB_STATE_SUSPENDED)
145615123006SSarah Sharp 			udev->active_duration += jiffies;
1457645daaabSAlan Stern 		udev->state = new_state;
1458b94dc6b5SDavid Brownell 	} else
14591da177e4SLinus Torvalds 		recursively_mark_NOTATTACHED(udev);
14601da177e4SLinus Torvalds 	spin_unlock_irqrestore(&device_state_lock, flags);
14611da177e4SLinus Torvalds }
14626da9c990SDavid Vrabel EXPORT_SYMBOL_GPL(usb_set_device_state);
14631da177e4SLinus Torvalds 
14648af548dcSInaky Perez-Gonzalez /*
14658af548dcSInaky Perez-Gonzalez  * WUSB devices are simple: they have no hubs behind, so the mapping
14668af548dcSInaky Perez-Gonzalez  * device <-> virtual port number becomes 1:1. Why? to simplify the
14678af548dcSInaky Perez-Gonzalez  * life of the device connection logic in
14688af548dcSInaky Perez-Gonzalez  * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
14698af548dcSInaky Perez-Gonzalez  * handshake we need to assign a temporary address in the unauthorized
14708af548dcSInaky Perez-Gonzalez  * space. For simplicity we use the first virtual port number found to
14718af548dcSInaky Perez-Gonzalez  * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
14728af548dcSInaky Perez-Gonzalez  * and that becomes it's address [X < 128] or its unauthorized address
14738af548dcSInaky Perez-Gonzalez  * [X | 0x80].
14748af548dcSInaky Perez-Gonzalez  *
14758af548dcSInaky Perez-Gonzalez  * We add 1 as an offset to the one-based USB-stack port number
14768af548dcSInaky Perez-Gonzalez  * (zero-based wusb virtual port index) for two reasons: (a) dev addr
14778af548dcSInaky Perez-Gonzalez  * 0 is reserved by USB for default address; (b) Linux's USB stack
14788af548dcSInaky Perez-Gonzalez  * uses always #1 for the root hub of the controller. So USB stack's
14798af548dcSInaky Perez-Gonzalez  * port #1, which is wusb virtual-port #0 has address #2.
1480c6515272SSarah Sharp  *
1481c6515272SSarah Sharp  * Devices connected under xHCI are not as simple.  The host controller
1482c6515272SSarah Sharp  * supports virtualization, so the hardware assigns device addresses and
1483c6515272SSarah Sharp  * the HCD must setup data structures before issuing a set address
1484c6515272SSarah Sharp  * command to the hardware.
14858af548dcSInaky Perez-Gonzalez  */
14861da177e4SLinus Torvalds static void choose_address(struct usb_device *udev)
14871da177e4SLinus Torvalds {
14881da177e4SLinus Torvalds 	int		devnum;
14891da177e4SLinus Torvalds 	struct usb_bus	*bus = udev->bus;
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	/* If khubd ever becomes multithreaded, this will need a lock */
14928af548dcSInaky Perez-Gonzalez 	if (udev->wusb) {
14938af548dcSInaky Perez-Gonzalez 		devnum = udev->portnum + 1;
14948af548dcSInaky Perez-Gonzalez 		BUG_ON(test_bit(devnum, bus->devmap.devicemap));
14958af548dcSInaky Perez-Gonzalez 	} else {
14968af548dcSInaky Perez-Gonzalez 		/* Try to allocate the next devnum beginning at
14978af548dcSInaky Perez-Gonzalez 		 * bus->devnum_next. */
14981da177e4SLinus Torvalds 		devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
14991da177e4SLinus Torvalds 					    bus->devnum_next);
15001da177e4SLinus Torvalds 		if (devnum >= 128)
15018af548dcSInaky Perez-Gonzalez 			devnum = find_next_zero_bit(bus->devmap.devicemap,
15028af548dcSInaky Perez-Gonzalez 						    128, 1);
15031da177e4SLinus Torvalds 		bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
15048af548dcSInaky Perez-Gonzalez 	}
15051da177e4SLinus Torvalds 	if (devnum < 128) {
15061da177e4SLinus Torvalds 		set_bit(devnum, bus->devmap.devicemap);
15071da177e4SLinus Torvalds 		udev->devnum = devnum;
15081da177e4SLinus Torvalds 	}
15091da177e4SLinus Torvalds }
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds static void release_address(struct usb_device *udev)
15121da177e4SLinus Torvalds {
15131da177e4SLinus Torvalds 	if (udev->devnum > 0) {
15141da177e4SLinus Torvalds 		clear_bit(udev->devnum, udev->bus->devmap.devicemap);
15151da177e4SLinus Torvalds 		udev->devnum = -1;
15161da177e4SLinus Torvalds 	}
15171da177e4SLinus Torvalds }
15181da177e4SLinus Torvalds 
15194953d141SDavid Vrabel static void update_address(struct usb_device *udev, int devnum)
15204953d141SDavid Vrabel {
15214953d141SDavid Vrabel 	/* The address for a WUSB device is managed by wusbcore. */
15224953d141SDavid Vrabel 	if (!udev->wusb)
15234953d141SDavid Vrabel 		udev->devnum = devnum;
15244953d141SDavid Vrabel }
15254953d141SDavid Vrabel 
1526f7410cedSHerbert Xu static void hub_free_dev(struct usb_device *udev)
1527f7410cedSHerbert Xu {
1528f7410cedSHerbert Xu 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1529f7410cedSHerbert Xu 
1530f7410cedSHerbert Xu 	/* Root hubs aren't real devices, so don't free HCD resources */
1531f7410cedSHerbert Xu 	if (hcd->driver->free_dev && udev->parent)
1532f7410cedSHerbert Xu 		hcd->driver->free_dev(hcd, udev);
1533f7410cedSHerbert Xu }
1534f7410cedSHerbert Xu 
15351da177e4SLinus Torvalds /**
15361da177e4SLinus Torvalds  * usb_disconnect - disconnect a device (usbcore-internal)
15371da177e4SLinus Torvalds  * @pdev: pointer to device being disconnected
15381da177e4SLinus Torvalds  * Context: !in_interrupt ()
15391da177e4SLinus Torvalds  *
15401da177e4SLinus Torvalds  * Something got disconnected. Get rid of it and all of its children.
15411da177e4SLinus Torvalds  *
15421da177e4SLinus Torvalds  * If *pdev is a normal device then the parent hub must already be locked.
15431da177e4SLinus Torvalds  * If *pdev is a root hub then this routine will acquire the
15441da177e4SLinus Torvalds  * usb_bus_list_lock on behalf of the caller.
15451da177e4SLinus Torvalds  *
15461da177e4SLinus Torvalds  * Only hub drivers (including virtual root hub drivers for host
15471da177e4SLinus Torvalds  * controllers) should ever call this.
15481da177e4SLinus Torvalds  *
15491da177e4SLinus Torvalds  * This call is synchronous, and may not be used in an interrupt context.
15501da177e4SLinus Torvalds  */
15511da177e4SLinus Torvalds void usb_disconnect(struct usb_device **pdev)
15521da177e4SLinus Torvalds {
15531da177e4SLinus Torvalds 	struct usb_device	*udev = *pdev;
15541da177e4SLinus Torvalds 	int			i;
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 	if (!udev) {
1557441b62c1SHarvey Harrison 		pr_debug ("%s nodev\n", __func__);
15581da177e4SLinus Torvalds 		return;
15591da177e4SLinus Torvalds 	}
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	/* mark the device as inactive, so any further urb submissions for
15621da177e4SLinus Torvalds 	 * this device (and any of its children) will fail immediately.
15631da177e4SLinus Torvalds 	 * this quiesces everyting except pending urbs.
15641da177e4SLinus Torvalds 	 */
15651da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
15661da177e4SLinus Torvalds 	dev_info (&udev->dev, "USB disconnect, address %d\n", udev->devnum);
15671da177e4SLinus Torvalds 
15689ad3d6ccSAlan Stern 	usb_lock_device(udev);
15699ad3d6ccSAlan Stern 
15701da177e4SLinus Torvalds 	/* Free up all the children before we remove this device */
15711da177e4SLinus Torvalds 	for (i = 0; i < USB_MAXCHILDREN; i++) {
15721da177e4SLinus Torvalds 		if (udev->children[i])
15731da177e4SLinus Torvalds 			usb_disconnect(&udev->children[i]);
15741da177e4SLinus Torvalds 	}
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	/* deallocate hcd/hardware state ... nuking all pending urbs and
15771da177e4SLinus Torvalds 	 * cleaning up all state associated with the current configuration
15781da177e4SLinus Torvalds 	 * so that the hardware is now fully quiesced.
15791da177e4SLinus Torvalds 	 */
1580782da727SAlan Stern 	dev_dbg (&udev->dev, "unregistering device\n");
15811da177e4SLinus Torvalds 	usb_disable_device(udev, 0);
1582cde217a5SAlan Stern 	usb_hcd_synchronize_unlinks(udev);
15831da177e4SLinus Torvalds 
15843b23dd6fSAlan Stern 	usb_remove_ep_devs(&udev->ep0);
1585782da727SAlan Stern 	usb_unlock_device(udev);
15863099e75aSGreg Kroah-Hartman 
1587782da727SAlan Stern 	/* Unregister the device.  The device driver is responsible
15883b23dd6fSAlan Stern 	 * for de-configuring the device and invoking the remove-device
15893b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
1590782da727SAlan Stern 	 */
1591782da727SAlan Stern 	device_del(&udev->dev);
1592782da727SAlan Stern 
1593782da727SAlan Stern 	/* Free the device number and delete the parent's children[]
15941da177e4SLinus Torvalds 	 * (or root_hub) pointer.
15951da177e4SLinus Torvalds 	 */
15961da177e4SLinus Torvalds 	release_address(udev);
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	/* Avoid races with recursively_mark_NOTATTACHED() */
15991da177e4SLinus Torvalds 	spin_lock_irq(&device_state_lock);
16001da177e4SLinus Torvalds 	*pdev = NULL;
16011da177e4SLinus Torvalds 	spin_unlock_irq(&device_state_lock);
16021da177e4SLinus Torvalds 
1603f7410cedSHerbert Xu 	hub_free_dev(udev);
1604f7410cedSHerbert Xu 
1605782da727SAlan Stern 	put_device(&udev->dev);
16061da177e4SLinus Torvalds }
16071da177e4SLinus Torvalds 
1608f2a383e4SGreg Kroah-Hartman #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
16091da177e4SLinus Torvalds static void show_string(struct usb_device *udev, char *id, char *string)
16101da177e4SLinus Torvalds {
16111da177e4SLinus Torvalds 	if (!string)
16121da177e4SLinus Torvalds 		return;
16131da177e4SLinus Torvalds 	dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
16141da177e4SLinus Torvalds }
16151da177e4SLinus Torvalds 
1616f2a383e4SGreg Kroah-Hartman static void announce_device(struct usb_device *udev)
1617f2a383e4SGreg Kroah-Hartman {
1618f2a383e4SGreg Kroah-Hartman 	dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
1619f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idVendor),
1620f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idProduct));
1621b9cef6c3SWu Fengguang 	dev_info(&udev->dev,
1622b9cef6c3SWu Fengguang 		"New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
1623f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iManufacturer,
1624f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iProduct,
1625f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iSerialNumber);
1626f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Product", udev->product);
1627f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Manufacturer", udev->manufacturer);
1628f2a383e4SGreg Kroah-Hartman 	show_string(udev, "SerialNumber", udev->serial);
1629f2a383e4SGreg Kroah-Hartman }
16301da177e4SLinus Torvalds #else
1631f2a383e4SGreg Kroah-Hartman static inline void announce_device(struct usb_device *udev) { }
16321da177e4SLinus Torvalds #endif
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
16351da177e4SLinus Torvalds #include "otg_whitelist.h"
16361da177e4SLinus Torvalds #endif
16371da177e4SLinus Torvalds 
16383ede760fSOliver Neukum /**
16398d8558d1SAlan Stern  * usb_enumerate_device_otg - FIXME (usbcore-internal)
16403ede760fSOliver Neukum  * @udev: newly addressed device (in ADDRESS state)
16413ede760fSOliver Neukum  *
16428d8558d1SAlan Stern  * Finish enumeration for On-The-Go devices
16433ede760fSOliver Neukum  */
16448d8558d1SAlan Stern static int usb_enumerate_device_otg(struct usb_device *udev)
16451da177e4SLinus Torvalds {
1646d9d16e8aSInaky Perez-Gonzalez 	int err = 0;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
16491da177e4SLinus Torvalds 	/*
16501da177e4SLinus Torvalds 	 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
16511da177e4SLinus Torvalds 	 * to wake us after we've powered off VBUS; and HNP, switching roles
16521da177e4SLinus Torvalds 	 * "host" to "peripheral".  The OTG descriptor helps figure this out.
16531da177e4SLinus Torvalds 	 */
16541da177e4SLinus Torvalds 	if (!udev->bus->is_b_host
16551da177e4SLinus Torvalds 			&& udev->config
16561da177e4SLinus Torvalds 			&& udev->parent == udev->bus->root_hub) {
16572eb5052eSFelipe Balbi 		struct usb_otg_descriptor	*desc = NULL;
16581da177e4SLinus Torvalds 		struct usb_bus			*bus = udev->bus;
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 		/* descriptor may appear anywhere in config */
16611da177e4SLinus Torvalds 		if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
16621da177e4SLinus Torvalds 					le16_to_cpu(udev->config[0].desc.wTotalLength),
16631da177e4SLinus Torvalds 					USB_DT_OTG, (void **) &desc) == 0) {
16641da177e4SLinus Torvalds 			if (desc->bmAttributes & USB_OTG_HNP) {
166512c3da34SAlan Stern 				unsigned		port1 = udev->portnum;
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 				dev_info(&udev->dev,
16681da177e4SLinus Torvalds 					"Dual-Role OTG device on %sHNP port\n",
16691da177e4SLinus Torvalds 					(port1 == bus->otg_port)
16701da177e4SLinus Torvalds 						? "" : "non-");
16711da177e4SLinus Torvalds 
16721da177e4SLinus Torvalds 				/* enable HNP before suspend, it's simpler */
16731da177e4SLinus Torvalds 				if (port1 == bus->otg_port)
16741da177e4SLinus Torvalds 					bus->b_hnp_enable = 1;
16751da177e4SLinus Torvalds 				err = usb_control_msg(udev,
16761da177e4SLinus Torvalds 					usb_sndctrlpipe(udev, 0),
16771da177e4SLinus Torvalds 					USB_REQ_SET_FEATURE, 0,
16781da177e4SLinus Torvalds 					bus->b_hnp_enable
16791da177e4SLinus Torvalds 						? USB_DEVICE_B_HNP_ENABLE
16801da177e4SLinus Torvalds 						: USB_DEVICE_A_ALT_HNP_SUPPORT,
16811da177e4SLinus Torvalds 					0, NULL, 0, USB_CTRL_SET_TIMEOUT);
16821da177e4SLinus Torvalds 				if (err < 0) {
16831da177e4SLinus Torvalds 					/* OTG MESSAGE: report errors here,
16841da177e4SLinus Torvalds 					 * customize to match your product.
16851da177e4SLinus Torvalds 					 */
16861da177e4SLinus Torvalds 					dev_info(&udev->dev,
1687b9cef6c3SWu Fengguang 						"can't set HNP mode: %d\n",
16881da177e4SLinus Torvalds 						err);
16891da177e4SLinus Torvalds 					bus->b_hnp_enable = 0;
16901da177e4SLinus Torvalds 				}
16911da177e4SLinus Torvalds 			}
16921da177e4SLinus Torvalds 		}
16931da177e4SLinus Torvalds 	}
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 	if (!is_targeted(udev)) {
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 		/* Maybe it can talk to us, though we can't talk to it.
16981da177e4SLinus Torvalds 		 * (Includes HNP test device.)
16991da177e4SLinus Torvalds 		 */
17001da177e4SLinus Torvalds 		if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
1701634a84f8SDavid Brownell 			err = usb_port_suspend(udev, PMSG_SUSPEND);
17021da177e4SLinus Torvalds 			if (err < 0)
17031da177e4SLinus Torvalds 				dev_dbg(&udev->dev, "HNP fail, %d\n", err);
17041da177e4SLinus Torvalds 		}
1705ffcdc18dSVikram Pandita 		err = -ENOTSUPP;
17061da177e4SLinus Torvalds 		goto fail;
17071da177e4SLinus Torvalds 	}
1708d9d16e8aSInaky Perez-Gonzalez fail:
17091da177e4SLinus Torvalds #endif
1710d9d16e8aSInaky Perez-Gonzalez 	return err;
1711d9d16e8aSInaky Perez-Gonzalez }
17121da177e4SLinus Torvalds 
1713d9d16e8aSInaky Perez-Gonzalez 
1714d9d16e8aSInaky Perez-Gonzalez /**
17158d8558d1SAlan Stern  * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
1716d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
1717d9d16e8aSInaky Perez-Gonzalez  *
1718d9d16e8aSInaky Perez-Gonzalez  * This is only called by usb_new_device() and usb_authorize_device()
1719d9d16e8aSInaky Perez-Gonzalez  * and FIXME -- all comments that apply to them apply here wrt to
1720d9d16e8aSInaky Perez-Gonzalez  * environment.
1721d9d16e8aSInaky Perez-Gonzalez  *
1722d9d16e8aSInaky Perez-Gonzalez  * If the device is WUSB and not authorized, we don't attempt to read
1723d9d16e8aSInaky Perez-Gonzalez  * the string descriptors, as they will be errored out by the device
1724d9d16e8aSInaky Perez-Gonzalez  * until it has been authorized.
1725d9d16e8aSInaky Perez-Gonzalez  */
17268d8558d1SAlan Stern static int usb_enumerate_device(struct usb_device *udev)
1727d9d16e8aSInaky Perez-Gonzalez {
1728d9d16e8aSInaky Perez-Gonzalez 	int err;
1729d9d16e8aSInaky Perez-Gonzalez 
1730d9d16e8aSInaky Perez-Gonzalez 	if (udev->config == NULL) {
1731d9d16e8aSInaky Perez-Gonzalez 		err = usb_get_configuration(udev);
1732d9d16e8aSInaky Perez-Gonzalez 		if (err < 0) {
1733d9d16e8aSInaky Perez-Gonzalez 			dev_err(&udev->dev, "can't read configurations, error %d\n",
1734d9d16e8aSInaky Perez-Gonzalez 				err);
1735d9d16e8aSInaky Perez-Gonzalez 			goto fail;
1736d9d16e8aSInaky Perez-Gonzalez 		}
1737d9d16e8aSInaky Perez-Gonzalez 	}
1738d9d16e8aSInaky Perez-Gonzalez 	if (udev->wusb == 1 && udev->authorized == 0) {
1739d9d16e8aSInaky Perez-Gonzalez 		udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1740d9d16e8aSInaky Perez-Gonzalez 		udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1741d9d16e8aSInaky Perez-Gonzalez 		udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1742d9d16e8aSInaky Perez-Gonzalez 	}
1743d9d16e8aSInaky Perez-Gonzalez 	else {
1744d9d16e8aSInaky Perez-Gonzalez 		/* read the standard strings and cache them if present */
1745d9d16e8aSInaky Perez-Gonzalez 		udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
1746d9d16e8aSInaky Perez-Gonzalez 		udev->manufacturer = usb_cache_string(udev,
1747d9d16e8aSInaky Perez-Gonzalez 						      udev->descriptor.iManufacturer);
1748d9d16e8aSInaky Perez-Gonzalez 		udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
1749d9d16e8aSInaky Perez-Gonzalez 	}
17508d8558d1SAlan Stern 	err = usb_enumerate_device_otg(udev);
1751d9d16e8aSInaky Perez-Gonzalez fail:
1752d9d16e8aSInaky Perez-Gonzalez 	return err;
1753d9d16e8aSInaky Perez-Gonzalez }
1754d9d16e8aSInaky Perez-Gonzalez 
1755d9d16e8aSInaky Perez-Gonzalez 
1756d9d16e8aSInaky Perez-Gonzalez /**
1757d9d16e8aSInaky Perez-Gonzalez  * usb_new_device - perform initial device setup (usbcore-internal)
1758d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
1759d9d16e8aSInaky Perez-Gonzalez  *
17608d8558d1SAlan Stern  * This is called with devices which have been detected but not fully
17618d8558d1SAlan Stern  * enumerated.  The device descriptor is available, but not descriptors
1762d9d16e8aSInaky Perez-Gonzalez  * for any device configuration.  The caller must have locked either
1763d9d16e8aSInaky Perez-Gonzalez  * the parent hub (if udev is a normal device) or else the
1764d9d16e8aSInaky Perez-Gonzalez  * usb_bus_list_lock (if udev is a root hub).  The parent's pointer to
1765d9d16e8aSInaky Perez-Gonzalez  * udev has already been installed, but udev is not yet visible through
1766d9d16e8aSInaky Perez-Gonzalez  * sysfs or other filesystem code.
1767d9d16e8aSInaky Perez-Gonzalez  *
1768d9d16e8aSInaky Perez-Gonzalez  * It will return if the device is configured properly or not.  Zero if
1769d9d16e8aSInaky Perez-Gonzalez  * the interface was registered with the driver core; else a negative
1770d9d16e8aSInaky Perez-Gonzalez  * errno value.
1771d9d16e8aSInaky Perez-Gonzalez  *
1772d9d16e8aSInaky Perez-Gonzalez  * This call is synchronous, and may not be used in an interrupt context.
1773d9d16e8aSInaky Perez-Gonzalez  *
1774d9d16e8aSInaky Perez-Gonzalez  * Only the hub driver or root-hub registrar should ever call this.
1775d9d16e8aSInaky Perez-Gonzalez  */
1776d9d16e8aSInaky Perez-Gonzalez int usb_new_device(struct usb_device *udev)
1777d9d16e8aSInaky Perez-Gonzalez {
1778d9d16e8aSInaky Perez-Gonzalez 	int err;
1779d9d16e8aSInaky Perez-Gonzalez 
178016985408SDan Streetman 	if (udev->parent) {
178116985408SDan Streetman 		/* Initialize non-root-hub device wakeup to disabled;
178216985408SDan Streetman 		 * device (un)configuration controls wakeup capable
178316985408SDan Streetman 		 * sysfs power/wakeup controls wakeup enabled/disabled
178416985408SDan Streetman 		 */
178516985408SDan Streetman 		device_init_wakeup(&udev->dev, 0);
178616985408SDan Streetman 		device_set_wakeup_enable(&udev->dev, 1);
178716985408SDan Streetman 	}
178816985408SDan Streetman 
17899bbdf1e0SAlan Stern 	/* Tell the runtime-PM framework the device is active */
17909bbdf1e0SAlan Stern 	pm_runtime_set_active(&udev->dev);
17919bbdf1e0SAlan Stern 	pm_runtime_enable(&udev->dev);
17929bbdf1e0SAlan Stern 
17938d8558d1SAlan Stern 	usb_detect_quirks(udev);
17948d8558d1SAlan Stern 	err = usb_enumerate_device(udev);	/* Read descriptors */
1795d9d16e8aSInaky Perez-Gonzalez 	if (err < 0)
1796d9d16e8aSInaky Perez-Gonzalez 		goto fail;
1797c6515272SSarah Sharp 	dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
1798c6515272SSarah Sharp 			udev->devnum, udev->bus->busnum,
1799c6515272SSarah Sharp 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
18009f8b17e6SKay Sievers 	/* export the usbdev device-node for libusb */
18019f8b17e6SKay Sievers 	udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
18029f8b17e6SKay Sievers 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
18039f8b17e6SKay Sievers 
18046cd13201SAlan Stern 	/* Tell the world! */
18056cd13201SAlan Stern 	announce_device(udev);
1806195af2ccSAlan Stern 
1807927bc916SRafael J. Wysocki 	device_enable_async_suspend(&udev->dev);
1808782da727SAlan Stern 	/* Register the device.  The device driver is responsible
18093b23dd6fSAlan Stern 	 * for configuring the device and invoking the add-device
18103b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
1811782da727SAlan Stern 	 */
18121da177e4SLinus Torvalds 	err = device_add(&udev->dev);
18131da177e4SLinus Torvalds 	if (err) {
18141da177e4SLinus Torvalds 		dev_err(&udev->dev, "can't device_add, error %d\n", err);
18151da177e4SLinus Torvalds 		goto fail;
18161da177e4SLinus Torvalds 	}
18179ad3d6ccSAlan Stern 
18183b23dd6fSAlan Stern 	(void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
1819c066475eSGreg Kroah-Hartman 	return err;
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds fail:
18221da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
18239bbdf1e0SAlan Stern 	pm_runtime_disable(&udev->dev);
18249bbdf1e0SAlan Stern 	pm_runtime_set_suspended(&udev->dev);
1825d9d16e8aSInaky Perez-Gonzalez 	return err;
18261da177e4SLinus Torvalds }
18271da177e4SLinus Torvalds 
182893993a0aSInaky Perez-Gonzalez 
182993993a0aSInaky Perez-Gonzalez /**
1830fd39c86bSRandy Dunlap  * usb_deauthorize_device - deauthorize a device (usbcore-internal)
1831fd39c86bSRandy Dunlap  * @usb_dev: USB device
1832fd39c86bSRandy Dunlap  *
1833fd39c86bSRandy Dunlap  * Move the USB device to a very basic state where interfaces are disabled
1834fd39c86bSRandy Dunlap  * and the device is in fact unconfigured and unusable.
183593993a0aSInaky Perez-Gonzalez  *
183693993a0aSInaky Perez-Gonzalez  * We share a lock (that we have) with device_del(), so we need to
183793993a0aSInaky Perez-Gonzalez  * defer its call.
183893993a0aSInaky Perez-Gonzalez  */
183993993a0aSInaky Perez-Gonzalez int usb_deauthorize_device(struct usb_device *usb_dev)
184093993a0aSInaky Perez-Gonzalez {
184193993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
184293993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 0)
184393993a0aSInaky Perez-Gonzalez 		goto out_unauthorized;
1844da307123SAlan Stern 
184593993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 0;
184693993a0aSInaky Perez-Gonzalez 	usb_set_configuration(usb_dev, -1);
1847da307123SAlan Stern 
1848da307123SAlan Stern 	kfree(usb_dev->product);
184993993a0aSInaky Perez-Gonzalez 	usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1850da307123SAlan Stern 	kfree(usb_dev->manufacturer);
185193993a0aSInaky Perez-Gonzalez 	usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1852da307123SAlan Stern 	kfree(usb_dev->serial);
185393993a0aSInaky Perez-Gonzalez 	usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1854da307123SAlan Stern 
1855da307123SAlan Stern 	usb_destroy_configuration(usb_dev);
185693993a0aSInaky Perez-Gonzalez 	usb_dev->descriptor.bNumConfigurations = 0;
1857da307123SAlan Stern 
185893993a0aSInaky Perez-Gonzalez out_unauthorized:
185993993a0aSInaky Perez-Gonzalez 	usb_unlock_device(usb_dev);
186093993a0aSInaky Perez-Gonzalez 	return 0;
186193993a0aSInaky Perez-Gonzalez }
186293993a0aSInaky Perez-Gonzalez 
186393993a0aSInaky Perez-Gonzalez 
186493993a0aSInaky Perez-Gonzalez int usb_authorize_device(struct usb_device *usb_dev)
186593993a0aSInaky Perez-Gonzalez {
186693993a0aSInaky Perez-Gonzalez 	int result = 0, c;
1867da307123SAlan Stern 
186893993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
186993993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 1)
187093993a0aSInaky Perez-Gonzalez 		goto out_authorized;
1871da307123SAlan Stern 
187293993a0aSInaky Perez-Gonzalez 	result = usb_autoresume_device(usb_dev);
187393993a0aSInaky Perez-Gonzalez 	if (result < 0) {
187493993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev,
187593993a0aSInaky Perez-Gonzalez 			"can't autoresume for authorization: %d\n", result);
187693993a0aSInaky Perez-Gonzalez 		goto error_autoresume;
187793993a0aSInaky Perez-Gonzalez 	}
187893993a0aSInaky Perez-Gonzalez 	result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
187993993a0aSInaky Perez-Gonzalez 	if (result < 0) {
188093993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev, "can't re-read device descriptor for "
188193993a0aSInaky Perez-Gonzalez 			"authorization: %d\n", result);
188293993a0aSInaky Perez-Gonzalez 		goto error_device_descriptor;
188393993a0aSInaky Perez-Gonzalez 	}
1884da307123SAlan Stern 
1885da307123SAlan Stern 	kfree(usb_dev->product);
1886da307123SAlan Stern 	usb_dev->product = NULL;
1887da307123SAlan Stern 	kfree(usb_dev->manufacturer);
1888da307123SAlan Stern 	usb_dev->manufacturer = NULL;
1889da307123SAlan Stern 	kfree(usb_dev->serial);
1890da307123SAlan Stern 	usb_dev->serial = NULL;
1891da307123SAlan Stern 
189293993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 1;
18938d8558d1SAlan Stern 	result = usb_enumerate_device(usb_dev);
189493993a0aSInaky Perez-Gonzalez 	if (result < 0)
18958d8558d1SAlan Stern 		goto error_enumerate;
189693993a0aSInaky Perez-Gonzalez 	/* Choose and set the configuration.  This registers the interfaces
189793993a0aSInaky Perez-Gonzalez 	 * with the driver core and lets interface drivers bind to them.
189893993a0aSInaky Perez-Gonzalez 	 */
1899b5ea060fSGreg Kroah-Hartman 	c = usb_choose_configuration(usb_dev);
190093993a0aSInaky Perez-Gonzalez 	if (c >= 0) {
190193993a0aSInaky Perez-Gonzalez 		result = usb_set_configuration(usb_dev, c);
190293993a0aSInaky Perez-Gonzalez 		if (result) {
190393993a0aSInaky Perez-Gonzalez 			dev_err(&usb_dev->dev,
190493993a0aSInaky Perez-Gonzalez 				"can't set config #%d, error %d\n", c, result);
190593993a0aSInaky Perez-Gonzalez 			/* This need not be fatal.  The user can try to
190693993a0aSInaky Perez-Gonzalez 			 * set other configurations. */
190793993a0aSInaky Perez-Gonzalez 		}
190893993a0aSInaky Perez-Gonzalez 	}
190993993a0aSInaky Perez-Gonzalez 	dev_info(&usb_dev->dev, "authorized to connect\n");
1910da307123SAlan Stern 
19118d8558d1SAlan Stern error_enumerate:
191293993a0aSInaky Perez-Gonzalez error_device_descriptor:
1913da307123SAlan Stern 	usb_autosuspend_device(usb_dev);
191493993a0aSInaky Perez-Gonzalez error_autoresume:
191593993a0aSInaky Perez-Gonzalez out_authorized:
191693993a0aSInaky Perez-Gonzalez 	usb_unlock_device(usb_dev);	// complements locktree
191793993a0aSInaky Perez-Gonzalez 	return result;
191893993a0aSInaky Perez-Gonzalez }
191993993a0aSInaky Perez-Gonzalez 
192093993a0aSInaky Perez-Gonzalez 
19210165de09SInaky Perez-Gonzalez /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
19220165de09SInaky Perez-Gonzalez static unsigned hub_is_wusb(struct usb_hub *hub)
19230165de09SInaky Perez-Gonzalez {
19240165de09SInaky Perez-Gonzalez 	struct usb_hcd *hcd;
19250165de09SInaky Perez-Gonzalez 	if (hub->hdev->parent != NULL)  /* not a root hub? */
19260165de09SInaky Perez-Gonzalez 		return 0;
19270165de09SInaky Perez-Gonzalez 	hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
19280165de09SInaky Perez-Gonzalez 	return hcd->wireless;
19290165de09SInaky Perez-Gonzalez }
19300165de09SInaky Perez-Gonzalez 
19310165de09SInaky Perez-Gonzalez 
19321da177e4SLinus Torvalds #define PORT_RESET_TRIES	5
19331da177e4SLinus Torvalds #define SET_ADDRESS_TRIES	2
19341da177e4SLinus Torvalds #define GET_DESCRIPTOR_TRIES	2
19351da177e4SLinus Torvalds #define SET_CONFIG_TRIES	(2 * (use_both_schemes + 1))
19361da177e4SLinus Torvalds #define USE_NEW_SCHEME(i)	((i) / 2 == old_scheme_first)
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds #define HUB_ROOT_RESET_TIME	50	/* times are in msec */
19391da177e4SLinus Torvalds #define HUB_SHORT_RESET_TIME	10
19401da177e4SLinus Torvalds #define HUB_LONG_RESET_TIME	200
19411da177e4SLinus Torvalds #define HUB_RESET_TIMEOUT	500
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds static int hub_port_wait_reset(struct usb_hub *hub, int port1,
19441da177e4SLinus Torvalds 				struct usb_device *udev, unsigned int delay)
19451da177e4SLinus Torvalds {
19461da177e4SLinus Torvalds 	int delay_time, ret;
19471da177e4SLinus Torvalds 	u16 portstatus;
19481da177e4SLinus Torvalds 	u16 portchange;
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 	for (delay_time = 0;
19511da177e4SLinus Torvalds 			delay_time < HUB_RESET_TIMEOUT;
19521da177e4SLinus Torvalds 			delay_time += delay) {
19531da177e4SLinus Torvalds 		/* wait to give the device a chance to reset */
19541da177e4SLinus Torvalds 		msleep(delay);
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds 		/* read and decode port status */
19571da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
19581da177e4SLinus Torvalds 		if (ret < 0)
19591da177e4SLinus Torvalds 			return ret;
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds 		/* Device went away? */
19621da177e4SLinus Torvalds 		if (!(portstatus & USB_PORT_STAT_CONNECTION))
19631da177e4SLinus Torvalds 			return -ENOTCONN;
19641da177e4SLinus Torvalds 
1965dd4dd19eSAlan Stern 		/* bomb out completely if the connection bounced */
19661da177e4SLinus Torvalds 		if ((portchange & USB_PORT_STAT_C_CONNECTION))
1967dd4dd19eSAlan Stern 			return -ENOTCONN;
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds 		/* if we`ve finished resetting, then break out of the loop */
19701da177e4SLinus Torvalds 		if (!(portstatus & USB_PORT_STAT_RESET) &&
19711da177e4SLinus Torvalds 		    (portstatus & USB_PORT_STAT_ENABLE)) {
19720165de09SInaky Perez-Gonzalez 			if (hub_is_wusb(hub))
1973551cdbbeSGreg Kroah-Hartman 				udev->speed = USB_SPEED_WIRELESS;
19740165de09SInaky Perez-Gonzalez 			else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
19751da177e4SLinus Torvalds 				udev->speed = USB_SPEED_HIGH;
19761da177e4SLinus Torvalds 			else if (portstatus & USB_PORT_STAT_LOW_SPEED)
19771da177e4SLinus Torvalds 				udev->speed = USB_SPEED_LOW;
19781da177e4SLinus Torvalds 			else
19791da177e4SLinus Torvalds 				udev->speed = USB_SPEED_FULL;
19801da177e4SLinus Torvalds 			return 0;
19811da177e4SLinus Torvalds 		}
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds 		/* switch to the long delay after two short delay failures */
19841da177e4SLinus Torvalds 		if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
19851da177e4SLinus Torvalds 			delay = HUB_LONG_RESET_TIME;
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
19881da177e4SLinus Torvalds 			"port %d not reset yet, waiting %dms\n",
19891da177e4SLinus Torvalds 			port1, delay);
19901da177e4SLinus Torvalds 	}
19911da177e4SLinus Torvalds 
19921da177e4SLinus Torvalds 	return -EBUSY;
19931da177e4SLinus Torvalds }
19941da177e4SLinus Torvalds 
19951da177e4SLinus Torvalds static int hub_port_reset(struct usb_hub *hub, int port1,
19961da177e4SLinus Torvalds 				struct usb_device *udev, unsigned int delay)
19971da177e4SLinus Torvalds {
19981da177e4SLinus Torvalds 	int i, status;
1999a5f0efabSSarah Sharp 	struct usb_hcd *hcd;
20001da177e4SLinus Torvalds 
2001a5f0efabSSarah Sharp 	hcd = bus_to_hcd(udev->bus);
200232fe0198SAlan Stern 	/* Block EHCI CF initialization during the port reset.
200332fe0198SAlan Stern 	 * Some companion controllers don't like it when they mix.
200432fe0198SAlan Stern 	 */
200532fe0198SAlan Stern 	down_read(&ehci_cf_port_reset_rwsem);
200632fe0198SAlan Stern 
20071da177e4SLinus Torvalds 	/* Reset the port */
20081da177e4SLinus Torvalds 	for (i = 0; i < PORT_RESET_TRIES; i++) {
20091da177e4SLinus Torvalds 		status = set_port_feature(hub->hdev,
20101da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_RESET);
20111da177e4SLinus Torvalds 		if (status)
20121da177e4SLinus Torvalds 			dev_err(hub->intfdev,
20131da177e4SLinus Torvalds 					"cannot reset port %d (err = %d)\n",
20141da177e4SLinus Torvalds 					port1, status);
20151da177e4SLinus Torvalds 		else {
20161da177e4SLinus Torvalds 			status = hub_port_wait_reset(hub, port1, udev, delay);
2017dd16525bSDavid Brownell 			if (status && status != -ENOTCONN)
20181da177e4SLinus Torvalds 				dev_dbg(hub->intfdev,
20191da177e4SLinus Torvalds 						"port_wait_reset: err = %d\n",
20201da177e4SLinus Torvalds 						status);
20211da177e4SLinus Torvalds 		}
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds 		/* return on disconnect or reset */
20241da177e4SLinus Torvalds 		switch (status) {
20251da177e4SLinus Torvalds 		case 0:
2026b789696aSDavid Brownell 			/* TRSTRCY = 10 ms; plus some extra */
2027b789696aSDavid Brownell 			msleep(10 + 40);
20284953d141SDavid Vrabel 			update_address(udev, 0);
2029a5f0efabSSarah Sharp 			if (hcd->driver->reset_device) {
2030a5f0efabSSarah Sharp 				status = hcd->driver->reset_device(hcd, udev);
2031a5f0efabSSarah Sharp 				if (status < 0) {
2032a5f0efabSSarah Sharp 					dev_err(&udev->dev, "Cannot reset "
2033a5f0efabSSarah Sharp 							"HCD device state\n");
2034a5f0efabSSarah Sharp 					break;
2035a5f0efabSSarah Sharp 				}
2036a5f0efabSSarah Sharp 			}
20371da177e4SLinus Torvalds 			/* FALL THROUGH */
20381da177e4SLinus Torvalds 		case -ENOTCONN:
20391da177e4SLinus Torvalds 		case -ENODEV:
20401da177e4SLinus Torvalds 			clear_port_feature(hub->hdev,
20411da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_C_RESET);
20421da177e4SLinus Torvalds 			/* FIXME need disconnect() for NOTATTACHED device */
20431da177e4SLinus Torvalds 			usb_set_device_state(udev, status
20441da177e4SLinus Torvalds 					? USB_STATE_NOTATTACHED
20451da177e4SLinus Torvalds 					: USB_STATE_DEFAULT);
204632fe0198SAlan Stern 			goto done;
20471da177e4SLinus Torvalds 		}
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
20501da177e4SLinus Torvalds 			"port %d not enabled, trying reset again...\n",
20511da177e4SLinus Torvalds 			port1);
20521da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
20531da177e4SLinus Torvalds 	}
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 	dev_err (hub->intfdev,
20561da177e4SLinus Torvalds 		"Cannot enable port %i.  Maybe the USB cable is bad?\n",
20571da177e4SLinus Torvalds 		port1);
20581da177e4SLinus Torvalds 
205932fe0198SAlan Stern  done:
206032fe0198SAlan Stern 	up_read(&ehci_cf_port_reset_rwsem);
20611da177e4SLinus Torvalds 	return status;
20621da177e4SLinus Torvalds }
20631da177e4SLinus Torvalds 
2064d388dab7SAlan Stern #ifdef	CONFIG_PM
20651da177e4SLinus Torvalds 
2066b01b03f3SAlan Stern #define MASK_BITS	(USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION | \
2067b01b03f3SAlan Stern 				USB_PORT_STAT_SUSPEND)
2068b01b03f3SAlan Stern #define WANT_BITS	(USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION)
2069b01b03f3SAlan Stern 
2070b01b03f3SAlan Stern /* Determine whether the device on a port is ready for a normal resume,
2071b01b03f3SAlan Stern  * is ready for a reset-resume, or should be disconnected.
2072b01b03f3SAlan Stern  */
2073b01b03f3SAlan Stern static int check_port_resume_type(struct usb_device *udev,
2074b01b03f3SAlan Stern 		struct usb_hub *hub, int port1,
2075b01b03f3SAlan Stern 		int status, unsigned portchange, unsigned portstatus)
2076b01b03f3SAlan Stern {
2077b01b03f3SAlan Stern 	/* Is the device still present? */
2078b01b03f3SAlan Stern 	if (status || (portstatus & MASK_BITS) != WANT_BITS) {
2079b01b03f3SAlan Stern 		if (status >= 0)
2080b01b03f3SAlan Stern 			status = -ENODEV;
2081b01b03f3SAlan Stern 	}
2082b01b03f3SAlan Stern 
208386c57edfSAlan Stern 	/* Can't do a normal resume if the port isn't enabled,
208486c57edfSAlan Stern 	 * so try a reset-resume instead.
208586c57edfSAlan Stern 	 */
208686c57edfSAlan Stern 	else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
208786c57edfSAlan Stern 		if (udev->persist_enabled)
208886c57edfSAlan Stern 			udev->reset_resume = 1;
208986c57edfSAlan Stern 		else
2090b01b03f3SAlan Stern 			status = -ENODEV;
209186c57edfSAlan Stern 	}
2092b01b03f3SAlan Stern 
2093b01b03f3SAlan Stern 	if (status) {
2094b01b03f3SAlan Stern 		dev_dbg(hub->intfdev,
2095b01b03f3SAlan Stern 				"port %d status %04x.%04x after resume, %d\n",
2096b01b03f3SAlan Stern 				port1, portchange, portstatus, status);
2097b01b03f3SAlan Stern 	} else if (udev->reset_resume) {
2098b01b03f3SAlan Stern 
2099b01b03f3SAlan Stern 		/* Late port handoff can set status-change bits */
2100b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION)
2101b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2102b01b03f3SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
2103b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE)
2104b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2105b01b03f3SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
2106b01b03f3SAlan Stern 	}
2107b01b03f3SAlan Stern 
2108b01b03f3SAlan Stern 	return status;
2109b01b03f3SAlan Stern }
2110b01b03f3SAlan Stern 
21111da177e4SLinus Torvalds #ifdef	CONFIG_USB_SUSPEND
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds /*
2114140d8f68SAlan Stern  * usb_port_suspend - suspend a usb device's upstream port
2115624d6c07SAlan Stern  * @udev: device that's no longer in active use, not a root hub
21165edbfb7cSDavid Brownell  * Context: must be able to sleep; device not locked; pm locks held
21171da177e4SLinus Torvalds  *
21181da177e4SLinus Torvalds  * Suspends a USB device that isn't in active use, conserving power.
21191da177e4SLinus Torvalds  * Devices may wake out of a suspend, if anything important happens,
21201da177e4SLinus Torvalds  * using the remote wakeup mechanism.  They may also be taken out of
2121140d8f68SAlan Stern  * suspend by the host, using usb_port_resume().  It's also routine
21221da177e4SLinus Torvalds  * to disconnect devices while they are suspended.
21231da177e4SLinus Torvalds  *
2124390a8c34SDavid Brownell  * This only affects the USB hardware for a device; its interfaces
2125390a8c34SDavid Brownell  * (and, for hubs, child devices) must already have been suspended.
2126390a8c34SDavid Brownell  *
2127624d6c07SAlan Stern  * Selective port suspend reduces power; most suspended devices draw
2128624d6c07SAlan Stern  * less than 500 uA.  It's also used in OTG, along with remote wakeup.
2129624d6c07SAlan Stern  * All devices below the suspended port are also suspended.
2130624d6c07SAlan Stern  *
2131624d6c07SAlan Stern  * Devices leave suspend state when the host wakes them up.  Some devices
2132624d6c07SAlan Stern  * also support "remote wakeup", where the device can activate the USB
2133624d6c07SAlan Stern  * tree above them to deliver data, such as a keypress or packet.  In
2134624d6c07SAlan Stern  * some cases, this wakes the USB host.
2135624d6c07SAlan Stern  *
21361da177e4SLinus Torvalds  * Suspending OTG devices may trigger HNP, if that's been enabled
21371da177e4SLinus Torvalds  * between a pair of dual-role devices.  That will change roles, such
21381da177e4SLinus Torvalds  * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
21391da177e4SLinus Torvalds  *
21404956eccdSAlan Stern  * Devices on USB hub ports have only one "suspend" state, corresponding
21414956eccdSAlan Stern  * to ACPI D2, "may cause the device to lose some context".
21424956eccdSAlan Stern  * State transitions include:
21434956eccdSAlan Stern  *
21444956eccdSAlan Stern  *   - suspend, resume ... when the VBUS power link stays live
21454956eccdSAlan Stern  *   - suspend, disconnect ... VBUS lost
21464956eccdSAlan Stern  *
21474956eccdSAlan Stern  * Once VBUS drop breaks the circuit, the port it's using has to go through
21484956eccdSAlan Stern  * normal re-enumeration procedures, starting with enabling VBUS power.
21494956eccdSAlan Stern  * Other than re-initializing the hub (plug/unplug, except for root hubs),
21504956eccdSAlan Stern  * Linux (2.6) currently has NO mechanisms to initiate that:  no khubd
21514956eccdSAlan Stern  * timer, no SRP, no requests through sysfs.
21524956eccdSAlan Stern  *
21534956eccdSAlan Stern  * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
21544956eccdSAlan Stern  * the root hub for their bus goes into global suspend ... so we don't
21554956eccdSAlan Stern  * (falsely) update the device power state to say it suspended.
21564956eccdSAlan Stern  *
21571da177e4SLinus Torvalds  * Returns 0 on success, else negative errno.
21581da177e4SLinus Torvalds  */
215965bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
21601da177e4SLinus Torvalds {
2161624d6c07SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2162624d6c07SAlan Stern 	int		port1 = udev->portnum;
2163624d6c07SAlan Stern 	int		status;
21644956eccdSAlan Stern 
2165624d6c07SAlan Stern 	// dev_dbg(hub->intfdev, "suspend port %d\n", port1);
2166624d6c07SAlan Stern 
2167624d6c07SAlan Stern 	/* enable remote wakeup when appropriate; this lets the device
2168624d6c07SAlan Stern 	 * wake up the upstream hub (including maybe the root hub).
2169624d6c07SAlan Stern 	 *
2170624d6c07SAlan Stern 	 * NOTE:  OTG devices may issue remote wakeup (or SRP) even when
2171624d6c07SAlan Stern 	 * we don't explicitly enable it here.
2172624d6c07SAlan Stern 	 */
2173624d6c07SAlan Stern 	if (udev->do_remote_wakeup) {
2174624d6c07SAlan Stern 		status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2175624d6c07SAlan Stern 				USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2176624d6c07SAlan Stern 				USB_DEVICE_REMOTE_WAKEUP, 0,
2177624d6c07SAlan Stern 				NULL, 0,
2178624d6c07SAlan Stern 				USB_CTRL_SET_TIMEOUT);
21790c487206SOliver Neukum 		if (status) {
2180624d6c07SAlan Stern 			dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2181624d6c07SAlan Stern 					status);
21820c487206SOliver Neukum 			/* bail if autosuspend is requested */
21830c487206SOliver Neukum 			if (msg.event & PM_EVENT_AUTO)
21840c487206SOliver Neukum 				return status;
21850c487206SOliver Neukum 		}
2186624d6c07SAlan Stern 	}
2187624d6c07SAlan Stern 
2188624d6c07SAlan Stern 	/* see 7.1.7.6 */
2189624d6c07SAlan Stern 	status = set_port_feature(hub->hdev, port1, USB_PORT_FEAT_SUSPEND);
2190624d6c07SAlan Stern 	if (status) {
2191624d6c07SAlan Stern 		dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2192624d6c07SAlan Stern 				port1, status);
2193624d6c07SAlan Stern 		/* paranoia:  "should not happen" */
21940c487206SOliver Neukum 		if (udev->do_remote_wakeup)
2195624d6c07SAlan Stern 			(void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2196624d6c07SAlan Stern 				USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2197624d6c07SAlan Stern 				USB_DEVICE_REMOTE_WAKEUP, 0,
2198624d6c07SAlan Stern 				NULL, 0,
2199624d6c07SAlan Stern 				USB_CTRL_SET_TIMEOUT);
2200624d6c07SAlan Stern 	} else {
2201624d6c07SAlan Stern 		/* device has up to 10 msec to fully suspend */
2202624d6c07SAlan Stern 		dev_dbg(&udev->dev, "usb %ssuspend\n",
220365bfd296SAlan Stern 				(msg.event & PM_EVENT_AUTO ? "auto-" : ""));
2204624d6c07SAlan Stern 		usb_set_device_state(udev, USB_STATE_SUSPENDED);
2205624d6c07SAlan Stern 		msleep(10);
2206624d6c07SAlan Stern 	}
22074956eccdSAlan Stern 	return status;
22081da177e4SLinus Torvalds }
2209f3f3253dSDavid Brownell 
22101da177e4SLinus Torvalds /*
2211390a8c34SDavid Brownell  * If the USB "suspend" state is in use (rather than "global suspend"),
2212390a8c34SDavid Brownell  * many devices will be individually taken out of suspend state using
221354515fe5SAlan Stern  * special "resume" signaling.  This routine kicks in shortly after
22141da177e4SLinus Torvalds  * hardware resume signaling is finished, either because of selective
22151da177e4SLinus Torvalds  * resume (by host) or remote wakeup (by device) ... now see what changed
22161da177e4SLinus Torvalds  * in the tree that's rooted at this device.
221754515fe5SAlan Stern  *
221854515fe5SAlan Stern  * If @udev->reset_resume is set then the device is reset before the
221954515fe5SAlan Stern  * status check is done.
22201da177e4SLinus Torvalds  */
2221140d8f68SAlan Stern static int finish_port_resume(struct usb_device *udev)
22221da177e4SLinus Torvalds {
222354515fe5SAlan Stern 	int	status = 0;
22241da177e4SLinus Torvalds 	u16	devstatus;
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds 	/* caller owns the udev device lock */
2227b9cef6c3SWu Fengguang 	dev_dbg(&udev->dev, "%s\n",
2228b9cef6c3SWu Fengguang 		udev->reset_resume ? "finish reset-resume" : "finish resume");
22291da177e4SLinus Torvalds 
22301da177e4SLinus Torvalds 	/* usb ch9 identifies four variants of SUSPENDED, based on what
22311da177e4SLinus Torvalds 	 * state the device resumes to.  Linux currently won't see the
22321da177e4SLinus Torvalds 	 * first two on the host side; they'd be inside hub_port_init()
22331da177e4SLinus Torvalds 	 * during many timeouts, but khubd can't suspend until later.
22341da177e4SLinus Torvalds 	 */
22351da177e4SLinus Torvalds 	usb_set_device_state(udev, udev->actconfig
22361da177e4SLinus Torvalds 			? USB_STATE_CONFIGURED
22371da177e4SLinus Torvalds 			: USB_STATE_ADDRESS);
22381da177e4SLinus Torvalds 
223954515fe5SAlan Stern 	/* 10.5.4.5 says not to reset a suspended port if the attached
224054515fe5SAlan Stern 	 * device is enabled for remote wakeup.  Hence the reset
224154515fe5SAlan Stern 	 * operation is carried out here, after the port has been
224254515fe5SAlan Stern 	 * resumed.
224354515fe5SAlan Stern 	 */
224454515fe5SAlan Stern 	if (udev->reset_resume)
224586c57edfSAlan Stern  retry_reset_resume:
2246742120c6SMing Lei 		status = usb_reset_and_verify_device(udev);
224754515fe5SAlan Stern 
22481da177e4SLinus Torvalds  	/* 10.5.4.5 says be sure devices in the tree are still there.
22491da177e4SLinus Torvalds  	 * For now let's assume the device didn't go crazy on resume,
22501da177e4SLinus Torvalds 	 * and device drivers will know about any resume quirks.
22511da177e4SLinus Torvalds 	 */
225254515fe5SAlan Stern 	if (status == 0) {
225346dede46SAlan Stern 		devstatus = 0;
22541da177e4SLinus Torvalds 		status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2255b40b7a90SAlan Stern 		if (status >= 0)
225646dede46SAlan Stern 			status = (status > 0 ? 0 : -ENODEV);
225786c57edfSAlan Stern 
225886c57edfSAlan Stern 		/* If a normal resume failed, try doing a reset-resume */
225986c57edfSAlan Stern 		if (status && !udev->reset_resume && udev->persist_enabled) {
226086c57edfSAlan Stern 			dev_dbg(&udev->dev, "retry with reset-resume\n");
226186c57edfSAlan Stern 			udev->reset_resume = 1;
226286c57edfSAlan Stern 			goto retry_reset_resume;
226386c57edfSAlan Stern 		}
226454515fe5SAlan Stern 	}
2265b40b7a90SAlan Stern 
2266624d6c07SAlan Stern 	if (status) {
2267624d6c07SAlan Stern 		dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
22681da177e4SLinus Torvalds 				status);
2269624d6c07SAlan Stern 	} else if (udev->actconfig) {
22701da177e4SLinus Torvalds 		le16_to_cpus(&devstatus);
2271686314cfSAlan Stern 		if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
22721da177e4SLinus Torvalds 			status = usb_control_msg(udev,
22731da177e4SLinus Torvalds 					usb_sndctrlpipe(udev, 0),
22741da177e4SLinus Torvalds 					USB_REQ_CLEAR_FEATURE,
22751da177e4SLinus Torvalds 						USB_RECIP_DEVICE,
22761da177e4SLinus Torvalds 					USB_DEVICE_REMOTE_WAKEUP, 0,
22771da177e4SLinus Torvalds 					NULL, 0,
22781da177e4SLinus Torvalds 					USB_CTRL_SET_TIMEOUT);
2279a8e7c565SAlan Stern 			if (status)
2280b9cef6c3SWu Fengguang 				dev_dbg(&udev->dev,
2281b9cef6c3SWu Fengguang 					"disable remote wakeup, status %d\n",
2282b9cef6c3SWu Fengguang 					status);
22834bf0ba86SAlan Stern 		}
22841da177e4SLinus Torvalds 		status = 0;
22851da177e4SLinus Torvalds 	}
22861da177e4SLinus Torvalds 	return status;
22871da177e4SLinus Torvalds }
22881da177e4SLinus Torvalds 
2289624d6c07SAlan Stern /*
2290624d6c07SAlan Stern  * usb_port_resume - re-activate a suspended usb device's upstream port
2291624d6c07SAlan Stern  * @udev: device to re-activate, not a root hub
2292624d6c07SAlan Stern  * Context: must be able to sleep; device not locked; pm locks held
2293624d6c07SAlan Stern  *
2294624d6c07SAlan Stern  * This will re-activate the suspended device, increasing power usage
2295624d6c07SAlan Stern  * while letting drivers communicate again with its endpoints.
2296624d6c07SAlan Stern  * USB resume explicitly guarantees that the power session between
2297624d6c07SAlan Stern  * the host and the device is the same as it was when the device
2298624d6c07SAlan Stern  * suspended.
2299624d6c07SAlan Stern  *
2300feccc30dSAlan Stern  * If @udev->reset_resume is set then this routine won't check that the
2301feccc30dSAlan Stern  * port is still enabled.  Furthermore, finish_port_resume() above will
230254515fe5SAlan Stern  * reset @udev.  The end result is that a broken power session can be
230354515fe5SAlan Stern  * recovered and @udev will appear to persist across a loss of VBUS power.
230454515fe5SAlan Stern  *
230554515fe5SAlan Stern  * For example, if a host controller doesn't maintain VBUS suspend current
230654515fe5SAlan Stern  * during a system sleep or is reset when the system wakes up, all the USB
230754515fe5SAlan Stern  * power sessions below it will be broken.  This is especially troublesome
230854515fe5SAlan Stern  * for mass-storage devices containing mounted filesystems, since the
230954515fe5SAlan Stern  * device will appear to have disconnected and all the memory mappings
231054515fe5SAlan Stern  * to it will be lost.  Using the USB_PERSIST facility, the device can be
231154515fe5SAlan Stern  * made to appear as if it had not disconnected.
231254515fe5SAlan Stern  *
2313742120c6SMing Lei  * This facility can be dangerous.  Although usb_reset_and_verify_device() makes
2314feccc30dSAlan Stern  * every effort to insure that the same device is present after the
231554515fe5SAlan Stern  * reset as before, it cannot provide a 100% guarantee.  Furthermore it's
231654515fe5SAlan Stern  * quite possible for a device to remain unaltered but its media to be
231754515fe5SAlan Stern  * changed.  If the user replaces a flash memory card while the system is
231854515fe5SAlan Stern  * asleep, he will have only himself to blame when the filesystem on the
231954515fe5SAlan Stern  * new card is corrupted and the system crashes.
232054515fe5SAlan Stern  *
2321624d6c07SAlan Stern  * Returns 0 on success, else negative errno.
2322624d6c07SAlan Stern  */
232365bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
23241da177e4SLinus Torvalds {
2325624d6c07SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2326624d6c07SAlan Stern 	int		port1 = udev->portnum;
23271da177e4SLinus Torvalds 	int		status;
2328d25450c6SAlan Stern 	u16		portchange, portstatus;
2329d25450c6SAlan Stern 
2330d25450c6SAlan Stern 	/* Skip the initial Clear-Suspend step for a remote wakeup */
2331d25450c6SAlan Stern 	status = hub_port_status(hub, port1, &portstatus, &portchange);
2332d25450c6SAlan Stern 	if (status == 0 && !(portstatus & USB_PORT_STAT_SUSPEND))
2333d25450c6SAlan Stern 		goto SuspendCleared;
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds 	// dev_dbg(hub->intfdev, "resume port %d\n", port1);
23361da177e4SLinus Torvalds 
2337d5cbad4bSAlan Stern 	set_bit(port1, hub->busy_bits);
2338d5cbad4bSAlan Stern 
23391da177e4SLinus Torvalds 	/* see 7.1.7.7; affects power usage, but not budgeting */
23401da177e4SLinus Torvalds 	status = clear_port_feature(hub->hdev,
23411da177e4SLinus Torvalds 			port1, USB_PORT_FEAT_SUSPEND);
23421da177e4SLinus Torvalds 	if (status) {
2343624d6c07SAlan Stern 		dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
23441da177e4SLinus Torvalds 				port1, status);
23451da177e4SLinus Torvalds 	} else {
23461da177e4SLinus Torvalds 		/* drive resume for at least 20 msec */
2347645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n",
234865bfd296SAlan Stern 				(msg.event & PM_EVENT_AUTO ? "auto-" : ""));
23491da177e4SLinus Torvalds 		msleep(25);
23501da177e4SLinus Torvalds 
23511da177e4SLinus Torvalds 		/* Virtual root hubs can trigger on GET_PORT_STATUS to
23521da177e4SLinus Torvalds 		 * stop resume signaling.  Then finish the resume
23531da177e4SLinus Torvalds 		 * sequence.
23541da177e4SLinus Torvalds 		 */
2355d25450c6SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
235654515fe5SAlan Stern 
23571da177e4SLinus Torvalds 		/* TRSMRCY = 10 msec */
23581da177e4SLinus Torvalds 		msleep(10);
23591da177e4SLinus Torvalds 	}
2360b01b03f3SAlan Stern 
2361b01b03f3SAlan Stern  SuspendCleared:
2362b01b03f3SAlan Stern 	if (status == 0) {
2363b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_SUSPEND)
2364b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2365b01b03f3SAlan Stern 					USB_PORT_FEAT_C_SUSPEND);
23661da177e4SLinus Torvalds 	}
23671da177e4SLinus Torvalds 
2368d5cbad4bSAlan Stern 	clear_bit(port1, hub->busy_bits);
2369d5cbad4bSAlan Stern 
2370b01b03f3SAlan Stern 	status = check_port_resume_type(udev,
2371b01b03f3SAlan Stern 			hub, port1, status, portchange, portstatus);
237254515fe5SAlan Stern 	if (status == 0)
237354515fe5SAlan Stern 		status = finish_port_resume(udev);
237454515fe5SAlan Stern 	if (status < 0) {
237554515fe5SAlan Stern 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
237654515fe5SAlan Stern 		hub_port_logical_disconnect(hub, port1);
237754515fe5SAlan Stern 	}
23781da177e4SLinus Torvalds 	return status;
23791da177e4SLinus Torvalds }
23801da177e4SLinus Torvalds 
23818808f00cSAlan Stern /* caller has locked udev */
23820534d468SAlan Stern int usb_remote_wakeup(struct usb_device *udev)
23831da177e4SLinus Torvalds {
23841da177e4SLinus Torvalds 	int	status = 0;
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds 	if (udev->state == USB_STATE_SUSPENDED) {
2387645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
23889bbdf1e0SAlan Stern 		status = usb_autoresume_device(udev);
23899bbdf1e0SAlan Stern 		if (status == 0) {
23909bbdf1e0SAlan Stern 			/* Let the drivers do their thing, then... */
23919bbdf1e0SAlan Stern 			usb_autosuspend_device(udev);
23929bbdf1e0SAlan Stern 		}
2393d25450c6SAlan Stern 	}
23941da177e4SLinus Torvalds 	return status;
23951da177e4SLinus Torvalds }
23961da177e4SLinus Torvalds 
2397d388dab7SAlan Stern #else	/* CONFIG_USB_SUSPEND */
2398d388dab7SAlan Stern 
2399d388dab7SAlan Stern /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
2400d388dab7SAlan Stern 
240165bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
2402d388dab7SAlan Stern {
2403d388dab7SAlan Stern 	return 0;
2404d388dab7SAlan Stern }
2405d388dab7SAlan Stern 
2406b01b03f3SAlan Stern /* However we may need to do a reset-resume */
2407b01b03f3SAlan Stern 
240865bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
2409d388dab7SAlan Stern {
2410b01b03f3SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2411b01b03f3SAlan Stern 	int		port1 = udev->portnum;
2412b01b03f3SAlan Stern 	int		status;
2413b01b03f3SAlan Stern 	u16		portchange, portstatus;
241454515fe5SAlan Stern 
2415b01b03f3SAlan Stern 	status = hub_port_status(hub, port1, &portstatus, &portchange);
2416b01b03f3SAlan Stern 	status = check_port_resume_type(udev,
2417b01b03f3SAlan Stern 			hub, port1, status, portchange, portstatus);
2418b01b03f3SAlan Stern 
2419b01b03f3SAlan Stern 	if (status) {
2420b01b03f3SAlan Stern 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2421b01b03f3SAlan Stern 		hub_port_logical_disconnect(hub, port1);
2422b01b03f3SAlan Stern 	} else if (udev->reset_resume) {
242354515fe5SAlan Stern 		dev_dbg(&udev->dev, "reset-resume\n");
2424742120c6SMing Lei 		status = usb_reset_and_verify_device(udev);
242554515fe5SAlan Stern 	}
242654515fe5SAlan Stern 	return status;
2427d388dab7SAlan Stern }
2428d388dab7SAlan Stern 
2429d388dab7SAlan Stern #endif
2430d388dab7SAlan Stern 
2431db690874SDavid Brownell static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
24321da177e4SLinus Torvalds {
24331da177e4SLinus Torvalds 	struct usb_hub		*hub = usb_get_intfdata (intf);
24341da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
24351da177e4SLinus Torvalds 	unsigned		port1;
24361da177e4SLinus Torvalds 
2437c9f89fa4SDavid Brownell 	/* fail if children aren't already suspended */
24381da177e4SLinus Torvalds 	for (port1 = 1; port1 <= hdev->maxchild; port1++) {
24391da177e4SLinus Torvalds 		struct usb_device	*udev;
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds 		udev = hdev->children [port1-1];
24426840d255SAlan Stern 		if (udev && udev->can_submit) {
244365bfd296SAlan Stern 			if (!(msg.event & PM_EVENT_AUTO))
2444645daaabSAlan Stern 				dev_dbg(&intf->dev, "port %d nyet suspended\n",
2445645daaabSAlan Stern 						port1);
2446c9f89fa4SDavid Brownell 			return -EBUSY;
2447c9f89fa4SDavid Brownell 		}
24481da177e4SLinus Torvalds 	}
24491da177e4SLinus Torvalds 
2450441b62c1SHarvey Harrison 	dev_dbg(&intf->dev, "%s\n", __func__);
245140f122f3SAlan Stern 
2452c9f89fa4SDavid Brownell 	/* stop khubd and related activity */
24534330354fSAlan Stern 	hub_quiesce(hub, HUB_SUSPEND);
2454b6f6436dSAlan Stern 	return 0;
24551da177e4SLinus Torvalds }
24561da177e4SLinus Torvalds 
24571da177e4SLinus Torvalds static int hub_resume(struct usb_interface *intf)
24581da177e4SLinus Torvalds {
24591da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata(intf);
24601da177e4SLinus Torvalds 
24615e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
2462f2835219SAlan Stern 	hub_activate(hub, HUB_RESUME);
24631da177e4SLinus Torvalds 	return 0;
24641da177e4SLinus Torvalds }
24651da177e4SLinus Torvalds 
2466b41a60ecSAlan Stern static int hub_reset_resume(struct usb_interface *intf)
2467f07600cfSAlan Stern {
2468b41a60ecSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
2469f07600cfSAlan Stern 
24705e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
2471f2835219SAlan Stern 	hub_activate(hub, HUB_RESET_RESUME);
2472f07600cfSAlan Stern 	return 0;
2473f07600cfSAlan Stern }
2474f07600cfSAlan Stern 
247554515fe5SAlan Stern /**
247654515fe5SAlan Stern  * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
247754515fe5SAlan Stern  * @rhdev: struct usb_device for the root hub
247854515fe5SAlan Stern  *
247954515fe5SAlan Stern  * The USB host controller driver calls this function when its root hub
248054515fe5SAlan Stern  * is resumed and Vbus power has been interrupted or the controller
2481feccc30dSAlan Stern  * has been reset.  The routine marks @rhdev as having lost power.
2482feccc30dSAlan Stern  * When the hub driver is resumed it will take notice and carry out
2483feccc30dSAlan Stern  * power-session recovery for all the "USB-PERSIST"-enabled child devices;
2484feccc30dSAlan Stern  * the others will be disconnected.
248554515fe5SAlan Stern  */
248654515fe5SAlan Stern void usb_root_hub_lost_power(struct usb_device *rhdev)
248754515fe5SAlan Stern {
248854515fe5SAlan Stern 	dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
248954515fe5SAlan Stern 	rhdev->reset_resume = 1;
249054515fe5SAlan Stern }
249154515fe5SAlan Stern EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
249254515fe5SAlan Stern 
2493d388dab7SAlan Stern #else	/* CONFIG_PM */
2494d388dab7SAlan Stern 
2495511366daSAndrew Morton #define hub_suspend		NULL
2496511366daSAndrew Morton #define hub_resume		NULL
2497f07600cfSAlan Stern #define hub_reset_resume	NULL
2498d388dab7SAlan Stern #endif
2499d388dab7SAlan Stern 
25001da177e4SLinus Torvalds 
25011da177e4SLinus Torvalds /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
25021da177e4SLinus Torvalds  *
25031da177e4SLinus Torvalds  * Between connect detection and reset signaling there must be a delay
25041da177e4SLinus Torvalds  * of 100ms at least for debounce and power-settling.  The corresponding
25051da177e4SLinus Torvalds  * timer shall restart whenever the downstream port detects a disconnect.
25061da177e4SLinus Torvalds  *
25071da177e4SLinus Torvalds  * Apparently there are some bluetooth and irda-dongles and a number of
25081da177e4SLinus Torvalds  * low-speed devices for which this debounce period may last over a second.
25091da177e4SLinus Torvalds  * Not covered by the spec - but easy to deal with.
25101da177e4SLinus Torvalds  *
25111da177e4SLinus Torvalds  * This implementation uses a 1500ms total debounce timeout; if the
25121da177e4SLinus Torvalds  * connection isn't stable by then it returns -ETIMEDOUT.  It checks
25131da177e4SLinus Torvalds  * every 25ms for transient disconnects.  When the port status has been
25141da177e4SLinus Torvalds  * unchanged for 100ms it returns the port status.
25151da177e4SLinus Torvalds  */
25161da177e4SLinus Torvalds static int hub_port_debounce(struct usb_hub *hub, int port1)
25171da177e4SLinus Torvalds {
25181da177e4SLinus Torvalds 	int ret;
25191da177e4SLinus Torvalds 	int total_time, stable_time = 0;
25201da177e4SLinus Torvalds 	u16 portchange, portstatus;
25211da177e4SLinus Torvalds 	unsigned connection = 0xffff;
25221da177e4SLinus Torvalds 
25231da177e4SLinus Torvalds 	for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
25241da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
25251da177e4SLinus Torvalds 		if (ret < 0)
25261da177e4SLinus Torvalds 			return ret;
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds 		if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
25291da177e4SLinus Torvalds 		     (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
25301da177e4SLinus Torvalds 			stable_time += HUB_DEBOUNCE_STEP;
25311da177e4SLinus Torvalds 			if (stable_time >= HUB_DEBOUNCE_STABLE)
25321da177e4SLinus Torvalds 				break;
25331da177e4SLinus Torvalds 		} else {
25341da177e4SLinus Torvalds 			stable_time = 0;
25351da177e4SLinus Torvalds 			connection = portstatus & USB_PORT_STAT_CONNECTION;
25361da177e4SLinus Torvalds 		}
25371da177e4SLinus Torvalds 
25381da177e4SLinus Torvalds 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
25391da177e4SLinus Torvalds 			clear_port_feature(hub->hdev, port1,
25401da177e4SLinus Torvalds 					USB_PORT_FEAT_C_CONNECTION);
25411da177e4SLinus Torvalds 		}
25421da177e4SLinus Torvalds 
25431da177e4SLinus Torvalds 		if (total_time >= HUB_DEBOUNCE_TIMEOUT)
25441da177e4SLinus Torvalds 			break;
25451da177e4SLinus Torvalds 		msleep(HUB_DEBOUNCE_STEP);
25461da177e4SLinus Torvalds 	}
25471da177e4SLinus Torvalds 
25481da177e4SLinus Torvalds 	dev_dbg (hub->intfdev,
25491da177e4SLinus Torvalds 		"debounce: port %d: total %dms stable %dms status 0x%x\n",
25501da177e4SLinus Torvalds 		port1, total_time, stable_time, portstatus);
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds 	if (stable_time < HUB_DEBOUNCE_STABLE)
25531da177e4SLinus Torvalds 		return -ETIMEDOUT;
25541da177e4SLinus Torvalds 	return portstatus;
25551da177e4SLinus Torvalds }
25561da177e4SLinus Torvalds 
2557fc721f51SInaky Perez-Gonzalez void usb_ep0_reinit(struct usb_device *udev)
25581da177e4SLinus Torvalds {
2559ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
2560ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
25612caf7fcdSAlan Stern 	usb_enable_endpoint(udev, &udev->ep0, true);
25621da177e4SLinus Torvalds }
2563fc721f51SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_ep0_reinit);
25641da177e4SLinus Torvalds 
25651da177e4SLinus Torvalds #define usb_sndaddr0pipe()	(PIPE_CONTROL << 30)
25661da177e4SLinus Torvalds #define usb_rcvaddr0pipe()	((PIPE_CONTROL << 30) | USB_DIR_IN)
25671da177e4SLinus Torvalds 
25684326ed0bSAlan Stern static int hub_set_address(struct usb_device *udev, int devnum)
25691da177e4SLinus Torvalds {
25701da177e4SLinus Torvalds 	int retval;
2571c6515272SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
25721da177e4SLinus Torvalds 
2573c6515272SSarah Sharp 	/*
2574c6515272SSarah Sharp 	 * The host controller will choose the device address,
2575c6515272SSarah Sharp 	 * instead of the core having chosen it earlier
2576c6515272SSarah Sharp 	 */
2577c6515272SSarah Sharp 	if (!hcd->driver->address_device && devnum <= 1)
25781da177e4SLinus Torvalds 		return -EINVAL;
25791da177e4SLinus Torvalds 	if (udev->state == USB_STATE_ADDRESS)
25801da177e4SLinus Torvalds 		return 0;
25811da177e4SLinus Torvalds 	if (udev->state != USB_STATE_DEFAULT)
25821da177e4SLinus Torvalds 		return -EINVAL;
2583c6515272SSarah Sharp 	if (hcd->driver->address_device) {
2584c6515272SSarah Sharp 		retval = hcd->driver->address_device(hcd, udev);
2585c6515272SSarah Sharp 	} else {
25861da177e4SLinus Torvalds 		retval = usb_control_msg(udev, usb_sndaddr0pipe(),
25874326ed0bSAlan Stern 				USB_REQ_SET_ADDRESS, 0, devnum, 0,
25881da177e4SLinus Torvalds 				NULL, 0, USB_CTRL_SET_TIMEOUT);
2589c6515272SSarah Sharp 		if (retval == 0)
2590c6515272SSarah Sharp 			update_address(udev, devnum);
2591c6515272SSarah Sharp 	}
25921da177e4SLinus Torvalds 	if (retval == 0) {
25934953d141SDavid Vrabel 		/* Device now using proper address. */
25941da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_ADDRESS);
2595fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
25961da177e4SLinus Torvalds 	}
25971da177e4SLinus Torvalds 	return retval;
25981da177e4SLinus Torvalds }
25991da177e4SLinus Torvalds 
26001da177e4SLinus Torvalds /* Reset device, (re)assign address, get device descriptor.
26011da177e4SLinus Torvalds  * Device connection must be stable, no more debouncing needed.
26021da177e4SLinus Torvalds  * Returns device in USB_STATE_ADDRESS, except on error.
26031da177e4SLinus Torvalds  *
26041da177e4SLinus Torvalds  * If this is called for an already-existing device (as part of
2605742120c6SMing Lei  * usb_reset_and_verify_device), the caller must own the device lock.  For a
26061da177e4SLinus Torvalds  * newly detected device that is not accessible through any global
26071da177e4SLinus Torvalds  * pointers, it's not necessary to lock the device.
26081da177e4SLinus Torvalds  */
26091da177e4SLinus Torvalds static int
26101da177e4SLinus Torvalds hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
26111da177e4SLinus Torvalds 		int retry_counter)
26121da177e4SLinus Torvalds {
26134186ecf8SArjan van de Ven 	static DEFINE_MUTEX(usb_address0_mutex);
26141da177e4SLinus Torvalds 
26151da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
2616e7b77172SSarah Sharp 	struct usb_hcd		*hcd = bus_to_hcd(hdev->bus);
26171da177e4SLinus Torvalds 	int			i, j, retval;
26181da177e4SLinus Torvalds 	unsigned		delay = HUB_SHORT_RESET_TIME;
26191da177e4SLinus Torvalds 	enum usb_device_speed	oldspeed = udev->speed;
262083a07196SInaky Perez-Gonzalez 	char 			*speed, *type;
26214326ed0bSAlan Stern 	int			devnum = udev->devnum;
26221da177e4SLinus Torvalds 
26231da177e4SLinus Torvalds 	/* root hub ports have a slightly longer reset period
26241da177e4SLinus Torvalds 	 * (from USB 2.0 spec, section 7.1.7.5)
26251da177e4SLinus Torvalds 	 */
26261da177e4SLinus Torvalds 	if (!hdev->parent) {
26271da177e4SLinus Torvalds 		delay = HUB_ROOT_RESET_TIME;
26281da177e4SLinus Torvalds 		if (port1 == hdev->bus->otg_port)
26291da177e4SLinus Torvalds 			hdev->bus->b_hnp_enable = 0;
26301da177e4SLinus Torvalds 	}
26311da177e4SLinus Torvalds 
26321da177e4SLinus Torvalds 	/* Some low speed devices have problems with the quick delay, so */
26331da177e4SLinus Torvalds 	/*  be a bit pessimistic with those devices. RHbug #23670 */
26341da177e4SLinus Torvalds 	if (oldspeed == USB_SPEED_LOW)
26351da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
26361da177e4SLinus Torvalds 
26374186ecf8SArjan van de Ven 	mutex_lock(&usb_address0_mutex);
26381da177e4SLinus Torvalds 
2639a5f0efabSSarah Sharp 	if (!udev->config && oldspeed == USB_SPEED_SUPER) {
2640e7b77172SSarah Sharp 		/* Don't reset USB 3.0 devices during an initial setup */
2641e7b77172SSarah Sharp 		usb_set_device_state(udev, USB_STATE_DEFAULT);
2642e7b77172SSarah Sharp 	} else {
26431da177e4SLinus Torvalds 		/* Reset the device; full speed may morph to high speed */
2644e7b77172SSarah Sharp 		/* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
26451da177e4SLinus Torvalds 		retval = hub_port_reset(hub, port1, udev, delay);
26461da177e4SLinus Torvalds 		if (retval < 0)		/* error or disconnect */
26471da177e4SLinus Torvalds 			goto fail;
26481da177e4SLinus Torvalds 		/* success, speed is known */
2649e7b77172SSarah Sharp 	}
26501da177e4SLinus Torvalds 	retval = -ENODEV;
26511da177e4SLinus Torvalds 
26521da177e4SLinus Torvalds 	if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
26531da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset changed speed!\n");
26541da177e4SLinus Torvalds 		goto fail;
26551da177e4SLinus Torvalds 	}
26561da177e4SLinus Torvalds 	oldspeed = udev->speed;
26571da177e4SLinus Torvalds 
26581da177e4SLinus Torvalds 	/* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
26591da177e4SLinus Torvalds 	 * it's fixed size except for full speed devices.
26605bb6e0aeSInaky Perez-Gonzalez 	 * For Wireless USB devices, ep0 max packet is always 512 (tho
26615bb6e0aeSInaky Perez-Gonzalez 	 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
26621da177e4SLinus Torvalds 	 */
26631da177e4SLinus Torvalds 	switch (udev->speed) {
26646b403b02SSarah Sharp 	case USB_SPEED_SUPER:
2665551cdbbeSGreg Kroah-Hartman 	case USB_SPEED_WIRELESS:	/* fixed at 512 */
2666551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
26675bb6e0aeSInaky Perez-Gonzalez 		break;
26681da177e4SLinus Torvalds 	case USB_SPEED_HIGH:		/* fixed at 64 */
2669551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
26701da177e4SLinus Torvalds 		break;
26711da177e4SLinus Torvalds 	case USB_SPEED_FULL:		/* 8, 16, 32, or 64 */
26721da177e4SLinus Torvalds 		/* to determine the ep0 maxpacket size, try to read
26731da177e4SLinus Torvalds 		 * the device descriptor to get bMaxPacketSize0 and
26741da177e4SLinus Torvalds 		 * then correct our initial guess.
26751da177e4SLinus Torvalds 		 */
2676551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
26771da177e4SLinus Torvalds 		break;
26781da177e4SLinus Torvalds 	case USB_SPEED_LOW:		/* fixed at 8 */
2679551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
26801da177e4SLinus Torvalds 		break;
26811da177e4SLinus Torvalds 	default:
26821da177e4SLinus Torvalds 		goto fail;
26831da177e4SLinus Torvalds 	}
26841da177e4SLinus Torvalds 
268583a07196SInaky Perez-Gonzalez 	type = "";
268683a07196SInaky Perez-Gonzalez 	switch (udev->speed) {
26871da177e4SLinus Torvalds 	case USB_SPEED_LOW:	speed = "low";	break;
26881da177e4SLinus Torvalds 	case USB_SPEED_FULL:	speed = "full";	break;
26891da177e4SLinus Torvalds 	case USB_SPEED_HIGH:	speed = "high";	break;
26906b403b02SSarah Sharp 	case USB_SPEED_SUPER:
26916b403b02SSarah Sharp 				speed = "super";
26926b403b02SSarah Sharp 				break;
2693551cdbbeSGreg Kroah-Hartman 	case USB_SPEED_WIRELESS:
269483a07196SInaky Perez-Gonzalez 				speed = "variable";
269583a07196SInaky Perez-Gonzalez 				type = "Wireless ";
269683a07196SInaky Perez-Gonzalez 				break;
26971da177e4SLinus Torvalds 	default: 		speed = "?";	break;
269883a07196SInaky Perez-Gonzalez 	}
2699c6515272SSarah Sharp 	if (udev->speed != USB_SPEED_SUPER)
270083a07196SInaky Perez-Gonzalez 		dev_info(&udev->dev,
270183a07196SInaky Perez-Gonzalez 				"%s %s speed %sUSB device using %s and address %d\n",
270283a07196SInaky Perez-Gonzalez 				(udev->config) ? "reset" : "new", speed, type,
27034326ed0bSAlan Stern 				udev->bus->controller->driver->name, devnum);
27041da177e4SLinus Torvalds 
27051da177e4SLinus Torvalds 	/* Set up TT records, if needed  */
27061da177e4SLinus Torvalds 	if (hdev->tt) {
27071da177e4SLinus Torvalds 		udev->tt = hdev->tt;
27081da177e4SLinus Torvalds 		udev->ttport = hdev->ttport;
27091da177e4SLinus Torvalds 	} else if (udev->speed != USB_SPEED_HIGH
27101da177e4SLinus Torvalds 			&& hdev->speed == USB_SPEED_HIGH) {
27111da177e4SLinus Torvalds 		udev->tt = &hub->tt;
27121da177e4SLinus Torvalds 		udev->ttport = port1;
27131da177e4SLinus Torvalds 	}
27141da177e4SLinus Torvalds 
27151da177e4SLinus Torvalds 	/* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
27161da177e4SLinus Torvalds 	 * Because device hardware and firmware is sometimes buggy in
27171da177e4SLinus Torvalds 	 * this area, and this is how Linux has done it for ages.
27181da177e4SLinus Torvalds 	 * Change it cautiously.
27191da177e4SLinus Torvalds 	 *
27201da177e4SLinus Torvalds 	 * NOTE:  If USE_NEW_SCHEME() is true we will start by issuing
27211da177e4SLinus Torvalds 	 * a 64-byte GET_DESCRIPTOR request.  This is what Windows does,
27221da177e4SLinus Torvalds 	 * so it may help with some non-standards-compliant devices.
27231da177e4SLinus Torvalds 	 * Otherwise we start with SET_ADDRESS and then try to read the
27241da177e4SLinus Torvalds 	 * first 8 bytes of the device descriptor to get the ep0 maxpacket
27251da177e4SLinus Torvalds 	 * value.
27261da177e4SLinus Torvalds 	 */
27271da177e4SLinus Torvalds 	for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
2728c6515272SSarah Sharp 		/*
2729c6515272SSarah Sharp 		 * An xHCI controller cannot send any packets to a device until
2730c6515272SSarah Sharp 		 * a set address command successfully completes.
2731c6515272SSarah Sharp 		 */
2732c6515272SSarah Sharp 		if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
27331da177e4SLinus Torvalds 			struct usb_device_descriptor *buf;
27341da177e4SLinus Torvalds 			int r = 0;
27351da177e4SLinus Torvalds 
27361da177e4SLinus Torvalds #define GET_DESCRIPTOR_BUFSIZE	64
27371da177e4SLinus Torvalds 			buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
27381da177e4SLinus Torvalds 			if (!buf) {
27391da177e4SLinus Torvalds 				retval = -ENOMEM;
27401da177e4SLinus Torvalds 				continue;
27411da177e4SLinus Torvalds 			}
27421da177e4SLinus Torvalds 
2743b89ee19aSAlan Stern 			/* Retry on all errors; some devices are flakey.
2744b89ee19aSAlan Stern 			 * 255 is for WUSB devices, we actually need to use
2745b89ee19aSAlan Stern 			 * 512 (WUSB1.0[4.8.1]).
27461da177e4SLinus Torvalds 			 */
27471da177e4SLinus Torvalds 			for (j = 0; j < 3; ++j) {
27481da177e4SLinus Torvalds 				buf->bMaxPacketSize0 = 0;
27491da177e4SLinus Torvalds 				r = usb_control_msg(udev, usb_rcvaddr0pipe(),
27501da177e4SLinus Torvalds 					USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
27511da177e4SLinus Torvalds 					USB_DT_DEVICE << 8, 0,
27521da177e4SLinus Torvalds 					buf, GET_DESCRIPTOR_BUFSIZE,
2753fd7c519dSJaroslav Kysela 					initial_descriptor_timeout);
27541da177e4SLinus Torvalds 				switch (buf->bMaxPacketSize0) {
27555bb6e0aeSInaky Perez-Gonzalez 				case 8: case 16: case 32: case 64: case 255:
27561da177e4SLinus Torvalds 					if (buf->bDescriptorType ==
27571da177e4SLinus Torvalds 							USB_DT_DEVICE) {
27581da177e4SLinus Torvalds 						r = 0;
27591da177e4SLinus Torvalds 						break;
27601da177e4SLinus Torvalds 					}
27611da177e4SLinus Torvalds 					/* FALL THROUGH */
27621da177e4SLinus Torvalds 				default:
27631da177e4SLinus Torvalds 					if (r == 0)
27641da177e4SLinus Torvalds 						r = -EPROTO;
27651da177e4SLinus Torvalds 					break;
27661da177e4SLinus Torvalds 				}
27671da177e4SLinus Torvalds 				if (r == 0)
27681da177e4SLinus Torvalds 					break;
27691da177e4SLinus Torvalds 			}
27701da177e4SLinus Torvalds 			udev->descriptor.bMaxPacketSize0 =
27711da177e4SLinus Torvalds 					buf->bMaxPacketSize0;
27721da177e4SLinus Torvalds 			kfree(buf);
27731da177e4SLinus Torvalds 
27741da177e4SLinus Torvalds 			retval = hub_port_reset(hub, port1, udev, delay);
27751da177e4SLinus Torvalds 			if (retval < 0)		/* error or disconnect */
27761da177e4SLinus Torvalds 				goto fail;
27771da177e4SLinus Torvalds 			if (oldspeed != udev->speed) {
27781da177e4SLinus Torvalds 				dev_dbg(&udev->dev,
27791da177e4SLinus Torvalds 					"device reset changed speed!\n");
27801da177e4SLinus Torvalds 				retval = -ENODEV;
27811da177e4SLinus Torvalds 				goto fail;
27821da177e4SLinus Torvalds 			}
27831da177e4SLinus Torvalds 			if (r) {
2784b9cef6c3SWu Fengguang 				dev_err(&udev->dev,
2785b9cef6c3SWu Fengguang 					"device descriptor read/64, error %d\n",
2786b9cef6c3SWu Fengguang 					r);
27871da177e4SLinus Torvalds 				retval = -EMSGSIZE;
27881da177e4SLinus Torvalds 				continue;
27891da177e4SLinus Torvalds 			}
27901da177e4SLinus Torvalds #undef GET_DESCRIPTOR_BUFSIZE
27911da177e4SLinus Torvalds 		}
27921da177e4SLinus Torvalds 
27936c529cdcSInaky Perez-Gonzalez  		/*
27946c529cdcSInaky Perez-Gonzalez  		 * If device is WUSB, we already assigned an
27956c529cdcSInaky Perez-Gonzalez  		 * unauthorized address in the Connect Ack sequence;
27966c529cdcSInaky Perez-Gonzalez  		 * authorization will assign the final address.
27976c529cdcSInaky Perez-Gonzalez  		 */
27986c529cdcSInaky Perez-Gonzalez 		if (udev->wusb == 0) {
27991da177e4SLinus Torvalds 			for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
28004326ed0bSAlan Stern 				retval = hub_set_address(udev, devnum);
28011da177e4SLinus Torvalds 				if (retval >= 0)
28021da177e4SLinus Torvalds 					break;
28031da177e4SLinus Torvalds 				msleep(200);
28041da177e4SLinus Torvalds 			}
28051da177e4SLinus Torvalds 			if (retval < 0) {
28061da177e4SLinus Torvalds 				dev_err(&udev->dev,
28071da177e4SLinus Torvalds 					"device not accepting address %d, error %d\n",
28084326ed0bSAlan Stern 					devnum, retval);
28091da177e4SLinus Torvalds 				goto fail;
28101da177e4SLinus Torvalds 			}
2811c6515272SSarah Sharp 			if (udev->speed == USB_SPEED_SUPER) {
2812c6515272SSarah Sharp 				devnum = udev->devnum;
2813c6515272SSarah Sharp 				dev_info(&udev->dev,
2814c6515272SSarah Sharp 						"%s SuperSpeed USB device using %s and address %d\n",
2815c6515272SSarah Sharp 						(udev->config) ? "reset" : "new",
2816c6515272SSarah Sharp 						udev->bus->controller->driver->name, devnum);
2817c6515272SSarah Sharp 			}
28181da177e4SLinus Torvalds 
28191da177e4SLinus Torvalds 			/* cope with hardware quirkiness:
28201da177e4SLinus Torvalds 			 *  - let SET_ADDRESS settle, some device hardware wants it
28211da177e4SLinus Torvalds 			 *  - read ep0 maxpacket even for high and low speed,
28221da177e4SLinus Torvalds 			 */
28231da177e4SLinus Torvalds 			msleep(10);
2824c6515272SSarah Sharp 			if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
28251da177e4SLinus Torvalds 				break;
28266c529cdcSInaky Perez-Gonzalez   		}
28271da177e4SLinus Torvalds 
28281da177e4SLinus Torvalds 		retval = usb_get_device_descriptor(udev, 8);
28291da177e4SLinus Torvalds 		if (retval < 8) {
2830b9cef6c3SWu Fengguang 			dev_err(&udev->dev,
2831b9cef6c3SWu Fengguang 					"device descriptor read/8, error %d\n",
2832b9cef6c3SWu Fengguang 					retval);
28331da177e4SLinus Torvalds 			if (retval >= 0)
28341da177e4SLinus Torvalds 				retval = -EMSGSIZE;
28351da177e4SLinus Torvalds 		} else {
28361da177e4SLinus Torvalds 			retval = 0;
28371da177e4SLinus Torvalds 			break;
28381da177e4SLinus Torvalds 		}
28391da177e4SLinus Torvalds 	}
28401da177e4SLinus Torvalds 	if (retval)
28411da177e4SLinus Torvalds 		goto fail;
28421da177e4SLinus Torvalds 
28436b403b02SSarah Sharp 	if (udev->descriptor.bMaxPacketSize0 == 0xff ||
28446b403b02SSarah Sharp 			udev->speed == USB_SPEED_SUPER)
28456b403b02SSarah Sharp 		i = 512;
28466b403b02SSarah Sharp 	else
28476b403b02SSarah Sharp 		i = udev->descriptor.bMaxPacketSize0;
28481da177e4SLinus Torvalds 	if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) {
28491da177e4SLinus Torvalds 		if (udev->speed != USB_SPEED_FULL ||
28501da177e4SLinus Torvalds 				!(i == 8 || i == 16 || i == 32 || i == 64)) {
28511da177e4SLinus Torvalds 			dev_err(&udev->dev, "ep0 maxpacket = %d\n", i);
28521da177e4SLinus Torvalds 			retval = -EMSGSIZE;
28531da177e4SLinus Torvalds 			goto fail;
28541da177e4SLinus Torvalds 		}
28551da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
28561da177e4SLinus Torvalds 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
2857fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
28581da177e4SLinus Torvalds 	}
28591da177e4SLinus Torvalds 
28601da177e4SLinus Torvalds 	retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
28611da177e4SLinus Torvalds 	if (retval < (signed)sizeof(udev->descriptor)) {
2862b9cef6c3SWu Fengguang 		dev_err(&udev->dev, "device descriptor read/all, error %d\n",
2863b9cef6c3SWu Fengguang 			retval);
28641da177e4SLinus Torvalds 		if (retval >= 0)
28651da177e4SLinus Torvalds 			retval = -ENOMSG;
28661da177e4SLinus Torvalds 		goto fail;
28671da177e4SLinus Torvalds 	}
28681da177e4SLinus Torvalds 
28691da177e4SLinus Torvalds 	retval = 0;
28701da177e4SLinus Torvalds 
28711da177e4SLinus Torvalds fail:
28724326ed0bSAlan Stern 	if (retval) {
28731da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 0);
28744953d141SDavid Vrabel 		update_address(udev, devnum);	/* for disconnect processing */
28754326ed0bSAlan Stern 	}
28764186ecf8SArjan van de Ven 	mutex_unlock(&usb_address0_mutex);
28771da177e4SLinus Torvalds 	return retval;
28781da177e4SLinus Torvalds }
28791da177e4SLinus Torvalds 
28801da177e4SLinus Torvalds static void
28811da177e4SLinus Torvalds check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
28821da177e4SLinus Torvalds {
28831da177e4SLinus Torvalds 	struct usb_qualifier_descriptor	*qual;
28841da177e4SLinus Torvalds 	int				status;
28851da177e4SLinus Torvalds 
2886e94b1766SChristoph Lameter 	qual = kmalloc (sizeof *qual, GFP_KERNEL);
28871da177e4SLinus Torvalds 	if (qual == NULL)
28881da177e4SLinus Torvalds 		return;
28891da177e4SLinus Torvalds 
28901da177e4SLinus Torvalds 	status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
28911da177e4SLinus Torvalds 			qual, sizeof *qual);
28921da177e4SLinus Torvalds 	if (status == sizeof *qual) {
28931da177e4SLinus Torvalds 		dev_info(&udev->dev, "not running at top speed; "
28941da177e4SLinus Torvalds 			"connect to a high speed hub\n");
28951da177e4SLinus Torvalds 		/* hub LEDs are probably harder to miss than syslog */
28961da177e4SLinus Torvalds 		if (hub->has_indicators) {
28971da177e4SLinus Torvalds 			hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
2898c4028958SDavid Howells 			schedule_delayed_work (&hub->leds, 0);
28991da177e4SLinus Torvalds 		}
29001da177e4SLinus Torvalds 	}
29011da177e4SLinus Torvalds 	kfree(qual);
29021da177e4SLinus Torvalds }
29031da177e4SLinus Torvalds 
29041da177e4SLinus Torvalds static unsigned
29051da177e4SLinus Torvalds hub_power_remaining (struct usb_hub *hub)
29061da177e4SLinus Torvalds {
29071da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
29081da177e4SLinus Torvalds 	int remaining;
290955c52718SAlan Stern 	int port1;
29101da177e4SLinus Torvalds 
291155c52718SAlan Stern 	if (!hub->limited_power)
29121da177e4SLinus Torvalds 		return 0;
29131da177e4SLinus Torvalds 
291455c52718SAlan Stern 	remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
291555c52718SAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
291655c52718SAlan Stern 		struct usb_device	*udev = hdev->children[port1 - 1];
291755c52718SAlan Stern 		int			delta;
29181da177e4SLinus Torvalds 
29191da177e4SLinus Torvalds 		if (!udev)
29201da177e4SLinus Torvalds 			continue;
29211da177e4SLinus Torvalds 
292255c52718SAlan Stern 		/* Unconfigured devices may not use more than 100mA,
292355c52718SAlan Stern 		 * or 8mA for OTG ports */
29241da177e4SLinus Torvalds 		if (udev->actconfig)
292555c52718SAlan Stern 			delta = udev->actconfig->desc.bMaxPower * 2;
292655c52718SAlan Stern 		else if (port1 != udev->bus->otg_port || hdev->parent)
292755c52718SAlan Stern 			delta = 100;
29281da177e4SLinus Torvalds 		else
292955c52718SAlan Stern 			delta = 8;
293055c52718SAlan Stern 		if (delta > hub->mA_per_port)
2931b9cef6c3SWu Fengguang 			dev_warn(&udev->dev,
2932b9cef6c3SWu Fengguang 				 "%dmA is over %umA budget for port %d!\n",
293355c52718SAlan Stern 				 delta, hub->mA_per_port, port1);
29341da177e4SLinus Torvalds 		remaining -= delta;
29351da177e4SLinus Torvalds 	}
29361da177e4SLinus Torvalds 	if (remaining < 0) {
293755c52718SAlan Stern 		dev_warn(hub->intfdev, "%dmA over power budget!\n",
293855c52718SAlan Stern 			- remaining);
29391da177e4SLinus Torvalds 		remaining = 0;
29401da177e4SLinus Torvalds 	}
29411da177e4SLinus Torvalds 	return remaining;
29421da177e4SLinus Torvalds }
29431da177e4SLinus Torvalds 
29441da177e4SLinus Torvalds /* Handle physical or logical connection change events.
29451da177e4SLinus Torvalds  * This routine is called when:
29461da177e4SLinus Torvalds  * 	a port connection-change occurs;
29471da177e4SLinus Torvalds  *	a port enable-change occurs (often caused by EMI);
2948742120c6SMing Lei  *	usb_reset_and_verify_device() encounters changed descriptors (as from
29491da177e4SLinus Torvalds  *		a firmware download)
29501da177e4SLinus Torvalds  * caller already locked the hub
29511da177e4SLinus Torvalds  */
29521da177e4SLinus Torvalds static void hub_port_connect_change(struct usb_hub *hub, int port1,
29531da177e4SLinus Torvalds 					u16 portstatus, u16 portchange)
29541da177e4SLinus Torvalds {
29551da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
29561da177e4SLinus Torvalds 	struct device *hub_dev = hub->intfdev;
295790da096eSBalaji Rao 	struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
295824618b0cSAlan Stern 	unsigned wHubCharacteristics =
295924618b0cSAlan Stern 			le16_to_cpu(hub->descriptor->wHubCharacteristics);
29608808f00cSAlan Stern 	struct usb_device *udev;
29611da177e4SLinus Torvalds 	int status, i;
29621da177e4SLinus Torvalds 
29631da177e4SLinus Torvalds 	dev_dbg (hub_dev,
29641da177e4SLinus Torvalds 		"port %d, status %04x, change %04x, %s\n",
29651da177e4SLinus Torvalds 		port1, portstatus, portchange, portspeed (portstatus));
29661da177e4SLinus Torvalds 
29671da177e4SLinus Torvalds 	if (hub->has_indicators) {
29681da177e4SLinus Torvalds 		set_port_led(hub, port1, HUB_LED_AUTO);
29691da177e4SLinus Torvalds 		hub->indicator[port1-1] = INDICATOR_AUTO;
29701da177e4SLinus Torvalds 	}
29711da177e4SLinus Torvalds 
29721da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
29731da177e4SLinus Torvalds 	/* during HNP, don't repeat the debounce */
29741da177e4SLinus Torvalds 	if (hdev->bus->is_b_host)
297524618b0cSAlan Stern 		portchange &= ~(USB_PORT_STAT_C_CONNECTION |
297624618b0cSAlan Stern 				USB_PORT_STAT_C_ENABLE);
29771da177e4SLinus Torvalds #endif
29781da177e4SLinus Torvalds 
29798808f00cSAlan Stern 	/* Try to resuscitate an existing device */
29808808f00cSAlan Stern 	udev = hdev->children[port1-1];
29818808f00cSAlan Stern 	if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
29828808f00cSAlan Stern 			udev->state != USB_STATE_NOTATTACHED) {
29838808f00cSAlan Stern 		usb_lock_device(udev);
29848808f00cSAlan Stern 		if (portstatus & USB_PORT_STAT_ENABLE) {
29858808f00cSAlan Stern 			status = 0;		/* Nothing to do */
29868808f00cSAlan Stern 
29878808f00cSAlan Stern #ifdef CONFIG_USB_SUSPEND
29885257d97aSAlan Stern 		} else if (udev->state == USB_STATE_SUSPENDED &&
29895257d97aSAlan Stern 				udev->persist_enabled) {
29908808f00cSAlan Stern 			/* For a suspended device, treat this as a
29918808f00cSAlan Stern 			 * remote wakeup event.
29928808f00cSAlan Stern 			 */
29930534d468SAlan Stern 			status = usb_remote_wakeup(udev);
29948808f00cSAlan Stern #endif
29958808f00cSAlan Stern 
29968808f00cSAlan Stern 		} else {
29975257d97aSAlan Stern 			status = -ENODEV;	/* Don't resuscitate */
29988808f00cSAlan Stern 		}
29998808f00cSAlan Stern 		usb_unlock_device(udev);
30008808f00cSAlan Stern 
30018808f00cSAlan Stern 		if (status == 0) {
30028808f00cSAlan Stern 			clear_bit(port1, hub->change_bits);
30038808f00cSAlan Stern 			return;
30048808f00cSAlan Stern 		}
30058808f00cSAlan Stern 	}
30068808f00cSAlan Stern 
300724618b0cSAlan Stern 	/* Disconnect any existing devices under this port */
30088808f00cSAlan Stern 	if (udev)
300924618b0cSAlan Stern 		usb_disconnect(&hdev->children[port1-1]);
301024618b0cSAlan Stern 	clear_bit(port1, hub->change_bits);
301124618b0cSAlan Stern 
3012253e0572SAlan Stern 	/* We can forget about a "removed" device when there's a physical
3013253e0572SAlan Stern 	 * disconnect or the connect status changes.
3014253e0572SAlan Stern 	 */
3015253e0572SAlan Stern 	if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3016253e0572SAlan Stern 			(portchange & USB_PORT_STAT_C_CONNECTION))
3017253e0572SAlan Stern 		clear_bit(port1, hub->removed_bits);
3018253e0572SAlan Stern 
30195257d97aSAlan Stern 	if (portchange & (USB_PORT_STAT_C_CONNECTION |
30205257d97aSAlan Stern 				USB_PORT_STAT_C_ENABLE)) {
30215257d97aSAlan Stern 		status = hub_port_debounce(hub, port1);
30225257d97aSAlan Stern 		if (status < 0) {
30235257d97aSAlan Stern 			if (printk_ratelimit())
30245257d97aSAlan Stern 				dev_err(hub_dev, "connect-debounce failed, "
30255257d97aSAlan Stern 						"port %d disabled\n", port1);
30265257d97aSAlan Stern 			portstatus &= ~USB_PORT_STAT_CONNECTION;
30275257d97aSAlan Stern 		} else {
30285257d97aSAlan Stern 			portstatus = status;
30295257d97aSAlan Stern 		}
30305257d97aSAlan Stern 	}
30315257d97aSAlan Stern 
3032253e0572SAlan Stern 	/* Return now if debouncing failed or nothing is connected or
3033253e0572SAlan Stern 	 * the device was "removed".
3034253e0572SAlan Stern 	 */
3035253e0572SAlan Stern 	if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3036253e0572SAlan Stern 			test_bit(port1, hub->removed_bits)) {
30371da177e4SLinus Torvalds 
30381da177e4SLinus Torvalds 		/* maybe switch power back on (e.g. root hub was reset) */
303974ad9bd2SGreg Kroah-Hartman 		if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
3040749da5f8SAlan Stern 				&& !(portstatus & USB_PORT_STAT_POWER))
30411da177e4SLinus Torvalds 			set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
30421da177e4SLinus Torvalds 
30431da177e4SLinus Torvalds 		if (portstatus & USB_PORT_STAT_ENABLE)
30441da177e4SLinus Torvalds   			goto done;
30451da177e4SLinus Torvalds 		return;
30461da177e4SLinus Torvalds 	}
30471da177e4SLinus Torvalds 
30481da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; i++) {
30491da177e4SLinus Torvalds 
30501da177e4SLinus Torvalds 		/* reallocate for each attempt, since references
30511da177e4SLinus Torvalds 		 * to the previous one can escape in various ways
30521da177e4SLinus Torvalds 		 */
30531da177e4SLinus Torvalds 		udev = usb_alloc_dev(hdev, hdev->bus, port1);
30541da177e4SLinus Torvalds 		if (!udev) {
30551da177e4SLinus Torvalds 			dev_err (hub_dev,
30561da177e4SLinus Torvalds 				"couldn't allocate port %d usb_device\n",
30571da177e4SLinus Torvalds 				port1);
30581da177e4SLinus Torvalds 			goto done;
30591da177e4SLinus Torvalds 		}
30601da177e4SLinus Torvalds 
30611da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_POWERED);
306255c52718SAlan Stern  		udev->bus_mA = hub->mA_per_port;
3063b6956ffaSAlan Stern 		udev->level = hdev->level + 1;
30648af548dcSInaky Perez-Gonzalez 		udev->wusb = hub_is_wusb(hub);
30651da177e4SLinus Torvalds 
3066e7b77172SSarah Sharp 		/*
3067e7b77172SSarah Sharp 		 * USB 3.0 devices are reset automatically before the connect
3068e7b77172SSarah Sharp 		 * port status change appears, and the root hub port status
3069e7b77172SSarah Sharp 		 * shows the correct speed.  We also get port change
3070e7b77172SSarah Sharp 		 * notifications for USB 3.0 devices from the USB 3.0 portion of
3071e7b77172SSarah Sharp 		 * an external USB 3.0 hub, but this isn't handled correctly yet
3072e7b77172SSarah Sharp 		 * FIXME.
3073e7b77172SSarah Sharp 		 */
3074e7b77172SSarah Sharp 
3075e7b77172SSarah Sharp 		if (!(hcd->driver->flags & HCD_USB3))
3076e7b77172SSarah Sharp 			udev->speed = USB_SPEED_UNKNOWN;
3077e7b77172SSarah Sharp 		else if ((hdev->parent == NULL) &&
3078288ead45SAlan Stern 				(portstatus & USB_PORT_STAT_SUPER_SPEED))
3079e7b77172SSarah Sharp 			udev->speed = USB_SPEED_SUPER;
3080e7b77172SSarah Sharp 		else
3081e7b77172SSarah Sharp 			udev->speed = USB_SPEED_UNKNOWN;
3082e7b77172SSarah Sharp 
3083c6515272SSarah Sharp 		/*
3084c6515272SSarah Sharp 		 * xHCI needs to issue an address device command later
3085c6515272SSarah Sharp 		 * in the hub_port_init sequence for SS/HS/FS/LS devices.
3086c6515272SSarah Sharp 		 */
3087c6515272SSarah Sharp 		if (!(hcd->driver->flags & HCD_USB3)) {
3088c6515272SSarah Sharp 			/* set the address */
3089c6515272SSarah Sharp 			choose_address(udev);
3090c6515272SSarah Sharp 			if (udev->devnum <= 0) {
3091c6515272SSarah Sharp 				status = -ENOTCONN;	/* Don't retry */
3092c6515272SSarah Sharp 				goto loop;
3093c6515272SSarah Sharp 			}
3094c6515272SSarah Sharp 		}
3095c6515272SSarah Sharp 
3096e7b77172SSarah Sharp 		/* reset (non-USB 3.0 devices) and get descriptor */
30971da177e4SLinus Torvalds 		status = hub_port_init(hub, udev, port1, i);
30981da177e4SLinus Torvalds 		if (status < 0)
30991da177e4SLinus Torvalds 			goto loop;
31001da177e4SLinus Torvalds 
31011da177e4SLinus Torvalds 		/* consecutive bus-powered hubs aren't reliable; they can
31021da177e4SLinus Torvalds 		 * violate the voltage drop budget.  if the new child has
31031da177e4SLinus Torvalds 		 * a "powered" LED, users should notice we didn't enable it
31041da177e4SLinus Torvalds 		 * (without reading syslog), even without per-port LEDs
31051da177e4SLinus Torvalds 		 * on the parent.
31061da177e4SLinus Torvalds 		 */
31071da177e4SLinus Torvalds 		if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
310855c52718SAlan Stern 				&& udev->bus_mA <= 100) {
31091da177e4SLinus Torvalds 			u16	devstat;
31101da177e4SLinus Torvalds 
31111da177e4SLinus Torvalds 			status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
31121da177e4SLinus Torvalds 					&devstat);
311355c52718SAlan Stern 			if (status < 2) {
31141da177e4SLinus Torvalds 				dev_dbg(&udev->dev, "get status %d ?\n", status);
31151da177e4SLinus Torvalds 				goto loop_disable;
31161da177e4SLinus Torvalds 			}
311755c52718SAlan Stern 			le16_to_cpus(&devstat);
31181da177e4SLinus Torvalds 			if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
31191da177e4SLinus Torvalds 				dev_err(&udev->dev,
31201da177e4SLinus Torvalds 					"can't connect bus-powered hub "
31211da177e4SLinus Torvalds 					"to this port\n");
31221da177e4SLinus Torvalds 				if (hub->has_indicators) {
31231da177e4SLinus Torvalds 					hub->indicator[port1-1] =
31241da177e4SLinus Torvalds 						INDICATOR_AMBER_BLINK;
3125c4028958SDavid Howells 					schedule_delayed_work (&hub->leds, 0);
31261da177e4SLinus Torvalds 				}
31271da177e4SLinus Torvalds 				status = -ENOTCONN;	/* Don't retry */
31281da177e4SLinus Torvalds 				goto loop_disable;
31291da177e4SLinus Torvalds 			}
31301da177e4SLinus Torvalds 		}
31311da177e4SLinus Torvalds 
31321da177e4SLinus Torvalds 		/* check for devices running slower than they could */
31331da177e4SLinus Torvalds 		if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
31341da177e4SLinus Torvalds 				&& udev->speed == USB_SPEED_FULL
31351da177e4SLinus Torvalds 				&& highspeed_hubs != 0)
31361da177e4SLinus Torvalds 			check_highspeed (hub, udev, port1);
31371da177e4SLinus Torvalds 
31381da177e4SLinus Torvalds 		/* Store the parent's children[] pointer.  At this point
31391da177e4SLinus Torvalds 		 * udev becomes globally accessible, although presumably
31401da177e4SLinus Torvalds 		 * no one will look at it until hdev is unlocked.
31411da177e4SLinus Torvalds 		 */
31421da177e4SLinus Torvalds 		status = 0;
31431da177e4SLinus Torvalds 
31441da177e4SLinus Torvalds 		/* We mustn't add new devices if the parent hub has
31451da177e4SLinus Torvalds 		 * been disconnected; we would race with the
31461da177e4SLinus Torvalds 		 * recursively_mark_NOTATTACHED() routine.
31471da177e4SLinus Torvalds 		 */
31481da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
31491da177e4SLinus Torvalds 		if (hdev->state == USB_STATE_NOTATTACHED)
31501da177e4SLinus Torvalds 			status = -ENOTCONN;
31511da177e4SLinus Torvalds 		else
31521da177e4SLinus Torvalds 			hdev->children[port1-1] = udev;
31531da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
31541da177e4SLinus Torvalds 
31551da177e4SLinus Torvalds 		/* Run it through the hoops (find a driver, etc) */
31561da177e4SLinus Torvalds 		if (!status) {
31571da177e4SLinus Torvalds 			status = usb_new_device(udev);
31581da177e4SLinus Torvalds 			if (status) {
31591da177e4SLinus Torvalds 				spin_lock_irq(&device_state_lock);
31601da177e4SLinus Torvalds 				hdev->children[port1-1] = NULL;
31611da177e4SLinus Torvalds 				spin_unlock_irq(&device_state_lock);
31621da177e4SLinus Torvalds 			}
31631da177e4SLinus Torvalds 		}
31641da177e4SLinus Torvalds 
31651da177e4SLinus Torvalds 		if (status)
31661da177e4SLinus Torvalds 			goto loop_disable;
31671da177e4SLinus Torvalds 
31681da177e4SLinus Torvalds 		status = hub_power_remaining(hub);
31691da177e4SLinus Torvalds 		if (status)
317055c52718SAlan Stern 			dev_dbg(hub_dev, "%dmA power budget left\n", status);
31711da177e4SLinus Torvalds 
31721da177e4SLinus Torvalds 		return;
31731da177e4SLinus Torvalds 
31741da177e4SLinus Torvalds loop_disable:
31751da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 1);
31761da177e4SLinus Torvalds loop:
3177fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
31781da177e4SLinus Torvalds 		release_address(udev);
3179f7410cedSHerbert Xu 		hub_free_dev(udev);
31801da177e4SLinus Torvalds 		usb_put_dev(udev);
3181ffcdc18dSVikram Pandita 		if ((status == -ENOTCONN) || (status == -ENOTSUPP))
31821da177e4SLinus Torvalds 			break;
31831da177e4SLinus Torvalds 	}
31843a31155cSAlan Stern 	if (hub->hdev->parent ||
31853a31155cSAlan Stern 			!hcd->driver->port_handed_over ||
31863a31155cSAlan Stern 			!(hcd->driver->port_handed_over)(hcd, port1))
31873a31155cSAlan Stern 		dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
31883a31155cSAlan Stern 				port1);
31891da177e4SLinus Torvalds 
31901da177e4SLinus Torvalds done:
31911da177e4SLinus Torvalds 	hub_port_disable(hub, port1, 1);
319290da096eSBalaji Rao 	if (hcd->driver->relinquish_port && !hub->hdev->parent)
319390da096eSBalaji Rao 		hcd->driver->relinquish_port(hcd, port1);
31941da177e4SLinus Torvalds }
31951da177e4SLinus Torvalds 
31961da177e4SLinus Torvalds static void hub_events(void)
31971da177e4SLinus Torvalds {
31981da177e4SLinus Torvalds 	struct list_head *tmp;
31991da177e4SLinus Torvalds 	struct usb_device *hdev;
32001da177e4SLinus Torvalds 	struct usb_interface *intf;
32011da177e4SLinus Torvalds 	struct usb_hub *hub;
32021da177e4SLinus Torvalds 	struct device *hub_dev;
32031da177e4SLinus Torvalds 	u16 hubstatus;
32041da177e4SLinus Torvalds 	u16 hubchange;
32051da177e4SLinus Torvalds 	u16 portstatus;
32061da177e4SLinus Torvalds 	u16 portchange;
32071da177e4SLinus Torvalds 	int i, ret;
32081da177e4SLinus Torvalds 	int connect_change;
32091da177e4SLinus Torvalds 
32101da177e4SLinus Torvalds 	/*
32111da177e4SLinus Torvalds 	 *  We restart the list every time to avoid a deadlock with
32121da177e4SLinus Torvalds 	 * deleting hubs downstream from this one. This should be
32131da177e4SLinus Torvalds 	 * safe since we delete the hub from the event list.
32141da177e4SLinus Torvalds 	 * Not the most efficient, but avoids deadlocks.
32151da177e4SLinus Torvalds 	 */
32161da177e4SLinus Torvalds 	while (1) {
32171da177e4SLinus Torvalds 
32181da177e4SLinus Torvalds 		/* Grab the first entry at the beginning of the list */
32191da177e4SLinus Torvalds 		spin_lock_irq(&hub_event_lock);
32201da177e4SLinus Torvalds 		if (list_empty(&hub_event_list)) {
32211da177e4SLinus Torvalds 			spin_unlock_irq(&hub_event_lock);
32221da177e4SLinus Torvalds 			break;
32231da177e4SLinus Torvalds 		}
32241da177e4SLinus Torvalds 
32251da177e4SLinus Torvalds 		tmp = hub_event_list.next;
32261da177e4SLinus Torvalds 		list_del_init(tmp);
32271da177e4SLinus Torvalds 
32281da177e4SLinus Torvalds 		hub = list_entry(tmp, struct usb_hub, event_list);
3229e8054854SAlan Stern 		kref_get(&hub->kref);
3230e8054854SAlan Stern 		spin_unlock_irq(&hub_event_lock);
32311da177e4SLinus Torvalds 
3232e8054854SAlan Stern 		hdev = hub->hdev;
3233e8054854SAlan Stern 		hub_dev = hub->intfdev;
3234e8054854SAlan Stern 		intf = to_usb_interface(hub_dev);
323540f122f3SAlan Stern 		dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
32361da177e4SLinus Torvalds 				hdev->state, hub->descriptor
32371da177e4SLinus Torvalds 					? hub->descriptor->bNbrPorts
32381da177e4SLinus Torvalds 					: 0,
32391da177e4SLinus Torvalds 				/* NOTE: expects max 15 ports... */
32401da177e4SLinus Torvalds 				(u16) hub->change_bits[0],
324140f122f3SAlan Stern 				(u16) hub->event_bits[0]);
32421da177e4SLinus Torvalds 
32431da177e4SLinus Torvalds 		/* Lock the device, then check to see if we were
32441da177e4SLinus Torvalds 		 * disconnected while waiting for the lock to succeed. */
324506b84e8aSAlan Stern 		usb_lock_device(hdev);
3246e8054854SAlan Stern 		if (unlikely(hub->disconnected))
32479bbdf1e0SAlan Stern 			goto loop_disconnected;
32481da177e4SLinus Torvalds 
32491da177e4SLinus Torvalds 		/* If the hub has died, clean up after it */
32501da177e4SLinus Torvalds 		if (hdev->state == USB_STATE_NOTATTACHED) {
32517de18d8bSAlan Stern 			hub->error = -ENODEV;
32524330354fSAlan Stern 			hub_quiesce(hub, HUB_DISCONNECT);
32531da177e4SLinus Torvalds 			goto loop;
32541da177e4SLinus Torvalds 		}
32551da177e4SLinus Torvalds 
325640f122f3SAlan Stern 		/* Autoresume */
325740f122f3SAlan Stern 		ret = usb_autopm_get_interface(intf);
325840f122f3SAlan Stern 		if (ret) {
325940f122f3SAlan Stern 			dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
32601da177e4SLinus Torvalds 			goto loop;
326140f122f3SAlan Stern 		}
326240f122f3SAlan Stern 
326340f122f3SAlan Stern 		/* If this is an inactive hub, do nothing */
326440f122f3SAlan Stern 		if (hub->quiescing)
326540f122f3SAlan Stern 			goto loop_autopm;
32661da177e4SLinus Torvalds 
32671da177e4SLinus Torvalds 		if (hub->error) {
32681da177e4SLinus Torvalds 			dev_dbg (hub_dev, "resetting for error %d\n",
32691da177e4SLinus Torvalds 				hub->error);
32701da177e4SLinus Torvalds 
3271742120c6SMing Lei 			ret = usb_reset_device(hdev);
32721da177e4SLinus Torvalds 			if (ret) {
32731da177e4SLinus Torvalds 				dev_dbg (hub_dev,
32741da177e4SLinus Torvalds 					"error resetting hub: %d\n", ret);
327540f122f3SAlan Stern 				goto loop_autopm;
32761da177e4SLinus Torvalds 			}
32771da177e4SLinus Torvalds 
32781da177e4SLinus Torvalds 			hub->nerrors = 0;
32791da177e4SLinus Torvalds 			hub->error = 0;
32801da177e4SLinus Torvalds 		}
32811da177e4SLinus Torvalds 
32821da177e4SLinus Torvalds 		/* deal with port status changes */
32831da177e4SLinus Torvalds 		for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
32841da177e4SLinus Torvalds 			if (test_bit(i, hub->busy_bits))
32851da177e4SLinus Torvalds 				continue;
32861da177e4SLinus Torvalds 			connect_change = test_bit(i, hub->change_bits);
32871da177e4SLinus Torvalds 			if (!test_and_clear_bit(i, hub->event_bits) &&
32886ee0b270SAlan Stern 					!connect_change)
32891da177e4SLinus Torvalds 				continue;
32901da177e4SLinus Torvalds 
32911da177e4SLinus Torvalds 			ret = hub_port_status(hub, i,
32921da177e4SLinus Torvalds 					&portstatus, &portchange);
32931da177e4SLinus Torvalds 			if (ret < 0)
32941da177e4SLinus Torvalds 				continue;
32951da177e4SLinus Torvalds 
32961da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_CONNECTION) {
32971da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
32981da177e4SLinus Torvalds 					USB_PORT_FEAT_C_CONNECTION);
32991da177e4SLinus Torvalds 				connect_change = 1;
33001da177e4SLinus Torvalds 			}
33011da177e4SLinus Torvalds 
33021da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_ENABLE) {
33031da177e4SLinus Torvalds 				if (!connect_change)
33041da177e4SLinus Torvalds 					dev_dbg (hub_dev,
33051da177e4SLinus Torvalds 						"port %d enable change, "
33061da177e4SLinus Torvalds 						"status %08x\n",
33071da177e4SLinus Torvalds 						i, portstatus);
33081da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
33091da177e4SLinus Torvalds 					USB_PORT_FEAT_C_ENABLE);
33101da177e4SLinus Torvalds 
33111da177e4SLinus Torvalds 				/*
33121da177e4SLinus Torvalds 				 * EM interference sometimes causes badly
33131da177e4SLinus Torvalds 				 * shielded USB devices to be shutdown by
33141da177e4SLinus Torvalds 				 * the hub, this hack enables them again.
33151da177e4SLinus Torvalds 				 * Works at least with mouse driver.
33161da177e4SLinus Torvalds 				 */
33171da177e4SLinus Torvalds 				if (!(portstatus & USB_PORT_STAT_ENABLE)
33181da177e4SLinus Torvalds 				    && !connect_change
33191da177e4SLinus Torvalds 				    && hdev->children[i-1]) {
33201da177e4SLinus Torvalds 					dev_err (hub_dev,
33211da177e4SLinus Torvalds 					    "port %i "
33221da177e4SLinus Torvalds 					    "disabled by hub (EMI?), "
33231da177e4SLinus Torvalds 					    "re-enabling...\n",
33241da177e4SLinus Torvalds 						i);
33251da177e4SLinus Torvalds 					connect_change = 1;
33261da177e4SLinus Torvalds 				}
33271da177e4SLinus Torvalds 			}
33281da177e4SLinus Torvalds 
33291da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_SUSPEND) {
33308808f00cSAlan Stern 				struct usb_device *udev;
33318808f00cSAlan Stern 
33321da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
33331da177e4SLinus Torvalds 					USB_PORT_FEAT_C_SUSPEND);
33348808f00cSAlan Stern 				udev = hdev->children[i-1];
33358808f00cSAlan Stern 				if (udev) {
333649d0f078SAlan Stern 					/* TRSMRCY = 10 msec */
333749d0f078SAlan Stern 					msleep(10);
333849d0f078SAlan Stern 
33398808f00cSAlan Stern 					usb_lock_device(udev);
33400534d468SAlan Stern 					ret = usb_remote_wakeup(hdev->
33411da177e4SLinus Torvalds 							children[i-1]);
33428808f00cSAlan Stern 					usb_unlock_device(udev);
33431da177e4SLinus Torvalds 					if (ret < 0)
33441da177e4SLinus Torvalds 						connect_change = 1;
33451da177e4SLinus Torvalds 				} else {
33461da177e4SLinus Torvalds 					ret = -ENODEV;
33471da177e4SLinus Torvalds 					hub_port_disable(hub, i, 1);
33481da177e4SLinus Torvalds 				}
33491da177e4SLinus Torvalds 				dev_dbg (hub_dev,
33501da177e4SLinus Torvalds 					"resume on port %d, status %d\n",
33511da177e4SLinus Torvalds 					i, ret);
33521da177e4SLinus Torvalds 			}
33531da177e4SLinus Torvalds 
33541da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
33551da177e4SLinus Torvalds 				dev_err (hub_dev,
33561da177e4SLinus Torvalds 					"over-current change on port %d\n",
33571da177e4SLinus Torvalds 					i);
33581da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
33591da177e4SLinus Torvalds 					USB_PORT_FEAT_C_OVER_CURRENT);
33608520f380SAlan Stern 				hub_power_on(hub, true);
33611da177e4SLinus Torvalds 			}
33621da177e4SLinus Torvalds 
33631da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_RESET) {
33641da177e4SLinus Torvalds 				dev_dbg (hub_dev,
33651da177e4SLinus Torvalds 					"reset change on port %d\n",
33661da177e4SLinus Torvalds 					i);
33671da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
33681da177e4SLinus Torvalds 					USB_PORT_FEAT_C_RESET);
33691da177e4SLinus Torvalds 			}
33701da177e4SLinus Torvalds 
33711da177e4SLinus Torvalds 			if (connect_change)
33721da177e4SLinus Torvalds 				hub_port_connect_change(hub, i,
33731da177e4SLinus Torvalds 						portstatus, portchange);
33741da177e4SLinus Torvalds 		} /* end for i */
33751da177e4SLinus Torvalds 
33761da177e4SLinus Torvalds 		/* deal with hub status changes */
33771da177e4SLinus Torvalds 		if (test_and_clear_bit(0, hub->event_bits) == 0)
33781da177e4SLinus Torvalds 			;	/* do nothing */
33791da177e4SLinus Torvalds 		else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
33801da177e4SLinus Torvalds 			dev_err (hub_dev, "get_hub_status failed\n");
33811da177e4SLinus Torvalds 		else {
33821da177e4SLinus Torvalds 			if (hubchange & HUB_CHANGE_LOCAL_POWER) {
33831da177e4SLinus Torvalds 				dev_dbg (hub_dev, "power change\n");
33841da177e4SLinus Torvalds 				clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
338555c52718SAlan Stern 				if (hubstatus & HUB_STATUS_LOCAL_POWER)
338655c52718SAlan Stern 					/* FIXME: Is this always true? */
338755c52718SAlan Stern 					hub->limited_power = 1;
3388403fae78Sjidong xiao 				else
3389403fae78Sjidong xiao 					hub->limited_power = 0;
33901da177e4SLinus Torvalds 			}
33911da177e4SLinus Torvalds 			if (hubchange & HUB_CHANGE_OVERCURRENT) {
33921da177e4SLinus Torvalds 				dev_dbg (hub_dev, "overcurrent change\n");
33931da177e4SLinus Torvalds 				msleep(500);	/* Cool down */
33941da177e4SLinus Torvalds 				clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
33958520f380SAlan Stern                         	hub_power_on(hub, true);
33961da177e4SLinus Torvalds 			}
33971da177e4SLinus Torvalds 		}
33981da177e4SLinus Torvalds 
339940f122f3SAlan Stern  loop_autopm:
34008e4ceb38SAlan Stern 		/* Balance the usb_autopm_get_interface() above */
34018e4ceb38SAlan Stern 		usb_autopm_put_interface_no_suspend(intf);
34021da177e4SLinus Torvalds  loop:
34038e4ceb38SAlan Stern 		/* Balance the usb_autopm_get_interface_no_resume() in
34048e4ceb38SAlan Stern 		 * kick_khubd() and allow autosuspend.
34058e4ceb38SAlan Stern 		 */
34068e4ceb38SAlan Stern 		usb_autopm_put_interface(intf);
34079bbdf1e0SAlan Stern  loop_disconnected:
34081da177e4SLinus Torvalds 		usb_unlock_device(hdev);
3409e8054854SAlan Stern 		kref_put(&hub->kref, hub_release);
34101da177e4SLinus Torvalds 
34111da177e4SLinus Torvalds         } /* end while (1) */
34121da177e4SLinus Torvalds }
34131da177e4SLinus Torvalds 
34141da177e4SLinus Torvalds static int hub_thread(void *__unused)
34151da177e4SLinus Torvalds {
34163bb1af52SAlan Stern 	/* khubd needs to be freezable to avoid intefering with USB-PERSIST
34173bb1af52SAlan Stern 	 * port handover.  Otherwise it might see that a full-speed device
34183bb1af52SAlan Stern 	 * was gone before the EHCI controller had handed its port over to
34193bb1af52SAlan Stern 	 * the companion full-speed controller.
34203bb1af52SAlan Stern 	 */
342183144186SRafael J. Wysocki 	set_freezable();
34223bb1af52SAlan Stern 
34231da177e4SLinus Torvalds 	do {
34241da177e4SLinus Torvalds 		hub_events();
3425e42837bcSRafael J. Wysocki 		wait_event_freezable(khubd_wait,
34269c8d6178Sakpm@osdl.org 				!list_empty(&hub_event_list) ||
34279c8d6178Sakpm@osdl.org 				kthread_should_stop());
34289c8d6178Sakpm@osdl.org 	} while (!kthread_should_stop() || !list_empty(&hub_event_list));
34291da177e4SLinus Torvalds 
34301da177e4SLinus Torvalds 	pr_debug("%s: khubd exiting\n", usbcore_name);
34319c8d6178Sakpm@osdl.org 	return 0;
34321da177e4SLinus Torvalds }
34331da177e4SLinus Torvalds 
34341e927d96SNémeth Márton static const struct usb_device_id hub_id_table[] = {
34351da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
34361da177e4SLinus Torvalds       .bDeviceClass = USB_CLASS_HUB},
34371da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
34381da177e4SLinus Torvalds       .bInterfaceClass = USB_CLASS_HUB},
34391da177e4SLinus Torvalds     { }						/* Terminating entry */
34401da177e4SLinus Torvalds };
34411da177e4SLinus Torvalds 
34421da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, hub_id_table);
34431da177e4SLinus Torvalds 
34441da177e4SLinus Torvalds static struct usb_driver hub_driver = {
34451da177e4SLinus Torvalds 	.name =		"hub",
34461da177e4SLinus Torvalds 	.probe =	hub_probe,
34471da177e4SLinus Torvalds 	.disconnect =	hub_disconnect,
34481da177e4SLinus Torvalds 	.suspend =	hub_suspend,
34491da177e4SLinus Torvalds 	.resume =	hub_resume,
3450f07600cfSAlan Stern 	.reset_resume =	hub_reset_resume,
34517de18d8bSAlan Stern 	.pre_reset =	hub_pre_reset,
34527de18d8bSAlan Stern 	.post_reset =	hub_post_reset,
34531da177e4SLinus Torvalds 	.ioctl =	hub_ioctl,
34541da177e4SLinus Torvalds 	.id_table =	hub_id_table,
345540f122f3SAlan Stern 	.supports_autosuspend =	1,
34561da177e4SLinus Torvalds };
34571da177e4SLinus Torvalds 
34581da177e4SLinus Torvalds int usb_hub_init(void)
34591da177e4SLinus Torvalds {
34601da177e4SLinus Torvalds 	if (usb_register(&hub_driver) < 0) {
34611da177e4SLinus Torvalds 		printk(KERN_ERR "%s: can't register hub driver\n",
34621da177e4SLinus Torvalds 			usbcore_name);
34631da177e4SLinus Torvalds 		return -1;
34641da177e4SLinus Torvalds 	}
34651da177e4SLinus Torvalds 
34669c8d6178Sakpm@osdl.org 	khubd_task = kthread_run(hub_thread, NULL, "khubd");
34679c8d6178Sakpm@osdl.org 	if (!IS_ERR(khubd_task))
34681da177e4SLinus Torvalds 		return 0;
34691da177e4SLinus Torvalds 
34701da177e4SLinus Torvalds 	/* Fall through if kernel_thread failed */
34711da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
34721da177e4SLinus Torvalds 	printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
34731da177e4SLinus Torvalds 
34741da177e4SLinus Torvalds 	return -1;
34751da177e4SLinus Torvalds }
34761da177e4SLinus Torvalds 
34771da177e4SLinus Torvalds void usb_hub_cleanup(void)
34781da177e4SLinus Torvalds {
34799c8d6178Sakpm@osdl.org 	kthread_stop(khubd_task);
34801da177e4SLinus Torvalds 
34811da177e4SLinus Torvalds 	/*
34821da177e4SLinus Torvalds 	 * Hub resources are freed for us by usb_deregister. It calls
34831da177e4SLinus Torvalds 	 * usb_driver_purge on every device which in turn calls that
34841da177e4SLinus Torvalds 	 * devices disconnect function if it is using this driver.
34851da177e4SLinus Torvalds 	 * The hub_disconnect function takes care of releasing the
34861da177e4SLinus Torvalds 	 * individual hub resources. -greg
34871da177e4SLinus Torvalds 	 */
34881da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
34891da177e4SLinus Torvalds } /* usb_hub_cleanup() */
34901da177e4SLinus Torvalds 
3491eb764c4bSAlan Stern static int descriptors_changed(struct usb_device *udev,
3492eb764c4bSAlan Stern 		struct usb_device_descriptor *old_device_descriptor)
34931da177e4SLinus Torvalds {
3494eb764c4bSAlan Stern 	int		changed = 0;
34951da177e4SLinus Torvalds 	unsigned	index;
3496eb764c4bSAlan Stern 	unsigned	serial_len = 0;
3497eb764c4bSAlan Stern 	unsigned	len;
3498eb764c4bSAlan Stern 	unsigned	old_length;
3499eb764c4bSAlan Stern 	int		length;
3500eb764c4bSAlan Stern 	char		*buf;
35011da177e4SLinus Torvalds 
3502eb764c4bSAlan Stern 	if (memcmp(&udev->descriptor, old_device_descriptor,
3503eb764c4bSAlan Stern 			sizeof(*old_device_descriptor)) != 0)
3504eb764c4bSAlan Stern 		return 1;
3505eb764c4bSAlan Stern 
3506eb764c4bSAlan Stern 	/* Since the idVendor, idProduct, and bcdDevice values in the
3507eb764c4bSAlan Stern 	 * device descriptor haven't changed, we will assume the
3508eb764c4bSAlan Stern 	 * Manufacturer and Product strings haven't changed either.
3509eb764c4bSAlan Stern 	 * But the SerialNumber string could be different (e.g., a
3510eb764c4bSAlan Stern 	 * different flash card of the same brand).
3511eb764c4bSAlan Stern 	 */
3512eb764c4bSAlan Stern 	if (udev->serial)
3513eb764c4bSAlan Stern 		serial_len = strlen(udev->serial) + 1;
3514eb764c4bSAlan Stern 
3515eb764c4bSAlan Stern 	len = serial_len;
35161da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
3517eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
3518eb764c4bSAlan Stern 		len = max(len, old_length);
35191da177e4SLinus Torvalds 	}
3520eb764c4bSAlan Stern 
35210cc1a51fSOliver Neukum 	buf = kmalloc(len, GFP_NOIO);
35221da177e4SLinus Torvalds 	if (buf == NULL) {
35231da177e4SLinus Torvalds 		dev_err(&udev->dev, "no mem to re-read configs after reset\n");
35241da177e4SLinus Torvalds 		/* assume the worst */
35251da177e4SLinus Torvalds 		return 1;
35261da177e4SLinus Torvalds 	}
35271da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
3528eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
35291da177e4SLinus Torvalds 		length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
35301da177e4SLinus Torvalds 				old_length);
3531eb764c4bSAlan Stern 		if (length != old_length) {
35321da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d, error %d\n",
35331da177e4SLinus Torvalds 					index, length);
3534eb764c4bSAlan Stern 			changed = 1;
35351da177e4SLinus Torvalds 			break;
35361da177e4SLinus Torvalds 		}
35371da177e4SLinus Torvalds 		if (memcmp (buf, udev->rawdescriptors[index], old_length)
35381da177e4SLinus Torvalds 				!= 0) {
35391da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
3540eb764c4bSAlan Stern 				index,
3541eb764c4bSAlan Stern 				((struct usb_config_descriptor *) buf)->
3542eb764c4bSAlan Stern 					bConfigurationValue);
3543eb764c4bSAlan Stern 			changed = 1;
35441da177e4SLinus Torvalds 			break;
35451da177e4SLinus Torvalds 		}
35461da177e4SLinus Torvalds 	}
3547eb764c4bSAlan Stern 
3548eb764c4bSAlan Stern 	if (!changed && serial_len) {
3549eb764c4bSAlan Stern 		length = usb_string(udev, udev->descriptor.iSerialNumber,
3550eb764c4bSAlan Stern 				buf, serial_len);
3551eb764c4bSAlan Stern 		if (length + 1 != serial_len) {
3552eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string error %d\n",
3553eb764c4bSAlan Stern 					length);
3554eb764c4bSAlan Stern 			changed = 1;
3555eb764c4bSAlan Stern 		} else if (memcmp(buf, udev->serial, length) != 0) {
3556eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string changed\n");
3557eb764c4bSAlan Stern 			changed = 1;
3558eb764c4bSAlan Stern 		}
3559eb764c4bSAlan Stern 	}
3560eb764c4bSAlan Stern 
35611da177e4SLinus Torvalds 	kfree(buf);
3562eb764c4bSAlan Stern 	return changed;
35631da177e4SLinus Torvalds }
35641da177e4SLinus Torvalds 
35651da177e4SLinus Torvalds /**
3566742120c6SMing Lei  * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
35671da177e4SLinus Torvalds  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
35681da177e4SLinus Torvalds  *
356979efa097SAlan Stern  * WARNING - don't use this routine to reset a composite device
357079efa097SAlan Stern  * (one with multiple interfaces owned by separate drivers)!
3571742120c6SMing Lei  * Use usb_reset_device() instead.
35721da177e4SLinus Torvalds  *
35731da177e4SLinus Torvalds  * Do a port reset, reassign the device's address, and establish its
35741da177e4SLinus Torvalds  * former operating configuration.  If the reset fails, or the device's
35751da177e4SLinus Torvalds  * descriptors change from their values before the reset, or the original
35761da177e4SLinus Torvalds  * configuration and altsettings cannot be restored, a flag will be set
35771da177e4SLinus Torvalds  * telling khubd to pretend the device has been disconnected and then
35781da177e4SLinus Torvalds  * re-connected.  All drivers will be unbound, and the device will be
35791da177e4SLinus Torvalds  * re-enumerated and probed all over again.
35801da177e4SLinus Torvalds  *
35811da177e4SLinus Torvalds  * Returns 0 if the reset succeeded, -ENODEV if the device has been
35821da177e4SLinus Torvalds  * flagged for logical disconnection, or some other negative error code
35831da177e4SLinus Torvalds  * if the reset wasn't even attempted.
35841da177e4SLinus Torvalds  *
35851da177e4SLinus Torvalds  * The caller must own the device lock.  For example, it's safe to use
35861da177e4SLinus Torvalds  * this from a driver probe() routine after downloading new firmware.
35871da177e4SLinus Torvalds  * For calls that might not occur during probe(), drivers should lock
35881da177e4SLinus Torvalds  * the device using usb_lock_device_for_reset().
35896bc6cff5SAlan Stern  *
35906bc6cff5SAlan Stern  * Locking exception: This routine may also be called from within an
35916bc6cff5SAlan Stern  * autoresume handler.  Such usage won't conflict with other tasks
35926bc6cff5SAlan Stern  * holding the device lock because these tasks should always call
35936bc6cff5SAlan Stern  * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
35941da177e4SLinus Torvalds  */
3595742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev)
35961da177e4SLinus Torvalds {
35971da177e4SLinus Torvalds 	struct usb_device		*parent_hdev = udev->parent;
35981da177e4SLinus Torvalds 	struct usb_hub			*parent_hub;
35993f0479e0SSarah Sharp 	struct usb_hcd			*hcd = bus_to_hcd(udev->bus);
36001da177e4SLinus Torvalds 	struct usb_device_descriptor	descriptor = udev->descriptor;
360112c3da34SAlan Stern 	int 				i, ret = 0;
360212c3da34SAlan Stern 	int				port1 = udev->portnum;
36031da177e4SLinus Torvalds 
36041da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED ||
36051da177e4SLinus Torvalds 			udev->state == USB_STATE_SUSPENDED) {
36061da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
36071da177e4SLinus Torvalds 				udev->state);
36081da177e4SLinus Torvalds 		return -EINVAL;
36091da177e4SLinus Torvalds 	}
36101da177e4SLinus Torvalds 
36111da177e4SLinus Torvalds 	if (!parent_hdev) {
36121da177e4SLinus Torvalds 		/* this requires hcd-specific logic; see OHCI hc_restart() */
3613441b62c1SHarvey Harrison 		dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
36141da177e4SLinus Torvalds 		return -EISDIR;
36151da177e4SLinus Torvalds 	}
36161da177e4SLinus Torvalds 	parent_hub = hdev_to_hub(parent_hdev);
36171da177e4SLinus Torvalds 
36181da177e4SLinus Torvalds 	set_bit(port1, parent_hub->busy_bits);
36191da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; ++i) {
36201da177e4SLinus Torvalds 
36211da177e4SLinus Torvalds 		/* ep0 maxpacket size may change; let the HCD know about it.
36221da177e4SLinus Torvalds 		 * Other endpoints will be handled by re-enumeration. */
3623fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
36241da177e4SLinus Torvalds 		ret = hub_port_init(parent_hub, udev, port1, i);
3625dd4dd19eSAlan Stern 		if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
36261da177e4SLinus Torvalds 			break;
36271da177e4SLinus Torvalds 	}
36281da177e4SLinus Torvalds 	clear_bit(port1, parent_hub->busy_bits);
3629d5cbad4bSAlan Stern 
36301da177e4SLinus Torvalds 	if (ret < 0)
36311da177e4SLinus Torvalds 		goto re_enumerate;
36321da177e4SLinus Torvalds 
36331da177e4SLinus Torvalds 	/* Device might have changed firmware (DFU or similar) */
3634eb764c4bSAlan Stern 	if (descriptors_changed(udev, &descriptor)) {
36351da177e4SLinus Torvalds 		dev_info(&udev->dev, "device firmware changed\n");
36361da177e4SLinus Torvalds 		udev->descriptor = descriptor;	/* for disconnect() calls */
36371da177e4SLinus Torvalds 		goto re_enumerate;
36381da177e4SLinus Torvalds   	}
36391da177e4SLinus Torvalds 
36404fe0387aSAlan Stern 	/* Restore the device's previous configuration */
36411da177e4SLinus Torvalds 	if (!udev->actconfig)
36421da177e4SLinus Torvalds 		goto done;
36433f0479e0SSarah Sharp 
36443f0479e0SSarah Sharp 	mutex_lock(&hcd->bandwidth_mutex);
36453f0479e0SSarah Sharp 	ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
36463f0479e0SSarah Sharp 	if (ret < 0) {
36473f0479e0SSarah Sharp 		dev_warn(&udev->dev,
36483f0479e0SSarah Sharp 				"Busted HC?  Not enough HCD resources for "
36493f0479e0SSarah Sharp 				"old configuration.\n");
36503f0479e0SSarah Sharp 		mutex_unlock(&hcd->bandwidth_mutex);
36513f0479e0SSarah Sharp 		goto re_enumerate;
36523f0479e0SSarah Sharp 	}
36531da177e4SLinus Torvalds 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
36541da177e4SLinus Torvalds 			USB_REQ_SET_CONFIGURATION, 0,
36551da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, 0,
36561da177e4SLinus Torvalds 			NULL, 0, USB_CTRL_SET_TIMEOUT);
36571da177e4SLinus Torvalds 	if (ret < 0) {
36581da177e4SLinus Torvalds 		dev_err(&udev->dev,
36591da177e4SLinus Torvalds 			"can't restore configuration #%d (error=%d)\n",
36601da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, ret);
36613f0479e0SSarah Sharp 		mutex_unlock(&hcd->bandwidth_mutex);
36621da177e4SLinus Torvalds 		goto re_enumerate;
36631da177e4SLinus Torvalds   	}
36643f0479e0SSarah Sharp 	mutex_unlock(&hcd->bandwidth_mutex);
36651da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_CONFIGURED);
36661da177e4SLinus Torvalds 
36674fe0387aSAlan Stern 	/* Put interfaces back into the same altsettings as before.
36684fe0387aSAlan Stern 	 * Don't bother to send the Set-Interface request for interfaces
36694fe0387aSAlan Stern 	 * that were already in altsetting 0; besides being unnecessary,
36704fe0387aSAlan Stern 	 * many devices can't handle it.  Instead just reset the host-side
36714fe0387aSAlan Stern 	 * endpoint state.
36724fe0387aSAlan Stern 	 */
36731da177e4SLinus Torvalds 	for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
36743f0479e0SSarah Sharp 		struct usb_host_config *config = udev->actconfig;
36753f0479e0SSarah Sharp 		struct usb_interface *intf = config->interface[i];
36761da177e4SLinus Torvalds 		struct usb_interface_descriptor *desc;
36771da177e4SLinus Torvalds 
36781da177e4SLinus Torvalds 		desc = &intf->cur_altsetting->desc;
36794fe0387aSAlan Stern 		if (desc->bAlternateSetting == 0) {
36804fe0387aSAlan Stern 			usb_disable_interface(udev, intf, true);
36814fe0387aSAlan Stern 			usb_enable_interface(udev, intf, true);
36824fe0387aSAlan Stern 			ret = 0;
36834fe0387aSAlan Stern 		} else {
368404a723eaSSarah Sharp 			/* Let the bandwidth allocation function know that this
368504a723eaSSarah Sharp 			 * device has been reset, and it will have to use
368604a723eaSSarah Sharp 			 * alternate setting 0 as the current alternate setting.
36873f0479e0SSarah Sharp 			 */
368804a723eaSSarah Sharp 			intf->resetting_device = 1;
36891da177e4SLinus Torvalds 			ret = usb_set_interface(udev, desc->bInterfaceNumber,
36901da177e4SLinus Torvalds 					desc->bAlternateSetting);
369104a723eaSSarah Sharp 			intf->resetting_device = 0;
36924fe0387aSAlan Stern 		}
36931da177e4SLinus Torvalds 		if (ret < 0) {
36941da177e4SLinus Torvalds 			dev_err(&udev->dev, "failed to restore interface %d "
36951da177e4SLinus Torvalds 				"altsetting %d (error=%d)\n",
36961da177e4SLinus Torvalds 				desc->bInterfaceNumber,
36971da177e4SLinus Torvalds 				desc->bAlternateSetting,
36981da177e4SLinus Torvalds 				ret);
36991da177e4SLinus Torvalds 			goto re_enumerate;
37001da177e4SLinus Torvalds 		}
37011da177e4SLinus Torvalds 	}
37021da177e4SLinus Torvalds 
37031da177e4SLinus Torvalds done:
37041da177e4SLinus Torvalds 	return 0;
37051da177e4SLinus Torvalds 
37061da177e4SLinus Torvalds re_enumerate:
37071da177e4SLinus Torvalds 	hub_port_logical_disconnect(parent_hub, port1);
37081da177e4SLinus Torvalds 	return -ENODEV;
37091da177e4SLinus Torvalds }
371079efa097SAlan Stern 
371179efa097SAlan Stern /**
3712742120c6SMing Lei  * usb_reset_device - warn interface drivers and perform a USB port reset
371379efa097SAlan Stern  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
371479efa097SAlan Stern  *
371579efa097SAlan Stern  * Warns all drivers bound to registered interfaces (using their pre_reset
371679efa097SAlan Stern  * method), performs the port reset, and then lets the drivers know that
371779efa097SAlan Stern  * the reset is over (using their post_reset method).
371879efa097SAlan Stern  *
3719742120c6SMing Lei  * Return value is the same as for usb_reset_and_verify_device().
372079efa097SAlan Stern  *
372179efa097SAlan Stern  * The caller must own the device lock.  For example, it's safe to use
372279efa097SAlan Stern  * this from a driver probe() routine after downloading new firmware.
372379efa097SAlan Stern  * For calls that might not occur during probe(), drivers should lock
372479efa097SAlan Stern  * the device using usb_lock_device_for_reset().
372578d9a487SAlan Stern  *
372678d9a487SAlan Stern  * If an interface is currently being probed or disconnected, we assume
372778d9a487SAlan Stern  * its driver knows how to handle resets.  For all other interfaces,
372878d9a487SAlan Stern  * if the driver doesn't have pre_reset and post_reset methods then
372978d9a487SAlan Stern  * we attempt to unbind it and rebind afterward.
373079efa097SAlan Stern  */
3731742120c6SMing Lei int usb_reset_device(struct usb_device *udev)
373279efa097SAlan Stern {
373379efa097SAlan Stern 	int ret;
3734852c4b43SAlan Stern 	int i;
373579efa097SAlan Stern 	struct usb_host_config *config = udev->actconfig;
373679efa097SAlan Stern 
373779efa097SAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED ||
373879efa097SAlan Stern 			udev->state == USB_STATE_SUSPENDED) {
373979efa097SAlan Stern 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
374079efa097SAlan Stern 				udev->state);
374179efa097SAlan Stern 		return -EINVAL;
374279efa097SAlan Stern 	}
374379efa097SAlan Stern 
3744645daaabSAlan Stern 	/* Prevent autosuspend during the reset */
374594fcda1fSAlan Stern 	usb_autoresume_device(udev);
3746645daaabSAlan Stern 
374779efa097SAlan Stern 	if (config) {
3748852c4b43SAlan Stern 		for (i = 0; i < config->desc.bNumInterfaces; ++i) {
3749852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
375079efa097SAlan Stern 			struct usb_driver *drv;
375178d9a487SAlan Stern 			int unbind = 0;
375279efa097SAlan Stern 
3753852c4b43SAlan Stern 			if (cintf->dev.driver) {
375479efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
375578d9a487SAlan Stern 				if (drv->pre_reset && drv->post_reset)
375678d9a487SAlan Stern 					unbind = (drv->pre_reset)(cintf);
375778d9a487SAlan Stern 				else if (cintf->condition ==
375878d9a487SAlan Stern 						USB_INTERFACE_BOUND)
375978d9a487SAlan Stern 					unbind = 1;
376078d9a487SAlan Stern 				if (unbind)
376178d9a487SAlan Stern 					usb_forced_unbind_intf(cintf);
376279efa097SAlan Stern 			}
376379efa097SAlan Stern 		}
376479efa097SAlan Stern 	}
376579efa097SAlan Stern 
3766742120c6SMing Lei 	ret = usb_reset_and_verify_device(udev);
376779efa097SAlan Stern 
376879efa097SAlan Stern 	if (config) {
3769852c4b43SAlan Stern 		for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
3770852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
377179efa097SAlan Stern 			struct usb_driver *drv;
377278d9a487SAlan Stern 			int rebind = cintf->needs_binding;
377379efa097SAlan Stern 
377478d9a487SAlan Stern 			if (!rebind && cintf->dev.driver) {
377579efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
377679efa097SAlan Stern 				if (drv->post_reset)
377778d9a487SAlan Stern 					rebind = (drv->post_reset)(cintf);
377878d9a487SAlan Stern 				else if (cintf->condition ==
377978d9a487SAlan Stern 						USB_INTERFACE_BOUND)
378078d9a487SAlan Stern 					rebind = 1;
378179efa097SAlan Stern 			}
37826c640945SAlan Stern 			if (ret == 0 && rebind)
378378d9a487SAlan Stern 				usb_rebind_intf(cintf);
378479efa097SAlan Stern 		}
378579efa097SAlan Stern 	}
378679efa097SAlan Stern 
378794fcda1fSAlan Stern 	usb_autosuspend_device(udev);
378879efa097SAlan Stern 	return ret;
378979efa097SAlan Stern }
3790742120c6SMing Lei EXPORT_SYMBOL_GPL(usb_reset_device);
3791dc023dceSInaky Perez-Gonzalez 
3792dc023dceSInaky Perez-Gonzalez 
3793dc023dceSInaky Perez-Gonzalez /**
3794dc023dceSInaky Perez-Gonzalez  * usb_queue_reset_device - Reset a USB device from an atomic context
3795dc023dceSInaky Perez-Gonzalez  * @iface: USB interface belonging to the device to reset
3796dc023dceSInaky Perez-Gonzalez  *
3797dc023dceSInaky Perez-Gonzalez  * This function can be used to reset a USB device from an atomic
3798dc023dceSInaky Perez-Gonzalez  * context, where usb_reset_device() won't work (as it blocks).
3799dc023dceSInaky Perez-Gonzalez  *
3800dc023dceSInaky Perez-Gonzalez  * Doing a reset via this method is functionally equivalent to calling
3801dc023dceSInaky Perez-Gonzalez  * usb_reset_device(), except for the fact that it is delayed to a
3802dc023dceSInaky Perez-Gonzalez  * workqueue. This means that any drivers bound to other interfaces
3803dc023dceSInaky Perez-Gonzalez  * might be unbound, as well as users from usbfs in user space.
3804dc023dceSInaky Perez-Gonzalez  *
3805dc023dceSInaky Perez-Gonzalez  * Corner cases:
3806dc023dceSInaky Perez-Gonzalez  *
3807dc023dceSInaky Perez-Gonzalez  * - Scheduling two resets at the same time from two different drivers
3808dc023dceSInaky Perez-Gonzalez  *   attached to two different interfaces of the same device is
3809dc023dceSInaky Perez-Gonzalez  *   possible; depending on how the driver attached to each interface
3810dc023dceSInaky Perez-Gonzalez  *   handles ->pre_reset(), the second reset might happen or not.
3811dc023dceSInaky Perez-Gonzalez  *
3812dc023dceSInaky Perez-Gonzalez  * - If a driver is unbound and it had a pending reset, the reset will
3813dc023dceSInaky Perez-Gonzalez  *   be cancelled.
3814dc023dceSInaky Perez-Gonzalez  *
3815dc023dceSInaky Perez-Gonzalez  * - This function can be called during .probe() or .disconnect()
3816dc023dceSInaky Perez-Gonzalez  *   times. On return from .disconnect(), any pending resets will be
3817dc023dceSInaky Perez-Gonzalez  *   cancelled.
3818dc023dceSInaky Perez-Gonzalez  *
3819dc023dceSInaky Perez-Gonzalez  * There is no no need to lock/unlock the @reset_ws as schedule_work()
3820dc023dceSInaky Perez-Gonzalez  * does its own.
3821dc023dceSInaky Perez-Gonzalez  *
3822dc023dceSInaky Perez-Gonzalez  * NOTE: We don't do any reference count tracking because it is not
3823dc023dceSInaky Perez-Gonzalez  *     needed. The lifecycle of the work_struct is tied to the
3824dc023dceSInaky Perez-Gonzalez  *     usb_interface. Before destroying the interface we cancel the
3825dc023dceSInaky Perez-Gonzalez  *     work_struct, so the fact that work_struct is queued and or
3826dc023dceSInaky Perez-Gonzalez  *     running means the interface (and thus, the device) exist and
3827dc023dceSInaky Perez-Gonzalez  *     are referenced.
3828dc023dceSInaky Perez-Gonzalez  */
3829dc023dceSInaky Perez-Gonzalez void usb_queue_reset_device(struct usb_interface *iface)
3830dc023dceSInaky Perez-Gonzalez {
3831dc023dceSInaky Perez-Gonzalez 	schedule_work(&iface->reset_ws);
3832dc023dceSInaky Perez-Gonzalez }
3833dc023dceSInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_queue_reset_device);
3834