blob: 6a9f11fbc2eb1f4e8417b135c1464460aa8addb0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB hub driver.
3 *
4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999 Johannes Erdfelt
6 * (C) Copyright 1999 Gregory P. Smith
7 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
8 *
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/completion.h>
16#include <linux/sched.h>
17#include <linux/list.h>
18#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/ioctl.h>
20#include <linux/usb.h>
21#include <linux/usbdevice_fs.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020022#include <linux/usb/hcd.h>
Richard Zhao925aa462012-07-11 11:09:28 +080023#include <linux/usb/otg.h>
Phil Dibowitz93362a82010-07-22 00:05:01 +020024#include <linux/usb/quirks.h>
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070025#include <linux/kthread.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010026#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080027#include <linux/freezer.h>
Theodore Ts'ob04b3152012-07-04 11:22:20 -040028#include <linux/random.h>
Lan Tianyuad493e52013-01-23 04:26:30 +080029#include <linux/pm_qos.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/uaccess.h>
32#include <asm/byteorder.h>
33
Lan Tianyu6e30d7c2013-01-11 20:10:38 +080034#include "hub.h"
Peter Chen026f3fc2014-08-19 09:51:53 +080035#include "otg_whitelist.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Ming Leie6f30de2012-10-24 11:59:24 +080037#define USB_VENDOR_GENESYS_LOGIC 0x05e3
38#define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01
39
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -070040/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -050041 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
43static DEFINE_SPINLOCK(device_state_lock);
44
45/* khubd's worklist and its lock */
46static DEFINE_SPINLOCK(hub_event_lock);
47static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
48
49/* Wakes up khubd */
50static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
51
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070052static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Dan Williamsd8521af2014-05-20 18:08:28 -070054/* synchronize hub-port add/remove and peering operations */
55DEFINE_MUTEX(usb_port_peer_mutex);
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/* cycle leds on hubs that aren't blinking for attention */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103058static bool blinkenlights = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059module_param (blinkenlights, bool, S_IRUGO);
60MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
61
62/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020063 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
64 * 10 seconds to send reply for the initial 64-byte descriptor request.
65 */
66/* define initial 64-byte descriptor request timeout in milliseconds */
67static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
68module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +080069MODULE_PARM_DESC(initial_descriptor_timeout,
70 "initial 64-byte descriptor request timeout in milliseconds "
71 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020072
73/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * As of 2.6.10 we introduce a new USB device initialization scheme which
75 * closely resembles the way Windows works. Hopefully it will be compatible
76 * with a wider range of devices than the old scheme. However some previously
77 * working devices may start giving rise to "device not accepting address"
78 * errors; if that happens the user can try the old scheme by adjusting the
79 * following module parameters.
80 *
81 * For maximum flexibility there are two boolean parameters to control the
82 * hub driver's behavior. On the first initialization attempt, if the
83 * "old_scheme_first" parameter is set then the old scheme will be used,
84 * otherwise the new scheme is used. If that fails and "use_both_schemes"
85 * is set, then the driver will make another attempt, using the other scheme.
86 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103087static bool old_scheme_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
89MODULE_PARM_DESC(old_scheme_first,
90 "start with the old device initialization scheme");
91
Rusty Russell90ab5ee2012-01-13 09:32:20 +103092static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
94MODULE_PARM_DESC(use_both_schemes,
95 "try the other device initialization scheme if the "
96 "first one fails");
97
Alan Stern32fe0192007-10-10 16:27:07 -040098/* Mutual exclusion for EHCI CF initialization. This interferes with
99 * port reset on some companion controllers.
100 */
101DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
102EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
103
Lan Tianyuad493e52013-01-23 04:26:30 +0800104#define HUB_DEBOUNCE_TIMEOUT 2000
Alan Stern948fea32008-04-28 11:07:07 -0400105#define HUB_DEBOUNCE_STEP 25
106#define HUB_DEBOUNCE_STABLE 100
107
Ming Lei742120c2008-06-18 22:00:29 +0800108static int usb_reset_and_verify_device(struct usb_device *udev);
109
Sarah Sharp131dec32010-12-06 21:00:19 -0800110static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Sarah Sharp131dec32010-12-06 21:00:19 -0800112 if (hub_is_superspeed(hub->hdev))
113 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500114 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Matthias Beyer469271f2013-10-10 23:41:27 +0200115 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500116 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return "1.5 Mb/s";
118 else
119 return "12 Mb/s";
120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/* Note that hdev or one of its children must be locked! */
Lan Tianyuad493e52013-01-23 04:26:30 +0800123struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Lan Tianyuff823c792012-09-05 13:44:32 +0800125 if (!hdev || !hdev->actconfig || !hdev->maxchild)
Alan Stern251180842009-07-22 14:41:18 -0400126 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return usb_get_intfdata(hdev->actconfig->interface[0]);
128}
129
Sarah Sharp140e3022014-01-22 13:35:02 -0800130static int usb_device_supports_lpm(struct usb_device *udev)
Sarah Sharpd9b20992012-02-20 08:31:26 -0800131{
132 /* USB 2.1 (and greater) devices indicate LPM support through
133 * their USB 2.0 Extended Capabilities BOS descriptor.
134 */
135 if (udev->speed == USB_SPEED_HIGH) {
136 if (udev->bos->ext_cap &&
137 (USB_LPM_SUPPORT &
138 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
139 return 1;
140 return 0;
141 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800142
Sarah Sharp25cd2882014-01-17 14:15:44 -0800143 /*
144 * According to the USB 3.0 spec, all USB 3.0 devices must support LPM.
145 * However, there are some that don't, and they set the U1/U2 exit
146 * latencies to zero.
Sarah Sharp51e0a012012-02-20 12:02:19 -0800147 */
148 if (!udev->bos->ss_cap) {
Sarah Sharp25cd2882014-01-17 14:15:44 -0800149 dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n");
Sarah Sharp51e0a012012-02-20 12:02:19 -0800150 return 0;
151 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800152
Sarah Sharp25cd2882014-01-17 14:15:44 -0800153 if (udev->bos->ss_cap->bU1devExitLat == 0 &&
154 udev->bos->ss_cap->bU2DevExitLat == 0) {
155 if (udev->parent)
156 dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n");
157 else
158 dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n");
159 return 0;
160 }
161
162 if (!udev->parent || udev->parent->lpm_capable)
163 return 1;
Sarah Sharpd9b20992012-02-20 08:31:26 -0800164 return 0;
165}
166
Sarah Sharp51e0a012012-02-20 12:02:19 -0800167/*
168 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
169 * either U1 or U2.
170 */
171static void usb_set_lpm_mel(struct usb_device *udev,
172 struct usb3_lpm_parameters *udev_lpm_params,
173 unsigned int udev_exit_latency,
174 struct usb_hub *hub,
175 struct usb3_lpm_parameters *hub_lpm_params,
176 unsigned int hub_exit_latency)
177{
178 unsigned int total_mel;
179 unsigned int device_mel;
180 unsigned int hub_mel;
181
182 /*
183 * Calculate the time it takes to transition all links from the roothub
184 * to the parent hub into U0. The parent hub must then decode the
185 * packet (hub header decode latency) to figure out which port it was
186 * bound for.
187 *
188 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
189 * means 0.1us). Multiply that by 100 to get nanoseconds.
190 */
191 total_mel = hub_lpm_params->mel +
192 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
193
194 /*
195 * How long will it take to transition the downstream hub's port into
196 * U0? The greater of either the hub exit latency or the device exit
197 * latency.
198 *
199 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
200 * Multiply that by 1000 to get nanoseconds.
201 */
202 device_mel = udev_exit_latency * 1000;
203 hub_mel = hub_exit_latency * 1000;
204 if (device_mel > hub_mel)
205 total_mel += device_mel;
206 else
207 total_mel += hub_mel;
208
209 udev_lpm_params->mel = total_mel;
210}
211
212/*
213 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
214 * a transition from either U1 or U2.
215 */
216static void usb_set_lpm_pel(struct usb_device *udev,
217 struct usb3_lpm_parameters *udev_lpm_params,
218 unsigned int udev_exit_latency,
219 struct usb_hub *hub,
220 struct usb3_lpm_parameters *hub_lpm_params,
221 unsigned int hub_exit_latency,
222 unsigned int port_to_port_exit_latency)
223{
224 unsigned int first_link_pel;
225 unsigned int hub_pel;
226
227 /*
228 * First, the device sends an LFPS to transition the link between the
229 * device and the parent hub into U0. The exit latency is the bigger of
230 * the device exit latency or the hub exit latency.
231 */
232 if (udev_exit_latency > hub_exit_latency)
233 first_link_pel = udev_exit_latency * 1000;
234 else
235 first_link_pel = hub_exit_latency * 1000;
236
237 /*
238 * When the hub starts to receive the LFPS, there is a slight delay for
239 * it to figure out that one of the ports is sending an LFPS. Then it
240 * will forward the LFPS to its upstream link. The exit latency is the
241 * delay, plus the PEL that we calculated for this hub.
242 */
243 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
244
245 /*
246 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
247 * is the greater of the two exit latencies.
248 */
249 if (first_link_pel > hub_pel)
250 udev_lpm_params->pel = first_link_pel;
251 else
252 udev_lpm_params->pel = hub_pel;
253}
254
255/*
256 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
257 * when a device initiates a transition to U0, until when it will receive the
258 * first packet from the host controller.
259 *
260 * Section C.1.5.1 describes the four components to this:
261 * - t1: device PEL
262 * - t2: time for the ERDY to make it from the device to the host.
263 * - t3: a host-specific delay to process the ERDY.
264 * - t4: time for the packet to make it from the host to the device.
265 *
266 * t3 is specific to both the xHCI host and the platform the host is integrated
267 * into. The Intel HW folks have said it's negligible, FIXME if a different
268 * vendor says otherwise.
269 */
270static void usb_set_lpm_sel(struct usb_device *udev,
271 struct usb3_lpm_parameters *udev_lpm_params)
272{
273 struct usb_device *parent;
274 unsigned int num_hubs;
275 unsigned int total_sel;
276
277 /* t1 = device PEL */
278 total_sel = udev_lpm_params->pel;
279 /* How many external hubs are in between the device & the root port. */
280 for (parent = udev->parent, num_hubs = 0; parent->parent;
281 parent = parent->parent)
282 num_hubs++;
283 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
284 if (num_hubs > 0)
285 total_sel += 2100 + 250 * (num_hubs - 1);
286
287 /* t4 = 250ns * num_hubs */
288 total_sel += 250 * num_hubs;
289
290 udev_lpm_params->sel = total_sel;
291}
292
293static void usb_set_lpm_parameters(struct usb_device *udev)
294{
295 struct usb_hub *hub;
296 unsigned int port_to_port_delay;
297 unsigned int udev_u1_del;
298 unsigned int udev_u2_del;
299 unsigned int hub_u1_del;
300 unsigned int hub_u2_del;
301
302 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
303 return;
304
Lan Tianyuad493e52013-01-23 04:26:30 +0800305 hub = usb_hub_to_struct_hub(udev->parent);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800306 /* It doesn't take time to transition the roothub into U0, since it
307 * doesn't have an upstream link.
308 */
309 if (!hub)
310 return;
311
312 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
Xenia Ragiadakou4d967992013-08-31 18:09:13 +0300313 udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800314 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
Xenia Ragiadakou4d967992013-08-31 18:09:13 +0300315 hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800316
317 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
318 hub, &udev->parent->u1_params, hub_u1_del);
319
320 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
321 hub, &udev->parent->u2_params, hub_u2_del);
322
323 /*
324 * Appendix C, section C.2.2.2, says that there is a slight delay from
325 * when the parent hub notices the downstream port is trying to
326 * transition to U0 to when the hub initiates a U0 transition on its
327 * upstream port. The section says the delays are tPort2PortU1EL and
328 * tPort2PortU2EL, but it doesn't define what they are.
329 *
330 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
331 * about the same delays. Use the maximum delay calculations from those
332 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
333 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
334 * assume the device exit latencies they are talking about are the hub
335 * exit latencies.
336 *
337 * What do we do if the U2 exit latency is less than the U1 exit
338 * latency? It's possible, although not likely...
339 */
340 port_to_port_delay = 1;
341
342 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
343 hub, &udev->parent->u1_params, hub_u1_del,
344 port_to_port_delay);
345
346 if (hub_u2_del > hub_u1_del)
347 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
348 else
349 port_to_port_delay = 1 + hub_u1_del;
350
351 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
352 hub, &udev->parent->u2_params, hub_u2_del,
353 port_to_port_delay);
354
355 /* Now that we've got PEL, calculate SEL. */
356 usb_set_lpm_sel(udev, &udev->u1_params);
357 usb_set_lpm_sel(udev, &udev->u2_params);
358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/* USB 2.0 spec Section 11.24.4.5 */
John Youndbe79bb2001-09-17 00:00:00 -0700361static int get_hub_descriptor(struct usb_device *hdev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
John Youndbe79bb2001-09-17 00:00:00 -0700363 int i, ret, size;
364 unsigned dtype;
365
366 if (hub_is_superspeed(hdev)) {
367 dtype = USB_DT_SS_HUB;
368 size = USB_DT_SS_HUB_SIZE;
369 } else {
370 dtype = USB_DT_HUB;
371 size = sizeof(struct usb_hub_descriptor);
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 for (i = 0; i < 3; i++) {
375 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
376 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
John Youndbe79bb2001-09-17 00:00:00 -0700377 dtype << 8, 0, data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 USB_CTRL_GET_TIMEOUT);
379 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
380 return ret;
381 }
382 return -EINVAL;
383}
384
385/*
386 * USB 2.0 spec Section 11.24.2.1
387 */
388static int clear_hub_feature(struct usb_device *hdev, int feature)
389{
390 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
391 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
392}
393
394/*
395 * USB 2.0 spec Section 11.24.2.2
396 */
Lan Tianyuad493e52013-01-23 04:26:30 +0800397int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
400 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
401 NULL, 0, 1000);
402}
403
404/*
405 * USB 2.0 spec Section 11.24.2.13
406 */
407static int set_port_feature(struct usb_device *hdev, int port1, int feature)
408{
409 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
410 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
411 NULL, 0, 1000);
412}
413
Dan Williamsd99f6b42014-05-20 18:08:17 -0700414static char *to_led_name(int selector)
415{
416 switch (selector) {
417 case HUB_LED_AMBER:
418 return "amber";
419 case HUB_LED_GREEN:
420 return "green";
421 case HUB_LED_OFF:
422 return "off";
423 case HUB_LED_AUTO:
424 return "auto";
425 default:
426 return "??";
427 }
428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430/*
431 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
432 * for info about using port indicators
433 */
Dan Williamsd99f6b42014-05-20 18:08:17 -0700434static void set_port_led(struct usb_hub *hub, int port1, int selector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Dan Williamsd99f6b42014-05-20 18:08:17 -0700436 struct usb_port *port_dev = hub->ports[port1 - 1];
437 int status;
438
439 status = set_port_feature(hub->hdev, (selector << 8) | port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 USB_PORT_FEAT_INDICATOR);
Dan Williamsd99f6b42014-05-20 18:08:17 -0700441 dev_dbg(&port_dev->dev, "indicator %s status %d\n",
442 to_led_name(selector), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445#define LED_CYCLE_PERIOD ((2*HZ)/3)
446
David Howellsc4028952006-11-22 14:57:56 +0000447static void led_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
David Howellsc4028952006-11-22 14:57:56 +0000449 struct usb_hub *hub =
450 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct usb_device *hdev = hub->hdev;
452 unsigned i;
453 unsigned changed = 0;
454 int cursor = -1;
455
456 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
457 return;
458
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +0200459 for (i = 0; i < hdev->maxchild; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 unsigned selector, mode;
461
462 /* 30%-50% duty cycle */
463
464 switch (hub->indicator[i]) {
465 /* cycle marker */
466 case INDICATOR_CYCLE:
467 cursor = i;
468 selector = HUB_LED_AUTO;
469 mode = INDICATOR_AUTO;
470 break;
471 /* blinking green = sw attention */
472 case INDICATOR_GREEN_BLINK:
473 selector = HUB_LED_GREEN;
474 mode = INDICATOR_GREEN_BLINK_OFF;
475 break;
476 case INDICATOR_GREEN_BLINK_OFF:
477 selector = HUB_LED_OFF;
478 mode = INDICATOR_GREEN_BLINK;
479 break;
480 /* blinking amber = hw attention */
481 case INDICATOR_AMBER_BLINK:
482 selector = HUB_LED_AMBER;
483 mode = INDICATOR_AMBER_BLINK_OFF;
484 break;
485 case INDICATOR_AMBER_BLINK_OFF:
486 selector = HUB_LED_OFF;
487 mode = INDICATOR_AMBER_BLINK;
488 break;
489 /* blink green/amber = reserved */
490 case INDICATOR_ALT_BLINK:
491 selector = HUB_LED_GREEN;
492 mode = INDICATOR_ALT_BLINK_OFF;
493 break;
494 case INDICATOR_ALT_BLINK_OFF:
495 selector = HUB_LED_AMBER;
496 mode = INDICATOR_ALT_BLINK;
497 break;
498 default:
499 continue;
500 }
501 if (selector != HUB_LED_AUTO)
502 changed = 1;
503 set_port_led(hub, i + 1, selector);
504 hub->indicator[i] = mode;
505 }
506 if (!changed && blinkenlights) {
507 cursor++;
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +0200508 cursor %= hdev->maxchild;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
510 hub->indicator[cursor] = INDICATOR_CYCLE;
511 changed++;
512 }
513 if (changed)
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -0800514 queue_delayed_work(system_power_efficient_wq,
515 &hub->leds, LED_CYCLE_PERIOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518/* use a short timeout for hub/port status fetches */
519#define USB_STS_TIMEOUT 1000
520#define USB_STS_RETRIES 5
521
522/*
523 * USB 2.0 spec Section 11.24.2.6
524 */
525static int get_hub_status(struct usb_device *hdev,
526 struct usb_hub_status *data)
527{
528 int i, status = -ETIMEDOUT;
529
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200530 for (i = 0; i < USB_STS_RETRIES &&
531 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
533 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
534 data, sizeof(*data), USB_STS_TIMEOUT);
535 }
536 return status;
537}
538
539/*
540 * USB 2.0 spec Section 11.24.2.7
541 */
542static int get_port_status(struct usb_device *hdev, int port1,
543 struct usb_port_status *data)
544{
545 int i, status = -ETIMEDOUT;
546
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200547 for (i = 0; i < USB_STS_RETRIES &&
548 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
550 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
551 data, sizeof(*data), USB_STS_TIMEOUT);
552 }
553 return status;
554}
555
Alan Stern3eb14912008-03-03 15:15:43 -0500556static int hub_port_status(struct usb_hub *hub, int port1,
557 u16 *status, u16 *change)
558{
559 int ret;
560
561 mutex_lock(&hub->status_mutex);
562 ret = get_port_status(hub->hdev, port1, &hub->status->port);
563 if (ret < 4) {
Alan Sterne9e88fb2013-03-27 16:14:01 -0400564 if (ret != -ENODEV)
565 dev_err(hub->intfdev,
566 "%s failed (err = %d)\n", __func__, ret);
Alan Stern3eb14912008-03-03 15:15:43 -0500567 if (ret >= 0)
568 ret = -EIO;
569 } else {
570 *status = le16_to_cpu(hub->status->port.wPortStatus);
571 *change = le16_to_cpu(hub->status->port.wPortChange);
John Youndbe79bb2001-09-17 00:00:00 -0700572
Alan Stern3eb14912008-03-03 15:15:43 -0500573 ret = 0;
574 }
575 mutex_unlock(&hub->status_mutex);
576 return ret;
577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579static void kick_khubd(struct usb_hub *hub)
580{
581 unsigned long flags;
582
583 spin_lock_irqsave(&hub_event_lock, flags);
Roel Kluin2e2c5ee2007-10-26 23:54:35 +0200584 if (!hub->disconnected && list_empty(&hub->event_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 list_add_tail(&hub->event_list, &hub_event_list);
Alan Stern8e4ceb32009-12-07 13:01:37 -0500586
587 /* Suppress autosuspend until khubd runs */
588 usb_autopm_get_interface_no_resume(
589 to_usb_interface(hub->intfdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 wake_up(&khubd_wait);
591 }
592 spin_unlock_irqrestore(&hub_event_lock, flags);
593}
594
595void usb_kick_khubd(struct usb_device *hdev)
596{
Lan Tianyuad493e52013-01-23 04:26:30 +0800597 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Alan Stern251180842009-07-22 14:41:18 -0400598
599 if (hub)
600 kick_khubd(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800603/*
604 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
605 * Notification, which indicates it had initiated remote wakeup.
606 *
607 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
608 * device initiates resume, so the USB core will not receive notice of the
609 * resume through the normal hub interrupt URB.
610 */
611void usb_wakeup_notification(struct usb_device *hdev,
612 unsigned int portnum)
613{
614 struct usb_hub *hub;
615
616 if (!hdev)
617 return;
618
Lan Tianyuad493e52013-01-23 04:26:30 +0800619 hub = usb_hub_to_struct_hub(hdev);
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800620 if (hub) {
621 set_bit(portnum, hub->wakeup_bits);
622 kick_khubd(hub);
623 }
624}
625EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100628static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200630 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400631 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100632 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 unsigned long bits;
634
Alan Sterne0152682007-08-24 15:42:52 -0400635 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 case -ENOENT: /* synchronous unlink */
637 case -ECONNRESET: /* async unlink */
638 case -ESHUTDOWN: /* hardware going away */
639 return;
640
641 default: /* presumably an error */
642 /* Cause a hub reset after 10 consecutive errors */
Alan Sterne0152682007-08-24 15:42:52 -0400643 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if ((++hub->nerrors < 10) || hub->error)
645 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400646 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* let khubd handle things */
650 case 0: /* we got data: port status changed */
651 bits = 0;
652 for (i = 0; i < urb->actual_length; ++i)
653 bits |= ((unsigned long) ((*hub->buffer)[i]))
654 << (i*8);
655 hub->event_bits[0] = bits;
656 break;
657 }
658
659 hub->nerrors = 0;
660
661 /* Something happened, let khubd figure it out */
662 kick_khubd(hub);
663
664resubmit:
665 if (hub->quiescing)
666 return;
667
668 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
669 && status != -ENODEV && status != -EPERM)
670 dev_err (hub->intfdev, "resubmit --> %d\n", status);
671}
672
673/* USB 2.0 spec Section 11.24.2.3 */
674static inline int
675hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
676{
William Gulland2c7b8712013-06-27 16:10:20 -0700677 /* Need to clear both directions for control ep */
678 if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) ==
679 USB_ENDPOINT_XFER_CONTROL) {
680 int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
681 HUB_CLEAR_TT_BUFFER, USB_RT_PORT,
682 devinfo ^ 0x8000, tt, NULL, 0, 1000);
683 if (status)
684 return status;
685 }
Alan Sternc2f65952009-11-18 11:37:15 -0500686 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
688 tt, NULL, 0, 1000);
689}
690
691/*
692 * enumeration blocks khubd for a long time. we use keventd instead, since
693 * long blocking there is the exception, not the rule. accordingly, HCDs
694 * talking to TTs must queue control transfers (not just bulk and iso), so
695 * both can talk to the same hub concurrently.
696 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400697static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
David Howellsc4028952006-11-22 14:57:56 +0000699 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400700 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 unsigned long flags;
702
703 spin_lock_irqsave (&hub->tt.lock, flags);
Octavian Purdila3b6054d2012-10-01 22:21:12 +0300704 while (!list_empty(&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400705 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct usb_tt_clear *clear;
707 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400708 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 int status;
710
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400711 next = hub->tt.clear_list.next;
712 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 list_del (&clear->clear_list);
714
715 /* drop lock so HCD can concurrently report other TT errors */
716 spin_unlock_irqrestore (&hub->tt.lock, flags);
717 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400718 if (status && status != -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 dev_err (&hdev->dev,
720 "clear tt %d (%04x) error %d\n",
721 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400722
723 /* Tell the HCD, even if the operation failed */
724 drv = clear->hcd->driver;
725 if (drv->clear_tt_buffer_complete)
726 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
727
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700728 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400729 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 spin_unlock_irqrestore (&hub->tt.lock, flags);
732}
733
734/**
Lan Tianyu971fcd42013-01-23 04:26:29 +0800735 * usb_hub_set_port_power - control hub port's power state
Mathias Nyman413412612013-06-18 17:28:48 +0300736 * @hdev: USB device belonging to the usb hub
737 * @hub: target hub
Lan Tianyu971fcd42013-01-23 04:26:29 +0800738 * @port1: port index
739 * @set: expected status
740 *
741 * call this function to control port's power via setting or
742 * clearing the port's PORT_POWER feature.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200743 *
744 * Return: 0 if successful. A negative error code otherwise.
Lan Tianyu971fcd42013-01-23 04:26:29 +0800745 */
Mathias Nyman413412612013-06-18 17:28:48 +0300746int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub,
747 int port1, bool set)
Lan Tianyu971fcd42013-01-23 04:26:29 +0800748{
749 int ret;
750
751 if (set)
752 ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
753 else
Lan Tianyuad493e52013-01-23 04:26:30 +0800754 ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
755
Dan Williamsd5c38342014-05-20 18:08:52 -0700756 if (ret)
757 return ret;
758
759 if (set)
760 set_bit(port1, hub->power_bits);
761 else
762 clear_bit(port1, hub->power_bits);
763 return 0;
Lan Tianyu971fcd42013-01-23 04:26:29 +0800764}
765
766/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400767 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
768 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 *
770 * High speed HCDs use this to tell the hub driver that some split control or
771 * bulk transaction failed in a way that requires clearing internal state of
772 * a transaction translator. This is normally detected (and reported) from
773 * interrupt context.
774 *
775 * It may not be possible for that hub to handle additional full (or low)
776 * speed transactions until that state is fully cleared out.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200777 *
778 * Return: 0 if successful. A negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400780int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400782 struct usb_device *udev = urb->dev;
783 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 struct usb_tt *tt = udev->tt;
785 unsigned long flags;
786 struct usb_tt_clear *clear;
787
788 /* we've got to cope with an arbitrary number of pending TT clears,
789 * since each TT has "at least two" buffers that can need it (and
790 * there can be many TTs per hub). even if they're uncommon.
791 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800792 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
794 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400795 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797
798 /* info that CLEAR_TT_BUFFER needs */
799 clear->tt = tt->multi ? udev->ttport : 1;
800 clear->devinfo = usb_pipeendpoint (pipe);
801 clear->devinfo |= udev->devnum << 4;
802 clear->devinfo |= usb_pipecontrol (pipe)
803 ? (USB_ENDPOINT_XFER_CONTROL << 11)
804 : (USB_ENDPOINT_XFER_BULK << 11);
805 if (usb_pipein (pipe))
806 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400807
808 /* info for completion callback */
809 clear->hcd = bus_to_hcd(udev->bus);
810 clear->ep = urb->ep;
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /* tell keventd to clear state for this TT */
813 spin_lock_irqsave (&tt->lock, flags);
814 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400815 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400819EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Dan Williams7ad3c472014-05-20 18:08:57 -0700821static void hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 int port1;
824
Alan Stern4489a572006-04-27 15:54:22 -0400825 /* Enable power on each port. Some hubs have reserved values
826 * of LPSM (> 2) in their descriptors, even though they are
827 * USB 2.0 hubs. Some hubs do not implement port-power switching
828 * but only emulate it. In all cases, the ports won't work
829 * unless we send these messages to the hub.
830 */
Dan Williams9262c192014-05-20 18:08:12 -0700831 if (hub_is_port_power_switchable(hub))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400833 else
834 dev_dbg(hub->intfdev, "trying to enable port power on "
835 "non-switchable hub\n");
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +0200836 for (port1 = 1; port1 <= hub->hdev->maxchild; port1++)
Dan Williamsd5c38342014-05-20 18:08:52 -0700837 if (test_bit(port1, hub->power_bits))
Lan Tianyuad493e52013-01-23 04:26:30 +0800838 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
839 else
840 usb_clear_port_feature(hub->hdev, port1,
841 USB_PORT_FEAT_POWER);
Alan Stern8520f382008-09-22 14:44:26 -0400842 if (do_delay)
Dan Williams7ad3c472014-05-20 18:08:57 -0700843 msleep(hub_power_on_good_delay(hub));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846static int hub_hub_status(struct usb_hub *hub,
847 u16 *status, u16 *change)
848{
849 int ret;
850
Alan Sterndb90e7a2007-02-05 09:56:15 -0500851 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 ret = get_hub_status(hub->hdev, &hub->status->hub);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400853 if (ret < 0) {
854 if (ret != -ENODEV)
855 dev_err(hub->intfdev,
856 "%s failed (err = %d)\n", __func__, ret);
857 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 *status = le16_to_cpu(hub->status->hub.wHubStatus);
Matthias Beyer469271f2013-10-10 23:41:27 +0200859 *change = le16_to_cpu(hub->status->hub.wHubChange);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 ret = 0;
861 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500862 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return ret;
864}
865
Sarah Sharp41e7e052012-11-14 16:42:32 -0800866static int hub_set_port_link_state(struct usb_hub *hub, int port1,
867 unsigned int link_status)
868{
869 return set_port_feature(hub->hdev,
870 port1 | (link_status << 3),
871 USB_PORT_FEAT_LINK_STATE);
872}
873
874/*
875 * If USB 3.0 ports are placed into the Disabled state, they will no longer
876 * detect any device connects or disconnects. This is generally not what the
877 * USB core wants, since it expects a disabled port to produce a port status
878 * change event when a new device connects.
879 *
880 * Instead, set the link state to Disabled, wait for the link to settle into
881 * that state, clear any change bits, and then put the port into the RxDetect
882 * state.
883 */
884static int hub_usb3_port_disable(struct usb_hub *hub, int port1)
885{
886 int ret;
887 int total_time;
888 u16 portchange, portstatus;
889
890 if (!hub_is_superspeed(hub->hdev))
891 return -EINVAL;
892
Gavin Guobb86cf52014-07-18 01:12:13 +0800893 ret = hub_port_status(hub, port1, &portstatus, &portchange);
894 if (ret < 0)
895 return ret;
896
897 /*
898 * USB controller Advanced Micro Devices, Inc. [AMD] FCH USB XHCI
899 * Controller [1022:7814] will have spurious result making the following
900 * usb 3.0 device hotplugging route to the 2.0 root hub and recognized
901 * as high-speed device if we set the usb 3.0 port link state to
902 * Disabled. Since it's already in USB_SS_PORT_LS_RX_DETECT state, we
903 * check the state here to avoid the bug.
904 */
905 if ((portstatus & USB_PORT_STAT_LINK_STATE) ==
906 USB_SS_PORT_LS_RX_DETECT) {
907 dev_dbg(&hub->ports[port1 - 1]->dev,
908 "Not disabling port; link state is RxDetect\n");
909 return ret;
910 }
911
Sarah Sharp41e7e052012-11-14 16:42:32 -0800912 ret = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_SS_DISABLED);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400913 if (ret)
Sarah Sharp41e7e052012-11-14 16:42:32 -0800914 return ret;
Sarah Sharp41e7e052012-11-14 16:42:32 -0800915
916 /* Wait for the link to enter the disabled state. */
917 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
918 ret = hub_port_status(hub, port1, &portstatus, &portchange);
919 if (ret < 0)
920 return ret;
921
922 if ((portstatus & USB_PORT_STAT_LINK_STATE) ==
923 USB_SS_PORT_LS_SS_DISABLED)
924 break;
925 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
926 break;
927 msleep(HUB_DEBOUNCE_STEP);
928 }
929 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
Dan Williamsd99f6b42014-05-20 18:08:17 -0700930 dev_warn(&hub->ports[port1 - 1]->dev,
931 "Could not disable after %d ms\n", total_time);
Sarah Sharp41e7e052012-11-14 16:42:32 -0800932
933 return hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_RX_DETECT);
934}
935
Alan Stern8b28c752005-08-10 17:04:13 -0400936static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
937{
Dan Williamsd99f6b42014-05-20 18:08:17 -0700938 struct usb_port *port_dev = hub->ports[port1 - 1];
Alan Stern8b28c752005-08-10 17:04:13 -0400939 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400940 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400941
Dan Williamsd99f6b42014-05-20 18:08:17 -0700942 if (port_dev->child && set_state)
943 usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED);
Sarah Sharp41e7e052012-11-14 16:42:32 -0800944 if (!hub->error) {
945 if (hub_is_superspeed(hub->hdev))
946 ret = hub_usb3_port_disable(hub, port1);
947 else
Lan Tianyuad493e52013-01-23 04:26:30 +0800948 ret = usb_clear_port_feature(hdev, port1,
Sarah Sharp41e7e052012-11-14 16:42:32 -0800949 USB_PORT_FEAT_ENABLE);
950 }
Alan Sterne9e88fb2013-03-27 16:14:01 -0400951 if (ret && ret != -ENODEV)
Dan Williamsd99f6b42014-05-20 18:08:17 -0700952 dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400953 return ret;
954}
955
Alan Stern0458d5b2007-05-04 11:52:20 -0400956/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800957 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400958 * time later khubd will disconnect() any existing usb_device on the port
959 * and will re-enumerate if there actually is a device attached.
960 */
961static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
962{
Dan Williamsd99f6b42014-05-20 18:08:17 -0700963 dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n");
Alan Stern0458d5b2007-05-04 11:52:20 -0400964 hub_port_disable(hub, port1, 1);
965
966 /* FIXME let caller ask to power down the port:
967 * - some devices won't enumerate without a VBUS power cycle
968 * - SRP saves power that way
969 * - ... new call, TBD ...
970 * That's easy if this hub can switch power per-port, and
971 * khubd reactivates the port later (timer, SRP, etc).
972 * Powerdown must be optional, because of reset/DFU.
973 */
974
975 set_bit(port1, hub->change_bits);
Matthias Beyer469271f2013-10-10 23:41:27 +0200976 kick_khubd(hub);
Alan Stern0458d5b2007-05-04 11:52:20 -0400977}
978
Alan Stern253e0572009-10-27 15:20:13 -0400979/**
980 * usb_remove_device - disable a device's port on its parent hub
981 * @udev: device to be disabled and removed
982 * Context: @udev locked, must be able to sleep.
983 *
984 * After @udev's port has been disabled, khubd is notified and it will
985 * see that the device has been disconnected. When the device is
986 * physically unplugged and something is plugged in, the events will
987 * be received and processed normally.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200988 *
989 * Return: 0 if successful. A negative error code otherwise.
Alan Stern253e0572009-10-27 15:20:13 -0400990 */
991int usb_remove_device(struct usb_device *udev)
992{
993 struct usb_hub *hub;
994 struct usb_interface *intf;
995
996 if (!udev->parent) /* Can't remove a root hub */
997 return -EINVAL;
Lan Tianyuad493e52013-01-23 04:26:30 +0800998 hub = usb_hub_to_struct_hub(udev->parent);
Alan Stern253e0572009-10-27 15:20:13 -0400999 intf = to_usb_interface(hub->intfdev);
1000
1001 usb_autopm_get_interface(intf);
1002 set_bit(udev->portnum, hub->removed_bits);
1003 hub_port_logical_disconnect(hub, udev->portnum);
1004 usb_autopm_put_interface(intf);
1005 return 0;
1006}
1007
Alan Stern6ee0b272008-04-28 11:06:42 -04001008enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -05001009 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -04001010 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -04001011};
Alan Stern5e6effa2008-03-03 15:15:51 -05001012
Alan Stern8520f382008-09-22 14:44:26 -04001013static void hub_init_func2(struct work_struct *ws);
1014static void hub_init_func3(struct work_struct *ws);
1015
Alan Sternf2835212008-04-28 11:07:17 -04001016static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -05001017{
1018 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -08001019 struct usb_hcd *hcd;
1020 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -05001021 int port1;
Alan Sternf2835212008-04-28 11:07:17 -04001022 int status;
Alan Stern948fea32008-04-28 11:07:07 -04001023 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -04001024 unsigned delay;
1025
1026 /* Continue a partial initialization */
1027 if (type == HUB_INIT2)
1028 goto init2;
1029 if (type == HUB_INIT3)
1030 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -05001031
Elric Fua45aa3b2012-02-18 13:32:27 +08001032 /* The superspeed hub except for root hub has to use Hub Depth
1033 * value as an offset into the route string to locate the bits
1034 * it uses to determine the downstream port number. So hub driver
1035 * should send a set hub depth request to superspeed hub after
1036 * the superspeed hub is set configuration in initialization or
1037 * reset procedure.
1038 *
1039 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -04001040 * For any other type of activation, turn it on.
1041 */
Alan Stern8520f382008-09-22 14:44:26 -04001042 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +08001043 if (hdev->parent && hub_is_superspeed(hdev)) {
1044 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1045 HUB_SET_DEPTH, USB_RT_HUB,
1046 hdev->level - 1, 0, NULL, 0,
1047 USB_CTRL_SET_TIMEOUT);
1048 if (ret < 0)
1049 dev_err(hub->intfdev,
1050 "set hub depth failed\n");
1051 }
Alan Stern8520f382008-09-22 14:44:26 -04001052
1053 /* Speed up system boot by using a delayed_work for the
1054 * hub's initial power-up delays. This is pretty awkward
1055 * and the implementation looks like a home-brewed sort of
1056 * setjmp/longjmp, but it saves at least 100 ms for each
1057 * root hub (assuming usbcore is compiled into the kernel
1058 * rather than as a module). It adds up.
1059 *
1060 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
1061 * because for those activation types the ports have to be
1062 * operational when we return. In theory this could be done
1063 * for HUB_POST_RESET, but it's easier not to.
1064 */
1065 if (type == HUB_INIT) {
Dan Williams7ad3c472014-05-20 18:08:57 -07001066 unsigned delay = hub_power_on_good_delay(hub);
1067
1068 hub_power_on(hub, false);
Tejun Heo77fa83c2014-03-07 10:24:48 -05001069 INIT_DELAYED_WORK(&hub->init_work, hub_init_func2);
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08001070 queue_delayed_work(system_power_efficient_wq,
1071 &hub->init_work,
Alan Stern8520f382008-09-22 14:44:26 -04001072 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -04001073
1074 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -05001075 usb_autopm_get_interface_no_resume(
1076 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -04001077 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001078 } else if (type == HUB_RESET_RESUME) {
1079 /* The internal host controller state for the hub device
1080 * may be gone after a host power loss on system resume.
1081 * Update the device's info so the HW knows it's a hub.
1082 */
1083 hcd = bus_to_hcd(hdev->bus);
1084 if (hcd->driver->update_hub_device) {
1085 ret = hcd->driver->update_hub_device(hcd, hdev,
1086 &hub->tt, GFP_NOIO);
1087 if (ret < 0) {
1088 dev_err(hub->intfdev, "Host not "
1089 "accepting hub info "
1090 "update.\n");
1091 dev_err(hub->intfdev, "LS/FS devices "
1092 "and hubs may not work "
1093 "under this hub\n.");
1094 }
1095 }
1096 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001097 } else {
1098 hub_power_on(hub, true);
1099 }
1100 }
1101 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001102
Dan Williamsd99f6b42014-05-20 18:08:17 -07001103 /*
1104 * Check each port and set hub->change_bits to let khubd know
Alan Stern6ee0b272008-04-28 11:06:42 -04001105 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001106 */
1107 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07001108 struct usb_port *port_dev = hub->ports[port1 - 1];
1109 struct usb_device *udev = port_dev->child;
Alan Stern5e6effa2008-03-03 15:15:51 -05001110 u16 portstatus, portchange;
1111
Alan Stern6ee0b272008-04-28 11:06:42 -04001112 portstatus = portchange = 0;
1113 status = hub_port_status(hub, port1, &portstatus, &portchange);
1114 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
Dan Williamsd99f6b42014-05-20 18:08:17 -07001115 dev_dbg(&port_dev->dev, "status %04x change %04x\n",
1116 portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001117
Dan Williamsd99f6b42014-05-20 18:08:17 -07001118 /*
1119 * After anything other than HUB_RESUME (i.e., initialization
Alan Stern6ee0b272008-04-28 11:06:42 -04001120 * or any sort of reset), every port should be disabled.
1121 * Unconnected ports should likewise be disabled (paranoia),
1122 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001123 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001124 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1125 type != HUB_RESUME ||
1126 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1127 !udev ||
1128 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001129 /*
1130 * USB3 protocol ports will automatically transition
1131 * to Enabled state when detect an USB3.0 device attach.
Dan Williamsfd1ac4c2013-10-07 11:58:20 -07001132 * Do not disable USB3 protocol ports, just pretend
1133 * power was lost
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001134 */
Dan Williamsfd1ac4c2013-10-07 11:58:20 -07001135 portstatus &= ~USB_PORT_STAT_ENABLE;
1136 if (!hub_is_superspeed(hdev))
Lan Tianyuad493e52013-01-23 04:26:30 +08001137 usb_clear_port_feature(hdev, port1,
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001138 USB_PORT_FEAT_ENABLE);
Alan Stern6ee0b272008-04-28 11:06:42 -04001139 }
1140
Alan Stern948fea32008-04-28 11:07:07 -04001141 /* Clear status-change flags; we'll debounce later */
1142 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1143 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001144 usb_clear_port_feature(hub->hdev, port1,
Alan Stern948fea32008-04-28 11:07:07 -04001145 USB_PORT_FEAT_C_CONNECTION);
1146 }
1147 if (portchange & USB_PORT_STAT_C_ENABLE) {
1148 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001149 usb_clear_port_feature(hub->hdev, port1,
Alan Stern948fea32008-04-28 11:07:07 -04001150 USB_PORT_FEAT_C_ENABLE);
1151 }
Julius Wernere92aee32013-10-15 17:45:00 -07001152 if (portchange & USB_PORT_STAT_C_RESET) {
1153 need_debounce_delay = true;
1154 usb_clear_port_feature(hub->hdev, port1,
1155 USB_PORT_FEAT_C_RESET);
1156 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001157 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1158 hub_is_superspeed(hub->hdev)) {
1159 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001160 usb_clear_port_feature(hub->hdev, port1,
Don Zickus79c3dd82011-11-03 09:07:18 -04001161 USB_PORT_FEAT_C_BH_PORT_RESET);
1162 }
Alan Stern253e0572009-10-27 15:20:13 -04001163 /* We can forget about a "removed" device when there's a
1164 * physical disconnect or the connect status changes.
1165 */
1166 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1167 (portchange & USB_PORT_STAT_C_CONNECTION))
1168 clear_bit(port1, hub->removed_bits);
1169
Alan Stern6ee0b272008-04-28 11:06:42 -04001170 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1171 /* Tell khubd to disconnect the device or
1172 * check for a new connection
1173 */
Shen Guang08d1dec2014-01-08 14:45:42 +08001174 if (udev || (portstatus & USB_PORT_STAT_CONNECTION) ||
1175 (portstatus & USB_PORT_STAT_OVERCURRENT))
Alan Stern6ee0b272008-04-28 11:06:42 -04001176 set_bit(port1, hub->change_bits);
1177
1178 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001179 bool port_resumed = (portstatus &
1180 USB_PORT_STAT_LINK_STATE) ==
1181 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001182 /* The power session apparently survived the resume.
1183 * If there was an overcurrent or suspend change
1184 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -08001185 * take care of it. Look at the port link state
1186 * for USB 3.0 hubs, since they don't have a suspend
1187 * change bit, and they don't set the port link change
1188 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001189 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001190 if (portchange || (hub_is_superspeed(hub->hdev) &&
1191 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001192 set_bit(port1, hub->change_bits);
1193
1194 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001195#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001196 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001197#endif
Lan Tianyuad493e52013-01-23 04:26:30 +08001198 /* Don't set the change_bits when the device
1199 * was powered off.
1200 */
Dan Williamsd5c38342014-05-20 18:08:52 -07001201 if (test_bit(port1, hub->power_bits))
Lan Tianyuad493e52013-01-23 04:26:30 +08001202 set_bit(port1, hub->change_bits);
Alan Stern8808f002008-04-28 11:06:55 -04001203
Alan Stern6ee0b272008-04-28 11:06:42 -04001204 } else {
1205 /* The power session is gone; tell khubd */
1206 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1207 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001208 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001209 }
1210
Alan Stern948fea32008-04-28 11:07:07 -04001211 /* If no port-status-change flags were set, we don't need any
1212 * debouncing. If flags were set we can try to debounce the
1213 * ports all at once right now, instead of letting khubd do them
1214 * one at a time later on.
1215 *
1216 * If any port-status changes do occur during this delay, khubd
1217 * will see them later and handle them normally.
1218 */
Alan Stern8520f382008-09-22 14:44:26 -04001219 if (need_debounce_delay) {
1220 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001221
Alan Stern8520f382008-09-22 14:44:26 -04001222 /* Don't do a long sleep inside a workqueue routine */
1223 if (type == HUB_INIT2) {
Tejun Heo77fa83c2014-03-07 10:24:48 -05001224 INIT_DELAYED_WORK(&hub->init_work, hub_init_func3);
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08001225 queue_delayed_work(system_power_efficient_wq,
1226 &hub->init_work,
Alan Stern8520f382008-09-22 14:44:26 -04001227 msecs_to_jiffies(delay));
1228 return; /* Continues at init3: below */
1229 } else {
1230 msleep(delay);
1231 }
1232 }
1233 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001234 hub->quiescing = 0;
1235
1236 status = usb_submit_urb(hub->urb, GFP_NOIO);
1237 if (status < 0)
1238 dev_err(hub->intfdev, "activate --> %d\n", status);
1239 if (hub->has_indicators && blinkenlights)
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08001240 queue_delayed_work(system_power_efficient_wq,
1241 &hub->leds, LED_CYCLE_PERIOD);
Alan Sternf2835212008-04-28 11:07:17 -04001242
1243 /* Scan all ports that need attention */
1244 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001245
1246 /* Allow autosuspend if it was suppressed */
1247 if (type <= HUB_INIT3)
1248 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -05001249}
1250
Alan Stern8520f382008-09-22 14:44:26 -04001251/* Implement the continuations for the delays above */
1252static void hub_init_func2(struct work_struct *ws)
1253{
1254 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1255
1256 hub_activate(hub, HUB_INIT2);
1257}
1258
1259static void hub_init_func3(struct work_struct *ws)
1260{
1261 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1262
1263 hub_activate(hub, HUB_INIT3);
1264}
1265
Alan Stern43303542008-04-28 11:07:31 -04001266enum hub_quiescing_type {
1267 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1268};
1269
1270static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1271{
1272 struct usb_device *hdev = hub->hdev;
1273 int i;
1274
Alan Stern8520f382008-09-22 14:44:26 -04001275 cancel_delayed_work_sync(&hub->init_work);
1276
Alan Stern43303542008-04-28 11:07:31 -04001277 /* khubd and related activity won't re-trigger */
1278 hub->quiescing = 1;
1279
1280 if (type != HUB_SUSPEND) {
1281 /* Disconnect all the children */
1282 for (i = 0; i < hdev->maxchild; ++i) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001283 if (hub->ports[i]->child)
1284 usb_disconnect(&hub->ports[i]->child);
Alan Stern43303542008-04-28 11:07:31 -04001285 }
1286 }
1287
1288 /* Stop khubd and related activity */
1289 usb_kill_urb(hub->urb);
1290 if (hub->has_indicators)
1291 cancel_delayed_work_sync(&hub->leds);
1292 if (hub->tt.hub)
Octavian Purdila036546b2012-10-23 11:33:12 +03001293 flush_work(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001294}
1295
Alan Stern600856c2014-05-20 18:08:07 -07001296static void hub_pm_barrier_for_all_ports(struct usb_hub *hub)
1297{
1298 int i;
1299
1300 for (i = 0; i < hub->hdev->maxchild; ++i)
1301 pm_runtime_barrier(&hub->ports[i]->dev);
1302}
1303
Alan Stern3eb14912008-03-03 15:15:43 -05001304/* caller has locked the hub device */
1305static int hub_pre_reset(struct usb_interface *intf)
1306{
1307 struct usb_hub *hub = usb_get_intfdata(intf);
1308
Alan Stern43303542008-04-28 11:07:31 -04001309 hub_quiesce(hub, HUB_PRE_RESET);
Alan Stern600856c2014-05-20 18:08:07 -07001310 hub->in_reset = 1;
1311 hub_pm_barrier_for_all_ports(hub);
Alan Sternf07600c2007-05-30 15:38:16 -04001312 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001313}
1314
1315/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001316static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001317{
Alan Stern7de18d82006-06-01 13:37:24 -04001318 struct usb_hub *hub = usb_get_intfdata(intf);
1319
Alan Stern600856c2014-05-20 18:08:07 -07001320 hub->in_reset = 0;
1321 hub_pm_barrier_for_all_ports(hub);
Alan Sternf2835212008-04-28 11:07:17 -04001322 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001323 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001324}
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326static int hub_configure(struct usb_hub *hub,
1327 struct usb_endpoint_descriptor *endpoint)
1328{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001329 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 struct usb_device *hdev = hub->hdev;
1331 struct device *hub_dev = hub->intfdev;
1332 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001333 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 unsigned int pipe;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001335 int maxp, ret, i;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001336 char *message = "out of memory";
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001337 unsigned unit_load;
1338 unsigned full_load;
Dan Williamsd8521af2014-05-20 18:08:28 -07001339 unsigned maxchild;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Alan Sternd697cdd2009-10-27 15:18:46 -04001341 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 ret = -ENOMEM;
1344 goto fail;
1345 }
1346
1347 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1348 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 ret = -ENOMEM;
1350 goto fail;
1351 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001352 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1355 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 ret = -ENOMEM;
1357 goto fail;
1358 }
1359
1360 /* Request the entire hub descriptor.
1361 * hub->descriptor can handle USB_MAXCHILDREN ports,
1362 * but the hub can/will return fewer bytes here.
1363 */
John Youndbe79bb2001-09-17 00:00:00 -07001364 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (ret < 0) {
1366 message = "can't read hub descriptor";
1367 goto fail;
1368 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1369 message = "hub has too many ports!";
1370 ret = -ENODEV;
1371 goto fail;
David Linares769d73682013-03-25 10:50:27 +00001372 } else if (hub->descriptor->bNbrPorts == 0) {
1373 message = "hub doesn't have any ports!";
1374 ret = -ENODEV;
1375 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377
Dan Williamsd8521af2014-05-20 18:08:28 -07001378 maxchild = hub->descriptor->bNbrPorts;
1379 dev_info(hub_dev, "%d port%s detected\n", maxchild,
1380 (maxchild == 1) ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Dan Williamsd8521af2014-05-20 18:08:28 -07001382 hub->ports = kzalloc(maxchild * sizeof(struct usb_port *), GFP_KERNEL);
Lan Tianyuff823c792012-09-05 13:44:32 +08001383 if (!hub->ports) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001384 ret = -ENOMEM;
1385 goto fail;
1386 }
1387
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001388 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001389 if (hub_is_superspeed(hdev)) {
1390 unit_load = 150;
1391 full_load = 900;
1392 } else {
1393 unit_load = 100;
1394 full_load = 500;
1395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
John Youndbe79bb2001-09-17 00:00:00 -07001397 /* FIXME for USB 3.0, skip for now */
1398 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1399 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int i;
Matthias Beyer469271f2013-10-10 23:41:27 +02001401 char portstr[USB_MAXCHILDREN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Dan Williamsd8521af2014-05-20 18:08:28 -07001403 for (i = 0; i < maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001404 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1406 ? 'F' : 'R';
Dan Williamsd8521af2014-05-20 18:08:28 -07001407 portstr[maxchild] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1409 } else
1410 dev_dbg(hub_dev, "standalone hub\n");
1411
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001412 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301413 case HUB_CHAR_COMMON_LPSM:
1414 dev_dbg(hub_dev, "ganged power switching\n");
1415 break;
1416 case HUB_CHAR_INDV_PORT_LPSM:
1417 dev_dbg(hub_dev, "individual port power switching\n");
1418 break;
1419 case HUB_CHAR_NO_LPSM:
1420 case HUB_CHAR_LPSM:
1421 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1422 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001425 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301426 case HUB_CHAR_COMMON_OCPM:
1427 dev_dbg(hub_dev, "global over-current protection\n");
1428 break;
1429 case HUB_CHAR_INDV_PORT_OCPM:
1430 dev_dbg(hub_dev, "individual port over-current protection\n");
1431 break;
1432 case HUB_CHAR_NO_OCPM:
1433 case HUB_CHAR_OCPM:
1434 dev_dbg(hub_dev, "no over-current protection\n");
1435 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437
1438 spin_lock_init (&hub->tt.lock);
1439 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001440 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301442 case USB_HUB_PR_FS:
1443 break;
1444 case USB_HUB_PR_HS_SINGLE_TT:
1445 dev_dbg(hub_dev, "Single TT\n");
1446 hub->tt.hub = hdev;
1447 break;
1448 case USB_HUB_PR_HS_MULTI_TT:
1449 ret = usb_set_interface(hdev, 0, 1);
1450 if (ret == 0) {
1451 dev_dbg(hub_dev, "TT per port\n");
1452 hub->tt.multi = 1;
1453 } else
1454 dev_err(hub_dev, "Using single TT (err %d)\n",
1455 ret);
1456 hub->tt.hub = hdev;
1457 break;
1458 case USB_HUB_PR_SS:
1459 /* USB 3.0 hubs don't have a TT */
1460 break;
1461 default:
1462 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1463 hdev->descriptor.bDeviceProtocol);
1464 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001467 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001468 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
Matthias Beyer469271f2013-10-10 23:41:27 +02001469 case HUB_TTTT_8_BITS:
1470 if (hdev->descriptor.bDeviceProtocol != 0) {
1471 hub->tt.think_time = 666;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001472 dev_dbg(hub_dev, "TT requires at most %d "
1473 "FS bit times (%d ns)\n",
Matthias Beyer469271f2013-10-10 23:41:27 +02001474 8, hub->tt.think_time);
1475 }
1476 break;
1477 case HUB_TTTT_16_BITS:
1478 hub->tt.think_time = 666 * 2;
1479 dev_dbg(hub_dev, "TT requires at most %d "
1480 "FS bit times (%d ns)\n",
1481 16, hub->tt.think_time);
1482 break;
1483 case HUB_TTTT_24_BITS:
1484 hub->tt.think_time = 666 * 3;
1485 dev_dbg(hub_dev, "TT requires at most %d "
1486 "FS bit times (%d ns)\n",
1487 24, hub->tt.think_time);
1488 break;
1489 case HUB_TTTT_32_BITS:
1490 hub->tt.think_time = 666 * 4;
1491 dev_dbg(hub_dev, "TT requires at most %d "
1492 "FS bit times (%d ns)\n",
1493 32, hub->tt.think_time);
1494 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496
1497 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001498 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 hub->has_indicators = 1;
1500 dev_dbg(hub_dev, "Port indicators are supported\n");
1501 }
1502
1503 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1504 hub->descriptor->bPwrOn2PwrGood * 2);
1505
1506 /* power budgeting mostly matters with bus-powered hubs,
1507 * and battery-powered root hubs (may provide just 8 mA).
1508 */
1509 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern15b73362013-07-30 15:35:40 -04001510 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 message = "can't get hub status";
1512 goto fail;
1513 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001514 hcd = bus_to_hcd(hdev->bus);
Alan Stern7d35b922005-04-25 11:18:32 -04001515 if (hdev == hdev->bus->root_hub) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001516 if (hcd->power_budget > 0)
1517 hdev->bus_mA = hcd->power_budget;
1518 else
Dan Williamsd8521af2014-05-20 18:08:28 -07001519 hdev->bus_mA = full_load * maxchild;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001520 if (hdev->bus_mA >= full_load)
1521 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001522 else {
1523 hub->mA_per_port = hdev->bus_mA;
1524 hub->limited_power = 1;
1525 }
Alan Stern7d35b922005-04-25 11:18:32 -04001526 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001527 int remaining = hdev->bus_mA -
1528 hub->descriptor->bHubContrCurrent;
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1531 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001532 hub->limited_power = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Dan Williamsd8521af2014-05-20 18:08:28 -07001534 if (remaining < maxchild * unit_load)
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001535 dev_warn(hub_dev,
Alan Stern55c52712005-11-23 12:03:12 -05001536 "insufficient power available "
1537 "to use all downstream ports\n");
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001538 hub->mA_per_port = unit_load; /* 7.2.1 */
1539
Alan Stern55c52712005-11-23 12:03:12 -05001540 } else { /* Self-powered external hub */
1541 /* FIXME: What about battery-powered external hubs that
1542 * provide less current per port? */
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001543 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001544 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001545 if (hub->mA_per_port < full_load)
Alan Stern55c52712005-11-23 12:03:12 -05001546 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1547 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1550 if (ret < 0) {
1551 message = "can't get hub status";
1552 goto fail;
1553 }
1554
1555 /* local power status reports aren't always correct */
1556 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1557 dev_dbg(hub_dev, "local power source is %s\n",
1558 (hubstatus & HUB_STATUS_LOCAL_POWER)
1559 ? "lost (inactive)" : "good");
1560
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001561 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 dev_dbg(hub_dev, "%sover-current condition exists\n",
1563 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1564
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001565 /* set up the interrupt endpoint
1566 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1567 * bytes as USB2.0[11.12.3] says because some hubs are known
1568 * to send more data (and thus cause overflow). For root hubs,
1569 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1570 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1572 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1573
1574 if (maxp > sizeof(*hub->buffer))
1575 maxp = sizeof(*hub->buffer);
1576
1577 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1578 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 ret = -ENOMEM;
1580 goto fail;
1581 }
1582
1583 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1584 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 /* maybe cycle the hub leds */
1587 if (hub->has_indicators && blinkenlights)
Matthias Beyer469271f2013-10-10 23:41:27 +02001588 hub->indicator[0] = INDICATOR_CYCLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Dan Williamsd8521af2014-05-20 18:08:28 -07001590 mutex_lock(&usb_port_peer_mutex);
1591 for (i = 0; i < maxchild; i++) {
Krzysztof Mazure58547e2013-08-22 14:49:39 +02001592 ret = usb_hub_create_port_device(hub, i + 1);
1593 if (ret < 0) {
Lan Tianyufa2a9562012-09-05 13:44:31 +08001594 dev_err(hub->intfdev,
1595 "couldn't create port%d device.\n", i + 1);
Dan Williamsd8521af2014-05-20 18:08:28 -07001596 break;
Krzysztof Mazure58547e2013-08-22 14:49:39 +02001597 }
1598 }
Dan Williamsd8521af2014-05-20 18:08:28 -07001599 hdev->maxchild = i;
Dan Williamse3d10502014-06-17 16:16:32 -07001600 for (i = 0; i < hdev->maxchild; i++) {
1601 struct usb_port *port_dev = hub->ports[i];
1602
1603 pm_runtime_put(&port_dev->dev);
1604 }
1605
Dan Williamsd8521af2014-05-20 18:08:28 -07001606 mutex_unlock(&usb_port_peer_mutex);
1607 if (ret < 0)
1608 goto fail;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001609
Dan Williamse3d95582014-06-05 14:23:04 -07001610 /* Update the HCD's internal representation of this hub before khubd
1611 * starts getting port status changes for devices under the hub.
1612 */
1613 if (hcd->driver->update_hub_device) {
1614 ret = hcd->driver->update_hub_device(hcd, hdev,
1615 &hub->tt, GFP_KERNEL);
1616 if (ret < 0) {
1617 message = "can't update HCD hub info";
1618 goto fail;
1619 }
1620 }
1621
Lan Tianyud2123fd2013-01-21 22:18:00 +08001622 usb_hub_adjust_deviceremovable(hdev, hub->descriptor);
1623
Alan Sternf2835212008-04-28 11:07:17 -04001624 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return 0;
1626
1627fail:
1628 dev_err (hub_dev, "config failed, %s (err %d)\n",
1629 message, ret);
1630 /* hub_disconnect() frees urb and descriptor */
1631 return ret;
1632}
1633
Alan Sterne8054852007-05-04 11:55:11 -04001634static void hub_release(struct kref *kref)
1635{
1636 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1637
Petr Mladek5d14f322014-09-19 17:32:19 +02001638 usb_put_dev(hub->hdev);
Alan Sterne8054852007-05-04 11:55:11 -04001639 usb_put_intf(to_usb_interface(hub->intfdev));
1640 kfree(hub);
1641}
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643static unsigned highspeed_hubs;
1644
1645static void hub_disconnect(struct usb_interface *intf)
1646{
Huajun Li88162302012-03-12 21:00:19 +08001647 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001648 struct usb_device *hdev = interface_to_usbdev(intf);
Alan Stern543d7782014-01-07 10:43:02 -05001649 int port1;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001650
Alan Sterne8054852007-05-04 11:55:11 -04001651 /* Take the hub off the event list and don't let it be added again */
1652 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001653 if (!list_empty(&hub->event_list)) {
1654 list_del_init(&hub->event_list);
1655 usb_autopm_put_interface_no_suspend(intf);
1656 }
Alan Sterne8054852007-05-04 11:55:11 -04001657 hub->disconnected = 1;
1658 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Alan Stern7de18d82006-06-01 13:37:24 -04001660 /* Disconnect all children and quiesce the hub */
1661 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001662 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001663
Dan Williamsd8521af2014-05-20 18:08:28 -07001664 mutex_lock(&usb_port_peer_mutex);
1665
Alan Stern543d7782014-01-07 10:43:02 -05001666 /* Avoid races with recursively_mark_NOTATTACHED() */
1667 spin_lock_irq(&device_state_lock);
1668 port1 = hdev->maxchild;
1669 hdev->maxchild = 0;
1670 usb_set_intfdata(intf, NULL);
1671 spin_unlock_irq(&device_state_lock);
Alexander Shishkin1f2235b2012-09-12 14:48:31 +03001672
Alan Stern543d7782014-01-07 10:43:02 -05001673 for (; port1 > 0; --port1)
1674 usb_hub_remove_port_device(hub, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Dan Williamsd8521af2014-05-20 18:08:28 -07001676 mutex_unlock(&usb_port_peer_mutex);
1677
Alan Sterne8054852007-05-04 11:55:11 -04001678 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 highspeed_hubs--;
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 usb_free_urb(hub->urb);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001682 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001683 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001684 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001685 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Lan Tianyu971fcd42013-01-23 04:26:29 +08001687 pm_suspend_ignore_children(&intf->dev, false);
Alan Sterne8054852007-05-04 11:55:11 -04001688 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
1691static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1692{
1693 struct usb_host_interface *desc;
1694 struct usb_endpoint_descriptor *endpoint;
1695 struct usb_device *hdev;
1696 struct usb_hub *hub;
1697
1698 desc = intf->cur_altsetting;
1699 hdev = interface_to_usbdev(intf);
1700
Ming Lei596d7892012-10-24 11:59:25 +08001701 /*
1702 * Set default autosuspend delay as 0 to speedup bus suspend,
1703 * based on the below considerations:
1704 *
1705 * - Unlike other drivers, the hub driver does not rely on the
1706 * autosuspend delay to provide enough time to handle a wakeup
1707 * event, and the submitted status URB is just to check future
1708 * change on hub downstream ports, so it is safe to do it.
1709 *
1710 * - The patch might cause one or more auto supend/resume for
1711 * below very rare devices when they are plugged into hub
1712 * first time:
1713 *
1714 * devices having trouble initializing, and disconnect
1715 * themselves from the bus and then reconnect a second
1716 * or so later
1717 *
1718 * devices just for downloading firmware, and disconnects
1719 * themselves after completing it
1720 *
1721 * For these quite rare devices, their drivers may change the
1722 * autosuspend delay of their parent hub in the probe() to one
1723 * appropriate value to avoid the subtle problem if someone
1724 * does care it.
1725 *
1726 * - The patch may cause one or more auto suspend/resume on
1727 * hub during running 'lsusb', but it is probably too
1728 * infrequent to worry about.
1729 *
1730 * - Change autosuspend delay of hub can avoid unnecessary auto
1731 * suspend timer for hub, also may decrease power consumption
1732 * of USB bus.
Roger Quadrosbdd405d2014-08-04 12:44:46 +03001733 *
1734 * - If user has indicated to prevent autosuspend by passing
1735 * usbcore.autosuspend = -1 then keep autosuspend disabled.
Ming Lei596d7892012-10-24 11:59:25 +08001736 */
Greg Kroah-Hartmana9ef8032014-08-27 16:55:29 -07001737#ifdef CONFIG_PM_RUNTIME
Roger Quadrosbdd405d2014-08-04 12:44:46 +03001738 if (hdev->dev.power.autosuspend_delay >= 0)
1739 pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
Greg Kroah-Hartmana9ef8032014-08-27 16:55:29 -07001740#endif
Ming Lei596d7892012-10-24 11:59:25 +08001741
Alan Stern8ef42dd2014-05-23 10:45:54 -04001742 /*
1743 * Hubs have proper suspend/resume support, except for root hubs
1744 * where the controller driver doesn't have bus_suspend and
1745 * bus_resume methods.
1746 */
1747 if (hdev->parent) { /* normal device */
1748 usb_enable_autosuspend(hdev);
1749 } else { /* root hub */
1750 const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver;
1751
1752 if (drv->bus_suspend && drv->bus_resume)
1753 usb_enable_autosuspend(hdev);
1754 }
Alan Stern088f7fe2010-01-08 12:56:54 -05001755
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001756 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001757 dev_err(&intf->dev,
1758 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001759 return -E2BIG;
1760 }
1761
David Brownell89ccbdc2006-04-02 10:18:09 -08001762#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1763 if (hdev->parent) {
1764 dev_warn(&intf->dev, "ignoring external hub\n");
1765 return -ENODEV;
1766 }
1767#endif
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /* Some hubs have a subclass of 1, which AFAICT according to the */
1770 /* specs is not defined, but it works */
1771 if ((desc->desc.bInterfaceSubClass != 0) &&
1772 (desc->desc.bInterfaceSubClass != 1)) {
1773descriptor_error:
1774 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1775 return -EIO;
1776 }
1777
1778 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1779 if (desc->desc.bNumEndpoints != 1)
1780 goto descriptor_error;
1781
1782 endpoint = &desc->endpoint[0].desc;
1783
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001784 /* If it's not an interrupt in endpoint, we'd better punt! */
1785 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 goto descriptor_error;
1787
1788 /* We found a hub */
1789 dev_info (&intf->dev, "USB hub found\n");
1790
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001791 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (!hub) {
1793 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1794 return -ENOMEM;
1795 }
1796
Alan Sterne8054852007-05-04 11:55:11 -04001797 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 INIT_LIST_HEAD(&hub->event_list);
1799 hub->intfdev = &intf->dev;
1800 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001801 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001802 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001803 usb_get_intf(intf);
Petr Mladek5d14f322014-09-19 17:32:19 +02001804 usb_get_dev(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001807 intf->needs_remote_wakeup = 1;
Lan Tianyu971fcd42013-01-23 04:26:29 +08001808 pm_suspend_ignore_children(&intf->dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 if (hdev->speed == USB_SPEED_HIGH)
1811 highspeed_hubs++;
1812
Ming Leie6f30de2012-10-24 11:59:24 +08001813 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1814 hub->quirk_check_port_auto_suspend = 1;
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (hub_configure(hub, endpoint) >= 0)
1817 return 0;
1818
1819 hub_disconnect (intf);
1820 return -ENODEV;
1821}
1822
1823static int
1824hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1825{
1826 struct usb_device *hdev = interface_to_usbdev (intf);
Lan Tianyuad493e52013-01-23 04:26:30 +08001827 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 /* assert ifno == 0 (part of hub spec) */
1830 switch (code) {
1831 case USBDEVFS_HUB_PORTINFO: {
1832 struct usbdevfs_hub_portinfo *info = user_data;
1833 int i;
1834
1835 spin_lock_irq(&device_state_lock);
1836 if (hdev->devnum <= 0)
1837 info->nports = 0;
1838 else {
1839 info->nports = hdev->maxchild;
1840 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001841 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 info->port[i] = 0;
1843 else
1844 info->port[i] =
Lan Tianyuff823c792012-09-05 13:44:32 +08001845 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
1847 }
1848 spin_unlock_irq(&device_state_lock);
1849
1850 return info->nports + 1;
1851 }
1852
1853 default:
1854 return -ENOSYS;
1855 }
1856}
1857
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001858/*
1859 * Allow user programs to claim ports on a hub. When a device is attached
1860 * to one of these "claimed" ports, the program will "own" the device.
1861 */
1862static int find_port_owner(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001863 struct usb_dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001864{
Mathias Nyman413412612013-06-18 17:28:48 +03001865 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
1866
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001867 if (hdev->state == USB_STATE_NOTATTACHED)
1868 return -ENODEV;
1869 if (port1 == 0 || port1 > hdev->maxchild)
1870 return -EINVAL;
1871
Mathias Nyman413412612013-06-18 17:28:48 +03001872 /* Devices not managed by the hub driver
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001873 * will always have maxchild equal to 0.
1874 */
Mathias Nyman413412612013-06-18 17:28:48 +03001875 *ppowner = &(hub->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001876 return 0;
1877}
1878
1879/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001880int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001881 struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001882{
1883 int rc;
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001884 struct usb_dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001885
1886 rc = find_port_owner(hdev, port1, &powner);
1887 if (rc)
1888 return rc;
1889 if (*powner)
1890 return -EBUSY;
1891 *powner = owner;
1892 return rc;
1893}
Valentina Manea6080cd02014-03-08 14:53:34 +02001894EXPORT_SYMBOL_GPL(usb_hub_claim_port);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001895
Lan Tianyu336c5c32012-07-06 14:13:52 +08001896int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001897 struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001898{
1899 int rc;
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001900 struct usb_dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001901
1902 rc = find_port_owner(hdev, port1, &powner);
1903 if (rc)
1904 return rc;
1905 if (*powner != owner)
1906 return -ENOENT;
1907 *powner = NULL;
1908 return rc;
1909}
Valentina Manea6080cd02014-03-08 14:53:34 +02001910EXPORT_SYMBOL_GPL(usb_hub_release_port);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001911
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001912void usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001913{
Lan Tianyuad493e52013-01-23 04:26:30 +08001914 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001915 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001916
Lan Tianyufa2a9562012-09-05 13:44:31 +08001917 for (n = 0; n < hdev->maxchild; n++) {
1918 if (hub->ports[n]->port_owner == owner)
1919 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001920 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001921
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001922}
1923
1924/* The caller must hold udev's lock */
1925bool usb_device_is_owned(struct usb_device *udev)
1926{
1927 struct usb_hub *hub;
1928
1929 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1930 return false;
Lan Tianyuad493e52013-01-23 04:26:30 +08001931 hub = usb_hub_to_struct_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001932 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001933}
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1936{
Lan Tianyuad493e52013-01-23 04:26:30 +08001937 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 int i;
1939
1940 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001941 if (hub->ports[i]->child)
1942 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001944 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001945 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 udev->state = USB_STATE_NOTATTACHED;
1947}
1948
1949/**
1950 * usb_set_device_state - change a device's current state (usbcore, hcds)
1951 * @udev: pointer to device whose state should be changed
1952 * @new_state: new state value to be stored
1953 *
1954 * udev->state is _not_ fully protected by the device lock. Although
1955 * most transitions are made only while holding the lock, the state can
1956 * can change to USB_STATE_NOTATTACHED at almost any time. This
1957 * is so that devices can be marked as disconnected as soon as possible,
1958 * without having to wait for any semaphores to be released. As a result,
1959 * all changes to any device's state must be protected by the
1960 * device_state_lock spinlock.
1961 *
1962 * Once a device has been added to the device tree, all changes to its state
1963 * should be made using this routine. The state should _not_ be set directly.
1964 *
1965 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1966 * Otherwise udev->state is set to new_state, and if new_state is
1967 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1968 * to USB_STATE_NOTATTACHED.
1969 */
1970void usb_set_device_state(struct usb_device *udev,
1971 enum usb_device_state new_state)
1972{
1973 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001974 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 spin_lock_irqsave(&device_state_lock, flags);
1977 if (udev->state == USB_STATE_NOTATTACHED)
1978 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001979 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001980
1981 /* root hub wakeup capabilities are managed out-of-band
1982 * and may involve silicon errata ... ignore them here.
1983 */
1984 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001985 if (udev->state == USB_STATE_SUSPENDED
1986 || new_state == USB_STATE_SUSPENDED)
1987 ; /* No change to wakeup settings */
1988 else if (new_state == USB_STATE_CONFIGURED)
Lu Baoluddbe1fc2014-09-19 10:13:50 +08001989 wakeup = (udev->quirks &
1990 USB_QUIRK_IGNORE_REMOTE_WAKEUP) ? 0 :
1991 udev->actconfig->desc.bmAttributes &
1992 USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001993 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001994 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001995 }
Sarah Sharp15123002007-12-21 16:54:15 -08001996 if (udev->state == USB_STATE_SUSPENDED &&
1997 new_state != USB_STATE_SUSPENDED)
1998 udev->active_duration -= jiffies;
1999 else if (new_state == USB_STATE_SUSPENDED &&
2000 udev->state != USB_STATE_SUSPENDED)
2001 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04002002 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07002003 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 recursively_mark_NOTATTACHED(udev);
2005 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01002006 if (wakeup >= 0)
2007 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
David Vrabel6da9c992009-02-18 14:43:47 +00002009EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002011/*
Alan Stern3b29b682011-02-22 09:53:41 -05002012 * Choose a device number.
2013 *
2014 * Device numbers are used as filenames in usbfs. On USB-1.1 and
2015 * USB-2.0 buses they are also used as device addresses, however on
2016 * USB-3.0 buses the address is assigned by the controller hardware
2017 * and it usually is not the same as the device number.
2018 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002019 * WUSB devices are simple: they have no hubs behind, so the mapping
2020 * device <-> virtual port number becomes 1:1. Why? to simplify the
2021 * life of the device connection logic in
2022 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
2023 * handshake we need to assign a temporary address in the unauthorized
2024 * space. For simplicity we use the first virtual port number found to
2025 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
2026 * and that becomes it's address [X < 128] or its unauthorized address
2027 * [X | 0x80].
2028 *
2029 * We add 1 as an offset to the one-based USB-stack port number
2030 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
2031 * 0 is reserved by USB for default address; (b) Linux's USB stack
2032 * uses always #1 for the root hub of the controller. So USB stack's
2033 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07002034 *
2035 * Devices connected under xHCI are not as simple. The host controller
2036 * supports virtualization, so the hardware assigns device addresses and
2037 * the HCD must setup data structures before issuing a set address
2038 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002039 */
Alan Stern3b29b682011-02-22 09:53:41 -05002040static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
2042 int devnum;
2043 struct usb_bus *bus = udev->bus;
2044
2045 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002046 if (udev->wusb) {
2047 devnum = udev->portnum + 1;
2048 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
2049 } else {
2050 /* Try to allocate the next devnum beginning at
2051 * bus->devnum_next. */
2052 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
2053 bus->devnum_next);
2054 if (devnum >= 128)
2055 devnum = find_next_zero_bit(bus->devmap.devicemap,
2056 128, 1);
Matthias Beyer469271f2013-10-10 23:41:27 +02002057 bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1);
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (devnum < 128) {
2060 set_bit(devnum, bus->devmap.devicemap);
2061 udev->devnum = devnum;
2062 }
2063}
2064
Alan Stern3b29b682011-02-22 09:53:41 -05002065static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 if (udev->devnum > 0) {
2068 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
2069 udev->devnum = -1;
2070 }
2071}
2072
Alan Stern3b29b682011-02-22 09:53:41 -05002073static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07002074{
2075 /* The address for a WUSB device is managed by wusbcore. */
2076 if (!udev->wusb)
2077 udev->devnum = devnum;
2078}
2079
Herbert Xuf7410ce2010-01-10 20:15:03 +11002080static void hub_free_dev(struct usb_device *udev)
2081{
2082 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2083
2084 /* Root hubs aren't real devices, so don't free HCD resources */
2085 if (hcd->driver->free_dev && udev->parent)
2086 hcd->driver->free_dev(hcd, udev);
2087}
2088
Dan Williams7027df32014-05-20 18:09:36 -07002089static void hub_disconnect_children(struct usb_device *udev)
2090{
2091 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
2092 int i;
2093
2094 /* Free up all the children before we remove this device */
2095 for (i = 0; i < udev->maxchild; i++) {
2096 if (hub->ports[i]->child)
2097 usb_disconnect(&hub->ports[i]->child);
2098 }
2099}
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101/**
2102 * usb_disconnect - disconnect a device (usbcore-internal)
2103 * @pdev: pointer to device being disconnected
2104 * Context: !in_interrupt ()
2105 *
2106 * Something got disconnected. Get rid of it and all of its children.
2107 *
2108 * If *pdev is a normal device then the parent hub must already be locked.
Bjorn Helgaasdb8f2aa2013-09-13 13:57:34 -06002109 * If *pdev is a root hub then the caller must hold the usb_bus_list_lock,
2110 * which protects the set of root hubs as well as the list of buses.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 *
2112 * Only hub drivers (including virtual root hub drivers for host
2113 * controllers) should ever call this.
2114 *
2115 * This call is synchronous, and may not be used in an interrupt context.
2116 */
2117void usb_disconnect(struct usb_device **pdev)
2118{
Dan Williams7027df32014-05-20 18:09:36 -07002119 struct usb_port *port_dev = NULL;
2120 struct usb_device *udev = *pdev;
Peter Chen5b1dc202014-08-19 08:56:21 +08002121 struct usb_hub *hub = NULL;
2122 int port1 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 /* mark the device as inactive, so any further urb submissions for
2125 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002126 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 */
2128 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05002129 dev_info(&udev->dev, "USB disconnect, device number %d\n",
2130 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002132 usb_lock_device(udev);
2133
Dan Williams7027df32014-05-20 18:09:36 -07002134 hub_disconnect_children(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 /* deallocate hcd/hardware state ... nuking all pending urbs and
2137 * cleaning up all state associated with the current configuration
2138 * so that the hardware is now fully quiesced.
2139 */
Alan Stern782da722006-07-01 22:09:35 -04002140 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04002142 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Lan Tianyufde26382013-01-20 01:53:33 +08002144 if (udev->parent) {
Dan Williams7027df32014-05-20 18:09:36 -07002145 port1 = udev->portnum;
2146 hub = usb_hub_to_struct_hub(udev->parent);
2147 port_dev = hub->ports[port1 - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002148
2149 sysfs_remove_link(&udev->dev.kobj, "port");
2150 sysfs_remove_link(&port_dev->dev.kobj, "device");
Lan Tianyu971fcd42013-01-23 04:26:29 +08002151
Dan Williams7027df32014-05-20 18:09:36 -07002152 /*
2153 * As usb_port_runtime_resume() de-references udev, make
2154 * sure no resumes occur during removal
2155 */
2156 if (!test_and_set_bit(port1, hub->child_usage_bits))
2157 pm_runtime_get_sync(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002158 }
2159
Alan Stern3b23dd62008-12-05 14:10:34 -05002160 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04002161 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07002162
Alan Stern782da722006-07-01 22:09:35 -04002163 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002164 * for de-configuring the device and invoking the remove-device
2165 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002166 */
2167 device_del(&udev->dev);
2168
2169 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 * (or root_hub) pointer.
2171 */
Alan Stern3b29b682011-02-22 09:53:41 -05002172 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 /* Avoid races with recursively_mark_NOTATTACHED() */
2175 spin_lock_irq(&device_state_lock);
2176 *pdev = NULL;
2177 spin_unlock_irq(&device_state_lock);
2178
Dan Williams7027df32014-05-20 18:09:36 -07002179 if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits))
2180 pm_runtime_put(&port_dev->dev);
2181
Herbert Xuf7410ce2010-01-10 20:15:03 +11002182 hub_free_dev(udev);
2183
Alan Stern782da722006-07-01 22:09:35 -04002184 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002187#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188static void show_string(struct usb_device *udev, char *id, char *string)
2189{
2190 if (!string)
2191 return;
Joe Perchesf2ec5222012-10-28 01:05:51 -07002192 dev_info(&udev->dev, "%s: %s\n", id, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002195static void announce_device(struct usb_device *udev)
2196{
2197 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2198 le16_to_cpu(udev->descriptor.idVendor),
2199 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002200 dev_info(&udev->dev,
2201 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002202 udev->descriptor.iManufacturer,
2203 udev->descriptor.iProduct,
2204 udev->descriptor.iSerialNumber);
2205 show_string(udev, "Product", udev->product);
2206 show_string(udev, "Manufacturer", udev->manufacturer);
2207 show_string(udev, "SerialNumber", udev->serial);
2208}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002210static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211#endif
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Oliver Neukum3ede7602007-01-11 14:35:50 +01002214/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002215 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002216 * @udev: newly addressed device (in ADDRESS state)
2217 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002218 * Finish enumeration for On-The-Go devices
Yacine Belkadi626f0902013-08-02 20:10:04 +02002219 *
2220 * Return: 0 if successful. A negative error code otherwise.
Oliver Neukum3ede7602007-01-11 14:35:50 +01002221 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002222static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002224 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226#ifdef CONFIG_USB_OTG
2227 /*
2228 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2229 * to wake us after we've powered off VBUS; and HNP, switching roles
2230 * "host" to "peripheral". The OTG descriptor helps figure this out.
2231 */
2232 if (!udev->bus->is_b_host
2233 && udev->config
2234 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002235 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 struct usb_bus *bus = udev->bus;
2237
2238 /* descriptor may appear anywhere in config */
2239 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2240 le16_to_cpu(udev->config[0].desc.wTotalLength),
2241 USB_DT_OTG, (void **) &desc) == 0) {
2242 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002243 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 dev_info(&udev->dev,
2246 "Dual-Role OTG device on %sHNP port\n",
2247 (port1 == bus->otg_port)
2248 ? "" : "non-");
2249
2250 /* enable HNP before suspend, it's simpler */
2251 if (port1 == bus->otg_port)
2252 bus->b_hnp_enable = 1;
2253 err = usb_control_msg(udev,
2254 usb_sndctrlpipe(udev, 0),
2255 USB_REQ_SET_FEATURE, 0,
2256 bus->b_hnp_enable
2257 ? USB_DEVICE_B_HNP_ENABLE
2258 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2259 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2260 if (err < 0) {
2261 /* OTG MESSAGE: report errors here,
2262 * customize to match your product.
2263 */
2264 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002265 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 err);
2267 bus->b_hnp_enable = 0;
2268 }
2269 }
2270 }
2271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002273 return err;
2274}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002276
2277/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002278 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002279 * @udev: newly addressed device (in ADDRESS state)
2280 *
2281 * This is only called by usb_new_device() and usb_authorize_device()
2282 * and FIXME -- all comments that apply to them apply here wrt to
2283 * environment.
2284 *
2285 * If the device is WUSB and not authorized, we don't attempt to read
2286 * the string descriptors, as they will be errored out by the device
2287 * until it has been authorized.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002288 *
2289 * Return: 0 if successful. A negative error code otherwise.
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002290 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002291static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002292{
2293 int err;
Peter Chen026f3fc2014-08-19 09:51:53 +08002294 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002295
2296 if (udev->config == NULL) {
2297 err = usb_get_configuration(udev);
2298 if (err < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04002299 if (err != -ENODEV)
2300 dev_err(&udev->dev, "can't read configurations, error %d\n",
2301 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002302 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002303 }
2304 }
Thomas Pugliese83e83ec2013-12-09 13:40:29 -06002305
2306 /* read the standard strings and cache them if present */
2307 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2308 udev->manufacturer = usb_cache_string(udev,
2309 udev->descriptor.iManufacturer);
2310 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2311
Alan Stern8d8558d2009-12-08 15:50:41 -05002312 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002313 if (err < 0)
2314 return err;
2315
Peter Chen026f3fc2014-08-19 09:51:53 +08002316 if (IS_ENABLED(CONFIG_USB_OTG_WHITELIST) && hcd->tpl_support &&
2317 !is_targeted(udev) && IS_ENABLED(CONFIG_USB_OTG)) {
2318 /* Maybe it can talk to us, though we can't talk to it.
2319 * (Includes HNP test device.)
2320 */
2321 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
2322 err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND);
2323 if (err < 0)
2324 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2325 }
2326 return -ENOTSUPP;
2327 }
2328
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002329 usb_detect_interface_quirks(udev);
2330
2331 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002332}
2333
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002334static void set_usb_port_removable(struct usb_device *udev)
2335{
2336 struct usb_device *hdev = udev->parent;
2337 struct usb_hub *hub;
2338 u8 port = udev->portnum;
2339 u16 wHubCharacteristics;
2340 bool removable = true;
2341
2342 if (!hdev)
2343 return;
2344
Lan Tianyuad493e52013-01-23 04:26:30 +08002345 hub = usb_hub_to_struct_hub(udev->parent);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002346
2347 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2348
2349 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2350 return;
2351
2352 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002353 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2354 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002355 removable = false;
2356 } else {
2357 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2358 removable = false;
2359 }
2360
2361 if (removable)
2362 udev->removable = USB_DEVICE_REMOVABLE;
2363 else
2364 udev->removable = USB_DEVICE_FIXED;
Dan Williamsa4204ff2014-05-20 18:08:22 -07002365
2366 /*
2367 * Platform firmware may have populated an alternative value for
2368 * removable. If the parent port has a known connect_type use
2369 * that instead.
2370 */
2371 switch (hub->ports[udev->portnum - 1]->connect_type) {
2372 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
2373 udev->removable = USB_DEVICE_REMOVABLE;
2374 break;
2375 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
2376 udev->removable = USB_DEVICE_FIXED;
2377 break;
2378 default: /* use what was set above */
2379 break;
2380 }
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002381}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002382
2383/**
2384 * usb_new_device - perform initial device setup (usbcore-internal)
2385 * @udev: newly addressed device (in ADDRESS state)
2386 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002387 * This is called with devices which have been detected but not fully
2388 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002389 * for any device configuration. The caller must have locked either
2390 * the parent hub (if udev is a normal device) or else the
2391 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2392 * udev has already been installed, but udev is not yet visible through
2393 * sysfs or other filesystem code.
2394 *
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002395 * This call is synchronous, and may not be used in an interrupt context.
2396 *
2397 * Only the hub driver or root-hub registrar should ever call this.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002398 *
2399 * Return: Whether the device is configured properly or not. Zero if the
2400 * interface was registered with the driver core; else a negative errno
2401 * value.
2402 *
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002403 */
2404int usb_new_device(struct usb_device *udev)
2405{
2406 int err;
2407
Dan Streetman16985402010-01-06 09:56:53 -05002408 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002409 /* Initialize non-root-hub device wakeup to disabled;
2410 * device (un)configuration controls wakeup capable
2411 * sysfs power/wakeup controls wakeup enabled/disabled
2412 */
2413 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002414 }
2415
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002416 /* Tell the runtime-PM framework the device is active */
2417 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002418 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002419 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002420 pm_runtime_enable(&udev->dev);
2421
Alan Sternc08512c2010-11-15 15:57:58 -05002422 /* By default, forbid autosuspend for all devices. It will be
2423 * allowed for hubs during binding.
2424 */
2425 usb_disable_autosuspend(udev);
2426
Alan Stern8d8558d2009-12-08 15:50:41 -05002427 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002428 if (err < 0)
2429 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002430 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2431 udev->devnum, udev->bus->busnum,
2432 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002433 /* export the usbdev device-node for libusb */
2434 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2435 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2436
Alan Stern6cd13202008-11-13 15:08:30 -05002437 /* Tell the world! */
2438 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002439
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002440 if (udev->serial)
2441 add_device_randomness(udev->serial, strlen(udev->serial));
2442 if (udev->product)
2443 add_device_randomness(udev->product, strlen(udev->product));
2444 if (udev->manufacturer)
2445 add_device_randomness(udev->manufacturer,
2446 strlen(udev->manufacturer));
2447
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002448 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002449
Dan Williamsa4204ff2014-05-20 18:08:22 -07002450 /* check whether the hub or firmware marks this port as non-removable */
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002451 if (udev->parent)
2452 set_usb_port_removable(udev);
2453
Alan Stern782da722006-07-01 22:09:35 -04002454 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002455 * for configuring the device and invoking the add-device
2456 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002457 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002458 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 if (err) {
2460 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2461 goto fail;
2462 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002463
Lan Tianyufde26382013-01-20 01:53:33 +08002464 /* Create link files between child device and usb port device. */
2465 if (udev->parent) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002466 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
Dan Williamsd5c38342014-05-20 18:08:52 -07002467 int port1 = udev->portnum;
2468 struct usb_port *port_dev = hub->ports[port1 - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002469
2470 err = sysfs_create_link(&udev->dev.kobj,
2471 &port_dev->dev.kobj, "port");
2472 if (err)
2473 goto fail;
2474
2475 err = sysfs_create_link(&port_dev->dev.kobj,
2476 &udev->dev.kobj, "device");
2477 if (err) {
2478 sysfs_remove_link(&udev->dev.kobj, "port");
2479 goto fail;
2480 }
Lan Tianyu971fcd42013-01-23 04:26:29 +08002481
Dan Williamsd5c38342014-05-20 18:08:52 -07002482 if (!test_and_set_bit(port1, hub->child_usage_bits))
2483 pm_runtime_get_sync(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002484 }
2485
Alan Stern3b23dd62008-12-05 14:10:34 -05002486 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002487 usb_mark_last_busy(udev);
2488 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002489 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491fail:
2492 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002493 pm_runtime_disable(&udev->dev);
2494 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002495 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496}
2497
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002498
2499/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002500 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2501 * @usb_dev: USB device
2502 *
2503 * Move the USB device to a very basic state where interfaces are disabled
2504 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002505 *
2506 * We share a lock (that we have) with device_del(), so we need to
2507 * defer its call.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002508 *
2509 * Return: 0.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002510 */
2511int usb_deauthorize_device(struct usb_device *usb_dev)
2512{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002513 usb_lock_device(usb_dev);
2514 if (usb_dev->authorized == 0)
2515 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002516
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002517 usb_dev->authorized = 0;
2518 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002519
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002520out_unauthorized:
2521 usb_unlock_device(usb_dev);
2522 return 0;
2523}
2524
2525
2526int usb_authorize_device(struct usb_device *usb_dev)
2527{
2528 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002529
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002530 usb_lock_device(usb_dev);
2531 if (usb_dev->authorized == 1)
2532 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002533
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002534 result = usb_autoresume_device(usb_dev);
2535 if (result < 0) {
2536 dev_err(&usb_dev->dev,
2537 "can't autoresume for authorization: %d\n", result);
2538 goto error_autoresume;
2539 }
2540 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2541 if (result < 0) {
2542 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2543 "authorization: %d\n", result);
2544 goto error_device_descriptor;
2545 }
Alan Sternda307122009-12-08 15:54:44 -05002546
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002547 usb_dev->authorized = 1;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002548 /* Choose and set the configuration. This registers the interfaces
2549 * with the driver core and lets interface drivers bind to them.
2550 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002551 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002552 if (c >= 0) {
2553 result = usb_set_configuration(usb_dev, c);
2554 if (result) {
2555 dev_err(&usb_dev->dev,
2556 "can't set config #%d, error %d\n", c, result);
2557 /* This need not be fatal. The user can try to
2558 * set other configurations. */
2559 }
2560 }
2561 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002562
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002563error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002564 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002565error_autoresume:
2566out_authorized:
Matthias Beyer781b2132013-10-10 23:41:29 +02002567 usb_unlock_device(usb_dev); /* complements locktree */
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002568 return result;
2569}
2570
2571
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002572/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2573static unsigned hub_is_wusb(struct usb_hub *hub)
2574{
2575 struct usb_hcd *hcd;
2576 if (hub->hdev->parent != NULL) /* not a root hub? */
2577 return 0;
2578 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2579 return hcd->wireless;
2580}
2581
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583#define PORT_RESET_TRIES 5
2584#define SET_ADDRESS_TRIES 2
2585#define GET_DESCRIPTOR_TRIES 2
2586#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302587#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2590#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002591#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592#define HUB_LONG_RESET_TIME 200
Sarah Sharp77c7f072012-11-14 17:16:52 -08002593#define HUB_RESET_TIMEOUT 800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Dan Williams48fc7db2013-12-05 17:07:27 -08002595/*
2596 * "New scheme" enumeration causes an extra state transition to be
2597 * exposed to an xhci host and causes USB3 devices to receive control
2598 * commands in the default state. This has been seen to cause
2599 * enumeration failures, so disable this enumeration scheme for USB3
2600 * devices.
2601 */
2602static bool use_new_scheme(struct usb_device *udev, int retry)
2603{
2604 if (udev->speed == USB_SPEED_SUPER)
2605 return false;
2606
2607 return USE_NEW_SCHEME(retry);
2608}
2609
Sarah Sharp10d674a2011-09-14 14:24:52 -07002610static int hub_port_reset(struct usb_hub *hub, int port1,
2611 struct usb_device *udev, unsigned int delay, bool warm);
2612
Rahul Bedarkar025d4432014-01-04 11:24:41 +05302613/* Is a USB 3.0 port in the Inactive or Compliance Mode state?
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002614 * Port worm reset is required to recover
2615 */
Dan Williams3cd12f92014-05-29 12:58:46 -07002616static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
2617 u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002618{
Dan Williams3cd12f92014-05-29 12:58:46 -07002619 u16 link_state;
2620
2621 if (!hub_is_superspeed(hub->hdev))
2622 return false;
2623
2624 if (test_bit(port1, hub->warm_reset_bits))
2625 return true;
2626
2627 link_state = portstatus & USB_PORT_STAT_LINK_STATE;
2628 return link_state == USB_SS_PORT_LS_SS_INACTIVE
2629 || link_state == USB_SS_PORT_LS_COMP_MOD;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002630}
2631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002633 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
2635 int delay_time, ret;
2636 u16 portstatus;
2637 u16 portchange;
2638
2639 for (delay_time = 0;
2640 delay_time < HUB_RESET_TIMEOUT;
2641 delay_time += delay) {
2642 /* wait to give the device a chance to reset */
2643 msleep(delay);
2644
2645 /* read and decode port status */
2646 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2647 if (ret < 0)
2648 return ret;
2649
Sarah Sharp4f434472012-11-15 14:58:04 -08002650 /* The port state is unknown until the reset completes. */
Sarah Sharp470f0be2012-12-11 10:14:03 -08002651 if (!(portstatus & USB_PORT_STAT_RESET))
2652 break;
Sarah Sharp4f434472012-11-15 14:58:04 -08002653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 /* switch to the long delay after two short delay failures */
2655 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2656 delay = HUB_LONG_RESET_TIME;
2657
Dan Williamsd99f6b42014-05-20 18:08:17 -07002658 dev_dbg(&hub->ports[port1 - 1]->dev,
2659 "not %sreset yet, waiting %dms\n",
2660 warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
2662
Sarah Sharp470f0be2012-12-11 10:14:03 -08002663 if ((portstatus & USB_PORT_STAT_RESET))
2664 return -EBUSY;
2665
Dan Williams3cd12f92014-05-29 12:58:46 -07002666 if (hub_port_warm_reset_required(hub, port1, portstatus))
Sarah Sharp470f0be2012-12-11 10:14:03 -08002667 return -ENOTCONN;
2668
2669 /* Device went away? */
2670 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2671 return -ENOTCONN;
2672
2673 /* bomb out completely if the connection bounced. A USB 3.0
2674 * connection may bounce if multiple warm resets were issued,
2675 * but the device may have successfully re-connected. Ignore it.
2676 */
2677 if (!hub_is_superspeed(hub->hdev) &&
2678 (portchange & USB_PORT_STAT_C_CONNECTION))
2679 return -ENOTCONN;
2680
2681 if (!(portstatus & USB_PORT_STAT_ENABLE))
2682 return -EBUSY;
2683
2684 if (!udev)
2685 return 0;
2686
2687 if (hub_is_wusb(hub))
2688 udev->speed = USB_SPEED_WIRELESS;
2689 else if (hub_is_superspeed(hub->hdev))
2690 udev->speed = USB_SPEED_SUPER;
2691 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2692 udev->speed = USB_SPEED_HIGH;
2693 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2694 udev->speed = USB_SPEED_LOW;
2695 else
2696 udev->speed = USB_SPEED_FULL;
2697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
Andiry Xu75d7cf72011-09-13 16:41:11 -07002700static void hub_port_finish_reset(struct usb_hub *hub, int port1,
Sarah Sharpa24a6072012-11-01 11:20:44 -07002701 struct usb_device *udev, int *status)
Andiry Xu75d7cf72011-09-13 16:41:11 -07002702{
2703 switch (*status) {
2704 case 0:
Sarah Sharpa24a6072012-11-01 11:20:44 -07002705 /* TRSTRCY = 10 ms; plus some extra */
2706 msleep(10 + 40);
2707 if (udev) {
2708 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2709
2710 update_devnum(udev, 0);
2711 /* The xHC may think the device is already reset,
2712 * so ignore the status.
2713 */
2714 if (hcd->driver->reset_device)
2715 hcd->driver->reset_device(hcd, udev);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002716 }
2717 /* FALL THROUGH */
2718 case -ENOTCONN:
2719 case -ENODEV:
Lan Tianyuad493e52013-01-23 04:26:30 +08002720 usb_clear_port_feature(hub->hdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002721 port1, USB_PORT_FEAT_C_RESET);
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002722 if (hub_is_superspeed(hub->hdev)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002723 usb_clear_port_feature(hub->hdev, port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002724 USB_PORT_FEAT_C_BH_PORT_RESET);
Lan Tianyuad493e52013-01-23 04:26:30 +08002725 usb_clear_port_feature(hub->hdev, port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002726 USB_PORT_FEAT_C_PORT_LINK_STATE);
Lan Tianyuad493e52013-01-23 04:26:30 +08002727 usb_clear_port_feature(hub->hdev, port1,
Sarah Sharpa24a6072012-11-01 11:20:44 -07002728 USB_PORT_FEAT_C_CONNECTION);
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002729 }
Sarah Sharpa24a6072012-11-01 11:20:44 -07002730 if (udev)
Andiry Xu75d7cf72011-09-13 16:41:11 -07002731 usb_set_device_state(udev, *status
2732 ? USB_STATE_NOTATTACHED
2733 : USB_STATE_DEFAULT);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002734 break;
2735 }
2736}
2737
2738/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002740 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741{
2742 int i, status;
Sarah Sharpa24a6072012-11-01 11:20:44 -07002743 u16 portchange, portstatus;
Dan Williamsd99f6b42014-05-20 18:08:17 -07002744 struct usb_port *port_dev = hub->ports[port1 - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002746 if (!hub_is_superspeed(hub->hdev)) {
2747 if (warm) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002748 dev_err(hub->intfdev, "only USB3 hub support "
2749 "warm reset\n");
2750 return -EINVAL;
2751 }
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002752 /* Block EHCI CF initialization during the port reset.
2753 * Some companion controllers don't like it when they mix.
2754 */
2755 down_read(&ehci_cf_port_reset_rwsem);
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07002756 } else if (!warm) {
2757 /*
2758 * If the caller hasn't explicitly requested a warm reset,
2759 * double check and see if one is needed.
2760 */
2761 status = hub_port_status(hub, port1,
2762 &portstatus, &portchange);
2763 if (status < 0)
2764 goto done;
2765
Dan Williams3cd12f92014-05-29 12:58:46 -07002766 if (hub_port_warm_reset_required(hub, port1, portstatus))
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07002767 warm = true;
Andiry Xu75d7cf72011-09-13 16:41:11 -07002768 }
Dan Williams3cd12f92014-05-29 12:58:46 -07002769 clear_bit(port1, hub->warm_reset_bits);
Alan Stern32fe0192007-10-10 16:27:07 -04002770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 /* Reset the port */
2772 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002773 status = set_port_feature(hub->hdev, port1, (warm ?
2774 USB_PORT_FEAT_BH_PORT_RESET :
2775 USB_PORT_FEAT_RESET));
Alan Sterne9e88fb2013-03-27 16:14:01 -04002776 if (status == -ENODEV) {
2777 ; /* The hub is gone */
2778 } else if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002779 dev_err(&port_dev->dev,
2780 "cannot %sreset (err = %d)\n",
2781 warm ? "warm " : "", status);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002782 } else {
2783 status = hub_port_wait_reset(hub, port1, udev, delay,
2784 warm);
Alan Sterne9e88fb2013-03-27 16:14:01 -04002785 if (status && status != -ENOTCONN && status != -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 dev_dbg(hub->intfdev,
2787 "port_wait_reset: err = %d\n",
2788 status);
2789 }
2790
Sarah Sharpa24a6072012-11-01 11:20:44 -07002791 /* Check for disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002792 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
Sarah Sharpa24a6072012-11-01 11:20:44 -07002793 hub_port_finish_reset(hub, port1, udev, &status);
2794
2795 if (!hub_is_superspeed(hub->hdev))
2796 goto done;
2797
2798 /*
2799 * If a USB 3.0 device migrates from reset to an error
2800 * state, re-issue the warm reset.
2801 */
2802 if (hub_port_status(hub, port1,
2803 &portstatus, &portchange) < 0)
2804 goto done;
2805
Dan Williams3cd12f92014-05-29 12:58:46 -07002806 if (!hub_port_warm_reset_required(hub, port1,
2807 portstatus))
Sarah Sharpa24a6072012-11-01 11:20:44 -07002808 goto done;
2809
2810 /*
2811 * If the port is in SS.Inactive or Compliance Mode, the
2812 * hot or warm reset failed. Try another warm reset.
2813 */
2814 if (!warm) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002815 dev_dbg(&port_dev->dev,
2816 "hot reset failed, warm reset\n");
Sarah Sharpa24a6072012-11-01 11:20:44 -07002817 warm = true;
2818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 }
2820
Dan Williamsd99f6b42014-05-20 18:08:17 -07002821 dev_dbg(&port_dev->dev,
2822 "not enabled, trying %sreset again...\n",
2823 warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 delay = HUB_LONG_RESET_TIME;
2825 }
2826
Dan Williamsd99f6b42014-05-20 18:08:17 -07002827 dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Andiry Xu75d7cf72011-09-13 16:41:11 -07002829done:
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002830 if (!hub_is_superspeed(hub->hdev))
Andiry Xu75d7cf72011-09-13 16:41:11 -07002831 up_read(&ehci_cf_port_reset_rwsem);
2832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return status;
2834}
2835
Andiry Xu0ed9a572011-04-27 18:07:43 +08002836/* Check if a port is power on */
2837static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2838{
2839 int ret = 0;
2840
2841 if (hub_is_superspeed(hub->hdev)) {
2842 if (portstatus & USB_SS_PORT_STAT_POWER)
2843 ret = 1;
2844 } else {
2845 if (portstatus & USB_PORT_STAT_POWER)
2846 ret = 1;
2847 }
2848
2849 return ret;
2850}
2851
Dan Williams5c79a1e2014-05-20 18:09:26 -07002852static void usb_lock_port(struct usb_port *port_dev)
2853 __acquires(&port_dev->status_lock)
2854{
2855 mutex_lock(&port_dev->status_lock);
2856 __acquire(&port_dev->status_lock);
2857}
2858
2859static void usb_unlock_port(struct usb_port *port_dev)
2860 __releases(&port_dev->status_lock)
2861{
2862 mutex_unlock(&port_dev->status_lock);
2863 __release(&port_dev->status_lock);
2864}
2865
Alan Sternd388dab2006-07-01 22:14:24 -04002866#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Andiry Xu0ed9a572011-04-27 18:07:43 +08002868/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2869static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2870{
2871 int ret = 0;
2872
2873 if (hub_is_superspeed(hub->hdev)) {
2874 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2875 == USB_SS_PORT_LS_U3)
2876 ret = 1;
2877 } else {
2878 if (portstatus & USB_PORT_STAT_SUSPEND)
2879 ret = 1;
2880 }
2881
2882 return ret;
2883}
Alan Sternb01b03f2008-04-28 11:06:11 -04002884
2885/* Determine whether the device on a port is ready for a normal resume,
2886 * is ready for a reset-resume, or should be disconnected.
2887 */
2888static int check_port_resume_type(struct usb_device *udev,
2889 struct usb_hub *hub, int port1,
2890 int status, unsigned portchange, unsigned portstatus)
2891{
Dan Williamsd99f6b42014-05-20 18:08:17 -07002892 struct usb_port *port_dev = hub->ports[port1 - 1];
2893
Dan Williams3cd12f92014-05-29 12:58:46 -07002894 /* Is a warm reset needed to recover the connection? */
2895 if (status == 0 && udev->reset_resume
2896 && hub_port_warm_reset_required(hub, port1, portstatus)) {
2897 /* pass */;
2898 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002899 /* Is the device still present? */
Dan Williams3cd12f92014-05-29 12:58:46 -07002900 else if (status || port_is_suspended(hub, portstatus) ||
Andiry Xu0ed9a572011-04-27 18:07:43 +08002901 !port_is_power_on(hub, portstatus) ||
2902 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002903 if (status >= 0)
2904 status = -ENODEV;
2905 }
2906
Alan Stern86c57ed2008-06-30 11:14:43 -04002907 /* Can't do a normal resume if the port isn't enabled,
2908 * so try a reset-resume instead.
2909 */
2910 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2911 if (udev->persist_enabled)
2912 udev->reset_resume = 1;
2913 else
2914 status = -ENODEV;
2915 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002916
2917 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002918 dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n",
2919 portchange, portstatus, status);
Alan Sternb01b03f2008-04-28 11:06:11 -04002920 } else if (udev->reset_resume) {
2921
2922 /* Late port handoff can set status-change bits */
2923 if (portchange & USB_PORT_STAT_C_CONNECTION)
Lan Tianyuad493e52013-01-23 04:26:30 +08002924 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04002925 USB_PORT_FEAT_C_CONNECTION);
2926 if (portchange & USB_PORT_STAT_C_ENABLE)
Lan Tianyuad493e52013-01-23 04:26:30 +08002927 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04002928 USB_PORT_FEAT_C_ENABLE);
2929 }
2930
2931 return status;
2932}
2933
Sarah Sharpf74631e2012-06-25 12:08:08 -07002934int usb_disable_ltm(struct usb_device *udev)
2935{
2936 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2937
2938 /* Check if the roothub and device supports LTM. */
2939 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2940 !usb_device_supports_ltm(udev))
2941 return 0;
2942
2943 /* Clear Feature LTM Enable can only be sent if the device is
2944 * configured.
2945 */
2946 if (!udev->actconfig)
2947 return 0;
2948
2949 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2950 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2951 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2952 USB_CTRL_SET_TIMEOUT);
2953}
2954EXPORT_SYMBOL_GPL(usb_disable_ltm);
2955
2956void usb_enable_ltm(struct usb_device *udev)
2957{
2958 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2959
2960 /* Check if the roothub and device supports LTM. */
2961 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2962 !usb_device_supports_ltm(udev))
2963 return;
2964
2965 /* Set Feature LTM Enable can only be sent if the device is
2966 * configured.
2967 */
2968 if (!udev->actconfig)
2969 return;
2970
2971 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2972 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2973 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2974 USB_CTRL_SET_TIMEOUT);
2975}
2976EXPORT_SYMBOL_GPL(usb_enable_ltm);
2977
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002978/*
Alan Stern28e861652013-07-30 15:37:33 -04002979 * usb_enable_remote_wakeup - enable remote wakeup for a device
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002980 * @udev: target device
2981 *
Alan Stern28e861652013-07-30 15:37:33 -04002982 * For USB-2 devices: Set the device's remote wakeup feature.
2983 *
2984 * For USB-3 devices: Assume there's only one function on the device and
2985 * enable remote wake for the first interface. FIXME if the interface
2986 * association descriptor shows there's more than one function.
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002987 */
Alan Stern28e861652013-07-30 15:37:33 -04002988static int usb_enable_remote_wakeup(struct usb_device *udev)
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002989{
Alan Stern28e861652013-07-30 15:37:33 -04002990 if (udev->speed < USB_SPEED_SUPER)
2991 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2992 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2993 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
2994 USB_CTRL_SET_TIMEOUT);
2995 else
2996 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2997 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
2998 USB_INTRF_FUNC_SUSPEND,
2999 USB_INTRF_FUNC_SUSPEND_RW |
3000 USB_INTRF_FUNC_SUSPEND_LP,
3001 NULL, 0, USB_CTRL_SET_TIMEOUT);
3002}
3003
3004/*
3005 * usb_disable_remote_wakeup - disable remote wakeup for a device
3006 * @udev: target device
3007 *
3008 * For USB-2 devices: Clear the device's remote wakeup feature.
3009 *
3010 * For USB-3 devices: Assume there's only one function on the device and
3011 * disable remote wake for the first interface. FIXME if the interface
3012 * association descriptor shows there's more than one function.
3013 */
3014static int usb_disable_remote_wakeup(struct usb_device *udev)
3015{
3016 if (udev->speed < USB_SPEED_SUPER)
3017 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3018 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
3019 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
3020 USB_CTRL_SET_TIMEOUT);
3021 else
3022 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003023 USB_REQ_CLEAR_FEATURE, USB_RECIP_INTERFACE,
3024 USB_INTRF_FUNC_SUSPEND, 0, NULL, 0,
3025 USB_CTRL_SET_TIMEOUT);
3026}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Alan Sterne583d9d2013-07-11 14:58:04 -04003028/* Count of wakeup-enabled devices at or below udev */
3029static unsigned wakeup_enabled_descendants(struct usb_device *udev)
3030{
3031 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
3032
3033 return udev->do_remote_wakeup +
3034 (hub ? hub->wakeup_enabled_descendants : 0);
3035}
3036
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037/*
Alan Stern140d8f62006-07-01 22:07:21 -04003038 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04003039 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07003040 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 *
3042 * Suspends a USB device that isn't in active use, conserving power.
3043 * Devices may wake out of a suspend, if anything important happens,
3044 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04003045 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 * to disconnect devices while they are suspended.
3047 *
David Brownell390a8c32005-09-13 19:57:27 -07003048 * This only affects the USB hardware for a device; its interfaces
3049 * (and, for hubs, child devices) must already have been suspended.
3050 *
Alan Stern624d6c02007-05-30 15:35:16 -04003051 * Selective port suspend reduces power; most suspended devices draw
3052 * less than 500 uA. It's also used in OTG, along with remote wakeup.
3053 * All devices below the suspended port are also suspended.
3054 *
3055 * Devices leave suspend state when the host wakes them up. Some devices
3056 * also support "remote wakeup", where the device can activate the USB
3057 * tree above them to deliver data, such as a keypress or packet. In
3058 * some cases, this wakes the USB host.
3059 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 * Suspending OTG devices may trigger HNP, if that's been enabled
3061 * between a pair of dual-role devices. That will change roles, such
3062 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
3063 *
Alan Stern4956ecc2007-05-30 16:51:28 -04003064 * Devices on USB hub ports have only one "suspend" state, corresponding
3065 * to ACPI D2, "may cause the device to lose some context".
3066 * State transitions include:
3067 *
3068 * - suspend, resume ... when the VBUS power link stays live
3069 * - suspend, disconnect ... VBUS lost
3070 *
3071 * Once VBUS drop breaks the circuit, the port it's using has to go through
3072 * normal re-enumeration procedures, starting with enabling VBUS power.
3073 * Other than re-initializing the hub (plug/unplug, except for root hubs),
3074 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
3075 * timer, no SRP, no requests through sysfs.
3076 *
Alan Sterne583d9d2013-07-11 14:58:04 -04003077 * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
3078 * suspended until their bus goes into global suspend (i.e., the root
Alan Stern0aa28322013-03-27 16:14:19 -04003079 * hub is suspended). Nevertheless, we change @udev->state to
3080 * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual
3081 * upstream port setting is stored in @udev->port_is_suspended.
Alan Stern4956ecc2007-05-30 16:51:28 -04003082 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 * Returns 0 on success, else negative errno.
3084 */
Alan Stern65bfd292008-11-25 16:39:18 -05003085int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086{
Lan Tianyuad493e52013-01-23 04:26:30 +08003087 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3088 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003089 int port1 = udev->portnum;
3090 int status;
Alan Stern0aa28322013-03-27 16:14:19 -04003091 bool really_suspend = true;
Alan Stern4956ecc2007-05-30 16:51:28 -04003092
Dan Williams5c79a1e2014-05-20 18:09:26 -07003093 usb_lock_port(port_dev);
3094
Alan Stern624d6c02007-05-30 15:35:16 -04003095 /* enable remote wakeup when appropriate; this lets the device
3096 * wake up the upstream hub (including maybe the root hub).
3097 *
3098 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
3099 * we don't explicitly enable it here.
3100 */
3101 if (udev->do_remote_wakeup) {
Alan Stern28e861652013-07-30 15:37:33 -04003102 status = usb_enable_remote_wakeup(udev);
Oliver Neukum0c487202009-10-19 13:19:41 +02003103 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003104 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
3105 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02003106 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02003107 if (PMSG_IS_AUTO(msg))
Alan Stern4fae6f02013-07-30 15:39:02 -04003108 goto err_wakeup;
Oliver Neukum0c487202009-10-19 13:19:41 +02003109 }
Alan Stern624d6c02007-05-30 15:35:16 -04003110 }
3111
Andiry Xu65580b432011-09-23 14:19:52 -07003112 /* disable USB2 hardware LPM */
3113 if (udev->usb2_hw_lpm_enabled == 1)
3114 usb_set_usb2_hardware_lpm(udev, 0);
3115
Sarah Sharpf74631e2012-06-25 12:08:08 -07003116 if (usb_disable_ltm(udev)) {
Alan Stern4fae6f02013-07-30 15:39:02 -04003117 dev_err(&udev->dev, "Failed to disable LTM before suspend\n.");
3118 status = -ENOMEM;
3119 if (PMSG_IS_AUTO(msg))
3120 goto err_ltm;
Sarah Sharpf74631e2012-06-25 12:08:08 -07003121 }
Sarah Sharp83060952012-05-02 14:25:52 -07003122 if (usb_unlocked_disable_lpm(udev)) {
Alan Stern4fae6f02013-07-30 15:39:02 -04003123 dev_err(&udev->dev, "Failed to disable LPM before suspend\n.");
3124 status = -ENOMEM;
3125 if (PMSG_IS_AUTO(msg))
3126 goto err_lpm3;
Sarah Sharp83060952012-05-02 14:25:52 -07003127 }
3128
Alan Stern624d6c02007-05-30 15:35:16 -04003129 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08003130 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003131 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
Alan Sterne583d9d2013-07-11 14:58:04 -04003132
Alan Stern0aa28322013-03-27 16:14:19 -04003133 /*
3134 * For system suspend, we do not need to enable the suspend feature
3135 * on individual USB-2 ports. The devices will automatically go
3136 * into suspend a few ms after the root hub stops sending packets.
3137 * The USB 2.0 spec calls this "global suspend".
Alan Sterne583d9d2013-07-11 14:58:04 -04003138 *
3139 * However, many USB hubs have a bug: They don't relay wakeup requests
3140 * from a downstream port if the port's suspend feature isn't on.
3141 * Therefore we will turn on the suspend feature if udev or any of its
3142 * descendants is enabled for remote wakeup.
Alan Stern0aa28322013-03-27 16:14:19 -04003143 */
Alan Sterne583d9d2013-07-11 14:58:04 -04003144 else if (PMSG_IS_AUTO(msg) || wakeup_enabled_descendants(udev) > 0)
3145 status = set_port_feature(hub->hdev, port1,
3146 USB_PORT_FEAT_SUSPEND);
Alan Stern0aa28322013-03-27 16:14:19 -04003147 else {
3148 really_suspend = false;
3149 status = 0;
3150 }
Alan Stern624d6c02007-05-30 15:35:16 -04003151 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003152 dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status);
Alan Sterncbb33002011-06-15 16:29:16 -04003153
Alan Stern4fae6f02013-07-30 15:39:02 -04003154 /* Try to enable USB3 LPM and LTM again */
3155 usb_unlocked_enable_lpm(udev);
3156 err_lpm3:
3157 usb_enable_ltm(udev);
3158 err_ltm:
Andiry Xuc3e751e2012-05-05 00:50:10 +08003159 /* Try to enable USB2 hardware LPM again */
3160 if (udev->usb2_hw_lpm_capable == 1)
3161 usb_set_usb2_hardware_lpm(udev, 1);
3162
Alan Stern4fae6f02013-07-30 15:39:02 -04003163 if (udev->do_remote_wakeup)
3164 (void) usb_disable_remote_wakeup(udev);
3165 err_wakeup:
Sarah Sharp83060952012-05-02 14:25:52 -07003166
Alan Sterncbb33002011-06-15 16:29:16 -04003167 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02003168 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003169 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04003170 } else {
Alan Stern30b1a7a2011-09-27 21:54:22 +02003171 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
3172 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
3173 udev->do_remote_wakeup);
Alan Stern0aa28322013-03-27 16:14:19 -04003174 if (really_suspend) {
3175 udev->port_is_suspended = 1;
Alan Sterne583d9d2013-07-11 14:58:04 -04003176
3177 /* device has up to 10 msec to fully suspend */
Alan Stern0aa28322013-03-27 16:14:19 -04003178 msleep(10);
3179 }
Alan Sterne583d9d2013-07-11 14:58:04 -04003180 usb_set_device_state(udev, USB_STATE_SUSPENDED);
Alan Stern624d6c02007-05-30 15:35:16 -04003181 }
Lan Tianyuad493e52013-01-23 04:26:30 +08003182
Dan Williamsd5c38342014-05-20 18:08:52 -07003183 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled
3184 && test_and_clear_bit(port1, hub->child_usage_bits))
Lan Tianyu98a4f1f2013-07-03 22:17:54 +08003185 pm_runtime_put_sync(&port_dev->dev);
Lan Tianyuad493e52013-01-23 04:26:30 +08003186
Alan Sternc08512c2010-11-15 15:57:58 -05003187 usb_mark_last_busy(hub->hdev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07003188
3189 usb_unlock_port(port_dev);
Alan Stern4956ecc2007-05-30 16:51:28 -04003190 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191}
David Brownellf3f32532005-09-22 22:37:29 -07003192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193/*
David Brownell390a8c32005-09-13 19:57:27 -07003194 * If the USB "suspend" state is in use (rather than "global suspend"),
3195 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04003196 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 * hardware resume signaling is finished, either because of selective
3198 * resume (by host) or remote wakeup (by device) ... now see what changed
3199 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04003200 *
3201 * If @udev->reset_resume is set then the device is reset before the
3202 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 */
Alan Stern140d8f62006-07-01 22:07:21 -04003204static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205{
Alan Stern54515fe2007-05-30 15:38:58 -04003206 int status = 0;
Oliver Neukum07e72b92012-11-29 15:05:57 +01003207 u16 devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
3209 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003210 dev_dbg(&udev->dev, "%s\n",
3211 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
3213 /* usb ch9 identifies four variants of SUSPENDED, based on what
3214 * state the device resumes to. Linux currently won't see the
3215 * first two on the host side; they'd be inside hub_port_init()
3216 * during many timeouts, but khubd can't suspend until later.
3217 */
3218 usb_set_device_state(udev, udev->actconfig
3219 ? USB_STATE_CONFIGURED
3220 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Alan Stern54515fe2007-05-30 15:38:58 -04003222 /* 10.5.4.5 says not to reset a suspended port if the attached
3223 * device is enabled for remote wakeup. Hence the reset
3224 * operation is carried out here, after the port has been
3225 * resumed.
3226 */
Alan Stern1d102552014-03-18 10:39:05 -04003227 if (udev->reset_resume) {
3228 /*
3229 * If the device morphs or switches modes when it is reset,
3230 * we don't want to perform a reset-resume. We'll fail the
3231 * resume, which will cause a logical disconnect, and then
3232 * the device will be rediscovered.
3233 */
Alan Stern86c57ed2008-06-30 11:14:43 -04003234 retry_reset_resume:
Alan Stern1d102552014-03-18 10:39:05 -04003235 if (udev->quirks & USB_QUIRK_RESET)
3236 status = -ENODEV;
3237 else
3238 status = usb_reset_and_verify_device(udev);
3239 }
Alan Stern54515fe2007-05-30 15:38:58 -04003240
Matthias Beyer469271f2013-10-10 23:41:27 +02003241 /* 10.5.4.5 says be sure devices in the tree are still there.
3242 * For now let's assume the device didn't go crazy on resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 * and device drivers will know about any resume quirks.
3244 */
Alan Stern54515fe2007-05-30 15:38:58 -04003245 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04003246 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04003247 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
Alan Stern86c57ed2008-06-30 11:14:43 -04003248
3249 /* If a normal resume failed, try doing a reset-resume */
3250 if (status && !udev->reset_resume && udev->persist_enabled) {
3251 dev_dbg(&udev->dev, "retry with reset-resume\n");
3252 udev->reset_resume = 1;
3253 goto retry_reset_resume;
3254 }
Alan Stern54515fe2007-05-30 15:38:58 -04003255 }
Alan Sternb40b7a92006-06-19 15:12:38 -04003256
Alan Stern624d6c02007-05-30 15:35:16 -04003257 if (status) {
3258 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
3259 status);
Oliver Neukum07e72b92012-11-29 15:05:57 +01003260 /*
3261 * There are a few quirky devices which violate the standard
3262 * by claiming to have remote wakeup enabled after a reset,
3263 * which crash if the feature is cleared, hence check for
3264 * udev->reset_resume
3265 */
3266 } else if (udev->actconfig && !udev->reset_resume) {
Alan Stern28e861652013-07-30 15:37:33 -04003267 if (udev->speed < USB_SPEED_SUPER) {
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003268 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
Alan Stern28e861652013-07-30 15:37:33 -04003269 status = usb_disable_remote_wakeup(udev);
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003270 } else {
3271 status = usb_get_status(udev, USB_RECIP_INTERFACE, 0,
3272 &devstatus);
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003273 if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP
3274 | USB_INTRF_STAT_FUNC_RW))
Alan Stern28e861652013-07-30 15:37:33 -04003275 status = usb_disable_remote_wakeup(udev);
Alan Stern4bf0ba82005-11-21 11:58:07 -05003276 }
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003277
3278 if (status)
3279 dev_dbg(&udev->dev,
3280 "disable remote wakeup, status %d\n",
3281 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 }
3284 return status;
3285}
3286
Alan Stern624d6c02007-05-30 15:35:16 -04003287/*
Pratyush Ananda40178b2014-07-18 12:37:10 +05303288 * There are some SS USB devices which take longer time for link training.
3289 * XHCI specs 4.19.4 says that when Link training is successful, port
3290 * sets CSC bit to 1. So if SW reads port status before successful link
3291 * training, then it will not find device to be present.
3292 * USB Analyzer log with such buggy devices show that in some cases
3293 * device switch on the RX termination after long delay of host enabling
3294 * the VBUS. In few other cases it has been seen that device fails to
3295 * negotiate link training in first attempt. It has been
3296 * reported till now that few devices take as long as 2000 ms to train
3297 * the link after host enabling its VBUS and termination. Following
3298 * routine implements a 2000 ms timeout for link training. If in a case
3299 * link trains before timeout, loop will exit earlier.
3300 *
3301 * FIXME: If a device was connected before suspend, but was removed
3302 * while system was asleep, then the loop in the following routine will
3303 * only exit at timeout.
3304 *
3305 * This routine should only be called when persist is enabled for a SS
3306 * device.
3307 */
3308static int wait_for_ss_port_enable(struct usb_device *udev,
3309 struct usb_hub *hub, int *port1,
3310 u16 *portchange, u16 *portstatus)
3311{
3312 int status = 0, delay_ms = 0;
3313
3314 while (delay_ms < 2000) {
3315 if (status || *portstatus & USB_PORT_STAT_CONNECTION)
3316 break;
3317 msleep(20);
3318 delay_ms += 20;
3319 status = hub_port_status(hub, *port1, portstatus, portchange);
3320 }
3321 return status;
3322}
3323
3324/*
Alan Stern624d6c02007-05-30 15:35:16 -04003325 * usb_port_resume - re-activate a suspended usb device's upstream port
3326 * @udev: device to re-activate, not a root hub
3327 * Context: must be able to sleep; device not locked; pm locks held
3328 *
3329 * This will re-activate the suspended device, increasing power usage
3330 * while letting drivers communicate again with its endpoints.
3331 * USB resume explicitly guarantees that the power session between
3332 * the host and the device is the same as it was when the device
3333 * suspended.
3334 *
Alan Sternfeccc302008-03-03 15:15:59 -05003335 * If @udev->reset_resume is set then this routine won't check that the
3336 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04003337 * reset @udev. The end result is that a broken power session can be
3338 * recovered and @udev will appear to persist across a loss of VBUS power.
3339 *
3340 * For example, if a host controller doesn't maintain VBUS suspend current
3341 * during a system sleep or is reset when the system wakes up, all the USB
3342 * power sessions below it will be broken. This is especially troublesome
3343 * for mass-storage devices containing mounted filesystems, since the
3344 * device will appear to have disconnected and all the memory mappings
3345 * to it will be lost. Using the USB_PERSIST facility, the device can be
3346 * made to appear as if it had not disconnected.
3347 *
Ming Lei742120c2008-06-18 22:00:29 +08003348 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05003349 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04003350 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3351 * quite possible for a device to remain unaltered but its media to be
3352 * changed. If the user replaces a flash memory card while the system is
3353 * asleep, he will have only himself to blame when the filesystem on the
3354 * new card is corrupted and the system crashes.
3355 *
Alan Stern624d6c02007-05-30 15:35:16 -04003356 * Returns 0 on success, else negative errno.
3357 */
Alan Stern65bfd292008-11-25 16:39:18 -05003358int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
Lan Tianyuad493e52013-01-23 04:26:30 +08003360 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3361 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003362 int port1 = udev->portnum;
3363 int status;
3364 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003365
Dan Williamsd5c38342014-05-20 18:08:52 -07003366 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08003367 status = pm_runtime_get_sync(&port_dev->dev);
Lan Tianyuad493e52013-01-23 04:26:30 +08003368 if (status < 0) {
3369 dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
3370 status);
3371 return status;
3372 }
3373 }
3374
Dan Williams5c79a1e2014-05-20 18:09:26 -07003375 usb_lock_port(port_dev);
3376
Alan Sternd25450c2006-11-20 11:14:30 -05003377 /* Skip the initial Clear-Suspend step for a remote wakeup */
3378 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003379 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003380 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003383 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003384 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
Andiry Xua7114232011-04-27 18:07:50 +08003385 else
Lan Tianyuad493e52013-01-23 04:26:30 +08003386 status = usb_clear_port_feature(hub->hdev,
Andiry Xua7114232011-04-27 18:07:50 +08003387 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003389 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04003392 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003393 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 msleep(25);
3395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3397 * stop resume signaling. Then finish the resume
3398 * sequence.
3399 */
Alan Sternd25450c2006-11-20 11:14:30 -05003400 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003401
Alan Sternb01b03f2008-04-28 11:06:11 -04003402 /* TRSMRCY = 10 msec */
3403 msleep(10);
3404 }
Alan Stern54515fe2007-05-30 15:38:58 -04003405
Alan Sternb01b03f2008-04-28 11:06:11 -04003406 SuspendCleared:
3407 if (status == 0) {
Alan Sternbfd1e912012-10-19 11:03:39 -04003408 udev->port_is_suspended = 0;
Andiry Xua7114232011-04-27 18:07:50 +08003409 if (hub_is_superspeed(hub->hdev)) {
3410 if (portchange & USB_PORT_STAT_C_LINK_STATE)
Lan Tianyuad493e52013-01-23 04:26:30 +08003411 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003412 USB_PORT_FEAT_C_PORT_LINK_STATE);
3413 } else {
3414 if (portchange & USB_PORT_STAT_C_SUSPEND)
Lan Tianyuad493e52013-01-23 04:26:30 +08003415 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003416 USB_PORT_FEAT_C_SUSPEND);
3417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Pratyush Ananda40178b2014-07-18 12:37:10 +05303420 if (udev->persist_enabled && hub_is_superspeed(hub->hdev))
3421 status = wait_for_ss_port_enable(udev, hub, &port1, &portchange,
3422 &portstatus);
3423
Alan Sternb01b03f2008-04-28 11:06:11 -04003424 status = check_port_resume_type(udev,
3425 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003426 if (status == 0)
3427 status = finish_port_resume(udev);
3428 if (status < 0) {
3429 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3430 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003431 } else {
3432 /* Try to enable USB2 hardware LPM */
3433 if (udev->usb2_hw_lpm_capable == 1)
3434 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003435
Sarah Sharpf74631e2012-06-25 12:08:08 -07003436 /* Try to enable USB3 LTM and LPM */
3437 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003438 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003439 }
Andiry Xu65580b432011-09-23 14:19:52 -07003440
Dan Williams5c79a1e2014-05-20 18:09:26 -07003441 usb_unlock_port(port_dev);
3442
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 return status;
3444}
3445
Alan Stern84ebc102013-03-27 16:14:46 -04003446#ifdef CONFIG_PM_RUNTIME
3447
Alan Stern0534d462010-01-08 12:56:30 -05003448int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449{
3450 int status = 0;
3451
Dan Williams5c79a1e2014-05-20 18:09:26 -07003452 usb_lock_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003454 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003455 status = usb_autoresume_device(udev);
3456 if (status == 0) {
3457 /* Let the drivers do their thing, then... */
3458 usb_autosuspend_device(udev);
3459 }
Alan Sternd25450c2006-11-20 11:14:30 -05003460 }
Dan Williams5c79a1e2014-05-20 18:09:26 -07003461 usb_unlock_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 return status;
3463}
3464
Dan Williams7e73be22014-05-20 18:09:31 -07003465/* Returns 1 if there was a remote wakeup and a connect status change. */
3466static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
3467 u16 portstatus, u16 portchange)
3468 __must_hold(&port_dev->status_lock)
3469{
3470 struct usb_port *port_dev = hub->ports[port - 1];
3471 struct usb_device *hdev;
3472 struct usb_device *udev;
3473 int connect_change = 0;
3474 int ret;
3475
3476 hdev = hub->hdev;
3477 udev = port_dev->child;
3478 if (!hub_is_superspeed(hdev)) {
3479 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
3480 return 0;
3481 usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
3482 } else {
3483 if (!udev || udev->state != USB_STATE_SUSPENDED ||
3484 (portstatus & USB_PORT_STAT_LINK_STATE) !=
3485 USB_SS_PORT_LS_U0)
3486 return 0;
3487 }
3488
3489 if (udev) {
3490 /* TRSMRCY = 10 msec */
3491 msleep(10);
3492
3493 usb_unlock_port(port_dev);
3494 ret = usb_remote_wakeup(udev);
3495 usb_lock_port(port_dev);
3496 if (ret < 0)
3497 connect_change = 1;
3498 } else {
3499 ret = -ENODEV;
3500 hub_port_disable(hub, port, 1);
3501 }
3502 dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
3503 return connect_change;
3504}
3505
3506#else
3507
3508static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
3509 u16 portstatus, u16 portchange)
3510{
3511 return 0;
3512}
3513
Alan Sternd388dab2006-07-01 22:14:24 -04003514#endif
3515
Ming Leie6f30de2012-10-24 11:59:24 +08003516static int check_ports_changed(struct usb_hub *hub)
3517{
3518 int port1;
3519
3520 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3521 u16 portstatus, portchange;
3522 int status;
3523
3524 status = hub_port_status(hub, port1, &portstatus, &portchange);
3525 if (!status && portchange)
3526 return 1;
3527 }
3528 return 0;
3529}
3530
David Brownelldb690872005-09-13 19:56:33 -07003531static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
3533 struct usb_hub *hub = usb_get_intfdata (intf);
3534 struct usb_device *hdev = hub->hdev;
3535 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003536 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Alan Sterne583d9d2013-07-11 14:58:04 -04003538 /*
3539 * Warn if children aren't already suspended.
3540 * Also, add up the number of wakeup-enabled descendants.
3541 */
3542 hub->wakeup_enabled_descendants = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003544 struct usb_port *port_dev = hub->ports[port1 - 1];
3545 struct usb_device *udev = port_dev->child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
Alan Stern6840d252007-09-10 11:34:26 -04003547 if (udev && udev->can_submit) {
Dan Williamsb658b8f2014-06-17 16:16:22 -07003548 dev_warn(&port_dev->dev, "device %s not suspended yet\n",
3549 dev_name(&udev->dev));
Alan Stern5b1b0b82011-08-19 23:49:48 +02003550 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003551 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003552 }
Alan Sterne583d9d2013-07-11 14:58:04 -04003553 if (udev)
3554 hub->wakeup_enabled_descendants +=
3555 wakeup_enabled_descendants(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 }
Ming Leie6f30de2012-10-24 11:59:24 +08003557
3558 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3559 /* check if there are changes pending on hub ports */
3560 if (check_ports_changed(hub)) {
3561 if (PMSG_IS_AUTO(msg))
3562 return -EBUSY;
3563 pm_wakeup_event(&hdev->dev, 2000);
3564 }
3565 }
3566
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003567 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3568 /* Enable hub to send remote wakeup for all ports. */
3569 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3570 status = set_port_feature(hdev,
3571 port1 |
3572 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3573 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3574 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3575 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3576 }
3577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Harvey Harrison441b62c2008-03-03 16:08:34 -08003579 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003580
David Brownellc9f89fa2005-09-13 19:57:04 -07003581 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003582 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584}
3585
3586static int hub_resume(struct usb_interface *intf)
3587{
Alan Stern5e6effa2008-03-03 15:15:51 -05003588 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Alan Stern5e6effa2008-03-03 15:15:51 -05003590 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003591 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 return 0;
3593}
3594
Alan Sternb41a60e2007-05-30 15:39:33 -04003595static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003596{
Alan Sternb41a60e2007-05-30 15:39:33 -04003597 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003598
Alan Stern5e6effa2008-03-03 15:15:51 -05003599 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003600 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003601 return 0;
3602}
3603
Alan Stern54515fe2007-05-30 15:38:58 -04003604/**
3605 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3606 * @rhdev: struct usb_device for the root hub
3607 *
3608 * The USB host controller driver calls this function when its root hub
3609 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003610 * has been reset. The routine marks @rhdev as having lost power.
3611 * When the hub driver is resumed it will take notice and carry out
3612 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3613 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003614 */
3615void usb_root_hub_lost_power(struct usb_device *rhdev)
3616{
3617 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3618 rhdev->reset_resume = 1;
3619}
3620EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3621
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003622static const char * const usb3_lpm_names[] = {
3623 "U0",
3624 "U1",
3625 "U2",
3626 "U3",
3627};
3628
3629/*
3630 * Send a Set SEL control transfer to the device, prior to enabling
3631 * device-initiated U1 or U2. This lets the device know the exit latencies from
3632 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3633 * packet from the host.
3634 *
3635 * This function will fail if the SEL or PEL values for udev are greater than
3636 * the maximum allowed values for the link state to be enabled.
3637 */
3638static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3639{
3640 struct usb_set_sel_req *sel_values;
3641 unsigned long long u1_sel;
3642 unsigned long long u1_pel;
3643 unsigned long long u2_sel;
3644 unsigned long long u2_pel;
3645 int ret;
3646
Xenia Ragiadakou38d7f682013-09-04 17:24:45 +03003647 if (udev->state != USB_STATE_CONFIGURED)
3648 return 0;
3649
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003650 /* Convert SEL and PEL stored in ns to us */
3651 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3652 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3653 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3654 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3655
3656 /*
3657 * Make sure that the calculated SEL and PEL values for the link
3658 * state we're enabling aren't bigger than the max SEL/PEL
3659 * value that will fit in the SET SEL control transfer.
3660 * Otherwise the device would get an incorrect idea of the exit
3661 * latency for the link state, and could start a device-initiated
3662 * U1/U2 when the exit latencies are too high.
3663 */
3664 if ((state == USB3_LPM_U1 &&
3665 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3666 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3667 (state == USB3_LPM_U2 &&
3668 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3669 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003670 dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n",
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003671 usb3_lpm_names[state], u1_sel, u1_pel);
3672 return -EINVAL;
3673 }
3674
3675 /*
3676 * If we're enabling device-initiated LPM for one link state,
3677 * but the other link state has a too high SEL or PEL value,
3678 * just set those values to the max in the Set SEL request.
3679 */
3680 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3681 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3682
3683 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3684 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3685
3686 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3687 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3688
3689 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3690 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3691
3692 /*
3693 * usb_enable_lpm() can be called as part of a failed device reset,
3694 * which may be initiated by an error path of a mass storage driver.
3695 * Therefore, use GFP_NOIO.
3696 */
3697 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3698 if (!sel_values)
3699 return -ENOMEM;
3700
3701 sel_values->u1_sel = u1_sel;
3702 sel_values->u1_pel = u1_pel;
3703 sel_values->u2_sel = cpu_to_le16(u2_sel);
3704 sel_values->u2_pel = cpu_to_le16(u2_pel);
3705
3706 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3707 USB_REQ_SET_SEL,
3708 USB_RECIP_DEVICE,
3709 0, 0,
3710 sel_values, sizeof *(sel_values),
3711 USB_CTRL_SET_TIMEOUT);
3712 kfree(sel_values);
3713 return ret;
3714}
3715
3716/*
3717 * Enable or disable device-initiated U1 or U2 transitions.
3718 */
3719static int usb_set_device_initiated_lpm(struct usb_device *udev,
3720 enum usb3_link_state state, bool enable)
3721{
3722 int ret;
3723 int feature;
3724
3725 switch (state) {
3726 case USB3_LPM_U1:
3727 feature = USB_DEVICE_U1_ENABLE;
3728 break;
3729 case USB3_LPM_U2:
3730 feature = USB_DEVICE_U2_ENABLE;
3731 break;
3732 default:
3733 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3734 __func__, enable ? "enable" : "disable");
3735 return -EINVAL;
3736 }
3737
3738 if (udev->state != USB_STATE_CONFIGURED) {
3739 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3740 "for unconfigured device.\n",
3741 __func__, enable ? "enable" : "disable",
3742 usb3_lpm_names[state]);
3743 return 0;
3744 }
3745
3746 if (enable) {
3747 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003748 * Now send the control transfer to enable device-initiated LPM
3749 * for either U1 or U2.
3750 */
3751 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3752 USB_REQ_SET_FEATURE,
3753 USB_RECIP_DEVICE,
3754 feature,
3755 0, NULL, 0,
3756 USB_CTRL_SET_TIMEOUT);
3757 } else {
3758 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3759 USB_REQ_CLEAR_FEATURE,
3760 USB_RECIP_DEVICE,
3761 feature,
3762 0, NULL, 0,
3763 USB_CTRL_SET_TIMEOUT);
3764 }
3765 if (ret < 0) {
3766 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3767 enable ? "Enable" : "Disable",
3768 usb3_lpm_names[state]);
3769 return -EBUSY;
3770 }
3771 return 0;
3772}
3773
3774static int usb_set_lpm_timeout(struct usb_device *udev,
3775 enum usb3_link_state state, int timeout)
3776{
3777 int ret;
3778 int feature;
3779
3780 switch (state) {
3781 case USB3_LPM_U1:
3782 feature = USB_PORT_FEAT_U1_TIMEOUT;
3783 break;
3784 case USB3_LPM_U2:
3785 feature = USB_PORT_FEAT_U2_TIMEOUT;
3786 break;
3787 default:
3788 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3789 __func__);
3790 return -EINVAL;
3791 }
3792
3793 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3794 timeout != USB3_LPM_DEVICE_INITIATED) {
3795 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3796 "which is a reserved value.\n",
3797 usb3_lpm_names[state], timeout);
3798 return -EINVAL;
3799 }
3800
3801 ret = set_port_feature(udev->parent,
3802 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3803 feature);
3804 if (ret < 0) {
3805 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3806 "error code %i\n", usb3_lpm_names[state],
3807 timeout, ret);
3808 return -EBUSY;
3809 }
3810 if (state == USB3_LPM_U1)
3811 udev->u1_params.timeout = timeout;
3812 else
3813 udev->u2_params.timeout = timeout;
3814 return 0;
3815}
3816
3817/*
3818 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3819 * U1/U2 entry.
3820 *
3821 * We will attempt to enable U1 or U2, but there are no guarantees that the
3822 * control transfers to set the hub timeout or enable device-initiated U1/U2
3823 * will be successful.
3824 *
3825 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3826 * driver know about it. If that call fails, it should be harmless, and just
3827 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3828 */
3829static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3830 enum usb3_link_state state)
3831{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003832 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003833 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3834 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3835
3836 /* If the device says it doesn't have *any* exit latency to come out of
3837 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3838 * state.
3839 */
3840 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3841 (state == USB3_LPM_U2 && u2_mel == 0))
3842 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003843
Sarah Sharp65a95b72012-10-05 10:32:07 -07003844 /*
3845 * First, let the device know about the exit latencies
3846 * associated with the link state we're about to enable.
3847 */
3848 ret = usb_req_set_sel(udev, state);
3849 if (ret < 0) {
3850 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3851 usb3_lpm_names[state]);
3852 return;
3853 }
3854
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003855 /* We allow the host controller to set the U1/U2 timeout internally
3856 * first, so that it can change its schedule to account for the
3857 * additional latency to send data to a device in a lower power
3858 * link state.
3859 */
3860 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3861
3862 /* xHCI host controller doesn't want to enable this LPM state. */
3863 if (timeout == 0)
3864 return;
3865
3866 if (timeout < 0) {
3867 dev_warn(&udev->dev, "Could not enable %s link state, "
3868 "xHCI error %i.\n", usb3_lpm_names[state],
3869 timeout);
3870 return;
3871 }
3872
3873 if (usb_set_lpm_timeout(udev, state, timeout))
3874 /* If we can't set the parent hub U1/U2 timeout,
3875 * device-initiated LPM won't be allowed either, so let the xHCI
3876 * host know that this link state won't be enabled.
3877 */
3878 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3879
3880 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3881 else if (udev->actconfig)
3882 usb_set_device_initiated_lpm(udev, state, true);
3883
3884}
3885
3886/*
3887 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3888 * U1/U2 entry.
3889 *
3890 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3891 * If zero is returned, the parent will not allow the link to go into U1/U2.
3892 *
3893 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3894 * it won't have an effect on the bus link state because the parent hub will
3895 * still disallow device-initiated U1/U2 entry.
3896 *
3897 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3898 * possible. The result will be slightly more bus bandwidth will be taken up
3899 * (to account for U1/U2 exit latency), but it should be harmless.
3900 */
3901static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3902 enum usb3_link_state state)
3903{
3904 int feature;
3905
3906 switch (state) {
3907 case USB3_LPM_U1:
3908 feature = USB_PORT_FEAT_U1_TIMEOUT;
3909 break;
3910 case USB3_LPM_U2:
3911 feature = USB_PORT_FEAT_U2_TIMEOUT;
3912 break;
3913 default:
3914 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3915 __func__);
3916 return -EINVAL;
3917 }
3918
3919 if (usb_set_lpm_timeout(udev, state, 0))
3920 return -EBUSY;
3921
3922 usb_set_device_initiated_lpm(udev, state, false);
3923
3924 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3925 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3926 "bus schedule bandwidth may be impacted.\n",
3927 usb3_lpm_names[state]);
3928 return 0;
3929}
3930
3931/*
3932 * Disable hub-initiated and device-initiated U1 and U2 entry.
3933 * Caller must own the bandwidth_mutex.
3934 *
3935 * This will call usb_enable_lpm() on failure, which will decrement
3936 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3937 */
3938int usb_disable_lpm(struct usb_device *udev)
3939{
3940 struct usb_hcd *hcd;
3941
3942 if (!udev || !udev->parent ||
3943 udev->speed != USB_SPEED_SUPER ||
Pratyush Anand51df62f2014-07-04 17:01:27 +03003944 !udev->lpm_capable ||
3945 udev->state < USB_STATE_DEFAULT)
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003946 return 0;
3947
3948 hcd = bus_to_hcd(udev->bus);
3949 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3950 return 0;
3951
3952 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003953 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003954 return 0;
3955
3956 /* If LPM is enabled, attempt to disable it. */
3957 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3958 goto enable_lpm;
3959 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3960 goto enable_lpm;
3961
3962 return 0;
3963
3964enable_lpm:
3965 usb_enable_lpm(udev);
3966 return -EBUSY;
3967}
3968EXPORT_SYMBOL_GPL(usb_disable_lpm);
3969
3970/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3971int usb_unlocked_disable_lpm(struct usb_device *udev)
3972{
3973 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3974 int ret;
3975
3976 if (!hcd)
3977 return -EINVAL;
3978
3979 mutex_lock(hcd->bandwidth_mutex);
3980 ret = usb_disable_lpm(udev);
3981 mutex_unlock(hcd->bandwidth_mutex);
3982
3983 return ret;
3984}
3985EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3986
3987/*
3988 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3989 * xHCI host policy may prevent U1 or U2 from being enabled.
3990 *
3991 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3992 * until the lpm_disable_count drops to zero. Caller must own the
3993 * bandwidth_mutex.
3994 */
3995void usb_enable_lpm(struct usb_device *udev)
3996{
3997 struct usb_hcd *hcd;
3998
3999 if (!udev || !udev->parent ||
4000 udev->speed != USB_SPEED_SUPER ||
Pratyush Anand51df62f2014-07-04 17:01:27 +03004001 !udev->lpm_capable ||
4002 udev->state < USB_STATE_DEFAULT)
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004003 return;
4004
4005 udev->lpm_disable_count--;
4006 hcd = bus_to_hcd(udev->bus);
4007 /* Double check that we can both enable and disable LPM.
4008 * Device must be configured to accept set feature U1/U2 timeout.
4009 */
4010 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
4011 !hcd->driver->disable_usb3_lpm_timeout)
4012 return;
4013
4014 if (udev->lpm_disable_count > 0)
4015 return;
4016
4017 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
4018 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
4019}
4020EXPORT_SYMBOL_GPL(usb_enable_lpm);
4021
4022/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
4023void usb_unlocked_enable_lpm(struct usb_device *udev)
4024{
4025 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4026
4027 if (!hcd)
4028 return;
4029
4030 mutex_lock(hcd->bandwidth_mutex);
4031 usb_enable_lpm(udev);
4032 mutex_unlock(hcd->bandwidth_mutex);
4033}
4034EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
4035
4036
Alan Sternd388dab2006-07-01 22:14:24 -04004037#else /* CONFIG_PM */
4038
Alan Sternf07600c2007-05-30 15:38:16 -04004039#define hub_suspend NULL
4040#define hub_resume NULL
4041#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004042
4043int usb_disable_lpm(struct usb_device *udev)
4044{
4045 return 0;
4046}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004047EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004048
4049void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004050EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004051
4052int usb_unlocked_disable_lpm(struct usb_device *udev)
4053{
4054 return 0;
4055}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004056EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004057
4058void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004059EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07004060
4061int usb_disable_ltm(struct usb_device *udev)
4062{
4063 return 0;
4064}
4065EXPORT_SYMBOL_GPL(usb_disable_ltm);
4066
4067void usb_enable_ltm(struct usb_device *udev) { }
4068EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternc4b51a42013-07-11 14:58:23 -04004069
Stephen Rothwell4a95b1f2014-05-29 18:55:06 +10004070static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
4071 u16 portstatus, u16 portchange)
4072{
4073 return 0;
4074}
4075
Alan Sternc4b51a42013-07-11 14:58:23 -04004076#endif /* CONFIG_PM */
Alan Sternd388dab2006-07-01 22:14:24 -04004077
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
4079/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
4080 *
4081 * Between connect detection and reset signaling there must be a delay
4082 * of 100ms at least for debounce and power-settling. The corresponding
4083 * timer shall restart whenever the downstream port detects a disconnect.
Matthias Beyer469271f2013-10-10 23:41:27 +02004084 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 * Apparently there are some bluetooth and irda-dongles and a number of
4086 * low-speed devices for which this debounce period may last over a second.
4087 * Not covered by the spec - but easy to deal with.
4088 *
4089 * This implementation uses a 1500ms total debounce timeout; if the
4090 * connection isn't stable by then it returns -ETIMEDOUT. It checks
4091 * every 25ms for transient disconnects. When the port status has been
4092 * unchanged for 100ms it returns the port status.
4093 */
Lan Tianyuad493e52013-01-23 04:26:30 +08004094int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095{
4096 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 u16 portchange, portstatus;
4098 unsigned connection = 0xffff;
Dan Williamsd99f6b42014-05-20 18:08:17 -07004099 int total_time, stable_time = 0;
4100 struct usb_port *port_dev = hub->ports[port1 - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
4102 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
4103 ret = hub_port_status(hub, port1, &portstatus, &portchange);
4104 if (ret < 0)
4105 return ret;
4106
4107 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
4108 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004109 if (!must_be_connected ||
4110 (connection == USB_PORT_STAT_CONNECTION))
4111 stable_time += HUB_DEBOUNCE_STEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 if (stable_time >= HUB_DEBOUNCE_STABLE)
4113 break;
4114 } else {
4115 stable_time = 0;
4116 connection = portstatus & USB_PORT_STAT_CONNECTION;
4117 }
4118
4119 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004120 usb_clear_port_feature(hub->hdev, port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 USB_PORT_FEAT_C_CONNECTION);
4122 }
4123
4124 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
4125 break;
4126 msleep(HUB_DEBOUNCE_STEP);
4127 }
4128
Dan Williamsd99f6b42014-05-20 18:08:17 -07004129 dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n",
4130 total_time, stable_time, portstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
4132 if (stable_time < HUB_DEBOUNCE_STABLE)
4133 return -ETIMEDOUT;
4134 return portstatus;
4135}
4136
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004137void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138{
Alan Sternddeac4e2009-01-15 17:03:33 -05004139 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
4140 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05004141 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004143EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
4145#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
4146#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
4147
Alan Stern4326ed02007-07-30 17:08:43 -04004148static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149{
4150 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07004151 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
Sarah Sharpc6515272009-04-27 19:57:26 -07004153 /*
4154 * The host controller will choose the device address,
4155 * instead of the core having chosen it earlier
4156 */
4157 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return -EINVAL;
4159 if (udev->state == USB_STATE_ADDRESS)
4160 return 0;
4161 if (udev->state != USB_STATE_DEFAULT)
4162 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07004163 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07004164 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004165 else
Sarah Sharpc6515272009-04-27 19:57:26 -07004166 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
4167 USB_REQ_SET_ADDRESS, 0, devnum, 0,
4168 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05004170 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07004171 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004173 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 }
4175 return retval;
4176}
4177
Mathias Nyman890dae82013-09-30 17:26:31 +03004178/*
4179 * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM
4180 * when they're plugged into a USB 2.0 port, but they don't work when LPM is
4181 * enabled.
4182 *
4183 * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the
4184 * device says it supports the new USB 2.0 Link PM errata by setting the BESL
4185 * support bit in the BOS descriptor.
4186 */
4187static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev)
4188{
Dan Williamsd99f6b42014-05-20 18:08:17 -07004189 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
4190 int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
Mathias Nyman890dae82013-09-30 17:26:31 +03004191
4192 if (!udev->usb2_hw_lpm_capable)
4193 return;
4194
Dan Williamsd99f6b42014-05-20 18:08:17 -07004195 if (hub)
4196 connect_type = hub->ports[udev->portnum - 1]->connect_type;
Mathias Nyman890dae82013-09-30 17:26:31 +03004197
Bjørn Mork23c05822014-02-27 14:23:55 +01004198 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) ||
Mathias Nyman890dae82013-09-30 17:26:31 +03004199 connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
4200 udev->usb2_hw_lpm_allowed = 1;
4201 usb_set_usb2_hardware_lpm(udev, 1);
4202 }
4203}
4204
Dan Williams48fc7db2013-12-05 17:07:27 -08004205static int hub_enable_device(struct usb_device *udev)
4206{
4207 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4208
4209 if (!hcd->driver->enable_device)
4210 return 0;
4211 if (udev->state == USB_STATE_ADDRESS)
4212 return 0;
4213 if (udev->state != USB_STATE_DEFAULT)
4214 return -EINVAL;
4215
4216 return hcd->driver->enable_device(hcd, udev);
4217}
4218
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219/* Reset device, (re)assign address, get device descriptor.
4220 * Device connection must be stable, no more debouncing needed.
4221 * Returns device in USB_STATE_ADDRESS, except on error.
4222 *
4223 * If this is called for an already-existing device (as part of
Dan Williams5c79a1e2014-05-20 18:09:26 -07004224 * usb_reset_and_verify_device), the caller must own the device lock and
4225 * the port lock. For a newly detected device that is not accessible
4226 * through any global pointers, it's not necessary to lock the device,
4227 * but it is still necessary to lock the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 */
4229static int
4230hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
4231 int retry_counter)
4232{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07004234 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 int i, j, retval;
4236 unsigned delay = HUB_SHORT_RESET_TIME;
4237 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004238 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04004239 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
4241 /* root hub ports have a slightly longer reset period
4242 * (from USB 2.0 spec, section 7.1.7.5)
4243 */
4244 if (!hdev->parent) {
4245 delay = HUB_ROOT_RESET_TIME;
4246 if (port1 == hdev->bus->otg_port)
4247 hdev->bus->b_hnp_enable = 0;
4248 }
4249
4250 /* Some low speed devices have problems with the quick delay, so */
4251 /* be a bit pessimistic with those devices. RHbug #23670 */
4252 if (oldspeed == USB_SPEED_LOW)
4253 delay = HUB_LONG_RESET_TIME;
4254
Todd E Brandt6fecd4f2014-05-19 10:55:32 -07004255 mutex_lock(&hdev->bus->usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Luben Tuikov07194ab2011-02-11 11:33:10 -08004257 /* Reset the device; full speed may morph to high speed */
4258 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07004259 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08004260 if (retval < 0) /* error or disconnect */
4261 goto fail;
4262 /* success, speed is known */
4263
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 retval = -ENODEV;
4265
4266 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
4267 dev_dbg(&udev->dev, "device reset changed speed!\n");
4268 goto fail;
4269 }
4270 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04004271
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
4273 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004274 * For Wireless USB devices, ep0 max packet is always 512 (tho
4275 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 */
4277 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07004278 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08004279 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004280 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004283 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 break;
4285 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
4286 /* to determine the ep0 maxpacket size, try to read
4287 * the device descriptor to get bMaxPacketSize0 and
4288 * then correct our initial guess.
4289 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004290 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 break;
4292 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004293 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 break;
4295 default:
4296 goto fail;
4297 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004298
4299 if (udev->speed == USB_SPEED_WIRELESS)
4300 speed = "variable speed Wireless";
4301 else
4302 speed = usb_speed_string(udev->speed);
4303
Sarah Sharpc6515272009-04-27 19:57:26 -07004304 if (udev->speed != USB_SPEED_SUPER)
4305 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004306 "%s %s USB device number %d using %s\n",
4307 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05004308 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
4310 /* Set up TT records, if needed */
4311 if (hdev->tt) {
4312 udev->tt = hdev->tt;
4313 udev->ttport = hdev->ttport;
4314 } else if (udev->speed != USB_SPEED_HIGH
4315 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05004316 if (!hub->tt.hub) {
4317 dev_err(&udev->dev, "parent hub has no TT\n");
4318 retval = -EINVAL;
4319 goto fail;
4320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 udev->tt = &hub->tt;
4322 udev->ttport = port1;
4323 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004324
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
4326 * Because device hardware and firmware is sometimes buggy in
4327 * this area, and this is how Linux has done it for ages.
4328 * Change it cautiously.
4329 *
Dan Williams48fc7db2013-12-05 17:07:27 -08004330 * NOTE: If use_new_scheme() is true we will start by issuing
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
4332 * so it may help with some non-standards-compliant devices.
4333 * Otherwise we start with SET_ADDRESS and then try to read the
4334 * first 8 bytes of the device descriptor to get the ep0 maxpacket
4335 * value.
4336 */
4337 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Dan Williams48fc7db2013-12-05 17:07:27 -08004338 bool did_new_scheme = false;
4339
4340 if (use_new_scheme(udev, retry_counter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 struct usb_device_descriptor *buf;
4342 int r = 0;
4343
Dan Williams48fc7db2013-12-05 17:07:27 -08004344 did_new_scheme = true;
4345 retval = hub_enable_device(udev);
Oliver Neukum938569e2014-02-27 10:57:10 +01004346 if (retval < 0) {
4347 dev_err(&udev->dev,
4348 "hub failed to enable device, error %d\n",
4349 retval);
Dan Williams48fc7db2013-12-05 17:07:27 -08004350 goto fail;
Oliver Neukum938569e2014-02-27 10:57:10 +01004351 }
Dan Williams48fc7db2013-12-05 17:07:27 -08004352
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353#define GET_DESCRIPTOR_BUFSIZE 64
4354 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
4355 if (!buf) {
4356 retval = -ENOMEM;
4357 continue;
4358 }
4359
Alan Sternb89ee192007-05-11 10:19:04 -04004360 /* Retry on all errors; some devices are flakey.
4361 * 255 is for WUSB devices, we actually need to use
4362 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 */
4364 for (j = 0; j < 3; ++j) {
4365 buf->bMaxPacketSize0 = 0;
4366 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
4367 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
4368 USB_DT_DEVICE << 8, 0,
4369 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02004370 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004372 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 if (buf->bDescriptorType ==
4374 USB_DT_DEVICE) {
4375 r = 0;
4376 break;
4377 }
4378 /* FALL THROUGH */
4379 default:
4380 if (r == 0)
4381 r = -EPROTO;
4382 break;
4383 }
4384 if (r == 0)
4385 break;
4386 }
4387 udev->descriptor.bMaxPacketSize0 =
4388 buf->bMaxPacketSize0;
4389 kfree(buf);
4390
Andiry Xu75d7cf72011-09-13 16:41:11 -07004391 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 if (retval < 0) /* error or disconnect */
4393 goto fail;
4394 if (oldspeed != udev->speed) {
4395 dev_dbg(&udev->dev,
4396 "device reset changed speed!\n");
4397 retval = -ENODEV;
4398 goto fail;
4399 }
4400 if (r) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004401 if (r != -ENODEV)
4402 dev_err(&udev->dev, "device descriptor read/64, error %d\n",
4403 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 retval = -EMSGSIZE;
4405 continue;
4406 }
4407#undef GET_DESCRIPTOR_BUFSIZE
4408 }
4409
Matthias Beyer469271f2013-10-10 23:41:27 +02004410 /*
4411 * If device is WUSB, we already assigned an
4412 * unauthorized address in the Connect Ack sequence;
4413 * authorization will assign the final address.
4414 */
Sarah Sharpc6515272009-04-27 19:57:26 -07004415 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004416 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
4417 retval = hub_set_address(udev, devnum);
4418 if (retval >= 0)
4419 break;
4420 msleep(200);
4421 }
4422 if (retval < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004423 if (retval != -ENODEV)
4424 dev_err(&udev->dev, "device not accepting address %d, error %d\n",
4425 devnum, retval);
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004426 goto fail;
4427 }
Sarah Sharpc6515272009-04-27 19:57:26 -07004428 if (udev->speed == USB_SPEED_SUPER) {
4429 devnum = udev->devnum;
4430 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05004431 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07004432 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05004433 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07004434 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004435
4436 /* cope with hardware quirkiness:
4437 * - let SET_ADDRESS settle, some device hardware wants it
4438 * - read ep0 maxpacket even for high and low speed,
4439 */
4440 msleep(10);
Dan Williams48fc7db2013-12-05 17:07:27 -08004441 /* use_new_scheme() checks the speed which may have
4442 * changed since the initial look so we cache the result
4443 * in did_new_scheme
4444 */
4445 if (did_new_scheme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 break;
Matthias Beyer469271f2013-10-10 23:41:27 +02004447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448
4449 retval = usb_get_device_descriptor(udev, 8);
4450 if (retval < 8) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004451 if (retval != -ENODEV)
4452 dev_err(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004453 "device descriptor read/8, error %d\n",
4454 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 if (retval >= 0)
4456 retval = -EMSGSIZE;
4457 } else {
4458 retval = 0;
4459 break;
4460 }
4461 }
4462 if (retval)
4463 goto fail;
4464
Peter Chenb76baa82012-11-09 09:44:43 +08004465 if (hcd->phy && !hdev->parent)
Peter Chenac965112012-11-09 09:44:44 +08004466 usb_phy_notify_connect(hcd->phy, udev->speed);
Peter Chenb76baa82012-11-09 09:44:43 +08004467
Elric Fud8aec3d2012-03-26 21:16:02 +08004468 /*
4469 * Some superspeed devices have finished the link training process
4470 * and attached to a superspeed hub port, but the device descriptor
4471 * got from those devices show they aren't superspeed devices. Warm
4472 * reset the port attached by the devices can fix them.
4473 */
4474 if ((udev->speed == USB_SPEED_SUPER) &&
4475 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4476 dev_err(&udev->dev, "got a wrong device descriptor, "
4477 "warm reset device\n");
4478 hub_port_reset(hub, port1, udev,
4479 HUB_BH_RESET_TIME, true);
4480 retval = -EINVAL;
4481 goto fail;
4482 }
4483
Sarah Sharp6b403b02009-04-27 19:54:10 -07004484 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
4485 udev->speed == USB_SPEED_SUPER)
4486 i = 512;
4487 else
4488 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07004489 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04004490 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04004492 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 retval = -EMSGSIZE;
4494 goto fail;
4495 }
Alan Stern56626a72010-10-14 15:25:21 -04004496 if (udev->speed == USB_SPEED_FULL)
4497 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4498 else
4499 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004501 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004503
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4505 if (retval < (signed)sizeof(udev->descriptor)) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004506 if (retval != -ENODEV)
4507 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4508 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 if (retval >= 0)
4510 retval = -ENOMSG;
4511 goto fail;
4512 }
4513
Andiry Xu1ff4df52011-09-23 14:19:48 -07004514 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4515 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004516 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004517 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004518 usb_set_lpm_parameters(udev);
4519 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004520 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004523 /* notify HCD that we have a device connected and addressed */
4524 if (hcd->driver->update_device)
4525 hcd->driver->update_device(hcd, udev);
Mathias Nyman890dae82013-09-30 17:26:31 +03004526 hub_set_initial_usb2_lpm_policy(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004528 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004530 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004531 }
Todd E Brandt6fecd4f2014-05-19 10:55:32 -07004532 mutex_unlock(&hdev->bus->usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 return retval;
4534}
4535
4536static void
4537check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4538{
4539 struct usb_qualifier_descriptor *qual;
4540 int status;
4541
Johan Hovold2a159382014-08-25 17:51:26 +02004542 if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER)
4543 return;
4544
Christoph Lametere94b1762006-12-06 20:33:17 -08004545 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 if (qual == NULL)
4547 return;
4548
4549 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4550 qual, sizeof *qual);
4551 if (status == sizeof *qual) {
4552 dev_info(&udev->dev, "not running at top speed; "
4553 "connect to a high speed hub\n");
4554 /* hub LEDs are probably harder to miss than syslog */
4555 if (hub->has_indicators) {
4556 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08004557 queue_delayed_work(system_power_efficient_wq,
4558 &hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 }
4560 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004561 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562}
4563
4564static unsigned
4565hub_power_remaining (struct usb_hub *hub)
4566{
4567 struct usb_device *hdev = hub->hdev;
4568 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004569 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Alan Stern55c52712005-11-23 12:03:12 -05004571 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 return 0;
4573
Alan Stern55c52712005-11-23 12:03:12 -05004574 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4575 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004576 struct usb_port *port_dev = hub->ports[port1 - 1];
4577 struct usb_device *udev = port_dev->child;
4578 unsigned unit_load;
4579 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
4581 if (!udev)
4582 continue;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004583 if (hub_is_superspeed(udev))
4584 unit_load = 150;
4585 else
4586 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004588 /*
4589 * Unconfigured devices may not use more than one unit load,
4590 * or 8mA for OTG ports
4591 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 if (udev->actconfig)
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +01004593 delta = usb_get_max_power(udev, udev->actconfig);
Alan Stern55c52712005-11-23 12:03:12 -05004594 else if (port1 != udev->bus->otg_port || hdev->parent)
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004595 delta = unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 else
Alan Stern55c52712005-11-23 12:03:12 -05004597 delta = 8;
4598 if (delta > hub->mA_per_port)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004599 dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n",
4600 delta, hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 remaining -= delta;
4602 }
4603 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004604 dev_warn(hub->intfdev, "%dmA over power budget!\n",
Matthias Beyer469271f2013-10-10 23:41:27 +02004605 -remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 remaining = 0;
4607 }
4608 return remaining;
4609}
4610
Dan Williamsaf376a42014-05-20 18:09:15 -07004611static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
4612 u16 portchange)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 int status, i;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004615 unsigned unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 struct usb_device *hdev = hub->hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004618 struct usb_port *port_dev = hub->ports[port1 - 1];
Dan Williamsaf376a42014-05-20 18:09:15 -07004619 struct usb_device *udev = port_dev->child;
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004620 static int unreliable_port = -1;
Alan Stern8808f002008-04-28 11:06:55 -04004621
Alan Stern24618b02008-04-28 11:06:28 -04004622 /* Disconnect any existing devices under this port */
Peter Chenb76baa82012-11-09 09:44:43 +08004623 if (udev) {
4624 if (hcd->phy && !hdev->parent &&
4625 !(portstatus & USB_PORT_STAT_CONNECTION))
Peter Chenac965112012-11-09 09:44:44 +08004626 usb_phy_notify_disconnect(hcd->phy, udev->speed);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004627 usb_disconnect(&port_dev->child);
Peter Chenb76baa82012-11-09 09:44:43 +08004628 }
Alan Stern24618b02008-04-28 11:06:28 -04004629
Alan Stern253e0572009-10-27 15:20:13 -04004630 /* We can forget about a "removed" device when there's a physical
4631 * disconnect or the connect status changes.
4632 */
4633 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4634 (portchange & USB_PORT_STAT_C_CONNECTION))
4635 clear_bit(port1, hub->removed_bits);
4636
Alan Stern5257d972008-09-22 14:43:08 -04004637 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4638 USB_PORT_STAT_C_ENABLE)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004639 status = hub_port_debounce_be_stable(hub, port1);
Alan Stern5257d972008-09-22 14:43:08 -04004640 if (status < 0) {
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004641 if (status != -ENODEV &&
4642 port1 != unreliable_port &&
4643 printk_ratelimit())
Takashi Iwaidd5f5002014-08-19 17:37:55 +02004644 dev_err(&port_dev->dev, "connect-debounce failed\n");
Alan Stern5257d972008-09-22 14:43:08 -04004645 portstatus &= ~USB_PORT_STAT_CONNECTION;
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004646 unreliable_port = port1;
Alan Stern5257d972008-09-22 14:43:08 -04004647 } else {
4648 portstatus = status;
4649 }
4650 }
4651
Alan Stern253e0572009-10-27 15:20:13 -04004652 /* Return now if debouncing failed or nothing is connected or
4653 * the device was "removed".
4654 */
4655 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4656 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657
4658 /* maybe switch power back on (e.g. root hub was reset) */
Dan Williams9262c192014-05-20 18:08:12 -07004659 if (hub_is_port_power_switchable(hub)
Andiry Xu0ed9a572011-04-27 18:07:43 +08004660 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004662
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 if (portstatus & USB_PORT_STAT_ENABLE)
Matthias Beyer469271f2013-10-10 23:41:27 +02004664 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 return;
4666 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004667 if (hub_is_superspeed(hub->hdev))
4668 unit_load = 150;
4669 else
4670 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
Alan Sterne9e88fb2013-03-27 16:14:01 -04004672 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
4675 /* reallocate for each attempt, since references
4676 * to the previous one can escape in various ways
4677 */
4678 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4679 if (!udev) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004680 dev_err(&port_dev->dev,
4681 "couldn't allocate usb_device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 goto done;
4683 }
4684
4685 usb_set_device_state(udev, USB_STATE_POWERED);
Matthias Beyer469271f2013-10-10 23:41:27 +02004686 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004687 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004688 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004689
Sarah Sharp131dec32010-12-06 21:00:19 -08004690 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4691 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004692 udev->speed = USB_SPEED_SUPER;
4693 else
4694 udev->speed = USB_SPEED_UNKNOWN;
4695
Alan Stern3b29b682011-02-22 09:53:41 -05004696 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004697 if (udev->devnum <= 0) {
4698 status = -ENOTCONN; /* Don't retry */
4699 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004700 }
4701
Sarah Sharpe7b77172009-04-27 19:54:26 -07004702 /* reset (non-USB 3.0 devices) and get descriptor */
Dan Williams5c79a1e2014-05-20 18:09:26 -07004703 usb_lock_port(port_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 status = hub_port_init(hub, udev, port1, i);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004705 usb_unlock_port(port_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 if (status < 0)
4707 goto loop;
4708
Phil Dibowitz93362a82010-07-22 00:05:01 +02004709 usb_detect_quirks(udev);
4710 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4711 msleep(1000);
4712
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 /* consecutive bus-powered hubs aren't reliable; they can
4714 * violate the voltage drop budget. if the new child has
4715 * a "powered" LED, users should notice we didn't enable it
4716 * (without reading syslog), even without per-port LEDs
4717 * on the parent.
4718 */
4719 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004720 && udev->bus_mA <= unit_load) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 u16 devstat;
4722
4723 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4724 &devstat);
Alan Stern15b73362013-07-30 15:35:40 -04004725 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 dev_dbg(&udev->dev, "get status %d ?\n", status);
4727 goto loop_disable;
4728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4730 dev_err(&udev->dev,
4731 "can't connect bus-powered hub "
4732 "to this port\n");
4733 if (hub->has_indicators) {
4734 hub->indicator[port1-1] =
4735 INDICATOR_AMBER_BLINK;
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08004736 queue_delayed_work(
4737 system_power_efficient_wq,
4738 &hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 }
4740 status = -ENOTCONN; /* Don't retry */
4741 goto loop_disable;
4742 }
4743 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004744
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 /* check for devices running slower than they could */
4746 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4747 && udev->speed == USB_SPEED_FULL
4748 && highspeed_hubs != 0)
4749 check_highspeed (hub, udev, port1);
4750
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004751 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 * udev becomes globally accessible, although presumably
4753 * no one will look at it until hdev is unlocked.
4754 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 status = 0;
4756
Dan Williamsd8521af2014-05-20 18:08:28 -07004757 mutex_lock(&usb_port_peer_mutex);
4758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 /* We mustn't add new devices if the parent hub has
4760 * been disconnected; we would race with the
4761 * recursively_mark_NOTATTACHED() routine.
4762 */
4763 spin_lock_irq(&device_state_lock);
4764 if (hdev->state == USB_STATE_NOTATTACHED)
4765 status = -ENOTCONN;
4766 else
Dan Williamsd99f6b42014-05-20 18:08:17 -07004767 port_dev->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 spin_unlock_irq(&device_state_lock);
Dan Williamsd8521af2014-05-20 18:08:28 -07004769 mutex_unlock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
4771 /* Run it through the hoops (find a driver, etc) */
4772 if (!status) {
4773 status = usb_new_device(udev);
4774 if (status) {
Dan Williamsd8521af2014-05-20 18:08:28 -07004775 mutex_lock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 spin_lock_irq(&device_state_lock);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004777 port_dev->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 spin_unlock_irq(&device_state_lock);
Dan Williamsd8521af2014-05-20 18:08:28 -07004779 mutex_unlock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 }
4781 }
4782
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 if (status)
4784 goto loop_disable;
4785
4786 status = hub_power_remaining(hub);
4787 if (status)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004788 dev_dbg(hub->intfdev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
4790 return;
4791
4792loop_disable:
4793 hub_port_disable(hub, port1, 1);
4794loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004795 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004796 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004797 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004799 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 break;
4801 }
Alan Stern3a311552008-05-20 16:58:29 -04004802 if (hub->hdev->parent ||
4803 !hcd->driver->port_handed_over ||
Alan Sterne9e88fb2013-03-27 16:14:01 -04004804 !(hcd->driver->port_handed_over)(hcd, port1)) {
4805 if (status != -ENOTCONN && status != -ENODEV)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004806 dev_err(&port_dev->dev,
4807 "unable to enumerate USB device\n");
Alan Sterne9e88fb2013-03-27 16:14:01 -04004808 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004809
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810done:
4811 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304812 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4813 hcd->driver->relinquish_port(hcd, port1);
Dan Williamsaf376a42014-05-20 18:09:15 -07004814
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815}
4816
Dan Williamsaf376a42014-05-20 18:09:15 -07004817/* Handle physical or logical connection change events.
4818 * This routine is called when:
4819 * a port connection-change occurs;
4820 * a port enable-change occurs (often caused by EMI);
4821 * usb_reset_and_verify_device() encounters changed descriptors (as from
4822 * a firmware download)
4823 * caller already locked the hub
4824 */
4825static void hub_port_connect_change(struct usb_hub *hub, int port1,
4826 u16 portstatus, u16 portchange)
Dan Williams5c79a1e2014-05-20 18:09:26 -07004827 __must_hold(&port_dev->status_lock)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004828{
Dan Williamsaf376a42014-05-20 18:09:15 -07004829 struct usb_port *port_dev = hub->ports[port1 - 1];
4830 struct usb_device *udev = port_dev->child;
4831 int status = -ENODEV;
Sarah Sharp714b07b2012-01-24 13:53:18 -08004832
Dan Williamsaf376a42014-05-20 18:09:15 -07004833 dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus,
4834 portchange, portspeed(hub, portstatus));
4835
4836 if (hub->has_indicators) {
4837 set_port_led(hub, port1, HUB_LED_AUTO);
4838 hub->indicator[port1-1] = INDICATOR_AUTO;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004839 }
4840
Dan Williamsaf376a42014-05-20 18:09:15 -07004841#ifdef CONFIG_USB_OTG
4842 /* during HNP, don't repeat the debounce */
4843 if (hub->hdev->bus->is_b_host)
4844 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4845 USB_PORT_STAT_C_ENABLE);
4846#endif
Sarah Sharp714b07b2012-01-24 13:53:18 -08004847
Dan Williamsaf376a42014-05-20 18:09:15 -07004848 /* Try to resuscitate an existing device */
4849 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4850 udev->state != USB_STATE_NOTATTACHED) {
4851 if (portstatus & USB_PORT_STAT_ENABLE) {
4852 status = 0; /* Nothing to do */
4853#ifdef CONFIG_PM_RUNTIME
4854 } else if (udev->state == USB_STATE_SUSPENDED &&
4855 udev->persist_enabled) {
4856 /* For a suspended device, treat this as a
4857 * remote wakeup event.
4858 */
Dan Williams5c79a1e2014-05-20 18:09:26 -07004859 usb_unlock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004860 status = usb_remote_wakeup(udev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004861 usb_lock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004862#endif
4863 } else {
4864 /* Don't resuscitate */;
4865 }
Sarah Sharp714b07b2012-01-24 13:53:18 -08004866 }
Dan Williamsaf376a42014-05-20 18:09:15 -07004867 clear_bit(port1, hub->change_bits);
4868
Dan Williams5c79a1e2014-05-20 18:09:26 -07004869 /* successfully revalidated the connection */
Dan Williamsaf376a42014-05-20 18:09:15 -07004870 if (status == 0)
4871 return;
4872
Dan Williams5c79a1e2014-05-20 18:09:26 -07004873 usb_unlock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004874 hub_port_connect(hub, port1, portstatus, portchange);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004875 usb_lock_port(port_dev);
Sarah Sharp714b07b2012-01-24 13:53:18 -08004876}
4877
Dan Williamsaf376a42014-05-20 18:09:15 -07004878static void port_event(struct usb_hub *hub, int port1)
Dan Williams5c79a1e2014-05-20 18:09:26 -07004879 __must_hold(&port_dev->status_lock)
Dan Williamsaf376a42014-05-20 18:09:15 -07004880{
4881 int connect_change, reset_device = 0;
4882 struct usb_port *port_dev = hub->ports[port1 - 1];
4883 struct usb_device *udev = port_dev->child;
4884 struct usb_device *hdev = hub->hdev;
4885 u16 portstatus, portchange;
4886
4887 connect_change = test_bit(port1, hub->change_bits);
4888 clear_bit(port1, hub->event_bits);
4889 clear_bit(port1, hub->wakeup_bits);
4890
4891 if (hub_port_status(hub, port1, &portstatus, &portchange) < 0)
4892 return;
4893
4894 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4895 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
4896 connect_change = 1;
4897 }
4898
4899 if (portchange & USB_PORT_STAT_C_ENABLE) {
4900 if (!connect_change)
4901 dev_dbg(&port_dev->dev, "enable change, status %08x\n",
4902 portstatus);
4903 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
4904
4905 /*
4906 * EM interference sometimes causes badly shielded USB devices
4907 * to be shutdown by the hub, this hack enables them again.
4908 * Works at least with mouse driver.
4909 */
4910 if (!(portstatus & USB_PORT_STAT_ENABLE)
4911 && !connect_change && udev) {
4912 dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n");
4913 connect_change = 1;
4914 }
4915 }
4916
4917 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
4918 u16 status = 0, unused;
4919
4920 dev_dbg(&port_dev->dev, "over-current change\n");
4921 usb_clear_port_feature(hdev, port1,
4922 USB_PORT_FEAT_C_OVER_CURRENT);
4923 msleep(100); /* Cool down */
4924 hub_power_on(hub, true);
4925 hub_port_status(hub, port1, &status, &unused);
4926 if (status & USB_PORT_STAT_OVERCURRENT)
4927 dev_err(&port_dev->dev, "over-current condition\n");
4928 }
4929
4930 if (portchange & USB_PORT_STAT_C_RESET) {
4931 dev_dbg(&port_dev->dev, "reset change\n");
4932 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET);
4933 }
4934 if ((portchange & USB_PORT_STAT_C_BH_RESET)
4935 && hub_is_superspeed(hdev)) {
4936 dev_dbg(&port_dev->dev, "warm reset change\n");
4937 usb_clear_port_feature(hdev, port1,
4938 USB_PORT_FEAT_C_BH_PORT_RESET);
4939 }
4940 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4941 dev_dbg(&port_dev->dev, "link state change\n");
4942 usb_clear_port_feature(hdev, port1,
4943 USB_PORT_FEAT_C_PORT_LINK_STATE);
4944 }
4945 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4946 dev_warn(&port_dev->dev, "config error\n");
4947 usb_clear_port_feature(hdev, port1,
4948 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4949 }
4950
Dan Williams097a1552014-05-20 18:09:20 -07004951 /* skip port actions that require the port to be powered on */
4952 if (!pm_runtime_active(&port_dev->dev))
4953 return;
4954
Dan Williamsaf376a42014-05-20 18:09:15 -07004955 if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange))
4956 connect_change = 1;
4957
4958 /*
4959 * Warm reset a USB3 protocol port if it's in
4960 * SS.Inactive state.
4961 */
Dan Williams3cd12f92014-05-29 12:58:46 -07004962 if (hub_port_warm_reset_required(hub, port1, portstatus)) {
Dan Williamsaf376a42014-05-20 18:09:15 -07004963 dev_dbg(&port_dev->dev, "do warm reset\n");
4964 if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION)
4965 || udev->state == USB_STATE_NOTATTACHED) {
4966 if (hub_port_reset(hub, port1, NULL,
4967 HUB_BH_RESET_TIME, true) < 0)
4968 hub_port_disable(hub, port1, 1);
4969 } else
4970 reset_device = 1;
4971 }
4972
4973 /*
4974 * On disconnect USB3 protocol ports transit from U0 to
4975 * SS.Inactive to Rx.Detect. If this happens a warm-
4976 * reset is not needed, but a (re)connect may happen
4977 * before khubd runs and sees the disconnect, and the
4978 * device may be an unknown state.
4979 *
4980 * If the port went through SS.Inactive without khubd
4981 * seeing it the C_LINK_STATE change flag will be set,
4982 * and we reset the dev to put it in a known state.
4983 */
4984 if (reset_device || (udev && hub_is_superspeed(hub->hdev)
4985 && (portchange & USB_PORT_STAT_C_LINK_STATE)
4986 && (portstatus & USB_PORT_STAT_CONNECTION))) {
Dan Williams5c79a1e2014-05-20 18:09:26 -07004987 usb_unlock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004988 usb_lock_device(udev);
4989 usb_reset_device(udev);
4990 usb_unlock_device(udev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004991 usb_lock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004992 connect_change = 0;
4993 }
4994
4995 if (connect_change)
4996 hub_port_connect_change(hub, port1, portstatus, portchange);
4997}
4998
4999
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000static void hub_events(void)
5001{
5002 struct list_head *tmp;
5003 struct usb_device *hdev;
5004 struct usb_interface *intf;
5005 struct usb_hub *hub;
5006 struct device *hub_dev;
5007 u16 hubstatus;
5008 u16 hubchange;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
5011 /*
5012 * We restart the list every time to avoid a deadlock with
5013 * deleting hubs downstream from this one. This should be
5014 * safe since we delete the hub from the event list.
5015 * Not the most efficient, but avoids deadlocks.
5016 */
5017 while (1) {
5018
5019 /* Grab the first entry at the beginning of the list */
5020 spin_lock_irq(&hub_event_lock);
5021 if (list_empty(&hub_event_list)) {
5022 spin_unlock_irq(&hub_event_lock);
5023 break;
5024 }
5025
5026 tmp = hub_event_list.next;
5027 list_del_init(tmp);
5028
5029 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04005030 kref_get(&hub->kref);
5031 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
Petr Mladek5d14f322014-09-19 17:32:19 +02005033 hdev = hub->hdev;
Alan Sterne8054852007-05-04 11:55:11 -04005034 hub_dev = hub->intfdev;
5035 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05005036 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +02005037 hdev->state, hdev->maxchild,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 /* NOTE: expects max 15 ports... */
5039 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05005040 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 /* Lock the device, then check to see if we were
5043 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04005044 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04005045 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05005046 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047
5048 /* If the hub has died, clean up after it */
5049 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04005050 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04005051 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 goto loop;
5053 }
5054
Alan Stern40f122f2006-11-09 14:44:33 -05005055 /* Autoresume */
5056 ret = usb_autopm_get_interface(intf);
5057 if (ret) {
5058 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05005060 }
5061
5062 /* If this is an inactive hub, do nothing */
5063 if (hub->quiescing)
5064 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
5066 if (hub->error) {
5067 dev_dbg (hub_dev, "resetting for error %d\n",
5068 hub->error);
5069
Ming Lei742120c2008-06-18 22:00:29 +08005070 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 if (ret) {
5072 dev_dbg (hub_dev,
5073 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05005074 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 }
5076
5077 hub->nerrors = 0;
5078 hub->error = 0;
5079 }
5080
5081 /* deal with port status changes */
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +02005082 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williams097a1552014-05-20 18:09:20 -07005083 struct usb_port *port_dev = hub->ports[i - 1];
Hans de Goedea82b76f2013-11-08 13:41:05 +01005084
Dan Williams5c79a1e2014-05-20 18:09:26 -07005085 if (test_bit(i, hub->event_bits)
5086 || test_bit(i, hub->change_bits)
5087 || test_bit(i, hub->wakeup_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 /*
Dan Williams097a1552014-05-20 18:09:20 -07005089 * The get_noresume and barrier ensure that if
5090 * the port was in the process of resuming, we
5091 * flush that work and keep the port active for
5092 * the duration of the port_event(). However,
5093 * if the port is runtime pm suspended
5094 * (powered-off), we leave it in that state, run
5095 * an abbreviated port_event(), and move on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 */
Dan Williams097a1552014-05-20 18:09:20 -07005097 pm_runtime_get_noresume(&port_dev->dev);
5098 pm_runtime_barrier(&port_dev->dev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07005099 usb_lock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07005100 port_event(hub, i);
Dan Williams5c79a1e2014-05-20 18:09:26 -07005101 usb_unlock_port(port_dev);
Dan Williams097a1552014-05-20 18:09:20 -07005102 pm_runtime_put_sync(&port_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 }
Dan Williamsaf376a42014-05-20 18:09:15 -07005104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105
5106 /* deal with hub status changes */
5107 if (test_and_clear_bit(0, hub->event_bits) == 0)
5108 ; /* do nothing */
5109 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
5110 dev_err (hub_dev, "get_hub_status failed\n");
5111 else {
5112 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
5113 dev_dbg (hub_dev, "power change\n");
5114 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05005115 if (hubstatus & HUB_STATUS_LOCAL_POWER)
5116 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05005117 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08005118 else
5119 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 }
5121 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01005122 u16 status = 0;
5123 u16 unused;
5124
5125 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01005127 msleep(500); /* Cool down */
Matthias Beyer469271f2013-10-10 23:41:27 +02005128 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01005129 hub_hub_status(hub, &status, &unused);
5130 if (status & HUB_STATUS_OVERCURRENT)
5131 dev_err(hub_dev, "over-current "
5132 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 }
5134 }
5135
Alan Stern8e4ceb32009-12-07 13:01:37 -05005136 loop_autopm:
5137 /* Balance the usb_autopm_get_interface() above */
5138 usb_autopm_put_interface_no_suspend(intf);
5139 loop:
5140 /* Balance the usb_autopm_get_interface_no_resume() in
5141 * kick_khubd() and allow autosuspend.
5142 */
5143 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05005144 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04005146 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
Matthias Beyer469271f2013-10-10 23:41:27 +02005148 } /* end while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149}
5150
5151static int hub_thread(void *__unused)
5152{
Rahul Bedarkar025d4432014-01-04 11:24:41 +05305153 /* khubd needs to be freezable to avoid interfering with USB-PERSIST
Alan Stern3bb1af52008-03-03 15:15:36 -05005154 * port handover. Otherwise it might see that a full-speed device
5155 * was gone before the EHCI controller had handed its port over to
5156 * the companion full-speed controller.
5157 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07005158 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05005159
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 do {
5161 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07005162 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07005163 !list_empty(&hub_event_list) ||
5164 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07005165 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07005167 pr_debug("%s: khubd exiting\n", usbcore_name);
5168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169}
5170
Németh Márton1e927d92010-01-10 15:34:53 +01005171static const struct usb_device_id hub_id_table[] = {
Ming Leie6f30de2012-10-24 11:59:24 +08005172 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
Matthias Beyer469271f2013-10-10 23:41:27 +02005173 | USB_DEVICE_ID_MATCH_INT_CLASS,
Ming Leie6f30de2012-10-24 11:59:24 +08005174 .idVendor = USB_VENDOR_GENESYS_LOGIC,
5175 .bInterfaceClass = USB_CLASS_HUB,
5176 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
5178 .bDeviceClass = USB_CLASS_HUB},
5179 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
5180 .bInterfaceClass = USB_CLASS_HUB},
5181 { } /* Terminating entry */
5182};
5183
5184MODULE_DEVICE_TABLE (usb, hub_id_table);
5185
5186static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 .name = "hub",
5188 .probe = hub_probe,
5189 .disconnect = hub_disconnect,
5190 .suspend = hub_suspend,
5191 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04005192 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04005193 .pre_reset = hub_pre_reset,
5194 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02005195 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05005197 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198};
5199
5200int usb_hub_init(void)
5201{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 if (usb_register(&hub_driver) < 0) {
5203 printk(KERN_ERR "%s: can't register hub driver\n",
5204 usbcore_name);
5205 return -1;
5206 }
5207
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07005208 khubd_task = kthread_run(hub_thread, NULL, "khubd");
5209 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211
5212 /* Fall through if kernel_thread failed */
5213 usb_deregister(&hub_driver);
5214 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
5215
5216 return -1;
5217}
5218
5219void usb_hub_cleanup(void)
5220{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07005221 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222
5223 /*
5224 * Hub resources are freed for us by usb_deregister. It calls
5225 * usb_driver_purge on every device which in turn calls that
5226 * devices disconnect function if it is using this driver.
5227 * The hub_disconnect function takes care of releasing the
5228 * individual hub resources. -greg
5229 */
5230 usb_deregister(&hub_driver);
5231} /* usb_hub_cleanup() */
5232
Alan Sterneb764c42008-03-03 15:16:04 -05005233static int descriptors_changed(struct usb_device *udev,
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005234 struct usb_device_descriptor *old_device_descriptor,
5235 struct usb_host_bos *old_bos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236{
Alan Sterneb764c42008-03-03 15:16:04 -05005237 int changed = 0;
5238 unsigned index;
5239 unsigned serial_len = 0;
5240 unsigned len;
5241 unsigned old_length;
5242 int length;
5243 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244
Alan Sterneb764c42008-03-03 15:16:04 -05005245 if (memcmp(&udev->descriptor, old_device_descriptor,
5246 sizeof(*old_device_descriptor)) != 0)
5247 return 1;
5248
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005249 if ((old_bos && !udev->bos) || (!old_bos && udev->bos))
5250 return 1;
5251 if (udev->bos) {
Xenia Ragiadakoub9a10482013-08-31 04:40:49 +03005252 len = le16_to_cpu(udev->bos->desc->wTotalLength);
5253 if (len != le16_to_cpu(old_bos->desc->wTotalLength))
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005254 return 1;
Xenia Ragiadakoub9a10482013-08-31 04:40:49 +03005255 if (memcmp(udev->bos->desc, old_bos->desc, len))
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005256 return 1;
5257 }
5258
Alan Sterneb764c42008-03-03 15:16:04 -05005259 /* Since the idVendor, idProduct, and bcdDevice values in the
5260 * device descriptor haven't changed, we will assume the
5261 * Manufacturer and Product strings haven't changed either.
5262 * But the SerialNumber string could be different (e.g., a
5263 * different flash card of the same brand).
5264 */
5265 if (udev->serial)
5266 serial_len = strlen(udev->serial) + 1;
5267
5268 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05005270 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
5271 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 }
Alan Sterneb764c42008-03-03 15:16:04 -05005273
Oliver Neukum0cc1a512008-01-10 10:31:48 +01005274 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 if (buf == NULL) {
5276 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
5277 /* assume the worst */
5278 return 1;
5279 }
5280 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05005281 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
5283 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05005284 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 dev_dbg(&udev->dev, "config index %d, error %d\n",
5286 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05005287 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 break;
5289 }
5290 if (memcmp (buf, udev->rawdescriptors[index], old_length)
5291 != 0) {
5292 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05005293 index,
5294 ((struct usb_config_descriptor *) buf)->
5295 bConfigurationValue);
5296 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 break;
5298 }
5299 }
Alan Sterneb764c42008-03-03 15:16:04 -05005300
5301 if (!changed && serial_len) {
5302 length = usb_string(udev, udev->descriptor.iSerialNumber,
5303 buf, serial_len);
5304 if (length + 1 != serial_len) {
5305 dev_dbg(&udev->dev, "serial string error %d\n",
5306 length);
5307 changed = 1;
5308 } else if (memcmp(buf, udev->serial, length) != 0) {
5309 dev_dbg(&udev->dev, "serial string changed\n");
5310 changed = 1;
5311 }
5312 }
5313
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05005315 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316}
5317
5318/**
Ming Lei742120c2008-06-18 22:00:29 +08005319 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5321 *
Alan Stern79efa092006-06-01 13:33:42 -04005322 * WARNING - don't use this routine to reset a composite device
5323 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08005324 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 *
5326 * Do a port reset, reassign the device's address, and establish its
5327 * former operating configuration. If the reset fails, or the device's
5328 * descriptors change from their values before the reset, or the original
5329 * configuration and altsettings cannot be restored, a flag will be set
5330 * telling khubd to pretend the device has been disconnected and then
5331 * re-connected. All drivers will be unbound, and the device will be
5332 * re-enumerated and probed all over again.
5333 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005334 * Return: 0 if the reset succeeded, -ENODEV if the device has been
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 * flagged for logical disconnection, or some other negative error code
5336 * if the reset wasn't even attempted.
5337 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005338 * Note:
Dan Williams5c79a1e2014-05-20 18:09:26 -07005339 * The caller must own the device lock and the port lock, the latter is
5340 * taken by usb_reset_device(). For example, it's safe to use
5341 * usb_reset_device() from a driver probe() routine after downloading
5342 * new firmware. For calls that might not occur during probe(), drivers
5343 * should lock the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04005344 *
5345 * Locking exception: This routine may also be called from within an
5346 * autoresume handler. Such usage won't conflict with other tasks
5347 * holding the device lock because these tasks should always call
Dan Williams5c79a1e2014-05-20 18:09:26 -07005348 * usb_autopm_resume_device(), thereby preventing any unwanted
5349 * autoresume. The autoresume handler is expected to have already
5350 * acquired the port lock before calling this routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 */
Ming Lei742120c2008-06-18 22:00:29 +08005352static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353{
5354 struct usb_device *parent_hdev = udev->parent;
5355 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005356 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 struct usb_device_descriptor descriptor = udev->descriptor;
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005358 struct usb_host_bos *bos;
Hans de Goede7a7b5622013-11-08 16:37:26 +01005359 int i, j, ret = 0;
Alan Stern12c3da32005-11-23 12:09:52 -05005360 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
5362 if (udev->state == USB_STATE_NOTATTACHED ||
5363 udev->state == USB_STATE_SUSPENDED) {
5364 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5365 udev->state);
5366 return -EINVAL;
5367 }
5368
Dan Williams5c79a1e2014-05-20 18:09:26 -07005369 if (!parent_hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 return -EISDIR;
Dan Williams5c79a1e2014-05-20 18:09:26 -07005371
Lan Tianyuad493e52013-01-23 04:26:30 +08005372 parent_hub = usb_hub_to_struct_hub(parent_hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373
Sarah Sharpdcc01c02013-09-30 17:26:29 +03005374 /* Disable USB2 hardware LPM.
5375 * It will be re-enabled by the enumeration process.
5376 */
5377 if (udev->usb2_hw_lpm_enabled == 1)
5378 usb_set_usb2_hardware_lpm(udev, 0);
5379
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005380 bos = udev->bos;
5381 udev->bos = NULL;
5382
Sarah Sharpf74631e2012-06-25 12:08:08 -07005383 /* Disable LPM and LTM while we reset the device and reinstall the alt
5384 * settings. Device-initiated LPM settings, and system exit latency
5385 * settings are cleared when the device is reset, so we have to set
5386 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005387 */
5388 ret = usb_unlocked_disable_lpm(udev);
5389 if (ret) {
5390 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
5391 goto re_enumerate;
5392 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07005393 ret = usb_disable_ltm(udev);
5394 if (ret) {
5395 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
5396 __func__);
5397 goto re_enumerate;
5398 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005399
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
5401
5402 /* ep0 maxpacket size may change; let the HCD know about it.
5403 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07005404 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04005406 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 break;
5408 }
Alan Sternd5cbad42006-08-11 16:52:39 -04005409
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 if (ret < 0)
5411 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005412
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 /* Device might have changed firmware (DFU or similar) */
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005414 if (descriptors_changed(udev, &descriptor, bos)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 dev_info(&udev->dev, "device firmware changed\n");
5416 udev->descriptor = descriptor; /* for disconnect() calls */
5417 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005418 }
Alan Stern4fe03872009-02-26 10:21:02 -05005419
5420 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 if (!udev->actconfig)
5422 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005423
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005424 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005425 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
5426 if (ret < 0) {
5427 dev_warn(&udev->dev,
5428 "Busted HC? Not enough HCD resources for "
5429 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005430 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005431 goto re_enumerate;
5432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
5434 USB_REQ_SET_CONFIGURATION, 0,
5435 udev->actconfig->desc.bConfigurationValue, 0,
5436 NULL, 0, USB_CTRL_SET_TIMEOUT);
5437 if (ret < 0) {
5438 dev_err(&udev->dev,
5439 "can't restore configuration #%d (error=%d)\n",
5440 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005441 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005443 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005444 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 usb_set_device_state(udev, USB_STATE_CONFIGURED);
5446
Alan Stern4fe03872009-02-26 10:21:02 -05005447 /* Put interfaces back into the same altsettings as before.
5448 * Don't bother to send the Set-Interface request for interfaces
5449 * that were already in altsetting 0; besides being unnecessary,
5450 * many devices can't handle it. Instead just reset the host-side
5451 * endpoint state.
5452 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005454 struct usb_host_config *config = udev->actconfig;
5455 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 struct usb_interface_descriptor *desc;
5457
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05005459 if (desc->bAlternateSetting == 0) {
5460 usb_disable_interface(udev, intf, true);
5461 usb_enable_interface(udev, intf, true);
5462 ret = 0;
5463 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08005464 /* Let the bandwidth allocation function know that this
5465 * device has been reset, and it will have to use
5466 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005467 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08005468 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05005469 ret = usb_set_interface(udev, desc->bInterfaceNumber,
5470 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08005471 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05005472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 if (ret < 0) {
5474 dev_err(&udev->dev, "failed to restore interface %d "
5475 "altsetting %d (error=%d)\n",
5476 desc->bInterfaceNumber,
5477 desc->bAlternateSetting,
5478 ret);
5479 goto re_enumerate;
5480 }
Hans de Goede7a7b5622013-11-08 16:37:26 +01005481 /* Resetting also frees any allocated streams */
5482 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++)
5483 intf->cur_altsetting->endpoint[j].streams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 }
5485
5486done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07005487 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Sarah Sharpde68bab2013-09-30 17:26:28 +03005488 usb_set_usb2_hardware_lpm(udev, 1);
Alan Stern78d9a482008-06-23 16:00:40 -04005489 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07005490 usb_enable_ltm(udev);
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005491 usb_release_bos_descriptor(udev);
5492 udev->bos = bos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 return 0;
Matthias Beyer469271f2013-10-10 23:41:27 +02005494
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005496 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 hub_port_logical_disconnect(parent_hub, port1);
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005498 usb_release_bos_descriptor(udev);
5499 udev->bos = bos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501}
5502
5503/**
5504 * usb_reset_device - warn interface drivers and perform a USB port reset
5505 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5506 *
Alan Stern79efa092006-06-01 13:33:42 -04005507 * Warns all drivers bound to registered interfaces (using their pre_reset
5508 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08005509 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04005510 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005511 * Return: The same as for usb_reset_and_verify_device().
Alan Stern79efa092006-06-01 13:33:42 -04005512 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005513 * Note:
Alan Stern79efa092006-06-01 13:33:42 -04005514 * The caller must own the device lock. For example, it's safe to use
5515 * this from a driver probe() routine after downloading new firmware.
5516 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08005517 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04005518 *
5519 * If an interface is currently being probed or disconnected, we assume
5520 * its driver knows how to handle resets. For all other interfaces,
5521 * if the driver doesn't have pre_reset and post_reset methods then
5522 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04005523 */
Ming Lei742120c2008-06-18 22:00:29 +08005524int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04005525{
5526 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05005527 int i;
Ming Lei4d769de2013-02-22 16:34:22 -08005528 unsigned int noio_flag;
Dan Williams5c79a1e2014-05-20 18:09:26 -07005529 struct usb_port *port_dev;
Alan Stern79efa092006-06-01 13:33:42 -04005530 struct usb_host_config *config = udev->actconfig;
Dan Williams5c79a1e2014-05-20 18:09:26 -07005531 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
Alan Stern79efa092006-06-01 13:33:42 -04005532
5533 if (udev->state == USB_STATE_NOTATTACHED ||
5534 udev->state == USB_STATE_SUSPENDED) {
5535 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5536 udev->state);
5537 return -EINVAL;
5538 }
5539
Dan Williams5c79a1e2014-05-20 18:09:26 -07005540 if (!udev->parent) {
5541 /* this requires hcd-specific logic; see ohci_restart() */
5542 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
5543 return -EISDIR;
5544 }
5545
5546 port_dev = hub->ports[udev->portnum - 1];
5547
Ming Lei4d769de2013-02-22 16:34:22 -08005548 /*
5549 * Don't allocate memory with GFP_KERNEL in current
5550 * context to avoid possible deadlock if usb mass
5551 * storage interface or usbnet interface(iSCSI case)
5552 * is included in current configuration. The easist
5553 * approach is to do it for every device reset,
5554 * because the device 'memalloc_noio' flag may have
5555 * not been set before reseting the usb device.
5556 */
5557 noio_flag = memalloc_noio_save();
5558
Alan Stern645daaa2006-08-30 15:47:02 -04005559 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05005560 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04005561
Alan Stern79efa092006-06-01 13:33:42 -04005562 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005563 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005564 struct usb_interface *cintf = config->interface[i];
5565 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005566 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05005567
5568 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005569 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04005570 if (drv->pre_reset && drv->post_reset)
5571 unbind = (drv->pre_reset)(cintf);
5572 else if (cintf->condition ==
5573 USB_INTERFACE_BOUND)
5574 unbind = 1;
5575 if (unbind)
5576 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005577 }
5578 }
5579 }
5580
Dan Williams5c79a1e2014-05-20 18:09:26 -07005581 usb_lock_port(port_dev);
Ming Lei742120c2008-06-18 22:00:29 +08005582 ret = usb_reset_and_verify_device(udev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07005583 usb_unlock_port(port_dev);
Alan Stern79efa092006-06-01 13:33:42 -04005584
5585 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005586 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005587 struct usb_interface *cintf = config->interface[i];
5588 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005589 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05005590
Alan Stern78d9a482008-06-23 16:00:40 -04005591 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005592 drv = to_usb_driver(cintf->dev.driver);
5593 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04005594 rebind = (drv->post_reset)(cintf);
5595 else if (cintf->condition ==
5596 USB_INTERFACE_BOUND)
5597 rebind = 1;
Alan Stern6aec0442014-03-12 11:30:38 -04005598 if (rebind)
5599 cintf->needs_binding = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005600 }
Alan Stern79efa092006-06-01 13:33:42 -04005601 }
Alan Stern6aec0442014-03-12 11:30:38 -04005602 usb_unbind_and_rebind_marked_interfaces(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005603 }
5604
Alan Stern94fcda12006-11-20 11:38:46 -05005605 usb_autosuspend_device(udev);
Ming Lei4d769de2013-02-22 16:34:22 -08005606 memalloc_noio_restore(noio_flag);
Alan Stern79efa092006-06-01 13:33:42 -04005607 return ret;
5608}
Ming Lei742120c2008-06-18 22:00:29 +08005609EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005610
5611
5612/**
5613 * usb_queue_reset_device - Reset a USB device from an atomic context
5614 * @iface: USB interface belonging to the device to reset
5615 *
5616 * This function can be used to reset a USB device from an atomic
5617 * context, where usb_reset_device() won't work (as it blocks).
5618 *
5619 * Doing a reset via this method is functionally equivalent to calling
5620 * usb_reset_device(), except for the fact that it is delayed to a
5621 * workqueue. This means that any drivers bound to other interfaces
5622 * might be unbound, as well as users from usbfs in user space.
5623 *
5624 * Corner cases:
5625 *
5626 * - Scheduling two resets at the same time from two different drivers
5627 * attached to two different interfaces of the same device is
5628 * possible; depending on how the driver attached to each interface
5629 * handles ->pre_reset(), the second reset might happen or not.
5630 *
5631 * - If a driver is unbound and it had a pending reset, the reset will
5632 * be cancelled.
5633 *
5634 * - This function can be called during .probe() or .disconnect()
5635 * times. On return from .disconnect(), any pending resets will be
5636 * cancelled.
5637 *
5638 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5639 * does its own.
5640 *
5641 * NOTE: We don't do any reference count tracking because it is not
5642 * needed. The lifecycle of the work_struct is tied to the
5643 * usb_interface. Before destroying the interface we cancel the
5644 * work_struct, so the fact that work_struct is queued and or
5645 * running means the interface (and thus, the device) exist and
5646 * are referenced.
5647 */
5648void usb_queue_reset_device(struct usb_interface *iface)
5649{
5650 schedule_work(&iface->reset_ws);
5651}
5652EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c792012-09-05 13:44:32 +08005653
5654/**
5655 * usb_hub_find_child - Get the pointer of child device
5656 * attached to the port which is specified by @port1.
5657 * @hdev: USB device belonging to the usb hub
5658 * @port1: port num to indicate which port the child device
5659 * is attached to.
5660 *
5661 * USB drivers call this function to get hub's child device
5662 * pointer.
5663 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005664 * Return: %NULL if input param is invalid and
Lan Tianyuff823c792012-09-05 13:44:32 +08005665 * child's usb_device pointer if non-NULL.
5666 */
5667struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5668 int port1)
5669{
Lan Tianyuad493e52013-01-23 04:26:30 +08005670 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyuff823c792012-09-05 13:44:32 +08005671
5672 if (port1 < 1 || port1 > hdev->maxchild)
5673 return NULL;
5674 return hub->ports[port1 - 1]->child;
5675}
5676EXPORT_SYMBOL_GPL(usb_hub_find_child);
Lan Tianyud5575422012-09-05 13:44:33 +08005677
Lan Tianyud2123fd2013-01-21 22:18:00 +08005678void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
5679 struct usb_hub_descriptor *desc)
5680{
Dan Williamsd99f6b42014-05-20 18:08:17 -07005681 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyud2123fd2013-01-21 22:18:00 +08005682 enum usb_port_connect_type connect_type;
5683 int i;
5684
Dan Williamsd99f6b42014-05-20 18:08:17 -07005685 if (!hub)
5686 return;
5687
Lan Tianyud2123fd2013-01-21 22:18:00 +08005688 if (!hub_is_superspeed(hdev)) {
5689 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005690 struct usb_port *port_dev = hub->ports[i - 1];
Lan Tianyud2123fd2013-01-21 22:18:00 +08005691
Dan Williamsd99f6b42014-05-20 18:08:17 -07005692 connect_type = port_dev->connect_type;
Lan Tianyud2123fd2013-01-21 22:18:00 +08005693 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5694 u8 mask = 1 << (i%8);
5695
5696 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005697 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
Lan Tianyud2123fd2013-01-21 22:18:00 +08005698 desc->u.hs.DeviceRemovable[i/8] |= mask;
5699 }
5700 }
5701 }
5702 } else {
5703 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable);
5704
5705 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005706 struct usb_port *port_dev = hub->ports[i - 1];
Lan Tianyud2123fd2013-01-21 22:18:00 +08005707
Dan Williamsd99f6b42014-05-20 18:08:17 -07005708 connect_type = port_dev->connect_type;
Lan Tianyud2123fd2013-01-21 22:18:00 +08005709 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5710 u16 mask = 1 << i;
5711
5712 if (!(port_removable & mask)) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005713 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
Lan Tianyud2123fd2013-01-21 22:18:00 +08005714 port_removable |= mask;
5715 }
5716 }
5717 }
5718
5719 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
5720 }
5721}
5722
Lan Tianyud5575422012-09-05 13:44:33 +08005723#ifdef CONFIG_ACPI
5724/**
5725 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5726 * @hdev: USB device belonging to the usb hub
5727 * @port1: port num of the port
5728 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005729 * Return: Port's acpi handle if successful, %NULL if params are
5730 * invalid.
Lan Tianyud5575422012-09-05 13:44:33 +08005731 */
5732acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5733 int port1)
5734{
Lan Tianyuad493e52013-01-23 04:26:30 +08005735 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyud5575422012-09-05 13:44:33 +08005736
Mathias Nyman413412612013-06-18 17:28:48 +03005737 if (!hub)
5738 return NULL;
5739
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +01005740 return ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
Lan Tianyud5575422012-09-05 13:44:33 +08005741}
5742#endif