xref: /openbmc/linux/drivers/usb/core/hub.c (revision b356b7c7)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * USB hub driver.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * (C) Copyright 1999 Linus Torvalds
51da177e4SLinus Torvalds  * (C) Copyright 1999 Johannes Erdfelt
61da177e4SLinus Torvalds  * (C) Copyright 1999 Gregory P. Smith
71da177e4SLinus Torvalds  * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/errno.h>
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/moduleparam.h>
151da177e4SLinus Torvalds #include <linux/completion.h>
161da177e4SLinus Torvalds #include <linux/sched.h>
171da177e4SLinus Torvalds #include <linux/list.h>
181da177e4SLinus Torvalds #include <linux/slab.h>
191da177e4SLinus Torvalds #include <linux/ioctl.h>
201da177e4SLinus Torvalds #include <linux/usb.h>
211da177e4SLinus Torvalds #include <linux/usbdevice_fs.h>
229c8d6178Sakpm@osdl.org #include <linux/kthread.h>
234186ecf8SArjan van de Ven #include <linux/mutex.h>
247dfb7103SNigel Cunningham #include <linux/freezer.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include <asm/uaccess.h>
271da177e4SLinus Torvalds #include <asm/byteorder.h>
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include "usb.h"
301da177e4SLinus Torvalds #include "hcd.h"
311da177e4SLinus Torvalds #include "hub.h"
321da177e4SLinus Torvalds 
33f2a383e4SGreg Kroah-Hartman /* if we are in debug mode, always announce new devices */
34f2a383e4SGreg Kroah-Hartman #ifdef DEBUG
35f2a383e4SGreg Kroah-Hartman #ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
36f2a383e4SGreg Kroah-Hartman #define CONFIG_USB_ANNOUNCE_NEW_DEVICES
37f2a383e4SGreg Kroah-Hartman #endif
38f2a383e4SGreg Kroah-Hartman #endif
39f2a383e4SGreg Kroah-Hartman 
401bb5f66bSAlan Stern struct usb_hub {
411bb5f66bSAlan Stern 	struct device		*intfdev;	/* the "interface" device */
421bb5f66bSAlan Stern 	struct usb_device	*hdev;
43e8054854SAlan Stern 	struct kref		kref;
441bb5f66bSAlan Stern 	struct urb		*urb;		/* for interrupt polling pipe */
451bb5f66bSAlan Stern 
461bb5f66bSAlan Stern 	/* buffer for urb ... with extra space in case of babble */
471bb5f66bSAlan Stern 	char			(*buffer)[8];
481bb5f66bSAlan Stern 	dma_addr_t		buffer_dma;	/* DMA address for buffer */
491bb5f66bSAlan Stern 	union {
501bb5f66bSAlan Stern 		struct usb_hub_status	hub;
511bb5f66bSAlan Stern 		struct usb_port_status	port;
521bb5f66bSAlan Stern 	}			*status;	/* buffer for status reports */
53db90e7a1SAlan Stern 	struct mutex		status_mutex;	/* for the status buffer */
541bb5f66bSAlan Stern 
551bb5f66bSAlan Stern 	int			error;		/* last reported error */
561bb5f66bSAlan Stern 	int			nerrors;	/* track consecutive errors */
571bb5f66bSAlan Stern 
581bb5f66bSAlan Stern 	struct list_head	event_list;	/* hubs w/data or errs ready */
591bb5f66bSAlan Stern 	unsigned long		event_bits[1];	/* status change bitmask */
601bb5f66bSAlan Stern 	unsigned long		change_bits[1];	/* ports with logical connect
611bb5f66bSAlan Stern 							status change */
621bb5f66bSAlan Stern 	unsigned long		busy_bits[1];	/* ports being reset or
631bb5f66bSAlan Stern 							resumed */
641bb5f66bSAlan Stern #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
651bb5f66bSAlan Stern #error event_bits[] is too short!
661bb5f66bSAlan Stern #endif
671bb5f66bSAlan Stern 
681bb5f66bSAlan Stern 	struct usb_hub_descriptor *descriptor;	/* class descriptor */
691bb5f66bSAlan Stern 	struct usb_tt		tt;		/* Transaction Translator */
701bb5f66bSAlan Stern 
711bb5f66bSAlan Stern 	unsigned		mA_per_port;	/* current for each child */
721bb5f66bSAlan Stern 
731bb5f66bSAlan Stern 	unsigned		limited_power:1;
741bb5f66bSAlan Stern 	unsigned		quiescing:1;
75e8054854SAlan Stern 	unsigned		disconnected:1;
761bb5f66bSAlan Stern 
771bb5f66bSAlan Stern 	unsigned		has_indicators:1;
781bb5f66bSAlan Stern 	u8			indicator[USB_MAXCHILDREN];
796d5aefb8SDavid Howells 	struct delayed_work	leds;
808520f380SAlan Stern 	struct delayed_work	init_work;
817cbe5dcaSAlan Stern 	void			**port_owners;
821bb5f66bSAlan Stern };
831bb5f66bSAlan Stern 
841bb5f66bSAlan Stern 
851da177e4SLinus Torvalds /* Protect struct usb_device->state and ->children members
869ad3d6ccSAlan Stern  * Note: Both are also protected by ->dev.sem, except that ->state can
871da177e4SLinus Torvalds  * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
881da177e4SLinus Torvalds static DEFINE_SPINLOCK(device_state_lock);
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* khubd's worklist and its lock */
911da177e4SLinus Torvalds static DEFINE_SPINLOCK(hub_event_lock);
921da177e4SLinus Torvalds static LIST_HEAD(hub_event_list);	/* List of hubs needing servicing */
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* Wakes up khubd */
951da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
961da177e4SLinus Torvalds 
979c8d6178Sakpm@osdl.org static struct task_struct *khubd_task;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds /* cycle leds on hubs that aren't blinking for attention */
1001da177e4SLinus Torvalds static int blinkenlights = 0;
1011da177e4SLinus Torvalds module_param (blinkenlights, bool, S_IRUGO);
1021da177e4SLinus Torvalds MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds /*
105fd7c519dSJaroslav Kysela  * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
106fd7c519dSJaroslav Kysela  * 10 seconds to send reply for the initial 64-byte descriptor request.
107fd7c519dSJaroslav Kysela  */
108fd7c519dSJaroslav Kysela /* define initial 64-byte descriptor request timeout in milliseconds */
109fd7c519dSJaroslav Kysela static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
110fd7c519dSJaroslav Kysela module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
111b9cef6c3SWu Fengguang MODULE_PARM_DESC(initial_descriptor_timeout,
112b9cef6c3SWu Fengguang 		"initial 64-byte descriptor request timeout in milliseconds "
113b9cef6c3SWu Fengguang 		"(default 5000 - 5.0 seconds)");
114fd7c519dSJaroslav Kysela 
115fd7c519dSJaroslav Kysela /*
1161da177e4SLinus Torvalds  * As of 2.6.10 we introduce a new USB device initialization scheme which
1171da177e4SLinus Torvalds  * closely resembles the way Windows works.  Hopefully it will be compatible
1181da177e4SLinus Torvalds  * with a wider range of devices than the old scheme.  However some previously
1191da177e4SLinus Torvalds  * working devices may start giving rise to "device not accepting address"
1201da177e4SLinus Torvalds  * errors; if that happens the user can try the old scheme by adjusting the
1211da177e4SLinus Torvalds  * following module parameters.
1221da177e4SLinus Torvalds  *
1231da177e4SLinus Torvalds  * For maximum flexibility there are two boolean parameters to control the
1241da177e4SLinus Torvalds  * hub driver's behavior.  On the first initialization attempt, if the
1251da177e4SLinus Torvalds  * "old_scheme_first" parameter is set then the old scheme will be used,
1261da177e4SLinus Torvalds  * otherwise the new scheme is used.  If that fails and "use_both_schemes"
1271da177e4SLinus Torvalds  * is set, then the driver will make another attempt, using the other scheme.
1281da177e4SLinus Torvalds  */
1291da177e4SLinus Torvalds static int old_scheme_first = 0;
1301da177e4SLinus Torvalds module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
1311da177e4SLinus Torvalds MODULE_PARM_DESC(old_scheme_first,
1321da177e4SLinus Torvalds 		 "start with the old device initialization scheme");
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds static int use_both_schemes = 1;
1351da177e4SLinus Torvalds module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
1361da177e4SLinus Torvalds MODULE_PARM_DESC(use_both_schemes,
1371da177e4SLinus Torvalds 		"try the other device initialization scheme if the "
1381da177e4SLinus Torvalds 		"first one fails");
1391da177e4SLinus Torvalds 
14032fe0198SAlan Stern /* Mutual exclusion for EHCI CF initialization.  This interferes with
14132fe0198SAlan Stern  * port reset on some companion controllers.
14232fe0198SAlan Stern  */
14332fe0198SAlan Stern DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
14432fe0198SAlan Stern EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
14532fe0198SAlan Stern 
146948fea37SAlan Stern #define HUB_DEBOUNCE_TIMEOUT	1500
147948fea37SAlan Stern #define HUB_DEBOUNCE_STEP	  25
148948fea37SAlan Stern #define HUB_DEBOUNCE_STABLE	 100
149948fea37SAlan Stern 
1501da177e4SLinus Torvalds 
151742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev);
152742120c6SMing Lei 
1531da177e4SLinus Torvalds static inline char *portspeed(int portstatus)
1541da177e4SLinus Torvalds {
1551da177e4SLinus Torvalds 	if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
1561da177e4SLinus Torvalds     		return "480 Mb/s";
1571da177e4SLinus Torvalds 	else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
1581da177e4SLinus Torvalds 		return "1.5 Mb/s";
159d2e9b4d6SSarah Sharp 	else if (portstatus & (1 << USB_PORT_FEAT_SUPERSPEED))
160d2e9b4d6SSarah Sharp 		return "5.0 Gb/s";
1611da177e4SLinus Torvalds 	else
1621da177e4SLinus Torvalds 		return "12 Mb/s";
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds /* Note that hdev or one of its children must be locked! */
16625118084SAlan Stern static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
1671da177e4SLinus Torvalds {
16825118084SAlan Stern 	if (!hdev || !hdev->actconfig)
16925118084SAlan Stern 		return NULL;
1701da177e4SLinus Torvalds 	return usb_get_intfdata(hdev->actconfig->interface[0]);
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.4.5 */
1741da177e4SLinus Torvalds static int get_hub_descriptor(struct usb_device *hdev, void *data, int size)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	int i, ret;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	for (i = 0; i < 3; i++) {
1791da177e4SLinus Torvalds 		ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
1801da177e4SLinus Torvalds 			USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
1811da177e4SLinus Torvalds 			USB_DT_HUB << 8, 0, data, size,
1821da177e4SLinus Torvalds 			USB_CTRL_GET_TIMEOUT);
1831da177e4SLinus Torvalds 		if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
1841da177e4SLinus Torvalds 			return ret;
1851da177e4SLinus Torvalds 	}
1861da177e4SLinus Torvalds 	return -EINVAL;
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds /*
1901da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.1
1911da177e4SLinus Torvalds  */
1921da177e4SLinus Torvalds static int clear_hub_feature(struct usb_device *hdev, int feature)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1951da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds /*
1991da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.2
2001da177e4SLinus Torvalds  */
2011da177e4SLinus Torvalds static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
2041da177e4SLinus Torvalds 		USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
2051da177e4SLinus Torvalds 		NULL, 0, 1000);
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds /*
2091da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.13
2101da177e4SLinus Torvalds  */
2111da177e4SLinus Torvalds static int set_port_feature(struct usb_device *hdev, int port1, int feature)
2121da177e4SLinus Torvalds {
2131da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
2141da177e4SLinus Torvalds 		USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
2151da177e4SLinus Torvalds 		NULL, 0, 1000);
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds /*
2191da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
2201da177e4SLinus Torvalds  * for info about using port indicators
2211da177e4SLinus Torvalds  */
2221da177e4SLinus Torvalds static void set_port_led(
2231da177e4SLinus Torvalds 	struct usb_hub *hub,
2241da177e4SLinus Torvalds 	int port1,
2251da177e4SLinus Torvalds 	int selector
2261da177e4SLinus Torvalds )
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	int status = set_port_feature(hub->hdev, (selector << 8) | port1,
2291da177e4SLinus Torvalds 			USB_PORT_FEAT_INDICATOR);
2301da177e4SLinus Torvalds 	if (status < 0)
2311da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
2321da177e4SLinus Torvalds 			"port %d indicator %s status %d\n",
2331da177e4SLinus Torvalds 			port1,
2341da177e4SLinus Torvalds 			({ char *s; switch (selector) {
2351da177e4SLinus Torvalds 			case HUB_LED_AMBER: s = "amber"; break;
2361da177e4SLinus Torvalds 			case HUB_LED_GREEN: s = "green"; break;
2371da177e4SLinus Torvalds 			case HUB_LED_OFF: s = "off"; break;
2381da177e4SLinus Torvalds 			case HUB_LED_AUTO: s = "auto"; break;
2391da177e4SLinus Torvalds 			default: s = "??"; break;
2401da177e4SLinus Torvalds 			}; s; }),
2411da177e4SLinus Torvalds 			status);
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds #define	LED_CYCLE_PERIOD	((2*HZ)/3)
2451da177e4SLinus Torvalds 
246c4028958SDavid Howells static void led_work (struct work_struct *work)
2471da177e4SLinus Torvalds {
248c4028958SDavid Howells 	struct usb_hub		*hub =
249c4028958SDavid Howells 		container_of(work, struct usb_hub, leds.work);
2501da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
2511da177e4SLinus Torvalds 	unsigned		i;
2521da177e4SLinus Torvalds 	unsigned		changed = 0;
2531da177e4SLinus Torvalds 	int			cursor = -1;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
2561da177e4SLinus Torvalds 		return;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
2591da177e4SLinus Torvalds 		unsigned	selector, mode;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 		/* 30%-50% duty cycle */
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 		switch (hub->indicator[i]) {
2641da177e4SLinus Torvalds 		/* cycle marker */
2651da177e4SLinus Torvalds 		case INDICATOR_CYCLE:
2661da177e4SLinus Torvalds 			cursor = i;
2671da177e4SLinus Torvalds 			selector = HUB_LED_AUTO;
2681da177e4SLinus Torvalds 			mode = INDICATOR_AUTO;
2691da177e4SLinus Torvalds 			break;
2701da177e4SLinus Torvalds 		/* blinking green = sw attention */
2711da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK:
2721da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
2731da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK_OFF;
2741da177e4SLinus Torvalds 			break;
2751da177e4SLinus Torvalds 		case INDICATOR_GREEN_BLINK_OFF:
2761da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
2771da177e4SLinus Torvalds 			mode = INDICATOR_GREEN_BLINK;
2781da177e4SLinus Torvalds 			break;
2791da177e4SLinus Torvalds 		/* blinking amber = hw attention */
2801da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK:
2811da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
2821da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK_OFF;
2831da177e4SLinus Torvalds 			break;
2841da177e4SLinus Torvalds 		case INDICATOR_AMBER_BLINK_OFF:
2851da177e4SLinus Torvalds 			selector = HUB_LED_OFF;
2861da177e4SLinus Torvalds 			mode = INDICATOR_AMBER_BLINK;
2871da177e4SLinus Torvalds 			break;
2881da177e4SLinus Torvalds 		/* blink green/amber = reserved */
2891da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK:
2901da177e4SLinus Torvalds 			selector = HUB_LED_GREEN;
2911da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK_OFF;
2921da177e4SLinus Torvalds 			break;
2931da177e4SLinus Torvalds 		case INDICATOR_ALT_BLINK_OFF:
2941da177e4SLinus Torvalds 			selector = HUB_LED_AMBER;
2951da177e4SLinus Torvalds 			mode = INDICATOR_ALT_BLINK;
2961da177e4SLinus Torvalds 			break;
2971da177e4SLinus Torvalds 		default:
2981da177e4SLinus Torvalds 			continue;
2991da177e4SLinus Torvalds 		}
3001da177e4SLinus Torvalds 		if (selector != HUB_LED_AUTO)
3011da177e4SLinus Torvalds 			changed = 1;
3021da177e4SLinus Torvalds 		set_port_led(hub, i + 1, selector);
3031da177e4SLinus Torvalds 		hub->indicator[i] = mode;
3041da177e4SLinus Torvalds 	}
3051da177e4SLinus Torvalds 	if (!changed && blinkenlights) {
3061da177e4SLinus Torvalds 		cursor++;
3071da177e4SLinus Torvalds 		cursor %= hub->descriptor->bNbrPorts;
3081da177e4SLinus Torvalds 		set_port_led(hub, cursor + 1, HUB_LED_GREEN);
3091da177e4SLinus Torvalds 		hub->indicator[cursor] = INDICATOR_CYCLE;
3101da177e4SLinus Torvalds 		changed++;
3111da177e4SLinus Torvalds 	}
3121da177e4SLinus Torvalds 	if (changed)
3131da177e4SLinus Torvalds 		schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds /* use a short timeout for hub/port status fetches */
3171da177e4SLinus Torvalds #define	USB_STS_TIMEOUT		1000
3181da177e4SLinus Torvalds #define	USB_STS_RETRIES		5
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds /*
3211da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.6
3221da177e4SLinus Torvalds  */
3231da177e4SLinus Torvalds static int get_hub_status(struct usb_device *hdev,
3241da177e4SLinus Torvalds 		struct usb_hub_status *data)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
3291da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
3301da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
3311da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
3321da177e4SLinus Torvalds 	}
3331da177e4SLinus Torvalds 	return status;
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds /*
3371da177e4SLinus Torvalds  * USB 2.0 spec Section 11.24.2.7
3381da177e4SLinus Torvalds  */
3391da177e4SLinus Torvalds static int get_port_status(struct usb_device *hdev, int port1,
3401da177e4SLinus Torvalds 		struct usb_port_status *data)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	int i, status = -ETIMEDOUT;
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
3451da177e4SLinus Torvalds 		status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
3461da177e4SLinus Torvalds 			USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
3471da177e4SLinus Torvalds 			data, sizeof(*data), USB_STS_TIMEOUT);
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 	return status;
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
3523eb14915SAlan Stern static int hub_port_status(struct usb_hub *hub, int port1,
3533eb14915SAlan Stern 		u16 *status, u16 *change)
3543eb14915SAlan Stern {
3553eb14915SAlan Stern 	int ret;
3563eb14915SAlan Stern 
3573eb14915SAlan Stern 	mutex_lock(&hub->status_mutex);
3583eb14915SAlan Stern 	ret = get_port_status(hub->hdev, port1, &hub->status->port);
3593eb14915SAlan Stern 	if (ret < 4) {
3603eb14915SAlan Stern 		dev_err(hub->intfdev,
3613eb14915SAlan Stern 			"%s failed (err = %d)\n", __func__, ret);
3623eb14915SAlan Stern 		if (ret >= 0)
3633eb14915SAlan Stern 			ret = -EIO;
3643eb14915SAlan Stern 	} else {
3653eb14915SAlan Stern 		*status = le16_to_cpu(hub->status->port.wPortStatus);
3663eb14915SAlan Stern 		*change = le16_to_cpu(hub->status->port.wPortChange);
3673eb14915SAlan Stern 		ret = 0;
3683eb14915SAlan Stern 	}
3693eb14915SAlan Stern 	mutex_unlock(&hub->status_mutex);
3703eb14915SAlan Stern 	return ret;
3713eb14915SAlan Stern }
3723eb14915SAlan Stern 
3731da177e4SLinus Torvalds static void kick_khubd(struct usb_hub *hub)
3741da177e4SLinus Torvalds {
3751da177e4SLinus Torvalds 	unsigned long	flags;
3761da177e4SLinus Torvalds 
37740f122f3SAlan Stern 	/* Suppress autosuspend until khubd runs */
378ccf5b801SAlan Stern 	atomic_set(&to_usb_interface(hub->intfdev)->pm_usage_cnt, 1);
37940f122f3SAlan Stern 
3801da177e4SLinus Torvalds 	spin_lock_irqsave(&hub_event_lock, flags);
3812e2c5eeaSRoel Kluin 	if (!hub->disconnected && list_empty(&hub->event_list)) {
3821da177e4SLinus Torvalds 		list_add_tail(&hub->event_list, &hub_event_list);
3831da177e4SLinus Torvalds 		wake_up(&khubd_wait);
3841da177e4SLinus Torvalds 	}
3851da177e4SLinus Torvalds 	spin_unlock_irqrestore(&hub_event_lock, flags);
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds void usb_kick_khubd(struct usb_device *hdev)
3891da177e4SLinus Torvalds {
39025118084SAlan Stern 	struct usb_hub *hub = hdev_to_hub(hdev);
39125118084SAlan Stern 
39225118084SAlan Stern 	if (hub)
39325118084SAlan Stern 		kick_khubd(hub);
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds /* completion function, fires on port status changes and various faults */
3987d12e780SDavid Howells static void hub_irq(struct urb *urb)
3991da177e4SLinus Torvalds {
400ec17cf1cSTobias Klauser 	struct usb_hub *hub = urb->context;
401e015268dSAlan Stern 	int status = urb->status;
40271d2718fSRoel Kluin 	unsigned i;
4031da177e4SLinus Torvalds 	unsigned long bits;
4041da177e4SLinus Torvalds 
405e015268dSAlan Stern 	switch (status) {
4061da177e4SLinus Torvalds 	case -ENOENT:		/* synchronous unlink */
4071da177e4SLinus Torvalds 	case -ECONNRESET:	/* async unlink */
4081da177e4SLinus Torvalds 	case -ESHUTDOWN:	/* hardware going away */
4091da177e4SLinus Torvalds 		return;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	default:		/* presumably an error */
4121da177e4SLinus Torvalds 		/* Cause a hub reset after 10 consecutive errors */
413e015268dSAlan Stern 		dev_dbg (hub->intfdev, "transfer --> %d\n", status);
4141da177e4SLinus Torvalds 		if ((++hub->nerrors < 10) || hub->error)
4151da177e4SLinus Torvalds 			goto resubmit;
416e015268dSAlan Stern 		hub->error = status;
4171da177e4SLinus Torvalds 		/* FALL THROUGH */
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	/* let khubd handle things */
4201da177e4SLinus Torvalds 	case 0:			/* we got data:  port status changed */
4211da177e4SLinus Torvalds 		bits = 0;
4221da177e4SLinus Torvalds 		for (i = 0; i < urb->actual_length; ++i)
4231da177e4SLinus Torvalds 			bits |= ((unsigned long) ((*hub->buffer)[i]))
4241da177e4SLinus Torvalds 					<< (i*8);
4251da177e4SLinus Torvalds 		hub->event_bits[0] = bits;
4261da177e4SLinus Torvalds 		break;
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	hub->nerrors = 0;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	/* Something happened, let khubd figure it out */
4321da177e4SLinus Torvalds 	kick_khubd(hub);
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds resubmit:
4351da177e4SLinus Torvalds 	if (hub->quiescing)
4361da177e4SLinus Torvalds 		return;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
4391da177e4SLinus Torvalds 			&& status != -ENODEV && status != -EPERM)
4401da177e4SLinus Torvalds 		dev_err (hub->intfdev, "resubmit --> %d\n", status);
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds /* USB 2.0 spec Section 11.24.2.3 */
4441da177e4SLinus Torvalds static inline int
4451da177e4SLinus Torvalds hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
4461da177e4SLinus Torvalds {
4471da177e4SLinus Torvalds 	return usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
4481da177e4SLinus Torvalds 			       HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
4491da177e4SLinus Torvalds 			       tt, NULL, 0, 1000);
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds /*
4531da177e4SLinus Torvalds  * enumeration blocks khubd for a long time. we use keventd instead, since
4541da177e4SLinus Torvalds  * long blocking there is the exception, not the rule.  accordingly, HCDs
4551da177e4SLinus Torvalds  * talking to TTs must queue control transfers (not just bulk and iso), so
4561da177e4SLinus Torvalds  * both can talk to the same hub concurrently.
4571da177e4SLinus Torvalds  */
458cb88a1b8SAlan Stern static void hub_tt_work(struct work_struct *work)
4591da177e4SLinus Torvalds {
460c4028958SDavid Howells 	struct usb_hub		*hub =
461cb88a1b8SAlan Stern 		container_of(work, struct usb_hub, tt.clear_work);
4621da177e4SLinus Torvalds 	unsigned long		flags;
46355e5fdfaSMark Lord 	int			limit = 100;
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	spin_lock_irqsave (&hub->tt.lock, flags);
46655e5fdfaSMark Lord 	while (--limit && !list_empty (&hub->tt.clear_list)) {
467d0f830d3SH Hartley Sweeten 		struct list_head	*next;
4681da177e4SLinus Torvalds 		struct usb_tt_clear	*clear;
4691da177e4SLinus Torvalds 		struct usb_device	*hdev = hub->hdev;
470cb88a1b8SAlan Stern 		const struct hc_driver	*drv;
4711da177e4SLinus Torvalds 		int			status;
4721da177e4SLinus Torvalds 
473d0f830d3SH Hartley Sweeten 		next = hub->tt.clear_list.next;
474d0f830d3SH Hartley Sweeten 		clear = list_entry (next, struct usb_tt_clear, clear_list);
4751da177e4SLinus Torvalds 		list_del (&clear->clear_list);
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 		/* drop lock so HCD can concurrently report other TT errors */
4781da177e4SLinus Torvalds 		spin_unlock_irqrestore (&hub->tt.lock, flags);
4791da177e4SLinus Torvalds 		status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
4801da177e4SLinus Torvalds 		if (status)
4811da177e4SLinus Torvalds 			dev_err (&hdev->dev,
4821da177e4SLinus Torvalds 				"clear tt %d (%04x) error %d\n",
4831da177e4SLinus Torvalds 				clear->tt, clear->devinfo, status);
484cb88a1b8SAlan Stern 
485cb88a1b8SAlan Stern 		/* Tell the HCD, even if the operation failed */
486cb88a1b8SAlan Stern 		drv = clear->hcd->driver;
487cb88a1b8SAlan Stern 		if (drv->clear_tt_buffer_complete)
488cb88a1b8SAlan Stern 			(drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
489cb88a1b8SAlan Stern 
4901da177e4SLinus Torvalds 		kfree(clear);
491cb88a1b8SAlan Stern 		spin_lock_irqsave(&hub->tt.lock, flags);
4921da177e4SLinus Torvalds 	}
4931da177e4SLinus Torvalds 	spin_unlock_irqrestore (&hub->tt.lock, flags);
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds /**
497cb88a1b8SAlan Stern  * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
498cb88a1b8SAlan Stern  * @urb: an URB associated with the failed or incomplete split transaction
4991da177e4SLinus Torvalds  *
5001da177e4SLinus Torvalds  * High speed HCDs use this to tell the hub driver that some split control or
5011da177e4SLinus Torvalds  * bulk transaction failed in a way that requires clearing internal state of
5021da177e4SLinus Torvalds  * a transaction translator.  This is normally detected (and reported) from
5031da177e4SLinus Torvalds  * interrupt context.
5041da177e4SLinus Torvalds  *
5051da177e4SLinus Torvalds  * It may not be possible for that hub to handle additional full (or low)
5061da177e4SLinus Torvalds  * speed transactions until that state is fully cleared out.
5071da177e4SLinus Torvalds  */
508cb88a1b8SAlan Stern int usb_hub_clear_tt_buffer(struct urb *urb)
5091da177e4SLinus Torvalds {
510cb88a1b8SAlan Stern 	struct usb_device	*udev = urb->dev;
511cb88a1b8SAlan Stern 	int			pipe = urb->pipe;
5121da177e4SLinus Torvalds 	struct usb_tt		*tt = udev->tt;
5131da177e4SLinus Torvalds 	unsigned long		flags;
5141da177e4SLinus Torvalds 	struct usb_tt_clear	*clear;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	/* we've got to cope with an arbitrary number of pending TT clears,
5171da177e4SLinus Torvalds 	 * since each TT has "at least two" buffers that can need it (and
5181da177e4SLinus Torvalds 	 * there can be many TTs per hub).  even if they're uncommon.
5191da177e4SLinus Torvalds 	 */
52054e6ecb2SChristoph Lameter 	if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
5211da177e4SLinus Torvalds 		dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
5221da177e4SLinus Torvalds 		/* FIXME recover somehow ... RESET_TT? */
523cb88a1b8SAlan Stern 		return -ENOMEM;
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	/* info that CLEAR_TT_BUFFER needs */
5271da177e4SLinus Torvalds 	clear->tt = tt->multi ? udev->ttport : 1;
5281da177e4SLinus Torvalds 	clear->devinfo = usb_pipeendpoint (pipe);
5291da177e4SLinus Torvalds 	clear->devinfo |= udev->devnum << 4;
5301da177e4SLinus Torvalds 	clear->devinfo |= usb_pipecontrol (pipe)
5311da177e4SLinus Torvalds 			? (USB_ENDPOINT_XFER_CONTROL << 11)
5321da177e4SLinus Torvalds 			: (USB_ENDPOINT_XFER_BULK << 11);
5331da177e4SLinus Torvalds 	if (usb_pipein (pipe))
5341da177e4SLinus Torvalds 		clear->devinfo |= 1 << 15;
5351da177e4SLinus Torvalds 
536cb88a1b8SAlan Stern 	/* info for completion callback */
537cb88a1b8SAlan Stern 	clear->hcd = bus_to_hcd(udev->bus);
538cb88a1b8SAlan Stern 	clear->ep = urb->ep;
539cb88a1b8SAlan Stern 
5401da177e4SLinus Torvalds 	/* tell keventd to clear state for this TT */
5411da177e4SLinus Torvalds 	spin_lock_irqsave (&tt->lock, flags);
5421da177e4SLinus Torvalds 	list_add_tail (&clear->clear_list, &tt->clear_list);
543cb88a1b8SAlan Stern 	schedule_work(&tt->clear_work);
5441da177e4SLinus Torvalds 	spin_unlock_irqrestore (&tt->lock, flags);
545cb88a1b8SAlan Stern 	return 0;
5461da177e4SLinus Torvalds }
547cb88a1b8SAlan Stern EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
5481da177e4SLinus Torvalds 
5498520f380SAlan Stern /* If do_delay is false, return the number of milliseconds the caller
5508520f380SAlan Stern  * needs to delay.
5518520f380SAlan Stern  */
5528520f380SAlan Stern static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
5531da177e4SLinus Torvalds {
5541da177e4SLinus Torvalds 	int port1;
555b789696aSDavid Brownell 	unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
5568520f380SAlan Stern 	unsigned delay;
5574489a571SAlan Stern 	u16 wHubCharacteristics =
5584489a571SAlan Stern 			le16_to_cpu(hub->descriptor->wHubCharacteristics);
5591da177e4SLinus Torvalds 
5604489a571SAlan Stern 	/* Enable power on each port.  Some hubs have reserved values
5614489a571SAlan Stern 	 * of LPSM (> 2) in their descriptors, even though they are
5624489a571SAlan Stern 	 * USB 2.0 hubs.  Some hubs do not implement port-power switching
5634489a571SAlan Stern 	 * but only emulate it.  In all cases, the ports won't work
5644489a571SAlan Stern 	 * unless we send these messages to the hub.
5654489a571SAlan Stern 	 */
5664489a571SAlan Stern 	if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
5671da177e4SLinus Torvalds 		dev_dbg(hub->intfdev, "enabling power on all ports\n");
5684489a571SAlan Stern 	else
5694489a571SAlan Stern 		dev_dbg(hub->intfdev, "trying to enable port power on "
5704489a571SAlan Stern 				"non-switchable hub\n");
5711da177e4SLinus Torvalds 	for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
5724489a571SAlan Stern 		set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
5731da177e4SLinus Torvalds 
574b789696aSDavid Brownell 	/* Wait at least 100 msec for power to become stable */
5758520f380SAlan Stern 	delay = max(pgood_delay, (unsigned) 100);
5768520f380SAlan Stern 	if (do_delay)
5778520f380SAlan Stern 		msleep(delay);
5788520f380SAlan Stern 	return delay;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds static int hub_hub_status(struct usb_hub *hub,
5821da177e4SLinus Torvalds 		u16 *status, u16 *change)
5831da177e4SLinus Torvalds {
5841da177e4SLinus Torvalds 	int ret;
5851da177e4SLinus Torvalds 
586db90e7a1SAlan Stern 	mutex_lock(&hub->status_mutex);
5871da177e4SLinus Torvalds 	ret = get_hub_status(hub->hdev, &hub->status->hub);
5881da177e4SLinus Torvalds 	if (ret < 0)
5891da177e4SLinus Torvalds 		dev_err (hub->intfdev,
590441b62c1SHarvey Harrison 			"%s failed (err = %d)\n", __func__, ret);
5911da177e4SLinus Torvalds 	else {
5921da177e4SLinus Torvalds 		*status = le16_to_cpu(hub->status->hub.wHubStatus);
5931da177e4SLinus Torvalds 		*change = le16_to_cpu(hub->status->hub.wHubChange);
5941da177e4SLinus Torvalds 		ret = 0;
5951da177e4SLinus Torvalds 	}
596db90e7a1SAlan Stern 	mutex_unlock(&hub->status_mutex);
5971da177e4SLinus Torvalds 	return ret;
5981da177e4SLinus Torvalds }
5991da177e4SLinus Torvalds 
6008b28c752SAlan Stern static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
6018b28c752SAlan Stern {
6028b28c752SAlan Stern 	struct usb_device *hdev = hub->hdev;
6030458d5b4SAlan Stern 	int ret = 0;
6048b28c752SAlan Stern 
6050458d5b4SAlan Stern 	if (hdev->children[port1-1] && set_state)
6068b28c752SAlan Stern 		usb_set_device_state(hdev->children[port1-1],
6078b28c752SAlan Stern 				USB_STATE_NOTATTACHED);
6080458d5b4SAlan Stern 	if (!hub->error)
6098b28c752SAlan Stern 		ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
6108b28c752SAlan Stern 	if (ret)
6118b28c752SAlan Stern 		dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
6128b28c752SAlan Stern 				port1, ret);
6138b28c752SAlan Stern 	return ret;
6148b28c752SAlan Stern }
6158b28c752SAlan Stern 
6160458d5b4SAlan Stern /*
6170458d5b4SAlan Stern  * Disable a port and mark a logical connnect-change event, so that some
6180458d5b4SAlan Stern  * time later khubd will disconnect() any existing usb_device on the port
6190458d5b4SAlan Stern  * and will re-enumerate if there actually is a device attached.
6200458d5b4SAlan Stern  */
6210458d5b4SAlan Stern static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
6227d069b7dSAlan Stern {
6230458d5b4SAlan Stern 	dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
6240458d5b4SAlan Stern 	hub_port_disable(hub, port1, 1);
6250458d5b4SAlan Stern 
6260458d5b4SAlan Stern 	/* FIXME let caller ask to power down the port:
6270458d5b4SAlan Stern 	 *  - some devices won't enumerate without a VBUS power cycle
6280458d5b4SAlan Stern 	 *  - SRP saves power that way
6290458d5b4SAlan Stern 	 *  - ... new call, TBD ...
6300458d5b4SAlan Stern 	 * That's easy if this hub can switch power per-port, and
6310458d5b4SAlan Stern 	 * khubd reactivates the port later (timer, SRP, etc).
6320458d5b4SAlan Stern 	 * Powerdown must be optional, because of reset/DFU.
6330458d5b4SAlan Stern 	 */
6340458d5b4SAlan Stern 
6350458d5b4SAlan Stern 	set_bit(port1, hub->change_bits);
6360458d5b4SAlan Stern  	kick_khubd(hub);
6370458d5b4SAlan Stern }
6380458d5b4SAlan Stern 
6396ee0b270SAlan Stern enum hub_activation_type {
6408520f380SAlan Stern 	HUB_INIT, HUB_INIT2, HUB_INIT3,
6418520f380SAlan Stern 	HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
6426ee0b270SAlan Stern };
6435e6effaeSAlan Stern 
6448520f380SAlan Stern static void hub_init_func2(struct work_struct *ws);
6458520f380SAlan Stern static void hub_init_func3(struct work_struct *ws);
6468520f380SAlan Stern 
647f2835219SAlan Stern static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
6485e6effaeSAlan Stern {
6495e6effaeSAlan Stern 	struct usb_device *hdev = hub->hdev;
6505e6effaeSAlan Stern 	int port1;
651f2835219SAlan Stern 	int status;
652948fea37SAlan Stern 	bool need_debounce_delay = false;
6538520f380SAlan Stern 	unsigned delay;
6548520f380SAlan Stern 
6558520f380SAlan Stern 	/* Continue a partial initialization */
6568520f380SAlan Stern 	if (type == HUB_INIT2)
6578520f380SAlan Stern 		goto init2;
6588520f380SAlan Stern 	if (type == HUB_INIT3)
6598520f380SAlan Stern 		goto init3;
6605e6effaeSAlan Stern 
661f2835219SAlan Stern 	/* After a resume, port power should still be on.
662f2835219SAlan Stern 	 * For any other type of activation, turn it on.
663f2835219SAlan Stern 	 */
6648520f380SAlan Stern 	if (type != HUB_RESUME) {
6658520f380SAlan Stern 
6668520f380SAlan Stern 		/* Speed up system boot by using a delayed_work for the
6678520f380SAlan Stern 		 * hub's initial power-up delays.  This is pretty awkward
6688520f380SAlan Stern 		 * and the implementation looks like a home-brewed sort of
6698520f380SAlan Stern 		 * setjmp/longjmp, but it saves at least 100 ms for each
6708520f380SAlan Stern 		 * root hub (assuming usbcore is compiled into the kernel
6718520f380SAlan Stern 		 * rather than as a module).  It adds up.
6728520f380SAlan Stern 		 *
6738520f380SAlan Stern 		 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
6748520f380SAlan Stern 		 * because for those activation types the ports have to be
6758520f380SAlan Stern 		 * operational when we return.  In theory this could be done
6768520f380SAlan Stern 		 * for HUB_POST_RESET, but it's easier not to.
6778520f380SAlan Stern 		 */
6788520f380SAlan Stern 		if (type == HUB_INIT) {
6798520f380SAlan Stern 			delay = hub_power_on(hub, false);
6808520f380SAlan Stern 			PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
6818520f380SAlan Stern 			schedule_delayed_work(&hub->init_work,
6828520f380SAlan Stern 					msecs_to_jiffies(delay));
68361fbeba1SAlan Stern 
68461fbeba1SAlan Stern 			/* Suppress autosuspend until init is done */
685ccf5b801SAlan Stern 			atomic_set(&to_usb_interface(hub->intfdev)->
686ccf5b801SAlan Stern 					pm_usage_cnt, 1);
6878520f380SAlan Stern 			return;		/* Continues at init2: below */
6888520f380SAlan Stern 		} else {
6898520f380SAlan Stern 			hub_power_on(hub, true);
6908520f380SAlan Stern 		}
6918520f380SAlan Stern 	}
6928520f380SAlan Stern  init2:
693f2835219SAlan Stern 
6946ee0b270SAlan Stern 	/* Check each port and set hub->change_bits to let khubd know
6956ee0b270SAlan Stern 	 * which ports need attention.
6965e6effaeSAlan Stern 	 */
6975e6effaeSAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
6985e6effaeSAlan Stern 		struct usb_device *udev = hdev->children[port1-1];
6995e6effaeSAlan Stern 		u16 portstatus, portchange;
7005e6effaeSAlan Stern 
7016ee0b270SAlan Stern 		portstatus = portchange = 0;
7026ee0b270SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
7036ee0b270SAlan Stern 		if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
7046ee0b270SAlan Stern 			dev_dbg(hub->intfdev,
7056ee0b270SAlan Stern 					"port %d: status %04x change %04x\n",
7066ee0b270SAlan Stern 					port1, portstatus, portchange);
7075e6effaeSAlan Stern 
7086ee0b270SAlan Stern 		/* After anything other than HUB_RESUME (i.e., initialization
7096ee0b270SAlan Stern 		 * or any sort of reset), every port should be disabled.
7106ee0b270SAlan Stern 		 * Unconnected ports should likewise be disabled (paranoia),
7116ee0b270SAlan Stern 		 * and so should ports for which we have no usb_device.
7125e6effaeSAlan Stern 		 */
7136ee0b270SAlan Stern 		if ((portstatus & USB_PORT_STAT_ENABLE) && (
7146ee0b270SAlan Stern 				type != HUB_RESUME ||
7156ee0b270SAlan Stern 				!(portstatus & USB_PORT_STAT_CONNECTION) ||
7166ee0b270SAlan Stern 				!udev ||
7176ee0b270SAlan Stern 				udev->state == USB_STATE_NOTATTACHED)) {
7186ee0b270SAlan Stern 			clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
7196ee0b270SAlan Stern 			portstatus &= ~USB_PORT_STAT_ENABLE;
7206ee0b270SAlan Stern 		}
7216ee0b270SAlan Stern 
722948fea37SAlan Stern 		/* Clear status-change flags; we'll debounce later */
723948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
724948fea37SAlan Stern 			need_debounce_delay = true;
725948fea37SAlan Stern 			clear_port_feature(hub->hdev, port1,
726948fea37SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
727948fea37SAlan Stern 		}
728948fea37SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE) {
729948fea37SAlan Stern 			need_debounce_delay = true;
730948fea37SAlan Stern 			clear_port_feature(hub->hdev, port1,
731948fea37SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
732948fea37SAlan Stern 		}
733948fea37SAlan Stern 
7346ee0b270SAlan Stern 		if (!udev || udev->state == USB_STATE_NOTATTACHED) {
7356ee0b270SAlan Stern 			/* Tell khubd to disconnect the device or
7366ee0b270SAlan Stern 			 * check for a new connection
7376ee0b270SAlan Stern 			 */
7386ee0b270SAlan Stern 			if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
7396ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
7406ee0b270SAlan Stern 
7416ee0b270SAlan Stern 		} else if (portstatus & USB_PORT_STAT_ENABLE) {
7426ee0b270SAlan Stern 			/* The power session apparently survived the resume.
7436ee0b270SAlan Stern 			 * If there was an overcurrent or suspend change
7446ee0b270SAlan Stern 			 * (i.e., remote wakeup request), have khubd
7456ee0b270SAlan Stern 			 * take care of it.
7466ee0b270SAlan Stern 			 */
7476ee0b270SAlan Stern 			if (portchange)
7486ee0b270SAlan Stern 				set_bit(port1, hub->change_bits);
7496ee0b270SAlan Stern 
7506ee0b270SAlan Stern 		} else if (udev->persist_enabled) {
7516ee0b270SAlan Stern #ifdef CONFIG_PM
7525e6effaeSAlan Stern 			udev->reset_resume = 1;
7536ee0b270SAlan Stern #endif
7548808f00cSAlan Stern 			set_bit(port1, hub->change_bits);
7558808f00cSAlan Stern 
7566ee0b270SAlan Stern 		} else {
7576ee0b270SAlan Stern 			/* The power session is gone; tell khubd */
7586ee0b270SAlan Stern 			usb_set_device_state(udev, USB_STATE_NOTATTACHED);
7596ee0b270SAlan Stern 			set_bit(port1, hub->change_bits);
7605e6effaeSAlan Stern 		}
7615e6effaeSAlan Stern 	}
7625e6effaeSAlan Stern 
763948fea37SAlan Stern 	/* If no port-status-change flags were set, we don't need any
764948fea37SAlan Stern 	 * debouncing.  If flags were set we can try to debounce the
765948fea37SAlan Stern 	 * ports all at once right now, instead of letting khubd do them
766948fea37SAlan Stern 	 * one at a time later on.
767948fea37SAlan Stern 	 *
768948fea37SAlan Stern 	 * If any port-status changes do occur during this delay, khubd
769948fea37SAlan Stern 	 * will see them later and handle them normally.
770948fea37SAlan Stern 	 */
7718520f380SAlan Stern 	if (need_debounce_delay) {
7728520f380SAlan Stern 		delay = HUB_DEBOUNCE_STABLE;
773f2835219SAlan Stern 
7748520f380SAlan Stern 		/* Don't do a long sleep inside a workqueue routine */
7758520f380SAlan Stern 		if (type == HUB_INIT2) {
7768520f380SAlan Stern 			PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
7778520f380SAlan Stern 			schedule_delayed_work(&hub->init_work,
7788520f380SAlan Stern 					msecs_to_jiffies(delay));
7798520f380SAlan Stern 			return;		/* Continues at init3: below */
7808520f380SAlan Stern 		} else {
7818520f380SAlan Stern 			msleep(delay);
7828520f380SAlan Stern 		}
7838520f380SAlan Stern 	}
7848520f380SAlan Stern  init3:
785f2835219SAlan Stern 	hub->quiescing = 0;
786f2835219SAlan Stern 
787f2835219SAlan Stern 	status = usb_submit_urb(hub->urb, GFP_NOIO);
788f2835219SAlan Stern 	if (status < 0)
789f2835219SAlan Stern 		dev_err(hub->intfdev, "activate --> %d\n", status);
790f2835219SAlan Stern 	if (hub->has_indicators && blinkenlights)
791f2835219SAlan Stern 		schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
792f2835219SAlan Stern 
793f2835219SAlan Stern 	/* Scan all ports that need attention */
794f2835219SAlan Stern 	kick_khubd(hub);
7955e6effaeSAlan Stern }
7965e6effaeSAlan Stern 
7978520f380SAlan Stern /* Implement the continuations for the delays above */
7988520f380SAlan Stern static void hub_init_func2(struct work_struct *ws)
7998520f380SAlan Stern {
8008520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
8018520f380SAlan Stern 
8028520f380SAlan Stern 	hub_activate(hub, HUB_INIT2);
8038520f380SAlan Stern }
8048520f380SAlan Stern 
8058520f380SAlan Stern static void hub_init_func3(struct work_struct *ws)
8068520f380SAlan Stern {
8078520f380SAlan Stern 	struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
8088520f380SAlan Stern 
8098520f380SAlan Stern 	hub_activate(hub, HUB_INIT3);
8108520f380SAlan Stern }
8118520f380SAlan Stern 
8124330354fSAlan Stern enum hub_quiescing_type {
8134330354fSAlan Stern 	HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
8144330354fSAlan Stern };
8154330354fSAlan Stern 
8164330354fSAlan Stern static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
8174330354fSAlan Stern {
8184330354fSAlan Stern 	struct usb_device *hdev = hub->hdev;
8194330354fSAlan Stern 	int i;
8204330354fSAlan Stern 
8218520f380SAlan Stern 	cancel_delayed_work_sync(&hub->init_work);
8228520f380SAlan Stern 
8234330354fSAlan Stern 	/* khubd and related activity won't re-trigger */
8244330354fSAlan Stern 	hub->quiescing = 1;
8254330354fSAlan Stern 
8264330354fSAlan Stern 	if (type != HUB_SUSPEND) {
8274330354fSAlan Stern 		/* Disconnect all the children */
8284330354fSAlan Stern 		for (i = 0; i < hdev->maxchild; ++i) {
8294330354fSAlan Stern 			if (hdev->children[i])
8304330354fSAlan Stern 				usb_disconnect(&hdev->children[i]);
8314330354fSAlan Stern 		}
8324330354fSAlan Stern 	}
8334330354fSAlan Stern 
8344330354fSAlan Stern 	/* Stop khubd and related activity */
8354330354fSAlan Stern 	usb_kill_urb(hub->urb);
8364330354fSAlan Stern 	if (hub->has_indicators)
8374330354fSAlan Stern 		cancel_delayed_work_sync(&hub->leds);
8384330354fSAlan Stern 	if (hub->tt.hub)
839cb88a1b8SAlan Stern 		cancel_work_sync(&hub->tt.clear_work);
8404330354fSAlan Stern }
8414330354fSAlan Stern 
8423eb14915SAlan Stern /* caller has locked the hub device */
8433eb14915SAlan Stern static int hub_pre_reset(struct usb_interface *intf)
8443eb14915SAlan Stern {
8453eb14915SAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
8463eb14915SAlan Stern 
8474330354fSAlan Stern 	hub_quiesce(hub, HUB_PRE_RESET);
848f07600cfSAlan Stern 	return 0;
8497d069b7dSAlan Stern }
8507d069b7dSAlan Stern 
8517d069b7dSAlan Stern /* caller has locked the hub device */
852f07600cfSAlan Stern static int hub_post_reset(struct usb_interface *intf)
8537d069b7dSAlan Stern {
8547de18d8bSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
8557de18d8bSAlan Stern 
856f2835219SAlan Stern 	hub_activate(hub, HUB_POST_RESET);
857f07600cfSAlan Stern 	return 0;
8587d069b7dSAlan Stern }
8597d069b7dSAlan Stern 
8601da177e4SLinus Torvalds static int hub_configure(struct usb_hub *hub,
8611da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint)
8621da177e4SLinus Torvalds {
863b356b7c7SSarah Sharp 	struct usb_hcd *hcd;
8641da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
8651da177e4SLinus Torvalds 	struct device *hub_dev = hub->intfdev;
8661da177e4SLinus Torvalds 	u16 hubstatus, hubchange;
86774ad9bd2SGreg Kroah-Hartman 	u16 wHubCharacteristics;
8681da177e4SLinus Torvalds 	unsigned int pipe;
8691da177e4SLinus Torvalds 	int maxp, ret;
8707cbe5dcaSAlan Stern 	char *message = "out of memory";
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	hub->buffer = usb_buffer_alloc(hdev, sizeof(*hub->buffer), GFP_KERNEL,
8731da177e4SLinus Torvalds 			&hub->buffer_dma);
8741da177e4SLinus Torvalds 	if (!hub->buffer) {
8751da177e4SLinus Torvalds 		ret = -ENOMEM;
8761da177e4SLinus Torvalds 		goto fail;
8771da177e4SLinus Torvalds 	}
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
8801da177e4SLinus Torvalds 	if (!hub->status) {
8811da177e4SLinus Torvalds 		ret = -ENOMEM;
8821da177e4SLinus Torvalds 		goto fail;
8831da177e4SLinus Torvalds 	}
884db90e7a1SAlan Stern 	mutex_init(&hub->status_mutex);
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
8871da177e4SLinus Torvalds 	if (!hub->descriptor) {
8881da177e4SLinus Torvalds 		ret = -ENOMEM;
8891da177e4SLinus Torvalds 		goto fail;
8901da177e4SLinus Torvalds 	}
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	/* Request the entire hub descriptor.
8931da177e4SLinus Torvalds 	 * hub->descriptor can handle USB_MAXCHILDREN ports,
8941da177e4SLinus Torvalds 	 * but the hub can/will return fewer bytes here.
8951da177e4SLinus Torvalds 	 */
8961da177e4SLinus Torvalds 	ret = get_hub_descriptor(hdev, hub->descriptor,
8971da177e4SLinus Torvalds 			sizeof(*hub->descriptor));
8981da177e4SLinus Torvalds 	if (ret < 0) {
8991da177e4SLinus Torvalds 		message = "can't read hub descriptor";
9001da177e4SLinus Torvalds 		goto fail;
9011da177e4SLinus Torvalds 	} else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
9021da177e4SLinus Torvalds 		message = "hub has too many ports!";
9031da177e4SLinus Torvalds 		ret = -ENODEV;
9041da177e4SLinus Torvalds 		goto fail;
9051da177e4SLinus Torvalds 	}
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	hdev->maxchild = hub->descriptor->bNbrPorts;
9081da177e4SLinus Torvalds 	dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
9091da177e4SLinus Torvalds 		(hdev->maxchild == 1) ? "" : "s");
9101da177e4SLinus Torvalds 
9117cbe5dcaSAlan Stern 	hub->port_owners = kzalloc(hdev->maxchild * sizeof(void *), GFP_KERNEL);
9127cbe5dcaSAlan Stern 	if (!hub->port_owners) {
9137cbe5dcaSAlan Stern 		ret = -ENOMEM;
9147cbe5dcaSAlan Stern 		goto fail;
9157cbe5dcaSAlan Stern 	}
9167cbe5dcaSAlan Stern 
91774ad9bd2SGreg Kroah-Hartman 	wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
9181da177e4SLinus Torvalds 
91974ad9bd2SGreg Kroah-Hartman 	if (wHubCharacteristics & HUB_CHAR_COMPOUND) {
9201da177e4SLinus Torvalds 		int	i;
9211da177e4SLinus Torvalds 		char	portstr [USB_MAXCHILDREN + 1];
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 		for (i = 0; i < hdev->maxchild; i++)
9241da177e4SLinus Torvalds 			portstr[i] = hub->descriptor->DeviceRemovable
9251da177e4SLinus Torvalds 				    [((i + 1) / 8)] & (1 << ((i + 1) % 8))
9261da177e4SLinus Torvalds 				? 'F' : 'R';
9271da177e4SLinus Torvalds 		portstr[hdev->maxchild] = 0;
9281da177e4SLinus Torvalds 		dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
9291da177e4SLinus Torvalds 	} else
9301da177e4SLinus Torvalds 		dev_dbg(hub_dev, "standalone hub\n");
9311da177e4SLinus Torvalds 
93274ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_LPSM) {
9331da177e4SLinus Torvalds 		case 0x00:
9341da177e4SLinus Torvalds 			dev_dbg(hub_dev, "ganged power switching\n");
9351da177e4SLinus Torvalds 			break;
9361da177e4SLinus Torvalds 		case 0x01:
9371da177e4SLinus Torvalds 			dev_dbg(hub_dev, "individual port power switching\n");
9381da177e4SLinus Torvalds 			break;
9391da177e4SLinus Torvalds 		case 0x02:
9401da177e4SLinus Torvalds 		case 0x03:
9411da177e4SLinus Torvalds 			dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
9421da177e4SLinus Torvalds 			break;
9431da177e4SLinus Torvalds 	}
9441da177e4SLinus Torvalds 
94574ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_OCPM) {
9461da177e4SLinus Torvalds 		case 0x00:
9471da177e4SLinus Torvalds 			dev_dbg(hub_dev, "global over-current protection\n");
9481da177e4SLinus Torvalds 			break;
9491da177e4SLinus Torvalds 		case 0x08:
9501da177e4SLinus Torvalds 			dev_dbg(hub_dev, "individual port over-current protection\n");
9511da177e4SLinus Torvalds 			break;
9521da177e4SLinus Torvalds 		case 0x10:
9531da177e4SLinus Torvalds 		case 0x18:
9541da177e4SLinus Torvalds 			dev_dbg(hub_dev, "no over-current protection\n");
9551da177e4SLinus Torvalds                         break;
9561da177e4SLinus Torvalds 	}
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	spin_lock_init (&hub->tt.lock);
9591da177e4SLinus Torvalds 	INIT_LIST_HEAD (&hub->tt.clear_list);
960cb88a1b8SAlan Stern 	INIT_WORK(&hub->tt.clear_work, hub_tt_work);
9611da177e4SLinus Torvalds 	switch (hdev->descriptor.bDeviceProtocol) {
9621da177e4SLinus Torvalds 		case 0:
9631da177e4SLinus Torvalds 			break;
9641da177e4SLinus Torvalds 		case 1:
9651da177e4SLinus Torvalds 			dev_dbg(hub_dev, "Single TT\n");
9661da177e4SLinus Torvalds 			hub->tt.hub = hdev;
9671da177e4SLinus Torvalds 			break;
9681da177e4SLinus Torvalds 		case 2:
9691da177e4SLinus Torvalds 			ret = usb_set_interface(hdev, 0, 1);
9701da177e4SLinus Torvalds 			if (ret == 0) {
9711da177e4SLinus Torvalds 				dev_dbg(hub_dev, "TT per port\n");
9721da177e4SLinus Torvalds 				hub->tt.multi = 1;
9731da177e4SLinus Torvalds 			} else
9741da177e4SLinus Torvalds 				dev_err(hub_dev, "Using single TT (err %d)\n",
9751da177e4SLinus Torvalds 					ret);
9761da177e4SLinus Torvalds 			hub->tt.hub = hdev;
9771da177e4SLinus Torvalds 			break;
978d2e9b4d6SSarah Sharp 		case 3:
979d2e9b4d6SSarah Sharp 			/* USB 3.0 hubs don't have a TT */
980d2e9b4d6SSarah Sharp 			break;
9811da177e4SLinus Torvalds 		default:
9821da177e4SLinus Torvalds 			dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
9831da177e4SLinus Torvalds 				hdev->descriptor.bDeviceProtocol);
9841da177e4SLinus Torvalds 			break;
9851da177e4SLinus Torvalds 	}
9861da177e4SLinus Torvalds 
987e09711aeSdavid-b@pacbell.net 	/* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
98874ad9bd2SGreg Kroah-Hartman 	switch (wHubCharacteristics & HUB_CHAR_TTTT) {
989e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_8_BITS:
990e09711aeSdavid-b@pacbell.net 			if (hdev->descriptor.bDeviceProtocol != 0) {
991e09711aeSdavid-b@pacbell.net 				hub->tt.think_time = 666;
992e09711aeSdavid-b@pacbell.net 				dev_dbg(hub_dev, "TT requires at most %d "
993e09711aeSdavid-b@pacbell.net 						"FS bit times (%d ns)\n",
994e09711aeSdavid-b@pacbell.net 					8, hub->tt.think_time);
995e09711aeSdavid-b@pacbell.net 			}
9961da177e4SLinus Torvalds 			break;
997e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_16_BITS:
998e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 2;
999e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1000e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1001e09711aeSdavid-b@pacbell.net 				16, hub->tt.think_time);
10021da177e4SLinus Torvalds 			break;
1003e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_24_BITS:
1004e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 3;
1005e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1006e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1007e09711aeSdavid-b@pacbell.net 				24, hub->tt.think_time);
10081da177e4SLinus Torvalds 			break;
1009e09711aeSdavid-b@pacbell.net 		case HUB_TTTT_32_BITS:
1010e09711aeSdavid-b@pacbell.net 			hub->tt.think_time = 666 * 4;
1011e09711aeSdavid-b@pacbell.net 			dev_dbg(hub_dev, "TT requires at most %d "
1012e09711aeSdavid-b@pacbell.net 					"FS bit times (%d ns)\n",
1013e09711aeSdavid-b@pacbell.net 				32, hub->tt.think_time);
10141da177e4SLinus Torvalds 			break;
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	/* probe() zeroes hub->indicator[] */
101874ad9bd2SGreg Kroah-Hartman 	if (wHubCharacteristics & HUB_CHAR_PORTIND) {
10191da177e4SLinus Torvalds 		hub->has_indicators = 1;
10201da177e4SLinus Torvalds 		dev_dbg(hub_dev, "Port indicators are supported\n");
10211da177e4SLinus Torvalds 	}
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	dev_dbg(hub_dev, "power on to power good time: %dms\n",
10241da177e4SLinus Torvalds 		hub->descriptor->bPwrOn2PwrGood * 2);
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	/* power budgeting mostly matters with bus-powered hubs,
10271da177e4SLinus Torvalds 	 * and battery-powered root hubs (may provide just 8 mA).
10281da177e4SLinus Torvalds 	 */
10291da177e4SLinus Torvalds 	ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
103055c52718SAlan Stern 	if (ret < 2) {
10311da177e4SLinus Torvalds 		message = "can't get hub status";
10321da177e4SLinus Torvalds 		goto fail;
10331da177e4SLinus Torvalds 	}
10347d35b929SAlan Stern 	le16_to_cpus(&hubstatus);
10357d35b929SAlan Stern 	if (hdev == hdev->bus->root_hub) {
103655c52718SAlan Stern 		if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
103755c52718SAlan Stern 			hub->mA_per_port = 500;
103855c52718SAlan Stern 		else {
103955c52718SAlan Stern 			hub->mA_per_port = hdev->bus_mA;
104055c52718SAlan Stern 			hub->limited_power = 1;
104155c52718SAlan Stern 		}
10427d35b929SAlan Stern 	} else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
10431da177e4SLinus Torvalds 		dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
10441da177e4SLinus Torvalds 			hub->descriptor->bHubContrCurrent);
104555c52718SAlan Stern 		hub->limited_power = 1;
104655c52718SAlan Stern 		if (hdev->maxchild > 0) {
104755c52718SAlan Stern 			int remaining = hdev->bus_mA -
104855c52718SAlan Stern 					hub->descriptor->bHubContrCurrent;
10491da177e4SLinus Torvalds 
105055c52718SAlan Stern 			if (remaining < hdev->maxchild * 100)
105155c52718SAlan Stern 				dev_warn(hub_dev,
105255c52718SAlan Stern 					"insufficient power available "
105355c52718SAlan Stern 					"to use all downstream ports\n");
105455c52718SAlan Stern 			hub->mA_per_port = 100;		/* 7.2.1.1 */
105555c52718SAlan Stern 		}
105655c52718SAlan Stern 	} else {	/* Self-powered external hub */
105755c52718SAlan Stern 		/* FIXME: What about battery-powered external hubs that
105855c52718SAlan Stern 		 * provide less current per port? */
105955c52718SAlan Stern 		hub->mA_per_port = 500;
106055c52718SAlan Stern 	}
106155c52718SAlan Stern 	if (hub->mA_per_port < 500)
106255c52718SAlan Stern 		dev_dbg(hub_dev, "%umA bus power budget for each child\n",
106355c52718SAlan Stern 				hub->mA_per_port);
10641da177e4SLinus Torvalds 
1065b356b7c7SSarah Sharp 	/* Update the HCD's internal representation of this hub before khubd
1066b356b7c7SSarah Sharp 	 * starts getting port status changes for devices under the hub.
1067b356b7c7SSarah Sharp 	 */
1068b356b7c7SSarah Sharp 	hcd = bus_to_hcd(hdev->bus);
1069b356b7c7SSarah Sharp 	if (hcd->driver->update_hub_device) {
1070b356b7c7SSarah Sharp 		ret = hcd->driver->update_hub_device(hcd, hdev,
1071b356b7c7SSarah Sharp 				&hub->tt, GFP_KERNEL);
1072b356b7c7SSarah Sharp 		if (ret < 0) {
1073b356b7c7SSarah Sharp 			message = "can't update HCD hub info";
1074b356b7c7SSarah Sharp 			goto fail;
1075b356b7c7SSarah Sharp 		}
1076b356b7c7SSarah Sharp 	}
1077b356b7c7SSarah Sharp 
10781da177e4SLinus Torvalds 	ret = hub_hub_status(hub, &hubstatus, &hubchange);
10791da177e4SLinus Torvalds 	if (ret < 0) {
10801da177e4SLinus Torvalds 		message = "can't get hub status";
10811da177e4SLinus Torvalds 		goto fail;
10821da177e4SLinus Torvalds 	}
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	/* local power status reports aren't always correct */
10851da177e4SLinus Torvalds 	if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
10861da177e4SLinus Torvalds 		dev_dbg(hub_dev, "local power source is %s\n",
10871da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_LOCAL_POWER)
10881da177e4SLinus Torvalds 			? "lost (inactive)" : "good");
10891da177e4SLinus Torvalds 
109074ad9bd2SGreg Kroah-Hartman 	if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
10911da177e4SLinus Torvalds 		dev_dbg(hub_dev, "%sover-current condition exists\n",
10921da177e4SLinus Torvalds 			(hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
10931da177e4SLinus Torvalds 
109488fafff9Sinaky@linux.intel.com 	/* set up the interrupt endpoint
109588fafff9Sinaky@linux.intel.com 	 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
109688fafff9Sinaky@linux.intel.com 	 * bytes as USB2.0[11.12.3] says because some hubs are known
109788fafff9Sinaky@linux.intel.com 	 * to send more data (and thus cause overflow). For root hubs,
109888fafff9Sinaky@linux.intel.com 	 * maxpktsize is defined in hcd.c's fake endpoint descriptors
109988fafff9Sinaky@linux.intel.com 	 * to be big enough for at least USB_MAXCHILDREN ports. */
11001da177e4SLinus Torvalds 	pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
11011da177e4SLinus Torvalds 	maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	if (maxp > sizeof(*hub->buffer))
11041da177e4SLinus Torvalds 		maxp = sizeof(*hub->buffer);
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	hub->urb = usb_alloc_urb(0, GFP_KERNEL);
11071da177e4SLinus Torvalds 	if (!hub->urb) {
11081da177e4SLinus Torvalds 		ret = -ENOMEM;
11091da177e4SLinus Torvalds 		goto fail;
11101da177e4SLinus Torvalds 	}
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 	usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
11131da177e4SLinus Torvalds 		hub, endpoint->bInterval);
11141da177e4SLinus Torvalds 	hub->urb->transfer_dma = hub->buffer_dma;
11151da177e4SLinus Torvalds 	hub->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	/* maybe cycle the hub leds */
11181da177e4SLinus Torvalds 	if (hub->has_indicators && blinkenlights)
11191da177e4SLinus Torvalds 		hub->indicator [0] = INDICATOR_CYCLE;
11201da177e4SLinus Torvalds 
1121f2835219SAlan Stern 	hub_activate(hub, HUB_INIT);
11221da177e4SLinus Torvalds 	return 0;
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds fail:
11251da177e4SLinus Torvalds 	dev_err (hub_dev, "config failed, %s (err %d)\n",
11261da177e4SLinus Torvalds 			message, ret);
11271da177e4SLinus Torvalds 	/* hub_disconnect() frees urb and descriptor */
11281da177e4SLinus Torvalds 	return ret;
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds 
1131e8054854SAlan Stern static void hub_release(struct kref *kref)
1132e8054854SAlan Stern {
1133e8054854SAlan Stern 	struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1134e8054854SAlan Stern 
1135e8054854SAlan Stern 	usb_put_intf(to_usb_interface(hub->intfdev));
1136e8054854SAlan Stern 	kfree(hub);
1137e8054854SAlan Stern }
1138e8054854SAlan Stern 
11391da177e4SLinus Torvalds static unsigned highspeed_hubs;
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds static void hub_disconnect(struct usb_interface *intf)
11421da177e4SLinus Torvalds {
11431da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata (intf);
1144e8054854SAlan Stern 
1145e8054854SAlan Stern 	/* Take the hub off the event list and don't let it be added again */
1146e8054854SAlan Stern 	spin_lock_irq(&hub_event_lock);
1147e8054854SAlan Stern 	list_del_init(&hub->event_list);
1148e8054854SAlan Stern 	hub->disconnected = 1;
1149e8054854SAlan Stern 	spin_unlock_irq(&hub_event_lock);
11501da177e4SLinus Torvalds 
11517de18d8bSAlan Stern 	/* Disconnect all children and quiesce the hub */
11527de18d8bSAlan Stern 	hub->error = 0;
11534330354fSAlan Stern 	hub_quiesce(hub, HUB_DISCONNECT);
11547de18d8bSAlan Stern 
11558b28c752SAlan Stern 	usb_set_intfdata (intf, NULL);
11567cbe5dcaSAlan Stern 	hub->hdev->maxchild = 0;
11571da177e4SLinus Torvalds 
1158e8054854SAlan Stern 	if (hub->hdev->speed == USB_SPEED_HIGH)
11591da177e4SLinus Torvalds 		highspeed_hubs--;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	usb_free_urb(hub->urb);
11627cbe5dcaSAlan Stern 	kfree(hub->port_owners);
11631da177e4SLinus Torvalds 	kfree(hub->descriptor);
11641da177e4SLinus Torvalds 	kfree(hub->status);
1165e8054854SAlan Stern 	usb_buffer_free(hub->hdev, sizeof(*hub->buffer), hub->buffer,
11661da177e4SLinus Torvalds 			hub->buffer_dma);
11671da177e4SLinus Torvalds 
1168e8054854SAlan Stern 	kref_put(&hub->kref, hub_release);
11691da177e4SLinus Torvalds }
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
11721da177e4SLinus Torvalds {
11731da177e4SLinus Torvalds 	struct usb_host_interface *desc;
11741da177e4SLinus Torvalds 	struct usb_endpoint_descriptor *endpoint;
11751da177e4SLinus Torvalds 	struct usb_device *hdev;
11761da177e4SLinus Torvalds 	struct usb_hub *hub;
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	desc = intf->cur_altsetting;
11791da177e4SLinus Torvalds 	hdev = interface_to_usbdev(intf);
11801da177e4SLinus Torvalds 
118138f3ad5eSFelipe Balbi 	if (hdev->level == MAX_TOPO_LEVEL) {
1182b9cef6c3SWu Fengguang 		dev_err(&intf->dev,
1183b9cef6c3SWu Fengguang 			"Unsupported bus topology: hub nested too deep\n");
118438f3ad5eSFelipe Balbi 		return -E2BIG;
118538f3ad5eSFelipe Balbi 	}
118638f3ad5eSFelipe Balbi 
118789ccbdc9SDavid Brownell #ifdef	CONFIG_USB_OTG_BLACKLIST_HUB
118889ccbdc9SDavid Brownell 	if (hdev->parent) {
118989ccbdc9SDavid Brownell 		dev_warn(&intf->dev, "ignoring external hub\n");
119089ccbdc9SDavid Brownell 		return -ENODEV;
119189ccbdc9SDavid Brownell 	}
119289ccbdc9SDavid Brownell #endif
119389ccbdc9SDavid Brownell 
11941da177e4SLinus Torvalds 	/* Some hubs have a subclass of 1, which AFAICT according to the */
11951da177e4SLinus Torvalds 	/*  specs is not defined, but it works */
11961da177e4SLinus Torvalds 	if ((desc->desc.bInterfaceSubClass != 0) &&
11971da177e4SLinus Torvalds 	    (desc->desc.bInterfaceSubClass != 1)) {
11981da177e4SLinus Torvalds descriptor_error:
11991da177e4SLinus Torvalds 		dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
12001da177e4SLinus Torvalds 		return -EIO;
12011da177e4SLinus Torvalds 	}
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds 	/* Multiple endpoints? What kind of mutant ninja-hub is this? */
12041da177e4SLinus Torvalds 	if (desc->desc.bNumEndpoints != 1)
12051da177e4SLinus Torvalds 		goto descriptor_error;
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 	endpoint = &desc->endpoint[0].desc;
12081da177e4SLinus Torvalds 
1209fbf81c29SLuiz Fernando N. Capitulino 	/* If it's not an interrupt in endpoint, we'd better punt! */
1210fbf81c29SLuiz Fernando N. Capitulino 	if (!usb_endpoint_is_int_in(endpoint))
12111da177e4SLinus Torvalds 		goto descriptor_error;
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 	/* We found a hub */
12141da177e4SLinus Torvalds 	dev_info (&intf->dev, "USB hub found\n");
12151da177e4SLinus Torvalds 
12160a1ef3b5SAlan Stern 	hub = kzalloc(sizeof(*hub), GFP_KERNEL);
12171da177e4SLinus Torvalds 	if (!hub) {
12181da177e4SLinus Torvalds 		dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
12191da177e4SLinus Torvalds 		return -ENOMEM;
12201da177e4SLinus Torvalds 	}
12211da177e4SLinus Torvalds 
1222e8054854SAlan Stern 	kref_init(&hub->kref);
12231da177e4SLinus Torvalds 	INIT_LIST_HEAD(&hub->event_list);
12241da177e4SLinus Torvalds 	hub->intfdev = &intf->dev;
12251da177e4SLinus Torvalds 	hub->hdev = hdev;
1226c4028958SDavid Howells 	INIT_DELAYED_WORK(&hub->leds, led_work);
12278520f380SAlan Stern 	INIT_DELAYED_WORK(&hub->init_work, NULL);
1228e8054854SAlan Stern 	usb_get_intf(intf);
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	usb_set_intfdata (intf, hub);
123140f122f3SAlan Stern 	intf->needs_remote_wakeup = 1;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	if (hdev->speed == USB_SPEED_HIGH)
12341da177e4SLinus Torvalds 		highspeed_hubs++;
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	if (hub_configure(hub, endpoint) >= 0)
12371da177e4SLinus Torvalds 		return 0;
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 	hub_disconnect (intf);
12401da177e4SLinus Torvalds 	return -ENODEV;
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds static int
12441da177e4SLinus Torvalds hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
12451da177e4SLinus Torvalds {
12461da177e4SLinus Torvalds 	struct usb_device *hdev = interface_to_usbdev (intf);
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 	/* assert ifno == 0 (part of hub spec) */
12491da177e4SLinus Torvalds 	switch (code) {
12501da177e4SLinus Torvalds 	case USBDEVFS_HUB_PORTINFO: {
12511da177e4SLinus Torvalds 		struct usbdevfs_hub_portinfo *info = user_data;
12521da177e4SLinus Torvalds 		int i;
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
12551da177e4SLinus Torvalds 		if (hdev->devnum <= 0)
12561da177e4SLinus Torvalds 			info->nports = 0;
12571da177e4SLinus Torvalds 		else {
12581da177e4SLinus Torvalds 			info->nports = hdev->maxchild;
12591da177e4SLinus Torvalds 			for (i = 0; i < info->nports; i++) {
12601da177e4SLinus Torvalds 				if (hdev->children[i] == NULL)
12611da177e4SLinus Torvalds 					info->port[i] = 0;
12621da177e4SLinus Torvalds 				else
12631da177e4SLinus Torvalds 					info->port[i] =
12641da177e4SLinus Torvalds 						hdev->children[i]->devnum;
12651da177e4SLinus Torvalds 			}
12661da177e4SLinus Torvalds 		}
12671da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 		return info->nports + 1;
12701da177e4SLinus Torvalds 		}
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	default:
12731da177e4SLinus Torvalds 		return -ENOSYS;
12741da177e4SLinus Torvalds 	}
12751da177e4SLinus Torvalds }
12761da177e4SLinus Torvalds 
12777cbe5dcaSAlan Stern /*
12787cbe5dcaSAlan Stern  * Allow user programs to claim ports on a hub.  When a device is attached
12797cbe5dcaSAlan Stern  * to one of these "claimed" ports, the program will "own" the device.
12807cbe5dcaSAlan Stern  */
12817cbe5dcaSAlan Stern static int find_port_owner(struct usb_device *hdev, unsigned port1,
12827cbe5dcaSAlan Stern 		void ***ppowner)
12837cbe5dcaSAlan Stern {
12847cbe5dcaSAlan Stern 	if (hdev->state == USB_STATE_NOTATTACHED)
12857cbe5dcaSAlan Stern 		return -ENODEV;
12867cbe5dcaSAlan Stern 	if (port1 == 0 || port1 > hdev->maxchild)
12877cbe5dcaSAlan Stern 		return -EINVAL;
12887cbe5dcaSAlan Stern 
12897cbe5dcaSAlan Stern 	/* This assumes that devices not managed by the hub driver
12907cbe5dcaSAlan Stern 	 * will always have maxchild equal to 0.
12917cbe5dcaSAlan Stern 	 */
12927cbe5dcaSAlan Stern 	*ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]);
12937cbe5dcaSAlan Stern 	return 0;
12947cbe5dcaSAlan Stern }
12957cbe5dcaSAlan Stern 
12967cbe5dcaSAlan Stern /* In the following three functions, the caller must hold hdev's lock */
12977cbe5dcaSAlan Stern int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, void *owner)
12987cbe5dcaSAlan Stern {
12997cbe5dcaSAlan Stern 	int rc;
13007cbe5dcaSAlan Stern 	void **powner;
13017cbe5dcaSAlan Stern 
13027cbe5dcaSAlan Stern 	rc = find_port_owner(hdev, port1, &powner);
13037cbe5dcaSAlan Stern 	if (rc)
13047cbe5dcaSAlan Stern 		return rc;
13057cbe5dcaSAlan Stern 	if (*powner)
13067cbe5dcaSAlan Stern 		return -EBUSY;
13077cbe5dcaSAlan Stern 	*powner = owner;
13087cbe5dcaSAlan Stern 	return rc;
13097cbe5dcaSAlan Stern }
13107cbe5dcaSAlan Stern 
13117cbe5dcaSAlan Stern int usb_hub_release_port(struct usb_device *hdev, unsigned port1, void *owner)
13127cbe5dcaSAlan Stern {
13137cbe5dcaSAlan Stern 	int rc;
13147cbe5dcaSAlan Stern 	void **powner;
13157cbe5dcaSAlan Stern 
13167cbe5dcaSAlan Stern 	rc = find_port_owner(hdev, port1, &powner);
13177cbe5dcaSAlan Stern 	if (rc)
13187cbe5dcaSAlan Stern 		return rc;
13197cbe5dcaSAlan Stern 	if (*powner != owner)
13207cbe5dcaSAlan Stern 		return -ENOENT;
13217cbe5dcaSAlan Stern 	*powner = NULL;
13227cbe5dcaSAlan Stern 	return rc;
13237cbe5dcaSAlan Stern }
13247cbe5dcaSAlan Stern 
13257cbe5dcaSAlan Stern void usb_hub_release_all_ports(struct usb_device *hdev, void *owner)
13267cbe5dcaSAlan Stern {
13277cbe5dcaSAlan Stern 	int n;
13287cbe5dcaSAlan Stern 	void **powner;
13297cbe5dcaSAlan Stern 
13307cbe5dcaSAlan Stern 	n = find_port_owner(hdev, 1, &powner);
13317cbe5dcaSAlan Stern 	if (n == 0) {
13327cbe5dcaSAlan Stern 		for (; n < hdev->maxchild; (++n, ++powner)) {
13337cbe5dcaSAlan Stern 			if (*powner == owner)
13347cbe5dcaSAlan Stern 				*powner = NULL;
13357cbe5dcaSAlan Stern 		}
13367cbe5dcaSAlan Stern 	}
13377cbe5dcaSAlan Stern }
13387cbe5dcaSAlan Stern 
13397cbe5dcaSAlan Stern /* The caller must hold udev's lock */
13407cbe5dcaSAlan Stern bool usb_device_is_owned(struct usb_device *udev)
13417cbe5dcaSAlan Stern {
13427cbe5dcaSAlan Stern 	struct usb_hub *hub;
13437cbe5dcaSAlan Stern 
13447cbe5dcaSAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
13457cbe5dcaSAlan Stern 		return false;
13467cbe5dcaSAlan Stern 	hub = hdev_to_hub(udev->parent);
13477cbe5dcaSAlan Stern 	return !!hub->port_owners[udev->portnum - 1];
13487cbe5dcaSAlan Stern }
13497cbe5dcaSAlan Stern 
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds static void recursively_mark_NOTATTACHED(struct usb_device *udev)
13521da177e4SLinus Torvalds {
13531da177e4SLinus Torvalds 	int i;
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	for (i = 0; i < udev->maxchild; ++i) {
13561da177e4SLinus Torvalds 		if (udev->children[i])
13571da177e4SLinus Torvalds 			recursively_mark_NOTATTACHED(udev->children[i]);
13581da177e4SLinus Torvalds 	}
135915123006SSarah Sharp 	if (udev->state == USB_STATE_SUSPENDED) {
1360ee49fb5dSAlan Stern 		udev->discon_suspended = 1;
136115123006SSarah Sharp 		udev->active_duration -= jiffies;
136215123006SSarah Sharp 	}
13631da177e4SLinus Torvalds 	udev->state = USB_STATE_NOTATTACHED;
13641da177e4SLinus Torvalds }
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds /**
13671da177e4SLinus Torvalds  * usb_set_device_state - change a device's current state (usbcore, hcds)
13681da177e4SLinus Torvalds  * @udev: pointer to device whose state should be changed
13691da177e4SLinus Torvalds  * @new_state: new state value to be stored
13701da177e4SLinus Torvalds  *
13711da177e4SLinus Torvalds  * udev->state is _not_ fully protected by the device lock.  Although
13721da177e4SLinus Torvalds  * most transitions are made only while holding the lock, the state can
13731da177e4SLinus Torvalds  * can change to USB_STATE_NOTATTACHED at almost any time.  This
13741da177e4SLinus Torvalds  * is so that devices can be marked as disconnected as soon as possible,
13751da177e4SLinus Torvalds  * without having to wait for any semaphores to be released.  As a result,
13761da177e4SLinus Torvalds  * all changes to any device's state must be protected by the
13771da177e4SLinus Torvalds  * device_state_lock spinlock.
13781da177e4SLinus Torvalds  *
13791da177e4SLinus Torvalds  * Once a device has been added to the device tree, all changes to its state
13801da177e4SLinus Torvalds  * should be made using this routine.  The state should _not_ be set directly.
13811da177e4SLinus Torvalds  *
13821da177e4SLinus Torvalds  * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
13831da177e4SLinus Torvalds  * Otherwise udev->state is set to new_state, and if new_state is
13841da177e4SLinus Torvalds  * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
13851da177e4SLinus Torvalds  * to USB_STATE_NOTATTACHED.
13861da177e4SLinus Torvalds  */
13871da177e4SLinus Torvalds void usb_set_device_state(struct usb_device *udev,
13881da177e4SLinus Torvalds 		enum usb_device_state new_state)
13891da177e4SLinus Torvalds {
13901da177e4SLinus Torvalds 	unsigned long flags;
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 	spin_lock_irqsave(&device_state_lock, flags);
13931da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED)
13941da177e4SLinus Torvalds 		;	/* do nothing */
1395b94dc6b5SDavid Brownell 	else if (new_state != USB_STATE_NOTATTACHED) {
1396fb669cc0SDavid Brownell 
1397fb669cc0SDavid Brownell 		/* root hub wakeup capabilities are managed out-of-band
1398fb669cc0SDavid Brownell 		 * and may involve silicon errata ... ignore them here.
1399fb669cc0SDavid Brownell 		 */
1400fb669cc0SDavid Brownell 		if (udev->parent) {
1401645daaabSAlan Stern 			if (udev->state == USB_STATE_SUSPENDED
1402645daaabSAlan Stern 					|| new_state == USB_STATE_SUSPENDED)
1403645daaabSAlan Stern 				;	/* No change to wakeup settings */
1404645daaabSAlan Stern 			else if (new_state == USB_STATE_CONFIGURED)
1405b94dc6b5SDavid Brownell 				device_init_wakeup(&udev->dev,
1406b94dc6b5SDavid Brownell 					(udev->actconfig->desc.bmAttributes
1407b94dc6b5SDavid Brownell 					 & USB_CONFIG_ATT_WAKEUP));
1408645daaabSAlan Stern 			else
1409b94dc6b5SDavid Brownell 				device_init_wakeup(&udev->dev, 0);
1410fb669cc0SDavid Brownell 		}
141115123006SSarah Sharp 		if (udev->state == USB_STATE_SUSPENDED &&
141215123006SSarah Sharp 			new_state != USB_STATE_SUSPENDED)
141315123006SSarah Sharp 			udev->active_duration -= jiffies;
141415123006SSarah Sharp 		else if (new_state == USB_STATE_SUSPENDED &&
141515123006SSarah Sharp 				udev->state != USB_STATE_SUSPENDED)
141615123006SSarah Sharp 			udev->active_duration += jiffies;
1417645daaabSAlan Stern 		udev->state = new_state;
1418b94dc6b5SDavid Brownell 	} else
14191da177e4SLinus Torvalds 		recursively_mark_NOTATTACHED(udev);
14201da177e4SLinus Torvalds 	spin_unlock_irqrestore(&device_state_lock, flags);
14211da177e4SLinus Torvalds }
14226da9c990SDavid Vrabel EXPORT_SYMBOL_GPL(usb_set_device_state);
14231da177e4SLinus Torvalds 
14248af548dcSInaky Perez-Gonzalez /*
14258af548dcSInaky Perez-Gonzalez  * WUSB devices are simple: they have no hubs behind, so the mapping
14268af548dcSInaky Perez-Gonzalez  * device <-> virtual port number becomes 1:1. Why? to simplify the
14278af548dcSInaky Perez-Gonzalez  * life of the device connection logic in
14288af548dcSInaky Perez-Gonzalez  * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
14298af548dcSInaky Perez-Gonzalez  * handshake we need to assign a temporary address in the unauthorized
14308af548dcSInaky Perez-Gonzalez  * space. For simplicity we use the first virtual port number found to
14318af548dcSInaky Perez-Gonzalez  * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
14328af548dcSInaky Perez-Gonzalez  * and that becomes it's address [X < 128] or its unauthorized address
14338af548dcSInaky Perez-Gonzalez  * [X | 0x80].
14348af548dcSInaky Perez-Gonzalez  *
14358af548dcSInaky Perez-Gonzalez  * We add 1 as an offset to the one-based USB-stack port number
14368af548dcSInaky Perez-Gonzalez  * (zero-based wusb virtual port index) for two reasons: (a) dev addr
14378af548dcSInaky Perez-Gonzalez  * 0 is reserved by USB for default address; (b) Linux's USB stack
14388af548dcSInaky Perez-Gonzalez  * uses always #1 for the root hub of the controller. So USB stack's
14398af548dcSInaky Perez-Gonzalez  * port #1, which is wusb virtual-port #0 has address #2.
1440c6515272SSarah Sharp  *
1441c6515272SSarah Sharp  * Devices connected under xHCI are not as simple.  The host controller
1442c6515272SSarah Sharp  * supports virtualization, so the hardware assigns device addresses and
1443c6515272SSarah Sharp  * the HCD must setup data structures before issuing a set address
1444c6515272SSarah Sharp  * command to the hardware.
14458af548dcSInaky Perez-Gonzalez  */
14461da177e4SLinus Torvalds static void choose_address(struct usb_device *udev)
14471da177e4SLinus Torvalds {
14481da177e4SLinus Torvalds 	int		devnum;
14491da177e4SLinus Torvalds 	struct usb_bus	*bus = udev->bus;
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 	/* If khubd ever becomes multithreaded, this will need a lock */
14528af548dcSInaky Perez-Gonzalez 	if (udev->wusb) {
14538af548dcSInaky Perez-Gonzalez 		devnum = udev->portnum + 1;
14548af548dcSInaky Perez-Gonzalez 		BUG_ON(test_bit(devnum, bus->devmap.devicemap));
14558af548dcSInaky Perez-Gonzalez 	} else {
14568af548dcSInaky Perez-Gonzalez 		/* Try to allocate the next devnum beginning at
14578af548dcSInaky Perez-Gonzalez 		 * bus->devnum_next. */
14581da177e4SLinus Torvalds 		devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
14591da177e4SLinus Torvalds 					    bus->devnum_next);
14601da177e4SLinus Torvalds 		if (devnum >= 128)
14618af548dcSInaky Perez-Gonzalez 			devnum = find_next_zero_bit(bus->devmap.devicemap,
14628af548dcSInaky Perez-Gonzalez 						    128, 1);
14631da177e4SLinus Torvalds 		bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
14648af548dcSInaky Perez-Gonzalez 	}
14651da177e4SLinus Torvalds 	if (devnum < 128) {
14661da177e4SLinus Torvalds 		set_bit(devnum, bus->devmap.devicemap);
14671da177e4SLinus Torvalds 		udev->devnum = devnum;
14681da177e4SLinus Torvalds 	}
14691da177e4SLinus Torvalds }
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds static void release_address(struct usb_device *udev)
14721da177e4SLinus Torvalds {
14731da177e4SLinus Torvalds 	if (udev->devnum > 0) {
14741da177e4SLinus Torvalds 		clear_bit(udev->devnum, udev->bus->devmap.devicemap);
14751da177e4SLinus Torvalds 		udev->devnum = -1;
14761da177e4SLinus Torvalds 	}
14771da177e4SLinus Torvalds }
14781da177e4SLinus Torvalds 
14794953d141SDavid Vrabel static void update_address(struct usb_device *udev, int devnum)
14804953d141SDavid Vrabel {
14814953d141SDavid Vrabel 	/* The address for a WUSB device is managed by wusbcore. */
14824953d141SDavid Vrabel 	if (!udev->wusb)
14834953d141SDavid Vrabel 		udev->devnum = devnum;
14844953d141SDavid Vrabel }
14854953d141SDavid Vrabel 
1486d5d4db70SAlan Stern #ifdef	CONFIG_USB_SUSPEND
1487d5d4db70SAlan Stern 
1488d5d4db70SAlan Stern static void usb_stop_pm(struct usb_device *udev)
1489d5d4db70SAlan Stern {
1490d5d4db70SAlan Stern 	/* Synchronize with the ksuspend thread to prevent any more
1491d5d4db70SAlan Stern 	 * autosuspend requests from being submitted, and decrement
1492d5d4db70SAlan Stern 	 * the parent's count of unsuspended children.
1493d5d4db70SAlan Stern 	 */
1494d5d4db70SAlan Stern 	usb_pm_lock(udev);
1495d5d4db70SAlan Stern 	if (udev->parent && !udev->discon_suspended)
1496d5d4db70SAlan Stern 		usb_autosuspend_device(udev->parent);
1497d5d4db70SAlan Stern 	usb_pm_unlock(udev);
1498d5d4db70SAlan Stern 
14999ac39f28SAlan Stern 	/* Stop any autosuspend or autoresume requests already submitted */
15009ac39f28SAlan Stern 	cancel_delayed_work_sync(&udev->autosuspend);
15019ac39f28SAlan Stern 	cancel_work_sync(&udev->autoresume);
1502d5d4db70SAlan Stern }
1503d5d4db70SAlan Stern 
1504d5d4db70SAlan Stern #else
1505d5d4db70SAlan Stern 
1506d5d4db70SAlan Stern static inline void usb_stop_pm(struct usb_device *udev)
1507d5d4db70SAlan Stern { }
1508d5d4db70SAlan Stern 
1509d5d4db70SAlan Stern #endif
1510d5d4db70SAlan Stern 
15111da177e4SLinus Torvalds /**
15121da177e4SLinus Torvalds  * usb_disconnect - disconnect a device (usbcore-internal)
15131da177e4SLinus Torvalds  * @pdev: pointer to device being disconnected
15141da177e4SLinus Torvalds  * Context: !in_interrupt ()
15151da177e4SLinus Torvalds  *
15161da177e4SLinus Torvalds  * Something got disconnected. Get rid of it and all of its children.
15171da177e4SLinus Torvalds  *
15181da177e4SLinus Torvalds  * If *pdev is a normal device then the parent hub must already be locked.
15191da177e4SLinus Torvalds  * If *pdev is a root hub then this routine will acquire the
15201da177e4SLinus Torvalds  * usb_bus_list_lock on behalf of the caller.
15211da177e4SLinus Torvalds  *
15221da177e4SLinus Torvalds  * Only hub drivers (including virtual root hub drivers for host
15231da177e4SLinus Torvalds  * controllers) should ever call this.
15241da177e4SLinus Torvalds  *
15251da177e4SLinus Torvalds  * This call is synchronous, and may not be used in an interrupt context.
15261da177e4SLinus Torvalds  */
15271da177e4SLinus Torvalds void usb_disconnect(struct usb_device **pdev)
15281da177e4SLinus Torvalds {
15291da177e4SLinus Torvalds 	struct usb_device	*udev = *pdev;
15301da177e4SLinus Torvalds 	int			i;
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds 	if (!udev) {
1533441b62c1SHarvey Harrison 		pr_debug ("%s nodev\n", __func__);
15341da177e4SLinus Torvalds 		return;
15351da177e4SLinus Torvalds 	}
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 	/* mark the device as inactive, so any further urb submissions for
15381da177e4SLinus Torvalds 	 * this device (and any of its children) will fail immediately.
15391da177e4SLinus Torvalds 	 * this quiesces everyting except pending urbs.
15401da177e4SLinus Torvalds 	 */
15411da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
15421da177e4SLinus Torvalds 	dev_info (&udev->dev, "USB disconnect, address %d\n", udev->devnum);
15431da177e4SLinus Torvalds 
15449ad3d6ccSAlan Stern 	usb_lock_device(udev);
15459ad3d6ccSAlan Stern 
15461da177e4SLinus Torvalds 	/* Free up all the children before we remove this device */
15471da177e4SLinus Torvalds 	for (i = 0; i < USB_MAXCHILDREN; i++) {
15481da177e4SLinus Torvalds 		if (udev->children[i])
15491da177e4SLinus Torvalds 			usb_disconnect(&udev->children[i]);
15501da177e4SLinus Torvalds 	}
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	/* deallocate hcd/hardware state ... nuking all pending urbs and
15531da177e4SLinus Torvalds 	 * cleaning up all state associated with the current configuration
15541da177e4SLinus Torvalds 	 * so that the hardware is now fully quiesced.
15551da177e4SLinus Torvalds 	 */
1556782da727SAlan Stern 	dev_dbg (&udev->dev, "unregistering device\n");
15571da177e4SLinus Torvalds 	usb_disable_device(udev, 0);
1558cde217a5SAlan Stern 	usb_hcd_synchronize_unlinks(udev);
15591da177e4SLinus Torvalds 
15603b23dd6fSAlan Stern 	usb_remove_ep_devs(&udev->ep0);
1561782da727SAlan Stern 	usb_unlock_device(udev);
15623099e75aSGreg Kroah-Hartman 
1563782da727SAlan Stern 	/* Unregister the device.  The device driver is responsible
15643b23dd6fSAlan Stern 	 * for de-configuring the device and invoking the remove-device
15653b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
1566782da727SAlan Stern 	 */
1567782da727SAlan Stern 	device_del(&udev->dev);
1568782da727SAlan Stern 
1569782da727SAlan Stern 	/* Free the device number and delete the parent's children[]
15701da177e4SLinus Torvalds 	 * (or root_hub) pointer.
15711da177e4SLinus Torvalds 	 */
15721da177e4SLinus Torvalds 	release_address(udev);
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	/* Avoid races with recursively_mark_NOTATTACHED() */
15751da177e4SLinus Torvalds 	spin_lock_irq(&device_state_lock);
15761da177e4SLinus Torvalds 	*pdev = NULL;
15771da177e4SLinus Torvalds 	spin_unlock_irq(&device_state_lock);
15781da177e4SLinus Torvalds 
1579d5d4db70SAlan Stern 	usb_stop_pm(udev);
1580ee49fb5dSAlan Stern 
1581782da727SAlan Stern 	put_device(&udev->dev);
15821da177e4SLinus Torvalds }
15831da177e4SLinus Torvalds 
1584f2a383e4SGreg Kroah-Hartman #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
15851da177e4SLinus Torvalds static void show_string(struct usb_device *udev, char *id, char *string)
15861da177e4SLinus Torvalds {
15871da177e4SLinus Torvalds 	if (!string)
15881da177e4SLinus Torvalds 		return;
15891da177e4SLinus Torvalds 	dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
15901da177e4SLinus Torvalds }
15911da177e4SLinus Torvalds 
1592f2a383e4SGreg Kroah-Hartman static void announce_device(struct usb_device *udev)
1593f2a383e4SGreg Kroah-Hartman {
1594f2a383e4SGreg Kroah-Hartman 	dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
1595f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idVendor),
1596f2a383e4SGreg Kroah-Hartman 		le16_to_cpu(udev->descriptor.idProduct));
1597b9cef6c3SWu Fengguang 	dev_info(&udev->dev,
1598b9cef6c3SWu Fengguang 		"New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
1599f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iManufacturer,
1600f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iProduct,
1601f2a383e4SGreg Kroah-Hartman 		udev->descriptor.iSerialNumber);
1602f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Product", udev->product);
1603f2a383e4SGreg Kroah-Hartman 	show_string(udev, "Manufacturer", udev->manufacturer);
1604f2a383e4SGreg Kroah-Hartman 	show_string(udev, "SerialNumber", udev->serial);
1605f2a383e4SGreg Kroah-Hartman }
16061da177e4SLinus Torvalds #else
1607f2a383e4SGreg Kroah-Hartman static inline void announce_device(struct usb_device *udev) { }
16081da177e4SLinus Torvalds #endif
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
16111da177e4SLinus Torvalds #include "otg_whitelist.h"
16121da177e4SLinus Torvalds #endif
16131da177e4SLinus Torvalds 
16143ede760fSOliver Neukum /**
1615d9d16e8aSInaky Perez-Gonzalez  * usb_configure_device_otg - FIXME (usbcore-internal)
16163ede760fSOliver Neukum  * @udev: newly addressed device (in ADDRESS state)
16173ede760fSOliver Neukum  *
1618d9d16e8aSInaky Perez-Gonzalez  * Do configuration for On-The-Go devices
16193ede760fSOliver Neukum  */
1620d9d16e8aSInaky Perez-Gonzalez static int usb_configure_device_otg(struct usb_device *udev)
16211da177e4SLinus Torvalds {
1622d9d16e8aSInaky Perez-Gonzalez 	int err = 0;
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
16251da177e4SLinus Torvalds 	/*
16261da177e4SLinus Torvalds 	 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
16271da177e4SLinus Torvalds 	 * to wake us after we've powered off VBUS; and HNP, switching roles
16281da177e4SLinus Torvalds 	 * "host" to "peripheral".  The OTG descriptor helps figure this out.
16291da177e4SLinus Torvalds 	 */
16301da177e4SLinus Torvalds 	if (!udev->bus->is_b_host
16311da177e4SLinus Torvalds 			&& udev->config
16321da177e4SLinus Torvalds 			&& udev->parent == udev->bus->root_hub) {
16331da177e4SLinus Torvalds 		struct usb_otg_descriptor	*desc = 0;
16341da177e4SLinus Torvalds 		struct usb_bus			*bus = udev->bus;
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds 		/* descriptor may appear anywhere in config */
16371da177e4SLinus Torvalds 		if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
16381da177e4SLinus Torvalds 					le16_to_cpu(udev->config[0].desc.wTotalLength),
16391da177e4SLinus Torvalds 					USB_DT_OTG, (void **) &desc) == 0) {
16401da177e4SLinus Torvalds 			if (desc->bmAttributes & USB_OTG_HNP) {
164112c3da34SAlan Stern 				unsigned		port1 = udev->portnum;
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 				dev_info(&udev->dev,
16441da177e4SLinus Torvalds 					"Dual-Role OTG device on %sHNP port\n",
16451da177e4SLinus Torvalds 					(port1 == bus->otg_port)
16461da177e4SLinus Torvalds 						? "" : "non-");
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 				/* enable HNP before suspend, it's simpler */
16491da177e4SLinus Torvalds 				if (port1 == bus->otg_port)
16501da177e4SLinus Torvalds 					bus->b_hnp_enable = 1;
16511da177e4SLinus Torvalds 				err = usb_control_msg(udev,
16521da177e4SLinus Torvalds 					usb_sndctrlpipe(udev, 0),
16531da177e4SLinus Torvalds 					USB_REQ_SET_FEATURE, 0,
16541da177e4SLinus Torvalds 					bus->b_hnp_enable
16551da177e4SLinus Torvalds 						? USB_DEVICE_B_HNP_ENABLE
16561da177e4SLinus Torvalds 						: USB_DEVICE_A_ALT_HNP_SUPPORT,
16571da177e4SLinus Torvalds 					0, NULL, 0, USB_CTRL_SET_TIMEOUT);
16581da177e4SLinus Torvalds 				if (err < 0) {
16591da177e4SLinus Torvalds 					/* OTG MESSAGE: report errors here,
16601da177e4SLinus Torvalds 					 * customize to match your product.
16611da177e4SLinus Torvalds 					 */
16621da177e4SLinus Torvalds 					dev_info(&udev->dev,
1663b9cef6c3SWu Fengguang 						"can't set HNP mode: %d\n",
16641da177e4SLinus Torvalds 						err);
16651da177e4SLinus Torvalds 					bus->b_hnp_enable = 0;
16661da177e4SLinus Torvalds 				}
16671da177e4SLinus Torvalds 			}
16681da177e4SLinus Torvalds 		}
16691da177e4SLinus Torvalds 	}
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	if (!is_targeted(udev)) {
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 		/* Maybe it can talk to us, though we can't talk to it.
16741da177e4SLinus Torvalds 		 * (Includes HNP test device.)
16751da177e4SLinus Torvalds 		 */
16761da177e4SLinus Torvalds 		if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
1677634a84f8SDavid Brownell 			err = usb_port_suspend(udev, PMSG_SUSPEND);
16781da177e4SLinus Torvalds 			if (err < 0)
16791da177e4SLinus Torvalds 				dev_dbg(&udev->dev, "HNP fail, %d\n", err);
16801da177e4SLinus Torvalds 		}
1681ffcdc18dSVikram Pandita 		err = -ENOTSUPP;
16821da177e4SLinus Torvalds 		goto fail;
16831da177e4SLinus Torvalds 	}
1684d9d16e8aSInaky Perez-Gonzalez fail:
16851da177e4SLinus Torvalds #endif
1686d9d16e8aSInaky Perez-Gonzalez 	return err;
1687d9d16e8aSInaky Perez-Gonzalez }
16881da177e4SLinus Torvalds 
1689d9d16e8aSInaky Perez-Gonzalez 
1690d9d16e8aSInaky Perez-Gonzalez /**
1691d9d16e8aSInaky Perez-Gonzalez  * usb_configure_device - Detect and probe device intfs/otg (usbcore-internal)
1692d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
1693d9d16e8aSInaky Perez-Gonzalez  *
1694d9d16e8aSInaky Perez-Gonzalez  * This is only called by usb_new_device() and usb_authorize_device()
1695d9d16e8aSInaky Perez-Gonzalez  * and FIXME -- all comments that apply to them apply here wrt to
1696d9d16e8aSInaky Perez-Gonzalez  * environment.
1697d9d16e8aSInaky Perez-Gonzalez  *
1698d9d16e8aSInaky Perez-Gonzalez  * If the device is WUSB and not authorized, we don't attempt to read
1699d9d16e8aSInaky Perez-Gonzalez  * the string descriptors, as they will be errored out by the device
1700d9d16e8aSInaky Perez-Gonzalez  * until it has been authorized.
1701d9d16e8aSInaky Perez-Gonzalez  */
1702d9d16e8aSInaky Perez-Gonzalez static int usb_configure_device(struct usb_device *udev)
1703d9d16e8aSInaky Perez-Gonzalez {
1704d9d16e8aSInaky Perez-Gonzalez 	int err;
1705d9d16e8aSInaky Perez-Gonzalez 
1706d9d16e8aSInaky Perez-Gonzalez 	if (udev->config == NULL) {
1707d9d16e8aSInaky Perez-Gonzalez 		err = usb_get_configuration(udev);
1708d9d16e8aSInaky Perez-Gonzalez 		if (err < 0) {
1709d9d16e8aSInaky Perez-Gonzalez 			dev_err(&udev->dev, "can't read configurations, error %d\n",
1710d9d16e8aSInaky Perez-Gonzalez 				err);
1711d9d16e8aSInaky Perez-Gonzalez 			goto fail;
1712d9d16e8aSInaky Perez-Gonzalez 		}
1713d9d16e8aSInaky Perez-Gonzalez 	}
1714d9d16e8aSInaky Perez-Gonzalez 	if (udev->wusb == 1 && udev->authorized == 0) {
1715d9d16e8aSInaky Perez-Gonzalez 		udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1716d9d16e8aSInaky Perez-Gonzalez 		udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1717d9d16e8aSInaky Perez-Gonzalez 		udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1718d9d16e8aSInaky Perez-Gonzalez 	}
1719d9d16e8aSInaky Perez-Gonzalez 	else {
1720d9d16e8aSInaky Perez-Gonzalez 		/* read the standard strings and cache them if present */
1721d9d16e8aSInaky Perez-Gonzalez 		udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
1722d9d16e8aSInaky Perez-Gonzalez 		udev->manufacturer = usb_cache_string(udev,
1723d9d16e8aSInaky Perez-Gonzalez 						      udev->descriptor.iManufacturer);
1724d9d16e8aSInaky Perez-Gonzalez 		udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
1725d9d16e8aSInaky Perez-Gonzalez 	}
1726d9d16e8aSInaky Perez-Gonzalez 	err = usb_configure_device_otg(udev);
1727d9d16e8aSInaky Perez-Gonzalez fail:
1728d9d16e8aSInaky Perez-Gonzalez 	return err;
1729d9d16e8aSInaky Perez-Gonzalez }
1730d9d16e8aSInaky Perez-Gonzalez 
1731d9d16e8aSInaky Perez-Gonzalez 
1732d9d16e8aSInaky Perez-Gonzalez /**
1733d9d16e8aSInaky Perez-Gonzalez  * usb_new_device - perform initial device setup (usbcore-internal)
1734d9d16e8aSInaky Perez-Gonzalez  * @udev: newly addressed device (in ADDRESS state)
1735d9d16e8aSInaky Perez-Gonzalez  *
1736d9d16e8aSInaky Perez-Gonzalez  * This is called with devices which have been enumerated, but not yet
1737d9d16e8aSInaky Perez-Gonzalez  * configured.  The device descriptor is available, but not descriptors
1738d9d16e8aSInaky Perez-Gonzalez  * for any device configuration.  The caller must have locked either
1739d9d16e8aSInaky Perez-Gonzalez  * the parent hub (if udev is a normal device) or else the
1740d9d16e8aSInaky Perez-Gonzalez  * usb_bus_list_lock (if udev is a root hub).  The parent's pointer to
1741d9d16e8aSInaky Perez-Gonzalez  * udev has already been installed, but udev is not yet visible through
1742d9d16e8aSInaky Perez-Gonzalez  * sysfs or other filesystem code.
1743d9d16e8aSInaky Perez-Gonzalez  *
1744d9d16e8aSInaky Perez-Gonzalez  * It will return if the device is configured properly or not.  Zero if
1745d9d16e8aSInaky Perez-Gonzalez  * the interface was registered with the driver core; else a negative
1746d9d16e8aSInaky Perez-Gonzalez  * errno value.
1747d9d16e8aSInaky Perez-Gonzalez  *
1748d9d16e8aSInaky Perez-Gonzalez  * This call is synchronous, and may not be used in an interrupt context.
1749d9d16e8aSInaky Perez-Gonzalez  *
1750d9d16e8aSInaky Perez-Gonzalez  * Only the hub driver or root-hub registrar should ever call this.
1751d9d16e8aSInaky Perez-Gonzalez  */
1752d9d16e8aSInaky Perez-Gonzalez int usb_new_device(struct usb_device *udev)
1753d9d16e8aSInaky Perez-Gonzalez {
1754d9d16e8aSInaky Perez-Gonzalez 	int err;
1755d9d16e8aSInaky Perez-Gonzalez 
17566cd13201SAlan Stern 	/* Increment the parent's count of unsuspended children */
17576cd13201SAlan Stern 	if (udev->parent)
17586cd13201SAlan Stern 		usb_autoresume_device(udev->parent);
17596cd13201SAlan Stern 
1760d9d16e8aSInaky Perez-Gonzalez 	usb_detect_quirks(udev);		/* Determine quirks */
1761d9d16e8aSInaky Perez-Gonzalez 	err = usb_configure_device(udev);	/* detect & probe dev/intfs */
1762d9d16e8aSInaky Perez-Gonzalez 	if (err < 0)
1763d9d16e8aSInaky Perez-Gonzalez 		goto fail;
1764c6515272SSarah Sharp 	dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
1765c6515272SSarah Sharp 			udev->devnum, udev->bus->busnum,
1766c6515272SSarah Sharp 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
17679f8b17e6SKay Sievers 	/* export the usbdev device-node for libusb */
17689f8b17e6SKay Sievers 	udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
17699f8b17e6SKay Sievers 			(((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
17709f8b17e6SKay Sievers 
17716cd13201SAlan Stern 	/* Tell the world! */
17726cd13201SAlan Stern 	announce_device(udev);
1773195af2ccSAlan Stern 
1774782da727SAlan Stern 	/* Register the device.  The device driver is responsible
17753b23dd6fSAlan Stern 	 * for configuring the device and invoking the add-device
17763b23dd6fSAlan Stern 	 * notifier chain (used by usbfs and possibly others).
1777782da727SAlan Stern 	 */
17781da177e4SLinus Torvalds 	err = device_add(&udev->dev);
17791da177e4SLinus Torvalds 	if (err) {
17801da177e4SLinus Torvalds 		dev_err(&udev->dev, "can't device_add, error %d\n", err);
17811da177e4SLinus Torvalds 		goto fail;
17821da177e4SLinus Torvalds 	}
17839ad3d6ccSAlan Stern 
17843b23dd6fSAlan Stern 	(void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
1785c066475eSGreg Kroah-Hartman 	return err;
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds fail:
17881da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_NOTATTACHED);
17896cd13201SAlan Stern 	usb_stop_pm(udev);
1790d9d16e8aSInaky Perez-Gonzalez 	return err;
17911da177e4SLinus Torvalds }
17921da177e4SLinus Torvalds 
179393993a0aSInaky Perez-Gonzalez 
179493993a0aSInaky Perez-Gonzalez /**
1795fd39c86bSRandy Dunlap  * usb_deauthorize_device - deauthorize a device (usbcore-internal)
1796fd39c86bSRandy Dunlap  * @usb_dev: USB device
1797fd39c86bSRandy Dunlap  *
1798fd39c86bSRandy Dunlap  * Move the USB device to a very basic state where interfaces are disabled
1799fd39c86bSRandy Dunlap  * and the device is in fact unconfigured and unusable.
180093993a0aSInaky Perez-Gonzalez  *
180193993a0aSInaky Perez-Gonzalez  * We share a lock (that we have) with device_del(), so we need to
180293993a0aSInaky Perez-Gonzalez  * defer its call.
180393993a0aSInaky Perez-Gonzalez  */
180493993a0aSInaky Perez-Gonzalez int usb_deauthorize_device(struct usb_device *usb_dev)
180593993a0aSInaky Perez-Gonzalez {
180693993a0aSInaky Perez-Gonzalez 	unsigned cnt;
180793993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
180893993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 0)
180993993a0aSInaky Perez-Gonzalez 		goto out_unauthorized;
181093993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 0;
181193993a0aSInaky Perez-Gonzalez 	usb_set_configuration(usb_dev, -1);
181293993a0aSInaky Perez-Gonzalez 	usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
181393993a0aSInaky Perez-Gonzalez 	usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
181493993a0aSInaky Perez-Gonzalez 	usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
181593993a0aSInaky Perez-Gonzalez 	kfree(usb_dev->config);
181693993a0aSInaky Perez-Gonzalez 	usb_dev->config = NULL;
181793993a0aSInaky Perez-Gonzalez 	for (cnt = 0; cnt < usb_dev->descriptor.bNumConfigurations; cnt++)
181893993a0aSInaky Perez-Gonzalez 		kfree(usb_dev->rawdescriptors[cnt]);
181993993a0aSInaky Perez-Gonzalez 	usb_dev->descriptor.bNumConfigurations = 0;
182093993a0aSInaky Perez-Gonzalez 	kfree(usb_dev->rawdescriptors);
182193993a0aSInaky Perez-Gonzalez out_unauthorized:
182293993a0aSInaky Perez-Gonzalez 	usb_unlock_device(usb_dev);
182393993a0aSInaky Perez-Gonzalez 	return 0;
182493993a0aSInaky Perez-Gonzalez }
182593993a0aSInaky Perez-Gonzalez 
182693993a0aSInaky Perez-Gonzalez 
182793993a0aSInaky Perez-Gonzalez int usb_authorize_device(struct usb_device *usb_dev)
182893993a0aSInaky Perez-Gonzalez {
182993993a0aSInaky Perez-Gonzalez 	int result = 0, c;
183093993a0aSInaky Perez-Gonzalez 	usb_lock_device(usb_dev);
183193993a0aSInaky Perez-Gonzalez 	if (usb_dev->authorized == 1)
183293993a0aSInaky Perez-Gonzalez 		goto out_authorized;
183393993a0aSInaky Perez-Gonzalez 	kfree(usb_dev->product);
183493993a0aSInaky Perez-Gonzalez 	usb_dev->product = NULL;
183593993a0aSInaky Perez-Gonzalez 	kfree(usb_dev->manufacturer);
183693993a0aSInaky Perez-Gonzalez 	usb_dev->manufacturer = NULL;
183793993a0aSInaky Perez-Gonzalez 	kfree(usb_dev->serial);
183893993a0aSInaky Perez-Gonzalez 	usb_dev->serial = NULL;
183993993a0aSInaky Perez-Gonzalez 	result = usb_autoresume_device(usb_dev);
184093993a0aSInaky Perez-Gonzalez 	if (result < 0) {
184193993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev,
184293993a0aSInaky Perez-Gonzalez 			"can't autoresume for authorization: %d\n", result);
184393993a0aSInaky Perez-Gonzalez 		goto error_autoresume;
184493993a0aSInaky Perez-Gonzalez 	}
184593993a0aSInaky Perez-Gonzalez 	result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
184693993a0aSInaky Perez-Gonzalez 	if (result < 0) {
184793993a0aSInaky Perez-Gonzalez 		dev_err(&usb_dev->dev, "can't re-read device descriptor for "
184893993a0aSInaky Perez-Gonzalez 			"authorization: %d\n", result);
184993993a0aSInaky Perez-Gonzalez 		goto error_device_descriptor;
185093993a0aSInaky Perez-Gonzalez 	}
185193993a0aSInaky Perez-Gonzalez 	usb_dev->authorized = 1;
185293993a0aSInaky Perez-Gonzalez 	result = usb_configure_device(usb_dev);
185393993a0aSInaky Perez-Gonzalez 	if (result < 0)
185493993a0aSInaky Perez-Gonzalez 		goto error_configure;
185593993a0aSInaky Perez-Gonzalez 	/* Choose and set the configuration.  This registers the interfaces
185693993a0aSInaky Perez-Gonzalez 	 * with the driver core and lets interface drivers bind to them.
185793993a0aSInaky Perez-Gonzalez 	 */
1858b5ea060fSGreg Kroah-Hartman 	c = usb_choose_configuration(usb_dev);
185993993a0aSInaky Perez-Gonzalez 	if (c >= 0) {
186093993a0aSInaky Perez-Gonzalez 		result = usb_set_configuration(usb_dev, c);
186193993a0aSInaky Perez-Gonzalez 		if (result) {
186293993a0aSInaky Perez-Gonzalez 			dev_err(&usb_dev->dev,
186393993a0aSInaky Perez-Gonzalez 				"can't set config #%d, error %d\n", c, result);
186493993a0aSInaky Perez-Gonzalez 			/* This need not be fatal.  The user can try to
186593993a0aSInaky Perez-Gonzalez 			 * set other configurations. */
186693993a0aSInaky Perez-Gonzalez 		}
186793993a0aSInaky Perez-Gonzalez 	}
186893993a0aSInaky Perez-Gonzalez 	dev_info(&usb_dev->dev, "authorized to connect\n");
186993993a0aSInaky Perez-Gonzalez error_configure:
187093993a0aSInaky Perez-Gonzalez error_device_descriptor:
187193993a0aSInaky Perez-Gonzalez error_autoresume:
187293993a0aSInaky Perez-Gonzalez out_authorized:
187393993a0aSInaky Perez-Gonzalez 	usb_unlock_device(usb_dev);	// complements locktree
187493993a0aSInaky Perez-Gonzalez 	return result;
187593993a0aSInaky Perez-Gonzalez }
187693993a0aSInaky Perez-Gonzalez 
187793993a0aSInaky Perez-Gonzalez 
18780165de09SInaky Perez-Gonzalez /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
18790165de09SInaky Perez-Gonzalez static unsigned hub_is_wusb(struct usb_hub *hub)
18800165de09SInaky Perez-Gonzalez {
18810165de09SInaky Perez-Gonzalez 	struct usb_hcd *hcd;
18820165de09SInaky Perez-Gonzalez 	if (hub->hdev->parent != NULL)  /* not a root hub? */
18830165de09SInaky Perez-Gonzalez 		return 0;
18840165de09SInaky Perez-Gonzalez 	hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
18850165de09SInaky Perez-Gonzalez 	return hcd->wireless;
18860165de09SInaky Perez-Gonzalez }
18870165de09SInaky Perez-Gonzalez 
18880165de09SInaky Perez-Gonzalez 
18891da177e4SLinus Torvalds #define PORT_RESET_TRIES	5
18901da177e4SLinus Torvalds #define SET_ADDRESS_TRIES	2
18911da177e4SLinus Torvalds #define GET_DESCRIPTOR_TRIES	2
18921da177e4SLinus Torvalds #define SET_CONFIG_TRIES	(2 * (use_both_schemes + 1))
18931da177e4SLinus Torvalds #define USE_NEW_SCHEME(i)	((i) / 2 == old_scheme_first)
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds #define HUB_ROOT_RESET_TIME	50	/* times are in msec */
18961da177e4SLinus Torvalds #define HUB_SHORT_RESET_TIME	10
18971da177e4SLinus Torvalds #define HUB_LONG_RESET_TIME	200
18981da177e4SLinus Torvalds #define HUB_RESET_TIMEOUT	500
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds static int hub_port_wait_reset(struct usb_hub *hub, int port1,
19011da177e4SLinus Torvalds 				struct usb_device *udev, unsigned int delay)
19021da177e4SLinus Torvalds {
19031da177e4SLinus Torvalds 	int delay_time, ret;
19041da177e4SLinus Torvalds 	u16 portstatus;
19051da177e4SLinus Torvalds 	u16 portchange;
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 	for (delay_time = 0;
19081da177e4SLinus Torvalds 			delay_time < HUB_RESET_TIMEOUT;
19091da177e4SLinus Torvalds 			delay_time += delay) {
19101da177e4SLinus Torvalds 		/* wait to give the device a chance to reset */
19111da177e4SLinus Torvalds 		msleep(delay);
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 		/* read and decode port status */
19141da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
19151da177e4SLinus Torvalds 		if (ret < 0)
19161da177e4SLinus Torvalds 			return ret;
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 		/* Device went away? */
19191da177e4SLinus Torvalds 		if (!(portstatus & USB_PORT_STAT_CONNECTION))
19201da177e4SLinus Torvalds 			return -ENOTCONN;
19211da177e4SLinus Torvalds 
1922dd4dd19eSAlan Stern 		/* bomb out completely if the connection bounced */
19231da177e4SLinus Torvalds 		if ((portchange & USB_PORT_STAT_C_CONNECTION))
1924dd4dd19eSAlan Stern 			return -ENOTCONN;
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 		/* if we`ve finished resetting, then break out of the loop */
19271da177e4SLinus Torvalds 		if (!(portstatus & USB_PORT_STAT_RESET) &&
19281da177e4SLinus Torvalds 		    (portstatus & USB_PORT_STAT_ENABLE)) {
19290165de09SInaky Perez-Gonzalez 			if (hub_is_wusb(hub))
19300165de09SInaky Perez-Gonzalez 				udev->speed = USB_SPEED_VARIABLE;
19310165de09SInaky Perez-Gonzalez 			else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
19321da177e4SLinus Torvalds 				udev->speed = USB_SPEED_HIGH;
19331da177e4SLinus Torvalds 			else if (portstatus & USB_PORT_STAT_LOW_SPEED)
19341da177e4SLinus Torvalds 				udev->speed = USB_SPEED_LOW;
19351da177e4SLinus Torvalds 			else
19361da177e4SLinus Torvalds 				udev->speed = USB_SPEED_FULL;
19371da177e4SLinus Torvalds 			return 0;
19381da177e4SLinus Torvalds 		}
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds 		/* switch to the long delay after two short delay failures */
19411da177e4SLinus Torvalds 		if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
19421da177e4SLinus Torvalds 			delay = HUB_LONG_RESET_TIME;
19431da177e4SLinus Torvalds 
19441da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
19451da177e4SLinus Torvalds 			"port %d not reset yet, waiting %dms\n",
19461da177e4SLinus Torvalds 			port1, delay);
19471da177e4SLinus Torvalds 	}
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds 	return -EBUSY;
19501da177e4SLinus Torvalds }
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds static int hub_port_reset(struct usb_hub *hub, int port1,
19531da177e4SLinus Torvalds 				struct usb_device *udev, unsigned int delay)
19541da177e4SLinus Torvalds {
19551da177e4SLinus Torvalds 	int i, status;
19561da177e4SLinus Torvalds 
195732fe0198SAlan Stern 	/* Block EHCI CF initialization during the port reset.
195832fe0198SAlan Stern 	 * Some companion controllers don't like it when they mix.
195932fe0198SAlan Stern 	 */
196032fe0198SAlan Stern 	down_read(&ehci_cf_port_reset_rwsem);
196132fe0198SAlan Stern 
19621da177e4SLinus Torvalds 	/* Reset the port */
19631da177e4SLinus Torvalds 	for (i = 0; i < PORT_RESET_TRIES; i++) {
19641da177e4SLinus Torvalds 		status = set_port_feature(hub->hdev,
19651da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_RESET);
19661da177e4SLinus Torvalds 		if (status)
19671da177e4SLinus Torvalds 			dev_err(hub->intfdev,
19681da177e4SLinus Torvalds 					"cannot reset port %d (err = %d)\n",
19691da177e4SLinus Torvalds 					port1, status);
19701da177e4SLinus Torvalds 		else {
19711da177e4SLinus Torvalds 			status = hub_port_wait_reset(hub, port1, udev, delay);
1972dd16525bSDavid Brownell 			if (status && status != -ENOTCONN)
19731da177e4SLinus Torvalds 				dev_dbg(hub->intfdev,
19741da177e4SLinus Torvalds 						"port_wait_reset: err = %d\n",
19751da177e4SLinus Torvalds 						status);
19761da177e4SLinus Torvalds 		}
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds 		/* return on disconnect or reset */
19791da177e4SLinus Torvalds 		switch (status) {
19801da177e4SLinus Torvalds 		case 0:
1981b789696aSDavid Brownell 			/* TRSTRCY = 10 ms; plus some extra */
1982b789696aSDavid Brownell 			msleep(10 + 40);
19834953d141SDavid Vrabel 			update_address(udev, 0);
19841da177e4SLinus Torvalds 			/* FALL THROUGH */
19851da177e4SLinus Torvalds 		case -ENOTCONN:
19861da177e4SLinus Torvalds 		case -ENODEV:
19871da177e4SLinus Torvalds 			clear_port_feature(hub->hdev,
19881da177e4SLinus Torvalds 				port1, USB_PORT_FEAT_C_RESET);
19891da177e4SLinus Torvalds 			/* FIXME need disconnect() for NOTATTACHED device */
19901da177e4SLinus Torvalds 			usb_set_device_state(udev, status
19911da177e4SLinus Torvalds 					? USB_STATE_NOTATTACHED
19921da177e4SLinus Torvalds 					: USB_STATE_DEFAULT);
199332fe0198SAlan Stern 			goto done;
19941da177e4SLinus Torvalds 		}
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 		dev_dbg (hub->intfdev,
19971da177e4SLinus Torvalds 			"port %d not enabled, trying reset again...\n",
19981da177e4SLinus Torvalds 			port1);
19991da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
20001da177e4SLinus Torvalds 	}
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds 	dev_err (hub->intfdev,
20031da177e4SLinus Torvalds 		"Cannot enable port %i.  Maybe the USB cable is bad?\n",
20041da177e4SLinus Torvalds 		port1);
20051da177e4SLinus Torvalds 
200632fe0198SAlan Stern  done:
200732fe0198SAlan Stern 	up_read(&ehci_cf_port_reset_rwsem);
20081da177e4SLinus Torvalds 	return status;
20091da177e4SLinus Torvalds }
20101da177e4SLinus Torvalds 
2011d388dab7SAlan Stern #ifdef	CONFIG_PM
20121da177e4SLinus Torvalds 
2013b01b03f3SAlan Stern #define MASK_BITS	(USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION | \
2014b01b03f3SAlan Stern 				USB_PORT_STAT_SUSPEND)
2015b01b03f3SAlan Stern #define WANT_BITS	(USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION)
2016b01b03f3SAlan Stern 
2017b01b03f3SAlan Stern /* Determine whether the device on a port is ready for a normal resume,
2018b01b03f3SAlan Stern  * is ready for a reset-resume, or should be disconnected.
2019b01b03f3SAlan Stern  */
2020b01b03f3SAlan Stern static int check_port_resume_type(struct usb_device *udev,
2021b01b03f3SAlan Stern 		struct usb_hub *hub, int port1,
2022b01b03f3SAlan Stern 		int status, unsigned portchange, unsigned portstatus)
2023b01b03f3SAlan Stern {
2024b01b03f3SAlan Stern 	/* Is the device still present? */
2025b01b03f3SAlan Stern 	if (status || (portstatus & MASK_BITS) != WANT_BITS) {
2026b01b03f3SAlan Stern 		if (status >= 0)
2027b01b03f3SAlan Stern 			status = -ENODEV;
2028b01b03f3SAlan Stern 	}
2029b01b03f3SAlan Stern 
203086c57edfSAlan Stern 	/* Can't do a normal resume if the port isn't enabled,
203186c57edfSAlan Stern 	 * so try a reset-resume instead.
203286c57edfSAlan Stern 	 */
203386c57edfSAlan Stern 	else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
203486c57edfSAlan Stern 		if (udev->persist_enabled)
203586c57edfSAlan Stern 			udev->reset_resume = 1;
203686c57edfSAlan Stern 		else
2037b01b03f3SAlan Stern 			status = -ENODEV;
203886c57edfSAlan Stern 	}
2039b01b03f3SAlan Stern 
2040b01b03f3SAlan Stern 	if (status) {
2041b01b03f3SAlan Stern 		dev_dbg(hub->intfdev,
2042b01b03f3SAlan Stern 				"port %d status %04x.%04x after resume, %d\n",
2043b01b03f3SAlan Stern 				port1, portchange, portstatus, status);
2044b01b03f3SAlan Stern 	} else if (udev->reset_resume) {
2045b01b03f3SAlan Stern 
2046b01b03f3SAlan Stern 		/* Late port handoff can set status-change bits */
2047b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_CONNECTION)
2048b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2049b01b03f3SAlan Stern 					USB_PORT_FEAT_C_CONNECTION);
2050b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_ENABLE)
2051b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2052b01b03f3SAlan Stern 					USB_PORT_FEAT_C_ENABLE);
2053b01b03f3SAlan Stern 	}
2054b01b03f3SAlan Stern 
2055b01b03f3SAlan Stern 	return status;
2056b01b03f3SAlan Stern }
2057b01b03f3SAlan Stern 
20581da177e4SLinus Torvalds #ifdef	CONFIG_USB_SUSPEND
20591da177e4SLinus Torvalds 
20601da177e4SLinus Torvalds /*
2061140d8f68SAlan Stern  * usb_port_suspend - suspend a usb device's upstream port
2062624d6c07SAlan Stern  * @udev: device that's no longer in active use, not a root hub
20635edbfb7cSDavid Brownell  * Context: must be able to sleep; device not locked; pm locks held
20641da177e4SLinus Torvalds  *
20651da177e4SLinus Torvalds  * Suspends a USB device that isn't in active use, conserving power.
20661da177e4SLinus Torvalds  * Devices may wake out of a suspend, if anything important happens,
20671da177e4SLinus Torvalds  * using the remote wakeup mechanism.  They may also be taken out of
2068140d8f68SAlan Stern  * suspend by the host, using usb_port_resume().  It's also routine
20691da177e4SLinus Torvalds  * to disconnect devices while they are suspended.
20701da177e4SLinus Torvalds  *
2071390a8c34SDavid Brownell  * This only affects the USB hardware for a device; its interfaces
2072390a8c34SDavid Brownell  * (and, for hubs, child devices) must already have been suspended.
2073390a8c34SDavid Brownell  *
2074624d6c07SAlan Stern  * Selective port suspend reduces power; most suspended devices draw
2075624d6c07SAlan Stern  * less than 500 uA.  It's also used in OTG, along with remote wakeup.
2076624d6c07SAlan Stern  * All devices below the suspended port are also suspended.
2077624d6c07SAlan Stern  *
2078624d6c07SAlan Stern  * Devices leave suspend state when the host wakes them up.  Some devices
2079624d6c07SAlan Stern  * also support "remote wakeup", where the device can activate the USB
2080624d6c07SAlan Stern  * tree above them to deliver data, such as a keypress or packet.  In
2081624d6c07SAlan Stern  * some cases, this wakes the USB host.
2082624d6c07SAlan Stern  *
20831da177e4SLinus Torvalds  * Suspending OTG devices may trigger HNP, if that's been enabled
20841da177e4SLinus Torvalds  * between a pair of dual-role devices.  That will change roles, such
20851da177e4SLinus Torvalds  * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
20861da177e4SLinus Torvalds  *
20874956eccdSAlan Stern  * Devices on USB hub ports have only one "suspend" state, corresponding
20884956eccdSAlan Stern  * to ACPI D2, "may cause the device to lose some context".
20894956eccdSAlan Stern  * State transitions include:
20904956eccdSAlan Stern  *
20914956eccdSAlan Stern  *   - suspend, resume ... when the VBUS power link stays live
20924956eccdSAlan Stern  *   - suspend, disconnect ... VBUS lost
20934956eccdSAlan Stern  *
20944956eccdSAlan Stern  * Once VBUS drop breaks the circuit, the port it's using has to go through
20954956eccdSAlan Stern  * normal re-enumeration procedures, starting with enabling VBUS power.
20964956eccdSAlan Stern  * Other than re-initializing the hub (plug/unplug, except for root hubs),
20974956eccdSAlan Stern  * Linux (2.6) currently has NO mechanisms to initiate that:  no khubd
20984956eccdSAlan Stern  * timer, no SRP, no requests through sysfs.
20994956eccdSAlan Stern  *
21004956eccdSAlan Stern  * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
21014956eccdSAlan Stern  * the root hub for their bus goes into global suspend ... so we don't
21024956eccdSAlan Stern  * (falsely) update the device power state to say it suspended.
21034956eccdSAlan Stern  *
21041da177e4SLinus Torvalds  * Returns 0 on success, else negative errno.
21051da177e4SLinus Torvalds  */
210665bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
21071da177e4SLinus Torvalds {
2108624d6c07SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2109624d6c07SAlan Stern 	int		port1 = udev->portnum;
2110624d6c07SAlan Stern 	int		status;
21114956eccdSAlan Stern 
2112624d6c07SAlan Stern 	// dev_dbg(hub->intfdev, "suspend port %d\n", port1);
2113624d6c07SAlan Stern 
2114624d6c07SAlan Stern 	/* enable remote wakeup when appropriate; this lets the device
2115624d6c07SAlan Stern 	 * wake up the upstream hub (including maybe the root hub).
2116624d6c07SAlan Stern 	 *
2117624d6c07SAlan Stern 	 * NOTE:  OTG devices may issue remote wakeup (or SRP) even when
2118624d6c07SAlan Stern 	 * we don't explicitly enable it here.
2119624d6c07SAlan Stern 	 */
2120624d6c07SAlan Stern 	if (udev->do_remote_wakeup) {
2121624d6c07SAlan Stern 		status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2122624d6c07SAlan Stern 				USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2123624d6c07SAlan Stern 				USB_DEVICE_REMOTE_WAKEUP, 0,
2124624d6c07SAlan Stern 				NULL, 0,
2125624d6c07SAlan Stern 				USB_CTRL_SET_TIMEOUT);
2126624d6c07SAlan Stern 		if (status)
2127624d6c07SAlan Stern 			dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2128624d6c07SAlan Stern 					status);
2129624d6c07SAlan Stern 	}
2130624d6c07SAlan Stern 
2131624d6c07SAlan Stern 	/* see 7.1.7.6 */
2132624d6c07SAlan Stern 	status = set_port_feature(hub->hdev, port1, USB_PORT_FEAT_SUSPEND);
2133624d6c07SAlan Stern 	if (status) {
2134624d6c07SAlan Stern 		dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2135624d6c07SAlan Stern 				port1, status);
2136624d6c07SAlan Stern 		/* paranoia:  "should not happen" */
2137624d6c07SAlan Stern 		(void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2138624d6c07SAlan Stern 				USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2139624d6c07SAlan Stern 				USB_DEVICE_REMOTE_WAKEUP, 0,
2140624d6c07SAlan Stern 				NULL, 0,
2141624d6c07SAlan Stern 				USB_CTRL_SET_TIMEOUT);
2142624d6c07SAlan Stern 	} else {
2143624d6c07SAlan Stern 		/* device has up to 10 msec to fully suspend */
2144624d6c07SAlan Stern 		dev_dbg(&udev->dev, "usb %ssuspend\n",
214565bfd296SAlan Stern 				(msg.event & PM_EVENT_AUTO ? "auto-" : ""));
2146624d6c07SAlan Stern 		usb_set_device_state(udev, USB_STATE_SUSPENDED);
2147624d6c07SAlan Stern 		msleep(10);
2148624d6c07SAlan Stern 	}
21494956eccdSAlan Stern 	return status;
21501da177e4SLinus Torvalds }
2151f3f3253dSDavid Brownell 
21521da177e4SLinus Torvalds /*
2153390a8c34SDavid Brownell  * If the USB "suspend" state is in use (rather than "global suspend"),
2154390a8c34SDavid Brownell  * many devices will be individually taken out of suspend state using
215554515fe5SAlan Stern  * special "resume" signaling.  This routine kicks in shortly after
21561da177e4SLinus Torvalds  * hardware resume signaling is finished, either because of selective
21571da177e4SLinus Torvalds  * resume (by host) or remote wakeup (by device) ... now see what changed
21581da177e4SLinus Torvalds  * in the tree that's rooted at this device.
215954515fe5SAlan Stern  *
216054515fe5SAlan Stern  * If @udev->reset_resume is set then the device is reset before the
216154515fe5SAlan Stern  * status check is done.
21621da177e4SLinus Torvalds  */
2163140d8f68SAlan Stern static int finish_port_resume(struct usb_device *udev)
21641da177e4SLinus Torvalds {
216554515fe5SAlan Stern 	int	status = 0;
21661da177e4SLinus Torvalds 	u16	devstatus;
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds 	/* caller owns the udev device lock */
2169b9cef6c3SWu Fengguang 	dev_dbg(&udev->dev, "%s\n",
2170b9cef6c3SWu Fengguang 		udev->reset_resume ? "finish reset-resume" : "finish resume");
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds 	/* usb ch9 identifies four variants of SUSPENDED, based on what
21731da177e4SLinus Torvalds 	 * state the device resumes to.  Linux currently won't see the
21741da177e4SLinus Torvalds 	 * first two on the host side; they'd be inside hub_port_init()
21751da177e4SLinus Torvalds 	 * during many timeouts, but khubd can't suspend until later.
21761da177e4SLinus Torvalds 	 */
21771da177e4SLinus Torvalds 	usb_set_device_state(udev, udev->actconfig
21781da177e4SLinus Torvalds 			? USB_STATE_CONFIGURED
21791da177e4SLinus Torvalds 			: USB_STATE_ADDRESS);
21801da177e4SLinus Torvalds 
218154515fe5SAlan Stern 	/* 10.5.4.5 says not to reset a suspended port if the attached
218254515fe5SAlan Stern 	 * device is enabled for remote wakeup.  Hence the reset
218354515fe5SAlan Stern 	 * operation is carried out here, after the port has been
218454515fe5SAlan Stern 	 * resumed.
218554515fe5SAlan Stern 	 */
218654515fe5SAlan Stern 	if (udev->reset_resume)
218786c57edfSAlan Stern  retry_reset_resume:
2188742120c6SMing Lei 		status = usb_reset_and_verify_device(udev);
218954515fe5SAlan Stern 
21901da177e4SLinus Torvalds  	/* 10.5.4.5 says be sure devices in the tree are still there.
21911da177e4SLinus Torvalds  	 * For now let's assume the device didn't go crazy on resume,
21921da177e4SLinus Torvalds 	 * and device drivers will know about any resume quirks.
21931da177e4SLinus Torvalds 	 */
219454515fe5SAlan Stern 	if (status == 0) {
219546dede46SAlan Stern 		devstatus = 0;
21961da177e4SLinus Torvalds 		status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2197b40b7a90SAlan Stern 		if (status >= 0)
219846dede46SAlan Stern 			status = (status > 0 ? 0 : -ENODEV);
219986c57edfSAlan Stern 
220086c57edfSAlan Stern 		/* If a normal resume failed, try doing a reset-resume */
220186c57edfSAlan Stern 		if (status && !udev->reset_resume && udev->persist_enabled) {
220286c57edfSAlan Stern 			dev_dbg(&udev->dev, "retry with reset-resume\n");
220386c57edfSAlan Stern 			udev->reset_resume = 1;
220486c57edfSAlan Stern 			goto retry_reset_resume;
220586c57edfSAlan Stern 		}
220654515fe5SAlan Stern 	}
2207b40b7a90SAlan Stern 
2208624d6c07SAlan Stern 	if (status) {
2209624d6c07SAlan Stern 		dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
22101da177e4SLinus Torvalds 				status);
2211624d6c07SAlan Stern 	} else if (udev->actconfig) {
22121da177e4SLinus Torvalds 		le16_to_cpus(&devstatus);
2213686314cfSAlan Stern 		if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
22141da177e4SLinus Torvalds 			status = usb_control_msg(udev,
22151da177e4SLinus Torvalds 					usb_sndctrlpipe(udev, 0),
22161da177e4SLinus Torvalds 					USB_REQ_CLEAR_FEATURE,
22171da177e4SLinus Torvalds 						USB_RECIP_DEVICE,
22181da177e4SLinus Torvalds 					USB_DEVICE_REMOTE_WAKEUP, 0,
22191da177e4SLinus Torvalds 					NULL, 0,
22201da177e4SLinus Torvalds 					USB_CTRL_SET_TIMEOUT);
2221a8e7c565SAlan Stern 			if (status)
2222b9cef6c3SWu Fengguang 				dev_dbg(&udev->dev,
2223b9cef6c3SWu Fengguang 					"disable remote wakeup, status %d\n",
2224b9cef6c3SWu Fengguang 					status);
22254bf0ba86SAlan Stern 		}
22261da177e4SLinus Torvalds 		status = 0;
22271da177e4SLinus Torvalds 	}
22281da177e4SLinus Torvalds 	return status;
22291da177e4SLinus Torvalds }
22301da177e4SLinus Torvalds 
2231624d6c07SAlan Stern /*
2232624d6c07SAlan Stern  * usb_port_resume - re-activate a suspended usb device's upstream port
2233624d6c07SAlan Stern  * @udev: device to re-activate, not a root hub
2234624d6c07SAlan Stern  * Context: must be able to sleep; device not locked; pm locks held
2235624d6c07SAlan Stern  *
2236624d6c07SAlan Stern  * This will re-activate the suspended device, increasing power usage
2237624d6c07SAlan Stern  * while letting drivers communicate again with its endpoints.
2238624d6c07SAlan Stern  * USB resume explicitly guarantees that the power session between
2239624d6c07SAlan Stern  * the host and the device is the same as it was when the device
2240624d6c07SAlan Stern  * suspended.
2241624d6c07SAlan Stern  *
2242feccc30dSAlan Stern  * If @udev->reset_resume is set then this routine won't check that the
2243feccc30dSAlan Stern  * port is still enabled.  Furthermore, finish_port_resume() above will
224454515fe5SAlan Stern  * reset @udev.  The end result is that a broken power session can be
224554515fe5SAlan Stern  * recovered and @udev will appear to persist across a loss of VBUS power.
224654515fe5SAlan Stern  *
224754515fe5SAlan Stern  * For example, if a host controller doesn't maintain VBUS suspend current
224854515fe5SAlan Stern  * during a system sleep or is reset when the system wakes up, all the USB
224954515fe5SAlan Stern  * power sessions below it will be broken.  This is especially troublesome
225054515fe5SAlan Stern  * for mass-storage devices containing mounted filesystems, since the
225154515fe5SAlan Stern  * device will appear to have disconnected and all the memory mappings
225254515fe5SAlan Stern  * to it will be lost.  Using the USB_PERSIST facility, the device can be
225354515fe5SAlan Stern  * made to appear as if it had not disconnected.
225454515fe5SAlan Stern  *
2255742120c6SMing Lei  * This facility can be dangerous.  Although usb_reset_and_verify_device() makes
2256feccc30dSAlan Stern  * every effort to insure that the same device is present after the
225754515fe5SAlan Stern  * reset as before, it cannot provide a 100% guarantee.  Furthermore it's
225854515fe5SAlan Stern  * quite possible for a device to remain unaltered but its media to be
225954515fe5SAlan Stern  * changed.  If the user replaces a flash memory card while the system is
226054515fe5SAlan Stern  * asleep, he will have only himself to blame when the filesystem on the
226154515fe5SAlan Stern  * new card is corrupted and the system crashes.
226254515fe5SAlan Stern  *
2263624d6c07SAlan Stern  * Returns 0 on success, else negative errno.
2264624d6c07SAlan Stern  */
226565bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
22661da177e4SLinus Torvalds {
2267624d6c07SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2268624d6c07SAlan Stern 	int		port1 = udev->portnum;
22691da177e4SLinus Torvalds 	int		status;
2270d25450c6SAlan Stern 	u16		portchange, portstatus;
2271d25450c6SAlan Stern 
2272d25450c6SAlan Stern 	/* Skip the initial Clear-Suspend step for a remote wakeup */
2273d25450c6SAlan Stern 	status = hub_port_status(hub, port1, &portstatus, &portchange);
2274d25450c6SAlan Stern 	if (status == 0 && !(portstatus & USB_PORT_STAT_SUSPEND))
2275d25450c6SAlan Stern 		goto SuspendCleared;
22761da177e4SLinus Torvalds 
22771da177e4SLinus Torvalds 	// dev_dbg(hub->intfdev, "resume port %d\n", port1);
22781da177e4SLinus Torvalds 
2279d5cbad4bSAlan Stern 	set_bit(port1, hub->busy_bits);
2280d5cbad4bSAlan Stern 
22811da177e4SLinus Torvalds 	/* see 7.1.7.7; affects power usage, but not budgeting */
22821da177e4SLinus Torvalds 	status = clear_port_feature(hub->hdev,
22831da177e4SLinus Torvalds 			port1, USB_PORT_FEAT_SUSPEND);
22841da177e4SLinus Torvalds 	if (status) {
2285624d6c07SAlan Stern 		dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
22861da177e4SLinus Torvalds 				port1, status);
22871da177e4SLinus Torvalds 	} else {
22881da177e4SLinus Torvalds 		/* drive resume for at least 20 msec */
2289645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n",
229065bfd296SAlan Stern 				(msg.event & PM_EVENT_AUTO ? "auto-" : ""));
22911da177e4SLinus Torvalds 		msleep(25);
22921da177e4SLinus Torvalds 
22931da177e4SLinus Torvalds 		/* Virtual root hubs can trigger on GET_PORT_STATUS to
22941da177e4SLinus Torvalds 		 * stop resume signaling.  Then finish the resume
22951da177e4SLinus Torvalds 		 * sequence.
22961da177e4SLinus Torvalds 		 */
2297d25450c6SAlan Stern 		status = hub_port_status(hub, port1, &portstatus, &portchange);
229854515fe5SAlan Stern 
22991da177e4SLinus Torvalds 		/* TRSMRCY = 10 msec */
23001da177e4SLinus Torvalds 		msleep(10);
23011da177e4SLinus Torvalds 	}
2302b01b03f3SAlan Stern 
2303b01b03f3SAlan Stern  SuspendCleared:
2304b01b03f3SAlan Stern 	if (status == 0) {
2305b01b03f3SAlan Stern 		if (portchange & USB_PORT_STAT_C_SUSPEND)
2306b01b03f3SAlan Stern 			clear_port_feature(hub->hdev, port1,
2307b01b03f3SAlan Stern 					USB_PORT_FEAT_C_SUSPEND);
23081da177e4SLinus Torvalds 	}
23091da177e4SLinus Torvalds 
2310d5cbad4bSAlan Stern 	clear_bit(port1, hub->busy_bits);
2311d5cbad4bSAlan Stern 
2312b01b03f3SAlan Stern 	status = check_port_resume_type(udev,
2313b01b03f3SAlan Stern 			hub, port1, status, portchange, portstatus);
231454515fe5SAlan Stern 	if (status == 0)
231554515fe5SAlan Stern 		status = finish_port_resume(udev);
231654515fe5SAlan Stern 	if (status < 0) {
231754515fe5SAlan Stern 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
231854515fe5SAlan Stern 		hub_port_logical_disconnect(hub, port1);
231954515fe5SAlan Stern 	}
23201da177e4SLinus Torvalds 	return status;
23211da177e4SLinus Torvalds }
23221da177e4SLinus Torvalds 
23238808f00cSAlan Stern /* caller has locked udev */
23241da177e4SLinus Torvalds static int remote_wakeup(struct usb_device *udev)
23251da177e4SLinus Torvalds {
23261da177e4SLinus Torvalds 	int	status = 0;
23271da177e4SLinus Torvalds 
23281da177e4SLinus Torvalds 	if (udev->state == USB_STATE_SUSPENDED) {
2329645daaabSAlan Stern 		dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
23301941044aSAlan Stern 		usb_mark_last_busy(udev);
233165bfd296SAlan Stern 		status = usb_external_resume_device(udev, PMSG_REMOTE_RESUME);
2332d25450c6SAlan Stern 	}
23331da177e4SLinus Torvalds 	return status;
23341da177e4SLinus Torvalds }
23351da177e4SLinus Torvalds 
2336d388dab7SAlan Stern #else	/* CONFIG_USB_SUSPEND */
2337d388dab7SAlan Stern 
2338d388dab7SAlan Stern /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
2339d388dab7SAlan Stern 
234065bfd296SAlan Stern int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
2341d388dab7SAlan Stern {
2342d388dab7SAlan Stern 	return 0;
2343d388dab7SAlan Stern }
2344d388dab7SAlan Stern 
2345b01b03f3SAlan Stern /* However we may need to do a reset-resume */
2346b01b03f3SAlan Stern 
234765bfd296SAlan Stern int usb_port_resume(struct usb_device *udev, pm_message_t msg)
2348d388dab7SAlan Stern {
2349b01b03f3SAlan Stern 	struct usb_hub	*hub = hdev_to_hub(udev->parent);
2350b01b03f3SAlan Stern 	int		port1 = udev->portnum;
2351b01b03f3SAlan Stern 	int		status;
2352b01b03f3SAlan Stern 	u16		portchange, portstatus;
235354515fe5SAlan Stern 
2354b01b03f3SAlan Stern 	status = hub_port_status(hub, port1, &portstatus, &portchange);
2355b01b03f3SAlan Stern 	status = check_port_resume_type(udev,
2356b01b03f3SAlan Stern 			hub, port1, status, portchange, portstatus);
2357b01b03f3SAlan Stern 
2358b01b03f3SAlan Stern 	if (status) {
2359b01b03f3SAlan Stern 		dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2360b01b03f3SAlan Stern 		hub_port_logical_disconnect(hub, port1);
2361b01b03f3SAlan Stern 	} else if (udev->reset_resume) {
236254515fe5SAlan Stern 		dev_dbg(&udev->dev, "reset-resume\n");
2363742120c6SMing Lei 		status = usb_reset_and_verify_device(udev);
236454515fe5SAlan Stern 	}
236554515fe5SAlan Stern 	return status;
2366d388dab7SAlan Stern }
2367d388dab7SAlan Stern 
2368d388dab7SAlan Stern static inline int remote_wakeup(struct usb_device *udev)
2369d388dab7SAlan Stern {
2370d388dab7SAlan Stern 	return 0;
2371d388dab7SAlan Stern }
2372d388dab7SAlan Stern 
2373d388dab7SAlan Stern #endif
2374d388dab7SAlan Stern 
2375db690874SDavid Brownell static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
23761da177e4SLinus Torvalds {
23771da177e4SLinus Torvalds 	struct usb_hub		*hub = usb_get_intfdata (intf);
23781da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
23791da177e4SLinus Torvalds 	unsigned		port1;
23801da177e4SLinus Torvalds 
2381c9f89fa4SDavid Brownell 	/* fail if children aren't already suspended */
23821da177e4SLinus Torvalds 	for (port1 = 1; port1 <= hdev->maxchild; port1++) {
23831da177e4SLinus Torvalds 		struct usb_device	*udev;
23841da177e4SLinus Torvalds 
23851da177e4SLinus Torvalds 		udev = hdev->children [port1-1];
23866840d255SAlan Stern 		if (udev && udev->can_submit) {
238765bfd296SAlan Stern 			if (!(msg.event & PM_EVENT_AUTO))
2388645daaabSAlan Stern 				dev_dbg(&intf->dev, "port %d nyet suspended\n",
2389645daaabSAlan Stern 						port1);
2390c9f89fa4SDavid Brownell 			return -EBUSY;
2391c9f89fa4SDavid Brownell 		}
23921da177e4SLinus Torvalds 	}
23931da177e4SLinus Torvalds 
2394441b62c1SHarvey Harrison 	dev_dbg(&intf->dev, "%s\n", __func__);
239540f122f3SAlan Stern 
2396c9f89fa4SDavid Brownell 	/* stop khubd and related activity */
23974330354fSAlan Stern 	hub_quiesce(hub, HUB_SUSPEND);
2398b6f6436dSAlan Stern 	return 0;
23991da177e4SLinus Torvalds }
24001da177e4SLinus Torvalds 
24011da177e4SLinus Torvalds static int hub_resume(struct usb_interface *intf)
24021da177e4SLinus Torvalds {
24031da177e4SLinus Torvalds 	struct usb_hub *hub = usb_get_intfdata(intf);
24041da177e4SLinus Torvalds 
24055e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
2406f2835219SAlan Stern 	hub_activate(hub, HUB_RESUME);
24071da177e4SLinus Torvalds 	return 0;
24081da177e4SLinus Torvalds }
24091da177e4SLinus Torvalds 
2410b41a60ecSAlan Stern static int hub_reset_resume(struct usb_interface *intf)
2411f07600cfSAlan Stern {
2412b41a60ecSAlan Stern 	struct usb_hub *hub = usb_get_intfdata(intf);
2413f07600cfSAlan Stern 
24145e6effaeSAlan Stern 	dev_dbg(&intf->dev, "%s\n", __func__);
2415f2835219SAlan Stern 	hub_activate(hub, HUB_RESET_RESUME);
2416f07600cfSAlan Stern 	return 0;
2417f07600cfSAlan Stern }
2418f07600cfSAlan Stern 
241954515fe5SAlan Stern /**
242054515fe5SAlan Stern  * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
242154515fe5SAlan Stern  * @rhdev: struct usb_device for the root hub
242254515fe5SAlan Stern  *
242354515fe5SAlan Stern  * The USB host controller driver calls this function when its root hub
242454515fe5SAlan Stern  * is resumed and Vbus power has been interrupted or the controller
2425feccc30dSAlan Stern  * has been reset.  The routine marks @rhdev as having lost power.
2426feccc30dSAlan Stern  * When the hub driver is resumed it will take notice and carry out
2427feccc30dSAlan Stern  * power-session recovery for all the "USB-PERSIST"-enabled child devices;
2428feccc30dSAlan Stern  * the others will be disconnected.
242954515fe5SAlan Stern  */
243054515fe5SAlan Stern void usb_root_hub_lost_power(struct usb_device *rhdev)
243154515fe5SAlan Stern {
243254515fe5SAlan Stern 	dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
243354515fe5SAlan Stern 	rhdev->reset_resume = 1;
243454515fe5SAlan Stern }
243554515fe5SAlan Stern EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
243654515fe5SAlan Stern 
2437d388dab7SAlan Stern #else	/* CONFIG_PM */
2438d388dab7SAlan Stern 
2439d388dab7SAlan Stern static inline int remote_wakeup(struct usb_device *udev)
2440d388dab7SAlan Stern {
2441d388dab7SAlan Stern 	return 0;
2442d388dab7SAlan Stern }
2443d388dab7SAlan Stern 
2444511366daSAndrew Morton #define hub_suspend		NULL
2445511366daSAndrew Morton #define hub_resume		NULL
2446f07600cfSAlan Stern #define hub_reset_resume	NULL
2447d388dab7SAlan Stern #endif
2448d388dab7SAlan Stern 
24491da177e4SLinus Torvalds 
24501da177e4SLinus Torvalds /* USB 2.0 spec, 7.1.7.3 / fig 7-29:
24511da177e4SLinus Torvalds  *
24521da177e4SLinus Torvalds  * Between connect detection and reset signaling there must be a delay
24531da177e4SLinus Torvalds  * of 100ms at least for debounce and power-settling.  The corresponding
24541da177e4SLinus Torvalds  * timer shall restart whenever the downstream port detects a disconnect.
24551da177e4SLinus Torvalds  *
24561da177e4SLinus Torvalds  * Apparently there are some bluetooth and irda-dongles and a number of
24571da177e4SLinus Torvalds  * low-speed devices for which this debounce period may last over a second.
24581da177e4SLinus Torvalds  * Not covered by the spec - but easy to deal with.
24591da177e4SLinus Torvalds  *
24601da177e4SLinus Torvalds  * This implementation uses a 1500ms total debounce timeout; if the
24611da177e4SLinus Torvalds  * connection isn't stable by then it returns -ETIMEDOUT.  It checks
24621da177e4SLinus Torvalds  * every 25ms for transient disconnects.  When the port status has been
24631da177e4SLinus Torvalds  * unchanged for 100ms it returns the port status.
24641da177e4SLinus Torvalds  */
24651da177e4SLinus Torvalds static int hub_port_debounce(struct usb_hub *hub, int port1)
24661da177e4SLinus Torvalds {
24671da177e4SLinus Torvalds 	int ret;
24681da177e4SLinus Torvalds 	int total_time, stable_time = 0;
24691da177e4SLinus Torvalds 	u16 portchange, portstatus;
24701da177e4SLinus Torvalds 	unsigned connection = 0xffff;
24711da177e4SLinus Torvalds 
24721da177e4SLinus Torvalds 	for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
24731da177e4SLinus Torvalds 		ret = hub_port_status(hub, port1, &portstatus, &portchange);
24741da177e4SLinus Torvalds 		if (ret < 0)
24751da177e4SLinus Torvalds 			return ret;
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds 		if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
24781da177e4SLinus Torvalds 		     (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
24791da177e4SLinus Torvalds 			stable_time += HUB_DEBOUNCE_STEP;
24801da177e4SLinus Torvalds 			if (stable_time >= HUB_DEBOUNCE_STABLE)
24811da177e4SLinus Torvalds 				break;
24821da177e4SLinus Torvalds 		} else {
24831da177e4SLinus Torvalds 			stable_time = 0;
24841da177e4SLinus Torvalds 			connection = portstatus & USB_PORT_STAT_CONNECTION;
24851da177e4SLinus Torvalds 		}
24861da177e4SLinus Torvalds 
24871da177e4SLinus Torvalds 		if (portchange & USB_PORT_STAT_C_CONNECTION) {
24881da177e4SLinus Torvalds 			clear_port_feature(hub->hdev, port1,
24891da177e4SLinus Torvalds 					USB_PORT_FEAT_C_CONNECTION);
24901da177e4SLinus Torvalds 		}
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds 		if (total_time >= HUB_DEBOUNCE_TIMEOUT)
24931da177e4SLinus Torvalds 			break;
24941da177e4SLinus Torvalds 		msleep(HUB_DEBOUNCE_STEP);
24951da177e4SLinus Torvalds 	}
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds 	dev_dbg (hub->intfdev,
24981da177e4SLinus Torvalds 		"debounce: port %d: total %dms stable %dms status 0x%x\n",
24991da177e4SLinus Torvalds 		port1, total_time, stable_time, portstatus);
25001da177e4SLinus Torvalds 
25011da177e4SLinus Torvalds 	if (stable_time < HUB_DEBOUNCE_STABLE)
25021da177e4SLinus Torvalds 		return -ETIMEDOUT;
25031da177e4SLinus Torvalds 	return portstatus;
25041da177e4SLinus Torvalds }
25051da177e4SLinus Torvalds 
2506fc721f51SInaky Perez-Gonzalez void usb_ep0_reinit(struct usb_device *udev)
25071da177e4SLinus Torvalds {
2508ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
2509ddeac4e7SAlan Stern 	usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
25102caf7fcdSAlan Stern 	usb_enable_endpoint(udev, &udev->ep0, true);
25111da177e4SLinus Torvalds }
2512fc721f51SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_ep0_reinit);
25131da177e4SLinus Torvalds 
25141da177e4SLinus Torvalds #define usb_sndaddr0pipe()	(PIPE_CONTROL << 30)
25151da177e4SLinus Torvalds #define usb_rcvaddr0pipe()	((PIPE_CONTROL << 30) | USB_DIR_IN)
25161da177e4SLinus Torvalds 
25174326ed0bSAlan Stern static int hub_set_address(struct usb_device *udev, int devnum)
25181da177e4SLinus Torvalds {
25191da177e4SLinus Torvalds 	int retval;
2520c6515272SSarah Sharp 	struct usb_hcd *hcd = bus_to_hcd(udev->bus);
25211da177e4SLinus Torvalds 
2522c6515272SSarah Sharp 	/*
2523c6515272SSarah Sharp 	 * The host controller will choose the device address,
2524c6515272SSarah Sharp 	 * instead of the core having chosen it earlier
2525c6515272SSarah Sharp 	 */
2526c6515272SSarah Sharp 	if (!hcd->driver->address_device && devnum <= 1)
25271da177e4SLinus Torvalds 		return -EINVAL;
25281da177e4SLinus Torvalds 	if (udev->state == USB_STATE_ADDRESS)
25291da177e4SLinus Torvalds 		return 0;
25301da177e4SLinus Torvalds 	if (udev->state != USB_STATE_DEFAULT)
25311da177e4SLinus Torvalds 		return -EINVAL;
2532c6515272SSarah Sharp 	if (hcd->driver->address_device) {
2533c6515272SSarah Sharp 		retval = hcd->driver->address_device(hcd, udev);
2534c6515272SSarah Sharp 	} else {
25351da177e4SLinus Torvalds 		retval = usb_control_msg(udev, usb_sndaddr0pipe(),
25364326ed0bSAlan Stern 				USB_REQ_SET_ADDRESS, 0, devnum, 0,
25371da177e4SLinus Torvalds 				NULL, 0, USB_CTRL_SET_TIMEOUT);
2538c6515272SSarah Sharp 		if (retval == 0)
2539c6515272SSarah Sharp 			update_address(udev, devnum);
2540c6515272SSarah Sharp 	}
25411da177e4SLinus Torvalds 	if (retval == 0) {
25424953d141SDavid Vrabel 		/* Device now using proper address. */
25431da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_ADDRESS);
2544fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
25451da177e4SLinus Torvalds 	}
25461da177e4SLinus Torvalds 	return retval;
25471da177e4SLinus Torvalds }
25481da177e4SLinus Torvalds 
25491da177e4SLinus Torvalds /* Reset device, (re)assign address, get device descriptor.
25501da177e4SLinus Torvalds  * Device connection must be stable, no more debouncing needed.
25511da177e4SLinus Torvalds  * Returns device in USB_STATE_ADDRESS, except on error.
25521da177e4SLinus Torvalds  *
25531da177e4SLinus Torvalds  * If this is called for an already-existing device (as part of
2554742120c6SMing Lei  * usb_reset_and_verify_device), the caller must own the device lock.  For a
25551da177e4SLinus Torvalds  * newly detected device that is not accessible through any global
25561da177e4SLinus Torvalds  * pointers, it's not necessary to lock the device.
25571da177e4SLinus Torvalds  */
25581da177e4SLinus Torvalds static int
25591da177e4SLinus Torvalds hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
25601da177e4SLinus Torvalds 		int retry_counter)
25611da177e4SLinus Torvalds {
25624186ecf8SArjan van de Ven 	static DEFINE_MUTEX(usb_address0_mutex);
25631da177e4SLinus Torvalds 
25641da177e4SLinus Torvalds 	struct usb_device	*hdev = hub->hdev;
2565e7b77172SSarah Sharp 	struct usb_hcd		*hcd = bus_to_hcd(hdev->bus);
25661da177e4SLinus Torvalds 	int			i, j, retval;
25671da177e4SLinus Torvalds 	unsigned		delay = HUB_SHORT_RESET_TIME;
25681da177e4SLinus Torvalds 	enum usb_device_speed	oldspeed = udev->speed;
256983a07196SInaky Perez-Gonzalez 	char 			*speed, *type;
25704326ed0bSAlan Stern 	int			devnum = udev->devnum;
25711da177e4SLinus Torvalds 
25721da177e4SLinus Torvalds 	/* root hub ports have a slightly longer reset period
25731da177e4SLinus Torvalds 	 * (from USB 2.0 spec, section 7.1.7.5)
25741da177e4SLinus Torvalds 	 */
25751da177e4SLinus Torvalds 	if (!hdev->parent) {
25761da177e4SLinus Torvalds 		delay = HUB_ROOT_RESET_TIME;
25771da177e4SLinus Torvalds 		if (port1 == hdev->bus->otg_port)
25781da177e4SLinus Torvalds 			hdev->bus->b_hnp_enable = 0;
25791da177e4SLinus Torvalds 	}
25801da177e4SLinus Torvalds 
25811da177e4SLinus Torvalds 	/* Some low speed devices have problems with the quick delay, so */
25821da177e4SLinus Torvalds 	/*  be a bit pessimistic with those devices. RHbug #23670 */
25831da177e4SLinus Torvalds 	if (oldspeed == USB_SPEED_LOW)
25841da177e4SLinus Torvalds 		delay = HUB_LONG_RESET_TIME;
25851da177e4SLinus Torvalds 
25864186ecf8SArjan van de Ven 	mutex_lock(&usb_address0_mutex);
25871da177e4SLinus Torvalds 
2588e7b77172SSarah Sharp 	if ((hcd->driver->flags & HCD_USB3) && udev->config) {
2589e7b77172SSarah Sharp 		/* FIXME this will need special handling by the xHCI driver. */
2590e7b77172SSarah Sharp 		dev_dbg(&udev->dev,
2591e7b77172SSarah Sharp 				"xHCI reset of configured device "
2592e7b77172SSarah Sharp 				"not supported yet.\n");
2593e7b77172SSarah Sharp 		retval = -EINVAL;
2594e7b77172SSarah Sharp 		goto fail;
2595e7b77172SSarah Sharp 	} else if (!udev->config && oldspeed == USB_SPEED_SUPER) {
2596e7b77172SSarah Sharp 		/* Don't reset USB 3.0 devices during an initial setup */
2597e7b77172SSarah Sharp 		usb_set_device_state(udev, USB_STATE_DEFAULT);
2598e7b77172SSarah Sharp 	} else {
25991da177e4SLinus Torvalds 		/* Reset the device; full speed may morph to high speed */
2600e7b77172SSarah Sharp 		/* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
26011da177e4SLinus Torvalds 		retval = hub_port_reset(hub, port1, udev, delay);
26021da177e4SLinus Torvalds 		if (retval < 0)		/* error or disconnect */
26031da177e4SLinus Torvalds 			goto fail;
26041da177e4SLinus Torvalds 		/* success, speed is known */
2605e7b77172SSarah Sharp 	}
26061da177e4SLinus Torvalds 	retval = -ENODEV;
26071da177e4SLinus Torvalds 
26081da177e4SLinus Torvalds 	if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
26091da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset changed speed!\n");
26101da177e4SLinus Torvalds 		goto fail;
26111da177e4SLinus Torvalds 	}
26121da177e4SLinus Torvalds 	oldspeed = udev->speed;
26131da177e4SLinus Torvalds 
26141da177e4SLinus Torvalds 	/* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
26151da177e4SLinus Torvalds 	 * it's fixed size except for full speed devices.
26165bb6e0aeSInaky Perez-Gonzalez 	 * For Wireless USB devices, ep0 max packet is always 512 (tho
26175bb6e0aeSInaky Perez-Gonzalez 	 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
26181da177e4SLinus Torvalds 	 */
26191da177e4SLinus Torvalds 	switch (udev->speed) {
26206b403b02SSarah Sharp 	case USB_SPEED_SUPER:
26215bb6e0aeSInaky Perez-Gonzalez 	case USB_SPEED_VARIABLE:	/* fixed at 512 */
2622551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
26235bb6e0aeSInaky Perez-Gonzalez 		break;
26241da177e4SLinus Torvalds 	case USB_SPEED_HIGH:		/* fixed at 64 */
2625551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
26261da177e4SLinus Torvalds 		break;
26271da177e4SLinus Torvalds 	case USB_SPEED_FULL:		/* 8, 16, 32, or 64 */
26281da177e4SLinus Torvalds 		/* to determine the ep0 maxpacket size, try to read
26291da177e4SLinus Torvalds 		 * the device descriptor to get bMaxPacketSize0 and
26301da177e4SLinus Torvalds 		 * then correct our initial guess.
26311da177e4SLinus Torvalds 		 */
2632551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
26331da177e4SLinus Torvalds 		break;
26341da177e4SLinus Torvalds 	case USB_SPEED_LOW:		/* fixed at 8 */
2635551509d2SHarvey Harrison 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
26361da177e4SLinus Torvalds 		break;
26371da177e4SLinus Torvalds 	default:
26381da177e4SLinus Torvalds 		goto fail;
26391da177e4SLinus Torvalds 	}
26401da177e4SLinus Torvalds 
264183a07196SInaky Perez-Gonzalez 	type = "";
264283a07196SInaky Perez-Gonzalez 	switch (udev->speed) {
26431da177e4SLinus Torvalds 	case USB_SPEED_LOW:	speed = "low";	break;
26441da177e4SLinus Torvalds 	case USB_SPEED_FULL:	speed = "full";	break;
26451da177e4SLinus Torvalds 	case USB_SPEED_HIGH:	speed = "high";	break;
26466b403b02SSarah Sharp 	case USB_SPEED_SUPER:
26476b403b02SSarah Sharp 				speed = "super";
26486b403b02SSarah Sharp 				break;
264983a07196SInaky Perez-Gonzalez 	case USB_SPEED_VARIABLE:
265083a07196SInaky Perez-Gonzalez 				speed = "variable";
265183a07196SInaky Perez-Gonzalez 				type = "Wireless ";
265283a07196SInaky Perez-Gonzalez 				break;
26531da177e4SLinus Torvalds 	default: 		speed = "?";	break;
265483a07196SInaky Perez-Gonzalez 	}
2655c6515272SSarah Sharp 	if (udev->speed != USB_SPEED_SUPER)
265683a07196SInaky Perez-Gonzalez 		dev_info(&udev->dev,
265783a07196SInaky Perez-Gonzalez 				"%s %s speed %sUSB device using %s and address %d\n",
265883a07196SInaky Perez-Gonzalez 				(udev->config) ? "reset" : "new", speed, type,
26594326ed0bSAlan Stern 				udev->bus->controller->driver->name, devnum);
26601da177e4SLinus Torvalds 
26611da177e4SLinus Torvalds 	/* Set up TT records, if needed  */
26621da177e4SLinus Torvalds 	if (hdev->tt) {
26631da177e4SLinus Torvalds 		udev->tt = hdev->tt;
26641da177e4SLinus Torvalds 		udev->ttport = hdev->ttport;
26651da177e4SLinus Torvalds 	} else if (udev->speed != USB_SPEED_HIGH
26661da177e4SLinus Torvalds 			&& hdev->speed == USB_SPEED_HIGH) {
26671da177e4SLinus Torvalds 		udev->tt = &hub->tt;
26681da177e4SLinus Torvalds 		udev->ttport = port1;
26691da177e4SLinus Torvalds 	}
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds 	/* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
26721da177e4SLinus Torvalds 	 * Because device hardware and firmware is sometimes buggy in
26731da177e4SLinus Torvalds 	 * this area, and this is how Linux has done it for ages.
26741da177e4SLinus Torvalds 	 * Change it cautiously.
26751da177e4SLinus Torvalds 	 *
26761da177e4SLinus Torvalds 	 * NOTE:  If USE_NEW_SCHEME() is true we will start by issuing
26771da177e4SLinus Torvalds 	 * a 64-byte GET_DESCRIPTOR request.  This is what Windows does,
26781da177e4SLinus Torvalds 	 * so it may help with some non-standards-compliant devices.
26791da177e4SLinus Torvalds 	 * Otherwise we start with SET_ADDRESS and then try to read the
26801da177e4SLinus Torvalds 	 * first 8 bytes of the device descriptor to get the ep0 maxpacket
26811da177e4SLinus Torvalds 	 * value.
26821da177e4SLinus Torvalds 	 */
26831da177e4SLinus Torvalds 	for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
2684c6515272SSarah Sharp 		/*
2685c6515272SSarah Sharp 		 * An xHCI controller cannot send any packets to a device until
2686c6515272SSarah Sharp 		 * a set address command successfully completes.
2687c6515272SSarah Sharp 		 */
2688c6515272SSarah Sharp 		if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
26891da177e4SLinus Torvalds 			struct usb_device_descriptor *buf;
26901da177e4SLinus Torvalds 			int r = 0;
26911da177e4SLinus Torvalds 
26921da177e4SLinus Torvalds #define GET_DESCRIPTOR_BUFSIZE	64
26931da177e4SLinus Torvalds 			buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
26941da177e4SLinus Torvalds 			if (!buf) {
26951da177e4SLinus Torvalds 				retval = -ENOMEM;
26961da177e4SLinus Torvalds 				continue;
26971da177e4SLinus Torvalds 			}
26981da177e4SLinus Torvalds 
2699b89ee19aSAlan Stern 			/* Retry on all errors; some devices are flakey.
2700b89ee19aSAlan Stern 			 * 255 is for WUSB devices, we actually need to use
2701b89ee19aSAlan Stern 			 * 512 (WUSB1.0[4.8.1]).
27021da177e4SLinus Torvalds 			 */
27031da177e4SLinus Torvalds 			for (j = 0; j < 3; ++j) {
27041da177e4SLinus Torvalds 				buf->bMaxPacketSize0 = 0;
27051da177e4SLinus Torvalds 				r = usb_control_msg(udev, usb_rcvaddr0pipe(),
27061da177e4SLinus Torvalds 					USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
27071da177e4SLinus Torvalds 					USB_DT_DEVICE << 8, 0,
27081da177e4SLinus Torvalds 					buf, GET_DESCRIPTOR_BUFSIZE,
2709fd7c519dSJaroslav Kysela 					initial_descriptor_timeout);
27101da177e4SLinus Torvalds 				switch (buf->bMaxPacketSize0) {
27115bb6e0aeSInaky Perez-Gonzalez 				case 8: case 16: case 32: case 64: case 255:
27121da177e4SLinus Torvalds 					if (buf->bDescriptorType ==
27131da177e4SLinus Torvalds 							USB_DT_DEVICE) {
27141da177e4SLinus Torvalds 						r = 0;
27151da177e4SLinus Torvalds 						break;
27161da177e4SLinus Torvalds 					}
27171da177e4SLinus Torvalds 					/* FALL THROUGH */
27181da177e4SLinus Torvalds 				default:
27191da177e4SLinus Torvalds 					if (r == 0)
27201da177e4SLinus Torvalds 						r = -EPROTO;
27211da177e4SLinus Torvalds 					break;
27221da177e4SLinus Torvalds 				}
27231da177e4SLinus Torvalds 				if (r == 0)
27241da177e4SLinus Torvalds 					break;
27251da177e4SLinus Torvalds 			}
27261da177e4SLinus Torvalds 			udev->descriptor.bMaxPacketSize0 =
27271da177e4SLinus Torvalds 					buf->bMaxPacketSize0;
27281da177e4SLinus Torvalds 			kfree(buf);
27291da177e4SLinus Torvalds 
27301da177e4SLinus Torvalds 			retval = hub_port_reset(hub, port1, udev, delay);
27311da177e4SLinus Torvalds 			if (retval < 0)		/* error or disconnect */
27321da177e4SLinus Torvalds 				goto fail;
27331da177e4SLinus Torvalds 			if (oldspeed != udev->speed) {
27341da177e4SLinus Torvalds 				dev_dbg(&udev->dev,
27351da177e4SLinus Torvalds 					"device reset changed speed!\n");
27361da177e4SLinus Torvalds 				retval = -ENODEV;
27371da177e4SLinus Torvalds 				goto fail;
27381da177e4SLinus Torvalds 			}
27391da177e4SLinus Torvalds 			if (r) {
2740b9cef6c3SWu Fengguang 				dev_err(&udev->dev,
2741b9cef6c3SWu Fengguang 					"device descriptor read/64, error %d\n",
2742b9cef6c3SWu Fengguang 					r);
27431da177e4SLinus Torvalds 				retval = -EMSGSIZE;
27441da177e4SLinus Torvalds 				continue;
27451da177e4SLinus Torvalds 			}
27461da177e4SLinus Torvalds #undef GET_DESCRIPTOR_BUFSIZE
27471da177e4SLinus Torvalds 		}
27481da177e4SLinus Torvalds 
27496c529cdcSInaky Perez-Gonzalez  		/*
27506c529cdcSInaky Perez-Gonzalez  		 * If device is WUSB, we already assigned an
27516c529cdcSInaky Perez-Gonzalez  		 * unauthorized address in the Connect Ack sequence;
27526c529cdcSInaky Perez-Gonzalez  		 * authorization will assign the final address.
27536c529cdcSInaky Perez-Gonzalez  		 */
27546c529cdcSInaky Perez-Gonzalez 		if (udev->wusb == 0) {
27551da177e4SLinus Torvalds 			for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
27564326ed0bSAlan Stern 				retval = hub_set_address(udev, devnum);
27571da177e4SLinus Torvalds 				if (retval >= 0)
27581da177e4SLinus Torvalds 					break;
27591da177e4SLinus Torvalds 				msleep(200);
27601da177e4SLinus Torvalds 			}
27611da177e4SLinus Torvalds 			if (retval < 0) {
27621da177e4SLinus Torvalds 				dev_err(&udev->dev,
27631da177e4SLinus Torvalds 					"device not accepting address %d, error %d\n",
27644326ed0bSAlan Stern 					devnum, retval);
27651da177e4SLinus Torvalds 				goto fail;
27661da177e4SLinus Torvalds 			}
2767c6515272SSarah Sharp 			if (udev->speed == USB_SPEED_SUPER) {
2768c6515272SSarah Sharp 				devnum = udev->devnum;
2769c6515272SSarah Sharp 				dev_info(&udev->dev,
2770c6515272SSarah Sharp 						"%s SuperSpeed USB device using %s and address %d\n",
2771c6515272SSarah Sharp 						(udev->config) ? "reset" : "new",
2772c6515272SSarah Sharp 						udev->bus->controller->driver->name, devnum);
2773c6515272SSarah Sharp 			}
27741da177e4SLinus Torvalds 
27751da177e4SLinus Torvalds 			/* cope with hardware quirkiness:
27761da177e4SLinus Torvalds 			 *  - let SET_ADDRESS settle, some device hardware wants it
27771da177e4SLinus Torvalds 			 *  - read ep0 maxpacket even for high and low speed,
27781da177e4SLinus Torvalds 			 */
27791da177e4SLinus Torvalds 			msleep(10);
2780c6515272SSarah Sharp 			if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
27811da177e4SLinus Torvalds 				break;
27826c529cdcSInaky Perez-Gonzalez   		}
27831da177e4SLinus Torvalds 
27841da177e4SLinus Torvalds 		retval = usb_get_device_descriptor(udev, 8);
27851da177e4SLinus Torvalds 		if (retval < 8) {
2786b9cef6c3SWu Fengguang 			dev_err(&udev->dev,
2787b9cef6c3SWu Fengguang 					"device descriptor read/8, error %d\n",
2788b9cef6c3SWu Fengguang 					retval);
27891da177e4SLinus Torvalds 			if (retval >= 0)
27901da177e4SLinus Torvalds 				retval = -EMSGSIZE;
27911da177e4SLinus Torvalds 		} else {
27921da177e4SLinus Torvalds 			retval = 0;
27931da177e4SLinus Torvalds 			break;
27941da177e4SLinus Torvalds 		}
27951da177e4SLinus Torvalds 	}
27961da177e4SLinus Torvalds 	if (retval)
27971da177e4SLinus Torvalds 		goto fail;
27981da177e4SLinus Torvalds 
27996b403b02SSarah Sharp 	if (udev->descriptor.bMaxPacketSize0 == 0xff ||
28006b403b02SSarah Sharp 			udev->speed == USB_SPEED_SUPER)
28016b403b02SSarah Sharp 		i = 512;
28026b403b02SSarah Sharp 	else
28036b403b02SSarah Sharp 		i = udev->descriptor.bMaxPacketSize0;
28041da177e4SLinus Torvalds 	if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) {
28051da177e4SLinus Torvalds 		if (udev->speed != USB_SPEED_FULL ||
28061da177e4SLinus Torvalds 				!(i == 8 || i == 16 || i == 32 || i == 64)) {
28071da177e4SLinus Torvalds 			dev_err(&udev->dev, "ep0 maxpacket = %d\n", i);
28081da177e4SLinus Torvalds 			retval = -EMSGSIZE;
28091da177e4SLinus Torvalds 			goto fail;
28101da177e4SLinus Torvalds 		}
28111da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
28121da177e4SLinus Torvalds 		udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
2813fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
28141da177e4SLinus Torvalds 	}
28151da177e4SLinus Torvalds 
28161da177e4SLinus Torvalds 	retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
28171da177e4SLinus Torvalds 	if (retval < (signed)sizeof(udev->descriptor)) {
2818b9cef6c3SWu Fengguang 		dev_err(&udev->dev, "device descriptor read/all, error %d\n",
2819b9cef6c3SWu Fengguang 			retval);
28201da177e4SLinus Torvalds 		if (retval >= 0)
28211da177e4SLinus Torvalds 			retval = -ENOMSG;
28221da177e4SLinus Torvalds 		goto fail;
28231da177e4SLinus Torvalds 	}
28241da177e4SLinus Torvalds 
28251da177e4SLinus Torvalds 	retval = 0;
28261da177e4SLinus Torvalds 
28271da177e4SLinus Torvalds fail:
28284326ed0bSAlan Stern 	if (retval) {
28291da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 0);
28304953d141SDavid Vrabel 		update_address(udev, devnum);	/* for disconnect processing */
28314326ed0bSAlan Stern 	}
28324186ecf8SArjan van de Ven 	mutex_unlock(&usb_address0_mutex);
28331da177e4SLinus Torvalds 	return retval;
28341da177e4SLinus Torvalds }
28351da177e4SLinus Torvalds 
28361da177e4SLinus Torvalds static void
28371da177e4SLinus Torvalds check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
28381da177e4SLinus Torvalds {
28391da177e4SLinus Torvalds 	struct usb_qualifier_descriptor	*qual;
28401da177e4SLinus Torvalds 	int				status;
28411da177e4SLinus Torvalds 
2842e94b1766SChristoph Lameter 	qual = kmalloc (sizeof *qual, GFP_KERNEL);
28431da177e4SLinus Torvalds 	if (qual == NULL)
28441da177e4SLinus Torvalds 		return;
28451da177e4SLinus Torvalds 
28461da177e4SLinus Torvalds 	status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
28471da177e4SLinus Torvalds 			qual, sizeof *qual);
28481da177e4SLinus Torvalds 	if (status == sizeof *qual) {
28491da177e4SLinus Torvalds 		dev_info(&udev->dev, "not running at top speed; "
28501da177e4SLinus Torvalds 			"connect to a high speed hub\n");
28511da177e4SLinus Torvalds 		/* hub LEDs are probably harder to miss than syslog */
28521da177e4SLinus Torvalds 		if (hub->has_indicators) {
28531da177e4SLinus Torvalds 			hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
2854c4028958SDavid Howells 			schedule_delayed_work (&hub->leds, 0);
28551da177e4SLinus Torvalds 		}
28561da177e4SLinus Torvalds 	}
28571da177e4SLinus Torvalds 	kfree(qual);
28581da177e4SLinus Torvalds }
28591da177e4SLinus Torvalds 
28601da177e4SLinus Torvalds static unsigned
28611da177e4SLinus Torvalds hub_power_remaining (struct usb_hub *hub)
28621da177e4SLinus Torvalds {
28631da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
28641da177e4SLinus Torvalds 	int remaining;
286555c52718SAlan Stern 	int port1;
28661da177e4SLinus Torvalds 
286755c52718SAlan Stern 	if (!hub->limited_power)
28681da177e4SLinus Torvalds 		return 0;
28691da177e4SLinus Torvalds 
287055c52718SAlan Stern 	remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
287155c52718SAlan Stern 	for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
287255c52718SAlan Stern 		struct usb_device	*udev = hdev->children[port1 - 1];
287355c52718SAlan Stern 		int			delta;
28741da177e4SLinus Torvalds 
28751da177e4SLinus Torvalds 		if (!udev)
28761da177e4SLinus Torvalds 			continue;
28771da177e4SLinus Torvalds 
287855c52718SAlan Stern 		/* Unconfigured devices may not use more than 100mA,
287955c52718SAlan Stern 		 * or 8mA for OTG ports */
28801da177e4SLinus Torvalds 		if (udev->actconfig)
288155c52718SAlan Stern 			delta = udev->actconfig->desc.bMaxPower * 2;
288255c52718SAlan Stern 		else if (port1 != udev->bus->otg_port || hdev->parent)
288355c52718SAlan Stern 			delta = 100;
28841da177e4SLinus Torvalds 		else
288555c52718SAlan Stern 			delta = 8;
288655c52718SAlan Stern 		if (delta > hub->mA_per_port)
2887b9cef6c3SWu Fengguang 			dev_warn(&udev->dev,
2888b9cef6c3SWu Fengguang 				 "%dmA is over %umA budget for port %d!\n",
288955c52718SAlan Stern 				 delta, hub->mA_per_port, port1);
28901da177e4SLinus Torvalds 		remaining -= delta;
28911da177e4SLinus Torvalds 	}
28921da177e4SLinus Torvalds 	if (remaining < 0) {
289355c52718SAlan Stern 		dev_warn(hub->intfdev, "%dmA over power budget!\n",
289455c52718SAlan Stern 			- remaining);
28951da177e4SLinus Torvalds 		remaining = 0;
28961da177e4SLinus Torvalds 	}
28971da177e4SLinus Torvalds 	return remaining;
28981da177e4SLinus Torvalds }
28991da177e4SLinus Torvalds 
29001da177e4SLinus Torvalds /* Handle physical or logical connection change events.
29011da177e4SLinus Torvalds  * This routine is called when:
29021da177e4SLinus Torvalds  * 	a port connection-change occurs;
29031da177e4SLinus Torvalds  *	a port enable-change occurs (often caused by EMI);
2904742120c6SMing Lei  *	usb_reset_and_verify_device() encounters changed descriptors (as from
29051da177e4SLinus Torvalds  *		a firmware download)
29061da177e4SLinus Torvalds  * caller already locked the hub
29071da177e4SLinus Torvalds  */
29081da177e4SLinus Torvalds static void hub_port_connect_change(struct usb_hub *hub, int port1,
29091da177e4SLinus Torvalds 					u16 portstatus, u16 portchange)
29101da177e4SLinus Torvalds {
29111da177e4SLinus Torvalds 	struct usb_device *hdev = hub->hdev;
29121da177e4SLinus Torvalds 	struct device *hub_dev = hub->intfdev;
291390da096eSBalaji Rao 	struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
291424618b0cSAlan Stern 	unsigned wHubCharacteristics =
291524618b0cSAlan Stern 			le16_to_cpu(hub->descriptor->wHubCharacteristics);
29168808f00cSAlan Stern 	struct usb_device *udev;
29171da177e4SLinus Torvalds 	int status, i;
29181da177e4SLinus Torvalds 
29191da177e4SLinus Torvalds 	dev_dbg (hub_dev,
29201da177e4SLinus Torvalds 		"port %d, status %04x, change %04x, %s\n",
29211da177e4SLinus Torvalds 		port1, portstatus, portchange, portspeed (portstatus));
29221da177e4SLinus Torvalds 
29231da177e4SLinus Torvalds 	if (hub->has_indicators) {
29241da177e4SLinus Torvalds 		set_port_led(hub, port1, HUB_LED_AUTO);
29251da177e4SLinus Torvalds 		hub->indicator[port1-1] = INDICATOR_AUTO;
29261da177e4SLinus Torvalds 	}
29271da177e4SLinus Torvalds 
29281da177e4SLinus Torvalds #ifdef	CONFIG_USB_OTG
29291da177e4SLinus Torvalds 	/* during HNP, don't repeat the debounce */
29301da177e4SLinus Torvalds 	if (hdev->bus->is_b_host)
293124618b0cSAlan Stern 		portchange &= ~(USB_PORT_STAT_C_CONNECTION |
293224618b0cSAlan Stern 				USB_PORT_STAT_C_ENABLE);
29331da177e4SLinus Torvalds #endif
29341da177e4SLinus Torvalds 
29358808f00cSAlan Stern 	/* Try to resuscitate an existing device */
29368808f00cSAlan Stern 	udev = hdev->children[port1-1];
29378808f00cSAlan Stern 	if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
29388808f00cSAlan Stern 			udev->state != USB_STATE_NOTATTACHED) {
29398808f00cSAlan Stern 		usb_lock_device(udev);
29408808f00cSAlan Stern 		if (portstatus & USB_PORT_STAT_ENABLE) {
29418808f00cSAlan Stern 			status = 0;		/* Nothing to do */
29428808f00cSAlan Stern 
29438808f00cSAlan Stern #ifdef CONFIG_USB_SUSPEND
29445257d97aSAlan Stern 		} else if (udev->state == USB_STATE_SUSPENDED &&
29455257d97aSAlan Stern 				udev->persist_enabled) {
29468808f00cSAlan Stern 			/* For a suspended device, treat this as a
29478808f00cSAlan Stern 			 * remote wakeup event.
29488808f00cSAlan Stern 			 */
29498808f00cSAlan Stern 			status = remote_wakeup(udev);
29508808f00cSAlan Stern #endif
29518808f00cSAlan Stern 
29528808f00cSAlan Stern 		} else {
29535257d97aSAlan Stern 			status = -ENODEV;	/* Don't resuscitate */
29548808f00cSAlan Stern 		}
29558808f00cSAlan Stern 		usb_unlock_device(udev);
29568808f00cSAlan Stern 
29578808f00cSAlan Stern 		if (status == 0) {
29588808f00cSAlan Stern 			clear_bit(port1, hub->change_bits);
29598808f00cSAlan Stern 			return;
29608808f00cSAlan Stern 		}
29618808f00cSAlan Stern 	}
29628808f00cSAlan Stern 
296324618b0cSAlan Stern 	/* Disconnect any existing devices under this port */
29648808f00cSAlan Stern 	if (udev)
296524618b0cSAlan Stern 		usb_disconnect(&hdev->children[port1-1]);
296624618b0cSAlan Stern 	clear_bit(port1, hub->change_bits);
296724618b0cSAlan Stern 
29685257d97aSAlan Stern 	if (portchange & (USB_PORT_STAT_C_CONNECTION |
29695257d97aSAlan Stern 				USB_PORT_STAT_C_ENABLE)) {
29705257d97aSAlan Stern 		status = hub_port_debounce(hub, port1);
29715257d97aSAlan Stern 		if (status < 0) {
29725257d97aSAlan Stern 			if (printk_ratelimit())
29735257d97aSAlan Stern 				dev_err(hub_dev, "connect-debounce failed, "
29745257d97aSAlan Stern 						"port %d disabled\n", port1);
29755257d97aSAlan Stern 			portstatus &= ~USB_PORT_STAT_CONNECTION;
29765257d97aSAlan Stern 		} else {
29775257d97aSAlan Stern 			portstatus = status;
29785257d97aSAlan Stern 		}
29795257d97aSAlan Stern 	}
29805257d97aSAlan Stern 
298124618b0cSAlan Stern 	/* Return now if debouncing failed or nothing is connected */
29821da177e4SLinus Torvalds 	if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
29831da177e4SLinus Torvalds 
29841da177e4SLinus Torvalds 		/* maybe switch power back on (e.g. root hub was reset) */
298574ad9bd2SGreg Kroah-Hartman 		if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
29861da177e4SLinus Torvalds 				&& !(portstatus & (1 << USB_PORT_FEAT_POWER)))
29871da177e4SLinus Torvalds 			set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
29881da177e4SLinus Torvalds 
29891da177e4SLinus Torvalds 		if (portstatus & USB_PORT_STAT_ENABLE)
29901da177e4SLinus Torvalds   			goto done;
29911da177e4SLinus Torvalds 		return;
29921da177e4SLinus Torvalds 	}
29931da177e4SLinus Torvalds 
29941da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; i++) {
29951da177e4SLinus Torvalds 
29961da177e4SLinus Torvalds 		/* reallocate for each attempt, since references
29971da177e4SLinus Torvalds 		 * to the previous one can escape in various ways
29981da177e4SLinus Torvalds 		 */
29991da177e4SLinus Torvalds 		udev = usb_alloc_dev(hdev, hdev->bus, port1);
30001da177e4SLinus Torvalds 		if (!udev) {
30011da177e4SLinus Torvalds 			dev_err (hub_dev,
30021da177e4SLinus Torvalds 				"couldn't allocate port %d usb_device\n",
30031da177e4SLinus Torvalds 				port1);
30041da177e4SLinus Torvalds 			goto done;
30051da177e4SLinus Torvalds 		}
30061da177e4SLinus Torvalds 
30071da177e4SLinus Torvalds 		usb_set_device_state(udev, USB_STATE_POWERED);
300855c52718SAlan Stern  		udev->bus_mA = hub->mA_per_port;
3009b6956ffaSAlan Stern 		udev->level = hdev->level + 1;
30108af548dcSInaky Perez-Gonzalez 		udev->wusb = hub_is_wusb(hub);
30111da177e4SLinus Torvalds 
3012e7b77172SSarah Sharp 		/*
3013e7b77172SSarah Sharp 		 * USB 3.0 devices are reset automatically before the connect
3014e7b77172SSarah Sharp 		 * port status change appears, and the root hub port status
3015e7b77172SSarah Sharp 		 * shows the correct speed.  We also get port change
3016e7b77172SSarah Sharp 		 * notifications for USB 3.0 devices from the USB 3.0 portion of
3017e7b77172SSarah Sharp 		 * an external USB 3.0 hub, but this isn't handled correctly yet
3018e7b77172SSarah Sharp 		 * FIXME.
3019e7b77172SSarah Sharp 		 */
3020e7b77172SSarah Sharp 
3021e7b77172SSarah Sharp 		if (!(hcd->driver->flags & HCD_USB3))
3022e7b77172SSarah Sharp 			udev->speed = USB_SPEED_UNKNOWN;
3023e7b77172SSarah Sharp 		else if ((hdev->parent == NULL) &&
3024e7b77172SSarah Sharp 				(portstatus & (1 << USB_PORT_FEAT_SUPERSPEED)))
3025e7b77172SSarah Sharp 			udev->speed = USB_SPEED_SUPER;
3026e7b77172SSarah Sharp 		else
3027e7b77172SSarah Sharp 			udev->speed = USB_SPEED_UNKNOWN;
3028e7b77172SSarah Sharp 
3029c6515272SSarah Sharp 		/*
3030c6515272SSarah Sharp 		 * xHCI needs to issue an address device command later
3031c6515272SSarah Sharp 		 * in the hub_port_init sequence for SS/HS/FS/LS devices.
3032c6515272SSarah Sharp 		 */
3033c6515272SSarah Sharp 		if (!(hcd->driver->flags & HCD_USB3)) {
3034c6515272SSarah Sharp 			/* set the address */
3035c6515272SSarah Sharp 			choose_address(udev);
3036c6515272SSarah Sharp 			if (udev->devnum <= 0) {
3037c6515272SSarah Sharp 				status = -ENOTCONN;	/* Don't retry */
3038c6515272SSarah Sharp 				goto loop;
3039c6515272SSarah Sharp 			}
3040c6515272SSarah Sharp 		}
3041c6515272SSarah Sharp 
3042e7b77172SSarah Sharp 		/* reset (non-USB 3.0 devices) and get descriptor */
30431da177e4SLinus Torvalds 		status = hub_port_init(hub, udev, port1, i);
30441da177e4SLinus Torvalds 		if (status < 0)
30451da177e4SLinus Torvalds 			goto loop;
30461da177e4SLinus Torvalds 
30471da177e4SLinus Torvalds 		/* consecutive bus-powered hubs aren't reliable; they can
30481da177e4SLinus Torvalds 		 * violate the voltage drop budget.  if the new child has
30491da177e4SLinus Torvalds 		 * a "powered" LED, users should notice we didn't enable it
30501da177e4SLinus Torvalds 		 * (without reading syslog), even without per-port LEDs
30511da177e4SLinus Torvalds 		 * on the parent.
30521da177e4SLinus Torvalds 		 */
30531da177e4SLinus Torvalds 		if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
305455c52718SAlan Stern 				&& udev->bus_mA <= 100) {
30551da177e4SLinus Torvalds 			u16	devstat;
30561da177e4SLinus Torvalds 
30571da177e4SLinus Torvalds 			status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
30581da177e4SLinus Torvalds 					&devstat);
305955c52718SAlan Stern 			if (status < 2) {
30601da177e4SLinus Torvalds 				dev_dbg(&udev->dev, "get status %d ?\n", status);
30611da177e4SLinus Torvalds 				goto loop_disable;
30621da177e4SLinus Torvalds 			}
306355c52718SAlan Stern 			le16_to_cpus(&devstat);
30641da177e4SLinus Torvalds 			if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
30651da177e4SLinus Torvalds 				dev_err(&udev->dev,
30661da177e4SLinus Torvalds 					"can't connect bus-powered hub "
30671da177e4SLinus Torvalds 					"to this port\n");
30681da177e4SLinus Torvalds 				if (hub->has_indicators) {
30691da177e4SLinus Torvalds 					hub->indicator[port1-1] =
30701da177e4SLinus Torvalds 						INDICATOR_AMBER_BLINK;
3071c4028958SDavid Howells 					schedule_delayed_work (&hub->leds, 0);
30721da177e4SLinus Torvalds 				}
30731da177e4SLinus Torvalds 				status = -ENOTCONN;	/* Don't retry */
30741da177e4SLinus Torvalds 				goto loop_disable;
30751da177e4SLinus Torvalds 			}
30761da177e4SLinus Torvalds 		}
30771da177e4SLinus Torvalds 
30781da177e4SLinus Torvalds 		/* check for devices running slower than they could */
30791da177e4SLinus Torvalds 		if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
30801da177e4SLinus Torvalds 				&& udev->speed == USB_SPEED_FULL
30811da177e4SLinus Torvalds 				&& highspeed_hubs != 0)
30821da177e4SLinus Torvalds 			check_highspeed (hub, udev, port1);
30831da177e4SLinus Torvalds 
30841da177e4SLinus Torvalds 		/* Store the parent's children[] pointer.  At this point
30851da177e4SLinus Torvalds 		 * udev becomes globally accessible, although presumably
30861da177e4SLinus Torvalds 		 * no one will look at it until hdev is unlocked.
30871da177e4SLinus Torvalds 		 */
30881da177e4SLinus Torvalds 		status = 0;
30891da177e4SLinus Torvalds 
30901da177e4SLinus Torvalds 		/* We mustn't add new devices if the parent hub has
30911da177e4SLinus Torvalds 		 * been disconnected; we would race with the
30921da177e4SLinus Torvalds 		 * recursively_mark_NOTATTACHED() routine.
30931da177e4SLinus Torvalds 		 */
30941da177e4SLinus Torvalds 		spin_lock_irq(&device_state_lock);
30951da177e4SLinus Torvalds 		if (hdev->state == USB_STATE_NOTATTACHED)
30961da177e4SLinus Torvalds 			status = -ENOTCONN;
30971da177e4SLinus Torvalds 		else
30981da177e4SLinus Torvalds 			hdev->children[port1-1] = udev;
30991da177e4SLinus Torvalds 		spin_unlock_irq(&device_state_lock);
31001da177e4SLinus Torvalds 
31011da177e4SLinus Torvalds 		/* Run it through the hoops (find a driver, etc) */
31021da177e4SLinus Torvalds 		if (!status) {
31031da177e4SLinus Torvalds 			status = usb_new_device(udev);
31041da177e4SLinus Torvalds 			if (status) {
31051da177e4SLinus Torvalds 				spin_lock_irq(&device_state_lock);
31061da177e4SLinus Torvalds 				hdev->children[port1-1] = NULL;
31071da177e4SLinus Torvalds 				spin_unlock_irq(&device_state_lock);
31081da177e4SLinus Torvalds 			}
31091da177e4SLinus Torvalds 		}
31101da177e4SLinus Torvalds 
31111da177e4SLinus Torvalds 		if (status)
31121da177e4SLinus Torvalds 			goto loop_disable;
31131da177e4SLinus Torvalds 
31141da177e4SLinus Torvalds 		status = hub_power_remaining(hub);
31151da177e4SLinus Torvalds 		if (status)
311655c52718SAlan Stern 			dev_dbg(hub_dev, "%dmA power budget left\n", status);
31171da177e4SLinus Torvalds 
31181da177e4SLinus Torvalds 		return;
31191da177e4SLinus Torvalds 
31201da177e4SLinus Torvalds loop_disable:
31211da177e4SLinus Torvalds 		hub_port_disable(hub, port1, 1);
31221da177e4SLinus Torvalds loop:
3123fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
31241da177e4SLinus Torvalds 		release_address(udev);
31251da177e4SLinus Torvalds 		usb_put_dev(udev);
3126ffcdc18dSVikram Pandita 		if ((status == -ENOTCONN) || (status == -ENOTSUPP))
31271da177e4SLinus Torvalds 			break;
31281da177e4SLinus Torvalds 	}
31293a31155cSAlan Stern 	if (hub->hdev->parent ||
31303a31155cSAlan Stern 			!hcd->driver->port_handed_over ||
31313a31155cSAlan Stern 			!(hcd->driver->port_handed_over)(hcd, port1))
31323a31155cSAlan Stern 		dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
31333a31155cSAlan Stern 				port1);
31341da177e4SLinus Torvalds 
31351da177e4SLinus Torvalds done:
31361da177e4SLinus Torvalds 	hub_port_disable(hub, port1, 1);
313790da096eSBalaji Rao 	if (hcd->driver->relinquish_port && !hub->hdev->parent)
313890da096eSBalaji Rao 		hcd->driver->relinquish_port(hcd, port1);
31391da177e4SLinus Torvalds }
31401da177e4SLinus Torvalds 
31411da177e4SLinus Torvalds static void hub_events(void)
31421da177e4SLinus Torvalds {
31431da177e4SLinus Torvalds 	struct list_head *tmp;
31441da177e4SLinus Torvalds 	struct usb_device *hdev;
31451da177e4SLinus Torvalds 	struct usb_interface *intf;
31461da177e4SLinus Torvalds 	struct usb_hub *hub;
31471da177e4SLinus Torvalds 	struct device *hub_dev;
31481da177e4SLinus Torvalds 	u16 hubstatus;
31491da177e4SLinus Torvalds 	u16 hubchange;
31501da177e4SLinus Torvalds 	u16 portstatus;
31511da177e4SLinus Torvalds 	u16 portchange;
31521da177e4SLinus Torvalds 	int i, ret;
31531da177e4SLinus Torvalds 	int connect_change;
31541da177e4SLinus Torvalds 
31551da177e4SLinus Torvalds 	/*
31561da177e4SLinus Torvalds 	 *  We restart the list every time to avoid a deadlock with
31571da177e4SLinus Torvalds 	 * deleting hubs downstream from this one. This should be
31581da177e4SLinus Torvalds 	 * safe since we delete the hub from the event list.
31591da177e4SLinus Torvalds 	 * Not the most efficient, but avoids deadlocks.
31601da177e4SLinus Torvalds 	 */
31611da177e4SLinus Torvalds 	while (1) {
31621da177e4SLinus Torvalds 
31631da177e4SLinus Torvalds 		/* Grab the first entry at the beginning of the list */
31641da177e4SLinus Torvalds 		spin_lock_irq(&hub_event_lock);
31651da177e4SLinus Torvalds 		if (list_empty(&hub_event_list)) {
31661da177e4SLinus Torvalds 			spin_unlock_irq(&hub_event_lock);
31671da177e4SLinus Torvalds 			break;
31681da177e4SLinus Torvalds 		}
31691da177e4SLinus Torvalds 
31701da177e4SLinus Torvalds 		tmp = hub_event_list.next;
31711da177e4SLinus Torvalds 		list_del_init(tmp);
31721da177e4SLinus Torvalds 
31731da177e4SLinus Torvalds 		hub = list_entry(tmp, struct usb_hub, event_list);
3174e8054854SAlan Stern 		kref_get(&hub->kref);
3175e8054854SAlan Stern 		spin_unlock_irq(&hub_event_lock);
31761da177e4SLinus Torvalds 
3177e8054854SAlan Stern 		hdev = hub->hdev;
3178e8054854SAlan Stern 		hub_dev = hub->intfdev;
3179e8054854SAlan Stern 		intf = to_usb_interface(hub_dev);
318040f122f3SAlan Stern 		dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
31811da177e4SLinus Torvalds 				hdev->state, hub->descriptor
31821da177e4SLinus Torvalds 					? hub->descriptor->bNbrPorts
31831da177e4SLinus Torvalds 					: 0,
31841da177e4SLinus Torvalds 				/* NOTE: expects max 15 ports... */
31851da177e4SLinus Torvalds 				(u16) hub->change_bits[0],
318640f122f3SAlan Stern 				(u16) hub->event_bits[0]);
31871da177e4SLinus Torvalds 
31881da177e4SLinus Torvalds 		/* Lock the device, then check to see if we were
31891da177e4SLinus Torvalds 		 * disconnected while waiting for the lock to succeed. */
319006b84e8aSAlan Stern 		usb_lock_device(hdev);
3191e8054854SAlan Stern 		if (unlikely(hub->disconnected))
31921da177e4SLinus Torvalds 			goto loop;
31931da177e4SLinus Torvalds 
31941da177e4SLinus Torvalds 		/* If the hub has died, clean up after it */
31951da177e4SLinus Torvalds 		if (hdev->state == USB_STATE_NOTATTACHED) {
31967de18d8bSAlan Stern 			hub->error = -ENODEV;
31974330354fSAlan Stern 			hub_quiesce(hub, HUB_DISCONNECT);
31981da177e4SLinus Torvalds 			goto loop;
31991da177e4SLinus Torvalds 		}
32001da177e4SLinus Torvalds 
320140f122f3SAlan Stern 		/* Autoresume */
320240f122f3SAlan Stern 		ret = usb_autopm_get_interface(intf);
320340f122f3SAlan Stern 		if (ret) {
320440f122f3SAlan Stern 			dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
32051da177e4SLinus Torvalds 			goto loop;
320640f122f3SAlan Stern 		}
320740f122f3SAlan Stern 
320840f122f3SAlan Stern 		/* If this is an inactive hub, do nothing */
320940f122f3SAlan Stern 		if (hub->quiescing)
321040f122f3SAlan Stern 			goto loop_autopm;
32111da177e4SLinus Torvalds 
32121da177e4SLinus Torvalds 		if (hub->error) {
32131da177e4SLinus Torvalds 			dev_dbg (hub_dev, "resetting for error %d\n",
32141da177e4SLinus Torvalds 				hub->error);
32151da177e4SLinus Torvalds 
3216742120c6SMing Lei 			ret = usb_reset_device(hdev);
32171da177e4SLinus Torvalds 			if (ret) {
32181da177e4SLinus Torvalds 				dev_dbg (hub_dev,
32191da177e4SLinus Torvalds 					"error resetting hub: %d\n", ret);
322040f122f3SAlan Stern 				goto loop_autopm;
32211da177e4SLinus Torvalds 			}
32221da177e4SLinus Torvalds 
32231da177e4SLinus Torvalds 			hub->nerrors = 0;
32241da177e4SLinus Torvalds 			hub->error = 0;
32251da177e4SLinus Torvalds 		}
32261da177e4SLinus Torvalds 
32271da177e4SLinus Torvalds 		/* deal with port status changes */
32281da177e4SLinus Torvalds 		for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
32291da177e4SLinus Torvalds 			if (test_bit(i, hub->busy_bits))
32301da177e4SLinus Torvalds 				continue;
32311da177e4SLinus Torvalds 			connect_change = test_bit(i, hub->change_bits);
32321da177e4SLinus Torvalds 			if (!test_and_clear_bit(i, hub->event_bits) &&
32336ee0b270SAlan Stern 					!connect_change)
32341da177e4SLinus Torvalds 				continue;
32351da177e4SLinus Torvalds 
32361da177e4SLinus Torvalds 			ret = hub_port_status(hub, i,
32371da177e4SLinus Torvalds 					&portstatus, &portchange);
32381da177e4SLinus Torvalds 			if (ret < 0)
32391da177e4SLinus Torvalds 				continue;
32401da177e4SLinus Torvalds 
32411da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_CONNECTION) {
32421da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
32431da177e4SLinus Torvalds 					USB_PORT_FEAT_C_CONNECTION);
32441da177e4SLinus Torvalds 				connect_change = 1;
32451da177e4SLinus Torvalds 			}
32461da177e4SLinus Torvalds 
32471da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_ENABLE) {
32481da177e4SLinus Torvalds 				if (!connect_change)
32491da177e4SLinus Torvalds 					dev_dbg (hub_dev,
32501da177e4SLinus Torvalds 						"port %d enable change, "
32511da177e4SLinus Torvalds 						"status %08x\n",
32521da177e4SLinus Torvalds 						i, portstatus);
32531da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
32541da177e4SLinus Torvalds 					USB_PORT_FEAT_C_ENABLE);
32551da177e4SLinus Torvalds 
32561da177e4SLinus Torvalds 				/*
32571da177e4SLinus Torvalds 				 * EM interference sometimes causes badly
32581da177e4SLinus Torvalds 				 * shielded USB devices to be shutdown by
32591da177e4SLinus Torvalds 				 * the hub, this hack enables them again.
32601da177e4SLinus Torvalds 				 * Works at least with mouse driver.
32611da177e4SLinus Torvalds 				 */
32621da177e4SLinus Torvalds 				if (!(portstatus & USB_PORT_STAT_ENABLE)
32631da177e4SLinus Torvalds 				    && !connect_change
32641da177e4SLinus Torvalds 				    && hdev->children[i-1]) {
32651da177e4SLinus Torvalds 					dev_err (hub_dev,
32661da177e4SLinus Torvalds 					    "port %i "
32671da177e4SLinus Torvalds 					    "disabled by hub (EMI?), "
32681da177e4SLinus Torvalds 					    "re-enabling...\n",
32691da177e4SLinus Torvalds 						i);
32701da177e4SLinus Torvalds 					connect_change = 1;
32711da177e4SLinus Torvalds 				}
32721da177e4SLinus Torvalds 			}
32731da177e4SLinus Torvalds 
32741da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_SUSPEND) {
32758808f00cSAlan Stern 				struct usb_device *udev;
32768808f00cSAlan Stern 
32771da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
32781da177e4SLinus Torvalds 					USB_PORT_FEAT_C_SUSPEND);
32798808f00cSAlan Stern 				udev = hdev->children[i-1];
32808808f00cSAlan Stern 				if (udev) {
32818808f00cSAlan Stern 					usb_lock_device(udev);
32821da177e4SLinus Torvalds 					ret = remote_wakeup(hdev->
32831da177e4SLinus Torvalds 							children[i-1]);
32848808f00cSAlan Stern 					usb_unlock_device(udev);
32851da177e4SLinus Torvalds 					if (ret < 0)
32861da177e4SLinus Torvalds 						connect_change = 1;
32871da177e4SLinus Torvalds 				} else {
32881da177e4SLinus Torvalds 					ret = -ENODEV;
32891da177e4SLinus Torvalds 					hub_port_disable(hub, i, 1);
32901da177e4SLinus Torvalds 				}
32911da177e4SLinus Torvalds 				dev_dbg (hub_dev,
32921da177e4SLinus Torvalds 					"resume on port %d, status %d\n",
32931da177e4SLinus Torvalds 					i, ret);
32941da177e4SLinus Torvalds 			}
32951da177e4SLinus Torvalds 
32961da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
32971da177e4SLinus Torvalds 				dev_err (hub_dev,
32981da177e4SLinus Torvalds 					"over-current change on port %d\n",
32991da177e4SLinus Torvalds 					i);
33001da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
33011da177e4SLinus Torvalds 					USB_PORT_FEAT_C_OVER_CURRENT);
33028520f380SAlan Stern 				hub_power_on(hub, true);
33031da177e4SLinus Torvalds 			}
33041da177e4SLinus Torvalds 
33051da177e4SLinus Torvalds 			if (portchange & USB_PORT_STAT_C_RESET) {
33061da177e4SLinus Torvalds 				dev_dbg (hub_dev,
33071da177e4SLinus Torvalds 					"reset change on port %d\n",
33081da177e4SLinus Torvalds 					i);
33091da177e4SLinus Torvalds 				clear_port_feature(hdev, i,
33101da177e4SLinus Torvalds 					USB_PORT_FEAT_C_RESET);
33111da177e4SLinus Torvalds 			}
33121da177e4SLinus Torvalds 
33131da177e4SLinus Torvalds 			if (connect_change)
33141da177e4SLinus Torvalds 				hub_port_connect_change(hub, i,
33151da177e4SLinus Torvalds 						portstatus, portchange);
33161da177e4SLinus Torvalds 		} /* end for i */
33171da177e4SLinus Torvalds 
33181da177e4SLinus Torvalds 		/* deal with hub status changes */
33191da177e4SLinus Torvalds 		if (test_and_clear_bit(0, hub->event_bits) == 0)
33201da177e4SLinus Torvalds 			;	/* do nothing */
33211da177e4SLinus Torvalds 		else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
33221da177e4SLinus Torvalds 			dev_err (hub_dev, "get_hub_status failed\n");
33231da177e4SLinus Torvalds 		else {
33241da177e4SLinus Torvalds 			if (hubchange & HUB_CHANGE_LOCAL_POWER) {
33251da177e4SLinus Torvalds 				dev_dbg (hub_dev, "power change\n");
33261da177e4SLinus Torvalds 				clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
332755c52718SAlan Stern 				if (hubstatus & HUB_STATUS_LOCAL_POWER)
332855c52718SAlan Stern 					/* FIXME: Is this always true? */
332955c52718SAlan Stern 					hub->limited_power = 1;
3330403fae78Sjidong xiao 				else
3331403fae78Sjidong xiao 					hub->limited_power = 0;
33321da177e4SLinus Torvalds 			}
33331da177e4SLinus Torvalds 			if (hubchange & HUB_CHANGE_OVERCURRENT) {
33341da177e4SLinus Torvalds 				dev_dbg (hub_dev, "overcurrent change\n");
33351da177e4SLinus Torvalds 				msleep(500);	/* Cool down */
33361da177e4SLinus Torvalds 				clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
33378520f380SAlan Stern                         	hub_power_on(hub, true);
33381da177e4SLinus Torvalds 			}
33391da177e4SLinus Torvalds 		}
33401da177e4SLinus Torvalds 
334140f122f3SAlan Stern loop_autopm:
334240f122f3SAlan Stern 		/* Allow autosuspend if we're not going to run again */
334340f122f3SAlan Stern 		if (list_empty(&hub->event_list))
334440f122f3SAlan Stern 			usb_autopm_enable(intf);
33451da177e4SLinus Torvalds loop:
33461da177e4SLinus Torvalds 		usb_unlock_device(hdev);
3347e8054854SAlan Stern 		kref_put(&hub->kref, hub_release);
33481da177e4SLinus Torvalds 
33491da177e4SLinus Torvalds         } /* end while (1) */
33501da177e4SLinus Torvalds }
33511da177e4SLinus Torvalds 
33521da177e4SLinus Torvalds static int hub_thread(void *__unused)
33531da177e4SLinus Torvalds {
33543bb1af52SAlan Stern 	/* khubd needs to be freezable to avoid intefering with USB-PERSIST
33553bb1af52SAlan Stern 	 * port handover.  Otherwise it might see that a full-speed device
33563bb1af52SAlan Stern 	 * was gone before the EHCI controller had handed its port over to
33573bb1af52SAlan Stern 	 * the companion full-speed controller.
33583bb1af52SAlan Stern 	 */
335983144186SRafael J. Wysocki 	set_freezable();
33603bb1af52SAlan Stern 
33611da177e4SLinus Torvalds 	do {
33621da177e4SLinus Torvalds 		hub_events();
3363e42837bcSRafael J. Wysocki 		wait_event_freezable(khubd_wait,
33649c8d6178Sakpm@osdl.org 				!list_empty(&hub_event_list) ||
33659c8d6178Sakpm@osdl.org 				kthread_should_stop());
33669c8d6178Sakpm@osdl.org 	} while (!kthread_should_stop() || !list_empty(&hub_event_list));
33671da177e4SLinus Torvalds 
33681da177e4SLinus Torvalds 	pr_debug("%s: khubd exiting\n", usbcore_name);
33699c8d6178Sakpm@osdl.org 	return 0;
33701da177e4SLinus Torvalds }
33711da177e4SLinus Torvalds 
33721da177e4SLinus Torvalds static struct usb_device_id hub_id_table [] = {
33731da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
33741da177e4SLinus Torvalds       .bDeviceClass = USB_CLASS_HUB},
33751da177e4SLinus Torvalds     { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
33761da177e4SLinus Torvalds       .bInterfaceClass = USB_CLASS_HUB},
33771da177e4SLinus Torvalds     { }						/* Terminating entry */
33781da177e4SLinus Torvalds };
33791da177e4SLinus Torvalds 
33801da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, hub_id_table);
33811da177e4SLinus Torvalds 
33821da177e4SLinus Torvalds static struct usb_driver hub_driver = {
33831da177e4SLinus Torvalds 	.name =		"hub",
33841da177e4SLinus Torvalds 	.probe =	hub_probe,
33851da177e4SLinus Torvalds 	.disconnect =	hub_disconnect,
33861da177e4SLinus Torvalds 	.suspend =	hub_suspend,
33871da177e4SLinus Torvalds 	.resume =	hub_resume,
3388f07600cfSAlan Stern 	.reset_resume =	hub_reset_resume,
33897de18d8bSAlan Stern 	.pre_reset =	hub_pre_reset,
33907de18d8bSAlan Stern 	.post_reset =	hub_post_reset,
33911da177e4SLinus Torvalds 	.ioctl =	hub_ioctl,
33921da177e4SLinus Torvalds 	.id_table =	hub_id_table,
339340f122f3SAlan Stern 	.supports_autosuspend =	1,
33941da177e4SLinus Torvalds };
33951da177e4SLinus Torvalds 
33961da177e4SLinus Torvalds int usb_hub_init(void)
33971da177e4SLinus Torvalds {
33981da177e4SLinus Torvalds 	if (usb_register(&hub_driver) < 0) {
33991da177e4SLinus Torvalds 		printk(KERN_ERR "%s: can't register hub driver\n",
34001da177e4SLinus Torvalds 			usbcore_name);
34011da177e4SLinus Torvalds 		return -1;
34021da177e4SLinus Torvalds 	}
34031da177e4SLinus Torvalds 
34049c8d6178Sakpm@osdl.org 	khubd_task = kthread_run(hub_thread, NULL, "khubd");
34059c8d6178Sakpm@osdl.org 	if (!IS_ERR(khubd_task))
34061da177e4SLinus Torvalds 		return 0;
34071da177e4SLinus Torvalds 
34081da177e4SLinus Torvalds 	/* Fall through if kernel_thread failed */
34091da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
34101da177e4SLinus Torvalds 	printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
34111da177e4SLinus Torvalds 
34121da177e4SLinus Torvalds 	return -1;
34131da177e4SLinus Torvalds }
34141da177e4SLinus Torvalds 
34151da177e4SLinus Torvalds void usb_hub_cleanup(void)
34161da177e4SLinus Torvalds {
34179c8d6178Sakpm@osdl.org 	kthread_stop(khubd_task);
34181da177e4SLinus Torvalds 
34191da177e4SLinus Torvalds 	/*
34201da177e4SLinus Torvalds 	 * Hub resources are freed for us by usb_deregister. It calls
34211da177e4SLinus Torvalds 	 * usb_driver_purge on every device which in turn calls that
34221da177e4SLinus Torvalds 	 * devices disconnect function if it is using this driver.
34231da177e4SLinus Torvalds 	 * The hub_disconnect function takes care of releasing the
34241da177e4SLinus Torvalds 	 * individual hub resources. -greg
34251da177e4SLinus Torvalds 	 */
34261da177e4SLinus Torvalds 	usb_deregister(&hub_driver);
34271da177e4SLinus Torvalds } /* usb_hub_cleanup() */
34281da177e4SLinus Torvalds 
3429eb764c4bSAlan Stern static int descriptors_changed(struct usb_device *udev,
3430eb764c4bSAlan Stern 		struct usb_device_descriptor *old_device_descriptor)
34311da177e4SLinus Torvalds {
3432eb764c4bSAlan Stern 	int		changed = 0;
34331da177e4SLinus Torvalds 	unsigned	index;
3434eb764c4bSAlan Stern 	unsigned	serial_len = 0;
3435eb764c4bSAlan Stern 	unsigned	len;
3436eb764c4bSAlan Stern 	unsigned	old_length;
3437eb764c4bSAlan Stern 	int		length;
3438eb764c4bSAlan Stern 	char		*buf;
34391da177e4SLinus Torvalds 
3440eb764c4bSAlan Stern 	if (memcmp(&udev->descriptor, old_device_descriptor,
3441eb764c4bSAlan Stern 			sizeof(*old_device_descriptor)) != 0)
3442eb764c4bSAlan Stern 		return 1;
3443eb764c4bSAlan Stern 
3444eb764c4bSAlan Stern 	/* Since the idVendor, idProduct, and bcdDevice values in the
3445eb764c4bSAlan Stern 	 * device descriptor haven't changed, we will assume the
3446eb764c4bSAlan Stern 	 * Manufacturer and Product strings haven't changed either.
3447eb764c4bSAlan Stern 	 * But the SerialNumber string could be different (e.g., a
3448eb764c4bSAlan Stern 	 * different flash card of the same brand).
3449eb764c4bSAlan Stern 	 */
3450eb764c4bSAlan Stern 	if (udev->serial)
3451eb764c4bSAlan Stern 		serial_len = strlen(udev->serial) + 1;
3452eb764c4bSAlan Stern 
3453eb764c4bSAlan Stern 	len = serial_len;
34541da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
3455eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
3456eb764c4bSAlan Stern 		len = max(len, old_length);
34571da177e4SLinus Torvalds 	}
3458eb764c4bSAlan Stern 
34590cc1a51fSOliver Neukum 	buf = kmalloc(len, GFP_NOIO);
34601da177e4SLinus Torvalds 	if (buf == NULL) {
34611da177e4SLinus Torvalds 		dev_err(&udev->dev, "no mem to re-read configs after reset\n");
34621da177e4SLinus Torvalds 		/* assume the worst */
34631da177e4SLinus Torvalds 		return 1;
34641da177e4SLinus Torvalds 	}
34651da177e4SLinus Torvalds 	for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
3466eb764c4bSAlan Stern 		old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
34671da177e4SLinus Torvalds 		length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
34681da177e4SLinus Torvalds 				old_length);
3469eb764c4bSAlan Stern 		if (length != old_length) {
34701da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d, error %d\n",
34711da177e4SLinus Torvalds 					index, length);
3472eb764c4bSAlan Stern 			changed = 1;
34731da177e4SLinus Torvalds 			break;
34741da177e4SLinus Torvalds 		}
34751da177e4SLinus Torvalds 		if (memcmp (buf, udev->rawdescriptors[index], old_length)
34761da177e4SLinus Torvalds 				!= 0) {
34771da177e4SLinus Torvalds 			dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
3478eb764c4bSAlan Stern 				index,
3479eb764c4bSAlan Stern 				((struct usb_config_descriptor *) buf)->
3480eb764c4bSAlan Stern 					bConfigurationValue);
3481eb764c4bSAlan Stern 			changed = 1;
34821da177e4SLinus Torvalds 			break;
34831da177e4SLinus Torvalds 		}
34841da177e4SLinus Torvalds 	}
3485eb764c4bSAlan Stern 
3486eb764c4bSAlan Stern 	if (!changed && serial_len) {
3487eb764c4bSAlan Stern 		length = usb_string(udev, udev->descriptor.iSerialNumber,
3488eb764c4bSAlan Stern 				buf, serial_len);
3489eb764c4bSAlan Stern 		if (length + 1 != serial_len) {
3490eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string error %d\n",
3491eb764c4bSAlan Stern 					length);
3492eb764c4bSAlan Stern 			changed = 1;
3493eb764c4bSAlan Stern 		} else if (memcmp(buf, udev->serial, length) != 0) {
3494eb764c4bSAlan Stern 			dev_dbg(&udev->dev, "serial string changed\n");
3495eb764c4bSAlan Stern 			changed = 1;
3496eb764c4bSAlan Stern 		}
3497eb764c4bSAlan Stern 	}
3498eb764c4bSAlan Stern 
34991da177e4SLinus Torvalds 	kfree(buf);
3500eb764c4bSAlan Stern 	return changed;
35011da177e4SLinus Torvalds }
35021da177e4SLinus Torvalds 
35031da177e4SLinus Torvalds /**
3504742120c6SMing Lei  * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
35051da177e4SLinus Torvalds  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
35061da177e4SLinus Torvalds  *
350779efa097SAlan Stern  * WARNING - don't use this routine to reset a composite device
350879efa097SAlan Stern  * (one with multiple interfaces owned by separate drivers)!
3509742120c6SMing Lei  * Use usb_reset_device() instead.
35101da177e4SLinus Torvalds  *
35111da177e4SLinus Torvalds  * Do a port reset, reassign the device's address, and establish its
35121da177e4SLinus Torvalds  * former operating configuration.  If the reset fails, or the device's
35131da177e4SLinus Torvalds  * descriptors change from their values before the reset, or the original
35141da177e4SLinus Torvalds  * configuration and altsettings cannot be restored, a flag will be set
35151da177e4SLinus Torvalds  * telling khubd to pretend the device has been disconnected and then
35161da177e4SLinus Torvalds  * re-connected.  All drivers will be unbound, and the device will be
35171da177e4SLinus Torvalds  * re-enumerated and probed all over again.
35181da177e4SLinus Torvalds  *
35191da177e4SLinus Torvalds  * Returns 0 if the reset succeeded, -ENODEV if the device has been
35201da177e4SLinus Torvalds  * flagged for logical disconnection, or some other negative error code
35211da177e4SLinus Torvalds  * if the reset wasn't even attempted.
35221da177e4SLinus Torvalds  *
35231da177e4SLinus Torvalds  * The caller must own the device lock.  For example, it's safe to use
35241da177e4SLinus Torvalds  * this from a driver probe() routine after downloading new firmware.
35251da177e4SLinus Torvalds  * For calls that might not occur during probe(), drivers should lock
35261da177e4SLinus Torvalds  * the device using usb_lock_device_for_reset().
35276bc6cff5SAlan Stern  *
35286bc6cff5SAlan Stern  * Locking exception: This routine may also be called from within an
35296bc6cff5SAlan Stern  * autoresume handler.  Such usage won't conflict with other tasks
35306bc6cff5SAlan Stern  * holding the device lock because these tasks should always call
35316bc6cff5SAlan Stern  * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
35321da177e4SLinus Torvalds  */
3533742120c6SMing Lei static int usb_reset_and_verify_device(struct usb_device *udev)
35341da177e4SLinus Torvalds {
35351da177e4SLinus Torvalds 	struct usb_device		*parent_hdev = udev->parent;
35361da177e4SLinus Torvalds 	struct usb_hub			*parent_hub;
35371da177e4SLinus Torvalds 	struct usb_device_descriptor	descriptor = udev->descriptor;
353812c3da34SAlan Stern 	int 				i, ret = 0;
353912c3da34SAlan Stern 	int				port1 = udev->portnum;
35401da177e4SLinus Torvalds 
35411da177e4SLinus Torvalds 	if (udev->state == USB_STATE_NOTATTACHED ||
35421da177e4SLinus Torvalds 			udev->state == USB_STATE_SUSPENDED) {
35431da177e4SLinus Torvalds 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
35441da177e4SLinus Torvalds 				udev->state);
35451da177e4SLinus Torvalds 		return -EINVAL;
35461da177e4SLinus Torvalds 	}
35471da177e4SLinus Torvalds 
35481da177e4SLinus Torvalds 	if (!parent_hdev) {
35491da177e4SLinus Torvalds 		/* this requires hcd-specific logic; see OHCI hc_restart() */
3550441b62c1SHarvey Harrison 		dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
35511da177e4SLinus Torvalds 		return -EISDIR;
35521da177e4SLinus Torvalds 	}
35531da177e4SLinus Torvalds 	parent_hub = hdev_to_hub(parent_hdev);
35541da177e4SLinus Torvalds 
35551da177e4SLinus Torvalds 	set_bit(port1, parent_hub->busy_bits);
35561da177e4SLinus Torvalds 	for (i = 0; i < SET_CONFIG_TRIES; ++i) {
35571da177e4SLinus Torvalds 
35581da177e4SLinus Torvalds 		/* ep0 maxpacket size may change; let the HCD know about it.
35591da177e4SLinus Torvalds 		 * Other endpoints will be handled by re-enumeration. */
3560fc721f51SInaky Perez-Gonzalez 		usb_ep0_reinit(udev);
35611da177e4SLinus Torvalds 		ret = hub_port_init(parent_hub, udev, port1, i);
3562dd4dd19eSAlan Stern 		if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
35631da177e4SLinus Torvalds 			break;
35641da177e4SLinus Torvalds 	}
35651da177e4SLinus Torvalds 	clear_bit(port1, parent_hub->busy_bits);
3566d5cbad4bSAlan Stern 
35671da177e4SLinus Torvalds 	if (ret < 0)
35681da177e4SLinus Torvalds 		goto re_enumerate;
35691da177e4SLinus Torvalds 
35701da177e4SLinus Torvalds 	/* Device might have changed firmware (DFU or similar) */
3571eb764c4bSAlan Stern 	if (descriptors_changed(udev, &descriptor)) {
35721da177e4SLinus Torvalds 		dev_info(&udev->dev, "device firmware changed\n");
35731da177e4SLinus Torvalds 		udev->descriptor = descriptor;	/* for disconnect() calls */
35741da177e4SLinus Torvalds 		goto re_enumerate;
35751da177e4SLinus Torvalds   	}
35761da177e4SLinus Torvalds 
35774fe0387aSAlan Stern 	/* Restore the device's previous configuration */
35781da177e4SLinus Torvalds 	if (!udev->actconfig)
35791da177e4SLinus Torvalds 		goto done;
35801da177e4SLinus Torvalds 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
35811da177e4SLinus Torvalds 			USB_REQ_SET_CONFIGURATION, 0,
35821da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, 0,
35831da177e4SLinus Torvalds 			NULL, 0, USB_CTRL_SET_TIMEOUT);
35841da177e4SLinus Torvalds 	if (ret < 0) {
35851da177e4SLinus Torvalds 		dev_err(&udev->dev,
35861da177e4SLinus Torvalds 			"can't restore configuration #%d (error=%d)\n",
35871da177e4SLinus Torvalds 			udev->actconfig->desc.bConfigurationValue, ret);
35881da177e4SLinus Torvalds 		goto re_enumerate;
35891da177e4SLinus Torvalds   	}
35901da177e4SLinus Torvalds 	usb_set_device_state(udev, USB_STATE_CONFIGURED);
35911da177e4SLinus Torvalds 
35924fe0387aSAlan Stern 	/* Put interfaces back into the same altsettings as before.
35934fe0387aSAlan Stern 	 * Don't bother to send the Set-Interface request for interfaces
35944fe0387aSAlan Stern 	 * that were already in altsetting 0; besides being unnecessary,
35954fe0387aSAlan Stern 	 * many devices can't handle it.  Instead just reset the host-side
35964fe0387aSAlan Stern 	 * endpoint state.
35974fe0387aSAlan Stern 	 */
35981da177e4SLinus Torvalds 	for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
35991da177e4SLinus Torvalds 		struct usb_interface *intf = udev->actconfig->interface[i];
36001da177e4SLinus Torvalds 		struct usb_interface_descriptor *desc;
36011da177e4SLinus Torvalds 
36021da177e4SLinus Torvalds 		desc = &intf->cur_altsetting->desc;
36034fe0387aSAlan Stern 		if (desc->bAlternateSetting == 0) {
36044fe0387aSAlan Stern 			usb_disable_interface(udev, intf, true);
36054fe0387aSAlan Stern 			usb_enable_interface(udev, intf, true);
36064fe0387aSAlan Stern 			ret = 0;
36074fe0387aSAlan Stern 		} else {
36081da177e4SLinus Torvalds 			ret = usb_set_interface(udev, desc->bInterfaceNumber,
36091da177e4SLinus Torvalds 					desc->bAlternateSetting);
36104fe0387aSAlan Stern 		}
36111da177e4SLinus Torvalds 		if (ret < 0) {
36121da177e4SLinus Torvalds 			dev_err(&udev->dev, "failed to restore interface %d "
36131da177e4SLinus Torvalds 				"altsetting %d (error=%d)\n",
36141da177e4SLinus Torvalds 				desc->bInterfaceNumber,
36151da177e4SLinus Torvalds 				desc->bAlternateSetting,
36161da177e4SLinus Torvalds 				ret);
36171da177e4SLinus Torvalds 			goto re_enumerate;
36181da177e4SLinus Torvalds 		}
36191da177e4SLinus Torvalds 	}
36201da177e4SLinus Torvalds 
36211da177e4SLinus Torvalds done:
36221da177e4SLinus Torvalds 	return 0;
36231da177e4SLinus Torvalds 
36241da177e4SLinus Torvalds re_enumerate:
36251da177e4SLinus Torvalds 	hub_port_logical_disconnect(parent_hub, port1);
36261da177e4SLinus Torvalds 	return -ENODEV;
36271da177e4SLinus Torvalds }
362879efa097SAlan Stern 
362979efa097SAlan Stern /**
3630742120c6SMing Lei  * usb_reset_device - warn interface drivers and perform a USB port reset
363179efa097SAlan Stern  * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
363279efa097SAlan Stern  *
363379efa097SAlan Stern  * Warns all drivers bound to registered interfaces (using their pre_reset
363479efa097SAlan Stern  * method), performs the port reset, and then lets the drivers know that
363579efa097SAlan Stern  * the reset is over (using their post_reset method).
363679efa097SAlan Stern  *
3637742120c6SMing Lei  * Return value is the same as for usb_reset_and_verify_device().
363879efa097SAlan Stern  *
363979efa097SAlan Stern  * The caller must own the device lock.  For example, it's safe to use
364079efa097SAlan Stern  * this from a driver probe() routine after downloading new firmware.
364179efa097SAlan Stern  * For calls that might not occur during probe(), drivers should lock
364279efa097SAlan Stern  * the device using usb_lock_device_for_reset().
364378d9a487SAlan Stern  *
364478d9a487SAlan Stern  * If an interface is currently being probed or disconnected, we assume
364578d9a487SAlan Stern  * its driver knows how to handle resets.  For all other interfaces,
364678d9a487SAlan Stern  * if the driver doesn't have pre_reset and post_reset methods then
364778d9a487SAlan Stern  * we attempt to unbind it and rebind afterward.
364879efa097SAlan Stern  */
3649742120c6SMing Lei int usb_reset_device(struct usb_device *udev)
365079efa097SAlan Stern {
365179efa097SAlan Stern 	int ret;
3652852c4b43SAlan Stern 	int i;
365379efa097SAlan Stern 	struct usb_host_config *config = udev->actconfig;
365479efa097SAlan Stern 
365579efa097SAlan Stern 	if (udev->state == USB_STATE_NOTATTACHED ||
365679efa097SAlan Stern 			udev->state == USB_STATE_SUSPENDED) {
365779efa097SAlan Stern 		dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
365879efa097SAlan Stern 				udev->state);
365979efa097SAlan Stern 		return -EINVAL;
366079efa097SAlan Stern 	}
366179efa097SAlan Stern 
3662645daaabSAlan Stern 	/* Prevent autosuspend during the reset */
366394fcda1fSAlan Stern 	usb_autoresume_device(udev);
3664645daaabSAlan Stern 
366579efa097SAlan Stern 	if (config) {
3666852c4b43SAlan Stern 		for (i = 0; i < config->desc.bNumInterfaces; ++i) {
3667852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
366879efa097SAlan Stern 			struct usb_driver *drv;
366978d9a487SAlan Stern 			int unbind = 0;
367079efa097SAlan Stern 
3671852c4b43SAlan Stern 			if (cintf->dev.driver) {
367279efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
367378d9a487SAlan Stern 				if (drv->pre_reset && drv->post_reset)
367478d9a487SAlan Stern 					unbind = (drv->pre_reset)(cintf);
367578d9a487SAlan Stern 				else if (cintf->condition ==
367678d9a487SAlan Stern 						USB_INTERFACE_BOUND)
367778d9a487SAlan Stern 					unbind = 1;
367878d9a487SAlan Stern 				if (unbind)
367978d9a487SAlan Stern 					usb_forced_unbind_intf(cintf);
368079efa097SAlan Stern 			}
368179efa097SAlan Stern 		}
368279efa097SAlan Stern 	}
368379efa097SAlan Stern 
3684742120c6SMing Lei 	ret = usb_reset_and_verify_device(udev);
368579efa097SAlan Stern 
368679efa097SAlan Stern 	if (config) {
3687852c4b43SAlan Stern 		for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
3688852c4b43SAlan Stern 			struct usb_interface *cintf = config->interface[i];
368979efa097SAlan Stern 			struct usb_driver *drv;
369078d9a487SAlan Stern 			int rebind = cintf->needs_binding;
369179efa097SAlan Stern 
369278d9a487SAlan Stern 			if (!rebind && cintf->dev.driver) {
369379efa097SAlan Stern 				drv = to_usb_driver(cintf->dev.driver);
369479efa097SAlan Stern 				if (drv->post_reset)
369578d9a487SAlan Stern 					rebind = (drv->post_reset)(cintf);
369678d9a487SAlan Stern 				else if (cintf->condition ==
369778d9a487SAlan Stern 						USB_INTERFACE_BOUND)
369878d9a487SAlan Stern 					rebind = 1;
369979efa097SAlan Stern 			}
37006c640945SAlan Stern 			if (ret == 0 && rebind)
370178d9a487SAlan Stern 				usb_rebind_intf(cintf);
370279efa097SAlan Stern 		}
370379efa097SAlan Stern 	}
370479efa097SAlan Stern 
370594fcda1fSAlan Stern 	usb_autosuspend_device(udev);
370679efa097SAlan Stern 	return ret;
370779efa097SAlan Stern }
3708742120c6SMing Lei EXPORT_SYMBOL_GPL(usb_reset_device);
3709dc023dceSInaky Perez-Gonzalez 
3710dc023dceSInaky Perez-Gonzalez 
3711dc023dceSInaky Perez-Gonzalez /**
3712dc023dceSInaky Perez-Gonzalez  * usb_queue_reset_device - Reset a USB device from an atomic context
3713dc023dceSInaky Perez-Gonzalez  * @iface: USB interface belonging to the device to reset
3714dc023dceSInaky Perez-Gonzalez  *
3715dc023dceSInaky Perez-Gonzalez  * This function can be used to reset a USB device from an atomic
3716dc023dceSInaky Perez-Gonzalez  * context, where usb_reset_device() won't work (as it blocks).
3717dc023dceSInaky Perez-Gonzalez  *
3718dc023dceSInaky Perez-Gonzalez  * Doing a reset via this method is functionally equivalent to calling
3719dc023dceSInaky Perez-Gonzalez  * usb_reset_device(), except for the fact that it is delayed to a
3720dc023dceSInaky Perez-Gonzalez  * workqueue. This means that any drivers bound to other interfaces
3721dc023dceSInaky Perez-Gonzalez  * might be unbound, as well as users from usbfs in user space.
3722dc023dceSInaky Perez-Gonzalez  *
3723dc023dceSInaky Perez-Gonzalez  * Corner cases:
3724dc023dceSInaky Perez-Gonzalez  *
3725dc023dceSInaky Perez-Gonzalez  * - Scheduling two resets at the same time from two different drivers
3726dc023dceSInaky Perez-Gonzalez  *   attached to two different interfaces of the same device is
3727dc023dceSInaky Perez-Gonzalez  *   possible; depending on how the driver attached to each interface
3728dc023dceSInaky Perez-Gonzalez  *   handles ->pre_reset(), the second reset might happen or not.
3729dc023dceSInaky Perez-Gonzalez  *
3730dc023dceSInaky Perez-Gonzalez  * - If a driver is unbound and it had a pending reset, the reset will
3731dc023dceSInaky Perez-Gonzalez  *   be cancelled.
3732dc023dceSInaky Perez-Gonzalez  *
3733dc023dceSInaky Perez-Gonzalez  * - This function can be called during .probe() or .disconnect()
3734dc023dceSInaky Perez-Gonzalez  *   times. On return from .disconnect(), any pending resets will be
3735dc023dceSInaky Perez-Gonzalez  *   cancelled.
3736dc023dceSInaky Perez-Gonzalez  *
3737dc023dceSInaky Perez-Gonzalez  * There is no no need to lock/unlock the @reset_ws as schedule_work()
3738dc023dceSInaky Perez-Gonzalez  * does its own.
3739dc023dceSInaky Perez-Gonzalez  *
3740dc023dceSInaky Perez-Gonzalez  * NOTE: We don't do any reference count tracking because it is not
3741dc023dceSInaky Perez-Gonzalez  *     needed. The lifecycle of the work_struct is tied to the
3742dc023dceSInaky Perez-Gonzalez  *     usb_interface. Before destroying the interface we cancel the
3743dc023dceSInaky Perez-Gonzalez  *     work_struct, so the fact that work_struct is queued and or
3744dc023dceSInaky Perez-Gonzalez  *     running means the interface (and thus, the device) exist and
3745dc023dceSInaky Perez-Gonzalez  *     are referenced.
3746dc023dceSInaky Perez-Gonzalez  */
3747dc023dceSInaky Perez-Gonzalez void usb_queue_reset_device(struct usb_interface *iface)
3748dc023dceSInaky Perez-Gonzalez {
3749dc023dceSInaky Perez-Gonzalez 	schedule_work(&iface->reset_ws);
3750dc023dceSInaky Perez-Gonzalez }
3751dc023dceSInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(usb_queue_reset_device);
3752