blob: d5419292f89f9484a437b03b7cdc6555954173cd [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
1638 usb_put_intf(to_usb_interface(hub->intfdev));
1639 kfree(hub);
1640}
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642static unsigned highspeed_hubs;
1643
1644static void hub_disconnect(struct usb_interface *intf)
1645{
Huajun Li88162302012-03-12 21:00:19 +08001646 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001647 struct usb_device *hdev = interface_to_usbdev(intf);
Alan Stern543d7782014-01-07 10:43:02 -05001648 int port1;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001649
Alan Sterne8054852007-05-04 11:55:11 -04001650 /* Take the hub off the event list and don't let it be added again */
1651 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001652 if (!list_empty(&hub->event_list)) {
1653 list_del_init(&hub->event_list);
1654 usb_autopm_put_interface_no_suspend(intf);
1655 }
Alan Sterne8054852007-05-04 11:55:11 -04001656 hub->disconnected = 1;
1657 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Alan Stern7de18d82006-06-01 13:37:24 -04001659 /* Disconnect all children and quiesce the hub */
1660 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001661 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001662
Dan Williamsd8521af2014-05-20 18:08:28 -07001663 mutex_lock(&usb_port_peer_mutex);
1664
Alan Stern543d7782014-01-07 10:43:02 -05001665 /* Avoid races with recursively_mark_NOTATTACHED() */
1666 spin_lock_irq(&device_state_lock);
1667 port1 = hdev->maxchild;
1668 hdev->maxchild = 0;
1669 usb_set_intfdata(intf, NULL);
1670 spin_unlock_irq(&device_state_lock);
Alexander Shishkin1f2235b2012-09-12 14:48:31 +03001671
Alan Stern543d7782014-01-07 10:43:02 -05001672 for (; port1 > 0; --port1)
1673 usb_hub_remove_port_device(hub, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Dan Williamsd8521af2014-05-20 18:08:28 -07001675 mutex_unlock(&usb_port_peer_mutex);
1676
Alan Sterne8054852007-05-04 11:55:11 -04001677 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 highspeed_hubs--;
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 usb_free_urb(hub->urb);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001681 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001682 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001683 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001684 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Lan Tianyu971fcd42013-01-23 04:26:29 +08001686 pm_suspend_ignore_children(&intf->dev, false);
Alan Sterne8054852007-05-04 11:55:11 -04001687 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
1690static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1691{
1692 struct usb_host_interface *desc;
1693 struct usb_endpoint_descriptor *endpoint;
1694 struct usb_device *hdev;
1695 struct usb_hub *hub;
1696
1697 desc = intf->cur_altsetting;
1698 hdev = interface_to_usbdev(intf);
1699
Ming Lei596d7892012-10-24 11:59:25 +08001700 /*
1701 * Set default autosuspend delay as 0 to speedup bus suspend,
1702 * based on the below considerations:
1703 *
1704 * - Unlike other drivers, the hub driver does not rely on the
1705 * autosuspend delay to provide enough time to handle a wakeup
1706 * event, and the submitted status URB is just to check future
1707 * change on hub downstream ports, so it is safe to do it.
1708 *
1709 * - The patch might cause one or more auto supend/resume for
1710 * below very rare devices when they are plugged into hub
1711 * first time:
1712 *
1713 * devices having trouble initializing, and disconnect
1714 * themselves from the bus and then reconnect a second
1715 * or so later
1716 *
1717 * devices just for downloading firmware, and disconnects
1718 * themselves after completing it
1719 *
1720 * For these quite rare devices, their drivers may change the
1721 * autosuspend delay of their parent hub in the probe() to one
1722 * appropriate value to avoid the subtle problem if someone
1723 * does care it.
1724 *
1725 * - The patch may cause one or more auto suspend/resume on
1726 * hub during running 'lsusb', but it is probably too
1727 * infrequent to worry about.
1728 *
1729 * - Change autosuspend delay of hub can avoid unnecessary auto
1730 * suspend timer for hub, also may decrease power consumption
1731 * of USB bus.
Roger Quadrosbdd405d2014-08-04 12:44:46 +03001732 *
1733 * - If user has indicated to prevent autosuspend by passing
1734 * usbcore.autosuspend = -1 then keep autosuspend disabled.
Ming Lei596d7892012-10-24 11:59:25 +08001735 */
Greg Kroah-Hartmana9ef8032014-08-27 16:55:29 -07001736#ifdef CONFIG_PM_RUNTIME
Roger Quadrosbdd405d2014-08-04 12:44:46 +03001737 if (hdev->dev.power.autosuspend_delay >= 0)
1738 pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
Greg Kroah-Hartmana9ef8032014-08-27 16:55:29 -07001739#endif
Ming Lei596d7892012-10-24 11:59:25 +08001740
Alan Stern8ef42dd2014-05-23 10:45:54 -04001741 /*
1742 * Hubs have proper suspend/resume support, except for root hubs
1743 * where the controller driver doesn't have bus_suspend and
1744 * bus_resume methods.
1745 */
1746 if (hdev->parent) { /* normal device */
1747 usb_enable_autosuspend(hdev);
1748 } else { /* root hub */
1749 const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver;
1750
1751 if (drv->bus_suspend && drv->bus_resume)
1752 usb_enable_autosuspend(hdev);
1753 }
Alan Stern088f7fe2010-01-08 12:56:54 -05001754
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001755 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001756 dev_err(&intf->dev,
1757 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001758 return -E2BIG;
1759 }
1760
David Brownell89ccbdc2006-04-02 10:18:09 -08001761#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1762 if (hdev->parent) {
1763 dev_warn(&intf->dev, "ignoring external hub\n");
1764 return -ENODEV;
1765 }
1766#endif
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 /* Some hubs have a subclass of 1, which AFAICT according to the */
1769 /* specs is not defined, but it works */
1770 if ((desc->desc.bInterfaceSubClass != 0) &&
1771 (desc->desc.bInterfaceSubClass != 1)) {
1772descriptor_error:
1773 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1774 return -EIO;
1775 }
1776
1777 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1778 if (desc->desc.bNumEndpoints != 1)
1779 goto descriptor_error;
1780
1781 endpoint = &desc->endpoint[0].desc;
1782
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001783 /* If it's not an interrupt in endpoint, we'd better punt! */
1784 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto descriptor_error;
1786
1787 /* We found a hub */
1788 dev_info (&intf->dev, "USB hub found\n");
1789
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001790 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (!hub) {
1792 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1793 return -ENOMEM;
1794 }
1795
Alan Sterne8054852007-05-04 11:55:11 -04001796 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 INIT_LIST_HEAD(&hub->event_list);
1798 hub->intfdev = &intf->dev;
1799 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001800 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001801 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001802 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001805 intf->needs_remote_wakeup = 1;
Lan Tianyu971fcd42013-01-23 04:26:29 +08001806 pm_suspend_ignore_children(&intf->dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 if (hdev->speed == USB_SPEED_HIGH)
1809 highspeed_hubs++;
1810
Ming Leie6f30de2012-10-24 11:59:24 +08001811 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1812 hub->quirk_check_port_auto_suspend = 1;
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if (hub_configure(hub, endpoint) >= 0)
1815 return 0;
1816
1817 hub_disconnect (intf);
1818 return -ENODEV;
1819}
1820
1821static int
1822hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1823{
1824 struct usb_device *hdev = interface_to_usbdev (intf);
Lan Tianyuad493e52013-01-23 04:26:30 +08001825 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 /* assert ifno == 0 (part of hub spec) */
1828 switch (code) {
1829 case USBDEVFS_HUB_PORTINFO: {
1830 struct usbdevfs_hub_portinfo *info = user_data;
1831 int i;
1832
1833 spin_lock_irq(&device_state_lock);
1834 if (hdev->devnum <= 0)
1835 info->nports = 0;
1836 else {
1837 info->nports = hdev->maxchild;
1838 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001839 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 info->port[i] = 0;
1841 else
1842 info->port[i] =
Lan Tianyuff823c792012-09-05 13:44:32 +08001843 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
1845 }
1846 spin_unlock_irq(&device_state_lock);
1847
1848 return info->nports + 1;
1849 }
1850
1851 default:
1852 return -ENOSYS;
1853 }
1854}
1855
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001856/*
1857 * Allow user programs to claim ports on a hub. When a device is attached
1858 * to one of these "claimed" ports, the program will "own" the device.
1859 */
1860static int find_port_owner(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001861 struct usb_dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001862{
Mathias Nyman413412612013-06-18 17:28:48 +03001863 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
1864
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001865 if (hdev->state == USB_STATE_NOTATTACHED)
1866 return -ENODEV;
1867 if (port1 == 0 || port1 > hdev->maxchild)
1868 return -EINVAL;
1869
Mathias Nyman413412612013-06-18 17:28:48 +03001870 /* Devices not managed by the hub driver
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001871 * will always have maxchild equal to 0.
1872 */
Mathias Nyman413412612013-06-18 17:28:48 +03001873 *ppowner = &(hub->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001874 return 0;
1875}
1876
1877/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001878int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001879 struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001880{
1881 int rc;
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001882 struct usb_dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001883
1884 rc = find_port_owner(hdev, port1, &powner);
1885 if (rc)
1886 return rc;
1887 if (*powner)
1888 return -EBUSY;
1889 *powner = owner;
1890 return rc;
1891}
Valentina Manea6080cd02014-03-08 14:53:34 +02001892EXPORT_SYMBOL_GPL(usb_hub_claim_port);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001893
Lan Tianyu336c5c32012-07-06 14:13:52 +08001894int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001895 struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001896{
1897 int rc;
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001898 struct usb_dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001899
1900 rc = find_port_owner(hdev, port1, &powner);
1901 if (rc)
1902 return rc;
1903 if (*powner != owner)
1904 return -ENOENT;
1905 *powner = NULL;
1906 return rc;
1907}
Valentina Manea6080cd02014-03-08 14:53:34 +02001908EXPORT_SYMBOL_GPL(usb_hub_release_port);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001909
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001910void usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001911{
Lan Tianyuad493e52013-01-23 04:26:30 +08001912 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001913 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001914
Lan Tianyufa2a9562012-09-05 13:44:31 +08001915 for (n = 0; n < hdev->maxchild; n++) {
1916 if (hub->ports[n]->port_owner == owner)
1917 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001918 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001919
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001920}
1921
1922/* The caller must hold udev's lock */
1923bool usb_device_is_owned(struct usb_device *udev)
1924{
1925 struct usb_hub *hub;
1926
1927 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1928 return false;
Lan Tianyuad493e52013-01-23 04:26:30 +08001929 hub = usb_hub_to_struct_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001930 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001931}
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1934{
Lan Tianyuad493e52013-01-23 04:26:30 +08001935 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 int i;
1937
1938 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001939 if (hub->ports[i]->child)
1940 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001942 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001943 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 udev->state = USB_STATE_NOTATTACHED;
1945}
1946
1947/**
1948 * usb_set_device_state - change a device's current state (usbcore, hcds)
1949 * @udev: pointer to device whose state should be changed
1950 * @new_state: new state value to be stored
1951 *
1952 * udev->state is _not_ fully protected by the device lock. Although
1953 * most transitions are made only while holding the lock, the state can
1954 * can change to USB_STATE_NOTATTACHED at almost any time. This
1955 * is so that devices can be marked as disconnected as soon as possible,
1956 * without having to wait for any semaphores to be released. As a result,
1957 * all changes to any device's state must be protected by the
1958 * device_state_lock spinlock.
1959 *
1960 * Once a device has been added to the device tree, all changes to its state
1961 * should be made using this routine. The state should _not_ be set directly.
1962 *
1963 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1964 * Otherwise udev->state is set to new_state, and if new_state is
1965 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1966 * to USB_STATE_NOTATTACHED.
1967 */
1968void usb_set_device_state(struct usb_device *udev,
1969 enum usb_device_state new_state)
1970{
1971 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001972 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 spin_lock_irqsave(&device_state_lock, flags);
1975 if (udev->state == USB_STATE_NOTATTACHED)
1976 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001977 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001978
1979 /* root hub wakeup capabilities are managed out-of-band
1980 * and may involve silicon errata ... ignore them here.
1981 */
1982 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001983 if (udev->state == USB_STATE_SUSPENDED
1984 || new_state == USB_STATE_SUSPENDED)
1985 ; /* No change to wakeup settings */
1986 else if (new_state == USB_STATE_CONFIGURED)
Lu Baoluddbe1fc2014-09-19 10:13:50 +08001987 wakeup = (udev->quirks &
1988 USB_QUIRK_IGNORE_REMOTE_WAKEUP) ? 0 :
1989 udev->actconfig->desc.bmAttributes &
1990 USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001991 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001992 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001993 }
Sarah Sharp15123002007-12-21 16:54:15 -08001994 if (udev->state == USB_STATE_SUSPENDED &&
1995 new_state != USB_STATE_SUSPENDED)
1996 udev->active_duration -= jiffies;
1997 else if (new_state == USB_STATE_SUSPENDED &&
1998 udev->state != USB_STATE_SUSPENDED)
1999 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04002000 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07002001 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 recursively_mark_NOTATTACHED(udev);
2003 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01002004 if (wakeup >= 0)
2005 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
David Vrabel6da9c992009-02-18 14:43:47 +00002007EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002009/*
Alan Stern3b29b682011-02-22 09:53:41 -05002010 * Choose a device number.
2011 *
2012 * Device numbers are used as filenames in usbfs. On USB-1.1 and
2013 * USB-2.0 buses they are also used as device addresses, however on
2014 * USB-3.0 buses the address is assigned by the controller hardware
2015 * and it usually is not the same as the device number.
2016 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002017 * WUSB devices are simple: they have no hubs behind, so the mapping
2018 * device <-> virtual port number becomes 1:1. Why? to simplify the
2019 * life of the device connection logic in
2020 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
2021 * handshake we need to assign a temporary address in the unauthorized
2022 * space. For simplicity we use the first virtual port number found to
2023 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
2024 * and that becomes it's address [X < 128] or its unauthorized address
2025 * [X | 0x80].
2026 *
2027 * We add 1 as an offset to the one-based USB-stack port number
2028 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
2029 * 0 is reserved by USB for default address; (b) Linux's USB stack
2030 * uses always #1 for the root hub of the controller. So USB stack's
2031 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07002032 *
2033 * Devices connected under xHCI are not as simple. The host controller
2034 * supports virtualization, so the hardware assigns device addresses and
2035 * the HCD must setup data structures before issuing a set address
2036 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002037 */
Alan Stern3b29b682011-02-22 09:53:41 -05002038static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
2040 int devnum;
2041 struct usb_bus *bus = udev->bus;
2042
2043 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002044 if (udev->wusb) {
2045 devnum = udev->portnum + 1;
2046 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
2047 } else {
2048 /* Try to allocate the next devnum beginning at
2049 * bus->devnum_next. */
2050 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
2051 bus->devnum_next);
2052 if (devnum >= 128)
2053 devnum = find_next_zero_bit(bus->devmap.devicemap,
2054 128, 1);
Matthias Beyer469271f2013-10-10 23:41:27 +02002055 bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1);
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (devnum < 128) {
2058 set_bit(devnum, bus->devmap.devicemap);
2059 udev->devnum = devnum;
2060 }
2061}
2062
Alan Stern3b29b682011-02-22 09:53:41 -05002063static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 if (udev->devnum > 0) {
2066 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
2067 udev->devnum = -1;
2068 }
2069}
2070
Alan Stern3b29b682011-02-22 09:53:41 -05002071static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07002072{
2073 /* The address for a WUSB device is managed by wusbcore. */
2074 if (!udev->wusb)
2075 udev->devnum = devnum;
2076}
2077
Herbert Xuf7410ce2010-01-10 20:15:03 +11002078static void hub_free_dev(struct usb_device *udev)
2079{
2080 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2081
2082 /* Root hubs aren't real devices, so don't free HCD resources */
2083 if (hcd->driver->free_dev && udev->parent)
2084 hcd->driver->free_dev(hcd, udev);
2085}
2086
Dan Williams7027df32014-05-20 18:09:36 -07002087static void hub_disconnect_children(struct usb_device *udev)
2088{
2089 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
2090 int i;
2091
2092 /* Free up all the children before we remove this device */
2093 for (i = 0; i < udev->maxchild; i++) {
2094 if (hub->ports[i]->child)
2095 usb_disconnect(&hub->ports[i]->child);
2096 }
2097}
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099/**
2100 * usb_disconnect - disconnect a device (usbcore-internal)
2101 * @pdev: pointer to device being disconnected
2102 * Context: !in_interrupt ()
2103 *
2104 * Something got disconnected. Get rid of it and all of its children.
2105 *
2106 * If *pdev is a normal device then the parent hub must already be locked.
Bjorn Helgaasdb8f2aa2013-09-13 13:57:34 -06002107 * If *pdev is a root hub then the caller must hold the usb_bus_list_lock,
2108 * which protects the set of root hubs as well as the list of buses.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 *
2110 * Only hub drivers (including virtual root hub drivers for host
2111 * controllers) should ever call this.
2112 *
2113 * This call is synchronous, and may not be used in an interrupt context.
2114 */
2115void usb_disconnect(struct usb_device **pdev)
2116{
Dan Williams7027df32014-05-20 18:09:36 -07002117 struct usb_port *port_dev = NULL;
2118 struct usb_device *udev = *pdev;
Peter Chen5b1dc202014-08-19 08:56:21 +08002119 struct usb_hub *hub = NULL;
2120 int port1 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 /* mark the device as inactive, so any further urb submissions for
2123 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002124 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 */
2126 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05002127 dev_info(&udev->dev, "USB disconnect, device number %d\n",
2128 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002130 usb_lock_device(udev);
2131
Dan Williams7027df32014-05-20 18:09:36 -07002132 hub_disconnect_children(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 /* deallocate hcd/hardware state ... nuking all pending urbs and
2135 * cleaning up all state associated with the current configuration
2136 * so that the hardware is now fully quiesced.
2137 */
Alan Stern782da722006-07-01 22:09:35 -04002138 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04002140 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Lan Tianyufde26382013-01-20 01:53:33 +08002142 if (udev->parent) {
Dan Williams7027df32014-05-20 18:09:36 -07002143 port1 = udev->portnum;
2144 hub = usb_hub_to_struct_hub(udev->parent);
2145 port_dev = hub->ports[port1 - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002146
2147 sysfs_remove_link(&udev->dev.kobj, "port");
2148 sysfs_remove_link(&port_dev->dev.kobj, "device");
Lan Tianyu971fcd42013-01-23 04:26:29 +08002149
Dan Williams7027df32014-05-20 18:09:36 -07002150 /*
2151 * As usb_port_runtime_resume() de-references udev, make
2152 * sure no resumes occur during removal
2153 */
2154 if (!test_and_set_bit(port1, hub->child_usage_bits))
2155 pm_runtime_get_sync(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002156 }
2157
Alan Stern3b23dd62008-12-05 14:10:34 -05002158 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04002159 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07002160
Alan Stern782da722006-07-01 22:09:35 -04002161 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002162 * for de-configuring the device and invoking the remove-device
2163 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002164 */
2165 device_del(&udev->dev);
2166
2167 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 * (or root_hub) pointer.
2169 */
Alan Stern3b29b682011-02-22 09:53:41 -05002170 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 /* Avoid races with recursively_mark_NOTATTACHED() */
2173 spin_lock_irq(&device_state_lock);
2174 *pdev = NULL;
2175 spin_unlock_irq(&device_state_lock);
2176
Dan Williams7027df32014-05-20 18:09:36 -07002177 if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits))
2178 pm_runtime_put(&port_dev->dev);
2179
Herbert Xuf7410ce2010-01-10 20:15:03 +11002180 hub_free_dev(udev);
2181
Alan Stern782da722006-07-01 22:09:35 -04002182 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002185#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186static void show_string(struct usb_device *udev, char *id, char *string)
2187{
2188 if (!string)
2189 return;
Joe Perchesf2ec5222012-10-28 01:05:51 -07002190 dev_info(&udev->dev, "%s: %s\n", id, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002193static void announce_device(struct usb_device *udev)
2194{
2195 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2196 le16_to_cpu(udev->descriptor.idVendor),
2197 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002198 dev_info(&udev->dev,
2199 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002200 udev->descriptor.iManufacturer,
2201 udev->descriptor.iProduct,
2202 udev->descriptor.iSerialNumber);
2203 show_string(udev, "Product", udev->product);
2204 show_string(udev, "Manufacturer", udev->manufacturer);
2205 show_string(udev, "SerialNumber", udev->serial);
2206}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002208static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209#endif
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Oliver Neukum3ede7602007-01-11 14:35:50 +01002212/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002213 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002214 * @udev: newly addressed device (in ADDRESS state)
2215 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002216 * Finish enumeration for On-The-Go devices
Yacine Belkadi626f0902013-08-02 20:10:04 +02002217 *
2218 * Return: 0 if successful. A negative error code otherwise.
Oliver Neukum3ede7602007-01-11 14:35:50 +01002219 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002220static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002222 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224#ifdef CONFIG_USB_OTG
2225 /*
2226 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2227 * to wake us after we've powered off VBUS; and HNP, switching roles
2228 * "host" to "peripheral". The OTG descriptor helps figure this out.
2229 */
2230 if (!udev->bus->is_b_host
2231 && udev->config
2232 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002233 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 struct usb_bus *bus = udev->bus;
2235
2236 /* descriptor may appear anywhere in config */
2237 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2238 le16_to_cpu(udev->config[0].desc.wTotalLength),
2239 USB_DT_OTG, (void **) &desc) == 0) {
2240 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002241 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 dev_info(&udev->dev,
2244 "Dual-Role OTG device on %sHNP port\n",
2245 (port1 == bus->otg_port)
2246 ? "" : "non-");
2247
2248 /* enable HNP before suspend, it's simpler */
2249 if (port1 == bus->otg_port)
2250 bus->b_hnp_enable = 1;
2251 err = usb_control_msg(udev,
2252 usb_sndctrlpipe(udev, 0),
2253 USB_REQ_SET_FEATURE, 0,
2254 bus->b_hnp_enable
2255 ? USB_DEVICE_B_HNP_ENABLE
2256 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2257 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2258 if (err < 0) {
2259 /* OTG MESSAGE: report errors here,
2260 * customize to match your product.
2261 */
2262 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002263 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 err);
2265 bus->b_hnp_enable = 0;
2266 }
2267 }
2268 }
2269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002271 return err;
2272}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002274
2275/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002276 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002277 * @udev: newly addressed device (in ADDRESS state)
2278 *
2279 * This is only called by usb_new_device() and usb_authorize_device()
2280 * and FIXME -- all comments that apply to them apply here wrt to
2281 * environment.
2282 *
2283 * If the device is WUSB and not authorized, we don't attempt to read
2284 * the string descriptors, as they will be errored out by the device
2285 * until it has been authorized.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002286 *
2287 * Return: 0 if successful. A negative error code otherwise.
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002288 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002289static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002290{
2291 int err;
Peter Chen026f3fc2014-08-19 09:51:53 +08002292 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002293
2294 if (udev->config == NULL) {
2295 err = usb_get_configuration(udev);
2296 if (err < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04002297 if (err != -ENODEV)
2298 dev_err(&udev->dev, "can't read configurations, error %d\n",
2299 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002300 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002301 }
2302 }
Thomas Pugliese83e83ec2013-12-09 13:40:29 -06002303
2304 /* read the standard strings and cache them if present */
2305 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2306 udev->manufacturer = usb_cache_string(udev,
2307 udev->descriptor.iManufacturer);
2308 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2309
Alan Stern8d8558d2009-12-08 15:50:41 -05002310 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002311 if (err < 0)
2312 return err;
2313
Peter Chen026f3fc2014-08-19 09:51:53 +08002314 if (IS_ENABLED(CONFIG_USB_OTG_WHITELIST) && hcd->tpl_support &&
2315 !is_targeted(udev) && IS_ENABLED(CONFIG_USB_OTG)) {
2316 /* Maybe it can talk to us, though we can't talk to it.
2317 * (Includes HNP test device.)
2318 */
2319 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
2320 err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND);
2321 if (err < 0)
2322 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2323 }
2324 return -ENOTSUPP;
2325 }
2326
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002327 usb_detect_interface_quirks(udev);
2328
2329 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002330}
2331
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002332static void set_usb_port_removable(struct usb_device *udev)
2333{
2334 struct usb_device *hdev = udev->parent;
2335 struct usb_hub *hub;
2336 u8 port = udev->portnum;
2337 u16 wHubCharacteristics;
2338 bool removable = true;
2339
2340 if (!hdev)
2341 return;
2342
Lan Tianyuad493e52013-01-23 04:26:30 +08002343 hub = usb_hub_to_struct_hub(udev->parent);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002344
2345 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2346
2347 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2348 return;
2349
2350 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002351 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2352 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002353 removable = false;
2354 } else {
2355 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2356 removable = false;
2357 }
2358
2359 if (removable)
2360 udev->removable = USB_DEVICE_REMOVABLE;
2361 else
2362 udev->removable = USB_DEVICE_FIXED;
Dan Williamsa4204ff2014-05-20 18:08:22 -07002363
2364 /*
2365 * Platform firmware may have populated an alternative value for
2366 * removable. If the parent port has a known connect_type use
2367 * that instead.
2368 */
2369 switch (hub->ports[udev->portnum - 1]->connect_type) {
2370 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
2371 udev->removable = USB_DEVICE_REMOVABLE;
2372 break;
2373 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
2374 udev->removable = USB_DEVICE_FIXED;
2375 break;
2376 default: /* use what was set above */
2377 break;
2378 }
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002379}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002380
2381/**
2382 * usb_new_device - perform initial device setup (usbcore-internal)
2383 * @udev: newly addressed device (in ADDRESS state)
2384 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002385 * This is called with devices which have been detected but not fully
2386 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002387 * for any device configuration. The caller must have locked either
2388 * the parent hub (if udev is a normal device) or else the
2389 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2390 * udev has already been installed, but udev is not yet visible through
2391 * sysfs or other filesystem code.
2392 *
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002393 * This call is synchronous, and may not be used in an interrupt context.
2394 *
2395 * Only the hub driver or root-hub registrar should ever call this.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002396 *
2397 * Return: Whether the device is configured properly or not. Zero if the
2398 * interface was registered with the driver core; else a negative errno
2399 * value.
2400 *
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002401 */
2402int usb_new_device(struct usb_device *udev)
2403{
2404 int err;
2405
Dan Streetman16985402010-01-06 09:56:53 -05002406 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002407 /* Initialize non-root-hub device wakeup to disabled;
2408 * device (un)configuration controls wakeup capable
2409 * sysfs power/wakeup controls wakeup enabled/disabled
2410 */
2411 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002412 }
2413
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002414 /* Tell the runtime-PM framework the device is active */
2415 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002416 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002417 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002418 pm_runtime_enable(&udev->dev);
2419
Alan Sternc08512c2010-11-15 15:57:58 -05002420 /* By default, forbid autosuspend for all devices. It will be
2421 * allowed for hubs during binding.
2422 */
2423 usb_disable_autosuspend(udev);
2424
Alan Stern8d8558d2009-12-08 15:50:41 -05002425 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002426 if (err < 0)
2427 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002428 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2429 udev->devnum, udev->bus->busnum,
2430 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002431 /* export the usbdev device-node for libusb */
2432 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2433 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2434
Alan Stern6cd13202008-11-13 15:08:30 -05002435 /* Tell the world! */
2436 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002437
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002438 if (udev->serial)
2439 add_device_randomness(udev->serial, strlen(udev->serial));
2440 if (udev->product)
2441 add_device_randomness(udev->product, strlen(udev->product));
2442 if (udev->manufacturer)
2443 add_device_randomness(udev->manufacturer,
2444 strlen(udev->manufacturer));
2445
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002446 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002447
Dan Williamsa4204ff2014-05-20 18:08:22 -07002448 /* check whether the hub or firmware marks this port as non-removable */
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002449 if (udev->parent)
2450 set_usb_port_removable(udev);
2451
Alan Stern782da722006-07-01 22:09:35 -04002452 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002453 * for configuring the device and invoking the add-device
2454 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002455 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002456 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (err) {
2458 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2459 goto fail;
2460 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002461
Lan Tianyufde26382013-01-20 01:53:33 +08002462 /* Create link files between child device and usb port device. */
2463 if (udev->parent) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002464 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
Dan Williamsd5c38342014-05-20 18:08:52 -07002465 int port1 = udev->portnum;
2466 struct usb_port *port_dev = hub->ports[port1 - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002467
2468 err = sysfs_create_link(&udev->dev.kobj,
2469 &port_dev->dev.kobj, "port");
2470 if (err)
2471 goto fail;
2472
2473 err = sysfs_create_link(&port_dev->dev.kobj,
2474 &udev->dev.kobj, "device");
2475 if (err) {
2476 sysfs_remove_link(&udev->dev.kobj, "port");
2477 goto fail;
2478 }
Lan Tianyu971fcd42013-01-23 04:26:29 +08002479
Dan Williamsd5c38342014-05-20 18:08:52 -07002480 if (!test_and_set_bit(port1, hub->child_usage_bits))
2481 pm_runtime_get_sync(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002482 }
2483
Alan Stern3b23dd62008-12-05 14:10:34 -05002484 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002485 usb_mark_last_busy(udev);
2486 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002487 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489fail:
2490 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002491 pm_runtime_disable(&udev->dev);
2492 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002493 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002496
2497/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002498 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2499 * @usb_dev: USB device
2500 *
2501 * Move the USB device to a very basic state where interfaces are disabled
2502 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002503 *
2504 * We share a lock (that we have) with device_del(), so we need to
2505 * defer its call.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002506 *
2507 * Return: 0.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002508 */
2509int usb_deauthorize_device(struct usb_device *usb_dev)
2510{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002511 usb_lock_device(usb_dev);
2512 if (usb_dev->authorized == 0)
2513 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002514
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002515 usb_dev->authorized = 0;
2516 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002517
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002518out_unauthorized:
2519 usb_unlock_device(usb_dev);
2520 return 0;
2521}
2522
2523
2524int usb_authorize_device(struct usb_device *usb_dev)
2525{
2526 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002527
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002528 usb_lock_device(usb_dev);
2529 if (usb_dev->authorized == 1)
2530 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002531
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002532 result = usb_autoresume_device(usb_dev);
2533 if (result < 0) {
2534 dev_err(&usb_dev->dev,
2535 "can't autoresume for authorization: %d\n", result);
2536 goto error_autoresume;
2537 }
2538 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2539 if (result < 0) {
2540 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2541 "authorization: %d\n", result);
2542 goto error_device_descriptor;
2543 }
Alan Sternda307122009-12-08 15:54:44 -05002544
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002545 usb_dev->authorized = 1;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002546 /* Choose and set the configuration. This registers the interfaces
2547 * with the driver core and lets interface drivers bind to them.
2548 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002549 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002550 if (c >= 0) {
2551 result = usb_set_configuration(usb_dev, c);
2552 if (result) {
2553 dev_err(&usb_dev->dev,
2554 "can't set config #%d, error %d\n", c, result);
2555 /* This need not be fatal. The user can try to
2556 * set other configurations. */
2557 }
2558 }
2559 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002560
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002561error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002562 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002563error_autoresume:
2564out_authorized:
Matthias Beyer781b2132013-10-10 23:41:29 +02002565 usb_unlock_device(usb_dev); /* complements locktree */
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002566 return result;
2567}
2568
2569
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002570/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2571static unsigned hub_is_wusb(struct usb_hub *hub)
2572{
2573 struct usb_hcd *hcd;
2574 if (hub->hdev->parent != NULL) /* not a root hub? */
2575 return 0;
2576 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2577 return hcd->wireless;
2578}
2579
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581#define PORT_RESET_TRIES 5
2582#define SET_ADDRESS_TRIES 2
2583#define GET_DESCRIPTOR_TRIES 2
2584#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302585#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2588#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002589#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590#define HUB_LONG_RESET_TIME 200
Sarah Sharp77c7f072012-11-14 17:16:52 -08002591#define HUB_RESET_TIMEOUT 800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Dan Williams48fc7db2013-12-05 17:07:27 -08002593/*
2594 * "New scheme" enumeration causes an extra state transition to be
2595 * exposed to an xhci host and causes USB3 devices to receive control
2596 * commands in the default state. This has been seen to cause
2597 * enumeration failures, so disable this enumeration scheme for USB3
2598 * devices.
2599 */
2600static bool use_new_scheme(struct usb_device *udev, int retry)
2601{
2602 if (udev->speed == USB_SPEED_SUPER)
2603 return false;
2604
2605 return USE_NEW_SCHEME(retry);
2606}
2607
Sarah Sharp10d674a2011-09-14 14:24:52 -07002608static int hub_port_reset(struct usb_hub *hub, int port1,
2609 struct usb_device *udev, unsigned int delay, bool warm);
2610
Rahul Bedarkar025d4432014-01-04 11:24:41 +05302611/* Is a USB 3.0 port in the Inactive or Compliance Mode state?
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002612 * Port worm reset is required to recover
2613 */
Dan Williams3cd12f92014-05-29 12:58:46 -07002614static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
2615 u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002616{
Dan Williams3cd12f92014-05-29 12:58:46 -07002617 u16 link_state;
2618
2619 if (!hub_is_superspeed(hub->hdev))
2620 return false;
2621
2622 if (test_bit(port1, hub->warm_reset_bits))
2623 return true;
2624
2625 link_state = portstatus & USB_PORT_STAT_LINK_STATE;
2626 return link_state == USB_SS_PORT_LS_SS_INACTIVE
2627 || link_state == USB_SS_PORT_LS_COMP_MOD;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002628}
2629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002631 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
2633 int delay_time, ret;
2634 u16 portstatus;
2635 u16 portchange;
2636
2637 for (delay_time = 0;
2638 delay_time < HUB_RESET_TIMEOUT;
2639 delay_time += delay) {
2640 /* wait to give the device a chance to reset */
2641 msleep(delay);
2642
2643 /* read and decode port status */
2644 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2645 if (ret < 0)
2646 return ret;
2647
Sarah Sharp4f434472012-11-15 14:58:04 -08002648 /* The port state is unknown until the reset completes. */
Sarah Sharp470f0be2012-12-11 10:14:03 -08002649 if (!(portstatus & USB_PORT_STAT_RESET))
2650 break;
Sarah Sharp4f434472012-11-15 14:58:04 -08002651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 /* switch to the long delay after two short delay failures */
2653 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2654 delay = HUB_LONG_RESET_TIME;
2655
Dan Williamsd99f6b42014-05-20 18:08:17 -07002656 dev_dbg(&hub->ports[port1 - 1]->dev,
2657 "not %sreset yet, waiting %dms\n",
2658 warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 }
2660
Sarah Sharp470f0be2012-12-11 10:14:03 -08002661 if ((portstatus & USB_PORT_STAT_RESET))
2662 return -EBUSY;
2663
Dan Williams3cd12f92014-05-29 12:58:46 -07002664 if (hub_port_warm_reset_required(hub, port1, portstatus))
Sarah Sharp470f0be2012-12-11 10:14:03 -08002665 return -ENOTCONN;
2666
2667 /* Device went away? */
2668 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2669 return -ENOTCONN;
2670
2671 /* bomb out completely if the connection bounced. A USB 3.0
2672 * connection may bounce if multiple warm resets were issued,
2673 * but the device may have successfully re-connected. Ignore it.
2674 */
2675 if (!hub_is_superspeed(hub->hdev) &&
2676 (portchange & USB_PORT_STAT_C_CONNECTION))
2677 return -ENOTCONN;
2678
2679 if (!(portstatus & USB_PORT_STAT_ENABLE))
2680 return -EBUSY;
2681
2682 if (!udev)
2683 return 0;
2684
2685 if (hub_is_wusb(hub))
2686 udev->speed = USB_SPEED_WIRELESS;
2687 else if (hub_is_superspeed(hub->hdev))
2688 udev->speed = USB_SPEED_SUPER;
2689 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2690 udev->speed = USB_SPEED_HIGH;
2691 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2692 udev->speed = USB_SPEED_LOW;
2693 else
2694 udev->speed = USB_SPEED_FULL;
2695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696}
2697
Andiry Xu75d7cf72011-09-13 16:41:11 -07002698static void hub_port_finish_reset(struct usb_hub *hub, int port1,
Sarah Sharpa24a6072012-11-01 11:20:44 -07002699 struct usb_device *udev, int *status)
Andiry Xu75d7cf72011-09-13 16:41:11 -07002700{
2701 switch (*status) {
2702 case 0:
Sarah Sharpa24a6072012-11-01 11:20:44 -07002703 /* TRSTRCY = 10 ms; plus some extra */
2704 msleep(10 + 40);
2705 if (udev) {
2706 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2707
2708 update_devnum(udev, 0);
2709 /* The xHC may think the device is already reset,
2710 * so ignore the status.
2711 */
2712 if (hcd->driver->reset_device)
2713 hcd->driver->reset_device(hcd, udev);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002714 }
2715 /* FALL THROUGH */
2716 case -ENOTCONN:
2717 case -ENODEV:
Lan Tianyuad493e52013-01-23 04:26:30 +08002718 usb_clear_port_feature(hub->hdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002719 port1, USB_PORT_FEAT_C_RESET);
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002720 if (hub_is_superspeed(hub->hdev)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002721 usb_clear_port_feature(hub->hdev, port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002722 USB_PORT_FEAT_C_BH_PORT_RESET);
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_PORT_LINK_STATE);
Lan Tianyuad493e52013-01-23 04:26:30 +08002725 usb_clear_port_feature(hub->hdev, port1,
Sarah Sharpa24a6072012-11-01 11:20:44 -07002726 USB_PORT_FEAT_C_CONNECTION);
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002727 }
Sarah Sharpa24a6072012-11-01 11:20:44 -07002728 if (udev)
Andiry Xu75d7cf72011-09-13 16:41:11 -07002729 usb_set_device_state(udev, *status
2730 ? USB_STATE_NOTATTACHED
2731 : USB_STATE_DEFAULT);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002732 break;
2733 }
2734}
2735
2736/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002738 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739{
2740 int i, status;
Sarah Sharpa24a6072012-11-01 11:20:44 -07002741 u16 portchange, portstatus;
Dan Williamsd99f6b42014-05-20 18:08:17 -07002742 struct usb_port *port_dev = hub->ports[port1 - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002744 if (!hub_is_superspeed(hub->hdev)) {
2745 if (warm) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002746 dev_err(hub->intfdev, "only USB3 hub support "
2747 "warm reset\n");
2748 return -EINVAL;
2749 }
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002750 /* Block EHCI CF initialization during the port reset.
2751 * Some companion controllers don't like it when they mix.
2752 */
2753 down_read(&ehci_cf_port_reset_rwsem);
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07002754 } else if (!warm) {
2755 /*
2756 * If the caller hasn't explicitly requested a warm reset,
2757 * double check and see if one is needed.
2758 */
2759 status = hub_port_status(hub, port1,
2760 &portstatus, &portchange);
2761 if (status < 0)
2762 goto done;
2763
Dan Williams3cd12f92014-05-29 12:58:46 -07002764 if (hub_port_warm_reset_required(hub, port1, portstatus))
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07002765 warm = true;
Andiry Xu75d7cf72011-09-13 16:41:11 -07002766 }
Dan Williams3cd12f92014-05-29 12:58:46 -07002767 clear_bit(port1, hub->warm_reset_bits);
Alan Stern32fe0192007-10-10 16:27:07 -04002768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 /* Reset the port */
2770 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002771 status = set_port_feature(hub->hdev, port1, (warm ?
2772 USB_PORT_FEAT_BH_PORT_RESET :
2773 USB_PORT_FEAT_RESET));
Alan Sterne9e88fb2013-03-27 16:14:01 -04002774 if (status == -ENODEV) {
2775 ; /* The hub is gone */
2776 } else if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002777 dev_err(&port_dev->dev,
2778 "cannot %sreset (err = %d)\n",
2779 warm ? "warm " : "", status);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002780 } else {
2781 status = hub_port_wait_reset(hub, port1, udev, delay,
2782 warm);
Alan Sterne9e88fb2013-03-27 16:14:01 -04002783 if (status && status != -ENOTCONN && status != -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 dev_dbg(hub->intfdev,
2785 "port_wait_reset: err = %d\n",
2786 status);
2787 }
2788
Sarah Sharpa24a6072012-11-01 11:20:44 -07002789 /* Check for disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002790 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
Sarah Sharpa24a6072012-11-01 11:20:44 -07002791 hub_port_finish_reset(hub, port1, udev, &status);
2792
2793 if (!hub_is_superspeed(hub->hdev))
2794 goto done;
2795
2796 /*
2797 * If a USB 3.0 device migrates from reset to an error
2798 * state, re-issue the warm reset.
2799 */
2800 if (hub_port_status(hub, port1,
2801 &portstatus, &portchange) < 0)
2802 goto done;
2803
Dan Williams3cd12f92014-05-29 12:58:46 -07002804 if (!hub_port_warm_reset_required(hub, port1,
2805 portstatus))
Sarah Sharpa24a6072012-11-01 11:20:44 -07002806 goto done;
2807
2808 /*
2809 * If the port is in SS.Inactive or Compliance Mode, the
2810 * hot or warm reset failed. Try another warm reset.
2811 */
2812 if (!warm) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002813 dev_dbg(&port_dev->dev,
2814 "hot reset failed, warm reset\n");
Sarah Sharpa24a6072012-11-01 11:20:44 -07002815 warm = true;
2816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 }
2818
Dan Williamsd99f6b42014-05-20 18:08:17 -07002819 dev_dbg(&port_dev->dev,
2820 "not enabled, trying %sreset again...\n",
2821 warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 delay = HUB_LONG_RESET_TIME;
2823 }
2824
Dan Williamsd99f6b42014-05-20 18:08:17 -07002825 dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Andiry Xu75d7cf72011-09-13 16:41:11 -07002827done:
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002828 if (!hub_is_superspeed(hub->hdev))
Andiry Xu75d7cf72011-09-13 16:41:11 -07002829 up_read(&ehci_cf_port_reset_rwsem);
2830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 return status;
2832}
2833
Andiry Xu0ed9a572011-04-27 18:07:43 +08002834/* Check if a port is power on */
2835static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2836{
2837 int ret = 0;
2838
2839 if (hub_is_superspeed(hub->hdev)) {
2840 if (portstatus & USB_SS_PORT_STAT_POWER)
2841 ret = 1;
2842 } else {
2843 if (portstatus & USB_PORT_STAT_POWER)
2844 ret = 1;
2845 }
2846
2847 return ret;
2848}
2849
Dan Williams5c79a1e2014-05-20 18:09:26 -07002850static void usb_lock_port(struct usb_port *port_dev)
2851 __acquires(&port_dev->status_lock)
2852{
2853 mutex_lock(&port_dev->status_lock);
2854 __acquire(&port_dev->status_lock);
2855}
2856
2857static void usb_unlock_port(struct usb_port *port_dev)
2858 __releases(&port_dev->status_lock)
2859{
2860 mutex_unlock(&port_dev->status_lock);
2861 __release(&port_dev->status_lock);
2862}
2863
Alan Sternd388dab2006-07-01 22:14:24 -04002864#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Andiry Xu0ed9a572011-04-27 18:07:43 +08002866/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2867static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2868{
2869 int ret = 0;
2870
2871 if (hub_is_superspeed(hub->hdev)) {
2872 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2873 == USB_SS_PORT_LS_U3)
2874 ret = 1;
2875 } else {
2876 if (portstatus & USB_PORT_STAT_SUSPEND)
2877 ret = 1;
2878 }
2879
2880 return ret;
2881}
Alan Sternb01b03f2008-04-28 11:06:11 -04002882
2883/* Determine whether the device on a port is ready for a normal resume,
2884 * is ready for a reset-resume, or should be disconnected.
2885 */
2886static int check_port_resume_type(struct usb_device *udev,
2887 struct usb_hub *hub, int port1,
2888 int status, unsigned portchange, unsigned portstatus)
2889{
Dan Williamsd99f6b42014-05-20 18:08:17 -07002890 struct usb_port *port_dev = hub->ports[port1 - 1];
2891
Dan Williams3cd12f92014-05-29 12:58:46 -07002892 /* Is a warm reset needed to recover the connection? */
2893 if (status == 0 && udev->reset_resume
2894 && hub_port_warm_reset_required(hub, port1, portstatus)) {
2895 /* pass */;
2896 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002897 /* Is the device still present? */
Dan Williams3cd12f92014-05-29 12:58:46 -07002898 else if (status || port_is_suspended(hub, portstatus) ||
Andiry Xu0ed9a572011-04-27 18:07:43 +08002899 !port_is_power_on(hub, portstatus) ||
2900 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002901 if (status >= 0)
2902 status = -ENODEV;
2903 }
2904
Alan Stern86c57ed2008-06-30 11:14:43 -04002905 /* Can't do a normal resume if the port isn't enabled,
2906 * so try a reset-resume instead.
2907 */
2908 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2909 if (udev->persist_enabled)
2910 udev->reset_resume = 1;
2911 else
2912 status = -ENODEV;
2913 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002914
2915 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002916 dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n",
2917 portchange, portstatus, status);
Alan Sternb01b03f2008-04-28 11:06:11 -04002918 } else if (udev->reset_resume) {
2919
2920 /* Late port handoff can set status-change bits */
2921 if (portchange & USB_PORT_STAT_C_CONNECTION)
Lan Tianyuad493e52013-01-23 04:26:30 +08002922 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04002923 USB_PORT_FEAT_C_CONNECTION);
2924 if (portchange & USB_PORT_STAT_C_ENABLE)
Lan Tianyuad493e52013-01-23 04:26:30 +08002925 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04002926 USB_PORT_FEAT_C_ENABLE);
2927 }
2928
2929 return status;
2930}
2931
Sarah Sharpf74631e2012-06-25 12:08:08 -07002932int usb_disable_ltm(struct usb_device *udev)
2933{
2934 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2935
2936 /* Check if the roothub and device supports LTM. */
2937 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2938 !usb_device_supports_ltm(udev))
2939 return 0;
2940
2941 /* Clear Feature LTM Enable can only be sent if the device is
2942 * configured.
2943 */
2944 if (!udev->actconfig)
2945 return 0;
2946
2947 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2948 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2949 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2950 USB_CTRL_SET_TIMEOUT);
2951}
2952EXPORT_SYMBOL_GPL(usb_disable_ltm);
2953
2954void usb_enable_ltm(struct usb_device *udev)
2955{
2956 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2957
2958 /* Check if the roothub and device supports LTM. */
2959 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2960 !usb_device_supports_ltm(udev))
2961 return;
2962
2963 /* Set Feature LTM Enable can only be sent if the device is
2964 * configured.
2965 */
2966 if (!udev->actconfig)
2967 return;
2968
2969 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2970 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2971 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2972 USB_CTRL_SET_TIMEOUT);
2973}
2974EXPORT_SYMBOL_GPL(usb_enable_ltm);
2975
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002976/*
Alan Stern28e861652013-07-30 15:37:33 -04002977 * usb_enable_remote_wakeup - enable remote wakeup for a device
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002978 * @udev: target device
2979 *
Alan Stern28e861652013-07-30 15:37:33 -04002980 * For USB-2 devices: Set the device's remote wakeup feature.
2981 *
2982 * For USB-3 devices: Assume there's only one function on the device and
2983 * enable remote wake for the first interface. FIXME if the interface
2984 * association descriptor shows there's more than one function.
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002985 */
Alan Stern28e861652013-07-30 15:37:33 -04002986static int usb_enable_remote_wakeup(struct usb_device *udev)
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002987{
Alan Stern28e861652013-07-30 15:37:33 -04002988 if (udev->speed < USB_SPEED_SUPER)
2989 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2990 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2991 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
2992 USB_CTRL_SET_TIMEOUT);
2993 else
2994 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2995 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
2996 USB_INTRF_FUNC_SUSPEND,
2997 USB_INTRF_FUNC_SUSPEND_RW |
2998 USB_INTRF_FUNC_SUSPEND_LP,
2999 NULL, 0, USB_CTRL_SET_TIMEOUT);
3000}
3001
3002/*
3003 * usb_disable_remote_wakeup - disable remote wakeup for a device
3004 * @udev: target device
3005 *
3006 * For USB-2 devices: Clear the device's remote wakeup feature.
3007 *
3008 * For USB-3 devices: Assume there's only one function on the device and
3009 * disable remote wake for the first interface. FIXME if the interface
3010 * association descriptor shows there's more than one function.
3011 */
3012static int usb_disable_remote_wakeup(struct usb_device *udev)
3013{
3014 if (udev->speed < USB_SPEED_SUPER)
3015 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3016 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
3017 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
3018 USB_CTRL_SET_TIMEOUT);
3019 else
3020 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003021 USB_REQ_CLEAR_FEATURE, USB_RECIP_INTERFACE,
3022 USB_INTRF_FUNC_SUSPEND, 0, NULL, 0,
3023 USB_CTRL_SET_TIMEOUT);
3024}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Alan Sterne583d9d2013-07-11 14:58:04 -04003026/* Count of wakeup-enabled devices at or below udev */
3027static unsigned wakeup_enabled_descendants(struct usb_device *udev)
3028{
3029 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
3030
3031 return udev->do_remote_wakeup +
3032 (hub ? hub->wakeup_enabled_descendants : 0);
3033}
3034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035/*
Alan Stern140d8f62006-07-01 22:07:21 -04003036 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04003037 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07003038 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 *
3040 * Suspends a USB device that isn't in active use, conserving power.
3041 * Devices may wake out of a suspend, if anything important happens,
3042 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04003043 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 * to disconnect devices while they are suspended.
3045 *
David Brownell390a8c32005-09-13 19:57:27 -07003046 * This only affects the USB hardware for a device; its interfaces
3047 * (and, for hubs, child devices) must already have been suspended.
3048 *
Alan Stern624d6c02007-05-30 15:35:16 -04003049 * Selective port suspend reduces power; most suspended devices draw
3050 * less than 500 uA. It's also used in OTG, along with remote wakeup.
3051 * All devices below the suspended port are also suspended.
3052 *
3053 * Devices leave suspend state when the host wakes them up. Some devices
3054 * also support "remote wakeup", where the device can activate the USB
3055 * tree above them to deliver data, such as a keypress or packet. In
3056 * some cases, this wakes the USB host.
3057 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 * Suspending OTG devices may trigger HNP, if that's been enabled
3059 * between a pair of dual-role devices. That will change roles, such
3060 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
3061 *
Alan Stern4956ecc2007-05-30 16:51:28 -04003062 * Devices on USB hub ports have only one "suspend" state, corresponding
3063 * to ACPI D2, "may cause the device to lose some context".
3064 * State transitions include:
3065 *
3066 * - suspend, resume ... when the VBUS power link stays live
3067 * - suspend, disconnect ... VBUS lost
3068 *
3069 * Once VBUS drop breaks the circuit, the port it's using has to go through
3070 * normal re-enumeration procedures, starting with enabling VBUS power.
3071 * Other than re-initializing the hub (plug/unplug, except for root hubs),
3072 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
3073 * timer, no SRP, no requests through sysfs.
3074 *
Alan Sterne583d9d2013-07-11 14:58:04 -04003075 * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
3076 * suspended until their bus goes into global suspend (i.e., the root
Alan Stern0aa28322013-03-27 16:14:19 -04003077 * hub is suspended). Nevertheless, we change @udev->state to
3078 * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual
3079 * upstream port setting is stored in @udev->port_is_suspended.
Alan Stern4956ecc2007-05-30 16:51:28 -04003080 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 * Returns 0 on success, else negative errno.
3082 */
Alan Stern65bfd292008-11-25 16:39:18 -05003083int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
Lan Tianyuad493e52013-01-23 04:26:30 +08003085 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3086 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003087 int port1 = udev->portnum;
3088 int status;
Alan Stern0aa28322013-03-27 16:14:19 -04003089 bool really_suspend = true;
Alan Stern4956ecc2007-05-30 16:51:28 -04003090
Dan Williams5c79a1e2014-05-20 18:09:26 -07003091 usb_lock_port(port_dev);
3092
Alan Stern624d6c02007-05-30 15:35:16 -04003093 /* enable remote wakeup when appropriate; this lets the device
3094 * wake up the upstream hub (including maybe the root hub).
3095 *
3096 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
3097 * we don't explicitly enable it here.
3098 */
3099 if (udev->do_remote_wakeup) {
Alan Stern28e861652013-07-30 15:37:33 -04003100 status = usb_enable_remote_wakeup(udev);
Oliver Neukum0c487202009-10-19 13:19:41 +02003101 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003102 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
3103 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02003104 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02003105 if (PMSG_IS_AUTO(msg))
Alan Stern4fae6f02013-07-30 15:39:02 -04003106 goto err_wakeup;
Oliver Neukum0c487202009-10-19 13:19:41 +02003107 }
Alan Stern624d6c02007-05-30 15:35:16 -04003108 }
3109
Andiry Xu65580b432011-09-23 14:19:52 -07003110 /* disable USB2 hardware LPM */
3111 if (udev->usb2_hw_lpm_enabled == 1)
3112 usb_set_usb2_hardware_lpm(udev, 0);
3113
Sarah Sharpf74631e2012-06-25 12:08:08 -07003114 if (usb_disable_ltm(udev)) {
Alan Stern4fae6f02013-07-30 15:39:02 -04003115 dev_err(&udev->dev, "Failed to disable LTM before suspend\n.");
3116 status = -ENOMEM;
3117 if (PMSG_IS_AUTO(msg))
3118 goto err_ltm;
Sarah Sharpf74631e2012-06-25 12:08:08 -07003119 }
Sarah Sharp83060952012-05-02 14:25:52 -07003120 if (usb_unlocked_disable_lpm(udev)) {
Alan Stern4fae6f02013-07-30 15:39:02 -04003121 dev_err(&udev->dev, "Failed to disable LPM before suspend\n.");
3122 status = -ENOMEM;
3123 if (PMSG_IS_AUTO(msg))
3124 goto err_lpm3;
Sarah Sharp83060952012-05-02 14:25:52 -07003125 }
3126
Alan Stern624d6c02007-05-30 15:35:16 -04003127 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08003128 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003129 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
Alan Sterne583d9d2013-07-11 14:58:04 -04003130
Alan Stern0aa28322013-03-27 16:14:19 -04003131 /*
3132 * For system suspend, we do not need to enable the suspend feature
3133 * on individual USB-2 ports. The devices will automatically go
3134 * into suspend a few ms after the root hub stops sending packets.
3135 * The USB 2.0 spec calls this "global suspend".
Alan Sterne583d9d2013-07-11 14:58:04 -04003136 *
3137 * However, many USB hubs have a bug: They don't relay wakeup requests
3138 * from a downstream port if the port's suspend feature isn't on.
3139 * Therefore we will turn on the suspend feature if udev or any of its
3140 * descendants is enabled for remote wakeup.
Alan Stern0aa28322013-03-27 16:14:19 -04003141 */
Alan Sterne583d9d2013-07-11 14:58:04 -04003142 else if (PMSG_IS_AUTO(msg) || wakeup_enabled_descendants(udev) > 0)
3143 status = set_port_feature(hub->hdev, port1,
3144 USB_PORT_FEAT_SUSPEND);
Alan Stern0aa28322013-03-27 16:14:19 -04003145 else {
3146 really_suspend = false;
3147 status = 0;
3148 }
Alan Stern624d6c02007-05-30 15:35:16 -04003149 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003150 dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status);
Alan Sterncbb33002011-06-15 16:29:16 -04003151
Alan Stern4fae6f02013-07-30 15:39:02 -04003152 /* Try to enable USB3 LPM and LTM again */
3153 usb_unlocked_enable_lpm(udev);
3154 err_lpm3:
3155 usb_enable_ltm(udev);
3156 err_ltm:
Andiry Xuc3e751e2012-05-05 00:50:10 +08003157 /* Try to enable USB2 hardware LPM again */
3158 if (udev->usb2_hw_lpm_capable == 1)
3159 usb_set_usb2_hardware_lpm(udev, 1);
3160
Alan Stern4fae6f02013-07-30 15:39:02 -04003161 if (udev->do_remote_wakeup)
3162 (void) usb_disable_remote_wakeup(udev);
3163 err_wakeup:
Sarah Sharp83060952012-05-02 14:25:52 -07003164
Alan Sterncbb33002011-06-15 16:29:16 -04003165 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02003166 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003167 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04003168 } else {
Alan Stern30b1a7a2011-09-27 21:54:22 +02003169 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
3170 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
3171 udev->do_remote_wakeup);
Alan Stern0aa28322013-03-27 16:14:19 -04003172 if (really_suspend) {
3173 udev->port_is_suspended = 1;
Alan Sterne583d9d2013-07-11 14:58:04 -04003174
3175 /* device has up to 10 msec to fully suspend */
Alan Stern0aa28322013-03-27 16:14:19 -04003176 msleep(10);
3177 }
Alan Sterne583d9d2013-07-11 14:58:04 -04003178 usb_set_device_state(udev, USB_STATE_SUSPENDED);
Alan Stern624d6c02007-05-30 15:35:16 -04003179 }
Lan Tianyuad493e52013-01-23 04:26:30 +08003180
Dan Williamsd5c38342014-05-20 18:08:52 -07003181 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled
3182 && test_and_clear_bit(port1, hub->child_usage_bits))
Lan Tianyu98a4f1f2013-07-03 22:17:54 +08003183 pm_runtime_put_sync(&port_dev->dev);
Lan Tianyuad493e52013-01-23 04:26:30 +08003184
Alan Sternc08512c2010-11-15 15:57:58 -05003185 usb_mark_last_busy(hub->hdev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07003186
3187 usb_unlock_port(port_dev);
Alan Stern4956ecc2007-05-30 16:51:28 -04003188 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189}
David Brownellf3f32532005-09-22 22:37:29 -07003190
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191/*
David Brownell390a8c32005-09-13 19:57:27 -07003192 * If the USB "suspend" state is in use (rather than "global suspend"),
3193 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04003194 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 * hardware resume signaling is finished, either because of selective
3196 * resume (by host) or remote wakeup (by device) ... now see what changed
3197 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04003198 *
3199 * If @udev->reset_resume is set then the device is reset before the
3200 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 */
Alan Stern140d8f62006-07-01 22:07:21 -04003202static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
Alan Stern54515fe2007-05-30 15:38:58 -04003204 int status = 0;
Oliver Neukum07e72b92012-11-29 15:05:57 +01003205 u16 devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003208 dev_dbg(&udev->dev, "%s\n",
3209 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211 /* usb ch9 identifies four variants of SUSPENDED, based on what
3212 * state the device resumes to. Linux currently won't see the
3213 * first two on the host side; they'd be inside hub_port_init()
3214 * during many timeouts, but khubd can't suspend until later.
3215 */
3216 usb_set_device_state(udev, udev->actconfig
3217 ? USB_STATE_CONFIGURED
3218 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Alan Stern54515fe2007-05-30 15:38:58 -04003220 /* 10.5.4.5 says not to reset a suspended port if the attached
3221 * device is enabled for remote wakeup. Hence the reset
3222 * operation is carried out here, after the port has been
3223 * resumed.
3224 */
Alan Stern1d102552014-03-18 10:39:05 -04003225 if (udev->reset_resume) {
3226 /*
3227 * If the device morphs or switches modes when it is reset,
3228 * we don't want to perform a reset-resume. We'll fail the
3229 * resume, which will cause a logical disconnect, and then
3230 * the device will be rediscovered.
3231 */
Alan Stern86c57ed2008-06-30 11:14:43 -04003232 retry_reset_resume:
Alan Stern1d102552014-03-18 10:39:05 -04003233 if (udev->quirks & USB_QUIRK_RESET)
3234 status = -ENODEV;
3235 else
3236 status = usb_reset_and_verify_device(udev);
3237 }
Alan Stern54515fe2007-05-30 15:38:58 -04003238
Matthias Beyer469271f2013-10-10 23:41:27 +02003239 /* 10.5.4.5 says be sure devices in the tree are still there.
3240 * For now let's assume the device didn't go crazy on resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 * and device drivers will know about any resume quirks.
3242 */
Alan Stern54515fe2007-05-30 15:38:58 -04003243 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04003244 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04003245 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
Alan Stern86c57ed2008-06-30 11:14:43 -04003246
3247 /* If a normal resume failed, try doing a reset-resume */
3248 if (status && !udev->reset_resume && udev->persist_enabled) {
3249 dev_dbg(&udev->dev, "retry with reset-resume\n");
3250 udev->reset_resume = 1;
3251 goto retry_reset_resume;
3252 }
Alan Stern54515fe2007-05-30 15:38:58 -04003253 }
Alan Sternb40b7a92006-06-19 15:12:38 -04003254
Alan Stern624d6c02007-05-30 15:35:16 -04003255 if (status) {
3256 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
3257 status);
Oliver Neukum07e72b92012-11-29 15:05:57 +01003258 /*
3259 * There are a few quirky devices which violate the standard
3260 * by claiming to have remote wakeup enabled after a reset,
3261 * which crash if the feature is cleared, hence check for
3262 * udev->reset_resume
3263 */
3264 } else if (udev->actconfig && !udev->reset_resume) {
Alan Stern28e861652013-07-30 15:37:33 -04003265 if (udev->speed < USB_SPEED_SUPER) {
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003266 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
Alan Stern28e861652013-07-30 15:37:33 -04003267 status = usb_disable_remote_wakeup(udev);
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003268 } else {
3269 status = usb_get_status(udev, USB_RECIP_INTERFACE, 0,
3270 &devstatus);
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003271 if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP
3272 | USB_INTRF_STAT_FUNC_RW))
Alan Stern28e861652013-07-30 15:37:33 -04003273 status = usb_disable_remote_wakeup(udev);
Alan Stern4bf0ba82005-11-21 11:58:07 -05003274 }
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003275
3276 if (status)
3277 dev_dbg(&udev->dev,
3278 "disable remote wakeup, status %d\n",
3279 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 }
3282 return status;
3283}
3284
Alan Stern624d6c02007-05-30 15:35:16 -04003285/*
Pratyush Ananda40178b2014-07-18 12:37:10 +05303286 * There are some SS USB devices which take longer time for link training.
3287 * XHCI specs 4.19.4 says that when Link training is successful, port
3288 * sets CSC bit to 1. So if SW reads port status before successful link
3289 * training, then it will not find device to be present.
3290 * USB Analyzer log with such buggy devices show that in some cases
3291 * device switch on the RX termination after long delay of host enabling
3292 * the VBUS. In few other cases it has been seen that device fails to
3293 * negotiate link training in first attempt. It has been
3294 * reported till now that few devices take as long as 2000 ms to train
3295 * the link after host enabling its VBUS and termination. Following
3296 * routine implements a 2000 ms timeout for link training. If in a case
3297 * link trains before timeout, loop will exit earlier.
3298 *
3299 * FIXME: If a device was connected before suspend, but was removed
3300 * while system was asleep, then the loop in the following routine will
3301 * only exit at timeout.
3302 *
3303 * This routine should only be called when persist is enabled for a SS
3304 * device.
3305 */
3306static int wait_for_ss_port_enable(struct usb_device *udev,
3307 struct usb_hub *hub, int *port1,
3308 u16 *portchange, u16 *portstatus)
3309{
3310 int status = 0, delay_ms = 0;
3311
3312 while (delay_ms < 2000) {
3313 if (status || *portstatus & USB_PORT_STAT_CONNECTION)
3314 break;
3315 msleep(20);
3316 delay_ms += 20;
3317 status = hub_port_status(hub, *port1, portstatus, portchange);
3318 }
3319 return status;
3320}
3321
3322/*
Alan Stern624d6c02007-05-30 15:35:16 -04003323 * usb_port_resume - re-activate a suspended usb device's upstream port
3324 * @udev: device to re-activate, not a root hub
3325 * Context: must be able to sleep; device not locked; pm locks held
3326 *
3327 * This will re-activate the suspended device, increasing power usage
3328 * while letting drivers communicate again with its endpoints.
3329 * USB resume explicitly guarantees that the power session between
3330 * the host and the device is the same as it was when the device
3331 * suspended.
3332 *
Alan Sternfeccc302008-03-03 15:15:59 -05003333 * If @udev->reset_resume is set then this routine won't check that the
3334 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04003335 * reset @udev. The end result is that a broken power session can be
3336 * recovered and @udev will appear to persist across a loss of VBUS power.
3337 *
3338 * For example, if a host controller doesn't maintain VBUS suspend current
3339 * during a system sleep or is reset when the system wakes up, all the USB
3340 * power sessions below it will be broken. This is especially troublesome
3341 * for mass-storage devices containing mounted filesystems, since the
3342 * device will appear to have disconnected and all the memory mappings
3343 * to it will be lost. Using the USB_PERSIST facility, the device can be
3344 * made to appear as if it had not disconnected.
3345 *
Ming Lei742120c2008-06-18 22:00:29 +08003346 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05003347 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04003348 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3349 * quite possible for a device to remain unaltered but its media to be
3350 * changed. If the user replaces a flash memory card while the system is
3351 * asleep, he will have only himself to blame when the filesystem on the
3352 * new card is corrupted and the system crashes.
3353 *
Alan Stern624d6c02007-05-30 15:35:16 -04003354 * Returns 0 on success, else negative errno.
3355 */
Alan Stern65bfd292008-11-25 16:39:18 -05003356int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
Lan Tianyuad493e52013-01-23 04:26:30 +08003358 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3359 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003360 int port1 = udev->portnum;
3361 int status;
3362 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003363
Dan Williamsd5c38342014-05-20 18:08:52 -07003364 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08003365 status = pm_runtime_get_sync(&port_dev->dev);
Lan Tianyuad493e52013-01-23 04:26:30 +08003366 if (status < 0) {
3367 dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
3368 status);
3369 return status;
3370 }
3371 }
3372
Dan Williams5c79a1e2014-05-20 18:09:26 -07003373 usb_lock_port(port_dev);
3374
Alan Sternd25450c2006-11-20 11:14:30 -05003375 /* Skip the initial Clear-Suspend step for a remote wakeup */
3376 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003377 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003378 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003381 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003382 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
Andiry Xua7114232011-04-27 18:07:50 +08003383 else
Lan Tianyuad493e52013-01-23 04:26:30 +08003384 status = usb_clear_port_feature(hub->hdev,
Andiry Xua7114232011-04-27 18:07:50 +08003385 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003387 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04003390 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003391 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 msleep(25);
3393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3395 * stop resume signaling. Then finish the resume
3396 * sequence.
3397 */
Alan Sternd25450c2006-11-20 11:14:30 -05003398 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003399
Alan Sternb01b03f2008-04-28 11:06:11 -04003400 /* TRSMRCY = 10 msec */
3401 msleep(10);
3402 }
Alan Stern54515fe2007-05-30 15:38:58 -04003403
Alan Sternb01b03f2008-04-28 11:06:11 -04003404 SuspendCleared:
3405 if (status == 0) {
Alan Sternbfd1e912012-10-19 11:03:39 -04003406 udev->port_is_suspended = 0;
Andiry Xua7114232011-04-27 18:07:50 +08003407 if (hub_is_superspeed(hub->hdev)) {
3408 if (portchange & USB_PORT_STAT_C_LINK_STATE)
Lan Tianyuad493e52013-01-23 04:26:30 +08003409 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003410 USB_PORT_FEAT_C_PORT_LINK_STATE);
3411 } else {
3412 if (portchange & USB_PORT_STAT_C_SUSPEND)
Lan Tianyuad493e52013-01-23 04:26:30 +08003413 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003414 USB_PORT_FEAT_C_SUSPEND);
3415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
Pratyush Ananda40178b2014-07-18 12:37:10 +05303418 if (udev->persist_enabled && hub_is_superspeed(hub->hdev))
3419 status = wait_for_ss_port_enable(udev, hub, &port1, &portchange,
3420 &portstatus);
3421
Alan Sternb01b03f2008-04-28 11:06:11 -04003422 status = check_port_resume_type(udev,
3423 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003424 if (status == 0)
3425 status = finish_port_resume(udev);
3426 if (status < 0) {
3427 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3428 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003429 } else {
3430 /* Try to enable USB2 hardware LPM */
3431 if (udev->usb2_hw_lpm_capable == 1)
3432 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003433
Sarah Sharpf74631e2012-06-25 12:08:08 -07003434 /* Try to enable USB3 LTM and LPM */
3435 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003436 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003437 }
Andiry Xu65580b432011-09-23 14:19:52 -07003438
Dan Williams5c79a1e2014-05-20 18:09:26 -07003439 usb_unlock_port(port_dev);
3440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 return status;
3442}
3443
Alan Stern84ebc102013-03-27 16:14:46 -04003444#ifdef CONFIG_PM_RUNTIME
3445
Alan Stern0534d462010-01-08 12:56:30 -05003446int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447{
3448 int status = 0;
3449
Dan Williams5c79a1e2014-05-20 18:09:26 -07003450 usb_lock_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003452 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003453 status = usb_autoresume_device(udev);
3454 if (status == 0) {
3455 /* Let the drivers do their thing, then... */
3456 usb_autosuspend_device(udev);
3457 }
Alan Sternd25450c2006-11-20 11:14:30 -05003458 }
Dan Williams5c79a1e2014-05-20 18:09:26 -07003459 usb_unlock_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 return status;
3461}
3462
Dan Williams7e73be22014-05-20 18:09:31 -07003463/* Returns 1 if there was a remote wakeup and a connect status change. */
3464static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
3465 u16 portstatus, u16 portchange)
3466 __must_hold(&port_dev->status_lock)
3467{
3468 struct usb_port *port_dev = hub->ports[port - 1];
3469 struct usb_device *hdev;
3470 struct usb_device *udev;
3471 int connect_change = 0;
3472 int ret;
3473
3474 hdev = hub->hdev;
3475 udev = port_dev->child;
3476 if (!hub_is_superspeed(hdev)) {
3477 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
3478 return 0;
3479 usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
3480 } else {
3481 if (!udev || udev->state != USB_STATE_SUSPENDED ||
3482 (portstatus & USB_PORT_STAT_LINK_STATE) !=
3483 USB_SS_PORT_LS_U0)
3484 return 0;
3485 }
3486
3487 if (udev) {
3488 /* TRSMRCY = 10 msec */
3489 msleep(10);
3490
3491 usb_unlock_port(port_dev);
3492 ret = usb_remote_wakeup(udev);
3493 usb_lock_port(port_dev);
3494 if (ret < 0)
3495 connect_change = 1;
3496 } else {
3497 ret = -ENODEV;
3498 hub_port_disable(hub, port, 1);
3499 }
3500 dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
3501 return connect_change;
3502}
3503
3504#else
3505
3506static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
3507 u16 portstatus, u16 portchange)
3508{
3509 return 0;
3510}
3511
Alan Sternd388dab2006-07-01 22:14:24 -04003512#endif
3513
Ming Leie6f30de2012-10-24 11:59:24 +08003514static int check_ports_changed(struct usb_hub *hub)
3515{
3516 int port1;
3517
3518 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3519 u16 portstatus, portchange;
3520 int status;
3521
3522 status = hub_port_status(hub, port1, &portstatus, &portchange);
3523 if (!status && portchange)
3524 return 1;
3525 }
3526 return 0;
3527}
3528
David Brownelldb690872005-09-13 19:56:33 -07003529static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
3531 struct usb_hub *hub = usb_get_intfdata (intf);
3532 struct usb_device *hdev = hub->hdev;
3533 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003534 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Alan Sterne583d9d2013-07-11 14:58:04 -04003536 /*
3537 * Warn if children aren't already suspended.
3538 * Also, add up the number of wakeup-enabled descendants.
3539 */
3540 hub->wakeup_enabled_descendants = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003542 struct usb_port *port_dev = hub->ports[port1 - 1];
3543 struct usb_device *udev = port_dev->child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Alan Stern6840d252007-09-10 11:34:26 -04003545 if (udev && udev->can_submit) {
Dan Williamsb658b8f2014-06-17 16:16:22 -07003546 dev_warn(&port_dev->dev, "device %s not suspended yet\n",
3547 dev_name(&udev->dev));
Alan Stern5b1b0b82011-08-19 23:49:48 +02003548 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003549 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003550 }
Alan Sterne583d9d2013-07-11 14:58:04 -04003551 if (udev)
3552 hub->wakeup_enabled_descendants +=
3553 wakeup_enabled_descendants(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
Ming Leie6f30de2012-10-24 11:59:24 +08003555
3556 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3557 /* check if there are changes pending on hub ports */
3558 if (check_ports_changed(hub)) {
3559 if (PMSG_IS_AUTO(msg))
3560 return -EBUSY;
3561 pm_wakeup_event(&hdev->dev, 2000);
3562 }
3563 }
3564
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003565 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3566 /* Enable hub to send remote wakeup for all ports. */
3567 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3568 status = set_port_feature(hdev,
3569 port1 |
3570 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3571 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3572 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3573 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3574 }
3575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
Harvey Harrison441b62c2008-03-03 16:08:34 -08003577 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003578
David Brownellc9f89fa2005-09-13 19:57:04 -07003579 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003580 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582}
3583
3584static int hub_resume(struct usb_interface *intf)
3585{
Alan Stern5e6effa2008-03-03 15:15:51 -05003586 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
Alan Stern5e6effa2008-03-03 15:15:51 -05003588 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003589 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 return 0;
3591}
3592
Alan Sternb41a60e2007-05-30 15:39:33 -04003593static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003594{
Alan Sternb41a60e2007-05-30 15:39:33 -04003595 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003596
Alan Stern5e6effa2008-03-03 15:15:51 -05003597 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003598 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003599 return 0;
3600}
3601
Alan Stern54515fe2007-05-30 15:38:58 -04003602/**
3603 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3604 * @rhdev: struct usb_device for the root hub
3605 *
3606 * The USB host controller driver calls this function when its root hub
3607 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003608 * has been reset. The routine marks @rhdev as having lost power.
3609 * When the hub driver is resumed it will take notice and carry out
3610 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3611 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003612 */
3613void usb_root_hub_lost_power(struct usb_device *rhdev)
3614{
3615 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3616 rhdev->reset_resume = 1;
3617}
3618EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3619
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003620static const char * const usb3_lpm_names[] = {
3621 "U0",
3622 "U1",
3623 "U2",
3624 "U3",
3625};
3626
3627/*
3628 * Send a Set SEL control transfer to the device, prior to enabling
3629 * device-initiated U1 or U2. This lets the device know the exit latencies from
3630 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3631 * packet from the host.
3632 *
3633 * This function will fail if the SEL or PEL values for udev are greater than
3634 * the maximum allowed values for the link state to be enabled.
3635 */
3636static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3637{
3638 struct usb_set_sel_req *sel_values;
3639 unsigned long long u1_sel;
3640 unsigned long long u1_pel;
3641 unsigned long long u2_sel;
3642 unsigned long long u2_pel;
3643 int ret;
3644
Xenia Ragiadakou38d7f682013-09-04 17:24:45 +03003645 if (udev->state != USB_STATE_CONFIGURED)
3646 return 0;
3647
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003648 /* Convert SEL and PEL stored in ns to us */
3649 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3650 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3651 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3652 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3653
3654 /*
3655 * Make sure that the calculated SEL and PEL values for the link
3656 * state we're enabling aren't bigger than the max SEL/PEL
3657 * value that will fit in the SET SEL control transfer.
3658 * Otherwise the device would get an incorrect idea of the exit
3659 * latency for the link state, and could start a device-initiated
3660 * U1/U2 when the exit latencies are too high.
3661 */
3662 if ((state == USB3_LPM_U1 &&
3663 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3664 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3665 (state == USB3_LPM_U2 &&
3666 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3667 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003668 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 -07003669 usb3_lpm_names[state], u1_sel, u1_pel);
3670 return -EINVAL;
3671 }
3672
3673 /*
3674 * If we're enabling device-initiated LPM for one link state,
3675 * but the other link state has a too high SEL or PEL value,
3676 * just set those values to the max in the Set SEL request.
3677 */
3678 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3679 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3680
3681 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3682 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3683
3684 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3685 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3686
3687 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3688 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3689
3690 /*
3691 * usb_enable_lpm() can be called as part of a failed device reset,
3692 * which may be initiated by an error path of a mass storage driver.
3693 * Therefore, use GFP_NOIO.
3694 */
3695 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3696 if (!sel_values)
3697 return -ENOMEM;
3698
3699 sel_values->u1_sel = u1_sel;
3700 sel_values->u1_pel = u1_pel;
3701 sel_values->u2_sel = cpu_to_le16(u2_sel);
3702 sel_values->u2_pel = cpu_to_le16(u2_pel);
3703
3704 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3705 USB_REQ_SET_SEL,
3706 USB_RECIP_DEVICE,
3707 0, 0,
3708 sel_values, sizeof *(sel_values),
3709 USB_CTRL_SET_TIMEOUT);
3710 kfree(sel_values);
3711 return ret;
3712}
3713
3714/*
3715 * Enable or disable device-initiated U1 or U2 transitions.
3716 */
3717static int usb_set_device_initiated_lpm(struct usb_device *udev,
3718 enum usb3_link_state state, bool enable)
3719{
3720 int ret;
3721 int feature;
3722
3723 switch (state) {
3724 case USB3_LPM_U1:
3725 feature = USB_DEVICE_U1_ENABLE;
3726 break;
3727 case USB3_LPM_U2:
3728 feature = USB_DEVICE_U2_ENABLE;
3729 break;
3730 default:
3731 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3732 __func__, enable ? "enable" : "disable");
3733 return -EINVAL;
3734 }
3735
3736 if (udev->state != USB_STATE_CONFIGURED) {
3737 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3738 "for unconfigured device.\n",
3739 __func__, enable ? "enable" : "disable",
3740 usb3_lpm_names[state]);
3741 return 0;
3742 }
3743
3744 if (enable) {
3745 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003746 * Now send the control transfer to enable device-initiated LPM
3747 * for either U1 or U2.
3748 */
3749 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3750 USB_REQ_SET_FEATURE,
3751 USB_RECIP_DEVICE,
3752 feature,
3753 0, NULL, 0,
3754 USB_CTRL_SET_TIMEOUT);
3755 } else {
3756 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3757 USB_REQ_CLEAR_FEATURE,
3758 USB_RECIP_DEVICE,
3759 feature,
3760 0, NULL, 0,
3761 USB_CTRL_SET_TIMEOUT);
3762 }
3763 if (ret < 0) {
3764 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3765 enable ? "Enable" : "Disable",
3766 usb3_lpm_names[state]);
3767 return -EBUSY;
3768 }
3769 return 0;
3770}
3771
3772static int usb_set_lpm_timeout(struct usb_device *udev,
3773 enum usb3_link_state state, int timeout)
3774{
3775 int ret;
3776 int feature;
3777
3778 switch (state) {
3779 case USB3_LPM_U1:
3780 feature = USB_PORT_FEAT_U1_TIMEOUT;
3781 break;
3782 case USB3_LPM_U2:
3783 feature = USB_PORT_FEAT_U2_TIMEOUT;
3784 break;
3785 default:
3786 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3787 __func__);
3788 return -EINVAL;
3789 }
3790
3791 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3792 timeout != USB3_LPM_DEVICE_INITIATED) {
3793 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3794 "which is a reserved value.\n",
3795 usb3_lpm_names[state], timeout);
3796 return -EINVAL;
3797 }
3798
3799 ret = set_port_feature(udev->parent,
3800 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3801 feature);
3802 if (ret < 0) {
3803 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3804 "error code %i\n", usb3_lpm_names[state],
3805 timeout, ret);
3806 return -EBUSY;
3807 }
3808 if (state == USB3_LPM_U1)
3809 udev->u1_params.timeout = timeout;
3810 else
3811 udev->u2_params.timeout = timeout;
3812 return 0;
3813}
3814
3815/*
3816 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3817 * U1/U2 entry.
3818 *
3819 * We will attempt to enable U1 or U2, but there are no guarantees that the
3820 * control transfers to set the hub timeout or enable device-initiated U1/U2
3821 * will be successful.
3822 *
3823 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3824 * driver know about it. If that call fails, it should be harmless, and just
3825 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3826 */
3827static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3828 enum usb3_link_state state)
3829{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003830 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003831 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3832 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3833
3834 /* If the device says it doesn't have *any* exit latency to come out of
3835 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3836 * state.
3837 */
3838 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3839 (state == USB3_LPM_U2 && u2_mel == 0))
3840 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003841
Sarah Sharp65a95b72012-10-05 10:32:07 -07003842 /*
3843 * First, let the device know about the exit latencies
3844 * associated with the link state we're about to enable.
3845 */
3846 ret = usb_req_set_sel(udev, state);
3847 if (ret < 0) {
3848 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3849 usb3_lpm_names[state]);
3850 return;
3851 }
3852
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003853 /* We allow the host controller to set the U1/U2 timeout internally
3854 * first, so that it can change its schedule to account for the
3855 * additional latency to send data to a device in a lower power
3856 * link state.
3857 */
3858 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3859
3860 /* xHCI host controller doesn't want to enable this LPM state. */
3861 if (timeout == 0)
3862 return;
3863
3864 if (timeout < 0) {
3865 dev_warn(&udev->dev, "Could not enable %s link state, "
3866 "xHCI error %i.\n", usb3_lpm_names[state],
3867 timeout);
3868 return;
3869 }
3870
3871 if (usb_set_lpm_timeout(udev, state, timeout))
3872 /* If we can't set the parent hub U1/U2 timeout,
3873 * device-initiated LPM won't be allowed either, so let the xHCI
3874 * host know that this link state won't be enabled.
3875 */
3876 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3877
3878 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3879 else if (udev->actconfig)
3880 usb_set_device_initiated_lpm(udev, state, true);
3881
3882}
3883
3884/*
3885 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3886 * U1/U2 entry.
3887 *
3888 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3889 * If zero is returned, the parent will not allow the link to go into U1/U2.
3890 *
3891 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3892 * it won't have an effect on the bus link state because the parent hub will
3893 * still disallow device-initiated U1/U2 entry.
3894 *
3895 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3896 * possible. The result will be slightly more bus bandwidth will be taken up
3897 * (to account for U1/U2 exit latency), but it should be harmless.
3898 */
3899static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3900 enum usb3_link_state state)
3901{
3902 int feature;
3903
3904 switch (state) {
3905 case USB3_LPM_U1:
3906 feature = USB_PORT_FEAT_U1_TIMEOUT;
3907 break;
3908 case USB3_LPM_U2:
3909 feature = USB_PORT_FEAT_U2_TIMEOUT;
3910 break;
3911 default:
3912 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3913 __func__);
3914 return -EINVAL;
3915 }
3916
3917 if (usb_set_lpm_timeout(udev, state, 0))
3918 return -EBUSY;
3919
3920 usb_set_device_initiated_lpm(udev, state, false);
3921
3922 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3923 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3924 "bus schedule bandwidth may be impacted.\n",
3925 usb3_lpm_names[state]);
3926 return 0;
3927}
3928
3929/*
3930 * Disable hub-initiated and device-initiated U1 and U2 entry.
3931 * Caller must own the bandwidth_mutex.
3932 *
3933 * This will call usb_enable_lpm() on failure, which will decrement
3934 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3935 */
3936int usb_disable_lpm(struct usb_device *udev)
3937{
3938 struct usb_hcd *hcd;
3939
3940 if (!udev || !udev->parent ||
3941 udev->speed != USB_SPEED_SUPER ||
Pratyush Anand51df62f2014-07-04 17:01:27 +03003942 !udev->lpm_capable ||
3943 udev->state < USB_STATE_DEFAULT)
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003944 return 0;
3945
3946 hcd = bus_to_hcd(udev->bus);
3947 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3948 return 0;
3949
3950 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003951 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003952 return 0;
3953
3954 /* If LPM is enabled, attempt to disable it. */
3955 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3956 goto enable_lpm;
3957 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3958 goto enable_lpm;
3959
3960 return 0;
3961
3962enable_lpm:
3963 usb_enable_lpm(udev);
3964 return -EBUSY;
3965}
3966EXPORT_SYMBOL_GPL(usb_disable_lpm);
3967
3968/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3969int usb_unlocked_disable_lpm(struct usb_device *udev)
3970{
3971 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3972 int ret;
3973
3974 if (!hcd)
3975 return -EINVAL;
3976
3977 mutex_lock(hcd->bandwidth_mutex);
3978 ret = usb_disable_lpm(udev);
3979 mutex_unlock(hcd->bandwidth_mutex);
3980
3981 return ret;
3982}
3983EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3984
3985/*
3986 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3987 * xHCI host policy may prevent U1 or U2 from being enabled.
3988 *
3989 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3990 * until the lpm_disable_count drops to zero. Caller must own the
3991 * bandwidth_mutex.
3992 */
3993void usb_enable_lpm(struct usb_device *udev)
3994{
3995 struct usb_hcd *hcd;
3996
3997 if (!udev || !udev->parent ||
3998 udev->speed != USB_SPEED_SUPER ||
Pratyush Anand51df62f2014-07-04 17:01:27 +03003999 !udev->lpm_capable ||
4000 udev->state < USB_STATE_DEFAULT)
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004001 return;
4002
4003 udev->lpm_disable_count--;
4004 hcd = bus_to_hcd(udev->bus);
4005 /* Double check that we can both enable and disable LPM.
4006 * Device must be configured to accept set feature U1/U2 timeout.
4007 */
4008 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
4009 !hcd->driver->disable_usb3_lpm_timeout)
4010 return;
4011
4012 if (udev->lpm_disable_count > 0)
4013 return;
4014
4015 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
4016 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
4017}
4018EXPORT_SYMBOL_GPL(usb_enable_lpm);
4019
4020/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
4021void usb_unlocked_enable_lpm(struct usb_device *udev)
4022{
4023 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4024
4025 if (!hcd)
4026 return;
4027
4028 mutex_lock(hcd->bandwidth_mutex);
4029 usb_enable_lpm(udev);
4030 mutex_unlock(hcd->bandwidth_mutex);
4031}
4032EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
4033
4034
Alan Sternd388dab2006-07-01 22:14:24 -04004035#else /* CONFIG_PM */
4036
Alan Sternf07600c2007-05-30 15:38:16 -04004037#define hub_suspend NULL
4038#define hub_resume NULL
4039#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004040
4041int usb_disable_lpm(struct usb_device *udev)
4042{
4043 return 0;
4044}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004045EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004046
4047void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004048EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004049
4050int usb_unlocked_disable_lpm(struct usb_device *udev)
4051{
4052 return 0;
4053}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004054EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004055
4056void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004057EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07004058
4059int usb_disable_ltm(struct usb_device *udev)
4060{
4061 return 0;
4062}
4063EXPORT_SYMBOL_GPL(usb_disable_ltm);
4064
4065void usb_enable_ltm(struct usb_device *udev) { }
4066EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternc4b51a42013-07-11 14:58:23 -04004067
Stephen Rothwell4a95b1f2014-05-29 18:55:06 +10004068static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
4069 u16 portstatus, u16 portchange)
4070{
4071 return 0;
4072}
4073
Alan Sternc4b51a42013-07-11 14:58:23 -04004074#endif /* CONFIG_PM */
Alan Sternd388dab2006-07-01 22:14:24 -04004075
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
4077/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
4078 *
4079 * Between connect detection and reset signaling there must be a delay
4080 * of 100ms at least for debounce and power-settling. The corresponding
4081 * timer shall restart whenever the downstream port detects a disconnect.
Matthias Beyer469271f2013-10-10 23:41:27 +02004082 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 * Apparently there are some bluetooth and irda-dongles and a number of
4084 * low-speed devices for which this debounce period may last over a second.
4085 * Not covered by the spec - but easy to deal with.
4086 *
4087 * This implementation uses a 1500ms total debounce timeout; if the
4088 * connection isn't stable by then it returns -ETIMEDOUT. It checks
4089 * every 25ms for transient disconnects. When the port status has been
4090 * unchanged for 100ms it returns the port status.
4091 */
Lan Tianyuad493e52013-01-23 04:26:30 +08004092int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093{
4094 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 u16 portchange, portstatus;
4096 unsigned connection = 0xffff;
Dan Williamsd99f6b42014-05-20 18:08:17 -07004097 int total_time, stable_time = 0;
4098 struct usb_port *port_dev = hub->ports[port1 - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
4100 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
4101 ret = hub_port_status(hub, port1, &portstatus, &portchange);
4102 if (ret < 0)
4103 return ret;
4104
4105 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
4106 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004107 if (!must_be_connected ||
4108 (connection == USB_PORT_STAT_CONNECTION))
4109 stable_time += HUB_DEBOUNCE_STEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 if (stable_time >= HUB_DEBOUNCE_STABLE)
4111 break;
4112 } else {
4113 stable_time = 0;
4114 connection = portstatus & USB_PORT_STAT_CONNECTION;
4115 }
4116
4117 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004118 usb_clear_port_feature(hub->hdev, port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 USB_PORT_FEAT_C_CONNECTION);
4120 }
4121
4122 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
4123 break;
4124 msleep(HUB_DEBOUNCE_STEP);
4125 }
4126
Dan Williamsd99f6b42014-05-20 18:08:17 -07004127 dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n",
4128 total_time, stable_time, portstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
4130 if (stable_time < HUB_DEBOUNCE_STABLE)
4131 return -ETIMEDOUT;
4132 return portstatus;
4133}
4134
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004135void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136{
Alan Sternddeac4e2009-01-15 17:03:33 -05004137 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
4138 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05004139 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004141EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
4143#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
4144#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
4145
Alan Stern4326ed02007-07-30 17:08:43 -04004146static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
4148 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07004149 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Sarah Sharpc6515272009-04-27 19:57:26 -07004151 /*
4152 * The host controller will choose the device address,
4153 * instead of the core having chosen it earlier
4154 */
4155 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 return -EINVAL;
4157 if (udev->state == USB_STATE_ADDRESS)
4158 return 0;
4159 if (udev->state != USB_STATE_DEFAULT)
4160 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07004161 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07004162 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004163 else
Sarah Sharpc6515272009-04-27 19:57:26 -07004164 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
4165 USB_REQ_SET_ADDRESS, 0, devnum, 0,
4166 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05004168 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07004169 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004171 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 }
4173 return retval;
4174}
4175
Mathias Nyman890dae82013-09-30 17:26:31 +03004176/*
4177 * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM
4178 * when they're plugged into a USB 2.0 port, but they don't work when LPM is
4179 * enabled.
4180 *
4181 * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the
4182 * device says it supports the new USB 2.0 Link PM errata by setting the BESL
4183 * support bit in the BOS descriptor.
4184 */
4185static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev)
4186{
Dan Williamsd99f6b42014-05-20 18:08:17 -07004187 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
4188 int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
Mathias Nyman890dae82013-09-30 17:26:31 +03004189
4190 if (!udev->usb2_hw_lpm_capable)
4191 return;
4192
Dan Williamsd99f6b42014-05-20 18:08:17 -07004193 if (hub)
4194 connect_type = hub->ports[udev->portnum - 1]->connect_type;
Mathias Nyman890dae82013-09-30 17:26:31 +03004195
Bjørn Mork23c05822014-02-27 14:23:55 +01004196 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) ||
Mathias Nyman890dae82013-09-30 17:26:31 +03004197 connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
4198 udev->usb2_hw_lpm_allowed = 1;
4199 usb_set_usb2_hardware_lpm(udev, 1);
4200 }
4201}
4202
Dan Williams48fc7db2013-12-05 17:07:27 -08004203static int hub_enable_device(struct usb_device *udev)
4204{
4205 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4206
4207 if (!hcd->driver->enable_device)
4208 return 0;
4209 if (udev->state == USB_STATE_ADDRESS)
4210 return 0;
4211 if (udev->state != USB_STATE_DEFAULT)
4212 return -EINVAL;
4213
4214 return hcd->driver->enable_device(hcd, udev);
4215}
4216
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217/* Reset device, (re)assign address, get device descriptor.
4218 * Device connection must be stable, no more debouncing needed.
4219 * Returns device in USB_STATE_ADDRESS, except on error.
4220 *
4221 * If this is called for an already-existing device (as part of
Dan Williams5c79a1e2014-05-20 18:09:26 -07004222 * usb_reset_and_verify_device), the caller must own the device lock and
4223 * the port lock. For a newly detected device that is not accessible
4224 * through any global pointers, it's not necessary to lock the device,
4225 * but it is still necessary to lock the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 */
4227static int
4228hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
4229 int retry_counter)
4230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07004232 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 int i, j, retval;
4234 unsigned delay = HUB_SHORT_RESET_TIME;
4235 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004236 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04004237 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
4239 /* root hub ports have a slightly longer reset period
4240 * (from USB 2.0 spec, section 7.1.7.5)
4241 */
4242 if (!hdev->parent) {
4243 delay = HUB_ROOT_RESET_TIME;
4244 if (port1 == hdev->bus->otg_port)
4245 hdev->bus->b_hnp_enable = 0;
4246 }
4247
4248 /* Some low speed devices have problems with the quick delay, so */
4249 /* be a bit pessimistic with those devices. RHbug #23670 */
4250 if (oldspeed == USB_SPEED_LOW)
4251 delay = HUB_LONG_RESET_TIME;
4252
Todd E Brandt6fecd4f2014-05-19 10:55:32 -07004253 mutex_lock(&hdev->bus->usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Luben Tuikov07194ab2011-02-11 11:33:10 -08004255 /* Reset the device; full speed may morph to high speed */
4256 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07004257 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08004258 if (retval < 0) /* error or disconnect */
4259 goto fail;
4260 /* success, speed is known */
4261
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 retval = -ENODEV;
4263
4264 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
4265 dev_dbg(&udev->dev, "device reset changed speed!\n");
4266 goto fail;
4267 }
4268 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04004269
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
4271 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004272 * For Wireless USB devices, ep0 max packet is always 512 (tho
4273 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 */
4275 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07004276 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08004277 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004278 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004279 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004281 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 break;
4283 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
4284 /* to determine the ep0 maxpacket size, try to read
4285 * the device descriptor to get bMaxPacketSize0 and
4286 * then correct our initial guess.
4287 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004288 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 break;
4290 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004291 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 break;
4293 default:
4294 goto fail;
4295 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004296
4297 if (udev->speed == USB_SPEED_WIRELESS)
4298 speed = "variable speed Wireless";
4299 else
4300 speed = usb_speed_string(udev->speed);
4301
Sarah Sharpc6515272009-04-27 19:57:26 -07004302 if (udev->speed != USB_SPEED_SUPER)
4303 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004304 "%s %s USB device number %d using %s\n",
4305 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05004306 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
4308 /* Set up TT records, if needed */
4309 if (hdev->tt) {
4310 udev->tt = hdev->tt;
4311 udev->ttport = hdev->ttport;
4312 } else if (udev->speed != USB_SPEED_HIGH
4313 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05004314 if (!hub->tt.hub) {
4315 dev_err(&udev->dev, "parent hub has no TT\n");
4316 retval = -EINVAL;
4317 goto fail;
4318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 udev->tt = &hub->tt;
4320 udev->ttport = port1;
4321 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004322
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
4324 * Because device hardware and firmware is sometimes buggy in
4325 * this area, and this is how Linux has done it for ages.
4326 * Change it cautiously.
4327 *
Dan Williams48fc7db2013-12-05 17:07:27 -08004328 * NOTE: If use_new_scheme() is true we will start by issuing
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
4330 * so it may help with some non-standards-compliant devices.
4331 * Otherwise we start with SET_ADDRESS and then try to read the
4332 * first 8 bytes of the device descriptor to get the ep0 maxpacket
4333 * value.
4334 */
4335 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Dan Williams48fc7db2013-12-05 17:07:27 -08004336 bool did_new_scheme = false;
4337
4338 if (use_new_scheme(udev, retry_counter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 struct usb_device_descriptor *buf;
4340 int r = 0;
4341
Dan Williams48fc7db2013-12-05 17:07:27 -08004342 did_new_scheme = true;
4343 retval = hub_enable_device(udev);
Oliver Neukum938569e2014-02-27 10:57:10 +01004344 if (retval < 0) {
4345 dev_err(&udev->dev,
4346 "hub failed to enable device, error %d\n",
4347 retval);
Dan Williams48fc7db2013-12-05 17:07:27 -08004348 goto fail;
Oliver Neukum938569e2014-02-27 10:57:10 +01004349 }
Dan Williams48fc7db2013-12-05 17:07:27 -08004350
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351#define GET_DESCRIPTOR_BUFSIZE 64
4352 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
4353 if (!buf) {
4354 retval = -ENOMEM;
4355 continue;
4356 }
4357
Alan Sternb89ee192007-05-11 10:19:04 -04004358 /* Retry on all errors; some devices are flakey.
4359 * 255 is for WUSB devices, we actually need to use
4360 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 */
4362 for (j = 0; j < 3; ++j) {
4363 buf->bMaxPacketSize0 = 0;
4364 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
4365 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
4366 USB_DT_DEVICE << 8, 0,
4367 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02004368 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004370 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 if (buf->bDescriptorType ==
4372 USB_DT_DEVICE) {
4373 r = 0;
4374 break;
4375 }
4376 /* FALL THROUGH */
4377 default:
4378 if (r == 0)
4379 r = -EPROTO;
4380 break;
4381 }
4382 if (r == 0)
4383 break;
4384 }
4385 udev->descriptor.bMaxPacketSize0 =
4386 buf->bMaxPacketSize0;
4387 kfree(buf);
4388
Andiry Xu75d7cf72011-09-13 16:41:11 -07004389 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 if (retval < 0) /* error or disconnect */
4391 goto fail;
4392 if (oldspeed != udev->speed) {
4393 dev_dbg(&udev->dev,
4394 "device reset changed speed!\n");
4395 retval = -ENODEV;
4396 goto fail;
4397 }
4398 if (r) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004399 if (r != -ENODEV)
4400 dev_err(&udev->dev, "device descriptor read/64, error %d\n",
4401 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 retval = -EMSGSIZE;
4403 continue;
4404 }
4405#undef GET_DESCRIPTOR_BUFSIZE
4406 }
4407
Matthias Beyer469271f2013-10-10 23:41:27 +02004408 /*
4409 * If device is WUSB, we already assigned an
4410 * unauthorized address in the Connect Ack sequence;
4411 * authorization will assign the final address.
4412 */
Sarah Sharpc6515272009-04-27 19:57:26 -07004413 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004414 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
4415 retval = hub_set_address(udev, devnum);
4416 if (retval >= 0)
4417 break;
4418 msleep(200);
4419 }
4420 if (retval < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004421 if (retval != -ENODEV)
4422 dev_err(&udev->dev, "device not accepting address %d, error %d\n",
4423 devnum, retval);
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004424 goto fail;
4425 }
Sarah Sharpc6515272009-04-27 19:57:26 -07004426 if (udev->speed == USB_SPEED_SUPER) {
4427 devnum = udev->devnum;
4428 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05004429 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07004430 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05004431 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07004432 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004433
4434 /* cope with hardware quirkiness:
4435 * - let SET_ADDRESS settle, some device hardware wants it
4436 * - read ep0 maxpacket even for high and low speed,
4437 */
4438 msleep(10);
Dan Williams48fc7db2013-12-05 17:07:27 -08004439 /* use_new_scheme() checks the speed which may have
4440 * changed since the initial look so we cache the result
4441 * in did_new_scheme
4442 */
4443 if (did_new_scheme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 break;
Matthias Beyer469271f2013-10-10 23:41:27 +02004445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
4447 retval = usb_get_device_descriptor(udev, 8);
4448 if (retval < 8) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004449 if (retval != -ENODEV)
4450 dev_err(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004451 "device descriptor read/8, error %d\n",
4452 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 if (retval >= 0)
4454 retval = -EMSGSIZE;
4455 } else {
4456 retval = 0;
4457 break;
4458 }
4459 }
4460 if (retval)
4461 goto fail;
4462
Peter Chenb76baa82012-11-09 09:44:43 +08004463 if (hcd->phy && !hdev->parent)
Peter Chenac965112012-11-09 09:44:44 +08004464 usb_phy_notify_connect(hcd->phy, udev->speed);
Peter Chenb76baa82012-11-09 09:44:43 +08004465
Elric Fud8aec3d2012-03-26 21:16:02 +08004466 /*
4467 * Some superspeed devices have finished the link training process
4468 * and attached to a superspeed hub port, but the device descriptor
4469 * got from those devices show they aren't superspeed devices. Warm
4470 * reset the port attached by the devices can fix them.
4471 */
4472 if ((udev->speed == USB_SPEED_SUPER) &&
4473 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4474 dev_err(&udev->dev, "got a wrong device descriptor, "
4475 "warm reset device\n");
4476 hub_port_reset(hub, port1, udev,
4477 HUB_BH_RESET_TIME, true);
4478 retval = -EINVAL;
4479 goto fail;
4480 }
4481
Sarah Sharp6b403b02009-04-27 19:54:10 -07004482 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
4483 udev->speed == USB_SPEED_SUPER)
4484 i = 512;
4485 else
4486 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07004487 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04004488 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04004490 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 retval = -EMSGSIZE;
4492 goto fail;
4493 }
Alan Stern56626a72010-10-14 15:25:21 -04004494 if (udev->speed == USB_SPEED_FULL)
4495 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4496 else
4497 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004499 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004501
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4503 if (retval < (signed)sizeof(udev->descriptor)) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004504 if (retval != -ENODEV)
4505 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4506 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 if (retval >= 0)
4508 retval = -ENOMSG;
4509 goto fail;
4510 }
4511
Andiry Xu1ff4df52011-09-23 14:19:48 -07004512 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4513 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004514 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004515 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004516 usb_set_lpm_parameters(udev);
4517 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004518 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004519
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004521 /* notify HCD that we have a device connected and addressed */
4522 if (hcd->driver->update_device)
4523 hcd->driver->update_device(hcd, udev);
Mathias Nyman890dae82013-09-30 17:26:31 +03004524 hub_set_initial_usb2_lpm_policy(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004526 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004528 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004529 }
Todd E Brandt6fecd4f2014-05-19 10:55:32 -07004530 mutex_unlock(&hdev->bus->usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 return retval;
4532}
4533
4534static void
4535check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4536{
4537 struct usb_qualifier_descriptor *qual;
4538 int status;
4539
Johan Hovold2a159382014-08-25 17:51:26 +02004540 if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER)
4541 return;
4542
Christoph Lametere94b1762006-12-06 20:33:17 -08004543 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 if (qual == NULL)
4545 return;
4546
4547 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4548 qual, sizeof *qual);
4549 if (status == sizeof *qual) {
4550 dev_info(&udev->dev, "not running at top speed; "
4551 "connect to a high speed hub\n");
4552 /* hub LEDs are probably harder to miss than syslog */
4553 if (hub->has_indicators) {
4554 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08004555 queue_delayed_work(system_power_efficient_wq,
4556 &hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 }
4558 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004559 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560}
4561
4562static unsigned
4563hub_power_remaining (struct usb_hub *hub)
4564{
4565 struct usb_device *hdev = hub->hdev;
4566 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004567 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
Alan Stern55c52712005-11-23 12:03:12 -05004569 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 return 0;
4571
Alan Stern55c52712005-11-23 12:03:12 -05004572 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4573 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004574 struct usb_port *port_dev = hub->ports[port1 - 1];
4575 struct usb_device *udev = port_dev->child;
4576 unsigned unit_load;
4577 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
4579 if (!udev)
4580 continue;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004581 if (hub_is_superspeed(udev))
4582 unit_load = 150;
4583 else
4584 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004586 /*
4587 * Unconfigured devices may not use more than one unit load,
4588 * or 8mA for OTG ports
4589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 if (udev->actconfig)
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +01004591 delta = usb_get_max_power(udev, udev->actconfig);
Alan Stern55c52712005-11-23 12:03:12 -05004592 else if (port1 != udev->bus->otg_port || hdev->parent)
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004593 delta = unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 else
Alan Stern55c52712005-11-23 12:03:12 -05004595 delta = 8;
4596 if (delta > hub->mA_per_port)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004597 dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n",
4598 delta, hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 remaining -= delta;
4600 }
4601 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004602 dev_warn(hub->intfdev, "%dmA over power budget!\n",
Matthias Beyer469271f2013-10-10 23:41:27 +02004603 -remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 remaining = 0;
4605 }
4606 return remaining;
4607}
4608
Dan Williamsaf376a42014-05-20 18:09:15 -07004609static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
4610 u16 portchange)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 int status, i;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004613 unsigned unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 struct usb_device *hdev = hub->hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004616 struct usb_port *port_dev = hub->ports[port1 - 1];
Dan Williamsaf376a42014-05-20 18:09:15 -07004617 struct usb_device *udev = port_dev->child;
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004618 static int unreliable_port = -1;
Alan Stern8808f002008-04-28 11:06:55 -04004619
Alan Stern24618b02008-04-28 11:06:28 -04004620 /* Disconnect any existing devices under this port */
Peter Chenb76baa82012-11-09 09:44:43 +08004621 if (udev) {
4622 if (hcd->phy && !hdev->parent &&
4623 !(portstatus & USB_PORT_STAT_CONNECTION))
Peter Chenac965112012-11-09 09:44:44 +08004624 usb_phy_notify_disconnect(hcd->phy, udev->speed);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004625 usb_disconnect(&port_dev->child);
Peter Chenb76baa82012-11-09 09:44:43 +08004626 }
Alan Stern24618b02008-04-28 11:06:28 -04004627
Alan Stern253e0572009-10-27 15:20:13 -04004628 /* We can forget about a "removed" device when there's a physical
4629 * disconnect or the connect status changes.
4630 */
4631 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4632 (portchange & USB_PORT_STAT_C_CONNECTION))
4633 clear_bit(port1, hub->removed_bits);
4634
Alan Stern5257d972008-09-22 14:43:08 -04004635 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4636 USB_PORT_STAT_C_ENABLE)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004637 status = hub_port_debounce_be_stable(hub, port1);
Alan Stern5257d972008-09-22 14:43:08 -04004638 if (status < 0) {
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004639 if (status != -ENODEV &&
4640 port1 != unreliable_port &&
4641 printk_ratelimit())
Takashi Iwaidd5f5002014-08-19 17:37:55 +02004642 dev_err(&port_dev->dev, "connect-debounce failed\n");
Alan Stern5257d972008-09-22 14:43:08 -04004643 portstatus &= ~USB_PORT_STAT_CONNECTION;
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004644 unreliable_port = port1;
Alan Stern5257d972008-09-22 14:43:08 -04004645 } else {
4646 portstatus = status;
4647 }
4648 }
4649
Alan Stern253e0572009-10-27 15:20:13 -04004650 /* Return now if debouncing failed or nothing is connected or
4651 * the device was "removed".
4652 */
4653 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4654 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655
4656 /* maybe switch power back on (e.g. root hub was reset) */
Dan Williams9262c192014-05-20 18:08:12 -07004657 if (hub_is_port_power_switchable(hub)
Andiry Xu0ed9a572011-04-27 18:07:43 +08004658 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004660
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 if (portstatus & USB_PORT_STAT_ENABLE)
Matthias Beyer469271f2013-10-10 23:41:27 +02004662 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 return;
4664 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004665 if (hub_is_superspeed(hub->hdev))
4666 unit_load = 150;
4667 else
4668 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
Alan Sterne9e88fb2013-03-27 16:14:01 -04004670 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
4673 /* reallocate for each attempt, since references
4674 * to the previous one can escape in various ways
4675 */
4676 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4677 if (!udev) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004678 dev_err(&port_dev->dev,
4679 "couldn't allocate usb_device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 goto done;
4681 }
4682
4683 usb_set_device_state(udev, USB_STATE_POWERED);
Matthias Beyer469271f2013-10-10 23:41:27 +02004684 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004685 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004686 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004687
Sarah Sharp131dec32010-12-06 21:00:19 -08004688 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4689 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004690 udev->speed = USB_SPEED_SUPER;
4691 else
4692 udev->speed = USB_SPEED_UNKNOWN;
4693
Alan Stern3b29b682011-02-22 09:53:41 -05004694 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004695 if (udev->devnum <= 0) {
4696 status = -ENOTCONN; /* Don't retry */
4697 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004698 }
4699
Sarah Sharpe7b77172009-04-27 19:54:26 -07004700 /* reset (non-USB 3.0 devices) and get descriptor */
Dan Williams5c79a1e2014-05-20 18:09:26 -07004701 usb_lock_port(port_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 status = hub_port_init(hub, udev, port1, i);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004703 usb_unlock_port(port_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 if (status < 0)
4705 goto loop;
4706
Phil Dibowitz93362a82010-07-22 00:05:01 +02004707 usb_detect_quirks(udev);
4708 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4709 msleep(1000);
4710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 /* consecutive bus-powered hubs aren't reliable; they can
4712 * violate the voltage drop budget. if the new child has
4713 * a "powered" LED, users should notice we didn't enable it
4714 * (without reading syslog), even without per-port LEDs
4715 * on the parent.
4716 */
4717 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004718 && udev->bus_mA <= unit_load) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 u16 devstat;
4720
4721 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4722 &devstat);
Alan Stern15b73362013-07-30 15:35:40 -04004723 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 dev_dbg(&udev->dev, "get status %d ?\n", status);
4725 goto loop_disable;
4726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4728 dev_err(&udev->dev,
4729 "can't connect bus-powered hub "
4730 "to this port\n");
4731 if (hub->has_indicators) {
4732 hub->indicator[port1-1] =
4733 INDICATOR_AMBER_BLINK;
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08004734 queue_delayed_work(
4735 system_power_efficient_wq,
4736 &hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 }
4738 status = -ENOTCONN; /* Don't retry */
4739 goto loop_disable;
4740 }
4741 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004742
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 /* check for devices running slower than they could */
4744 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4745 && udev->speed == USB_SPEED_FULL
4746 && highspeed_hubs != 0)
4747 check_highspeed (hub, udev, port1);
4748
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004749 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 * udev becomes globally accessible, although presumably
4751 * no one will look at it until hdev is unlocked.
4752 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 status = 0;
4754
Dan Williamsd8521af2014-05-20 18:08:28 -07004755 mutex_lock(&usb_port_peer_mutex);
4756
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 /* We mustn't add new devices if the parent hub has
4758 * been disconnected; we would race with the
4759 * recursively_mark_NOTATTACHED() routine.
4760 */
4761 spin_lock_irq(&device_state_lock);
4762 if (hdev->state == USB_STATE_NOTATTACHED)
4763 status = -ENOTCONN;
4764 else
Dan Williamsd99f6b42014-05-20 18:08:17 -07004765 port_dev->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 spin_unlock_irq(&device_state_lock);
Dan Williamsd8521af2014-05-20 18:08:28 -07004767 mutex_unlock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
4769 /* Run it through the hoops (find a driver, etc) */
4770 if (!status) {
4771 status = usb_new_device(udev);
4772 if (status) {
Dan Williamsd8521af2014-05-20 18:08:28 -07004773 mutex_lock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 spin_lock_irq(&device_state_lock);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004775 port_dev->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 spin_unlock_irq(&device_state_lock);
Dan Williamsd8521af2014-05-20 18:08:28 -07004777 mutex_unlock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 }
4779 }
4780
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 if (status)
4782 goto loop_disable;
4783
4784 status = hub_power_remaining(hub);
4785 if (status)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004786 dev_dbg(hub->intfdev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
4788 return;
4789
4790loop_disable:
4791 hub_port_disable(hub, port1, 1);
4792loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004793 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004794 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004795 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004797 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 break;
4799 }
Alan Stern3a311552008-05-20 16:58:29 -04004800 if (hub->hdev->parent ||
4801 !hcd->driver->port_handed_over ||
Alan Sterne9e88fb2013-03-27 16:14:01 -04004802 !(hcd->driver->port_handed_over)(hcd, port1)) {
4803 if (status != -ENOTCONN && status != -ENODEV)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004804 dev_err(&port_dev->dev,
4805 "unable to enumerate USB device\n");
Alan Sterne9e88fb2013-03-27 16:14:01 -04004806 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004807
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808done:
4809 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304810 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4811 hcd->driver->relinquish_port(hcd, port1);
Dan Williamsaf376a42014-05-20 18:09:15 -07004812
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813}
4814
Dan Williamsaf376a42014-05-20 18:09:15 -07004815/* Handle physical or logical connection change events.
4816 * This routine is called when:
4817 * a port connection-change occurs;
4818 * a port enable-change occurs (often caused by EMI);
4819 * usb_reset_and_verify_device() encounters changed descriptors (as from
4820 * a firmware download)
4821 * caller already locked the hub
4822 */
4823static void hub_port_connect_change(struct usb_hub *hub, int port1,
4824 u16 portstatus, u16 portchange)
Dan Williams5c79a1e2014-05-20 18:09:26 -07004825 __must_hold(&port_dev->status_lock)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004826{
Dan Williamsaf376a42014-05-20 18:09:15 -07004827 struct usb_port *port_dev = hub->ports[port1 - 1];
4828 struct usb_device *udev = port_dev->child;
4829 int status = -ENODEV;
Sarah Sharp714b07b2012-01-24 13:53:18 -08004830
Dan Williamsaf376a42014-05-20 18:09:15 -07004831 dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus,
4832 portchange, portspeed(hub, portstatus));
4833
4834 if (hub->has_indicators) {
4835 set_port_led(hub, port1, HUB_LED_AUTO);
4836 hub->indicator[port1-1] = INDICATOR_AUTO;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004837 }
4838
Dan Williamsaf376a42014-05-20 18:09:15 -07004839#ifdef CONFIG_USB_OTG
4840 /* during HNP, don't repeat the debounce */
4841 if (hub->hdev->bus->is_b_host)
4842 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4843 USB_PORT_STAT_C_ENABLE);
4844#endif
Sarah Sharp714b07b2012-01-24 13:53:18 -08004845
Dan Williamsaf376a42014-05-20 18:09:15 -07004846 /* Try to resuscitate an existing device */
4847 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4848 udev->state != USB_STATE_NOTATTACHED) {
4849 if (portstatus & USB_PORT_STAT_ENABLE) {
4850 status = 0; /* Nothing to do */
4851#ifdef CONFIG_PM_RUNTIME
4852 } else if (udev->state == USB_STATE_SUSPENDED &&
4853 udev->persist_enabled) {
4854 /* For a suspended device, treat this as a
4855 * remote wakeup event.
4856 */
Dan Williams5c79a1e2014-05-20 18:09:26 -07004857 usb_unlock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004858 status = usb_remote_wakeup(udev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004859 usb_lock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004860#endif
4861 } else {
4862 /* Don't resuscitate */;
4863 }
Sarah Sharp714b07b2012-01-24 13:53:18 -08004864 }
Dan Williamsaf376a42014-05-20 18:09:15 -07004865 clear_bit(port1, hub->change_bits);
4866
Dan Williams5c79a1e2014-05-20 18:09:26 -07004867 /* successfully revalidated the connection */
Dan Williamsaf376a42014-05-20 18:09:15 -07004868 if (status == 0)
4869 return;
4870
Dan Williams5c79a1e2014-05-20 18:09:26 -07004871 usb_unlock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004872 hub_port_connect(hub, port1, portstatus, portchange);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004873 usb_lock_port(port_dev);
Sarah Sharp714b07b2012-01-24 13:53:18 -08004874}
4875
Dan Williamsaf376a42014-05-20 18:09:15 -07004876static void port_event(struct usb_hub *hub, int port1)
Dan Williams5c79a1e2014-05-20 18:09:26 -07004877 __must_hold(&port_dev->status_lock)
Dan Williamsaf376a42014-05-20 18:09:15 -07004878{
4879 int connect_change, reset_device = 0;
4880 struct usb_port *port_dev = hub->ports[port1 - 1];
4881 struct usb_device *udev = port_dev->child;
4882 struct usb_device *hdev = hub->hdev;
4883 u16 portstatus, portchange;
4884
4885 connect_change = test_bit(port1, hub->change_bits);
4886 clear_bit(port1, hub->event_bits);
4887 clear_bit(port1, hub->wakeup_bits);
4888
4889 if (hub_port_status(hub, port1, &portstatus, &portchange) < 0)
4890 return;
4891
4892 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4893 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
4894 connect_change = 1;
4895 }
4896
4897 if (portchange & USB_PORT_STAT_C_ENABLE) {
4898 if (!connect_change)
4899 dev_dbg(&port_dev->dev, "enable change, status %08x\n",
4900 portstatus);
4901 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
4902
4903 /*
4904 * EM interference sometimes causes badly shielded USB devices
4905 * to be shutdown by the hub, this hack enables them again.
4906 * Works at least with mouse driver.
4907 */
4908 if (!(portstatus & USB_PORT_STAT_ENABLE)
4909 && !connect_change && udev) {
4910 dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n");
4911 connect_change = 1;
4912 }
4913 }
4914
4915 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
4916 u16 status = 0, unused;
4917
4918 dev_dbg(&port_dev->dev, "over-current change\n");
4919 usb_clear_port_feature(hdev, port1,
4920 USB_PORT_FEAT_C_OVER_CURRENT);
4921 msleep(100); /* Cool down */
4922 hub_power_on(hub, true);
4923 hub_port_status(hub, port1, &status, &unused);
4924 if (status & USB_PORT_STAT_OVERCURRENT)
4925 dev_err(&port_dev->dev, "over-current condition\n");
4926 }
4927
4928 if (portchange & USB_PORT_STAT_C_RESET) {
4929 dev_dbg(&port_dev->dev, "reset change\n");
4930 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET);
4931 }
4932 if ((portchange & USB_PORT_STAT_C_BH_RESET)
4933 && hub_is_superspeed(hdev)) {
4934 dev_dbg(&port_dev->dev, "warm reset change\n");
4935 usb_clear_port_feature(hdev, port1,
4936 USB_PORT_FEAT_C_BH_PORT_RESET);
4937 }
4938 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4939 dev_dbg(&port_dev->dev, "link state change\n");
4940 usb_clear_port_feature(hdev, port1,
4941 USB_PORT_FEAT_C_PORT_LINK_STATE);
4942 }
4943 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4944 dev_warn(&port_dev->dev, "config error\n");
4945 usb_clear_port_feature(hdev, port1,
4946 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4947 }
4948
Dan Williams097a1552014-05-20 18:09:20 -07004949 /* skip port actions that require the port to be powered on */
4950 if (!pm_runtime_active(&port_dev->dev))
4951 return;
4952
Dan Williamsaf376a42014-05-20 18:09:15 -07004953 if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange))
4954 connect_change = 1;
4955
4956 /*
4957 * Warm reset a USB3 protocol port if it's in
4958 * SS.Inactive state.
4959 */
Dan Williams3cd12f92014-05-29 12:58:46 -07004960 if (hub_port_warm_reset_required(hub, port1, portstatus)) {
Dan Williamsaf376a42014-05-20 18:09:15 -07004961 dev_dbg(&port_dev->dev, "do warm reset\n");
4962 if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION)
4963 || udev->state == USB_STATE_NOTATTACHED) {
4964 if (hub_port_reset(hub, port1, NULL,
4965 HUB_BH_RESET_TIME, true) < 0)
4966 hub_port_disable(hub, port1, 1);
4967 } else
4968 reset_device = 1;
4969 }
4970
4971 /*
4972 * On disconnect USB3 protocol ports transit from U0 to
4973 * SS.Inactive to Rx.Detect. If this happens a warm-
4974 * reset is not needed, but a (re)connect may happen
4975 * before khubd runs and sees the disconnect, and the
4976 * device may be an unknown state.
4977 *
4978 * If the port went through SS.Inactive without khubd
4979 * seeing it the C_LINK_STATE change flag will be set,
4980 * and we reset the dev to put it in a known state.
4981 */
4982 if (reset_device || (udev && hub_is_superspeed(hub->hdev)
4983 && (portchange & USB_PORT_STAT_C_LINK_STATE)
4984 && (portstatus & USB_PORT_STAT_CONNECTION))) {
Dan Williams5c79a1e2014-05-20 18:09:26 -07004985 usb_unlock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004986 usb_lock_device(udev);
4987 usb_reset_device(udev);
4988 usb_unlock_device(udev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004989 usb_lock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004990 connect_change = 0;
4991 }
4992
4993 if (connect_change)
4994 hub_port_connect_change(hub, port1, portstatus, portchange);
4995}
4996
4997
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998static void hub_events(void)
4999{
5000 struct list_head *tmp;
5001 struct usb_device *hdev;
5002 struct usb_interface *intf;
5003 struct usb_hub *hub;
5004 struct device *hub_dev;
5005 u16 hubstatus;
5006 u16 hubchange;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
5009 /*
5010 * We restart the list every time to avoid a deadlock with
5011 * deleting hubs downstream from this one. This should be
5012 * safe since we delete the hub from the event list.
5013 * Not the most efficient, but avoids deadlocks.
5014 */
5015 while (1) {
5016
5017 /* Grab the first entry at the beginning of the list */
5018 spin_lock_irq(&hub_event_lock);
5019 if (list_empty(&hub_event_list)) {
5020 spin_unlock_irq(&hub_event_lock);
5021 break;
5022 }
5023
5024 tmp = hub_event_list.next;
5025 list_del_init(tmp);
5026
5027 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04005028 kref_get(&hub->kref);
Joe Lawrencec605f3c2014-09-10 15:07:50 -04005029 hdev = hub->hdev;
5030 usb_get_dev(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04005031 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
Alan Sterne8054852007-05-04 11:55:11 -04005033 hub_dev = hub->intfdev;
5034 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05005035 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +02005036 hdev->state, hdev->maxchild,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 /* NOTE: expects max 15 ports... */
5038 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05005039 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 /* Lock the device, then check to see if we were
5042 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04005043 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04005044 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05005045 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
5047 /* If the hub has died, clean up after it */
5048 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04005049 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04005050 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 goto loop;
5052 }
5053
Alan Stern40f122f2006-11-09 14:44:33 -05005054 /* Autoresume */
5055 ret = usb_autopm_get_interface(intf);
5056 if (ret) {
5057 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05005059 }
5060
5061 /* If this is an inactive hub, do nothing */
5062 if (hub->quiescing)
5063 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064
5065 if (hub->error) {
5066 dev_dbg (hub_dev, "resetting for error %d\n",
5067 hub->error);
5068
Ming Lei742120c2008-06-18 22:00:29 +08005069 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 if (ret) {
5071 dev_dbg (hub_dev,
5072 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05005073 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 }
5075
5076 hub->nerrors = 0;
5077 hub->error = 0;
5078 }
5079
5080 /* deal with port status changes */
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +02005081 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williams097a1552014-05-20 18:09:20 -07005082 struct usb_port *port_dev = hub->ports[i - 1];
Hans de Goedea82b76f2013-11-08 13:41:05 +01005083
Dan Williams5c79a1e2014-05-20 18:09:26 -07005084 if (test_bit(i, hub->event_bits)
5085 || test_bit(i, hub->change_bits)
5086 || test_bit(i, hub->wakeup_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 /*
Dan Williams097a1552014-05-20 18:09:20 -07005088 * The get_noresume and barrier ensure that if
5089 * the port was in the process of resuming, we
5090 * flush that work and keep the port active for
5091 * the duration of the port_event(). However,
5092 * if the port is runtime pm suspended
5093 * (powered-off), we leave it in that state, run
5094 * an abbreviated port_event(), and move on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 */
Dan Williams097a1552014-05-20 18:09:20 -07005096 pm_runtime_get_noresume(&port_dev->dev);
5097 pm_runtime_barrier(&port_dev->dev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07005098 usb_lock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07005099 port_event(hub, i);
Dan Williams5c79a1e2014-05-20 18:09:26 -07005100 usb_unlock_port(port_dev);
Dan Williams097a1552014-05-20 18:09:20 -07005101 pm_runtime_put_sync(&port_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 }
Dan Williamsaf376a42014-05-20 18:09:15 -07005103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
5105 /* deal with hub status changes */
5106 if (test_and_clear_bit(0, hub->event_bits) == 0)
5107 ; /* do nothing */
5108 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
5109 dev_err (hub_dev, "get_hub_status failed\n");
5110 else {
5111 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
5112 dev_dbg (hub_dev, "power change\n");
5113 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05005114 if (hubstatus & HUB_STATUS_LOCAL_POWER)
5115 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05005116 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08005117 else
5118 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 }
5120 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01005121 u16 status = 0;
5122 u16 unused;
5123
5124 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01005126 msleep(500); /* Cool down */
Matthias Beyer469271f2013-10-10 23:41:27 +02005127 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01005128 hub_hub_status(hub, &status, &unused);
5129 if (status & HUB_STATUS_OVERCURRENT)
5130 dev_err(hub_dev, "over-current "
5131 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 }
5133 }
5134
Alan Stern8e4ceb32009-12-07 13:01:37 -05005135 loop_autopm:
5136 /* Balance the usb_autopm_get_interface() above */
5137 usb_autopm_put_interface_no_suspend(intf);
5138 loop:
5139 /* Balance the usb_autopm_get_interface_no_resume() in
5140 * kick_khubd() and allow autosuspend.
5141 */
5142 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05005143 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 usb_unlock_device(hdev);
Joe Lawrencec605f3c2014-09-10 15:07:50 -04005145 usb_put_dev(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