xref: /openbmc/linux/drivers/usb/core/hub.c (revision 0c9ffe0f)
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>
2393362a87SPhil Dibowitz #include <linux/usb/quirks.h>
249c8d6178Sakpm@osdl.org #include <linux/kthread.h>
254186ecf8SArjan van de Ven #include <linux/mutex.h>
267dfb7103SNigel Cunningham #include <linux/freezer.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 
85dbe79bbeSJohn Youn static inline int hub_is_superspeed(struct usb_device *hdev)
86dbe79bbeSJohn Youn {
87dbe79bbeSJohn Youn 	return (hdev->descriptor.bDeviceProtocol == 3);
88dbe79bbeSJohn Youn }
891bb5f66bSAlan Stern 
901da177e4SLinus Torvalds /* Protect struct usb_device->state and ->children members
919ad3d6ccSAlan Stern  * Note: Both are also protected by ->dev.sem, except that ->state can
921da177e4SLinus Torvalds  * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
931da177e4SLinus Torvalds static DEFINE_SPINLOCK(device_state_lock);
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds /* khubd's worklist and its lock */
961da177e4SLinus Torvalds static DEFINE_SPINLOCK(hub_event_lock);
971da177e4SLinus Torvalds static LIST_HEAD(hub_event_list);	/* List of hubs needing servicing */
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds /* Wakes up khubd */
1001da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
1011da177e4SLinus Torvalds 
1029c8d6178Sakpm@osdl.org static struct task_struct *khubd_task;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds /* cycle leds on hubs that aren't blinking for attention */
1051da177e4SLinus Torvalds static int blinkenlights = 0;
1061da177e4SLinus Torvalds module_param (blinkenlights, bool, S_IRUGO);
1071da177e4SLinus Torvalds MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds /*
110fd7c519dSJaroslav Kysela  * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
111fd7c519dSJaroslav Kysela  * 10 seconds to send reply for the initial 64-byte descriptor request.
112fd7c519dSJaroslav Kysela  */
113fd7c519dSJaroslav Kysela /* define initial 64-byte descriptor request timeout in milliseconds */
114fd7c519dSJaroslav Kysela static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
115fd7c519dSJaroslav Kysela module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
116b9cef6c3SWu Fengguang MODULE_PARM_DESC(initial_descriptor_timeout,
117b9cef6c3SWu Fengguang 		"initial 64-byte descriptor request timeout in milliseconds "
118b9cef6c3SWu Fengguang 		"(default 5000 - 5.0 seconds)");
119fd7c519dSJaroslav Kysela 
120fd7c519dSJaroslav Kysela /*
1211da177e4SLinus Torvalds  * As of 2.6.10 we introduce a new USB device initialization scheme which
1221da177e4SLinus Torvalds  * closely resembles the way Windows works.  Hopefully it will be compatible
1231da177e4SLinus Torvalds  * with a wider range of devices than the old scheme.  However some previously
1241da177e4SLinus Torvalds  * working devices may start giving rise to "device not accepting address"
1251da177e4SLinus Torvalds  * errors; if that happens the user can try the old scheme by adjusting the
1261da177e4SLinus Torvalds  * following module parameters.
1271da177e4SLinus Torvalds  *
1281da177e4SLinus Torvalds  * For maximum flexibility there are two boolean parameters to control the
1291da177e4SLinus Torvalds  * hub driver's behavior.  On the first initialization attempt, if the
1301da177e4SLinus Torvalds  * "old_scheme_first" parameter is set then the old scheme will be used,
1311da177e4SLinus Torvalds  * otherwise the new scheme is used.  If that fails and "use_both_schemes"
1321da177e4SLinus Torvalds  * is set, then the driver will make another attempt, using the other scheme.
1331da177e4SLinus Torvalds  */
1341da177e4SLinus Torvalds static int old_scheme_first = 0;
1351da177e4SLinus Torvalds module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
1361da177e4SLinus Torvalds MODULE_PARM_DESC(old_scheme_first,
1371da177e4SLinus Torvalds 		 "start with the old device initialization scheme");
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds static int use_both_schemes = 1;
1401da177e4SLinus Torvalds module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
1411da177e4SLinus Torvalds MODULE_PARM_DESC(use_both_schemes,
1421da177e4SLinus Torvalds 		"try the other device initialization scheme if the "
1431da177e4SLinus Torvalds 		"first one fails");
1441da177e4SLinus Torvalds 
14532fe0198SAlan Stern /* Mutual exclusion for EHCI CF initialization.  This interferes with
14632fe0198SAlan Stern  * port reset on some companion controllers.
14732fe0198SAlan Stern  */
14832fe0198SAlan Stern DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
14932fe0198SAlan Stern EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
15032fe0198SAlan Stern 
151948fea37SAlan Stern #define HUB_DEBOUNCE_TIMEOUT	1500
152948fea37SAlan Stern #define HUB_DEBOUNCE_STEP	  25
153948fea37SAlan Stern #define HUB_DEBOUNCE_STABLE	 100
154948fea37SAlan Stern 
1551da177e4SLinus Torvalds 
156742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev);
157742120c6SMing Lei 
158131dec34SSarah Sharp static inline char *portspeed(struct usb_hub *hub, int portstatus)
1591da177e4SLinus Torvalds {
160131dec34SSarah Sharp 	if (hub_is_superspeed(hub->hdev))
161131dec34SSarah Sharp 		return "5.0 Gb/s";
162288ead45SAlan Stern 	if (portstatus & USB_PORT_STAT_HIGH_SPEED)
1631da177e4SLinus Torvalds     		return "480 Mb/s";
164288ead45SAlan Stern 	else if (portstatus & USB_PORT_STAT_LOW_SPEED)
1651da177e4SLinus Torvalds 		return "1.5 Mb/s";
1661da177e4SLinus Torvalds 	else
1671da177e4SLinus Torvalds 		return "12 Mb/s";
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds /* Note that hdev or one of its children must be locked! */
17125118084SAlan Stern static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
1721da177e4SLinus Torvalds {
17325118084SAlan Stern 	if (!hdev || !hdev->actconfig)
17425118084SAlan Stern 		return NULL;
1751da177e4SLinus Torvalds 	return usb_get_intfdata(hdev->actconfig->interface[0]);
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.4.5 */
179dbe79bbeSJohn Youn static int get_hub_descriptor(struct usb_device *hdev, void *data)
1801da177e4SLinus Torvalds {
181dbe79bbeSJohn Youn 	int i, ret, size;
182dbe79bbeSJohn Youn 	unsigned dtype;
183dbe79bbeSJohn Youn 
184dbe79bbeSJohn Youn 	if (hub_is_superspeed(hdev)) {
185dbe79bbeSJohn Youn 		dtype = USB_DT_SS_HUB;
186dbe79bbeSJohn Youn 		size = USB_DT_SS_HUB_SIZE;
187dbe79bbeSJohn Youn 	} else {
188dbe79bbeSJohn Youn 		dtype = USB_DT_HUB;
189dbe79bbeSJohn Youn 		size = sizeof(struct usb_hub_descriptor);
190dbe79bbeSJohn Youn 	}
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	for (i = 0; i < 3; i++) {
1931da177e4SLinus Torvalds 		ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
1941da177e4SLinus Torvalds 			USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
195dbe79bbeSJohn Youn 			dtype << 8, 0, data, size,
1961da177e4SLinus Torvalds 			USB_CTRL_GET_TIMEOUT);
1971da177e4SLinus Torvalds 		if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
1981da177e4SLinus Torvalds 			return ret;
1991da177e4SLinus Torvalds 	}
2001da177e4SLinus Torvalds 	return -EINVAL;
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds /*
2041da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.1
2051da177e4SLinus Torvalds  */
2061da177e4SLinus Torvalds static int clear_hub_feature(struct usb_device *hdev, int feature)
2071da177e4SLinus Torvalds {
2081da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
2091da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds /*
2131da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.2
2141da177e4SLinus Torvalds  */
2151da177e4SLinus Torvalds static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
2181da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
2191da177e4SLinus Torvalds 		NULL, 0, 1000);
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds /*
2231da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.13
2241da177e4SLinus Torvalds  */
2251da177e4SLinus Torvalds static int set_port_feature(struct usb_device *hdev, int port1, int feature)
2261da177e4SLinus Torvalds {
2271da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
2281da177e4SLinus Torvalds 		USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
2291da177e4SLinus Torvalds 		NULL, 0, 1000);
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds /*
2331da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
2341da177e4SLinus Torvalds  * for info about using port indicators
2351da177e4SLinus Torvalds  */
2361da177e4SLinus Torvalds static void set_port_led(
2371da177e4SLinus Torvalds 	struct usb_hub *hub,
2381da177e4SLinus Torvalds 	int port1,
2391da177e4SLinus Torvalds 	int selector
2401da177e4SLinus Torvalds )
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	int status = set_port_feature(hub->hdev, (selector << 8) | port1,
2431da177e4SLinus Torvalds 			USB_PORT_FEAT_INDICATOR);
2441da177e4SLinus Torvalds 	if (status < 0)
2451da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
2461da177e4SLinus Torvalds 			"port %d indicator %s status %d\n",
2471da177e4SLinus Torvalds 			port1,
2481da177e4SLinus Torvalds 			({ char *s; switch (selector) {
2491da177e4SLinus Torvalds 			case HUB_LED_AMBER: s = "amber"; break;
2501da177e4SLinus Torvalds 			case HUB_LED_GREEN: s = "green"; break;
2511da177e4SLinus Torvalds 			case HUB_LED_OFF: s = "off"; break;
2521da177e4SLinus Torvalds 			case HUB_LED_AUTO: s = "auto"; break;
2531da177e4SLinus Torvalds 			default: s = "??"; break;
2541da177e4SLinus Torvalds 			}; s; }),
2551da177e4SLinus Torvalds 			status);
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds #define	LED_CYCLE_PERIOD	((2*HZ)/3)
2591da177e4SLinus Torvalds 
260c4028958SDavid Howells static void led_work (struct work_struct *work)
2611da177e4SLinus Torvalds {
262c4028958SDavid Howells 	struct usb_hub		*hub =
263c4028958SDavid Howells 		container_of(work, struct usb_hub, leds.work);
2641da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
2651da177e4SLinus Torvalds 	unsigned		i;
2661da177e4SLinus Torvalds 	unsigned		changed = 0;
2671da177e4SLinus Torvalds 	int			cursor = -1;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
2701da177e4SLinus Torvalds 		return;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
2731da177e4SLinus Torvalds 		unsigned	selector, mode;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 		/* 30%-50% duty cycle */
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 		switch (hub->indicator[i]) {
2781da177e4SLinus Torvalds 		/* cycle marker */
2791da177e4SLinus Torvalds 		case INDICATOR_CYCLE:
2801da177e4SLinus Torvalds 			cursor = i;
2811da177e4SLinus Torvalds 			selector = HUB_LED_AUTO;
2821da177e4SLinus Torvalds 			mode = INDICATOR_AUTO;
2831da177e4SLinus Torvalds 			break;
2841da177e4SLinus Torvalds 		/* blinking green = sw attention */
2851da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK:
2861da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
2871da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK_OFF;
2881da177e4SLinus Torvalds 			break;
2891da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK_OFF:
2901da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
2911da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK;
2921da177e4SLinus Torvalds 			break;
2931da177e4SLinus Torvalds 		/* blinking amber = hw attention */
2941da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK:
2951da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
2961da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK_OFF;
2971da177e4SLinus Torvalds 			break;
2981da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK_OFF:
2991da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
3001da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK;
3011da177e4SLinus Torvalds 			break;
3021da177e4SLinus Torvalds 		/* blink green/amber = reserved */
3031da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK:
3041da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
3051da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK_OFF;
3061da177e4SLinus Torvalds 			break;
3071da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK_OFF:
3081da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
3091da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK;
3101da177e4SLinus Torvalds 			break;
3111da177e4SLinus Torvalds 		default:
3121da177e4SLinus Torvalds 			continue;
3131da177e4SLinus Torvalds 		}
3141da177e4SLinus Torvalds 		if (selector != HUB_LED_AUTO)
3151da177e4SLinus Torvalds 			changed = 1;
3161da177e4SLinus Torvalds 		set_port_led(hub, i + 1, selector);
3171da177e4SLinus Torvalds 		hub->indicator[i] = mode;
3181da177e4SLinus Torvalds 	}
3191da177e4SLinus Torvalds 	if (!changed && blinkenlights) {
3201da177e4SLinus Torvalds 		cursor++;
3211da177e4SLinus Torvalds 		cursor %= hub->descriptor->bNbrPorts;
3221da177e4SLinus Torvalds 		set_port_led(hub, cursor + 1, HUB_LED_GREEN);
3231da177e4SLinus Torvalds 		hub->indicator[cursor] = INDICATOR_CYCLE;
3241da177e4SLinus Torvalds 		changed++;
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 	if (changed)
3271da177e4SLinus Torvalds 		schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds /* use a short timeout for hub/port status fetches */
3311da177e4SLinus Torvalds #define	USB_STS_TIMEOUT		1000
3321da177e4SLinus Torvalds #define	USB_STS_RETRIES		5
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds /*
3351da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.6
3361da177e4SLinus Torvalds  */
3371da177e4SLinus Torvalds static int get_hub_status(struct usb_device *hdev,
3381da177e4SLinus Torvalds 		struct usb_hub_status *data)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
3431da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
3441da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
3451da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds 	return status;
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds /*
3511da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7
3521da177e4SLinus Torvalds  */
3531da177e4SLinus Torvalds static int get_port_status(struct usb_device *hdev, int port1,
3541da177e4SLinus Torvalds 		struct usb_port_status *data)
3551da177e4SLinus Torvalds {
3561da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
3591da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
3601da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
3611da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds 	return status;
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
3663eb14915SAlan Stern static int hub_port_status(struct usb_hub *hub, int port1,
3673eb14915SAlan Stern 		u16 *status, u16 *change)
3683eb14915SAlan Stern {
3693eb14915SAlan Stern 	int ret;
3703eb14915SAlan Stern 
3713eb14915SAlan Stern 	mutex_lock(&hub->status_mutex);
3723eb14915SAlan Stern 	ret = get_port_status(hub->hdev, port1, &hub->status->port);
3733eb14915SAlan Stern 	if (ret < 4) {
3743eb14915SAlan Stern 		dev_err(hub->intfdev,
3753eb14915SAlan Stern 			"%s failed (err = %d)\n", __func__, ret);
3763eb14915SAlan Stern 		if (ret >= 0)
3773eb14915SAlan Stern 			ret = -EIO;
3783eb14915SAlan Stern 	} else {
3793eb14915SAlan Stern 		*status = le16_to_cpu(hub->status->port.wPortStatus);
3803eb14915SAlan Stern 		*change = le16_to_cpu(hub->status->port.wPortChange);
381dbe79bbeSJohn Youn 
382dbe79bbeSJohn Youn 		if ((hub->hdev->parent != NULL) &&
383dbe79bbeSJohn Youn 				hub_is_superspeed(hub->hdev)) {
384dbe79bbeSJohn Youn 			/* Translate the USB 3 port status */
385dbe79bbeSJohn Youn 			u16 tmp = *status & USB_SS_PORT_STAT_MASK;
386dbe79bbeSJohn Youn 			if (*status & USB_SS_PORT_STAT_POWER)
387dbe79bbeSJohn Youn 				tmp |= USB_PORT_STAT_POWER;
388dbe79bbeSJohn Youn 			*status = tmp;
389dbe79bbeSJohn Youn 		}
390dbe79bbeSJohn Youn 
3913eb14915SAlan Stern 		ret = 0;
3923eb14915SAlan Stern 	}
3933eb14915SAlan Stern 	mutex_unlock(&hub->status_mutex);
3943eb14915SAlan Stern 	return ret;
3953eb14915SAlan Stern }
3963eb14915SAlan Stern 
3971da177e4SLinus Torvalds static void kick_khubd(struct usb_hub *hub)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	unsigned long	flags;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	spin_lock_irqsave(&hub_event_lock, flags);
4022e2c5eeaSRoel Kluin 	if (!hub->disconnected && list_empty(&hub->event_list)) {
4031da177e4SLinus Torvalds 		list_add_tail(&hub->event_list, &hub_event_list);
4048e4ceb38SAlan Stern 
4058e4ceb38SAlan Stern 		/* Suppress autosuspend until khubd runs */
4068e4ceb38SAlan Stern 		usb_autopm_get_interface_no_resume(
4078e4ceb38SAlan Stern 				to_usb_interface(hub->intfdev));
4081da177e4SLinus Torvalds 		wake_up(&khubd_wait);
4091da177e4SLinus Torvalds 	}
4101da177e4SLinus Torvalds 	spin_unlock_irqrestore(&hub_event_lock, flags);
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds void usb_kick_khubd(struct usb_device *hdev)
4141da177e4SLinus Torvalds {
41525118084SAlan Stern 	struct usb_hub *hub = hdev_to_hub(hdev);
41625118084SAlan Stern 
41725118084SAlan Stern 	if (hub)
41825118084SAlan Stern 		kick_khubd(hub);
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds /* completion function, fires on port status changes and various faults */
4237d12e780SDavid Howells static void hub_irq(struct urb *urb)
4241da177e4SLinus Torvalds {
425ec17cf1cSTobias Klauser 	struct usb_hub *hub = urb->context;
426e015268dSAlan Stern 	int status = urb->status;
42771d2718fSRoel Kluin 	unsigned i;
4281da177e4SLinus Torvalds 	unsigned long bits;
4291da177e4SLinus Torvalds 
430e015268dSAlan Stern 	switch (status) {
4311da177e4SLinus Torvalds 	case -ENOENT:		/* synchronous unlink */
4321da177e4SLinus Torvalds 	case -ECONNRESET:	/* async unlink */
4331da177e4SLinus Torvalds 	case -ESHUTDOWN:	/* hardware going away */
4341da177e4SLinus Torvalds 		return;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	default:		/* presumably an error */
4371da177e4SLinus Torvalds 		/* Cause a hub reset after 10 consecutive errors */
438e015268dSAlan Stern 		dev_dbg (hub->intfdev, "transfer --> %d\n", status);
4391da177e4SLinus Torvalds 		if ((++hub->nerrors < 10) || hub->error)
4401da177e4SLinus Torvalds 			goto resubmit;
441e015268dSAlan Stern 		hub->error = status;
4421da177e4SLinus Torvalds 		/* FALL THROUGH */
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	/* let khubd handle things */
4451da177e4SLinus Torvalds 	case 0:			/* we got data:  port status changed */
4461da177e4SLinus Torvalds 		bits = 0;
4471da177e4SLinus Torvalds 		for (i = 0; i < urb->actual_length; ++i)
4481da177e4SLinus Torvalds 			bits |= ((unsigned long) ((*hub->buffer)[i]))
4491da177e4SLinus Torvalds 					<< (i*8);
4501da177e4SLinus Torvalds 		hub->event_bits[0] = bits;
4511da177e4SLinus Torvalds 		break;
4521da177e4SLinus Torvalds 	}
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	hub->nerrors = 0;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	/* Something happened, let khubd figure it out */
4571da177e4SLinus Torvalds 	kick_khubd(hub);
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds resubmit:
4601da177e4SLinus Torvalds 	if (hub->quiescing)
4611da177e4SLinus Torvalds 		return;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
4641da177e4SLinus Torvalds 			&& status != -ENODEV && status != -EPERM)
4651da177e4SLinus Torvalds 		dev_err (hub->intfdev, "resubmit --> %d\n", status);
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.2.3 */
4691da177e4SLinus Torvalds static inline int
4701da177e4SLinus Torvalds hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
4711da177e4SLinus Torvalds {
472c2f6595fSAlan Stern 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
4731da177e4SLinus Torvalds 			       HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
4741da177e4SLinus Torvalds 			       tt, NULL, 0, 1000);
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds /*
4781da177e4SLinus Torvalds  * enumeration blocks khubd for a long time. we use keventd instead, since
4791da177e4SLinus Torvalds  * long blocking there is the exception, not the rule.  accordingly, HCDs
4801da177e4SLinus Torvalds  * talking to TTs must queue control transfers (not just bulk and iso), so
4811da177e4SLinus Torvalds  * both can talk to the same hub concurrently.
4821da177e4SLinus Torvalds  */
483cb88a1b8SAlan Stern static void hub_tt_work(struct work_struct *work)
4841da177e4SLinus Torvalds {
485c4028958SDavid Howells 	struct usb_hub		*hub =
486cb88a1b8SAlan Stern 		container_of(work, struct usb_hub, tt.clear_work);
4871da177e4SLinus Torvalds 	unsigned long		flags;
48855e5fdfaSMark Lord 	int			limit = 100;
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	spin_lock_irqsave (&hub->tt.lock, flags);
49155e5fdfaSMark Lord 	while (--limit && !list_empty (&hub->tt.clear_list)) {
492d0f830d3SH Hartley Sweeten 		struct list_head	*next;
4931da177e4SLinus Torvalds 		struct usb_tt_clear	*clear;
4941da177e4SLinus Torvalds 		struct usb_device	*hdev = hub->hdev;
495cb88a1b8SAlan Stern 		const struct hc_driver	*drv;
4961da177e4SLinus Torvalds 		int			status;
4971da177e4SLinus Torvalds 
498d0f830d3SH Hartley Sweeten 		next = hub->tt.clear_list.next;
499d0f830d3SH Hartley Sweeten 		clear = list_entry (next, struct usb_tt_clear, clear_list);
5001da177e4SLinus Torvalds 		list_del (&clear->clear_list);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 		/* drop lock so HCD can concurrently report other TT errors */
5031da177e4SLinus Torvalds 		spin_unlock_irqrestore (&hub->tt.lock, flags);
5041da177e4SLinus Torvalds 		status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
5051da177e4SLinus Torvalds 		if (status)
5061da177e4SLinus Torvalds 			dev_err (&hdev->dev,
5071da177e4SLinus Torvalds 				"clear tt %d (%04x) error %d\n",
5081da177e4SLinus Torvalds 				clear->tt, clear->devinfo, status);
509cb88a1b8SAlan Stern 
510cb88a1b8SAlan Stern 		/* Tell the HCD, even if the operation failed */
511cb88a1b8SAlan Stern 		drv = clear->hcd->driver;
512cb88a1b8SAlan Stern 		if (drv->clear_tt_buffer_complete)
513cb88a1b8SAlan Stern 			(drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
514cb88a1b8SAlan Stern 
5151da177e4SLinus Torvalds 		kfree(clear);
516cb88a1b8SAlan Stern 		spin_lock_irqsave(&hub->tt.lock, flags);
5171da177e4SLinus Torvalds 	}
5181da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hub->tt.lock, flags);
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds /**
522cb88a1b8SAlan Stern  * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
523cb88a1b8SAlan Stern  * @urb: an URB associated with the failed or incomplete split transaction
5241da177e4SLinus Torvalds  *
5251da177e4SLinus Torvalds  * High speed HCDs use this to tell the hub driver that some split control or
5261da177e4SLinus Torvalds  * bulk transaction failed in a way that requires clearing internal state of
5271da177e4SLinus Torvalds  * a transaction translator.  This is normally detected (and reported) from
5281da177e4SLinus Torvalds  * interrupt context.
5291da177e4SLinus Torvalds  *
5301da177e4SLinus Torvalds  * It may not be possible for that hub to handle additional full (or low)
5311da177e4SLinus Torvalds  * speed transactions until that state is fully cleared out.
5321da177e4SLinus Torvalds  */
533cb88a1b8SAlan Stern int usb_hub_clear_tt_buffer(struct urb *urb)
5341da177e4SLinus Torvalds {
535cb88a1b8SAlan Stern 	struct usb_device	*udev = urb->dev;
536cb88a1b8SAlan Stern 	int			pipe = urb->pipe;
5371da177e4SLinus Torvalds 	struct usb_tt		*tt = udev->tt;
5381da177e4SLinus Torvalds 	unsigned long		flags;
5391da177e4SLinus Torvalds 	struct usb_tt_clear	*clear;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	/* we've got to cope with an arbitrary number of pending TT clears,
5421da177e4SLinus Torvalds 	 * since each TT has "at least two" buffers that can need it (and
5431da177e4SLinus Torvalds 	 * there can be many TTs per hub).  even if they're uncommon.
5441da177e4SLinus Torvalds 	 */
54554e6ecb2SChristoph Lameter 	if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
5461da177e4SLinus Torvalds 		dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
5471da177e4SLinus Torvalds 		/* FIXME recover somehow ... RESET_TT? */
548cb88a1b8SAlan Stern 		return -ENOMEM;
5491da177e4SLinus Torvalds 	}
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	/* info that CLEAR_TT_BUFFER needs */
5521da177e4SLinus Torvalds 	clear->tt = tt->multi ? udev->ttport : 1;
5531da177e4SLinus Torvalds 	clear->devinfo = usb_pipeendpoint (pipe);
5541da177e4SLinus Torvalds 	clear->devinfo |= udev->devnum << 4;
5551da177e4SLinus Torvalds 	clear->devinfo |= usb_pipecontrol (pipe)
5561da177e4SLinus Torvalds 			? (USB_ENDPOINT_XFER_CONTROL << 11)
5571da177e4SLinus Torvalds 			: (USB_ENDPOINT_XFER_BULK << 11);
5581da177e4SLinus Torvalds 	if (usb_pipein (pipe))
5591da177e4SLinus Torvalds 		clear->devinfo |= 1 << 15;
5601da177e4SLinus Torvalds 
561cb88a1b8SAlan Stern 	/* info for completion callback */
562cb88a1b8SAlan Stern 	clear->hcd = bus_to_hcd(udev->bus);
563cb88a1b8SAlan Stern 	clear->ep = urb->ep;
564cb88a1b8SAlan Stern 
5651da177e4SLinus Torvalds 	/* tell keventd to clear state for this TT */
5661da177e4SLinus Torvalds 	spin_lock_irqsave (&tt->lock, flags);
5671da177e4SLinus Torvalds 	list_add_tail (&clear->clear_list, &tt->clear_list);
568cb88a1b8SAlan Stern 	schedule_work(&tt->clear_work);
5691da177e4SLinus Torvalds 	spin_unlock_irqrestore (&tt->lock, flags);
570cb88a1b8SAlan Stern 	return 0;
5711da177e4SLinus Torvalds }
572cb88a1b8SAlan Stern EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
5731da177e4SLinus Torvalds 
5748520f380SAlan Stern /* If do_delay is false, return the number of milliseconds the caller
5758520f380SAlan Stern  * needs to delay.
5768520f380SAlan Stern  */
5778520f380SAlan Stern static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
5781da177e4SLinus Torvalds {
5791da177e4SLinus Torvalds 	int port1;
580b789696aSDavid Brownell 	unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
5818520f380SAlan Stern 	unsigned delay;
5824489a571SAlan Stern 	u16 wHubCharacteristics =
5834489a571SAlan Stern 			le16_to_cpu(hub->descriptor->wHubCharacteristics);
5841da177e4SLinus Torvalds 
5854489a571SAlan Stern 	/* Enable power on each port.  Some hubs have reserved values
5864489a571SAlan Stern 	 * of LPSM (> 2) in their descriptors, even though they are
5874489a571SAlan Stern 	 * USB 2.0 hubs.  Some hubs do not implement port-power switching
5884489a571SAlan Stern 	 * but only emulate it.  In all cases, the ports won't work
5894489a571SAlan Stern 	 * unless we send these messages to the hub.
5904489a571SAlan Stern 	 */
5914489a571SAlan Stern 	if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
5921da177e4SLinus Torvalds 		dev_dbg(hub->intfdev, "enabling power on all ports\n");
5934489a571SAlan Stern 	else
5944489a571SAlan Stern 		dev_dbg(hub->intfdev, "trying to enable port power on "
5954489a571SAlan Stern 				"non-switchable hub\n");
5961da177e4SLinus Torvalds 	for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
5974489a571SAlan Stern 		set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
5981da177e4SLinus Torvalds 
599b789696aSDavid Brownell 	/* Wait at least 100 msec for power to become stable */
6008520f380SAlan Stern 	delay = max(pgood_delay, (unsigned) 100);
6018520f380SAlan Stern 	if (do_delay)
6028520f380SAlan Stern 		msleep(delay);
6038520f380SAlan Stern 	return delay;
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds static int hub_hub_status(struct usb_hub *hub,
6071da177e4SLinus Torvalds 		u16 *status, u16 *change)
6081da177e4SLinus Torvalds {
6091da177e4SLinus Torvalds 	int ret;
6101da177e4SLinus Torvalds 
611db90e7a1SAlan Stern 	mutex_lock(&hub->status_mutex);
6121da177e4SLinus Torvalds 	ret = get_hub_status(hub->hdev, &hub->status->hub);
6131da177e4SLinus Torvalds 	if (ret < 0)
6141da177e4SLinus Torvalds 		dev_err (hub->intfdev,
615441b62c1SHarvey Harrison 			"%s failed (err = %d)\n", __func__, ret);
6161da177e4SLinus Torvalds 	else {
6171da177e4SLinus Torvalds 		*status = le16_to_cpu(hub->status->hub.wHubStatus);
6181da177e4SLinus Torvalds 		*change = le16_to_cpu(hub->status->hub.wHubChange);
6191da177e4SLinus Torvalds 		ret = 0;
6201da177e4SLinus Torvalds 	}
621db90e7a1SAlan Stern 	mutex_unlock(&hub->status_mutex);
6221da177e4SLinus Torvalds 	return ret;
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
6258b28c752SAlan Stern static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
6268b28c752SAlan Stern {
6278b28c752SAlan Stern 	struct usb_device *hdev = hub->hdev;
6280458d5b4SAlan Stern 	int ret = 0;
6298b28c752SAlan Stern 
6300458d5b4SAlan Stern 	if (hdev->children[port1-1] && set_state)
6318b28c752SAlan Stern 		usb_set_device_state(hdev->children[port1-1],
6328b28c752SAlan Stern 				USB_STATE_NOTATTACHED);
633dbe79bbeSJohn Youn 	if (!hub->error && !hub_is_superspeed(hub->hdev))
6348b28c752SAlan Stern 		ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
6358b28c752SAlan Stern 	if (ret)
6368b28c752SAlan Stern 		dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
6378b28c752SAlan Stern 				port1, ret);
6388b28c752SAlan Stern 	return ret;
6398b28c752SAlan Stern }
6408b28c752SAlan Stern 
6410458d5b4SAlan Stern /*
6426d42fcdbSJustin P. Mattock  * Disable a port and mark a logical connect-change event, so that some
6430458d5b4SAlan Stern  * time later khubd will disconnect() any existing usb_device on the port
6440458d5b4SAlan Stern  * and will re-enumerate if there actually is a device attached.
6450458d5b4SAlan Stern  */
6460458d5b4SAlan Stern static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
6477d069b7dSAlan Stern {
6480458d5b4SAlan Stern 	dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
6490458d5b4SAlan Stern 	hub_port_disable(hub, port1, 1);
6500458d5b4SAlan Stern 
6510458d5b4SAlan Stern 	/* FIXME let caller ask to power down the port:
6520458d5b4SAlan Stern 	 *  - some devices won't enumerate without a VBUS power cycle
6530458d5b4SAlan Stern 	 *  - SRP saves power that way
6540458d5b4SAlan Stern 	 *  - ... new call, TBD ...
6550458d5b4SAlan Stern 	 * That's easy if this hub can switch power per-port, and
6560458d5b4SAlan Stern 	 * khubd reactivates the port later (timer, SRP, etc).
6570458d5b4SAlan Stern 	 * Powerdown must be optional, because of reset/DFU.
6580458d5b4SAlan Stern 	 */
6590458d5b4SAlan Stern 
6600458d5b4SAlan Stern 	set_bit(port1, hub->change_bits);
6610458d5b4SAlan Stern  	kick_khubd(hub);
6620458d5b4SAlan Stern }
6630458d5b4SAlan Stern 
664253e0572SAlan Stern /**
665253e0572SAlan Stern  * usb_remove_device - disable a device's port on its parent hub
666253e0572SAlan Stern  * @udev: device to be disabled and removed
667253e0572SAlan Stern  * Context: @udev locked, must be able to sleep.
668253e0572SAlan Stern  *
669253e0572SAlan Stern  * After @udev's port has been disabled, khubd is notified and it will
670253e0572SAlan Stern  * see that the device has been disconnected.  When the device is
671253e0572SAlan Stern  * physically unplugged and something is plugged in, the events will
672253e0572SAlan Stern  * be received and processed normally.
673253e0572SAlan Stern  */
674253e0572SAlan Stern int usb_remove_device(struct usb_device *udev)
675253e0572SAlan Stern {
676253e0572SAlan Stern 	struct usb_hub *hub;
677253e0572SAlan Stern 	struct usb_interface *intf;
678253e0572SAlan Stern 
679253e0572SAlan Stern 	if (!udev->parent)	/* Can't remove a root hub */
680253e0572SAlan Stern 		return -EINVAL;
681253e0572SAlan Stern 	hub = hdev_to_hub(udev->parent);
682253e0572SAlan Stern 	intf = to_usb_interface(hub->intfdev);
683253e0572SAlan Stern 
684253e0572SAlan Stern 	usb_autopm_get_interface(intf);
685253e0572SAlan Stern 	set_bit(udev->portnum, hub->removed_bits);
686253e0572SAlan Stern 	hub_port_logical_disconnect(hub, udev->portnum);
687253e0572SAlan Stern 	usb_autopm_put_interface(intf);
688253e0572SAlan Stern 	return 0;
689253e0572SAlan Stern }
690253e0572SAlan Stern 
6916ee0b270SAlan Stern enum hub_activation_type {
6928e4ceb38SAlan Stern 	HUB_INIT, HUB_INIT2, HUB_INIT3,		/* INITs must come first */
6938520f380SAlan Stern 	HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
6946ee0b270SAlan Stern };
6955e6effaeSAlan Stern 
6968520f380SAlan Stern static void hub_init_func2(struct work_struct *ws);
6978520f380SAlan Stern static void hub_init_func3(struct work_struct *ws);
6988520f380SAlan Stern 
699f2835219SAlan Stern static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
7005e6effaeSAlan Stern {
7015e6effaeSAlan Stern 	struct usb_device *hdev = hub->hdev;
702653a39d1SSarah Sharp 	struct usb_hcd *hcd;
703653a39d1SSarah Sharp 	int ret;
7045e6effaeSAlan Stern 	int port1;
705f2835219SAlan Stern 	int status;
706948fea37SAlan Stern 	bool need_debounce_delay = false;
7078520f380SAlan Stern 	unsigned delay;
7088520f380SAlan Stern 
7098520f380SAlan Stern 	/* Continue a partial initialization */
7108520f380SAlan Stern 	if (type == HUB_INIT2)
7118520f380SAlan Stern 		goto init2;
7128520f380SAlan Stern 	if (type == HUB_INIT3)
7138520f380SAlan Stern 		goto init3;
7145e6effaeSAlan Stern 
715f2835219SAlan Stern 	/* After a resume, port power should still be on.
716f2835219SAlan Stern 	 * For any other type of activation, turn it on.
717f2835219SAlan Stern 	 */
7188520f380SAlan Stern 	if (type != HUB_RESUME) {
7198520f380SAlan Stern 
7208520f380SAlan Stern 		/* Speed up system boot by using a delayed_work for the
7218520f380SAlan Stern 		 * hub's initial power-up delays.  This is pretty awkward
7228520f380SAlan Stern 		 * and the implementation looks like a home-brewed sort of
7238520f380SAlan Stern 		 * setjmp/longjmp, but it saves at least 100 ms for each
7248520f380SAlan Stern 		 * root hub (assuming usbcore is compiled into the kernel
7258520f380SAlan Stern 		 * rather than as a module).  It adds up.
7268520f380SAlan Stern 		 *
7278520f380SAlan Stern 		 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
7288520f380SAlan Stern 		 * because for those activation types the ports have to be
7298520f380SAlan Stern 		 * operational when we return.  In theory this could be done
7308520f380SAlan Stern 		 * for HUB_POST_RESET, but it's easier not to.
7318520f380SAlan Stern 		 */
7328520f380SAlan Stern 		if (type == HUB_INIT) {
7338520f380SAlan Stern 			delay = hub_power_on(hub, false);
7348520f380SAlan Stern 			PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
7358520f380SAlan Stern 			schedule_delayed_work(&hub->init_work,
7368520f380SAlan Stern 					msecs_to_jiffies(delay));
73761fbeba1SAlan Stern 
73861fbeba1SAlan Stern 			/* Suppress autosuspend until init is done */
7398e4ceb38SAlan Stern 			usb_autopm_get_interface_no_resume(
7408e4ceb38SAlan Stern 					to_usb_interface(hub->intfdev));
7418520f380SAlan Stern 			return;		/* Continues at init2: below */
742653a39d1SSarah Sharp 		} else if (type == HUB_RESET_RESUME) {
743653a39d1SSarah Sharp 			/* The internal host controller state for the hub device
744653a39d1SSarah Sharp 			 * may be gone after a host power loss on system resume.
745653a39d1SSarah Sharp 			 * Update the device's info so the HW knows it's a hub.
746653a39d1SSarah Sharp 			 */
747653a39d1SSarah Sharp 			hcd = bus_to_hcd(hdev->bus);
748653a39d1SSarah Sharp 			if (hcd->driver->update_hub_device) {
749653a39d1SSarah Sharp 				ret = hcd->driver->update_hub_device(hcd, hdev,
750653a39d1SSarah Sharp 						&hub->tt, GFP_NOIO);
751653a39d1SSarah Sharp 				if (ret < 0) {
752653a39d1SSarah Sharp 					dev_err(hub->intfdev, "Host not "
753653a39d1SSarah Sharp 							"accepting hub info "
754653a39d1SSarah Sharp 							"update.\n");
755653a39d1SSarah Sharp 					dev_err(hub->intfdev, "LS/FS devices "
756653a39d1SSarah Sharp 							"and hubs may not work "
757653a39d1SSarah Sharp 							"under this hub\n.");
758653a39d1SSarah Sharp 				}
759653a39d1SSarah Sharp 			}
760653a39d1SSarah Sharp 			hub_power_on(hub, true);
7618520f380SAlan Stern 		} else {
7628520f380SAlan Stern 			hub_power_on(hub, true);
7638520f380SAlan Stern 		}
7648520f380SAlan Stern 	}
7658520f380SAlan Stern  init2:
766f2835219SAlan Stern 
7676ee0b270SAlan Stern 	/* Check each port and set hub->change_bits to let khubd know
7686ee0b270SAlan Stern 	 * which ports need attention.
7695e6effaeSAlan Stern 	 */
7705e6effaeSAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
7715e6effaeSAlan Stern 		struct usb_device *udev = hdev->children[port1-1];
7725e6effaeSAlan Stern 		u16 portstatus, portchange;
7735e6effaeSAlan Stern 
7746ee0b270SAlan Stern 		portstatus = portchange = 0;
7756ee0b270SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
7766ee0b270SAlan Stern 		if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
7776ee0b270SAlan Stern 			dev_dbg(hub->intfdev,
7786ee0b270SAlan Stern 					"port %d: status %04x change %04x\n",
7796ee0b270SAlan Stern 					port1, portstatus, portchange);
7805e6effaeSAlan Stern 
7816ee0b270SAlan Stern 		/* After anything other than HUB_RESUME (i.e., initialization
7826ee0b270SAlan Stern 		 * or any sort of reset), every port should be disabled.
7836ee0b270SAlan Stern 		 * Unconnected ports should likewise be disabled (paranoia),
7846ee0b270SAlan Stern 		 * and so should ports for which we have no usb_device.
7855e6effaeSAlan Stern 		 */
7866ee0b270SAlan Stern 		if ((portstatus & USB_PORT_STAT_ENABLE) && (
7876ee0b270SAlan Stern 				type != HUB_RESUME ||
7886ee0b270SAlan Stern 				!(portstatus & USB_PORT_STAT_CONNECTION) ||
7896ee0b270SAlan Stern 				!udev ||
7906ee0b270SAlan Stern 				udev->state == USB_STATE_NOTATTACHED)) {
7919f0a6cd3SAndiry Xu 			/*
7929f0a6cd3SAndiry Xu 			 * USB3 protocol ports will automatically transition
7939f0a6cd3SAndiry Xu 			 * to Enabled state when detect an USB3.0 device attach.
7949f0a6cd3SAndiry Xu 			 * Do not disable USB3 protocol ports.
7959f0a6cd3SAndiry Xu 			 */
796131dec34SSarah Sharp 			if (!hub_is_superspeed(hdev)) {
7979f0a6cd3SAndiry Xu 				clear_port_feature(hdev, port1,
7989f0a6cd3SAndiry Xu 						   USB_PORT_FEAT_ENABLE);
7996ee0b270SAlan Stern 				portstatus &= ~USB_PORT_STAT_ENABLE;
80085f0ff46SSarah Sharp 			} else {
80185f0ff46SSarah Sharp 				/* Pretend that power was lost for USB3 devs */
80285f0ff46SSarah Sharp 				portstatus &= ~USB_PORT_STAT_ENABLE;
8036ee0b270SAlan Stern 			}
8049f0a6cd3SAndiry Xu 		}
8056ee0b270SAlan Stern 
806948fea37SAlan Stern 		/* Clear status-change flags; we'll debounce later */
807948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
808948fea37SAlan Stern 			need_debounce_delay = true;
809948fea37SAlan Stern 			clear_port_feature(hub->hdev, port1,
810948fea37SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
811948fea37SAlan Stern 		}
812948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE) {
813948fea37SAlan Stern 			need_debounce_delay = true;
814948fea37SAlan Stern 			clear_port_feature(hub->hdev, port1,
815948fea37SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
816948fea37SAlan Stern 		}
817dbe79bbeSJohn Youn 		if (portchange & USB_PORT_STAT_C_LINK_STATE) {
818dbe79bbeSJohn Youn 			need_debounce_delay = true;
819dbe79bbeSJohn Youn 			clear_port_feature(hub->hdev, port1,
820dbe79bbeSJohn Youn 					USB_PORT_FEAT_C_PORT_LINK_STATE);
821dbe79bbeSJohn Youn 		}
822948fea37SAlan Stern 
823253e0572SAlan Stern 		/* We can forget about a "removed" device when there's a
824253e0572SAlan Stern 		 * physical disconnect or the connect status changes.
825253e0572SAlan Stern 		 */
826253e0572SAlan Stern 		if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
827253e0572SAlan Stern 				(portchange & USB_PORT_STAT_C_CONNECTION))
828253e0572SAlan Stern 			clear_bit(port1, hub->removed_bits);
829253e0572SAlan Stern 
8306ee0b270SAlan Stern 		if (!udev || udev->state == USB_STATE_NOTATTACHED) {
8316ee0b270SAlan Stern 			/* Tell khubd to disconnect the device or
8326ee0b270SAlan Stern 			 * check for a new connection
8336ee0b270SAlan Stern 			 */
8346ee0b270SAlan Stern 			if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
8356ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
8366ee0b270SAlan Stern 
8376ee0b270SAlan Stern 		} else if (portstatus & USB_PORT_STAT_ENABLE) {
8386ee0b270SAlan Stern 			/* The power session apparently survived the resume.
8396ee0b270SAlan Stern 			 * If there was an overcurrent or suspend change
8406ee0b270SAlan Stern 			 * (i.e., remote wakeup request), have khubd
8416ee0b270SAlan Stern 			 * take care of it.
8426ee0b270SAlan Stern 			 */
8436ee0b270SAlan Stern 			if (portchange)
8446ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
8456ee0b270SAlan Stern 
8466ee0b270SAlan Stern 		} else if (udev->persist_enabled) {
8476ee0b270SAlan Stern #ifdef CONFIG_PM
8485e6effaeSAlan Stern 			udev->reset_resume = 1;
8496ee0b270SAlan Stern #endif
8508808f00cSAlan Stern 			set_bit(port1, hub->change_bits);
8518808f00cSAlan Stern 
8526ee0b270SAlan Stern 		} else {
8536ee0b270SAlan Stern 			/* The power session is gone; tell khubd */
8546ee0b270SAlan Stern 			usb_set_device_state(udev, USB_STATE_NOTATTACHED);
8556ee0b270SAlan Stern 			set_bit(port1, hub->change_bits);
8565e6effaeSAlan Stern 		}
8575e6effaeSAlan Stern 	}
8585e6effaeSAlan Stern 
859948fea37SAlan Stern 	/* If no port-status-change flags were set, we don't need any
860948fea37SAlan Stern 	 * debouncing.  If flags were set we can try to debounce the
861948fea37SAlan Stern 	 * ports all at once right now, instead of letting khubd do them
862948fea37SAlan Stern 	 * one at a time later on.
863948fea37SAlan Stern 	 *
864948fea37SAlan Stern 	 * If any port-status changes do occur during this delay, khubd
865948fea37SAlan Stern 	 * will see them later and handle them normally.
866948fea37SAlan Stern 	 */
8678520f380SAlan Stern 	if (need_debounce_delay) {
8688520f380SAlan Stern 		delay = HUB_DEBOUNCE_STABLE;
869f2835219SAlan Stern 
8708520f380SAlan Stern 		/* Don't do a long sleep inside a workqueue routine */
8718520f380SAlan Stern 		if (type == HUB_INIT2) {
8728520f380SAlan Stern 			PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
8738520f380SAlan Stern 			schedule_delayed_work(&hub->init_work,
8748520f380SAlan Stern 					msecs_to_jiffies(delay));
8758520f380SAlan Stern 			return;		/* Continues at init3: below */
8768520f380SAlan Stern 		} else {
8778520f380SAlan Stern 			msleep(delay);
8788520f380SAlan Stern 		}
8798520f380SAlan Stern 	}
8808520f380SAlan Stern  init3:
881f2835219SAlan Stern 	hub->quiescing = 0;
882f2835219SAlan Stern 
883f2835219SAlan Stern 	status = usb_submit_urb(hub->urb, GFP_NOIO);
884f2835219SAlan Stern 	if (status < 0)
885f2835219SAlan Stern 		dev_err(hub->intfdev, "activate --> %d\n", status);
886f2835219SAlan Stern 	if (hub->has_indicators && blinkenlights)
887f2835219SAlan Stern 		schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
888f2835219SAlan Stern 
889f2835219SAlan Stern 	/* Scan all ports that need attention */
890f2835219SAlan Stern 	kick_khubd(hub);
8918e4ceb38SAlan Stern 
8928e4ceb38SAlan Stern 	/* Allow autosuspend if it was suppressed */
8938e4ceb38SAlan Stern 	if (type <= HUB_INIT3)
8948e4ceb38SAlan Stern 		usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
8955e6effaeSAlan Stern }
8965e6effaeSAlan Stern 
8978520f380SAlan Stern /* Implement the continuations for the delays above */
8988520f380SAlan Stern static void hub_init_func2(struct work_struct *ws)
8998520f380SAlan Stern {
9008520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
9018520f380SAlan Stern 
9028520f380SAlan Stern 	hub_activate(hub, HUB_INIT2);
9038520f380SAlan Stern }
9048520f380SAlan Stern 
9058520f380SAlan Stern static void hub_init_func3(struct work_struct *ws)
9068520f380SAlan Stern {
9078520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
9088520f380SAlan Stern 
9098520f380SAlan Stern 	hub_activate(hub, HUB_INIT3);
9108520f380SAlan Stern }
9118520f380SAlan Stern 
9124330354fSAlan Stern enum hub_quiescing_type {
9134330354fSAlan Stern 	HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
9144330354fSAlan Stern };
9154330354fSAlan Stern 
9164330354fSAlan Stern static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
9174330354fSAlan Stern {
9184330354fSAlan Stern 	struct usb_device *hdev = hub->hdev;
9194330354fSAlan Stern 	int i;
9204330354fSAlan Stern 
9218520f380SAlan Stern 	cancel_delayed_work_sync(&hub->init_work);
9228520f380SAlan Stern 
9234330354fSAlan Stern 	/* khubd and related activity won't re-trigger */
9244330354fSAlan Stern 	hub->quiescing = 1;
9254330354fSAlan Stern 
9264330354fSAlan Stern 	if (type != HUB_SUSPEND) {
9274330354fSAlan Stern 		/* Disconnect all the children */
9284330354fSAlan Stern 		for (i = 0; i < hdev->maxchild; ++i) {
9294330354fSAlan Stern 			if (hdev->children[i])
9304330354fSAlan Stern 				usb_disconnect(&hdev->children[i]);
9314330354fSAlan Stern 		}
9324330354fSAlan Stern 	}
9334330354fSAlan Stern 
9344330354fSAlan Stern 	/* Stop khubd and related activity */
9354330354fSAlan Stern 	usb_kill_urb(hub->urb);
9364330354fSAlan Stern 	if (hub->has_indicators)
9374330354fSAlan Stern 		cancel_delayed_work_sync(&hub->leds);
9384330354fSAlan Stern 	if (hub->tt.hub)
939cb88a1b8SAlan Stern 		cancel_work_sync(&hub->tt.clear_work);
9404330354fSAlan Stern }
9414330354fSAlan Stern 
9423eb14915SAlan Stern /* caller has locked the hub device */
9433eb14915SAlan Stern static int hub_pre_reset(struct usb_interface *intf)
9443eb14915SAlan Stern {
9453eb14915SAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
9463eb14915SAlan Stern 
9474330354fSAlan Stern 	hub_quiesce(hub, HUB_PRE_RESET);
948f07600cfSAlan Stern 	return 0;
9497d069b7dSAlan Stern }
9507d069b7dSAlan Stern 
9517d069b7dSAlan Stern /* caller has locked the hub device */
952f07600cfSAlan Stern static int hub_post_reset(struct usb_interface *intf)
9537d069b7dSAlan Stern {
9547de18d8bSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
9557de18d8bSAlan Stern 
956f2835219SAlan Stern 	hub_activate(hub, HUB_POST_RESET);
957f07600cfSAlan Stern 	return 0;
9587d069b7dSAlan Stern }
9597d069b7dSAlan Stern 
9601da177e4SLinus Torvalds static int hub_configure(struct usb_hub *hub,
9611da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint)
9621da177e4SLinus Torvalds {
963b356b7c7SSarah Sharp 	struct usb_hcd *hcd;
9641da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
9651da177e4SLinus Torvalds 	struct device *hub_dev = hub->intfdev;
9661da177e4SLinus Torvalds 	u16 hubstatus, hubchange;
96774ad9bd2SGreg Kroah-Hartman 	u16 wHubCharacteristics;
9681da177e4SLinus Torvalds 	unsigned int pipe;
9691da177e4SLinus Torvalds 	int maxp, ret;
9707cbe5dcaSAlan Stern 	char *message = "out of memory";
9711da177e4SLinus Torvalds 
972d697cddaSAlan Stern 	hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
9731da177e4SLinus Torvalds 	if (!hub->buffer) {
9741da177e4SLinus Torvalds 		ret = -ENOMEM;
9751da177e4SLinus Torvalds 		goto fail;
9761da177e4SLinus Torvalds 	}
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
9791da177e4SLinus Torvalds 	if (!hub->status) {
9801da177e4SLinus Torvalds 		ret = -ENOMEM;
9811da177e4SLinus Torvalds 		goto fail;
9821da177e4SLinus Torvalds 	}
983db90e7a1SAlan Stern 	mutex_init(&hub->status_mutex);
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
9861da177e4SLinus Torvalds 	if (!hub->descriptor) {
9871da177e4SLinus Torvalds 		ret = -ENOMEM;
9881da177e4SLinus Torvalds 		goto fail;
9891da177e4SLinus Torvalds 	}
9901da177e4SLinus Torvalds 
991dbe79bbeSJohn Youn 	if (hub_is_superspeed(hdev) && (hdev->parent != NULL)) {
992dbe79bbeSJohn Youn 		ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
993dbe79bbeSJohn Youn 				HUB_SET_DEPTH, USB_RT_HUB,
994dbe79bbeSJohn Youn 				hdev->level - 1, 0, NULL, 0,
995dbe79bbeSJohn Youn 				USB_CTRL_SET_TIMEOUT);
996dbe79bbeSJohn Youn 
997dbe79bbeSJohn Youn 		if (ret < 0) {
998dbe79bbeSJohn Youn 			message = "can't set hub depth";
999dbe79bbeSJohn Youn 			goto fail;
1000dbe79bbeSJohn Youn 		}
1001dbe79bbeSJohn Youn 	}
1002dbe79bbeSJohn Youn 
10031da177e4SLinus Torvalds 	/* Request the entire hub descriptor.
10041da177e4SLinus Torvalds 	 * hub->descriptor can handle USB_MAXCHILDREN ports,
10051da177e4SLinus Torvalds 	 * but the hub can/will return fewer bytes here.
10061da177e4SLinus Torvalds 	 */
1007dbe79bbeSJohn Youn 	ret = get_hub_descriptor(hdev, hub->descriptor);
10081da177e4SLinus Torvalds 	if (ret < 0) {
10091da177e4SLinus Torvalds 		message = "can't read hub descriptor";
10101da177e4SLinus Torvalds 		goto fail;
10111da177e4SLinus Torvalds 	} else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
10121da177e4SLinus Torvalds 		message = "hub has too many ports!";
10131da177e4SLinus Torvalds 		ret = -ENODEV;
10141da177e4SLinus Torvalds 		goto fail;
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	hdev->maxchild = hub->descriptor->bNbrPorts;
10181da177e4SLinus Torvalds 	dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
10191da177e4SLinus Torvalds 		(hdev->maxchild == 1) ? "" : "s");
10201da177e4SLinus Torvalds 
10217cbe5dcaSAlan Stern 	hub->port_owners = kzalloc(hdev->maxchild * sizeof(void *), GFP_KERNEL);
10227cbe5dcaSAlan Stern 	if (!hub->port_owners) {
10237cbe5dcaSAlan Stern 		ret = -ENOMEM;
10247cbe5dcaSAlan Stern 		goto fail;
10257cbe5dcaSAlan Stern 	}
10267cbe5dcaSAlan Stern 
102774ad9bd2SGreg Kroah-Hartman 	wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
10281da177e4SLinus Torvalds 
1029dbe79bbeSJohn Youn 	/* FIXME for USB 3.0, skip for now */
1030dbe79bbeSJohn Youn 	if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1031dbe79bbeSJohn Youn 			!(hub_is_superspeed(hdev))) {
10321da177e4SLinus Torvalds 		int	i;
10331da177e4SLinus Torvalds 		char	portstr [USB_MAXCHILDREN + 1];
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 		for (i = 0; i < hdev->maxchild; i++)
1036dbe79bbeSJohn Youn 			portstr[i] = hub->descriptor->u.hs.DeviceRemovable
10371da177e4SLinus Torvalds 				    [((i + 1) / 8)] & (1 << ((i + 1) % 8))
10381da177e4SLinus Torvalds 				? 'F' : 'R';
10391da177e4SLinus Torvalds 		portstr[hdev->maxchild] = 0;
10401da177e4SLinus Torvalds 		dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
10411da177e4SLinus Torvalds 	} else
10421da177e4SLinus Torvalds 		dev_dbg(hub_dev, "standalone hub\n");
10431da177e4SLinus Torvalds 
104474ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_LPSM) {
10451da177e4SLinus Torvalds 		case 0x00:
10461da177e4SLinus Torvalds 			dev_dbg(hub_dev, "ganged power switching\n");
10471da177e4SLinus Torvalds 			break;
10481da177e4SLinus Torvalds 		case 0x01:
10491da177e4SLinus Torvalds 			dev_dbg(hub_dev, "individual port power switching\n");
10501da177e4SLinus Torvalds 			break;
10511da177e4SLinus Torvalds 		case 0x02:
10521da177e4SLinus Torvalds 		case 0x03:
10531da177e4SLinus Torvalds 			dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
10541da177e4SLinus Torvalds 			break;
10551da177e4SLinus Torvalds 	}
10561da177e4SLinus Torvalds 
105774ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_OCPM) {
10581da177e4SLinus Torvalds 		case 0x00:
10591da177e4SLinus Torvalds 			dev_dbg(hub_dev, "global over-current protection\n");
10601da177e4SLinus Torvalds 			break;
10611da177e4SLinus Torvalds 		case 0x08:
10621da177e4SLinus Torvalds 			dev_dbg(hub_dev, "individual port over-current protection\n");
10631da177e4SLinus Torvalds 			break;
10641da177e4SLinus Torvalds 		case 0x10:
10651da177e4SLinus Torvalds 		case 0x18:
10661da177e4SLinus Torvalds 			dev_dbg(hub_dev, "no over-current protection\n");
10671da177e4SLinus Torvalds                         break;
10681da177e4SLinus Torvalds 	}
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	spin_lock_init (&hub->tt.lock);
10711da177e4SLinus Torvalds 	INIT_LIST_HEAD (&hub->tt.clear_list);
1072cb88a1b8SAlan Stern 	INIT_WORK(&hub->tt.clear_work, hub_tt_work);
10731da177e4SLinus Torvalds 	switch (hdev->descriptor.bDeviceProtocol) {
10741da177e4SLinus Torvalds 		case 0:
10751da177e4SLinus Torvalds 			break;
10761da177e4SLinus Torvalds 		case 1:
10771da177e4SLinus Torvalds 			dev_dbg(hub_dev, "Single TT\n");
10781da177e4SLinus Torvalds 			hub->tt.hub = hdev;
10791da177e4SLinus Torvalds 			break;
10801da177e4SLinus Torvalds 		case 2:
10811da177e4SLinus Torvalds 			ret = usb_set_interface(hdev, 0, 1);
10821da177e4SLinus Torvalds 			if (ret == 0) {
10831da177e4SLinus Torvalds 				dev_dbg(hub_dev, "TT per port\n");
10841da177e4SLinus Torvalds 				hub->tt.multi = 1;
10851da177e4SLinus Torvalds 			} else
10861da177e4SLinus Torvalds 				dev_err(hub_dev, "Using single TT (err %d)\n",
10871da177e4SLinus Torvalds 					ret);
10881da177e4SLinus Torvalds 			hub->tt.hub = hdev;
10891da177e4SLinus Torvalds 			break;
1090d2e9b4d6SSarah Sharp 		case 3:
1091d2e9b4d6SSarah Sharp 			/* USB 3.0 hubs don't have a TT */
1092d2e9b4d6SSarah Sharp 			break;
10931da177e4SLinus Torvalds 		default:
10941da177e4SLinus Torvalds 			dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
10951da177e4SLinus Torvalds 				hdev->descriptor.bDeviceProtocol);
10961da177e4SLinus Torvalds 			break;
10971da177e4SLinus Torvalds 	}
10981da177e4SLinus Torvalds 
1099e09711aeSdavid-b@pacbell.net 	/* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
110074ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_TTTT) {
1101e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_8_BITS:
1102e09711aeSdavid-b@pacbell.net 			if (hdev->descriptor.bDeviceProtocol != 0) {
1103e09711aeSdavid-b@pacbell.net 				hub->tt.think_time = 666;
1104e09711aeSdavid-b@pacbell.net 				dev_dbg(hub_dev, "TT requires at most %d "
1105e09711aeSdavid-b@pacbell.net 						"FS bit times (%d ns)\n",
1106e09711aeSdavid-b@pacbell.net 					8, hub->tt.think_time);
1107e09711aeSdavid-b@pacbell.net 			}
11081da177e4SLinus Torvalds 			break;
1109e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_16_BITS:
1110e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 2;
1111e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1112e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1113e09711aeSdavid-b@pacbell.net 				16, hub->tt.think_time);
11141da177e4SLinus Torvalds 			break;
1115e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_24_BITS:
1116e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 3;
1117e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1118e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1119e09711aeSdavid-b@pacbell.net 				24, hub->tt.think_time);
11201da177e4SLinus Torvalds 			break;
1121e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_32_BITS:
1122e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 4;
1123e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1124e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1125e09711aeSdavid-b@pacbell.net 				32, hub->tt.think_time);
11261da177e4SLinus Torvalds 			break;
11271da177e4SLinus Torvalds 	}
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	/* probe() zeroes hub->indicator[] */
113074ad9bd2SGreg Kroah-Hartman 	if (wHubCharacteristics & HUB_CHAR_PORTIND) {
11311da177e4SLinus Torvalds 		hub->has_indicators = 1;
11321da177e4SLinus Torvalds 		dev_dbg(hub_dev, "Port indicators are supported\n");
11331da177e4SLinus Torvalds 	}
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	dev_dbg(hub_dev, "power on to power good time: %dms\n",
11361da177e4SLinus Torvalds 		hub->descriptor->bPwrOn2PwrGood * 2);
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	/* power budgeting mostly matters with bus-powered hubs,
11391da177e4SLinus Torvalds 	 * and battery-powered root hubs (may provide just 8 mA).
11401da177e4SLinus Torvalds 	 */
11411da177e4SLinus Torvalds 	ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
114255c52718SAlan Stern 	if (ret < 2) {
11431da177e4SLinus Torvalds 		message = "can't get hub status";
11441da177e4SLinus Torvalds 		goto fail;
11451da177e4SLinus Torvalds 	}
11467d35b929SAlan Stern 	le16_to_cpus(&hubstatus);
11477d35b929SAlan Stern 	if (hdev == hdev->bus->root_hub) {
114855c52718SAlan Stern 		if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
114955c52718SAlan Stern 			hub->mA_per_port = 500;
115055c52718SAlan Stern 		else {
115155c52718SAlan Stern 			hub->mA_per_port = hdev->bus_mA;
115255c52718SAlan Stern 			hub->limited_power = 1;
115355c52718SAlan Stern 		}
11547d35b929SAlan Stern 	} else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
11551da177e4SLinus Torvalds 		dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
11561da177e4SLinus Torvalds 			hub->descriptor->bHubContrCurrent);
115755c52718SAlan Stern 		hub->limited_power = 1;
115855c52718SAlan Stern 		if (hdev->maxchild > 0) {
115955c52718SAlan Stern 			int remaining = hdev->bus_mA -
116055c52718SAlan Stern 					hub->descriptor->bHubContrCurrent;
11611da177e4SLinus Torvalds 
116255c52718SAlan Stern 			if (remaining < hdev->maxchild * 100)
116355c52718SAlan Stern 				dev_warn(hub_dev,
116455c52718SAlan Stern 					"insufficient power available "
116555c52718SAlan Stern 					"to use all downstream ports\n");
116655c52718SAlan Stern 			hub->mA_per_port = 100;		/* 7.2.1.1 */
116755c52718SAlan Stern 		}
116855c52718SAlan Stern 	} else {	/* Self-powered external hub */
116955c52718SAlan Stern 		/* FIXME: What about battery-powered external hubs that
117055c52718SAlan Stern 		 * provide less current per port? */
117155c52718SAlan Stern 		hub->mA_per_port = 500;
117255c52718SAlan Stern 	}
117355c52718SAlan Stern 	if (hub->mA_per_port < 500)
117455c52718SAlan Stern 		dev_dbg(hub_dev, "%umA bus power budget for each child\n",
117555c52718SAlan Stern 				hub->mA_per_port);
11761da177e4SLinus Torvalds 
1177b356b7c7SSarah Sharp 	/* Update the HCD's internal representation of this hub before khubd
1178b356b7c7SSarah Sharp 	 * starts getting port status changes for devices under the hub.
1179b356b7c7SSarah Sharp 	 */
1180b356b7c7SSarah Sharp 	hcd = bus_to_hcd(hdev->bus);
1181b356b7c7SSarah Sharp 	if (hcd->driver->update_hub_device) {
1182b356b7c7SSarah Sharp 		ret = hcd->driver->update_hub_device(hcd, hdev,
1183b356b7c7SSarah Sharp 				&hub->tt, GFP_KERNEL);
1184b356b7c7SSarah Sharp 		if (ret < 0) {
1185b356b7c7SSarah Sharp 			message = "can't update HCD hub info";
1186b356b7c7SSarah Sharp 			goto fail;
1187b356b7c7SSarah Sharp 		}
1188b356b7c7SSarah Sharp 	}
1189b356b7c7SSarah Sharp 
11901da177e4SLinus Torvalds 	ret = hub_hub_status(hub, &hubstatus, &hubchange);
11911da177e4SLinus Torvalds 	if (ret < 0) {
11921da177e4SLinus Torvalds 		message = "can't get hub status";
11931da177e4SLinus Torvalds 		goto fail;
11941da177e4SLinus Torvalds 	}
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	/* local power status reports aren't always correct */
11971da177e4SLinus Torvalds 	if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
11981da177e4SLinus Torvalds 		dev_dbg(hub_dev, "local power source is %s\n",
11991da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_LOCAL_POWER)
12001da177e4SLinus Torvalds 			? "lost (inactive)" : "good");
12011da177e4SLinus Torvalds 
120274ad9bd2SGreg Kroah-Hartman 	if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
12031da177e4SLinus Torvalds 		dev_dbg(hub_dev, "%sover-current condition exists\n",
12041da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
12051da177e4SLinus Torvalds 
120688fafff9Sinaky@linux.intel.com 	/* set up the interrupt endpoint
120788fafff9Sinaky@linux.intel.com 	 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
120888fafff9Sinaky@linux.intel.com 	 * bytes as USB2.0[11.12.3] says because some hubs are known
120988fafff9Sinaky@linux.intel.com 	 * to send more data (and thus cause overflow). For root hubs,
121088fafff9Sinaky@linux.intel.com 	 * maxpktsize is defined in hcd.c's fake endpoint descriptors
121188fafff9Sinaky@linux.intel.com 	 * to be big enough for at least USB_MAXCHILDREN ports. */
12121da177e4SLinus Torvalds 	pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
12131da177e4SLinus Torvalds 	maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 	if (maxp > sizeof(*hub->buffer))
12161da177e4SLinus Torvalds 		maxp = sizeof(*hub->buffer);
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	hub->urb = usb_alloc_urb(0, GFP_KERNEL);
12191da177e4SLinus Torvalds 	if (!hub->urb) {
12201da177e4SLinus Torvalds 		ret = -ENOMEM;
12211da177e4SLinus Torvalds 		goto fail;
12221da177e4SLinus Torvalds 	}
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
12251da177e4SLinus Torvalds 		hub, endpoint->bInterval);
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	/* maybe cycle the hub leds */
12281da177e4SLinus Torvalds 	if (hub->has_indicators && blinkenlights)
12291da177e4SLinus Torvalds 		hub->indicator [0] = INDICATOR_CYCLE;
12301da177e4SLinus Torvalds 
1231f2835219SAlan Stern 	hub_activate(hub, HUB_INIT);
12321da177e4SLinus Torvalds 	return 0;
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds fail:
12351da177e4SLinus Torvalds 	dev_err (hub_dev, "config failed, %s (err %d)\n",
12361da177e4SLinus Torvalds 			message, ret);
12371da177e4SLinus Torvalds 	/* hub_disconnect() frees urb and descriptor */
12381da177e4SLinus Torvalds 	return ret;
12391da177e4SLinus Torvalds }
12401da177e4SLinus Torvalds 
1241e8054854SAlan Stern static void hub_release(struct kref *kref)
1242e8054854SAlan Stern {
1243e8054854SAlan Stern 	struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1244e8054854SAlan Stern 
1245e8054854SAlan Stern 	usb_put_intf(to_usb_interface(hub->intfdev));
1246e8054854SAlan Stern 	kfree(hub);
1247e8054854SAlan Stern }
1248e8054854SAlan Stern 
12491da177e4SLinus Torvalds static unsigned highspeed_hubs;
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds static void hub_disconnect(struct usb_interface *intf)
12521da177e4SLinus Torvalds {
12531da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata (intf);
1254e8054854SAlan Stern 
1255e8054854SAlan Stern 	/* Take the hub off the event list and don't let it be added again */
1256e8054854SAlan Stern 	spin_lock_irq(&hub_event_lock);
12578e4ceb38SAlan Stern 	if (!list_empty(&hub->event_list)) {
1258e8054854SAlan Stern 		list_del_init(&hub->event_list);
12598e4ceb38SAlan Stern 		usb_autopm_put_interface_no_suspend(intf);
12608e4ceb38SAlan Stern 	}
1261e8054854SAlan Stern 	hub->disconnected = 1;
1262e8054854SAlan Stern 	spin_unlock_irq(&hub_event_lock);
12631da177e4SLinus Torvalds 
12647de18d8bSAlan Stern 	/* Disconnect all children and quiesce the hub */
12657de18d8bSAlan Stern 	hub->error = 0;
12664330354fSAlan Stern 	hub_quiesce(hub, HUB_DISCONNECT);
12677de18d8bSAlan Stern 
12688b28c752SAlan Stern 	usb_set_intfdata (intf, NULL);
12697cbe5dcaSAlan Stern 	hub->hdev->maxchild = 0;
12701da177e4SLinus Torvalds 
1271e8054854SAlan Stern 	if (hub->hdev->speed == USB_SPEED_HIGH)
12721da177e4SLinus Torvalds 		highspeed_hubs--;
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	usb_free_urb(hub->urb);
12757cbe5dcaSAlan Stern 	kfree(hub->port_owners);
12761da177e4SLinus Torvalds 	kfree(hub->descriptor);
12771da177e4SLinus Torvalds 	kfree(hub->status);
1278d697cddaSAlan Stern 	kfree(hub->buffer);
12791da177e4SLinus Torvalds 
1280e8054854SAlan Stern 	kref_put(&hub->kref, hub_release);
12811da177e4SLinus Torvalds }
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
12841da177e4SLinus Torvalds {
12851da177e4SLinus Torvalds 	struct usb_host_interface *desc;
12861da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint;
12871da177e4SLinus Torvalds 	struct usb_device *hdev;
12881da177e4SLinus Torvalds 	struct usb_hub *hub;
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	desc = intf->cur_altsetting;
12911da177e4SLinus Torvalds 	hdev = interface_to_usbdev(intf);
12921da177e4SLinus Torvalds 
12930c9ffe0fSSarah Sharp 	/* Hubs have proper suspend/resume support.  USB 3.0 device suspend is
12940c9ffe0fSSarah Sharp 	 * different from USB 2.0/1.1 device suspend, and unfortunately we
12950c9ffe0fSSarah Sharp 	 * don't support it yet.  So leave autosuspend disabled for USB 3.0
12960c9ffe0fSSarah Sharp 	 * external hubs for now.  Enable autosuspend for USB 3.0 roothubs,
12970c9ffe0fSSarah Sharp 	 * since that isn't a "real" hub.
12980c9ffe0fSSarah Sharp 	 */
12990c9ffe0fSSarah Sharp 	if (!hub_is_superspeed(hdev) || !hdev->parent)
1300088f7fecSAlan Stern 		usb_enable_autosuspend(hdev);
1301088f7fecSAlan Stern 
130238f3ad5eSFelipe Balbi 	if (hdev->level == MAX_TOPO_LEVEL) {
1303b9cef6c3SWu Fengguang 		dev_err(&intf->dev,
1304b9cef6c3SWu Fengguang 			"Unsupported bus topology: hub nested too deep\n");
130538f3ad5eSFelipe Balbi 		return -E2BIG;
130638f3ad5eSFelipe Balbi 	}
130738f3ad5eSFelipe Balbi 
130889ccbdc9SDavid Brownell #ifdef	CONFIG_USB_OTG_BLACKLIST_HUB
130989ccbdc9SDavid Brownell 	if (hdev->parent) {
131089ccbdc9SDavid Brownell 		dev_warn(&intf->dev, "ignoring external hub\n");
131189ccbdc9SDavid Brownell 		return -ENODEV;
131289ccbdc9SDavid Brownell 	}
131389ccbdc9SDavid Brownell #endif
131489ccbdc9SDavid Brownell 
13151da177e4SLinus Torvalds 	/* Some hubs have a subclass of 1, which AFAICT according to the */
13161da177e4SLinus Torvalds 	/*  specs is not defined, but it works */
13171da177e4SLinus Torvalds 	if ((desc->desc.bInterfaceSubClass != 0) &&
13181da177e4SLinus Torvalds 	    (desc->desc.bInterfaceSubClass != 1)) {
13191da177e4SLinus Torvalds descriptor_error:
13201da177e4SLinus Torvalds 		dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
13211da177e4SLinus Torvalds 		return -EIO;
13221da177e4SLinus Torvalds 	}
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	/* Multiple endpoints? What kind of mutant ninja-hub is this? */
13251da177e4SLinus Torvalds 	if (desc->desc.bNumEndpoints != 1)
13261da177e4SLinus Torvalds 		goto descriptor_error;
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 	endpoint = &desc->endpoint[0].desc;
13291da177e4SLinus Torvalds 
1330fbf81c29SLuiz Fernando N. Capitulino 	/* If it's not an interrupt in endpoint, we'd better punt! */
1331fbf81c29SLuiz Fernando N. Capitulino 	if (!usb_endpoint_is_int_in(endpoint))
13321da177e4SLinus Torvalds 		goto descriptor_error;
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 	/* We found a hub */
13351da177e4SLinus Torvalds 	dev_info (&intf->dev, "USB hub found\n");
13361da177e4SLinus Torvalds 
13370a1ef3b5SAlan Stern 	hub = kzalloc(sizeof(*hub), GFP_KERNEL);
13381da177e4SLinus Torvalds 	if (!hub) {
13391da177e4SLinus Torvalds 		dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
13401da177e4SLinus Torvalds 		return -ENOMEM;
13411da177e4SLinus Torvalds 	}
13421da177e4SLinus Torvalds 
1343e8054854SAlan Stern 	kref_init(&hub->kref);
13441da177e4SLinus Torvalds 	INIT_LIST_HEAD(&hub->event_list);
13451da177e4SLinus Torvalds 	hub->intfdev = &intf->dev;
13461da177e4SLinus Torvalds 	hub->hdev = hdev;
1347c4028958SDavid Howells 	INIT_DELAYED_WORK(&hub->leds, led_work);
13488520f380SAlan Stern 	INIT_DELAYED_WORK(&hub->init_work, NULL);
1349e8054854SAlan Stern 	usb_get_intf(intf);
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	usb_set_intfdata (intf, hub);
135240f122f3SAlan Stern 	intf->needs_remote_wakeup = 1;
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	if (hdev->speed == USB_SPEED_HIGH)
13551da177e4SLinus Torvalds 		highspeed_hubs++;
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	if (hub_configure(hub, endpoint) >= 0)
13581da177e4SLinus Torvalds 		return 0;
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	hub_disconnect (intf);
13611da177e4SLinus Torvalds 	return -ENODEV;
13621da177e4SLinus Torvalds }
13631da177e4SLinus Torvalds 
1364c532b29aSAndi Kleen /* No BKL needed */
13651da177e4SLinus Torvalds static int
13661da177e4SLinus Torvalds hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
13671da177e4SLinus Torvalds {
13681da177e4SLinus Torvalds 	struct usb_device *hdev = interface_to_usbdev (intf);
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 	/* assert ifno == 0 (part of hub spec) */
13711da177e4SLinus Torvalds 	switch (code) {
13721da177e4SLinus Torvalds 	case USBDEVFS_HUB_PORTINFO: {
13731da177e4SLinus Torvalds 		struct usbdevfs_hub_portinfo *info = user_data;
13741da177e4SLinus Torvalds 		int i;
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
13771da177e4SLinus Torvalds 		if (hdev->devnum <= 0)
13781da177e4SLinus Torvalds 			info->nports = 0;
13791da177e4SLinus Torvalds 		else {
13801da177e4SLinus Torvalds 			info->nports = hdev->maxchild;
13811da177e4SLinus Torvalds 			for (i = 0; i < info->nports; i++) {
13821da177e4SLinus Torvalds 				if (hdev->children[i] == NULL)
13831da177e4SLinus Torvalds 					info->port[i] = 0;
13841da177e4SLinus Torvalds 				else
13851da177e4SLinus Torvalds 					info->port[i] =
13861da177e4SLinus Torvalds 						hdev->children[i]->devnum;
13871da177e4SLinus Torvalds 			}
13881da177e4SLinus Torvalds 		}
13891da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 		return info->nports + 1;
13921da177e4SLinus Torvalds 		}
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 	default:
13951da177e4SLinus Torvalds 		return -ENOSYS;
13961da177e4SLinus Torvalds 	}
13971da177e4SLinus Torvalds }
13981da177e4SLinus Torvalds 
13997cbe5dcaSAlan Stern /*
14007cbe5dcaSAlan Stern  * Allow user programs to claim ports on a hub.  When a device is attached
14017cbe5dcaSAlan Stern  * to one of these "claimed" ports, the program will "own" the device.
14027cbe5dcaSAlan Stern  */
14037cbe5dcaSAlan Stern static int find_port_owner(struct usb_device *hdev, unsigned port1,
14047cbe5dcaSAlan Stern 		void ***ppowner)
14057cbe5dcaSAlan Stern {
14067cbe5dcaSAlan Stern 	if (hdev->state == USB_STATE_NOTATTACHED)
14077cbe5dcaSAlan Stern 		return -ENODEV;
14087cbe5dcaSAlan Stern 	if (port1 == 0 || port1 > hdev->maxchild)
14097cbe5dcaSAlan Stern 		return -EINVAL;
14107cbe5dcaSAlan Stern 
14117cbe5dcaSAlan Stern 	/* This assumes that devices not managed by the hub driver
14127cbe5dcaSAlan Stern 	 * will always have maxchild equal to 0.
14137cbe5dcaSAlan Stern 	 */
14147cbe5dcaSAlan Stern 	*ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]);
14157cbe5dcaSAlan Stern 	return 0;
14167cbe5dcaSAlan Stern }
14177cbe5dcaSAlan Stern 
14187cbe5dcaSAlan Stern /* In the following three functions, the caller must hold hdev's lock */
14197cbe5dcaSAlan Stern int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, void *owner)
14207cbe5dcaSAlan Stern {
14217cbe5dcaSAlan Stern 	int rc;
14227cbe5dcaSAlan Stern 	void **powner;
14237cbe5dcaSAlan Stern 
14247cbe5dcaSAlan Stern 	rc = find_port_owner(hdev, port1, &powner);
14257cbe5dcaSAlan Stern 	if (rc)
14267cbe5dcaSAlan Stern 		return rc;
14277cbe5dcaSAlan Stern 	if (*powner)
14287cbe5dcaSAlan Stern 		return -EBUSY;
14297cbe5dcaSAlan Stern 	*powner = owner;
14307cbe5dcaSAlan Stern 	return rc;
14317cbe5dcaSAlan Stern }
14327cbe5dcaSAlan Stern 
14337cbe5dcaSAlan Stern int usb_hub_release_port(struct usb_device *hdev, unsigned port1, void *owner)
14347cbe5dcaSAlan Stern {
14357cbe5dcaSAlan Stern 	int rc;
14367cbe5dcaSAlan Stern 	void **powner;
14377cbe5dcaSAlan Stern 
14387cbe5dcaSAlan Stern 	rc = find_port_owner(hdev, port1, &powner);
14397cbe5dcaSAlan Stern 	if (rc)
14407cbe5dcaSAlan Stern 		return rc;
14417cbe5dcaSAlan Stern 	if (*powner != owner)
14427cbe5dcaSAlan Stern 		return -ENOENT;
14437cbe5dcaSAlan Stern 	*powner = NULL;
14447cbe5dcaSAlan Stern 	return rc;
14457cbe5dcaSAlan Stern }
14467cbe5dcaSAlan Stern 
14477cbe5dcaSAlan Stern void usb_hub_release_all_ports(struct usb_device *hdev, void *owner)
14487cbe5dcaSAlan Stern {
14497cbe5dcaSAlan Stern 	int n;
14507cbe5dcaSAlan Stern 	void **powner;
14517cbe5dcaSAlan Stern 
14527cbe5dcaSAlan Stern 	n = find_port_owner(hdev, 1, &powner);
14537cbe5dcaSAlan Stern 	if (n == 0) {
14547cbe5dcaSAlan Stern 		for (; n < hdev->maxchild; (++n, ++powner)) {
14557cbe5dcaSAlan Stern 			if (*powner == owner)
14567cbe5dcaSAlan Stern 				*powner = NULL;
14577cbe5dcaSAlan Stern 		}
14587cbe5dcaSAlan Stern 	}
14597cbe5dcaSAlan Stern }
14607cbe5dcaSAlan Stern 
14617cbe5dcaSAlan Stern /* The caller must hold udev's lock */
14627cbe5dcaSAlan Stern bool usb_device_is_owned(struct usb_device *udev)
14637cbe5dcaSAlan Stern {
14647cbe5dcaSAlan Stern 	struct usb_hub *hub;
14657cbe5dcaSAlan Stern 
14667cbe5dcaSAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
14677cbe5dcaSAlan Stern 		return false;
14687cbe5dcaSAlan Stern 	hub = hdev_to_hub(udev->parent);
14697cbe5dcaSAlan Stern 	return !!hub->port_owners[udev->portnum - 1];
14707cbe5dcaSAlan Stern }
14717cbe5dcaSAlan Stern 
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds static void recursively_mark_NOTATTACHED(struct usb_device *udev)
14741da177e4SLinus Torvalds {
14751da177e4SLinus Torvalds 	int i;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 	for (i = 0; i < udev->maxchild; ++i) {
14781da177e4SLinus Torvalds 		if (udev->children[i])
14791da177e4SLinus Torvalds 			recursively_mark_NOTATTACHED(udev->children[i]);
14801da177e4SLinus Torvalds 	}
14819bbdf1e0SAlan Stern 	if (udev->state == USB_STATE_SUSPENDED)
148215123006SSarah Sharp 		udev->active_duration -= jiffies;
14831da177e4SLinus Torvalds 	udev->state = USB_STATE_NOTATTACHED;
14841da177e4SLinus Torvalds }
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds /**
14871da177e4SLinus Torvalds  * usb_set_device_state - change a device's current state (usbcore, hcds)
14881da177e4SLinus Torvalds  * @udev: pointer to device whose state should be changed
14891da177e4SLinus Torvalds  * @new_state: new state value to be stored
14901da177e4SLinus Torvalds  *
14911da177e4SLinus Torvalds  * udev->state is _not_ fully protected by the device lock.  Although
14921da177e4SLinus Torvalds  * most transitions are made only while holding the lock, the state can
14931da177e4SLinus Torvalds  * can change to USB_STATE_NOTATTACHED at almost any time.  This
14941da177e4SLinus Torvalds  * is so that devices can be marked as disconnected as soon as possible,
14951da177e4SLinus Torvalds  * without having to wait for any semaphores to be released.  As a result,
14961da177e4SLinus Torvalds  * all changes to any device's state must be protected by the
14971da177e4SLinus Torvalds  * device_state_lock spinlock.
14981da177e4SLinus Torvalds  *
14991da177e4SLinus Torvalds  * Once a device has been added to the device tree, all changes to its state
15001da177e4SLinus Torvalds  * should be made using this routine.  The state should _not_ be set directly.
15011da177e4SLinus Torvalds  *
15021da177e4SLinus Torvalds  * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
15031da177e4SLinus Torvalds  * Otherwise udev->state is set to new_state, and if new_state is
15041da177e4SLinus Torvalds  * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
15051da177e4SLinus Torvalds  * to USB_STATE_NOTATTACHED.
15061da177e4SLinus Torvalds  */
15071da177e4SLinus Torvalds void usb_set_device_state(struct usb_device *udev,
15081da177e4SLinus Torvalds 		enum usb_device_state new_state)
15091da177e4SLinus Torvalds {
15101da177e4SLinus Torvalds 	unsigned long flags;
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 	spin_lock_irqsave(&device_state_lock, flags);
15131da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED)
15141da177e4SLinus Torvalds 		;	/* do nothing */
1515b94dc6b5SDavid Brownell 	else if (new_state != USB_STATE_NOTATTACHED) {
1516fb669cc0SDavid Brownell 
1517fb669cc0SDavid Brownell 		/* root hub wakeup capabilities are managed out-of-band
1518fb669cc0SDavid Brownell 		 * and may involve silicon errata ... ignore them here.
1519fb669cc0SDavid Brownell 		 */
1520fb669cc0SDavid Brownell 		if (udev->parent) {
1521645daaabSAlan Stern 			if (udev->state == USB_STATE_SUSPENDED
1522645daaabSAlan Stern 					|| new_state == USB_STATE_SUSPENDED)
1523645daaabSAlan Stern 				;	/* No change to wakeup settings */
1524645daaabSAlan Stern 			else if (new_state == USB_STATE_CONFIGURED)
152516985408SDan Streetman 				device_set_wakeup_capable(&udev->dev,
1526b94dc6b5SDavid Brownell 					(udev->actconfig->desc.bmAttributes
1527b94dc6b5SDavid Brownell 					 & USB_CONFIG_ATT_WAKEUP));
1528645daaabSAlan Stern 			else
152916985408SDan Streetman 				device_set_wakeup_capable(&udev->dev, 0);
1530fb669cc0SDavid Brownell 		}
153115123006SSarah Sharp 		if (udev->state == USB_STATE_SUSPENDED &&
153215123006SSarah Sharp 			new_state != USB_STATE_SUSPENDED)
153315123006SSarah Sharp 			udev->active_duration -= jiffies;
153415123006SSarah Sharp 		else if (new_state == USB_STATE_SUSPENDED &&
153515123006SSarah Sharp 				udev->state != USB_STATE_SUSPENDED)
153615123006SSarah Sharp 			udev->active_duration += jiffies;
1537645daaabSAlan Stern 		udev->state = new_state;
1538b94dc6b5SDavid Brownell 	} else
15391da177e4SLinus Torvalds 		recursively_mark_NOTATTACHED(udev);
15401da177e4SLinus Torvalds 	spin_unlock_irqrestore(&device_state_lock, flags);
15411da177e4SLinus Torvalds }
15426da9c990SDavid Vrabel EXPORT_SYMBOL_GPL(usb_set_device_state);
15431da177e4SLinus Torvalds 
15448af548dcSInaky Perez-Gonzalez /*
15453b29b68bSAlan Stern  * Choose a device number.
15463b29b68bSAlan Stern  *
15473b29b68bSAlan Stern  * Device numbers are used as filenames in usbfs.  On USB-1.1 and
15483b29b68bSAlan Stern  * USB-2.0 buses they are also used as device addresses, however on
15493b29b68bSAlan Stern  * USB-3.0 buses the address is assigned by the controller hardware
15503b29b68bSAlan Stern  * and it usually is not the same as the device number.
15513b29b68bSAlan Stern  *
15528af548dcSInaky Perez-Gonzalez  * WUSB devices are simple: they have no hubs behind, so the mapping
15538af548dcSInaky Perez-Gonzalez  * device <-> virtual port number becomes 1:1. Why? to simplify the
15548af548dcSInaky Perez-Gonzalez  * life of the device connection logic in
15558af548dcSInaky Perez-Gonzalez  * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
15568af548dcSInaky Perez-Gonzalez  * handshake we need to assign a temporary address in the unauthorized
15578af548dcSInaky Perez-Gonzalez  * space. For simplicity we use the first virtual port number found to
15588af548dcSInaky Perez-Gonzalez  * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
15598af548dcSInaky Perez-Gonzalez  * and that becomes it's address [X < 128] or its unauthorized address
15608af548dcSInaky Perez-Gonzalez  * [X | 0x80].
15618af548dcSInaky Perez-Gonzalez  *
15628af548dcSInaky Perez-Gonzalez  * We add 1 as an offset to the one-based USB-stack port number
15638af548dcSInaky Perez-Gonzalez  * (zero-based wusb virtual port index) for two reasons: (a) dev addr
15648af548dcSInaky Perez-Gonzalez  * 0 is reserved by USB for default address; (b) Linux's USB stack
15658af548dcSInaky Perez-Gonzalez  * uses always #1 for the root hub of the controller. So USB stack's
15668af548dcSInaky Perez-Gonzalez  * port #1, which is wusb virtual-port #0 has address #2.
1567c6515272SSarah Sharp  *
1568c6515272SSarah Sharp  * Devices connected under xHCI are not as simple.  The host controller
1569c6515272SSarah Sharp  * supports virtualization, so the hardware assigns device addresses and
1570c6515272SSarah Sharp  * the HCD must setup data structures before issuing a set address
1571c6515272SSarah Sharp  * command to the hardware.
15728af548dcSInaky Perez-Gonzalez  */
15733b29b68bSAlan Stern static void choose_devnum(struct usb_device *udev)
15741da177e4SLinus Torvalds {
15751da177e4SLinus Torvalds 	int		devnum;
15761da177e4SLinus Torvalds 	struct usb_bus	*bus = udev->bus;
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds 	/* If khubd ever becomes multithreaded, this will need a lock */
15798af548dcSInaky Perez-Gonzalez 	if (udev->wusb) {
15808af548dcSInaky Perez-Gonzalez 		devnum = udev->portnum + 1;
15818af548dcSInaky Perez-Gonzalez 		BUG_ON(test_bit(devnum, bus->devmap.devicemap));
15828af548dcSInaky Perez-Gonzalez 	} else {
15838af548dcSInaky Perez-Gonzalez 		/* Try to allocate the next devnum beginning at
15848af548dcSInaky Perez-Gonzalez 		 * bus->devnum_next. */
15851da177e4SLinus Torvalds 		devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
15861da177e4SLinus Torvalds 					    bus->devnum_next);
15871da177e4SLinus Torvalds 		if (devnum >= 128)
15888af548dcSInaky Perez-Gonzalez 			devnum = find_next_zero_bit(bus->devmap.devicemap,
15898af548dcSInaky Perez-Gonzalez 						    128, 1);
15901da177e4SLinus Torvalds 		bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
15918af548dcSInaky Perez-Gonzalez 	}
15921da177e4SLinus Torvalds 	if (devnum < 128) {
15931da177e4SLinus Torvalds 		set_bit(devnum, bus->devmap.devicemap);
15941da177e4SLinus Torvalds 		udev->devnum = devnum;
15951da177e4SLinus Torvalds 	}
15961da177e4SLinus Torvalds }
15971da177e4SLinus Torvalds 
15983b29b68bSAlan Stern static void release_devnum(struct usb_device *udev)
15991da177e4SLinus Torvalds {
16001da177e4SLinus Torvalds 	if (udev->devnum > 0) {
16011da177e4SLinus Torvalds 		clear_bit(udev->devnum, udev->bus->devmap.devicemap);
16021da177e4SLinus Torvalds 		udev->devnum = -1;
16031da177e4SLinus Torvalds 	}
16041da177e4SLinus Torvalds }
16051da177e4SLinus Torvalds 
16063b29b68bSAlan Stern static void update_devnum(struct usb_device *udev, int devnum)
16074953d141SDavid Vrabel {
16084953d141SDavid Vrabel 	/* The address for a WUSB device is managed by wusbcore. */
16094953d141SDavid Vrabel 	if (!udev->wusb)
16104953d141SDavid Vrabel 		udev->devnum = devnum;
16114953d141SDavid Vrabel }
16124953d141SDavid Vrabel 
1613f7410cedSHerbert Xu static void hub_free_dev(struct usb_device *udev)
1614f7410cedSHerbert Xu {
1615f7410cedSHerbert Xu 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1616f7410cedSHerbert Xu 
1617f7410cedSHerbert Xu 	/* Root hubs aren't real devices, so don't free HCD resources */
1618f7410cedSHerbert Xu 	if (hcd->driver->free_dev && udev->parent)
1619f7410cedSHerbert Xu 		hcd->driver->free_dev(hcd, udev);
1620f7410cedSHerbert Xu }
1621f7410cedSHerbert Xu 
16221da177e4SLinus Torvalds /**
16231da177e4SLinus Torvalds  * usb_disconnect - disconnect a device (usbcore-internal)
16241da177e4SLinus Torvalds  * @pdev: pointer to device being disconnected
16251da177e4SLinus Torvalds  * Context: !in_interrupt ()
16261da177e4SLinus Torvalds  *
16271da177e4SLinus Torvalds  * Something got disconnected. Get rid of it and all of its children.
16281da177e4SLinus Torvalds  *
16291da177e4SLinus Torvalds  * If *pdev is a normal device then the parent hub must already be locked.
16301da177e4SLinus Torvalds  * If *pdev is a root hub then this routine will acquire the
16311da177e4SLinus Torvalds  * usb_bus_list_lock on behalf of the caller.
16321da177e4SLinus Torvalds  *
16331da177e4SLinus Torvalds  * Only hub drivers (including virtual root hub drivers for host
16341da177e4SLinus Torvalds  * controllers) should ever call this.
16351da177e4SLinus Torvalds  *
16361da177e4SLinus Torvalds  * This call is synchronous, and may not be used in an interrupt context.
16371da177e4SLinus Torvalds  */
16381da177e4SLinus Torvalds void usb_disconnect(struct usb_device **pdev)
16391da177e4SLinus Torvalds {
16401da177e4SLinus Torvalds 	struct usb_device	*udev = *pdev;
16411da177e4SLinus Torvalds 	int			i;
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 	if (!udev) {
1644441b62c1SHarvey Harrison 		pr_debug ("%s nodev\n", __func__);
16451da177e4SLinus Torvalds 		return;
16461da177e4SLinus Torvalds 	}
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	/* mark the device as inactive, so any further urb submissions for
16491da177e4SLinus Torvalds 	 * this device (and any of its children) will fail immediately.
16501da177e4SLinus Torvalds 	 * this quiesces everyting except pending urbs.
16511da177e4SLinus Torvalds 	 */
16521da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
16533b29b68bSAlan Stern 	dev_info(&udev->dev, "USB disconnect, device number %d\n",
16543b29b68bSAlan Stern 			udev->devnum);
16551da177e4SLinus Torvalds 
16569ad3d6ccSAlan Stern 	usb_lock_device(udev);
16579ad3d6ccSAlan Stern 
16581da177e4SLinus Torvalds 	/* Free up all the children before we remove this device */
16591da177e4SLinus Torvalds 	for (i = 0; i < USB_MAXCHILDREN; i++) {
16601da177e4SLinus Torvalds 		if (udev->children[i])
16611da177e4SLinus Torvalds 			usb_disconnect(&udev->children[i]);
16621da177e4SLinus Torvalds 	}
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	/* deallocate hcd/hardware state ... nuking all pending urbs and
16651da177e4SLinus Torvalds 	 * cleaning up all state associated with the current configuration
16661da177e4SLinus Torvalds 	 * so that the hardware is now fully quiesced.
16671da177e4SLinus Torvalds 	 */
1668782da727SAlan Stern 	dev_dbg (&udev->dev, "unregistering device\n");
16691da177e4SLinus Torvalds 	usb_disable_device(udev, 0);
1670cde217a5SAlan Stern 	usb_hcd_synchronize_unlinks(udev);
16711da177e4SLinus Torvalds 
16723b23dd6fSAlan Stern 	usb_remove_ep_devs(&udev->ep0);
1673782da727SAlan Stern 	usb_unlock_device(udev);
16743099e75aSGreg Kroah-Hartman 
1675782da727SAlan Stern 	/* Unregister the device.  The device driver is responsible
16763b23dd6fSAlan Stern 	 * for de-configuring the device and invoking the remove-device
16773b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
1678782da727SAlan Stern 	 */
1679782da727SAlan Stern 	device_del(&udev->dev);
1680782da727SAlan Stern 
1681782da727SAlan Stern 	/* Free the device number and delete the parent's children[]
16821da177e4SLinus Torvalds 	 * (or root_hub) pointer.
16831da177e4SLinus Torvalds 	 */
16843b29b68bSAlan Stern 	release_devnum(udev);
16851da177e4SLinus Torvalds 
16861da177e4SLinus Torvalds 	/* Avoid races with recursively_mark_NOTATTACHED() */
16871da177e4SLinus Torvalds 	spin_lock_irq(&device_state_lock);
16881da177e4SLinus Torvalds 	*pdev = NULL;
16891da177e4SLinus Torvalds 	spin_unlock_irq(&device_state_lock);
16901da177e4SLinus Torvalds 
1691f7410cedSHerbert Xu 	hub_free_dev(udev);
1692f7410cedSHerbert Xu 
1693782da727SAlan Stern 	put_device(&udev->dev);
16941da177e4SLinus Torvalds }
16951da177e4SLinus Torvalds 
1696f2a383e4SGreg Kroah-Hartman #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
16971da177e4SLinus Torvalds static void show_string(struct usb_device *udev, char *id, char *string)
16981da177e4SLinus Torvalds {
16991da177e4SLinus Torvalds 	if (!string)
17001da177e4SLinus Torvalds 		return;
17011da177e4SLinus Torvalds 	dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
17021da177e4SLinus Torvalds }
17031da177e4SLinus Torvalds 
1704f2a383e4SGreg Kroah-Hartman static void announce_device(struct usb_device *udev)
1705f2a383e4SGreg Kroah-Hartman {
1706f2a383e4SGreg Kroah-Hartman 	dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
1707f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idVendor),
1708f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idProduct));
1709b9cef6c3SWu Fengguang 	dev_info(&udev->dev,
1710b9cef6c3SWu Fengguang 		"New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
1711f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iManufacturer,
1712f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iProduct,
1713f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iSerialNumber);
1714f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Product", udev->product);
1715f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Manufacturer", udev->manufacturer);
1716f2a383e4SGreg Kroah-Hartman 	show_string(udev, "SerialNumber", udev->serial);
1717f2a383e4SGreg Kroah-Hartman }
17181da177e4SLinus Torvalds #else
1719f2a383e4SGreg Kroah-Hartman static inline void announce_device(struct usb_device *udev) { }
17201da177e4SLinus Torvalds #endif
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
17231da177e4SLinus Torvalds #include "otg_whitelist.h"
17241da177e4SLinus Torvalds #endif
17251da177e4SLinus Torvalds 
17263ede760fSOliver Neukum /**
17278d8558d1SAlan Stern  * usb_enumerate_device_otg - FIXME (usbcore-internal)
17283ede760fSOliver Neukum  * @udev: newly addressed device (in ADDRESS state)
17293ede760fSOliver Neukum  *
17308d8558d1SAlan Stern  * Finish enumeration for On-The-Go devices
17313ede760fSOliver Neukum  */
17328d8558d1SAlan Stern static int usb_enumerate_device_otg(struct usb_device *udev)
17331da177e4SLinus Torvalds {
1734d9d16e8aSInaky Perez-Gonzalez 	int err = 0;
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
17371da177e4SLinus Torvalds 	/*
17381da177e4SLinus Torvalds 	 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
17391da177e4SLinus Torvalds 	 * to wake us after we've powered off VBUS; and HNP, switching roles
17401da177e4SLinus Torvalds 	 * "host" to "peripheral".  The OTG descriptor helps figure this out.
17411da177e4SLinus Torvalds 	 */
17421da177e4SLinus Torvalds 	if (!udev->bus->is_b_host
17431da177e4SLinus Torvalds 			&& udev->config
17441da177e4SLinus Torvalds 			&& udev->parent == udev->bus->root_hub) {
17452eb5052eSFelipe Balbi 		struct usb_otg_descriptor	*desc = NULL;
17461da177e4SLinus Torvalds 		struct usb_bus			*bus = udev->bus;
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 		/* descriptor may appear anywhere in config */
17491da177e4SLinus Torvalds 		if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
17501da177e4SLinus Torvalds 					le16_to_cpu(udev->config[0].desc.wTotalLength),
17511da177e4SLinus Torvalds 					USB_DT_OTG, (void **) &desc) == 0) {
17521da177e4SLinus Torvalds 			if (desc->bmAttributes & USB_OTG_HNP) {
175312c3da34SAlan Stern 				unsigned		port1 = udev->portnum;
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 				dev_info(&udev->dev,
17561da177e4SLinus Torvalds 					"Dual-Role OTG device on %sHNP port\n",
17571da177e4SLinus Torvalds 					(port1 == bus->otg_port)
17581da177e4SLinus Torvalds 						? "" : "non-");
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds 				/* enable HNP before suspend, it's simpler */
17611da177e4SLinus Torvalds 				if (port1 == bus->otg_port)
17621da177e4SLinus Torvalds 					bus->b_hnp_enable = 1;
17631da177e4SLinus Torvalds 				err = usb_control_msg(udev,
17641da177e4SLinus Torvalds 					usb_sndctrlpipe(udev, 0),
17651da177e4SLinus Torvalds 					USB_REQ_SET_FEATURE, 0,
17661da177e4SLinus Torvalds 					bus->b_hnp_enable
17671da177e4SLinus Torvalds 						? USB_DEVICE_B_HNP_ENABLE
17681da177e4SLinus Torvalds 						: USB_DEVICE_A_ALT_HNP_SUPPORT,
17691da177e4SLinus Torvalds 					0, NULL, 0, USB_CTRL_SET_TIMEOUT);
17701da177e4SLinus Torvalds 				if (err < 0) {
17711da177e4SLinus Torvalds 					/* OTG MESSAGE: report errors here,
17721da177e4SLinus Torvalds 					 * customize to match your product.
17731da177e4SLinus Torvalds 					 */
17741da177e4SLinus Torvalds 					dev_info(&udev->dev,
1775b9cef6c3SWu Fengguang 						"can't set HNP mode: %d\n",
17761da177e4SLinus Torvalds 						err);
17771da177e4SLinus Torvalds 					bus->b_hnp_enable = 0;
17781da177e4SLinus Torvalds 				}
17791da177e4SLinus Torvalds 			}
17801da177e4SLinus Torvalds 		}
17811da177e4SLinus Torvalds 	}
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	if (!is_targeted(udev)) {
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 		/* Maybe it can talk to us, though we can't talk to it.
17861da177e4SLinus Torvalds 		 * (Includes HNP test device.)
17871da177e4SLinus Torvalds 		 */
17881da177e4SLinus Torvalds 		if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
1789634a84f8SDavid Brownell 			err = usb_port_suspend(udev, PMSG_SUSPEND);
17901da177e4SLinus Torvalds 			if (err < 0)
17911da177e4SLinus Torvalds 				dev_dbg(&udev->dev, "HNP fail, %d\n", err);
17921da177e4SLinus Torvalds 		}
1793ffcdc18dSVikram Pandita 		err = -ENOTSUPP;
17941da177e4SLinus Torvalds 		goto fail;
17951da177e4SLinus Torvalds 	}
1796d9d16e8aSInaky Perez-Gonzalez fail:
17971da177e4SLinus Torvalds #endif
1798d9d16e8aSInaky Perez-Gonzalez 	return err;
1799d9d16e8aSInaky Perez-Gonzalez }
18001da177e4SLinus Torvalds 
1801d9d16e8aSInaky Perez-Gonzalez 
1802d9d16e8aSInaky Perez-Gonzalez /**
18038d8558d1SAlan Stern  * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
1804d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
1805d9d16e8aSInaky Perez-Gonzalez  *
1806d9d16e8aSInaky Perez-Gonzalez  * This is only called by usb_new_device() and usb_authorize_device()
1807d9d16e8aSInaky Perez-Gonzalez  * and FIXME -- all comments that apply to them apply here wrt to
1808d9d16e8aSInaky Perez-Gonzalez  * environment.
1809d9d16e8aSInaky Perez-Gonzalez  *
1810d9d16e8aSInaky Perez-Gonzalez  * If the device is WUSB and not authorized, we don't attempt to read
1811d9d16e8aSInaky Perez-Gonzalez  * the string descriptors, as they will be errored out by the device
1812d9d16e8aSInaky Perez-Gonzalez  * until it has been authorized.
1813d9d16e8aSInaky Perez-Gonzalez  */
18148d8558d1SAlan Stern static int usb_enumerate_device(struct usb_device *udev)
1815d9d16e8aSInaky Perez-Gonzalez {
1816d9d16e8aSInaky Perez-Gonzalez 	int err;
1817d9d16e8aSInaky Perez-Gonzalez 
1818d9d16e8aSInaky Perez-Gonzalez 	if (udev->config == NULL) {
1819d9d16e8aSInaky Perez-Gonzalez 		err = usb_get_configuration(udev);
1820d9d16e8aSInaky Perez-Gonzalez 		if (err < 0) {
1821d9d16e8aSInaky Perez-Gonzalez 			dev_err(&udev->dev, "can't read configurations, error %d\n",
1822d9d16e8aSInaky Perez-Gonzalez 				err);
1823d9d16e8aSInaky Perez-Gonzalez 			goto fail;
1824d9d16e8aSInaky Perez-Gonzalez 		}
1825d9d16e8aSInaky Perez-Gonzalez 	}
1826d9d16e8aSInaky Perez-Gonzalez 	if (udev->wusb == 1 && udev->authorized == 0) {
1827d9d16e8aSInaky Perez-Gonzalez 		udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1828d9d16e8aSInaky Perez-Gonzalez 		udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1829d9d16e8aSInaky Perez-Gonzalez 		udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1830d9d16e8aSInaky Perez-Gonzalez 	}
1831d9d16e8aSInaky Perez-Gonzalez 	else {
1832d9d16e8aSInaky Perez-Gonzalez 		/* read the standard strings and cache them if present */
1833d9d16e8aSInaky Perez-Gonzalez 		udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
1834d9d16e8aSInaky Perez-Gonzalez 		udev->manufacturer = usb_cache_string(udev,
1835d9d16e8aSInaky Perez-Gonzalez 						      udev->descriptor.iManufacturer);
1836d9d16e8aSInaky Perez-Gonzalez 		udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
1837d9d16e8aSInaky Perez-Gonzalez 	}
18388d8558d1SAlan Stern 	err = usb_enumerate_device_otg(udev);
1839d9d16e8aSInaky Perez-Gonzalez fail:
1840d9d16e8aSInaky Perez-Gonzalez 	return err;
1841d9d16e8aSInaky Perez-Gonzalez }
1842d9d16e8aSInaky Perez-Gonzalez 
1843d9d16e8aSInaky Perez-Gonzalez 
1844d9d16e8aSInaky Perez-Gonzalez /**
1845d9d16e8aSInaky Perez-Gonzalez  * usb_new_device - perform initial device setup (usbcore-internal)
1846d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
1847d9d16e8aSInaky Perez-Gonzalez  *
18488d8558d1SAlan Stern  * This is called with devices which have been detected but not fully
18498d8558d1SAlan Stern  * enumerated.  The device descriptor is available, but not descriptors
1850d9d16e8aSInaky Perez-Gonzalez  * for any device configuration.  The caller must have locked either
1851d9d16e8aSInaky Perez-Gonzalez  * the parent hub (if udev is a normal device) or else the
1852d9d16e8aSInaky Perez-Gonzalez  * usb_bus_list_lock (if udev is a root hub).  The parent's pointer to
1853d9d16e8aSInaky Perez-Gonzalez  * udev has already been installed, but udev is not yet visible through
1854d9d16e8aSInaky Perez-Gonzalez  * sysfs or other filesystem code.
1855d9d16e8aSInaky Perez-Gonzalez  *
1856d9d16e8aSInaky Perez-Gonzalez  * It will return if the device is configured properly or not.  Zero if
1857d9d16e8aSInaky Perez-Gonzalez  * the interface was registered with the driver core; else a negative
1858d9d16e8aSInaky Perez-Gonzalez  * errno value.
1859d9d16e8aSInaky Perez-Gonzalez  *
1860d9d16e8aSInaky Perez-Gonzalez  * This call is synchronous, and may not be used in an interrupt context.
1861d9d16e8aSInaky Perez-Gonzalez  *
1862d9d16e8aSInaky Perez-Gonzalez  * Only the hub driver or root-hub registrar should ever call this.
1863d9d16e8aSInaky Perez-Gonzalez  */
1864d9d16e8aSInaky Perez-Gonzalez int usb_new_device(struct usb_device *udev)
1865d9d16e8aSInaky Perez-Gonzalez {
1866d9d16e8aSInaky Perez-Gonzalez 	int err;
1867d9d16e8aSInaky Perez-Gonzalez 
186816985408SDan Streetman 	if (udev->parent) {
186916985408SDan Streetman 		/* Initialize non-root-hub device wakeup to disabled;
187016985408SDan Streetman 		 * device (un)configuration controls wakeup capable
187116985408SDan Streetman 		 * sysfs power/wakeup controls wakeup enabled/disabled
187216985408SDan Streetman 		 */
187316985408SDan Streetman 		device_init_wakeup(&udev->dev, 0);
187416985408SDan Streetman 	}
187516985408SDan Streetman 
18769bbdf1e0SAlan Stern 	/* Tell the runtime-PM framework the device is active */
18779bbdf1e0SAlan Stern 	pm_runtime_set_active(&udev->dev);
1878c08512c7SAlan Stern 	pm_runtime_get_noresume(&udev->dev);
1879fcc4a01eSAlan Stern 	pm_runtime_use_autosuspend(&udev->dev);
18809bbdf1e0SAlan Stern 	pm_runtime_enable(&udev->dev);
18819bbdf1e0SAlan Stern 
1882c08512c7SAlan Stern 	/* By default, forbid autosuspend for all devices.  It will be
1883c08512c7SAlan Stern 	 * allowed for hubs during binding.
1884c08512c7SAlan Stern 	 */
1885c08512c7SAlan Stern 	usb_disable_autosuspend(udev);
1886c08512c7SAlan Stern 
18878d8558d1SAlan Stern 	err = usb_enumerate_device(udev);	/* Read descriptors */
1888d9d16e8aSInaky Perez-Gonzalez 	if (err < 0)
1889d9d16e8aSInaky Perez-Gonzalez 		goto fail;
1890c6515272SSarah Sharp 	dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
1891c6515272SSarah Sharp 			udev->devnum, udev->bus->busnum,
1892c6515272SSarah Sharp 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
18939f8b17e6SKay Sievers 	/* export the usbdev device-node for libusb */
18949f8b17e6SKay Sievers 	udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
18959f8b17e6SKay Sievers 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
18969f8b17e6SKay Sievers 
18976cd13201SAlan Stern 	/* Tell the world! */
18986cd13201SAlan Stern 	announce_device(udev);
1899195af2ccSAlan Stern 
1900927bc916SRafael J. Wysocki 	device_enable_async_suspend(&udev->dev);
1901782da727SAlan Stern 	/* Register the device.  The device driver is responsible
19023b23dd6fSAlan Stern 	 * for configuring the device and invoking the add-device
19033b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
1904782da727SAlan Stern 	 */
19051da177e4SLinus Torvalds 	err = device_add(&udev->dev);
19061da177e4SLinus Torvalds 	if (err) {
19071da177e4SLinus Torvalds 		dev_err(&udev->dev, "can't device_add, error %d\n", err);
19081da177e4SLinus Torvalds 		goto fail;
19091da177e4SLinus Torvalds 	}
19109ad3d6ccSAlan Stern 
19113b23dd6fSAlan Stern 	(void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
1912c08512c7SAlan Stern 	usb_mark_last_busy(udev);
1913c08512c7SAlan Stern 	pm_runtime_put_sync_autosuspend(&udev->dev);
1914c066475eSGreg Kroah-Hartman 	return err;
19151da177e4SLinus Torvalds 
19161da177e4SLinus Torvalds fail:
19171da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
19189bbdf1e0SAlan Stern 	pm_runtime_disable(&udev->dev);
19199bbdf1e0SAlan Stern 	pm_runtime_set_suspended(&udev->dev);
1920d9d16e8aSInaky Perez-Gonzalez 	return err;
19211da177e4SLinus Torvalds }
19221da177e4SLinus Torvalds 
192393993a0aSInaky Perez-Gonzalez 
192493993a0aSInaky Perez-Gonzalez /**
1925fd39c86bSRandy Dunlap  * usb_deauthorize_device - deauthorize a device (usbcore-internal)
1926fd39c86bSRandy Dunlap  * @usb_dev: USB device
1927fd39c86bSRandy Dunlap  *
1928fd39c86bSRandy Dunlap  * Move the USB device to a very basic state where interfaces are disabled
1929fd39c86bSRandy Dunlap  * and the device is in fact unconfigured and unusable.
193093993a0aSInaky Perez-Gonzalez  *
193193993a0aSInaky Perez-Gonzalez  * We share a lock (that we have) with device_del(), so we need to
193293993a0aSInaky Perez-Gonzalez  * defer its call.
193393993a0aSInaky Perez-Gonzalez  */
193493993a0aSInaky Perez-Gonzalez int usb_deauthorize_device(struct usb_device *usb_dev)
193593993a0aSInaky Perez-Gonzalez {
193693993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
193793993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 0)
193893993a0aSInaky Perez-Gonzalez 		goto out_unauthorized;
1939da307123SAlan Stern 
194093993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 0;
194193993a0aSInaky Perez-Gonzalez 	usb_set_configuration(usb_dev, -1);
1942da307123SAlan Stern 
1943da307123SAlan Stern 	kfree(usb_dev->product);
194493993a0aSInaky Perez-Gonzalez 	usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1945da307123SAlan Stern 	kfree(usb_dev->manufacturer);
194693993a0aSInaky Perez-Gonzalez 	usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1947da307123SAlan Stern 	kfree(usb_dev->serial);
194893993a0aSInaky Perez-Gonzalez 	usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1949da307123SAlan Stern 
1950da307123SAlan Stern 	usb_destroy_configuration(usb_dev);
195193993a0aSInaky Perez-Gonzalez 	usb_dev->descriptor.bNumConfigurations = 0;
1952da307123SAlan Stern 
195393993a0aSInaky Perez-Gonzalez out_unauthorized:
195493993a0aSInaky Perez-Gonzalez 	usb_unlock_device(usb_dev);
195593993a0aSInaky Perez-Gonzalez 	return 0;
195693993a0aSInaky Perez-Gonzalez }
195793993a0aSInaky Perez-Gonzalez 
195893993a0aSInaky Perez-Gonzalez 
195993993a0aSInaky Perez-Gonzalez int usb_authorize_device(struct usb_device *usb_dev)
196093993a0aSInaky Perez-Gonzalez {
196193993a0aSInaky Perez-Gonzalez 	int result = 0, c;
1962da307123SAlan Stern 
196393993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
196493993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 1)
196593993a0aSInaky Perez-Gonzalez 		goto out_authorized;
1966da307123SAlan Stern 
196793993a0aSInaky Perez-Gonzalez 	result = usb_autoresume_device(usb_dev);
196893993a0aSInaky Perez-Gonzalez 	if (result < 0) {
196993993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev,
197093993a0aSInaky Perez-Gonzalez 			"can't autoresume for authorization: %d\n", result);
197193993a0aSInaky Perez-Gonzalez 		goto error_autoresume;
197293993a0aSInaky Perez-Gonzalez 	}
197393993a0aSInaky Perez-Gonzalez 	result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
197493993a0aSInaky Perez-Gonzalez 	if (result < 0) {
197593993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev, "can't re-read device descriptor for "
197693993a0aSInaky Perez-Gonzalez 			"authorization: %d\n", result);
197793993a0aSInaky Perez-Gonzalez 		goto error_device_descriptor;
197893993a0aSInaky Perez-Gonzalez 	}
1979da307123SAlan Stern 
1980da307123SAlan Stern 	kfree(usb_dev->product);
1981da307123SAlan Stern 	usb_dev->product = NULL;
1982da307123SAlan Stern 	kfree(usb_dev->manufacturer);
1983da307123SAlan Stern 	usb_dev->manufacturer = NULL;
1984da307123SAlan Stern 	kfree(usb_dev->serial);
1985da307123SAlan Stern 	usb_dev->serial = NULL;
1986da307123SAlan Stern 
198793993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 1;
19888d8558d1SAlan Stern 	result = usb_enumerate_device(usb_dev);
198993993a0aSInaky Perez-Gonzalez 	if (result < 0)
19908d8558d1SAlan Stern 		goto error_enumerate;
199193993a0aSInaky Perez-Gonzalez 	/* Choose and set the configuration.  This registers the interfaces
199293993a0aSInaky Perez-Gonzalez 	 * with the driver core and lets interface drivers bind to them.
199393993a0aSInaky Perez-Gonzalez 	 */
1994b5ea060fSGreg Kroah-Hartman 	c = usb_choose_configuration(usb_dev);
199593993a0aSInaky Perez-Gonzalez 	if (c >= 0) {
199693993a0aSInaky Perez-Gonzalez 		result = usb_set_configuration(usb_dev, c);
199793993a0aSInaky Perez-Gonzalez 		if (result) {
199893993a0aSInaky Perez-Gonzalez 			dev_err(&usb_dev->dev,
199993993a0aSInaky Perez-Gonzalez 				"can't set config #%d, error %d\n", c, result);
200093993a0aSInaky Perez-Gonzalez 			/* This need not be fatal.  The user can try to
200193993a0aSInaky Perez-Gonzalez 			 * set other configurations. */
200293993a0aSInaky Perez-Gonzalez 		}
200393993a0aSInaky Perez-Gonzalez 	}
200493993a0aSInaky Perez-Gonzalez 	dev_info(&usb_dev->dev, "authorized to connect\n");
2005da307123SAlan Stern 
20068d8558d1SAlan Stern error_enumerate:
200793993a0aSInaky Perez-Gonzalez error_device_descriptor:
2008da307123SAlan Stern 	usb_autosuspend_device(usb_dev);
200993993a0aSInaky Perez-Gonzalez error_autoresume:
201093993a0aSInaky Perez-Gonzalez out_authorized:
201193993a0aSInaky Perez-Gonzalez 	usb_unlock_device(usb_dev);	// complements locktree
201293993a0aSInaky Perez-Gonzalez 	return result;
201393993a0aSInaky Perez-Gonzalez }
201493993a0aSInaky Perez-Gonzalez 
201593993a0aSInaky Perez-Gonzalez 
20160165de09SInaky Perez-Gonzalez /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
20170165de09SInaky Perez-Gonzalez static unsigned hub_is_wusb(struct usb_hub *hub)
20180165de09SInaky Perez-Gonzalez {
20190165de09SInaky Perez-Gonzalez 	struct usb_hcd *hcd;
20200165de09SInaky Perez-Gonzalez 	if (hub->hdev->parent != NULL)  /* not a root hub? */
20210165de09SInaky Perez-Gonzalez 		return 0;
20220165de09SInaky Perez-Gonzalez 	hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
20230165de09SInaky Perez-Gonzalez 	return hcd->wireless;
20240165de09SInaky Perez-Gonzalez }
20250165de09SInaky Perez-Gonzalez 
20260165de09SInaky Perez-Gonzalez 
20271da177e4SLinus Torvalds #define PORT_RESET_TRIES	5
20281da177e4SLinus Torvalds #define SET_ADDRESS_TRIES	2
20291da177e4SLinus Torvalds #define GET_DESCRIPTOR_TRIES	2
20301da177e4SLinus Torvalds #define SET_CONFIG_TRIES	(2 * (use_both_schemes + 1))
20311da177e4SLinus Torvalds #define USE_NEW_SCHEME(i)	((i) / 2 == old_scheme_first)
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds #define HUB_ROOT_RESET_TIME	50	/* times are in msec */
20341da177e4SLinus Torvalds #define HUB_SHORT_RESET_TIME	10
20351da177e4SLinus Torvalds #define HUB_LONG_RESET_TIME	200
20361da177e4SLinus Torvalds #define HUB_RESET_TIMEOUT	500
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds static int hub_port_wait_reset(struct usb_hub *hub, int port1,
20391da177e4SLinus Torvalds 				struct usb_device *udev, unsigned int delay)
20401da177e4SLinus Torvalds {
20411da177e4SLinus Torvalds 	int delay_time, ret;
20421da177e4SLinus Torvalds 	u16 portstatus;
20431da177e4SLinus Torvalds 	u16 portchange;
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds 	for (delay_time = 0;
20461da177e4SLinus Torvalds 			delay_time < HUB_RESET_TIMEOUT;
20471da177e4SLinus Torvalds 			delay_time += delay) {
20481da177e4SLinus Torvalds 		/* wait to give the device a chance to reset */
20491da177e4SLinus Torvalds 		msleep(delay);
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds 		/* read and decode port status */
20521da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
20531da177e4SLinus Torvalds 		if (ret < 0)
20541da177e4SLinus Torvalds 			return ret;
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 		/* Device went away? */
20571da177e4SLinus Torvalds 		if (!(portstatus & USB_PORT_STAT_CONNECTION))
20581da177e4SLinus Torvalds 			return -ENOTCONN;
20591da177e4SLinus Torvalds 
2060dd4dd19eSAlan Stern 		/* bomb out completely if the connection bounced */
20611da177e4SLinus Torvalds 		if ((portchange & USB_PORT_STAT_C_CONNECTION))
2062dd4dd19eSAlan Stern 			return -ENOTCONN;
20631da177e4SLinus Torvalds 
20641da177e4SLinus Torvalds 		/* if we`ve finished resetting, then break out of the loop */
20651da177e4SLinus Torvalds 		if (!(portstatus & USB_PORT_STAT_RESET) &&
20661da177e4SLinus Torvalds 		    (portstatus & USB_PORT_STAT_ENABLE)) {
20670165de09SInaky Perez-Gonzalez 			if (hub_is_wusb(hub))
2068551cdbbeSGreg Kroah-Hartman 				udev->speed = USB_SPEED_WIRELESS;
2069131dec34SSarah Sharp 			else if (hub_is_superspeed(hub->hdev))
2070809cd1cbSSarah Sharp 				udev->speed = USB_SPEED_SUPER;
20710165de09SInaky Perez-Gonzalez 			else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
20721da177e4SLinus Torvalds 				udev->speed = USB_SPEED_HIGH;
20731da177e4SLinus Torvalds 			else if (portstatus & USB_PORT_STAT_LOW_SPEED)
20741da177e4SLinus Torvalds 				udev->speed = USB_SPEED_LOW;
20751da177e4SLinus Torvalds 			else
20761da177e4SLinus Torvalds 				udev->speed = USB_SPEED_FULL;
20771da177e4SLinus Torvalds 			return 0;
20781da177e4SLinus Torvalds 		}
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 		/* switch to the long delay after two short delay failures */
20811da177e4SLinus Torvalds 		if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
20821da177e4SLinus Torvalds 			delay = HUB_LONG_RESET_TIME;
20831da177e4SLinus Torvalds 
20841da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
20851da177e4SLinus Torvalds 			"port %d not reset yet, waiting %dms\n",
20861da177e4SLinus Torvalds 			port1, delay);
20871da177e4SLinus Torvalds 	}
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 	return -EBUSY;
20901da177e4SLinus Torvalds }
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds static int hub_port_reset(struct usb_hub *hub, int port1,
20931da177e4SLinus Torvalds 				struct usb_device *udev, unsigned int delay)
20941da177e4SLinus Torvalds {
20951da177e4SLinus Torvalds 	int i, status;
2096a5f0efabSSarah Sharp 	struct usb_hcd *hcd;
20971da177e4SLinus Torvalds 
2098a5f0efabSSarah Sharp 	hcd = bus_to_hcd(udev->bus);
209932fe0198SAlan Stern 	/* Block EHCI CF initialization during the port reset.
210032fe0198SAlan Stern 	 * Some companion controllers don't like it when they mix.
210132fe0198SAlan Stern 	 */
210232fe0198SAlan Stern 	down_read(&ehci_cf_port_reset_rwsem);
210332fe0198SAlan Stern 
21041da177e4SLinus Torvalds 	/* Reset the port */
21051da177e4SLinus Torvalds 	for (i = 0; i < PORT_RESET_TRIES; i++) {
21061da177e4SLinus Torvalds 		status = set_port_feature(hub->hdev,
21071da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_RESET);
21081da177e4SLinus Torvalds 		if (status)
21091da177e4SLinus Torvalds 			dev_err(hub->intfdev,
21101da177e4SLinus Torvalds 					"cannot reset port %d (err = %d)\n",
21111da177e4SLinus Torvalds 					port1, status);
21121da177e4SLinus Torvalds 		else {
21131da177e4SLinus Torvalds 			status = hub_port_wait_reset(hub, port1, udev, delay);
2114dd16525bSDavid Brownell 			if (status && status != -ENOTCONN)
21151da177e4SLinus Torvalds 				dev_dbg(hub->intfdev,
21161da177e4SLinus Torvalds 						"port_wait_reset: err = %d\n",
21171da177e4SLinus Torvalds 						status);
21181da177e4SLinus Torvalds 		}
21191da177e4SLinus Torvalds 
21201da177e4SLinus Torvalds 		/* return on disconnect or reset */
21211da177e4SLinus Torvalds 		switch (status) {
21221da177e4SLinus Torvalds 		case 0:
2123b789696aSDavid Brownell 			/* TRSTRCY = 10 ms; plus some extra */
2124b789696aSDavid Brownell 			msleep(10 + 40);
21253b29b68bSAlan Stern 			update_devnum(udev, 0);
2126a5f0efabSSarah Sharp 			if (hcd->driver->reset_device) {
2127a5f0efabSSarah Sharp 				status = hcd->driver->reset_device(hcd, udev);
2128a5f0efabSSarah Sharp 				if (status < 0) {
2129a5f0efabSSarah Sharp 					dev_err(&udev->dev, "Cannot reset "
2130a5f0efabSSarah Sharp 							"HCD device state\n");
2131a5f0efabSSarah Sharp 					break;
2132a5f0efabSSarah Sharp 				}
2133a5f0efabSSarah Sharp 			}
21341da177e4SLinus Torvalds 			/* FALL THROUGH */
21351da177e4SLinus Torvalds 		case -ENOTCONN:
21361da177e4SLinus Torvalds 		case -ENODEV:
21371da177e4SLinus Torvalds 			clear_port_feature(hub->hdev,
21381da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_C_RESET);
21391da177e4SLinus Torvalds 			/* FIXME need disconnect() for NOTATTACHED device */
21401da177e4SLinus Torvalds 			usb_set_device_state(udev, status
21411da177e4SLinus Torvalds 					? USB_STATE_NOTATTACHED
21421da177e4SLinus Torvalds 					: USB_STATE_DEFAULT);
214332fe0198SAlan Stern 			goto done;
21441da177e4SLinus Torvalds 		}
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
21471da177e4SLinus Torvalds 			"port %d not enabled, trying reset again...\n",
21481da177e4SLinus Torvalds 			port1);
21491da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
21501da177e4SLinus Torvalds 	}
21511da177e4SLinus Torvalds 
21521da177e4SLinus Torvalds 	dev_err (hub->intfdev,
21531da177e4SLinus Torvalds 		"Cannot enable port %i.  Maybe the USB cable is bad?\n",
21541da177e4SLinus Torvalds 		port1);
21551da177e4SLinus Torvalds 
215632fe0198SAlan Stern  done:
215732fe0198SAlan Stern 	up_read(&ehci_cf_port_reset_rwsem);
21581da177e4SLinus Torvalds 	return status;
21591da177e4SLinus Torvalds }
21601da177e4SLinus Torvalds 
2161d388dab7SAlan Stern #ifdef	CONFIG_PM
21621da177e4SLinus Torvalds 
2163b01b03f3SAlan Stern #define MASK_BITS	(USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION | \
2164b01b03f3SAlan Stern 				USB_PORT_STAT_SUSPEND)
2165b01b03f3SAlan Stern #define WANT_BITS	(USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION)
2166b01b03f3SAlan Stern 
2167b01b03f3SAlan Stern /* Determine whether the device on a port is ready for a normal resume,
2168b01b03f3SAlan Stern  * is ready for a reset-resume, or should be disconnected.
2169b01b03f3SAlan Stern  */
2170b01b03f3SAlan Stern static int check_port_resume_type(struct usb_device *udev,
2171b01b03f3SAlan Stern 		struct usb_hub *hub, int port1,
2172b01b03f3SAlan Stern 		int status, unsigned portchange, unsigned portstatus)
2173b01b03f3SAlan Stern {
2174b01b03f3SAlan Stern 	/* Is the device still present? */
2175b01b03f3SAlan Stern 	if (status || (portstatus & MASK_BITS) != WANT_BITS) {
2176b01b03f3SAlan Stern 		if (status >= 0)
2177b01b03f3SAlan Stern 			status = -ENODEV;
2178b01b03f3SAlan Stern 	}
2179b01b03f3SAlan Stern 
218086c57edfSAlan Stern 	/* Can't do a normal resume if the port isn't enabled,
218186c57edfSAlan Stern 	 * so try a reset-resume instead.
218286c57edfSAlan Stern 	 */
218386c57edfSAlan Stern 	else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
218486c57edfSAlan Stern 		if (udev->persist_enabled)
218586c57edfSAlan Stern 			udev->reset_resume = 1;
218686c57edfSAlan Stern 		else
2187b01b03f3SAlan Stern 			status = -ENODEV;
218886c57edfSAlan Stern 	}
2189b01b03f3SAlan Stern 
2190b01b03f3SAlan Stern 	if (status) {
2191b01b03f3SAlan Stern 		dev_dbg(hub->intfdev,
2192b01b03f3SAlan Stern 				"port %d status %04x.%04x after resume, %d\n",
2193b01b03f3SAlan Stern 				port1, portchange, portstatus, status);
2194b01b03f3SAlan Stern 	} else if (udev->reset_resume) {
2195b01b03f3SAlan Stern 
2196b01b03f3SAlan Stern 		/* Late port handoff can set status-change bits */
2197b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION)
2198b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2199b01b03f3SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
2200b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE)
2201b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2202b01b03f3SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
2203b01b03f3SAlan Stern 	}
2204b01b03f3SAlan Stern 
2205b01b03f3SAlan Stern 	return status;
2206b01b03f3SAlan Stern }
2207b01b03f3SAlan Stern 
22081da177e4SLinus Torvalds #ifdef	CONFIG_USB_SUSPEND
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds /*
2211140d8f68SAlan Stern  * usb_port_suspend - suspend a usb device's upstream port
2212624d6c07SAlan Stern  * @udev: device that's no longer in active use, not a root hub
22135edbfb7cSDavid Brownell  * Context: must be able to sleep; device not locked; pm locks held
22141da177e4SLinus Torvalds  *
22151da177e4SLinus Torvalds  * Suspends a USB device that isn't in active use, conserving power.
22161da177e4SLinus Torvalds  * Devices may wake out of a suspend, if anything important happens,
22171da177e4SLinus Torvalds  * using the remote wakeup mechanism.  They may also be taken out of
2218140d8f68SAlan Stern  * suspend by the host, using usb_port_resume().  It's also routine
22191da177e4SLinus Torvalds  * to disconnect devices while they are suspended.
22201da177e4SLinus Torvalds  *
2221390a8c34SDavid Brownell  * This only affects the USB hardware for a device; its interfaces
2222390a8c34SDavid Brownell  * (and, for hubs, child devices) must already have been suspended.
2223390a8c34SDavid Brownell  *
2224624d6c07SAlan Stern  * Selective port suspend reduces power; most suspended devices draw
2225624d6c07SAlan Stern  * less than 500 uA.  It's also used in OTG, along with remote wakeup.
2226624d6c07SAlan Stern  * All devices below the suspended port are also suspended.
2227624d6c07SAlan Stern  *
2228624d6c07SAlan Stern  * Devices leave suspend state when the host wakes them up.  Some devices
2229624d6c07SAlan Stern  * also support "remote wakeup", where the device can activate the USB
2230624d6c07SAlan Stern  * tree above them to deliver data, such as a keypress or packet.  In
2231624d6c07SAlan Stern  * some cases, this wakes the USB host.
2232624d6c07SAlan Stern  *
22331da177e4SLinus Torvalds  * Suspending OTG devices may trigger HNP, if that's been enabled
22341da177e4SLinus Torvalds  * between a pair of dual-role devices.  That will change roles, such
22351da177e4SLinus Torvalds  * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
22361da177e4SLinus Torvalds  *
22374956eccdSAlan Stern  * Devices on USB hub ports have only one "suspend" state, corresponding
22384956eccdSAlan Stern  * to ACPI D2, "may cause the device to lose some context".
22394956eccdSAlan Stern  * State transitions include:
22404956eccdSAlan Stern  *
22414956eccdSAlan Stern  *   - suspend, resume ... when the VBUS power link stays live
22424956eccdSAlan Stern  *   - suspend, disconnect ... VBUS lost
22434956eccdSAlan Stern  *
22444956eccdSAlan Stern  * Once VBUS drop breaks the circuit, the port it's using has to go through
22454956eccdSAlan Stern  * normal re-enumeration procedures, starting with enabling VBUS power.
22464956eccdSAlan Stern  * Other than re-initializing the hub (plug/unplug, except for root hubs),
22474956eccdSAlan Stern  * Linux (2.6) currently has NO mechanisms to initiate that:  no khubd
22484956eccdSAlan Stern  * timer, no SRP, no requests through sysfs.
22494956eccdSAlan Stern  *
22504956eccdSAlan Stern  * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
22514956eccdSAlan Stern  * the root hub for their bus goes into global suspend ... so we don't
22524956eccdSAlan Stern  * (falsely) update the device power state to say it suspended.
22534956eccdSAlan Stern  *
22541da177e4SLinus Torvalds  * Returns 0 on success, else negative errno.
22551da177e4SLinus Torvalds  */
225665bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
22571da177e4SLinus Torvalds {
2258624d6c07SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2259624d6c07SAlan Stern 	int		port1 = udev->portnum;
2260624d6c07SAlan Stern 	int		status;
22614956eccdSAlan Stern 
2262624d6c07SAlan Stern 	// dev_dbg(hub->intfdev, "suspend port %d\n", port1);
2263624d6c07SAlan Stern 
2264624d6c07SAlan Stern 	/* enable remote wakeup when appropriate; this lets the device
2265624d6c07SAlan Stern 	 * wake up the upstream hub (including maybe the root hub).
2266624d6c07SAlan Stern 	 *
2267624d6c07SAlan Stern 	 * NOTE:  OTG devices may issue remote wakeup (or SRP) even when
2268624d6c07SAlan Stern 	 * we don't explicitly enable it here.
2269624d6c07SAlan Stern 	 */
2270624d6c07SAlan Stern 	if (udev->do_remote_wakeup) {
2271624d6c07SAlan Stern 		status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2272624d6c07SAlan Stern 				USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2273624d6c07SAlan Stern 				USB_DEVICE_REMOTE_WAKEUP, 0,
2274624d6c07SAlan Stern 				NULL, 0,
2275624d6c07SAlan Stern 				USB_CTRL_SET_TIMEOUT);
22760c487206SOliver Neukum 		if (status) {
2277624d6c07SAlan Stern 			dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2278624d6c07SAlan Stern 					status);
22790c487206SOliver Neukum 			/* bail if autosuspend is requested */
22800c487206SOliver Neukum 			if (msg.event & PM_EVENT_AUTO)
22810c487206SOliver Neukum 				return status;
22820c487206SOliver Neukum 		}
2283624d6c07SAlan Stern 	}
2284624d6c07SAlan Stern 
2285624d6c07SAlan Stern 	/* see 7.1.7.6 */
2286624d6c07SAlan Stern 	status = set_port_feature(hub->hdev, port1, USB_PORT_FEAT_SUSPEND);
2287624d6c07SAlan Stern 	if (status) {
2288624d6c07SAlan Stern 		dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2289624d6c07SAlan Stern 				port1, status);
2290624d6c07SAlan Stern 		/* paranoia:  "should not happen" */
22910c487206SOliver Neukum 		if (udev->do_remote_wakeup)
2292624d6c07SAlan Stern 			(void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2293624d6c07SAlan Stern 				USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2294624d6c07SAlan Stern 				USB_DEVICE_REMOTE_WAKEUP, 0,
2295624d6c07SAlan Stern 				NULL, 0,
2296624d6c07SAlan Stern 				USB_CTRL_SET_TIMEOUT);
2297624d6c07SAlan Stern 	} else {
2298624d6c07SAlan Stern 		/* device has up to 10 msec to fully suspend */
2299624d6c07SAlan Stern 		dev_dbg(&udev->dev, "usb %ssuspend\n",
230065bfd296SAlan Stern 				(msg.event & PM_EVENT_AUTO ? "auto-" : ""));
2301624d6c07SAlan Stern 		usb_set_device_state(udev, USB_STATE_SUSPENDED);
2302624d6c07SAlan Stern 		msleep(10);
2303624d6c07SAlan Stern 	}
2304c08512c7SAlan Stern 	usb_mark_last_busy(hub->hdev);
23054956eccdSAlan Stern 	return status;
23061da177e4SLinus Torvalds }
2307f3f3253dSDavid Brownell 
23081da177e4SLinus Torvalds /*
2309390a8c34SDavid Brownell  * If the USB "suspend" state is in use (rather than "global suspend"),
2310390a8c34SDavid Brownell  * many devices will be individually taken out of suspend state using
231154515fe5SAlan Stern  * special "resume" signaling.  This routine kicks in shortly after
23121da177e4SLinus Torvalds  * hardware resume signaling is finished, either because of selective
23131da177e4SLinus Torvalds  * resume (by host) or remote wakeup (by device) ... now see what changed
23141da177e4SLinus Torvalds  * in the tree that's rooted at this device.
231554515fe5SAlan Stern  *
231654515fe5SAlan Stern  * If @udev->reset_resume is set then the device is reset before the
231754515fe5SAlan Stern  * status check is done.
23181da177e4SLinus Torvalds  */
2319140d8f68SAlan Stern static int finish_port_resume(struct usb_device *udev)
23201da177e4SLinus Torvalds {
232154515fe5SAlan Stern 	int	status = 0;
23221da177e4SLinus Torvalds 	u16	devstatus;
23231da177e4SLinus Torvalds 
23241da177e4SLinus Torvalds 	/* caller owns the udev device lock */
2325b9cef6c3SWu Fengguang 	dev_dbg(&udev->dev, "%s\n",
2326b9cef6c3SWu Fengguang 		udev->reset_resume ? "finish reset-resume" : "finish resume");
23271da177e4SLinus Torvalds 
23281da177e4SLinus Torvalds 	/* usb ch9 identifies four variants of SUSPENDED, based on what
23291da177e4SLinus Torvalds 	 * state the device resumes to.  Linux currently won't see the
23301da177e4SLinus Torvalds 	 * first two on the host side; they'd be inside hub_port_init()
23311da177e4SLinus Torvalds 	 * during many timeouts, but khubd can't suspend until later.
23321da177e4SLinus Torvalds 	 */
23331da177e4SLinus Torvalds 	usb_set_device_state(udev, udev->actconfig
23341da177e4SLinus Torvalds 			? USB_STATE_CONFIGURED
23351da177e4SLinus Torvalds 			: USB_STATE_ADDRESS);
23361da177e4SLinus Torvalds 
233754515fe5SAlan Stern 	/* 10.5.4.5 says not to reset a suspended port if the attached
233854515fe5SAlan Stern 	 * device is enabled for remote wakeup.  Hence the reset
233954515fe5SAlan Stern 	 * operation is carried out here, after the port has been
234054515fe5SAlan Stern 	 * resumed.
234154515fe5SAlan Stern 	 */
234254515fe5SAlan Stern 	if (udev->reset_resume)
234386c57edfSAlan Stern  retry_reset_resume:
2344742120c6SMing Lei 		status = usb_reset_and_verify_device(udev);
234554515fe5SAlan Stern 
23461da177e4SLinus Torvalds  	/* 10.5.4.5 says be sure devices in the tree are still there.
23471da177e4SLinus Torvalds  	 * For now let's assume the device didn't go crazy on resume,
23481da177e4SLinus Torvalds 	 * and device drivers will know about any resume quirks.
23491da177e4SLinus Torvalds 	 */
235054515fe5SAlan Stern 	if (status == 0) {
235146dede46SAlan Stern 		devstatus = 0;
23521da177e4SLinus Torvalds 		status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2353b40b7a90SAlan Stern 		if (status >= 0)
235446dede46SAlan Stern 			status = (status > 0 ? 0 : -ENODEV);
235586c57edfSAlan Stern 
235686c57edfSAlan Stern 		/* If a normal resume failed, try doing a reset-resume */
235786c57edfSAlan Stern 		if (status && !udev->reset_resume && udev->persist_enabled) {
235886c57edfSAlan Stern 			dev_dbg(&udev->dev, "retry with reset-resume\n");
235986c57edfSAlan Stern 			udev->reset_resume = 1;
236086c57edfSAlan Stern 			goto retry_reset_resume;
236186c57edfSAlan Stern 		}
236254515fe5SAlan Stern 	}
2363b40b7a90SAlan Stern 
2364624d6c07SAlan Stern 	if (status) {
2365624d6c07SAlan Stern 		dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
23661da177e4SLinus Torvalds 				status);
2367624d6c07SAlan Stern 	} else if (udev->actconfig) {
23681da177e4SLinus Torvalds 		le16_to_cpus(&devstatus);
2369686314cfSAlan Stern 		if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
23701da177e4SLinus Torvalds 			status = usb_control_msg(udev,
23711da177e4SLinus Torvalds 					usb_sndctrlpipe(udev, 0),
23721da177e4SLinus Torvalds 					USB_REQ_CLEAR_FEATURE,
23731da177e4SLinus Torvalds 						USB_RECIP_DEVICE,
23741da177e4SLinus Torvalds 					USB_DEVICE_REMOTE_WAKEUP, 0,
23751da177e4SLinus Torvalds 					NULL, 0,
23761da177e4SLinus Torvalds 					USB_CTRL_SET_TIMEOUT);
2377a8e7c565SAlan Stern 			if (status)
2378b9cef6c3SWu Fengguang 				dev_dbg(&udev->dev,
2379b9cef6c3SWu Fengguang 					"disable remote wakeup, status %d\n",
2380b9cef6c3SWu Fengguang 					status);
23814bf0ba86SAlan Stern 		}
23821da177e4SLinus Torvalds 		status = 0;
23831da177e4SLinus Torvalds 	}
23841da177e4SLinus Torvalds 	return status;
23851da177e4SLinus Torvalds }
23861da177e4SLinus Torvalds 
2387624d6c07SAlan Stern /*
2388624d6c07SAlan Stern  * usb_port_resume - re-activate a suspended usb device's upstream port
2389624d6c07SAlan Stern  * @udev: device to re-activate, not a root hub
2390624d6c07SAlan Stern  * Context: must be able to sleep; device not locked; pm locks held
2391624d6c07SAlan Stern  *
2392624d6c07SAlan Stern  * This will re-activate the suspended device, increasing power usage
2393624d6c07SAlan Stern  * while letting drivers communicate again with its endpoints.
2394624d6c07SAlan Stern  * USB resume explicitly guarantees that the power session between
2395624d6c07SAlan Stern  * the host and the device is the same as it was when the device
2396624d6c07SAlan Stern  * suspended.
2397624d6c07SAlan Stern  *
2398feccc30dSAlan Stern  * If @udev->reset_resume is set then this routine won't check that the
2399feccc30dSAlan Stern  * port is still enabled.  Furthermore, finish_port_resume() above will
240054515fe5SAlan Stern  * reset @udev.  The end result is that a broken power session can be
240154515fe5SAlan Stern  * recovered and @udev will appear to persist across a loss of VBUS power.
240254515fe5SAlan Stern  *
240354515fe5SAlan Stern  * For example, if a host controller doesn't maintain VBUS suspend current
240454515fe5SAlan Stern  * during a system sleep or is reset when the system wakes up, all the USB
240554515fe5SAlan Stern  * power sessions below it will be broken.  This is especially troublesome
240654515fe5SAlan Stern  * for mass-storage devices containing mounted filesystems, since the
240754515fe5SAlan Stern  * device will appear to have disconnected and all the memory mappings
240854515fe5SAlan Stern  * to it will be lost.  Using the USB_PERSIST facility, the device can be
240954515fe5SAlan Stern  * made to appear as if it had not disconnected.
241054515fe5SAlan Stern  *
2411742120c6SMing Lei  * This facility can be dangerous.  Although usb_reset_and_verify_device() makes
2412feccc30dSAlan Stern  * every effort to insure that the same device is present after the
241354515fe5SAlan Stern  * reset as before, it cannot provide a 100% guarantee.  Furthermore it's
241454515fe5SAlan Stern  * quite possible for a device to remain unaltered but its media to be
241554515fe5SAlan Stern  * changed.  If the user replaces a flash memory card while the system is
241654515fe5SAlan Stern  * asleep, he will have only himself to blame when the filesystem on the
241754515fe5SAlan Stern  * new card is corrupted and the system crashes.
241854515fe5SAlan Stern  *
2419624d6c07SAlan Stern  * Returns 0 on success, else negative errno.
2420624d6c07SAlan Stern  */
242165bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
24221da177e4SLinus Torvalds {
2423624d6c07SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2424624d6c07SAlan Stern 	int		port1 = udev->portnum;
24251da177e4SLinus Torvalds 	int		status;
2426d25450c6SAlan Stern 	u16		portchange, portstatus;
2427d25450c6SAlan Stern 
2428d25450c6SAlan Stern 	/* Skip the initial Clear-Suspend step for a remote wakeup */
2429d25450c6SAlan Stern 	status = hub_port_status(hub, port1, &portstatus, &portchange);
2430d25450c6SAlan Stern 	if (status == 0 && !(portstatus & USB_PORT_STAT_SUSPEND))
2431d25450c6SAlan Stern 		goto SuspendCleared;
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds 	// dev_dbg(hub->intfdev, "resume port %d\n", port1);
24341da177e4SLinus Torvalds 
2435d5cbad4bSAlan Stern 	set_bit(port1, hub->busy_bits);
2436d5cbad4bSAlan Stern 
24371da177e4SLinus Torvalds 	/* see 7.1.7.7; affects power usage, but not budgeting */
24381da177e4SLinus Torvalds 	status = clear_port_feature(hub->hdev,
24391da177e4SLinus Torvalds 			port1, USB_PORT_FEAT_SUSPEND);
24401da177e4SLinus Torvalds 	if (status) {
2441624d6c07SAlan Stern 		dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
24421da177e4SLinus Torvalds 				port1, status);
24431da177e4SLinus Torvalds 	} else {
24441da177e4SLinus Torvalds 		/* drive resume for at least 20 msec */
2445645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n",
244665bfd296SAlan Stern 				(msg.event & PM_EVENT_AUTO ? "auto-" : ""));
24471da177e4SLinus Torvalds 		msleep(25);
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds 		/* Virtual root hubs can trigger on GET_PORT_STATUS to
24501da177e4SLinus Torvalds 		 * stop resume signaling.  Then finish the resume
24511da177e4SLinus Torvalds 		 * sequence.
24521da177e4SLinus Torvalds 		 */
2453d25450c6SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
245454515fe5SAlan Stern 
24551da177e4SLinus Torvalds 		/* TRSMRCY = 10 msec */
24561da177e4SLinus Torvalds 		msleep(10);
24571da177e4SLinus Torvalds 	}
2458b01b03f3SAlan Stern 
2459b01b03f3SAlan Stern  SuspendCleared:
2460b01b03f3SAlan Stern 	if (status == 0) {
2461b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_SUSPEND)
2462b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2463b01b03f3SAlan Stern 					USB_PORT_FEAT_C_SUSPEND);
24641da177e4SLinus Torvalds 	}
24651da177e4SLinus Torvalds 
2466d5cbad4bSAlan Stern 	clear_bit(port1, hub->busy_bits);
2467d5cbad4bSAlan Stern 
2468b01b03f3SAlan Stern 	status = check_port_resume_type(udev,
2469b01b03f3SAlan Stern 			hub, port1, status, portchange, portstatus);
247054515fe5SAlan Stern 	if (status == 0)
247154515fe5SAlan Stern 		status = finish_port_resume(udev);
247254515fe5SAlan Stern 	if (status < 0) {
247354515fe5SAlan Stern 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
247454515fe5SAlan Stern 		hub_port_logical_disconnect(hub, port1);
247554515fe5SAlan Stern 	}
24761da177e4SLinus Torvalds 	return status;
24771da177e4SLinus Torvalds }
24781da177e4SLinus Torvalds 
24798808f00cSAlan Stern /* caller has locked udev */
24800534d468SAlan Stern int usb_remote_wakeup(struct usb_device *udev)
24811da177e4SLinus Torvalds {
24821da177e4SLinus Torvalds 	int	status = 0;
24831da177e4SLinus Torvalds 
24841da177e4SLinus Torvalds 	if (udev->state == USB_STATE_SUSPENDED) {
2485645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
24869bbdf1e0SAlan Stern 		status = usb_autoresume_device(udev);
24879bbdf1e0SAlan Stern 		if (status == 0) {
24889bbdf1e0SAlan Stern 			/* Let the drivers do their thing, then... */
24899bbdf1e0SAlan Stern 			usb_autosuspend_device(udev);
24909bbdf1e0SAlan Stern 		}
2491d25450c6SAlan Stern 	}
24921da177e4SLinus Torvalds 	return status;
24931da177e4SLinus Torvalds }
24941da177e4SLinus Torvalds 
2495d388dab7SAlan Stern #else	/* CONFIG_USB_SUSPEND */
2496d388dab7SAlan Stern 
2497d388dab7SAlan Stern /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
2498d388dab7SAlan Stern 
249965bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
2500d388dab7SAlan Stern {
2501d388dab7SAlan Stern 	return 0;
2502d388dab7SAlan Stern }
2503d388dab7SAlan Stern 
2504b01b03f3SAlan Stern /* However we may need to do a reset-resume */
2505b01b03f3SAlan Stern 
250665bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
2507d388dab7SAlan Stern {
2508b01b03f3SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2509b01b03f3SAlan Stern 	int		port1 = udev->portnum;
2510b01b03f3SAlan Stern 	int		status;
2511b01b03f3SAlan Stern 	u16		portchange, portstatus;
251254515fe5SAlan Stern 
2513b01b03f3SAlan Stern 	status = hub_port_status(hub, port1, &portstatus, &portchange);
2514b01b03f3SAlan Stern 	status = check_port_resume_type(udev,
2515b01b03f3SAlan Stern 			hub, port1, status, portchange, portstatus);
2516b01b03f3SAlan Stern 
2517b01b03f3SAlan Stern 	if (status) {
2518b01b03f3SAlan Stern 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2519b01b03f3SAlan Stern 		hub_port_logical_disconnect(hub, port1);
2520b01b03f3SAlan Stern 	} else if (udev->reset_resume) {
252154515fe5SAlan Stern 		dev_dbg(&udev->dev, "reset-resume\n");
2522742120c6SMing Lei 		status = usb_reset_and_verify_device(udev);
252354515fe5SAlan Stern 	}
252454515fe5SAlan Stern 	return status;
2525d388dab7SAlan Stern }
2526d388dab7SAlan Stern 
2527d388dab7SAlan Stern #endif
2528d388dab7SAlan Stern 
2529db690874SDavid Brownell static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
25301da177e4SLinus Torvalds {
25311da177e4SLinus Torvalds 	struct usb_hub		*hub = usb_get_intfdata (intf);
25321da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
25331da177e4SLinus Torvalds 	unsigned		port1;
25341da177e4SLinus Torvalds 
2535c9f89fa4SDavid Brownell 	/* fail if children aren't already suspended */
25361da177e4SLinus Torvalds 	for (port1 = 1; port1 <= hdev->maxchild; port1++) {
25371da177e4SLinus Torvalds 		struct usb_device	*udev;
25381da177e4SLinus Torvalds 
25391da177e4SLinus Torvalds 		udev = hdev->children [port1-1];
25406840d255SAlan Stern 		if (udev && udev->can_submit) {
254165bfd296SAlan Stern 			if (!(msg.event & PM_EVENT_AUTO))
2542645daaabSAlan Stern 				dev_dbg(&intf->dev, "port %d nyet suspended\n",
2543645daaabSAlan Stern 						port1);
2544c9f89fa4SDavid Brownell 			return -EBUSY;
2545c9f89fa4SDavid Brownell 		}
25461da177e4SLinus Torvalds 	}
25471da177e4SLinus Torvalds 
2548441b62c1SHarvey Harrison 	dev_dbg(&intf->dev, "%s\n", __func__);
254940f122f3SAlan Stern 
2550c9f89fa4SDavid Brownell 	/* stop khubd and related activity */
25514330354fSAlan Stern 	hub_quiesce(hub, HUB_SUSPEND);
2552b6f6436dSAlan Stern 	return 0;
25531da177e4SLinus Torvalds }
25541da177e4SLinus Torvalds 
25551da177e4SLinus Torvalds static int hub_resume(struct usb_interface *intf)
25561da177e4SLinus Torvalds {
25571da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata(intf);
25581da177e4SLinus Torvalds 
25595e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
2560f2835219SAlan Stern 	hub_activate(hub, HUB_RESUME);
25611da177e4SLinus Torvalds 	return 0;
25621da177e4SLinus Torvalds }
25631da177e4SLinus Torvalds 
2564b41a60ecSAlan Stern static int hub_reset_resume(struct usb_interface *intf)
2565f07600cfSAlan Stern {
2566b41a60ecSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
2567f07600cfSAlan Stern 
25685e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
2569f2835219SAlan Stern 	hub_activate(hub, HUB_RESET_RESUME);
2570f07600cfSAlan Stern 	return 0;
2571f07600cfSAlan Stern }
2572f07600cfSAlan Stern 
257354515fe5SAlan Stern /**
257454515fe5SAlan Stern  * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
257554515fe5SAlan Stern  * @rhdev: struct usb_device for the root hub
257654515fe5SAlan Stern  *
257754515fe5SAlan Stern  * The USB host controller driver calls this function when its root hub
257854515fe5SAlan Stern  * is resumed and Vbus power has been interrupted or the controller
2579feccc30dSAlan Stern  * has been reset.  The routine marks @rhdev as having lost power.
2580feccc30dSAlan Stern  * When the hub driver is resumed it will take notice and carry out
2581feccc30dSAlan Stern  * power-session recovery for all the "USB-PERSIST"-enabled child devices;
2582feccc30dSAlan Stern  * the others will be disconnected.
258354515fe5SAlan Stern  */
258454515fe5SAlan Stern void usb_root_hub_lost_power(struct usb_device *rhdev)
258554515fe5SAlan Stern {
258654515fe5SAlan Stern 	dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
258754515fe5SAlan Stern 	rhdev->reset_resume = 1;
258854515fe5SAlan Stern }
258954515fe5SAlan Stern EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
259054515fe5SAlan Stern 
2591d388dab7SAlan Stern #else	/* CONFIG_PM */
2592d388dab7SAlan Stern 
2593511366daSAndrew Morton #define hub_suspend		NULL
2594511366daSAndrew Morton #define hub_resume		NULL
2595f07600cfSAlan Stern #define hub_reset_resume	NULL
2596d388dab7SAlan Stern #endif
2597d388dab7SAlan Stern 
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
26001da177e4SLinus Torvalds  *
26011da177e4SLinus Torvalds  * Between connect detection and reset signaling there must be a delay
26021da177e4SLinus Torvalds  * of 100ms at least for debounce and power-settling.  The corresponding
26031da177e4SLinus Torvalds  * timer shall restart whenever the downstream port detects a disconnect.
26041da177e4SLinus Torvalds  *
26051da177e4SLinus Torvalds  * Apparently there are some bluetooth and irda-dongles and a number of
26061da177e4SLinus Torvalds  * low-speed devices for which this debounce period may last over a second.
26071da177e4SLinus Torvalds  * Not covered by the spec - but easy to deal with.
26081da177e4SLinus Torvalds  *
26091da177e4SLinus Torvalds  * This implementation uses a 1500ms total debounce timeout; if the
26101da177e4SLinus Torvalds  * connection isn't stable by then it returns -ETIMEDOUT.  It checks
26111da177e4SLinus Torvalds  * every 25ms for transient disconnects.  When the port status has been
26121da177e4SLinus Torvalds  * unchanged for 100ms it returns the port status.
26131da177e4SLinus Torvalds  */
26141da177e4SLinus Torvalds static int hub_port_debounce(struct usb_hub *hub, int port1)
26151da177e4SLinus Torvalds {
26161da177e4SLinus Torvalds 	int ret;
26171da177e4SLinus Torvalds 	int total_time, stable_time = 0;
26181da177e4SLinus Torvalds 	u16 portchange, portstatus;
26191da177e4SLinus Torvalds 	unsigned connection = 0xffff;
26201da177e4SLinus Torvalds 
26211da177e4SLinus Torvalds 	for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
26221da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
26231da177e4SLinus Torvalds 		if (ret < 0)
26241da177e4SLinus Torvalds 			return ret;
26251da177e4SLinus Torvalds 
26261da177e4SLinus Torvalds 		if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
26271da177e4SLinus Torvalds 		     (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
26281da177e4SLinus Torvalds 			stable_time += HUB_DEBOUNCE_STEP;
26291da177e4SLinus Torvalds 			if (stable_time >= HUB_DEBOUNCE_STABLE)
26301da177e4SLinus Torvalds 				break;
26311da177e4SLinus Torvalds 		} else {
26321da177e4SLinus Torvalds 			stable_time = 0;
26331da177e4SLinus Torvalds 			connection = portstatus & USB_PORT_STAT_CONNECTION;
26341da177e4SLinus Torvalds 		}
26351da177e4SLinus Torvalds 
26361da177e4SLinus Torvalds 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
26371da177e4SLinus Torvalds 			clear_port_feature(hub->hdev, port1,
26381da177e4SLinus Torvalds 					USB_PORT_FEAT_C_CONNECTION);
26391da177e4SLinus Torvalds 		}
26401da177e4SLinus Torvalds 
26411da177e4SLinus Torvalds 		if (total_time >= HUB_DEBOUNCE_TIMEOUT)
26421da177e4SLinus Torvalds 			break;
26431da177e4SLinus Torvalds 		msleep(HUB_DEBOUNCE_STEP);
26441da177e4SLinus Torvalds 	}
26451da177e4SLinus Torvalds 
26461da177e4SLinus Torvalds 	dev_dbg (hub->intfdev,
26471da177e4SLinus Torvalds 		"debounce: port %d: total %dms stable %dms status 0x%x\n",
26481da177e4SLinus Torvalds 		port1, total_time, stable_time, portstatus);
26491da177e4SLinus Torvalds 
26501da177e4SLinus Torvalds 	if (stable_time < HUB_DEBOUNCE_STABLE)
26511da177e4SLinus Torvalds 		return -ETIMEDOUT;
26521da177e4SLinus Torvalds 	return portstatus;
26531da177e4SLinus Torvalds }
26541da177e4SLinus Torvalds 
2655fc721f51SInaky Perez-Gonzalez void usb_ep0_reinit(struct usb_device *udev)
26561da177e4SLinus Torvalds {
2657ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
2658ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
26592caf7fcdSAlan Stern 	usb_enable_endpoint(udev, &udev->ep0, true);
26601da177e4SLinus Torvalds }
2661fc721f51SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_ep0_reinit);
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds #define usb_sndaddr0pipe()	(PIPE_CONTROL << 30)
26641da177e4SLinus Torvalds #define usb_rcvaddr0pipe()	((PIPE_CONTROL << 30) | USB_DIR_IN)
26651da177e4SLinus Torvalds 
26664326ed0bSAlan Stern static int hub_set_address(struct usb_device *udev, int devnum)
26671da177e4SLinus Torvalds {
26681da177e4SLinus Torvalds 	int retval;
2669c6515272SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
26701da177e4SLinus Torvalds 
2671c6515272SSarah Sharp 	/*
2672c6515272SSarah Sharp 	 * The host controller will choose the device address,
2673c6515272SSarah Sharp 	 * instead of the core having chosen it earlier
2674c6515272SSarah Sharp 	 */
2675c6515272SSarah Sharp 	if (!hcd->driver->address_device && devnum <= 1)
26761da177e4SLinus Torvalds 		return -EINVAL;
26771da177e4SLinus Torvalds 	if (udev->state == USB_STATE_ADDRESS)
26781da177e4SLinus Torvalds 		return 0;
26791da177e4SLinus Torvalds 	if (udev->state != USB_STATE_DEFAULT)
26801da177e4SLinus Torvalds 		return -EINVAL;
2681c8d4af8eSAndiry Xu 	if (hcd->driver->address_device)
2682c6515272SSarah Sharp 		retval = hcd->driver->address_device(hcd, udev);
2683c8d4af8eSAndiry Xu 	else
26841da177e4SLinus Torvalds 		retval = usb_control_msg(udev, usb_sndaddr0pipe(),
26854326ed0bSAlan Stern 				USB_REQ_SET_ADDRESS, 0, devnum, 0,
26861da177e4SLinus Torvalds 				NULL, 0, USB_CTRL_SET_TIMEOUT);
26871da177e4SLinus Torvalds 	if (retval == 0) {
26883b29b68bSAlan Stern 		update_devnum(udev, devnum);
26894953d141SDavid Vrabel 		/* Device now using proper address. */
26901da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_ADDRESS);
2691fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
26921da177e4SLinus Torvalds 	}
26931da177e4SLinus Torvalds 	return retval;
26941da177e4SLinus Torvalds }
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds /* Reset device, (re)assign address, get device descriptor.
26971da177e4SLinus Torvalds  * Device connection must be stable, no more debouncing needed.
26981da177e4SLinus Torvalds  * Returns device in USB_STATE_ADDRESS, except on error.
26991da177e4SLinus Torvalds  *
27001da177e4SLinus Torvalds  * If this is called for an already-existing device (as part of
2701742120c6SMing Lei  * usb_reset_and_verify_device), the caller must own the device lock.  For a
27021da177e4SLinus Torvalds  * newly detected device that is not accessible through any global
27031da177e4SLinus Torvalds  * pointers, it's not necessary to lock the device.
27041da177e4SLinus Torvalds  */
27051da177e4SLinus Torvalds static int
27061da177e4SLinus Torvalds hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
27071da177e4SLinus Torvalds 		int retry_counter)
27081da177e4SLinus Torvalds {
27094186ecf8SArjan van de Ven 	static DEFINE_MUTEX(usb_address0_mutex);
27101da177e4SLinus Torvalds 
27111da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
2712e7b77172SSarah Sharp 	struct usb_hcd		*hcd = bus_to_hcd(hdev->bus);
27131da177e4SLinus Torvalds 	int			i, j, retval;
27141da177e4SLinus Torvalds 	unsigned		delay = HUB_SHORT_RESET_TIME;
27151da177e4SLinus Torvalds 	enum usb_device_speed	oldspeed = udev->speed;
271683a07196SInaky Perez-Gonzalez 	char 			*speed, *type;
27174326ed0bSAlan Stern 	int			devnum = udev->devnum;
27181da177e4SLinus Torvalds 
27191da177e4SLinus Torvalds 	/* root hub ports have a slightly longer reset period
27201da177e4SLinus Torvalds 	 * (from USB 2.0 spec, section 7.1.7.5)
27211da177e4SLinus Torvalds 	 */
27221da177e4SLinus Torvalds 	if (!hdev->parent) {
27231da177e4SLinus Torvalds 		delay = HUB_ROOT_RESET_TIME;
27241da177e4SLinus Torvalds 		if (port1 == hdev->bus->otg_port)
27251da177e4SLinus Torvalds 			hdev->bus->b_hnp_enable = 0;
27261da177e4SLinus Torvalds 	}
27271da177e4SLinus Torvalds 
27281da177e4SLinus Torvalds 	/* Some low speed devices have problems with the quick delay, so */
27291da177e4SLinus Torvalds 	/*  be a bit pessimistic with those devices. RHbug #23670 */
27301da177e4SLinus Torvalds 	if (oldspeed == USB_SPEED_LOW)
27311da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
27321da177e4SLinus Torvalds 
27334186ecf8SArjan van de Ven 	mutex_lock(&usb_address0_mutex);
27341da177e4SLinus Torvalds 
2735a5f0efabSSarah Sharp 	if (!udev->config && oldspeed == USB_SPEED_SUPER) {
2736e7b77172SSarah Sharp 		/* Don't reset USB 3.0 devices during an initial setup */
2737e7b77172SSarah Sharp 		usb_set_device_state(udev, USB_STATE_DEFAULT);
2738e7b77172SSarah Sharp 	} else {
27391da177e4SLinus Torvalds 		/* Reset the device; full speed may morph to high speed */
2740e7b77172SSarah Sharp 		/* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
27411da177e4SLinus Torvalds 		retval = hub_port_reset(hub, port1, udev, delay);
27421da177e4SLinus Torvalds 		if (retval < 0)		/* error or disconnect */
27431da177e4SLinus Torvalds 			goto fail;
27441da177e4SLinus Torvalds 		/* success, speed is known */
2745e7b77172SSarah Sharp 	}
27461da177e4SLinus Torvalds 	retval = -ENODEV;
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds 	if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
27491da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset changed speed!\n");
27501da177e4SLinus Torvalds 		goto fail;
27511da177e4SLinus Torvalds 	}
27521da177e4SLinus Torvalds 	oldspeed = udev->speed;
27531da177e4SLinus Torvalds 
27541da177e4SLinus Torvalds 	/* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
27551da177e4SLinus Torvalds 	 * it's fixed size except for full speed devices.
27565bb6e0aeSInaky Perez-Gonzalez 	 * For Wireless USB devices, ep0 max packet is always 512 (tho
27575bb6e0aeSInaky Perez-Gonzalez 	 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
27581da177e4SLinus Torvalds 	 */
27591da177e4SLinus Torvalds 	switch (udev->speed) {
27606b403b02SSarah Sharp 	case USB_SPEED_SUPER:
2761551cdbbeSGreg Kroah-Hartman 	case USB_SPEED_WIRELESS:	/* fixed at 512 */
2762551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
27635bb6e0aeSInaky Perez-Gonzalez 		break;
27641da177e4SLinus Torvalds 	case USB_SPEED_HIGH:		/* fixed at 64 */
2765551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
27661da177e4SLinus Torvalds 		break;
27671da177e4SLinus Torvalds 	case USB_SPEED_FULL:		/* 8, 16, 32, or 64 */
27681da177e4SLinus Torvalds 		/* to determine the ep0 maxpacket size, try to read
27691da177e4SLinus Torvalds 		 * the device descriptor to get bMaxPacketSize0 and
27701da177e4SLinus Torvalds 		 * then correct our initial guess.
27711da177e4SLinus Torvalds 		 */
2772551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
27731da177e4SLinus Torvalds 		break;
27741da177e4SLinus Torvalds 	case USB_SPEED_LOW:		/* fixed at 8 */
2775551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
27761da177e4SLinus Torvalds 		break;
27771da177e4SLinus Torvalds 	default:
27781da177e4SLinus Torvalds 		goto fail;
27791da177e4SLinus Torvalds 	}
27801da177e4SLinus Torvalds 
278183a07196SInaky Perez-Gonzalez 	type = "";
278283a07196SInaky Perez-Gonzalez 	switch (udev->speed) {
27831da177e4SLinus Torvalds 	case USB_SPEED_LOW:	speed = "low";	break;
27841da177e4SLinus Torvalds 	case USB_SPEED_FULL:	speed = "full";	break;
27851da177e4SLinus Torvalds 	case USB_SPEED_HIGH:	speed = "high";	break;
27866b403b02SSarah Sharp 	case USB_SPEED_SUPER:
27876b403b02SSarah Sharp 				speed = "super";
27886b403b02SSarah Sharp 				break;
2789551cdbbeSGreg Kroah-Hartman 	case USB_SPEED_WIRELESS:
279083a07196SInaky Perez-Gonzalez 				speed = "variable";
279183a07196SInaky Perez-Gonzalez 				type = "Wireless ";
279283a07196SInaky Perez-Gonzalez 				break;
27931da177e4SLinus Torvalds 	default: 		speed = "?";	break;
279483a07196SInaky Perez-Gonzalez 	}
2795c6515272SSarah Sharp 	if (udev->speed != USB_SPEED_SUPER)
279683a07196SInaky Perez-Gonzalez 		dev_info(&udev->dev,
27973b29b68bSAlan Stern 				"%s %s speed %sUSB device number %d using %s\n",
279883a07196SInaky Perez-Gonzalez 				(udev->config) ? "reset" : "new", speed, type,
27993b29b68bSAlan Stern 				devnum, udev->bus->controller->driver->name);
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds 	/* Set up TT records, if needed  */
28021da177e4SLinus Torvalds 	if (hdev->tt) {
28031da177e4SLinus Torvalds 		udev->tt = hdev->tt;
28041da177e4SLinus Torvalds 		udev->ttport = hdev->ttport;
28051da177e4SLinus Torvalds 	} else if (udev->speed != USB_SPEED_HIGH
28061da177e4SLinus Torvalds 			&& hdev->speed == USB_SPEED_HIGH) {
2807d199c96dSAlan Stern 		if (!hub->tt.hub) {
2808d199c96dSAlan Stern 			dev_err(&udev->dev, "parent hub has no TT\n");
2809d199c96dSAlan Stern 			retval = -EINVAL;
2810d199c96dSAlan Stern 			goto fail;
2811d199c96dSAlan Stern 		}
28121da177e4SLinus Torvalds 		udev->tt = &hub->tt;
28131da177e4SLinus Torvalds 		udev->ttport = port1;
28141da177e4SLinus Torvalds 	}
28151da177e4SLinus Torvalds 
28161da177e4SLinus Torvalds 	/* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
28171da177e4SLinus Torvalds 	 * Because device hardware and firmware is sometimes buggy in
28181da177e4SLinus Torvalds 	 * this area, and this is how Linux has done it for ages.
28191da177e4SLinus Torvalds 	 * Change it cautiously.
28201da177e4SLinus Torvalds 	 *
28211da177e4SLinus Torvalds 	 * NOTE:  If USE_NEW_SCHEME() is true we will start by issuing
28221da177e4SLinus Torvalds 	 * a 64-byte GET_DESCRIPTOR request.  This is what Windows does,
28231da177e4SLinus Torvalds 	 * so it may help with some non-standards-compliant devices.
28241da177e4SLinus Torvalds 	 * Otherwise we start with SET_ADDRESS and then try to read the
28251da177e4SLinus Torvalds 	 * first 8 bytes of the device descriptor to get the ep0 maxpacket
28261da177e4SLinus Torvalds 	 * value.
28271da177e4SLinus Torvalds 	 */
28281da177e4SLinus Torvalds 	for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
2829c6515272SSarah Sharp 		if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
28301da177e4SLinus Torvalds 			struct usb_device_descriptor *buf;
28311da177e4SLinus Torvalds 			int r = 0;
28321da177e4SLinus Torvalds 
28331da177e4SLinus Torvalds #define GET_DESCRIPTOR_BUFSIZE	64
28341da177e4SLinus Torvalds 			buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
28351da177e4SLinus Torvalds 			if (!buf) {
28361da177e4SLinus Torvalds 				retval = -ENOMEM;
28371da177e4SLinus Torvalds 				continue;
28381da177e4SLinus Torvalds 			}
28391da177e4SLinus Torvalds 
2840b89ee19aSAlan Stern 			/* Retry on all errors; some devices are flakey.
2841b89ee19aSAlan Stern 			 * 255 is for WUSB devices, we actually need to use
2842b89ee19aSAlan Stern 			 * 512 (WUSB1.0[4.8.1]).
28431da177e4SLinus Torvalds 			 */
28441da177e4SLinus Torvalds 			for (j = 0; j < 3; ++j) {
28451da177e4SLinus Torvalds 				buf->bMaxPacketSize0 = 0;
28461da177e4SLinus Torvalds 				r = usb_control_msg(udev, usb_rcvaddr0pipe(),
28471da177e4SLinus Torvalds 					USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
28481da177e4SLinus Torvalds 					USB_DT_DEVICE << 8, 0,
28491da177e4SLinus Torvalds 					buf, GET_DESCRIPTOR_BUFSIZE,
2850fd7c519dSJaroslav Kysela 					initial_descriptor_timeout);
28511da177e4SLinus Torvalds 				switch (buf->bMaxPacketSize0) {
28525bb6e0aeSInaky Perez-Gonzalez 				case 8: case 16: case 32: case 64: case 255:
28531da177e4SLinus Torvalds 					if (buf->bDescriptorType ==
28541da177e4SLinus Torvalds 							USB_DT_DEVICE) {
28551da177e4SLinus Torvalds 						r = 0;
28561da177e4SLinus Torvalds 						break;
28571da177e4SLinus Torvalds 					}
28581da177e4SLinus Torvalds 					/* FALL THROUGH */
28591da177e4SLinus Torvalds 				default:
28601da177e4SLinus Torvalds 					if (r == 0)
28611da177e4SLinus Torvalds 						r = -EPROTO;
28621da177e4SLinus Torvalds 					break;
28631da177e4SLinus Torvalds 				}
28641da177e4SLinus Torvalds 				if (r == 0)
28651da177e4SLinus Torvalds 					break;
28661da177e4SLinus Torvalds 			}
28671da177e4SLinus Torvalds 			udev->descriptor.bMaxPacketSize0 =
28681da177e4SLinus Torvalds 					buf->bMaxPacketSize0;
28691da177e4SLinus Torvalds 			kfree(buf);
28701da177e4SLinus Torvalds 
28711da177e4SLinus Torvalds 			retval = hub_port_reset(hub, port1, udev, delay);
28721da177e4SLinus Torvalds 			if (retval < 0)		/* error or disconnect */
28731da177e4SLinus Torvalds 				goto fail;
28741da177e4SLinus Torvalds 			if (oldspeed != udev->speed) {
28751da177e4SLinus Torvalds 				dev_dbg(&udev->dev,
28761da177e4SLinus Torvalds 					"device reset changed speed!\n");
28771da177e4SLinus Torvalds 				retval = -ENODEV;
28781da177e4SLinus Torvalds 				goto fail;
28791da177e4SLinus Torvalds 			}
28801da177e4SLinus Torvalds 			if (r) {
2881b9cef6c3SWu Fengguang 				dev_err(&udev->dev,
2882b9cef6c3SWu Fengguang 					"device descriptor read/64, error %d\n",
2883b9cef6c3SWu Fengguang 					r);
28841da177e4SLinus Torvalds 				retval = -EMSGSIZE;
28851da177e4SLinus Torvalds 				continue;
28861da177e4SLinus Torvalds 			}
28871da177e4SLinus Torvalds #undef GET_DESCRIPTOR_BUFSIZE
28881da177e4SLinus Torvalds 		}
28891da177e4SLinus Torvalds 
28906c529cdcSInaky Perez-Gonzalez  		/*
28916c529cdcSInaky Perez-Gonzalez  		 * If device is WUSB, we already assigned an
28926c529cdcSInaky Perez-Gonzalez  		 * unauthorized address in the Connect Ack sequence;
28936c529cdcSInaky Perez-Gonzalez  		 * authorization will assign the final address.
28946c529cdcSInaky Perez-Gonzalez  		 */
28956c529cdcSInaky Perez-Gonzalez 		if (udev->wusb == 0) {
28961da177e4SLinus Torvalds 			for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
28974326ed0bSAlan Stern 				retval = hub_set_address(udev, devnum);
28981da177e4SLinus Torvalds 				if (retval >= 0)
28991da177e4SLinus Torvalds 					break;
29001da177e4SLinus Torvalds 				msleep(200);
29011da177e4SLinus Torvalds 			}
29021da177e4SLinus Torvalds 			if (retval < 0) {
29031da177e4SLinus Torvalds 				dev_err(&udev->dev,
29041da177e4SLinus Torvalds 					"device not accepting address %d, error %d\n",
29054326ed0bSAlan Stern 					devnum, retval);
29061da177e4SLinus Torvalds 				goto fail;
29071da177e4SLinus Torvalds 			}
2908c6515272SSarah Sharp 			if (udev->speed == USB_SPEED_SUPER) {
2909c6515272SSarah Sharp 				devnum = udev->devnum;
2910c6515272SSarah Sharp 				dev_info(&udev->dev,
29113b29b68bSAlan Stern 						"%s SuperSpeed USB device number %d using %s\n",
2912c6515272SSarah Sharp 						(udev->config) ? "reset" : "new",
29133b29b68bSAlan Stern 						devnum, udev->bus->controller->driver->name);
2914c6515272SSarah Sharp 			}
29151da177e4SLinus Torvalds 
29161da177e4SLinus Torvalds 			/* cope with hardware quirkiness:
29171da177e4SLinus Torvalds 			 *  - let SET_ADDRESS settle, some device hardware wants it
29181da177e4SLinus Torvalds 			 *  - read ep0 maxpacket even for high and low speed,
29191da177e4SLinus Torvalds 			 */
29201da177e4SLinus Torvalds 			msleep(10);
2921c6515272SSarah Sharp 			if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
29221da177e4SLinus Torvalds 				break;
29236c529cdcSInaky Perez-Gonzalez   		}
29241da177e4SLinus Torvalds 
29251da177e4SLinus Torvalds 		retval = usb_get_device_descriptor(udev, 8);
29261da177e4SLinus Torvalds 		if (retval < 8) {
2927b9cef6c3SWu Fengguang 			dev_err(&udev->dev,
2928b9cef6c3SWu Fengguang 					"device descriptor read/8, error %d\n",
2929b9cef6c3SWu Fengguang 					retval);
29301da177e4SLinus Torvalds 			if (retval >= 0)
29311da177e4SLinus Torvalds 				retval = -EMSGSIZE;
29321da177e4SLinus Torvalds 		} else {
29331da177e4SLinus Torvalds 			retval = 0;
29341da177e4SLinus Torvalds 			break;
29351da177e4SLinus Torvalds 		}
29361da177e4SLinus Torvalds 	}
29371da177e4SLinus Torvalds 	if (retval)
29381da177e4SLinus Torvalds 		goto fail;
29391da177e4SLinus Torvalds 
29406b403b02SSarah Sharp 	if (udev->descriptor.bMaxPacketSize0 == 0xff ||
29416b403b02SSarah Sharp 			udev->speed == USB_SPEED_SUPER)
29426b403b02SSarah Sharp 		i = 512;
29436b403b02SSarah Sharp 	else
29446b403b02SSarah Sharp 		i = udev->descriptor.bMaxPacketSize0;
29451da177e4SLinus Torvalds 	if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) {
294656626a72SAlan Stern 		if (udev->speed == USB_SPEED_LOW ||
29471da177e4SLinus Torvalds 				!(i == 8 || i == 16 || i == 32 || i == 64)) {
294856626a72SAlan Stern 			dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
29491da177e4SLinus Torvalds 			retval = -EMSGSIZE;
29501da177e4SLinus Torvalds 			goto fail;
29511da177e4SLinus Torvalds 		}
295256626a72SAlan Stern 		if (udev->speed == USB_SPEED_FULL)
29531da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
295456626a72SAlan Stern 		else
295556626a72SAlan Stern 			dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
29561da177e4SLinus Torvalds 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
2957fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
29581da177e4SLinus Torvalds 	}
29591da177e4SLinus Torvalds 
29601da177e4SLinus Torvalds 	retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
29611da177e4SLinus Torvalds 	if (retval < (signed)sizeof(udev->descriptor)) {
2962b9cef6c3SWu Fengguang 		dev_err(&udev->dev, "device descriptor read/all, error %d\n",
2963b9cef6c3SWu Fengguang 			retval);
29641da177e4SLinus Torvalds 		if (retval >= 0)
29651da177e4SLinus Torvalds 			retval = -ENOMSG;
29661da177e4SLinus Torvalds 		goto fail;
29671da177e4SLinus Torvalds 	}
29681da177e4SLinus Torvalds 
29691da177e4SLinus Torvalds 	retval = 0;
297048f24970SAlek Du 	/* notify HCD that we have a device connected and addressed */
297148f24970SAlek Du 	if (hcd->driver->update_device)
297248f24970SAlek Du 		hcd->driver->update_device(hcd, udev);
29731da177e4SLinus Torvalds fail:
29744326ed0bSAlan Stern 	if (retval) {
29751da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 0);
29763b29b68bSAlan Stern 		update_devnum(udev, devnum);	/* for disconnect processing */
29774326ed0bSAlan Stern 	}
29784186ecf8SArjan van de Ven 	mutex_unlock(&usb_address0_mutex);
29791da177e4SLinus Torvalds 	return retval;
29801da177e4SLinus Torvalds }
29811da177e4SLinus Torvalds 
29821da177e4SLinus Torvalds static void
29831da177e4SLinus Torvalds check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
29841da177e4SLinus Torvalds {
29851da177e4SLinus Torvalds 	struct usb_qualifier_descriptor	*qual;
29861da177e4SLinus Torvalds 	int				status;
29871da177e4SLinus Torvalds 
2988e94b1766SChristoph Lameter 	qual = kmalloc (sizeof *qual, GFP_KERNEL);
29891da177e4SLinus Torvalds 	if (qual == NULL)
29901da177e4SLinus Torvalds 		return;
29911da177e4SLinus Torvalds 
29921da177e4SLinus Torvalds 	status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
29931da177e4SLinus Torvalds 			qual, sizeof *qual);
29941da177e4SLinus Torvalds 	if (status == sizeof *qual) {
29951da177e4SLinus Torvalds 		dev_info(&udev->dev, "not running at top speed; "
29961da177e4SLinus Torvalds 			"connect to a high speed hub\n");
29971da177e4SLinus Torvalds 		/* hub LEDs are probably harder to miss than syslog */
29981da177e4SLinus Torvalds 		if (hub->has_indicators) {
29991da177e4SLinus Torvalds 			hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
3000c4028958SDavid Howells 			schedule_delayed_work (&hub->leds, 0);
30011da177e4SLinus Torvalds 		}
30021da177e4SLinus Torvalds 	}
30031da177e4SLinus Torvalds 	kfree(qual);
30041da177e4SLinus Torvalds }
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds static unsigned
30071da177e4SLinus Torvalds hub_power_remaining (struct usb_hub *hub)
30081da177e4SLinus Torvalds {
30091da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
30101da177e4SLinus Torvalds 	int remaining;
301155c52718SAlan Stern 	int port1;
30121da177e4SLinus Torvalds 
301355c52718SAlan Stern 	if (!hub->limited_power)
30141da177e4SLinus Torvalds 		return 0;
30151da177e4SLinus Torvalds 
301655c52718SAlan Stern 	remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
301755c52718SAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
301855c52718SAlan Stern 		struct usb_device	*udev = hdev->children[port1 - 1];
301955c52718SAlan Stern 		int			delta;
30201da177e4SLinus Torvalds 
30211da177e4SLinus Torvalds 		if (!udev)
30221da177e4SLinus Torvalds 			continue;
30231da177e4SLinus Torvalds 
302455c52718SAlan Stern 		/* Unconfigured devices may not use more than 100mA,
302555c52718SAlan Stern 		 * or 8mA for OTG ports */
30261da177e4SLinus Torvalds 		if (udev->actconfig)
302755c52718SAlan Stern 			delta = udev->actconfig->desc.bMaxPower * 2;
302855c52718SAlan Stern 		else if (port1 != udev->bus->otg_port || hdev->parent)
302955c52718SAlan Stern 			delta = 100;
30301da177e4SLinus Torvalds 		else
303155c52718SAlan Stern 			delta = 8;
303255c52718SAlan Stern 		if (delta > hub->mA_per_port)
3033b9cef6c3SWu Fengguang 			dev_warn(&udev->dev,
3034b9cef6c3SWu Fengguang 				 "%dmA is over %umA budget for port %d!\n",
303555c52718SAlan Stern 				 delta, hub->mA_per_port, port1);
30361da177e4SLinus Torvalds 		remaining -= delta;
30371da177e4SLinus Torvalds 	}
30381da177e4SLinus Torvalds 	if (remaining < 0) {
303955c52718SAlan Stern 		dev_warn(hub->intfdev, "%dmA over power budget!\n",
304055c52718SAlan Stern 			- remaining);
30411da177e4SLinus Torvalds 		remaining = 0;
30421da177e4SLinus Torvalds 	}
30431da177e4SLinus Torvalds 	return remaining;
30441da177e4SLinus Torvalds }
30451da177e4SLinus Torvalds 
30461da177e4SLinus Torvalds /* Handle physical or logical connection change events.
30471da177e4SLinus Torvalds  * This routine is called when:
30481da177e4SLinus Torvalds  * 	a port connection-change occurs;
30491da177e4SLinus Torvalds  *	a port enable-change occurs (often caused by EMI);
3050742120c6SMing Lei  *	usb_reset_and_verify_device() encounters changed descriptors (as from
30511da177e4SLinus Torvalds  *		a firmware download)
30521da177e4SLinus Torvalds  * caller already locked the hub
30531da177e4SLinus Torvalds  */
30541da177e4SLinus Torvalds static void hub_port_connect_change(struct usb_hub *hub, int port1,
30551da177e4SLinus Torvalds 					u16 portstatus, u16 portchange)
30561da177e4SLinus Torvalds {
30571da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
30581da177e4SLinus Torvalds 	struct device *hub_dev = hub->intfdev;
305990da096eSBalaji Rao 	struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
306024618b0cSAlan Stern 	unsigned wHubCharacteristics =
306124618b0cSAlan Stern 			le16_to_cpu(hub->descriptor->wHubCharacteristics);
30628808f00cSAlan Stern 	struct usb_device *udev;
30631da177e4SLinus Torvalds 	int status, i;
30641da177e4SLinus Torvalds 
30651da177e4SLinus Torvalds 	dev_dbg (hub_dev,
30661da177e4SLinus Torvalds 		"port %d, status %04x, change %04x, %s\n",
3067131dec34SSarah Sharp 		port1, portstatus, portchange, portspeed(hub, portstatus));
30681da177e4SLinus Torvalds 
30691da177e4SLinus Torvalds 	if (hub->has_indicators) {
30701da177e4SLinus Torvalds 		set_port_led(hub, port1, HUB_LED_AUTO);
30711da177e4SLinus Torvalds 		hub->indicator[port1-1] = INDICATOR_AUTO;
30721da177e4SLinus Torvalds 	}
30731da177e4SLinus Torvalds 
30741da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
30751da177e4SLinus Torvalds 	/* during HNP, don't repeat the debounce */
30761da177e4SLinus Torvalds 	if (hdev->bus->is_b_host)
307724618b0cSAlan Stern 		portchange &= ~(USB_PORT_STAT_C_CONNECTION |
307824618b0cSAlan Stern 				USB_PORT_STAT_C_ENABLE);
30791da177e4SLinus Torvalds #endif
30801da177e4SLinus Torvalds 
30818808f00cSAlan Stern 	/* Try to resuscitate an existing device */
30828808f00cSAlan Stern 	udev = hdev->children[port1-1];
30838808f00cSAlan Stern 	if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
30848808f00cSAlan Stern 			udev->state != USB_STATE_NOTATTACHED) {
30858808f00cSAlan Stern 		usb_lock_device(udev);
30868808f00cSAlan Stern 		if (portstatus & USB_PORT_STAT_ENABLE) {
30878808f00cSAlan Stern 			status = 0;		/* Nothing to do */
30888808f00cSAlan Stern 
30898808f00cSAlan Stern #ifdef CONFIG_USB_SUSPEND
30905257d97aSAlan Stern 		} else if (udev->state == USB_STATE_SUSPENDED &&
30915257d97aSAlan Stern 				udev->persist_enabled) {
30928808f00cSAlan Stern 			/* For a suspended device, treat this as a
30938808f00cSAlan Stern 			 * remote wakeup event.
30948808f00cSAlan Stern 			 */
30950534d468SAlan Stern 			status = usb_remote_wakeup(udev);
30968808f00cSAlan Stern #endif
30978808f00cSAlan Stern 
30988808f00cSAlan Stern 		} else {
30995257d97aSAlan Stern 			status = -ENODEV;	/* Don't resuscitate */
31008808f00cSAlan Stern 		}
31018808f00cSAlan Stern 		usb_unlock_device(udev);
31028808f00cSAlan Stern 
31038808f00cSAlan Stern 		if (status == 0) {
31048808f00cSAlan Stern 			clear_bit(port1, hub->change_bits);
31058808f00cSAlan Stern 			return;
31068808f00cSAlan Stern 		}
31078808f00cSAlan Stern 	}
31088808f00cSAlan Stern 
310924618b0cSAlan Stern 	/* Disconnect any existing devices under this port */
31108808f00cSAlan Stern 	if (udev)
311124618b0cSAlan Stern 		usb_disconnect(&hdev->children[port1-1]);
311224618b0cSAlan Stern 	clear_bit(port1, hub->change_bits);
311324618b0cSAlan Stern 
3114253e0572SAlan Stern 	/* We can forget about a "removed" device when there's a physical
3115253e0572SAlan Stern 	 * disconnect or the connect status changes.
3116253e0572SAlan Stern 	 */
3117253e0572SAlan Stern 	if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3118253e0572SAlan Stern 			(portchange & USB_PORT_STAT_C_CONNECTION))
3119253e0572SAlan Stern 		clear_bit(port1, hub->removed_bits);
3120253e0572SAlan Stern 
31215257d97aSAlan Stern 	if (portchange & (USB_PORT_STAT_C_CONNECTION |
31225257d97aSAlan Stern 				USB_PORT_STAT_C_ENABLE)) {
31235257d97aSAlan Stern 		status = hub_port_debounce(hub, port1);
31245257d97aSAlan Stern 		if (status < 0) {
31255257d97aSAlan Stern 			if (printk_ratelimit())
31265257d97aSAlan Stern 				dev_err(hub_dev, "connect-debounce failed, "
31275257d97aSAlan Stern 						"port %d disabled\n", port1);
31285257d97aSAlan Stern 			portstatus &= ~USB_PORT_STAT_CONNECTION;
31295257d97aSAlan Stern 		} else {
31305257d97aSAlan Stern 			portstatus = status;
31315257d97aSAlan Stern 		}
31325257d97aSAlan Stern 	}
31335257d97aSAlan Stern 
3134253e0572SAlan Stern 	/* Return now if debouncing failed or nothing is connected or
3135253e0572SAlan Stern 	 * the device was "removed".
3136253e0572SAlan Stern 	 */
3137253e0572SAlan Stern 	if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3138253e0572SAlan Stern 			test_bit(port1, hub->removed_bits)) {
31391da177e4SLinus Torvalds 
31401da177e4SLinus Torvalds 		/* maybe switch power back on (e.g. root hub was reset) */
314174ad9bd2SGreg Kroah-Hartman 		if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
3142749da5f8SAlan Stern 				&& !(portstatus & USB_PORT_STAT_POWER))
31431da177e4SLinus Torvalds 			set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
31441da177e4SLinus Torvalds 
31451da177e4SLinus Torvalds 		if (portstatus & USB_PORT_STAT_ENABLE)
31461da177e4SLinus Torvalds   			goto done;
31471da177e4SLinus Torvalds 		return;
31481da177e4SLinus Torvalds 	}
31491da177e4SLinus Torvalds 
31501da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; i++) {
31511da177e4SLinus Torvalds 
31521da177e4SLinus Torvalds 		/* reallocate for each attempt, since references
31531da177e4SLinus Torvalds 		 * to the previous one can escape in various ways
31541da177e4SLinus Torvalds 		 */
31551da177e4SLinus Torvalds 		udev = usb_alloc_dev(hdev, hdev->bus, port1);
31561da177e4SLinus Torvalds 		if (!udev) {
31571da177e4SLinus Torvalds 			dev_err (hub_dev,
31581da177e4SLinus Torvalds 				"couldn't allocate port %d usb_device\n",
31591da177e4SLinus Torvalds 				port1);
31601da177e4SLinus Torvalds 			goto done;
31611da177e4SLinus Torvalds 		}
31621da177e4SLinus Torvalds 
31631da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_POWERED);
316455c52718SAlan Stern  		udev->bus_mA = hub->mA_per_port;
3165b6956ffaSAlan Stern 		udev->level = hdev->level + 1;
31668af548dcSInaky Perez-Gonzalez 		udev->wusb = hub_is_wusb(hub);
31671da177e4SLinus Torvalds 
3168131dec34SSarah Sharp 		/* Only USB 3.0 devices are connected to SuperSpeed hubs. */
3169131dec34SSarah Sharp 		if (hub_is_superspeed(hub->hdev))
3170e7b77172SSarah Sharp 			udev->speed = USB_SPEED_SUPER;
3171e7b77172SSarah Sharp 		else
3172e7b77172SSarah Sharp 			udev->speed = USB_SPEED_UNKNOWN;
3173e7b77172SSarah Sharp 
31743b29b68bSAlan Stern 		choose_devnum(udev);
3175c6515272SSarah Sharp 		if (udev->devnum <= 0) {
3176c6515272SSarah Sharp 			status = -ENOTCONN;	/* Don't retry */
3177c6515272SSarah Sharp 			goto loop;
3178c6515272SSarah Sharp 		}
3179c6515272SSarah Sharp 
3180e7b77172SSarah Sharp 		/* reset (non-USB 3.0 devices) and get descriptor */
31811da177e4SLinus Torvalds 		status = hub_port_init(hub, udev, port1, i);
31821da177e4SLinus Torvalds 		if (status < 0)
31831da177e4SLinus Torvalds 			goto loop;
31841da177e4SLinus Torvalds 
318593362a87SPhil Dibowitz 		usb_detect_quirks(udev);
318693362a87SPhil Dibowitz 		if (udev->quirks & USB_QUIRK_DELAY_INIT)
318793362a87SPhil Dibowitz 			msleep(1000);
318893362a87SPhil Dibowitz 
31891da177e4SLinus Torvalds 		/* consecutive bus-powered hubs aren't reliable; they can
31901da177e4SLinus Torvalds 		 * violate the voltage drop budget.  if the new child has
31911da177e4SLinus Torvalds 		 * a "powered" LED, users should notice we didn't enable it
31921da177e4SLinus Torvalds 		 * (without reading syslog), even without per-port LEDs
31931da177e4SLinus Torvalds 		 * on the parent.
31941da177e4SLinus Torvalds 		 */
31951da177e4SLinus Torvalds 		if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
319655c52718SAlan Stern 				&& udev->bus_mA <= 100) {
31971da177e4SLinus Torvalds 			u16	devstat;
31981da177e4SLinus Torvalds 
31991da177e4SLinus Torvalds 			status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
32001da177e4SLinus Torvalds 					&devstat);
320155c52718SAlan Stern 			if (status < 2) {
32021da177e4SLinus Torvalds 				dev_dbg(&udev->dev, "get status %d ?\n", status);
32031da177e4SLinus Torvalds 				goto loop_disable;
32041da177e4SLinus Torvalds 			}
320555c52718SAlan Stern 			le16_to_cpus(&devstat);
32061da177e4SLinus Torvalds 			if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
32071da177e4SLinus Torvalds 				dev_err(&udev->dev,
32081da177e4SLinus Torvalds 					"can't connect bus-powered hub "
32091da177e4SLinus Torvalds 					"to this port\n");
32101da177e4SLinus Torvalds 				if (hub->has_indicators) {
32111da177e4SLinus Torvalds 					hub->indicator[port1-1] =
32121da177e4SLinus Torvalds 						INDICATOR_AMBER_BLINK;
3213c4028958SDavid Howells 					schedule_delayed_work (&hub->leds, 0);
32141da177e4SLinus Torvalds 				}
32151da177e4SLinus Torvalds 				status = -ENOTCONN;	/* Don't retry */
32161da177e4SLinus Torvalds 				goto loop_disable;
32171da177e4SLinus Torvalds 			}
32181da177e4SLinus Torvalds 		}
32191da177e4SLinus Torvalds 
32201da177e4SLinus Torvalds 		/* check for devices running slower than they could */
32211da177e4SLinus Torvalds 		if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
32221da177e4SLinus Torvalds 				&& udev->speed == USB_SPEED_FULL
32231da177e4SLinus Torvalds 				&& highspeed_hubs != 0)
32241da177e4SLinus Torvalds 			check_highspeed (hub, udev, port1);
32251da177e4SLinus Torvalds 
32261da177e4SLinus Torvalds 		/* Store the parent's children[] pointer.  At this point
32271da177e4SLinus Torvalds 		 * udev becomes globally accessible, although presumably
32281da177e4SLinus Torvalds 		 * no one will look at it until hdev is unlocked.
32291da177e4SLinus Torvalds 		 */
32301da177e4SLinus Torvalds 		status = 0;
32311da177e4SLinus Torvalds 
32321da177e4SLinus Torvalds 		/* We mustn't add new devices if the parent hub has
32331da177e4SLinus Torvalds 		 * been disconnected; we would race with the
32341da177e4SLinus Torvalds 		 * recursively_mark_NOTATTACHED() routine.
32351da177e4SLinus Torvalds 		 */
32361da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
32371da177e4SLinus Torvalds 		if (hdev->state == USB_STATE_NOTATTACHED)
32381da177e4SLinus Torvalds 			status = -ENOTCONN;
32391da177e4SLinus Torvalds 		else
32401da177e4SLinus Torvalds 			hdev->children[port1-1] = udev;
32411da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
32421da177e4SLinus Torvalds 
32431da177e4SLinus Torvalds 		/* Run it through the hoops (find a driver, etc) */
32441da177e4SLinus Torvalds 		if (!status) {
32451da177e4SLinus Torvalds 			status = usb_new_device(udev);
32461da177e4SLinus Torvalds 			if (status) {
32471da177e4SLinus Torvalds 				spin_lock_irq(&device_state_lock);
32481da177e4SLinus Torvalds 				hdev->children[port1-1] = NULL;
32491da177e4SLinus Torvalds 				spin_unlock_irq(&device_state_lock);
32501da177e4SLinus Torvalds 			}
32511da177e4SLinus Torvalds 		}
32521da177e4SLinus Torvalds 
32531da177e4SLinus Torvalds 		if (status)
32541da177e4SLinus Torvalds 			goto loop_disable;
32551da177e4SLinus Torvalds 
32561da177e4SLinus Torvalds 		status = hub_power_remaining(hub);
32571da177e4SLinus Torvalds 		if (status)
325855c52718SAlan Stern 			dev_dbg(hub_dev, "%dmA power budget left\n", status);
32591da177e4SLinus Torvalds 
32601da177e4SLinus Torvalds 		return;
32611da177e4SLinus Torvalds 
32621da177e4SLinus Torvalds loop_disable:
32631da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 1);
32641da177e4SLinus Torvalds loop:
3265fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
32663b29b68bSAlan Stern 		release_devnum(udev);
3267f7410cedSHerbert Xu 		hub_free_dev(udev);
32681da177e4SLinus Torvalds 		usb_put_dev(udev);
3269ffcdc18dSVikram Pandita 		if ((status == -ENOTCONN) || (status == -ENOTSUPP))
32701da177e4SLinus Torvalds 			break;
32711da177e4SLinus Torvalds 	}
32723a31155cSAlan Stern 	if (hub->hdev->parent ||
32733a31155cSAlan Stern 			!hcd->driver->port_handed_over ||
32743a31155cSAlan Stern 			!(hcd->driver->port_handed_over)(hcd, port1))
32753a31155cSAlan Stern 		dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
32763a31155cSAlan Stern 				port1);
32771da177e4SLinus Torvalds 
32781da177e4SLinus Torvalds done:
32791da177e4SLinus Torvalds 	hub_port_disable(hub, port1, 1);
328090da096eSBalaji Rao 	if (hcd->driver->relinquish_port && !hub->hdev->parent)
328190da096eSBalaji Rao 		hcd->driver->relinquish_port(hcd, port1);
32821da177e4SLinus Torvalds }
32831da177e4SLinus Torvalds 
32841da177e4SLinus Torvalds static void hub_events(void)
32851da177e4SLinus Torvalds {
32861da177e4SLinus Torvalds 	struct list_head *tmp;
32871da177e4SLinus Torvalds 	struct usb_device *hdev;
32881da177e4SLinus Torvalds 	struct usb_interface *intf;
32891da177e4SLinus Torvalds 	struct usb_hub *hub;
32901da177e4SLinus Torvalds 	struct device *hub_dev;
32911da177e4SLinus Torvalds 	u16 hubstatus;
32921da177e4SLinus Torvalds 	u16 hubchange;
32931da177e4SLinus Torvalds 	u16 portstatus;
32941da177e4SLinus Torvalds 	u16 portchange;
32951da177e4SLinus Torvalds 	int i, ret;
32961da177e4SLinus Torvalds 	int connect_change;
32971da177e4SLinus Torvalds 
32981da177e4SLinus Torvalds 	/*
32991da177e4SLinus Torvalds 	 *  We restart the list every time to avoid a deadlock with
33001da177e4SLinus Torvalds 	 * deleting hubs downstream from this one. This should be
33011da177e4SLinus Torvalds 	 * safe since we delete the hub from the event list.
33021da177e4SLinus Torvalds 	 * Not the most efficient, but avoids deadlocks.
33031da177e4SLinus Torvalds 	 */
33041da177e4SLinus Torvalds 	while (1) {
33051da177e4SLinus Torvalds 
33061da177e4SLinus Torvalds 		/* Grab the first entry at the beginning of the list */
33071da177e4SLinus Torvalds 		spin_lock_irq(&hub_event_lock);
33081da177e4SLinus Torvalds 		if (list_empty(&hub_event_list)) {
33091da177e4SLinus Torvalds 			spin_unlock_irq(&hub_event_lock);
33101da177e4SLinus Torvalds 			break;
33111da177e4SLinus Torvalds 		}
33121da177e4SLinus Torvalds 
33131da177e4SLinus Torvalds 		tmp = hub_event_list.next;
33141da177e4SLinus Torvalds 		list_del_init(tmp);
33151da177e4SLinus Torvalds 
33161da177e4SLinus Torvalds 		hub = list_entry(tmp, struct usb_hub, event_list);
3317e8054854SAlan Stern 		kref_get(&hub->kref);
3318e8054854SAlan Stern 		spin_unlock_irq(&hub_event_lock);
33191da177e4SLinus Torvalds 
3320e8054854SAlan Stern 		hdev = hub->hdev;
3321e8054854SAlan Stern 		hub_dev = hub->intfdev;
3322e8054854SAlan Stern 		intf = to_usb_interface(hub_dev);
332340f122f3SAlan Stern 		dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
33241da177e4SLinus Torvalds 				hdev->state, hub->descriptor
33251da177e4SLinus Torvalds 					? hub->descriptor->bNbrPorts
33261da177e4SLinus Torvalds 					: 0,
33271da177e4SLinus Torvalds 				/* NOTE: expects max 15 ports... */
33281da177e4SLinus Torvalds 				(u16) hub->change_bits[0],
332940f122f3SAlan Stern 				(u16) hub->event_bits[0]);
33301da177e4SLinus Torvalds 
33311da177e4SLinus Torvalds 		/* Lock the device, then check to see if we were
33321da177e4SLinus Torvalds 		 * disconnected while waiting for the lock to succeed. */
333306b84e8aSAlan Stern 		usb_lock_device(hdev);
3334e8054854SAlan Stern 		if (unlikely(hub->disconnected))
33359bbdf1e0SAlan Stern 			goto loop_disconnected;
33361da177e4SLinus Torvalds 
33371da177e4SLinus Torvalds 		/* If the hub has died, clean up after it */
33381da177e4SLinus Torvalds 		if (hdev->state == USB_STATE_NOTATTACHED) {
33397de18d8bSAlan Stern 			hub->error = -ENODEV;
33404330354fSAlan Stern 			hub_quiesce(hub, HUB_DISCONNECT);
33411da177e4SLinus Torvalds 			goto loop;
33421da177e4SLinus Torvalds 		}
33431da177e4SLinus Torvalds 
334440f122f3SAlan Stern 		/* Autoresume */
334540f122f3SAlan Stern 		ret = usb_autopm_get_interface(intf);
334640f122f3SAlan Stern 		if (ret) {
334740f122f3SAlan Stern 			dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
33481da177e4SLinus Torvalds 			goto loop;
334940f122f3SAlan Stern 		}
335040f122f3SAlan Stern 
335140f122f3SAlan Stern 		/* If this is an inactive hub, do nothing */
335240f122f3SAlan Stern 		if (hub->quiescing)
335340f122f3SAlan Stern 			goto loop_autopm;
33541da177e4SLinus Torvalds 
33551da177e4SLinus Torvalds 		if (hub->error) {
33561da177e4SLinus Torvalds 			dev_dbg (hub_dev, "resetting for error %d\n",
33571da177e4SLinus Torvalds 				hub->error);
33581da177e4SLinus Torvalds 
3359742120c6SMing Lei 			ret = usb_reset_device(hdev);
33601da177e4SLinus Torvalds 			if (ret) {
33611da177e4SLinus Torvalds 				dev_dbg (hub_dev,
33621da177e4SLinus Torvalds 					"error resetting hub: %d\n", ret);
336340f122f3SAlan Stern 				goto loop_autopm;
33641da177e4SLinus Torvalds 			}
33651da177e4SLinus Torvalds 
33661da177e4SLinus Torvalds 			hub->nerrors = 0;
33671da177e4SLinus Torvalds 			hub->error = 0;
33681da177e4SLinus Torvalds 		}
33691da177e4SLinus Torvalds 
33701da177e4SLinus Torvalds 		/* deal with port status changes */
33711da177e4SLinus Torvalds 		for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
33721da177e4SLinus Torvalds 			if (test_bit(i, hub->busy_bits))
33731da177e4SLinus Torvalds 				continue;
33741da177e4SLinus Torvalds 			connect_change = test_bit(i, hub->change_bits);
33751da177e4SLinus Torvalds 			if (!test_and_clear_bit(i, hub->event_bits) &&
33766ee0b270SAlan Stern 					!connect_change)
33771da177e4SLinus Torvalds 				continue;
33781da177e4SLinus Torvalds 
33791da177e4SLinus Torvalds 			ret = hub_port_status(hub, i,
33801da177e4SLinus Torvalds 					&portstatus, &portchange);
33811da177e4SLinus Torvalds 			if (ret < 0)
33821da177e4SLinus Torvalds 				continue;
33831da177e4SLinus Torvalds 
33841da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_CONNECTION) {
33851da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
33861da177e4SLinus Torvalds 					USB_PORT_FEAT_C_CONNECTION);
33871da177e4SLinus Torvalds 				connect_change = 1;
33881da177e4SLinus Torvalds 			}
33891da177e4SLinus Torvalds 
33901da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_ENABLE) {
33911da177e4SLinus Torvalds 				if (!connect_change)
33921da177e4SLinus Torvalds 					dev_dbg (hub_dev,
33931da177e4SLinus Torvalds 						"port %d enable change, "
33941da177e4SLinus Torvalds 						"status %08x\n",
33951da177e4SLinus Torvalds 						i, portstatus);
33961da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
33971da177e4SLinus Torvalds 					USB_PORT_FEAT_C_ENABLE);
33981da177e4SLinus Torvalds 
33991da177e4SLinus Torvalds 				/*
34001da177e4SLinus Torvalds 				 * EM interference sometimes causes badly
34011da177e4SLinus Torvalds 				 * shielded USB devices to be shutdown by
34021da177e4SLinus Torvalds 				 * the hub, this hack enables them again.
34031da177e4SLinus Torvalds 				 * Works at least with mouse driver.
34041da177e4SLinus Torvalds 				 */
34051da177e4SLinus Torvalds 				if (!(portstatus & USB_PORT_STAT_ENABLE)
34061da177e4SLinus Torvalds 				    && !connect_change
34071da177e4SLinus Torvalds 				    && hdev->children[i-1]) {
34081da177e4SLinus Torvalds 					dev_err (hub_dev,
34091da177e4SLinus Torvalds 					    "port %i "
34101da177e4SLinus Torvalds 					    "disabled by hub (EMI?), "
34111da177e4SLinus Torvalds 					    "re-enabling...\n",
34121da177e4SLinus Torvalds 						i);
34131da177e4SLinus Torvalds 					connect_change = 1;
34141da177e4SLinus Torvalds 				}
34151da177e4SLinus Torvalds 			}
34161da177e4SLinus Torvalds 
34171da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_SUSPEND) {
34188808f00cSAlan Stern 				struct usb_device *udev;
34198808f00cSAlan Stern 
34201da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
34211da177e4SLinus Torvalds 					USB_PORT_FEAT_C_SUSPEND);
34228808f00cSAlan Stern 				udev = hdev->children[i-1];
34238808f00cSAlan Stern 				if (udev) {
342449d0f078SAlan Stern 					/* TRSMRCY = 10 msec */
342549d0f078SAlan Stern 					msleep(10);
342649d0f078SAlan Stern 
34278808f00cSAlan Stern 					usb_lock_device(udev);
34280534d468SAlan Stern 					ret = usb_remote_wakeup(hdev->
34291da177e4SLinus Torvalds 							children[i-1]);
34308808f00cSAlan Stern 					usb_unlock_device(udev);
34311da177e4SLinus Torvalds 					if (ret < 0)
34321da177e4SLinus Torvalds 						connect_change = 1;
34331da177e4SLinus Torvalds 				} else {
34341da177e4SLinus Torvalds 					ret = -ENODEV;
34351da177e4SLinus Torvalds 					hub_port_disable(hub, i, 1);
34361da177e4SLinus Torvalds 				}
34371da177e4SLinus Torvalds 				dev_dbg (hub_dev,
34381da177e4SLinus Torvalds 					"resume on port %d, status %d\n",
34391da177e4SLinus Torvalds 					i, ret);
34401da177e4SLinus Torvalds 			}
34411da177e4SLinus Torvalds 
34421da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
3443752d57a8SPaul Bolle 				u16 status = 0;
3444752d57a8SPaul Bolle 				u16 unused;
3445752d57a8SPaul Bolle 
3446752d57a8SPaul Bolle 				dev_dbg(hub_dev, "over-current change on port "
3447752d57a8SPaul Bolle 					"%d\n", i);
34481da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
34491da177e4SLinus Torvalds 					USB_PORT_FEAT_C_OVER_CURRENT);
3450752d57a8SPaul Bolle 				msleep(100);	/* Cool down */
34518520f380SAlan Stern 				hub_power_on(hub, true);
3452752d57a8SPaul Bolle 				hub_port_status(hub, i, &status, &unused);
3453752d57a8SPaul Bolle 				if (status & USB_PORT_STAT_OVERCURRENT)
3454752d57a8SPaul Bolle 					dev_err(hub_dev, "over-current "
3455752d57a8SPaul Bolle 						"condition on port %d\n", i);
34561da177e4SLinus Torvalds 			}
34571da177e4SLinus Torvalds 
34581da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_RESET) {
34591da177e4SLinus Torvalds 				dev_dbg (hub_dev,
34601da177e4SLinus Torvalds 					"reset change on port %d\n",
34611da177e4SLinus Torvalds 					i);
34621da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
34631da177e4SLinus Torvalds 					USB_PORT_FEAT_C_RESET);
34641da177e4SLinus Torvalds 			}
3465c7061574SSarah Sharp 			if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
3466c7061574SSarah Sharp 					hub_is_superspeed(hub->hdev)) {
3467c7061574SSarah Sharp 				dev_dbg(hub_dev,
3468c7061574SSarah Sharp 					"warm reset change on port %d\n",
3469c7061574SSarah Sharp 					i);
3470c7061574SSarah Sharp 				clear_port_feature(hdev, i,
3471c7061574SSarah Sharp 					USB_PORT_FEAT_C_BH_PORT_RESET);
3472c7061574SSarah Sharp 			}
3473dbe79bbeSJohn Youn 			if (portchange & USB_PORT_STAT_C_LINK_STATE) {
3474dbe79bbeSJohn Youn 				clear_port_feature(hub->hdev, i,
3475dbe79bbeSJohn Youn 						USB_PORT_FEAT_C_PORT_LINK_STATE);
3476dbe79bbeSJohn Youn 			}
3477dbe79bbeSJohn Youn 			if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
3478dbe79bbeSJohn Youn 				dev_warn(hub_dev,
3479dbe79bbeSJohn Youn 					"config error on port %d\n",
3480dbe79bbeSJohn Youn 					i);
3481dbe79bbeSJohn Youn 				clear_port_feature(hub->hdev, i,
3482dbe79bbeSJohn Youn 						USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
3483dbe79bbeSJohn Youn 			}
34841da177e4SLinus Torvalds 
34851da177e4SLinus Torvalds 			if (connect_change)
34861da177e4SLinus Torvalds 				hub_port_connect_change(hub, i,
34871da177e4SLinus Torvalds 						portstatus, portchange);
34881da177e4SLinus Torvalds 		} /* end for i */
34891da177e4SLinus Torvalds 
34901da177e4SLinus Torvalds 		/* deal with hub status changes */
34911da177e4SLinus Torvalds 		if (test_and_clear_bit(0, hub->event_bits) == 0)
34921da177e4SLinus Torvalds 			;	/* do nothing */
34931da177e4SLinus Torvalds 		else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
34941da177e4SLinus Torvalds 			dev_err (hub_dev, "get_hub_status failed\n");
34951da177e4SLinus Torvalds 		else {
34961da177e4SLinus Torvalds 			if (hubchange & HUB_CHANGE_LOCAL_POWER) {
34971da177e4SLinus Torvalds 				dev_dbg (hub_dev, "power change\n");
34981da177e4SLinus Torvalds 				clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
349955c52718SAlan Stern 				if (hubstatus & HUB_STATUS_LOCAL_POWER)
350055c52718SAlan Stern 					/* FIXME: Is this always true? */
350155c52718SAlan Stern 					hub->limited_power = 1;
3502403fae78Sjidong xiao 				else
3503403fae78Sjidong xiao 					hub->limited_power = 0;
35041da177e4SLinus Torvalds 			}
35051da177e4SLinus Torvalds 			if (hubchange & HUB_CHANGE_OVERCURRENT) {
3506752d57a8SPaul Bolle 				u16 status = 0;
3507752d57a8SPaul Bolle 				u16 unused;
3508752d57a8SPaul Bolle 
3509752d57a8SPaul Bolle 				dev_dbg(hub_dev, "over-current change\n");
35101da177e4SLinus Torvalds 				clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
3511752d57a8SPaul Bolle 				msleep(500);	/* Cool down */
35128520f380SAlan Stern                         	hub_power_on(hub, true);
3513752d57a8SPaul Bolle 				hub_hub_status(hub, &status, &unused);
3514752d57a8SPaul Bolle 				if (status & HUB_STATUS_OVERCURRENT)
3515752d57a8SPaul Bolle 					dev_err(hub_dev, "over-current "
3516752d57a8SPaul Bolle 						"condition\n");
35171da177e4SLinus Torvalds 			}
35181da177e4SLinus Torvalds 		}
35191da177e4SLinus Torvalds 
352040f122f3SAlan Stern  loop_autopm:
35218e4ceb38SAlan Stern 		/* Balance the usb_autopm_get_interface() above */
35228e4ceb38SAlan Stern 		usb_autopm_put_interface_no_suspend(intf);
35231da177e4SLinus Torvalds  loop:
35248e4ceb38SAlan Stern 		/* Balance the usb_autopm_get_interface_no_resume() in
35258e4ceb38SAlan Stern 		 * kick_khubd() and allow autosuspend.
35268e4ceb38SAlan Stern 		 */
35278e4ceb38SAlan Stern 		usb_autopm_put_interface(intf);
35289bbdf1e0SAlan Stern  loop_disconnected:
35291da177e4SLinus Torvalds 		usb_unlock_device(hdev);
3530e8054854SAlan Stern 		kref_put(&hub->kref, hub_release);
35311da177e4SLinus Torvalds 
35321da177e4SLinus Torvalds         } /* end while (1) */
35331da177e4SLinus Torvalds }
35341da177e4SLinus Torvalds 
35351da177e4SLinus Torvalds static int hub_thread(void *__unused)
35361da177e4SLinus Torvalds {
35373bb1af52SAlan Stern 	/* khubd needs to be freezable to avoid intefering with USB-PERSIST
35383bb1af52SAlan Stern 	 * port handover.  Otherwise it might see that a full-speed device
35393bb1af52SAlan Stern 	 * was gone before the EHCI controller had handed its port over to
35403bb1af52SAlan Stern 	 * the companion full-speed controller.
35413bb1af52SAlan Stern 	 */
354283144186SRafael J. Wysocki 	set_freezable();
35433bb1af52SAlan Stern 
35441da177e4SLinus Torvalds 	do {
35451da177e4SLinus Torvalds 		hub_events();
3546e42837bcSRafael J. Wysocki 		wait_event_freezable(khubd_wait,
35479c8d6178Sakpm@osdl.org 				!list_empty(&hub_event_list) ||
35489c8d6178Sakpm@osdl.org 				kthread_should_stop());
35499c8d6178Sakpm@osdl.org 	} while (!kthread_should_stop() || !list_empty(&hub_event_list));
35501da177e4SLinus Torvalds 
35511da177e4SLinus Torvalds 	pr_debug("%s: khubd exiting\n", usbcore_name);
35529c8d6178Sakpm@osdl.org 	return 0;
35531da177e4SLinus Torvalds }
35541da177e4SLinus Torvalds 
35551e927d96SNémeth Márton static const struct usb_device_id hub_id_table[] = {
35561da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
35571da177e4SLinus Torvalds       .bDeviceClass = USB_CLASS_HUB},
35581da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
35591da177e4SLinus Torvalds       .bInterfaceClass = USB_CLASS_HUB},
35601da177e4SLinus Torvalds     { }						/* Terminating entry */
35611da177e4SLinus Torvalds };
35621da177e4SLinus Torvalds 
35631da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, hub_id_table);
35641da177e4SLinus Torvalds 
35651da177e4SLinus Torvalds static struct usb_driver hub_driver = {
35661da177e4SLinus Torvalds 	.name =		"hub",
35671da177e4SLinus Torvalds 	.probe =	hub_probe,
35681da177e4SLinus Torvalds 	.disconnect =	hub_disconnect,
35691da177e4SLinus Torvalds 	.suspend =	hub_suspend,
35701da177e4SLinus Torvalds 	.resume =	hub_resume,
3571f07600cfSAlan Stern 	.reset_resume =	hub_reset_resume,
35727de18d8bSAlan Stern 	.pre_reset =	hub_pre_reset,
35737de18d8bSAlan Stern 	.post_reset =	hub_post_reset,
3574c532b29aSAndi Kleen 	.unlocked_ioctl = hub_ioctl,
35751da177e4SLinus Torvalds 	.id_table =	hub_id_table,
357640f122f3SAlan Stern 	.supports_autosuspend =	1,
35771da177e4SLinus Torvalds };
35781da177e4SLinus Torvalds 
35791da177e4SLinus Torvalds int usb_hub_init(void)
35801da177e4SLinus Torvalds {
35811da177e4SLinus Torvalds 	if (usb_register(&hub_driver) < 0) {
35821da177e4SLinus Torvalds 		printk(KERN_ERR "%s: can't register hub driver\n",
35831da177e4SLinus Torvalds 			usbcore_name);
35841da177e4SLinus Torvalds 		return -1;
35851da177e4SLinus Torvalds 	}
35861da177e4SLinus Torvalds 
35879c8d6178Sakpm@osdl.org 	khubd_task = kthread_run(hub_thread, NULL, "khubd");
35889c8d6178Sakpm@osdl.org 	if (!IS_ERR(khubd_task))
35891da177e4SLinus Torvalds 		return 0;
35901da177e4SLinus Torvalds 
35911da177e4SLinus Torvalds 	/* Fall through if kernel_thread failed */
35921da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
35931da177e4SLinus Torvalds 	printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
35941da177e4SLinus Torvalds 
35951da177e4SLinus Torvalds 	return -1;
35961da177e4SLinus Torvalds }
35971da177e4SLinus Torvalds 
35981da177e4SLinus Torvalds void usb_hub_cleanup(void)
35991da177e4SLinus Torvalds {
36009c8d6178Sakpm@osdl.org 	kthread_stop(khubd_task);
36011da177e4SLinus Torvalds 
36021da177e4SLinus Torvalds 	/*
36031da177e4SLinus Torvalds 	 * Hub resources are freed for us by usb_deregister. It calls
36041da177e4SLinus Torvalds 	 * usb_driver_purge on every device which in turn calls that
36051da177e4SLinus Torvalds 	 * devices disconnect function if it is using this driver.
36061da177e4SLinus Torvalds 	 * The hub_disconnect function takes care of releasing the
36071da177e4SLinus Torvalds 	 * individual hub resources. -greg
36081da177e4SLinus Torvalds 	 */
36091da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
36101da177e4SLinus Torvalds } /* usb_hub_cleanup() */
36111da177e4SLinus Torvalds 
3612eb764c4bSAlan Stern static int descriptors_changed(struct usb_device *udev,
3613eb764c4bSAlan Stern 		struct usb_device_descriptor *old_device_descriptor)
36141da177e4SLinus Torvalds {
3615eb764c4bSAlan Stern 	int		changed = 0;
36161da177e4SLinus Torvalds 	unsigned	index;
3617eb764c4bSAlan Stern 	unsigned	serial_len = 0;
3618eb764c4bSAlan Stern 	unsigned	len;
3619eb764c4bSAlan Stern 	unsigned	old_length;
3620eb764c4bSAlan Stern 	int		length;
3621eb764c4bSAlan Stern 	char		*buf;
36221da177e4SLinus Torvalds 
3623eb764c4bSAlan Stern 	if (memcmp(&udev->descriptor, old_device_descriptor,
3624eb764c4bSAlan Stern 			sizeof(*old_device_descriptor)) != 0)
3625eb764c4bSAlan Stern 		return 1;
3626eb764c4bSAlan Stern 
3627eb764c4bSAlan Stern 	/* Since the idVendor, idProduct, and bcdDevice values in the
3628eb764c4bSAlan Stern 	 * device descriptor haven't changed, we will assume the
3629eb764c4bSAlan Stern 	 * Manufacturer and Product strings haven't changed either.
3630eb764c4bSAlan Stern 	 * But the SerialNumber string could be different (e.g., a
3631eb764c4bSAlan Stern 	 * different flash card of the same brand).
3632eb764c4bSAlan Stern 	 */
3633eb764c4bSAlan Stern 	if (udev->serial)
3634eb764c4bSAlan Stern 		serial_len = strlen(udev->serial) + 1;
3635eb764c4bSAlan Stern 
3636eb764c4bSAlan Stern 	len = serial_len;
36371da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
3638eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
3639eb764c4bSAlan Stern 		len = max(len, old_length);
36401da177e4SLinus Torvalds 	}
3641eb764c4bSAlan Stern 
36420cc1a51fSOliver Neukum 	buf = kmalloc(len, GFP_NOIO);
36431da177e4SLinus Torvalds 	if (buf == NULL) {
36441da177e4SLinus Torvalds 		dev_err(&udev->dev, "no mem to re-read configs after reset\n");
36451da177e4SLinus Torvalds 		/* assume the worst */
36461da177e4SLinus Torvalds 		return 1;
36471da177e4SLinus Torvalds 	}
36481da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
3649eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
36501da177e4SLinus Torvalds 		length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
36511da177e4SLinus Torvalds 				old_length);
3652eb764c4bSAlan Stern 		if (length != old_length) {
36531da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d, error %d\n",
36541da177e4SLinus Torvalds 					index, length);
3655eb764c4bSAlan Stern 			changed = 1;
36561da177e4SLinus Torvalds 			break;
36571da177e4SLinus Torvalds 		}
36581da177e4SLinus Torvalds 		if (memcmp (buf, udev->rawdescriptors[index], old_length)
36591da177e4SLinus Torvalds 				!= 0) {
36601da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
3661eb764c4bSAlan Stern 				index,
3662eb764c4bSAlan Stern 				((struct usb_config_descriptor *) buf)->
3663eb764c4bSAlan Stern 					bConfigurationValue);
3664eb764c4bSAlan Stern 			changed = 1;
36651da177e4SLinus Torvalds 			break;
36661da177e4SLinus Torvalds 		}
36671da177e4SLinus Torvalds 	}
3668eb764c4bSAlan Stern 
3669eb764c4bSAlan Stern 	if (!changed && serial_len) {
3670eb764c4bSAlan Stern 		length = usb_string(udev, udev->descriptor.iSerialNumber,
3671eb764c4bSAlan Stern 				buf, serial_len);
3672eb764c4bSAlan Stern 		if (length + 1 != serial_len) {
3673eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string error %d\n",
3674eb764c4bSAlan Stern 					length);
3675eb764c4bSAlan Stern 			changed = 1;
3676eb764c4bSAlan Stern 		} else if (memcmp(buf, udev->serial, length) != 0) {
3677eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string changed\n");
3678eb764c4bSAlan Stern 			changed = 1;
3679eb764c4bSAlan Stern 		}
3680eb764c4bSAlan Stern 	}
3681eb764c4bSAlan Stern 
36821da177e4SLinus Torvalds 	kfree(buf);
3683eb764c4bSAlan Stern 	return changed;
36841da177e4SLinus Torvalds }
36851da177e4SLinus Torvalds 
36861da177e4SLinus Torvalds /**
3687742120c6SMing Lei  * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
36881da177e4SLinus Torvalds  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
36891da177e4SLinus Torvalds  *
369079efa097SAlan Stern  * WARNING - don't use this routine to reset a composite device
369179efa097SAlan Stern  * (one with multiple interfaces owned by separate drivers)!
3692742120c6SMing Lei  * Use usb_reset_device() instead.
36931da177e4SLinus Torvalds  *
36941da177e4SLinus Torvalds  * Do a port reset, reassign the device's address, and establish its
36951da177e4SLinus Torvalds  * former operating configuration.  If the reset fails, or the device's
36961da177e4SLinus Torvalds  * descriptors change from their values before the reset, or the original
36971da177e4SLinus Torvalds  * configuration and altsettings cannot be restored, a flag will be set
36981da177e4SLinus Torvalds  * telling khubd to pretend the device has been disconnected and then
36991da177e4SLinus Torvalds  * re-connected.  All drivers will be unbound, and the device will be
37001da177e4SLinus Torvalds  * re-enumerated and probed all over again.
37011da177e4SLinus Torvalds  *
37021da177e4SLinus Torvalds  * Returns 0 if the reset succeeded, -ENODEV if the device has been
37031da177e4SLinus Torvalds  * flagged for logical disconnection, or some other negative error code
37041da177e4SLinus Torvalds  * if the reset wasn't even attempted.
37051da177e4SLinus Torvalds  *
37061da177e4SLinus Torvalds  * The caller must own the device lock.  For example, it's safe to use
37071da177e4SLinus Torvalds  * this from a driver probe() routine after downloading new firmware.
37081da177e4SLinus Torvalds  * For calls that might not occur during probe(), drivers should lock
37091da177e4SLinus Torvalds  * the device using usb_lock_device_for_reset().
37106bc6cff5SAlan Stern  *
37116bc6cff5SAlan Stern  * Locking exception: This routine may also be called from within an
37126bc6cff5SAlan Stern  * autoresume handler.  Such usage won't conflict with other tasks
37136bc6cff5SAlan Stern  * holding the device lock because these tasks should always call
37146bc6cff5SAlan Stern  * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
37151da177e4SLinus Torvalds  */
3716742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev)
37171da177e4SLinus Torvalds {
37181da177e4SLinus Torvalds 	struct usb_device		*parent_hdev = udev->parent;
37191da177e4SLinus Torvalds 	struct usb_hub			*parent_hub;
37203f0479e0SSarah Sharp 	struct usb_hcd			*hcd = bus_to_hcd(udev->bus);
37211da177e4SLinus Torvalds 	struct usb_device_descriptor	descriptor = udev->descriptor;
372212c3da34SAlan Stern 	int 				i, ret = 0;
372312c3da34SAlan Stern 	int				port1 = udev->portnum;
37241da177e4SLinus Torvalds 
37251da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED ||
37261da177e4SLinus Torvalds 			udev->state == USB_STATE_SUSPENDED) {
37271da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
37281da177e4SLinus Torvalds 				udev->state);
37291da177e4SLinus Torvalds 		return -EINVAL;
37301da177e4SLinus Torvalds 	}
37311da177e4SLinus Torvalds 
37321da177e4SLinus Torvalds 	if (!parent_hdev) {
37334bec9917SWolfram Sang 		/* this requires hcd-specific logic; see ohci_restart() */
3734441b62c1SHarvey Harrison 		dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
37351da177e4SLinus Torvalds 		return -EISDIR;
37361da177e4SLinus Torvalds 	}
37371da177e4SLinus Torvalds 	parent_hub = hdev_to_hub(parent_hdev);
37381da177e4SLinus Torvalds 
37391da177e4SLinus Torvalds 	set_bit(port1, parent_hub->busy_bits);
37401da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; ++i) {
37411da177e4SLinus Torvalds 
37421da177e4SLinus Torvalds 		/* ep0 maxpacket size may change; let the HCD know about it.
37431da177e4SLinus Torvalds 		 * Other endpoints will be handled by re-enumeration. */
3744fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
37451da177e4SLinus Torvalds 		ret = hub_port_init(parent_hub, udev, port1, i);
3746dd4dd19eSAlan Stern 		if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
37471da177e4SLinus Torvalds 			break;
37481da177e4SLinus Torvalds 	}
37491da177e4SLinus Torvalds 	clear_bit(port1, parent_hub->busy_bits);
3750d5cbad4bSAlan Stern 
37511da177e4SLinus Torvalds 	if (ret < 0)
37521da177e4SLinus Torvalds 		goto re_enumerate;
37531da177e4SLinus Torvalds 
37541da177e4SLinus Torvalds 	/* Device might have changed firmware (DFU or similar) */
3755eb764c4bSAlan Stern 	if (descriptors_changed(udev, &descriptor)) {
37561da177e4SLinus Torvalds 		dev_info(&udev->dev, "device firmware changed\n");
37571da177e4SLinus Torvalds 		udev->descriptor = descriptor;	/* for disconnect() calls */
37581da177e4SLinus Torvalds 		goto re_enumerate;
37591da177e4SLinus Torvalds   	}
37601da177e4SLinus Torvalds 
37614fe0387aSAlan Stern 	/* Restore the device's previous configuration */
37621da177e4SLinus Torvalds 	if (!udev->actconfig)
37631da177e4SLinus Torvalds 		goto done;
37643f0479e0SSarah Sharp 
3765d673bfcbSSarah Sharp 	mutex_lock(hcd->bandwidth_mutex);
37663f0479e0SSarah Sharp 	ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
37673f0479e0SSarah Sharp 	if (ret < 0) {
37683f0479e0SSarah Sharp 		dev_warn(&udev->dev,
37693f0479e0SSarah Sharp 				"Busted HC?  Not enough HCD resources for "
37703f0479e0SSarah Sharp 				"old configuration.\n");
3771d673bfcbSSarah Sharp 		mutex_unlock(hcd->bandwidth_mutex);
37723f0479e0SSarah Sharp 		goto re_enumerate;
37733f0479e0SSarah Sharp 	}
37741da177e4SLinus Torvalds 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
37751da177e4SLinus Torvalds 			USB_REQ_SET_CONFIGURATION, 0,
37761da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, 0,
37771da177e4SLinus Torvalds 			NULL, 0, USB_CTRL_SET_TIMEOUT);
37781da177e4SLinus Torvalds 	if (ret < 0) {
37791da177e4SLinus Torvalds 		dev_err(&udev->dev,
37801da177e4SLinus Torvalds 			"can't restore configuration #%d (error=%d)\n",
37811da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, ret);
3782d673bfcbSSarah Sharp 		mutex_unlock(hcd->bandwidth_mutex);
37831da177e4SLinus Torvalds 		goto re_enumerate;
37841da177e4SLinus Torvalds   	}
3785d673bfcbSSarah Sharp 	mutex_unlock(hcd->bandwidth_mutex);
37861da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_CONFIGURED);
37871da177e4SLinus Torvalds 
37884fe0387aSAlan Stern 	/* Put interfaces back into the same altsettings as before.
37894fe0387aSAlan Stern 	 * Don't bother to send the Set-Interface request for interfaces
37904fe0387aSAlan Stern 	 * that were already in altsetting 0; besides being unnecessary,
37914fe0387aSAlan Stern 	 * many devices can't handle it.  Instead just reset the host-side
37924fe0387aSAlan Stern 	 * endpoint state.
37934fe0387aSAlan Stern 	 */
37941da177e4SLinus Torvalds 	for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
37953f0479e0SSarah Sharp 		struct usb_host_config *config = udev->actconfig;
37963f0479e0SSarah Sharp 		struct usb_interface *intf = config->interface[i];
37971da177e4SLinus Torvalds 		struct usb_interface_descriptor *desc;
37981da177e4SLinus Torvalds 
37991da177e4SLinus Torvalds 		desc = &intf->cur_altsetting->desc;
38004fe0387aSAlan Stern 		if (desc->bAlternateSetting == 0) {
38014fe0387aSAlan Stern 			usb_disable_interface(udev, intf, true);
38024fe0387aSAlan Stern 			usb_enable_interface(udev, intf, true);
38034fe0387aSAlan Stern 			ret = 0;
38044fe0387aSAlan Stern 		} else {
380504a723eaSSarah Sharp 			/* Let the bandwidth allocation function know that this
380604a723eaSSarah Sharp 			 * device has been reset, and it will have to use
380704a723eaSSarah Sharp 			 * alternate setting 0 as the current alternate setting.
38083f0479e0SSarah Sharp 			 */
380904a723eaSSarah Sharp 			intf->resetting_device = 1;
38101da177e4SLinus Torvalds 			ret = usb_set_interface(udev, desc->bInterfaceNumber,
38111da177e4SLinus Torvalds 					desc->bAlternateSetting);
381204a723eaSSarah Sharp 			intf->resetting_device = 0;
38134fe0387aSAlan Stern 		}
38141da177e4SLinus Torvalds 		if (ret < 0) {
38151da177e4SLinus Torvalds 			dev_err(&udev->dev, "failed to restore interface %d "
38161da177e4SLinus Torvalds 				"altsetting %d (error=%d)\n",
38171da177e4SLinus Torvalds 				desc->bInterfaceNumber,
38181da177e4SLinus Torvalds 				desc->bAlternateSetting,
38191da177e4SLinus Torvalds 				ret);
38201da177e4SLinus Torvalds 			goto re_enumerate;
38211da177e4SLinus Torvalds 		}
38221da177e4SLinus Torvalds 	}
38231da177e4SLinus Torvalds 
38241da177e4SLinus Torvalds done:
38251da177e4SLinus Torvalds 	return 0;
38261da177e4SLinus Torvalds 
38271da177e4SLinus Torvalds re_enumerate:
38281da177e4SLinus Torvalds 	hub_port_logical_disconnect(parent_hub, port1);
38291da177e4SLinus Torvalds 	return -ENODEV;
38301da177e4SLinus Torvalds }
383179efa097SAlan Stern 
383279efa097SAlan Stern /**
3833742120c6SMing Lei  * usb_reset_device - warn interface drivers and perform a USB port reset
383479efa097SAlan Stern  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
383579efa097SAlan Stern  *
383679efa097SAlan Stern  * Warns all drivers bound to registered interfaces (using their pre_reset
383779efa097SAlan Stern  * method), performs the port reset, and then lets the drivers know that
383879efa097SAlan Stern  * the reset is over (using their post_reset method).
383979efa097SAlan Stern  *
3840742120c6SMing Lei  * Return value is the same as for usb_reset_and_verify_device().
384179efa097SAlan Stern  *
384279efa097SAlan Stern  * The caller must own the device lock.  For example, it's safe to use
384379efa097SAlan Stern  * this from a driver probe() routine after downloading new firmware.
384479efa097SAlan Stern  * For calls that might not occur during probe(), drivers should lock
384579efa097SAlan Stern  * the device using usb_lock_device_for_reset().
384678d9a487SAlan Stern  *
384778d9a487SAlan Stern  * If an interface is currently being probed or disconnected, we assume
384878d9a487SAlan Stern  * its driver knows how to handle resets.  For all other interfaces,
384978d9a487SAlan Stern  * if the driver doesn't have pre_reset and post_reset methods then
385078d9a487SAlan Stern  * we attempt to unbind it and rebind afterward.
385179efa097SAlan Stern  */
3852742120c6SMing Lei int usb_reset_device(struct usb_device *udev)
385379efa097SAlan Stern {
385479efa097SAlan Stern 	int ret;
3855852c4b43SAlan Stern 	int i;
385679efa097SAlan Stern 	struct usb_host_config *config = udev->actconfig;
385779efa097SAlan Stern 
385879efa097SAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED ||
385979efa097SAlan Stern 			udev->state == USB_STATE_SUSPENDED) {
386079efa097SAlan Stern 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
386179efa097SAlan Stern 				udev->state);
386279efa097SAlan Stern 		return -EINVAL;
386379efa097SAlan Stern 	}
386479efa097SAlan Stern 
3865645daaabSAlan Stern 	/* Prevent autosuspend during the reset */
386694fcda1fSAlan Stern 	usb_autoresume_device(udev);
3867645daaabSAlan Stern 
386879efa097SAlan Stern 	if (config) {
3869852c4b43SAlan Stern 		for (i = 0; i < config->desc.bNumInterfaces; ++i) {
3870852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
387179efa097SAlan Stern 			struct usb_driver *drv;
387278d9a487SAlan Stern 			int unbind = 0;
387379efa097SAlan Stern 
3874852c4b43SAlan Stern 			if (cintf->dev.driver) {
387579efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
387678d9a487SAlan Stern 				if (drv->pre_reset && drv->post_reset)
387778d9a487SAlan Stern 					unbind = (drv->pre_reset)(cintf);
387878d9a487SAlan Stern 				else if (cintf->condition ==
387978d9a487SAlan Stern 						USB_INTERFACE_BOUND)
388078d9a487SAlan Stern 					unbind = 1;
388178d9a487SAlan Stern 				if (unbind)
388278d9a487SAlan Stern 					usb_forced_unbind_intf(cintf);
388379efa097SAlan Stern 			}
388479efa097SAlan Stern 		}
388579efa097SAlan Stern 	}
388679efa097SAlan Stern 
3887742120c6SMing Lei 	ret = usb_reset_and_verify_device(udev);
388879efa097SAlan Stern 
388979efa097SAlan Stern 	if (config) {
3890852c4b43SAlan Stern 		for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
3891852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
389279efa097SAlan Stern 			struct usb_driver *drv;
389378d9a487SAlan Stern 			int rebind = cintf->needs_binding;
389479efa097SAlan Stern 
389578d9a487SAlan Stern 			if (!rebind && cintf->dev.driver) {
389679efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
389779efa097SAlan Stern 				if (drv->post_reset)
389878d9a487SAlan Stern 					rebind = (drv->post_reset)(cintf);
389978d9a487SAlan Stern 				else if (cintf->condition ==
390078d9a487SAlan Stern 						USB_INTERFACE_BOUND)
390178d9a487SAlan Stern 					rebind = 1;
390279efa097SAlan Stern 			}
39036c640945SAlan Stern 			if (ret == 0 && rebind)
390478d9a487SAlan Stern 				usb_rebind_intf(cintf);
390579efa097SAlan Stern 		}
390679efa097SAlan Stern 	}
390779efa097SAlan Stern 
390894fcda1fSAlan Stern 	usb_autosuspend_device(udev);
390979efa097SAlan Stern 	return ret;
391079efa097SAlan Stern }
3911742120c6SMing Lei EXPORT_SYMBOL_GPL(usb_reset_device);
3912dc023dceSInaky Perez-Gonzalez 
3913dc023dceSInaky Perez-Gonzalez 
3914dc023dceSInaky Perez-Gonzalez /**
3915dc023dceSInaky Perez-Gonzalez  * usb_queue_reset_device - Reset a USB device from an atomic context
3916dc023dceSInaky Perez-Gonzalez  * @iface: USB interface belonging to the device to reset
3917dc023dceSInaky Perez-Gonzalez  *
3918dc023dceSInaky Perez-Gonzalez  * This function can be used to reset a USB device from an atomic
3919dc023dceSInaky Perez-Gonzalez  * context, where usb_reset_device() won't work (as it blocks).
3920dc023dceSInaky Perez-Gonzalez  *
3921dc023dceSInaky Perez-Gonzalez  * Doing a reset via this method is functionally equivalent to calling
3922dc023dceSInaky Perez-Gonzalez  * usb_reset_device(), except for the fact that it is delayed to a
3923dc023dceSInaky Perez-Gonzalez  * workqueue. This means that any drivers bound to other interfaces
3924dc023dceSInaky Perez-Gonzalez  * might be unbound, as well as users from usbfs in user space.
3925dc023dceSInaky Perez-Gonzalez  *
3926dc023dceSInaky Perez-Gonzalez  * Corner cases:
3927dc023dceSInaky Perez-Gonzalez  *
3928dc023dceSInaky Perez-Gonzalez  * - Scheduling two resets at the same time from two different drivers
3929dc023dceSInaky Perez-Gonzalez  *   attached to two different interfaces of the same device is
3930dc023dceSInaky Perez-Gonzalez  *   possible; depending on how the driver attached to each interface
3931dc023dceSInaky Perez-Gonzalez  *   handles ->pre_reset(), the second reset might happen or not.
3932dc023dceSInaky Perez-Gonzalez  *
3933dc023dceSInaky Perez-Gonzalez  * - If a driver is unbound and it had a pending reset, the reset will
3934dc023dceSInaky Perez-Gonzalez  *   be cancelled.
3935dc023dceSInaky Perez-Gonzalez  *
3936dc023dceSInaky Perez-Gonzalez  * - This function can be called during .probe() or .disconnect()
3937dc023dceSInaky Perez-Gonzalez  *   times. On return from .disconnect(), any pending resets will be
3938dc023dceSInaky Perez-Gonzalez  *   cancelled.
3939dc023dceSInaky Perez-Gonzalez  *
3940dc023dceSInaky Perez-Gonzalez  * There is no no need to lock/unlock the @reset_ws as schedule_work()
3941dc023dceSInaky Perez-Gonzalez  * does its own.
3942dc023dceSInaky Perez-Gonzalez  *
3943dc023dceSInaky Perez-Gonzalez  * NOTE: We don't do any reference count tracking because it is not
3944dc023dceSInaky Perez-Gonzalez  *     needed. The lifecycle of the work_struct is tied to the
3945dc023dceSInaky Perez-Gonzalez  *     usb_interface. Before destroying the interface we cancel the
3946dc023dceSInaky Perez-Gonzalez  *     work_struct, so the fact that work_struct is queued and or
3947dc023dceSInaky Perez-Gonzalez  *     running means the interface (and thus, the device) exist and
3948dc023dceSInaky Perez-Gonzalez  *     are referenced.
3949dc023dceSInaky Perez-Gonzalez  */
3950dc023dceSInaky Perez-Gonzalez void usb_queue_reset_device(struct usb_interface *iface)
3951dc023dceSInaky Perez-Gonzalez {
3952dc023dceSInaky Perez-Gonzalez 	schedule_work(&iface->reset_ws);
3953dc023dceSInaky Perez-Gonzalez }
3954dc023dceSInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_queue_reset_device);
3955