blob: db1d587cbb9554764eee704911b12872111dae10 [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)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001987 wakeup = udev->actconfig->desc.bmAttributes
1988 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001989 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001990 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001991 }
Sarah Sharp15123002007-12-21 16:54:15 -08001992 if (udev->state == USB_STATE_SUSPENDED &&
1993 new_state != USB_STATE_SUSPENDED)
1994 udev->active_duration -= jiffies;
1995 else if (new_state == USB_STATE_SUSPENDED &&
1996 udev->state != USB_STATE_SUSPENDED)
1997 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001998 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001999 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 recursively_mark_NOTATTACHED(udev);
2001 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01002002 if (wakeup >= 0)
2003 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
David Vrabel6da9c992009-02-18 14:43:47 +00002005EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002007/*
Alan Stern3b29b682011-02-22 09:53:41 -05002008 * Choose a device number.
2009 *
2010 * Device numbers are used as filenames in usbfs. On USB-1.1 and
2011 * USB-2.0 buses they are also used as device addresses, however on
2012 * USB-3.0 buses the address is assigned by the controller hardware
2013 * and it usually is not the same as the device number.
2014 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002015 * WUSB devices are simple: they have no hubs behind, so the mapping
2016 * device <-> virtual port number becomes 1:1. Why? to simplify the
2017 * life of the device connection logic in
2018 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
2019 * handshake we need to assign a temporary address in the unauthorized
2020 * space. For simplicity we use the first virtual port number found to
2021 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
2022 * and that becomes it's address [X < 128] or its unauthorized address
2023 * [X | 0x80].
2024 *
2025 * We add 1 as an offset to the one-based USB-stack port number
2026 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
2027 * 0 is reserved by USB for default address; (b) Linux's USB stack
2028 * uses always #1 for the root hub of the controller. So USB stack's
2029 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07002030 *
2031 * Devices connected under xHCI are not as simple. The host controller
2032 * supports virtualization, so the hardware assigns device addresses and
2033 * the HCD must setup data structures before issuing a set address
2034 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002035 */
Alan Stern3b29b682011-02-22 09:53:41 -05002036static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
2038 int devnum;
2039 struct usb_bus *bus = udev->bus;
2040
2041 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002042 if (udev->wusb) {
2043 devnum = udev->portnum + 1;
2044 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
2045 } else {
2046 /* Try to allocate the next devnum beginning at
2047 * bus->devnum_next. */
2048 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
2049 bus->devnum_next);
2050 if (devnum >= 128)
2051 devnum = find_next_zero_bit(bus->devmap.devicemap,
2052 128, 1);
Matthias Beyer469271f2013-10-10 23:41:27 +02002053 bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1);
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (devnum < 128) {
2056 set_bit(devnum, bus->devmap.devicemap);
2057 udev->devnum = devnum;
2058 }
2059}
2060
Alan Stern3b29b682011-02-22 09:53:41 -05002061static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
2063 if (udev->devnum > 0) {
2064 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
2065 udev->devnum = -1;
2066 }
2067}
2068
Alan Stern3b29b682011-02-22 09:53:41 -05002069static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07002070{
2071 /* The address for a WUSB device is managed by wusbcore. */
2072 if (!udev->wusb)
2073 udev->devnum = devnum;
2074}
2075
Herbert Xuf7410ce2010-01-10 20:15:03 +11002076static void hub_free_dev(struct usb_device *udev)
2077{
2078 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2079
2080 /* Root hubs aren't real devices, so don't free HCD resources */
2081 if (hcd->driver->free_dev && udev->parent)
2082 hcd->driver->free_dev(hcd, udev);
2083}
2084
Dan Williams7027df32014-05-20 18:09:36 -07002085static void hub_disconnect_children(struct usb_device *udev)
2086{
2087 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
2088 int i;
2089
2090 /* Free up all the children before we remove this device */
2091 for (i = 0; i < udev->maxchild; i++) {
2092 if (hub->ports[i]->child)
2093 usb_disconnect(&hub->ports[i]->child);
2094 }
2095}
2096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097/**
2098 * usb_disconnect - disconnect a device (usbcore-internal)
2099 * @pdev: pointer to device being disconnected
2100 * Context: !in_interrupt ()
2101 *
2102 * Something got disconnected. Get rid of it and all of its children.
2103 *
2104 * If *pdev is a normal device then the parent hub must already be locked.
Bjorn Helgaasdb8f2aa2013-09-13 13:57:34 -06002105 * If *pdev is a root hub then the caller must hold the usb_bus_list_lock,
2106 * which protects the set of root hubs as well as the list of buses.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 *
2108 * Only hub drivers (including virtual root hub drivers for host
2109 * controllers) should ever call this.
2110 *
2111 * This call is synchronous, and may not be used in an interrupt context.
2112 */
2113void usb_disconnect(struct usb_device **pdev)
2114{
Dan Williams7027df32014-05-20 18:09:36 -07002115 struct usb_port *port_dev = NULL;
2116 struct usb_device *udev = *pdev;
Peter Chen5b1dc202014-08-19 08:56:21 +08002117 struct usb_hub *hub = NULL;
2118 int port1 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 /* mark the device as inactive, so any further urb submissions for
2121 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002122 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 */
2124 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05002125 dev_info(&udev->dev, "USB disconnect, device number %d\n",
2126 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002128 usb_lock_device(udev);
2129
Dan Williams7027df32014-05-20 18:09:36 -07002130 hub_disconnect_children(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 /* deallocate hcd/hardware state ... nuking all pending urbs and
2133 * cleaning up all state associated with the current configuration
2134 * so that the hardware is now fully quiesced.
2135 */
Alan Stern782da722006-07-01 22:09:35 -04002136 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04002138 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Lan Tianyufde26382013-01-20 01:53:33 +08002140 if (udev->parent) {
Dan Williams7027df32014-05-20 18:09:36 -07002141 port1 = udev->portnum;
2142 hub = usb_hub_to_struct_hub(udev->parent);
2143 port_dev = hub->ports[port1 - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002144
2145 sysfs_remove_link(&udev->dev.kobj, "port");
2146 sysfs_remove_link(&port_dev->dev.kobj, "device");
Lan Tianyu971fcd42013-01-23 04:26:29 +08002147
Dan Williams7027df32014-05-20 18:09:36 -07002148 /*
2149 * As usb_port_runtime_resume() de-references udev, make
2150 * sure no resumes occur during removal
2151 */
2152 if (!test_and_set_bit(port1, hub->child_usage_bits))
2153 pm_runtime_get_sync(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002154 }
2155
Alan Stern3b23dd62008-12-05 14:10:34 -05002156 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04002157 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07002158
Alan Stern782da722006-07-01 22:09:35 -04002159 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002160 * for de-configuring the device and invoking the remove-device
2161 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002162 */
2163 device_del(&udev->dev);
2164
2165 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 * (or root_hub) pointer.
2167 */
Alan Stern3b29b682011-02-22 09:53:41 -05002168 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 /* Avoid races with recursively_mark_NOTATTACHED() */
2171 spin_lock_irq(&device_state_lock);
2172 *pdev = NULL;
2173 spin_unlock_irq(&device_state_lock);
2174
Dan Williams7027df32014-05-20 18:09:36 -07002175 if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits))
2176 pm_runtime_put(&port_dev->dev);
2177
Herbert Xuf7410ce2010-01-10 20:15:03 +11002178 hub_free_dev(udev);
2179
Alan Stern782da722006-07-01 22:09:35 -04002180 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002183#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184static void show_string(struct usb_device *udev, char *id, char *string)
2185{
2186 if (!string)
2187 return;
Joe Perchesf2ec5222012-10-28 01:05:51 -07002188 dev_info(&udev->dev, "%s: %s\n", id, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002191static void announce_device(struct usb_device *udev)
2192{
2193 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2194 le16_to_cpu(udev->descriptor.idVendor),
2195 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002196 dev_info(&udev->dev,
2197 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002198 udev->descriptor.iManufacturer,
2199 udev->descriptor.iProduct,
2200 udev->descriptor.iSerialNumber);
2201 show_string(udev, "Product", udev->product);
2202 show_string(udev, "Manufacturer", udev->manufacturer);
2203 show_string(udev, "SerialNumber", udev->serial);
2204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002206static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207#endif
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Oliver Neukum3ede7602007-01-11 14:35:50 +01002210/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002211 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002212 * @udev: newly addressed device (in ADDRESS state)
2213 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002214 * Finish enumeration for On-The-Go devices
Yacine Belkadi626f0902013-08-02 20:10:04 +02002215 *
2216 * Return: 0 if successful. A negative error code otherwise.
Oliver Neukum3ede7602007-01-11 14:35:50 +01002217 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002218static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002220 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222#ifdef CONFIG_USB_OTG
2223 /*
2224 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2225 * to wake us after we've powered off VBUS; and HNP, switching roles
2226 * "host" to "peripheral". The OTG descriptor helps figure this out.
2227 */
2228 if (!udev->bus->is_b_host
2229 && udev->config
2230 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002231 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 struct usb_bus *bus = udev->bus;
2233
2234 /* descriptor may appear anywhere in config */
2235 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2236 le16_to_cpu(udev->config[0].desc.wTotalLength),
2237 USB_DT_OTG, (void **) &desc) == 0) {
2238 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002239 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 dev_info(&udev->dev,
2242 "Dual-Role OTG device on %sHNP port\n",
2243 (port1 == bus->otg_port)
2244 ? "" : "non-");
2245
2246 /* enable HNP before suspend, it's simpler */
2247 if (port1 == bus->otg_port)
2248 bus->b_hnp_enable = 1;
2249 err = usb_control_msg(udev,
2250 usb_sndctrlpipe(udev, 0),
2251 USB_REQ_SET_FEATURE, 0,
2252 bus->b_hnp_enable
2253 ? USB_DEVICE_B_HNP_ENABLE
2254 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2255 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2256 if (err < 0) {
2257 /* OTG MESSAGE: report errors here,
2258 * customize to match your product.
2259 */
2260 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002261 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 err);
2263 bus->b_hnp_enable = 0;
2264 }
2265 }
2266 }
2267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002269 return err;
2270}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002272
2273/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002274 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002275 * @udev: newly addressed device (in ADDRESS state)
2276 *
2277 * This is only called by usb_new_device() and usb_authorize_device()
2278 * and FIXME -- all comments that apply to them apply here wrt to
2279 * environment.
2280 *
2281 * If the device is WUSB and not authorized, we don't attempt to read
2282 * the string descriptors, as they will be errored out by the device
2283 * until it has been authorized.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002284 *
2285 * Return: 0 if successful. A negative error code otherwise.
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002286 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002287static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002288{
2289 int err;
Peter Chen026f3fc2014-08-19 09:51:53 +08002290 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002291
2292 if (udev->config == NULL) {
2293 err = usb_get_configuration(udev);
2294 if (err < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04002295 if (err != -ENODEV)
2296 dev_err(&udev->dev, "can't read configurations, error %d\n",
2297 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002298 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002299 }
2300 }
Thomas Pugliese83e83ec2013-12-09 13:40:29 -06002301
2302 /* read the standard strings and cache them if present */
2303 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2304 udev->manufacturer = usb_cache_string(udev,
2305 udev->descriptor.iManufacturer);
2306 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2307
Alan Stern8d8558d2009-12-08 15:50:41 -05002308 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002309 if (err < 0)
2310 return err;
2311
Peter Chen026f3fc2014-08-19 09:51:53 +08002312 if (IS_ENABLED(CONFIG_USB_OTG_WHITELIST) && hcd->tpl_support &&
2313 !is_targeted(udev) && IS_ENABLED(CONFIG_USB_OTG)) {
2314 /* Maybe it can talk to us, though we can't talk to it.
2315 * (Includes HNP test device.)
2316 */
2317 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
2318 err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND);
2319 if (err < 0)
2320 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2321 }
2322 return -ENOTSUPP;
2323 }
2324
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002325 usb_detect_interface_quirks(udev);
2326
2327 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002328}
2329
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002330static void set_usb_port_removable(struct usb_device *udev)
2331{
2332 struct usb_device *hdev = udev->parent;
2333 struct usb_hub *hub;
2334 u8 port = udev->portnum;
2335 u16 wHubCharacteristics;
2336 bool removable = true;
2337
2338 if (!hdev)
2339 return;
2340
Lan Tianyuad493e52013-01-23 04:26:30 +08002341 hub = usb_hub_to_struct_hub(udev->parent);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002342
2343 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2344
2345 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2346 return;
2347
2348 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002349 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2350 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002351 removable = false;
2352 } else {
2353 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2354 removable = false;
2355 }
2356
2357 if (removable)
2358 udev->removable = USB_DEVICE_REMOVABLE;
2359 else
2360 udev->removable = USB_DEVICE_FIXED;
Dan Williamsa4204ff2014-05-20 18:08:22 -07002361
2362 /*
2363 * Platform firmware may have populated an alternative value for
2364 * removable. If the parent port has a known connect_type use
2365 * that instead.
2366 */
2367 switch (hub->ports[udev->portnum - 1]->connect_type) {
2368 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
2369 udev->removable = USB_DEVICE_REMOVABLE;
2370 break;
2371 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
2372 udev->removable = USB_DEVICE_FIXED;
2373 break;
2374 default: /* use what was set above */
2375 break;
2376 }
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002377}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002378
2379/**
2380 * usb_new_device - perform initial device setup (usbcore-internal)
2381 * @udev: newly addressed device (in ADDRESS state)
2382 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002383 * This is called with devices which have been detected but not fully
2384 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002385 * for any device configuration. The caller must have locked either
2386 * the parent hub (if udev is a normal device) or else the
2387 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2388 * udev has already been installed, but udev is not yet visible through
2389 * sysfs or other filesystem code.
2390 *
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002391 * This call is synchronous, and may not be used in an interrupt context.
2392 *
2393 * Only the hub driver or root-hub registrar should ever call this.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002394 *
2395 * Return: Whether the device is configured properly or not. Zero if the
2396 * interface was registered with the driver core; else a negative errno
2397 * value.
2398 *
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002399 */
2400int usb_new_device(struct usb_device *udev)
2401{
2402 int err;
2403
Dan Streetman16985402010-01-06 09:56:53 -05002404 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002405 /* Initialize non-root-hub device wakeup to disabled;
2406 * device (un)configuration controls wakeup capable
2407 * sysfs power/wakeup controls wakeup enabled/disabled
2408 */
2409 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002410 }
2411
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002412 /* Tell the runtime-PM framework the device is active */
2413 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002414 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002415 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002416 pm_runtime_enable(&udev->dev);
2417
Alan Sternc08512c2010-11-15 15:57:58 -05002418 /* By default, forbid autosuspend for all devices. It will be
2419 * allowed for hubs during binding.
2420 */
2421 usb_disable_autosuspend(udev);
2422
Alan Stern8d8558d2009-12-08 15:50:41 -05002423 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002424 if (err < 0)
2425 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002426 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2427 udev->devnum, udev->bus->busnum,
2428 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002429 /* export the usbdev device-node for libusb */
2430 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2431 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2432
Alan Stern6cd13202008-11-13 15:08:30 -05002433 /* Tell the world! */
2434 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002435
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002436 if (udev->serial)
2437 add_device_randomness(udev->serial, strlen(udev->serial));
2438 if (udev->product)
2439 add_device_randomness(udev->product, strlen(udev->product));
2440 if (udev->manufacturer)
2441 add_device_randomness(udev->manufacturer,
2442 strlen(udev->manufacturer));
2443
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002444 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002445
Dan Williamsa4204ff2014-05-20 18:08:22 -07002446 /* check whether the hub or firmware marks this port as non-removable */
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002447 if (udev->parent)
2448 set_usb_port_removable(udev);
2449
Alan Stern782da722006-07-01 22:09:35 -04002450 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002451 * for configuring the device and invoking the add-device
2452 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002453 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002454 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (err) {
2456 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2457 goto fail;
2458 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002459
Lan Tianyufde26382013-01-20 01:53:33 +08002460 /* Create link files between child device and usb port device. */
2461 if (udev->parent) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002462 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
Dan Williamsd5c38342014-05-20 18:08:52 -07002463 int port1 = udev->portnum;
2464 struct usb_port *port_dev = hub->ports[port1 - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002465
2466 err = sysfs_create_link(&udev->dev.kobj,
2467 &port_dev->dev.kobj, "port");
2468 if (err)
2469 goto fail;
2470
2471 err = sysfs_create_link(&port_dev->dev.kobj,
2472 &udev->dev.kobj, "device");
2473 if (err) {
2474 sysfs_remove_link(&udev->dev.kobj, "port");
2475 goto fail;
2476 }
Lan Tianyu971fcd42013-01-23 04:26:29 +08002477
Dan Williamsd5c38342014-05-20 18:08:52 -07002478 if (!test_and_set_bit(port1, hub->child_usage_bits))
2479 pm_runtime_get_sync(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002480 }
2481
Alan Stern3b23dd62008-12-05 14:10:34 -05002482 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002483 usb_mark_last_busy(udev);
2484 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002485 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
2487fail:
2488 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002489 pm_runtime_disable(&udev->dev);
2490 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002491 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002494
2495/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002496 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2497 * @usb_dev: USB device
2498 *
2499 * Move the USB device to a very basic state where interfaces are disabled
2500 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002501 *
2502 * We share a lock (that we have) with device_del(), so we need to
2503 * defer its call.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002504 *
2505 * Return: 0.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002506 */
2507int usb_deauthorize_device(struct usb_device *usb_dev)
2508{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002509 usb_lock_device(usb_dev);
2510 if (usb_dev->authorized == 0)
2511 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002512
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002513 usb_dev->authorized = 0;
2514 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002515
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002516out_unauthorized:
2517 usb_unlock_device(usb_dev);
2518 return 0;
2519}
2520
2521
2522int usb_authorize_device(struct usb_device *usb_dev)
2523{
2524 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002525
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002526 usb_lock_device(usb_dev);
2527 if (usb_dev->authorized == 1)
2528 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002529
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002530 result = usb_autoresume_device(usb_dev);
2531 if (result < 0) {
2532 dev_err(&usb_dev->dev,
2533 "can't autoresume for authorization: %d\n", result);
2534 goto error_autoresume;
2535 }
2536 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2537 if (result < 0) {
2538 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2539 "authorization: %d\n", result);
2540 goto error_device_descriptor;
2541 }
Alan Sternda307122009-12-08 15:54:44 -05002542
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002543 usb_dev->authorized = 1;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002544 /* Choose and set the configuration. This registers the interfaces
2545 * with the driver core and lets interface drivers bind to them.
2546 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002547 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002548 if (c >= 0) {
2549 result = usb_set_configuration(usb_dev, c);
2550 if (result) {
2551 dev_err(&usb_dev->dev,
2552 "can't set config #%d, error %d\n", c, result);
2553 /* This need not be fatal. The user can try to
2554 * set other configurations. */
2555 }
2556 }
2557 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002558
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002559error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002560 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002561error_autoresume:
2562out_authorized:
Matthias Beyer781b2132013-10-10 23:41:29 +02002563 usb_unlock_device(usb_dev); /* complements locktree */
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002564 return result;
2565}
2566
2567
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002568/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2569static unsigned hub_is_wusb(struct usb_hub *hub)
2570{
2571 struct usb_hcd *hcd;
2572 if (hub->hdev->parent != NULL) /* not a root hub? */
2573 return 0;
2574 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2575 return hcd->wireless;
2576}
2577
2578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579#define PORT_RESET_TRIES 5
2580#define SET_ADDRESS_TRIES 2
2581#define GET_DESCRIPTOR_TRIES 2
2582#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302583#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2586#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002587#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588#define HUB_LONG_RESET_TIME 200
Sarah Sharp77c7f072012-11-14 17:16:52 -08002589#define HUB_RESET_TIMEOUT 800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Dan Williams48fc7db2013-12-05 17:07:27 -08002591/*
2592 * "New scheme" enumeration causes an extra state transition to be
2593 * exposed to an xhci host and causes USB3 devices to receive control
2594 * commands in the default state. This has been seen to cause
2595 * enumeration failures, so disable this enumeration scheme for USB3
2596 * devices.
2597 */
2598static bool use_new_scheme(struct usb_device *udev, int retry)
2599{
2600 if (udev->speed == USB_SPEED_SUPER)
2601 return false;
2602
2603 return USE_NEW_SCHEME(retry);
2604}
2605
Sarah Sharp10d674a2011-09-14 14:24:52 -07002606static int hub_port_reset(struct usb_hub *hub, int port1,
2607 struct usb_device *udev, unsigned int delay, bool warm);
2608
Rahul Bedarkar025d4432014-01-04 11:24:41 +05302609/* Is a USB 3.0 port in the Inactive or Compliance Mode state?
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002610 * Port worm reset is required to recover
2611 */
Dan Williams3cd12f92014-05-29 12:58:46 -07002612static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
2613 u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002614{
Dan Williams3cd12f92014-05-29 12:58:46 -07002615 u16 link_state;
2616
2617 if (!hub_is_superspeed(hub->hdev))
2618 return false;
2619
2620 if (test_bit(port1, hub->warm_reset_bits))
2621 return true;
2622
2623 link_state = portstatus & USB_PORT_STAT_LINK_STATE;
2624 return link_state == USB_SS_PORT_LS_SS_INACTIVE
2625 || link_state == USB_SS_PORT_LS_COMP_MOD;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002626}
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002629 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
2631 int delay_time, ret;
2632 u16 portstatus;
2633 u16 portchange;
2634
2635 for (delay_time = 0;
2636 delay_time < HUB_RESET_TIMEOUT;
2637 delay_time += delay) {
2638 /* wait to give the device a chance to reset */
2639 msleep(delay);
2640
2641 /* read and decode port status */
2642 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2643 if (ret < 0)
2644 return ret;
2645
Sarah Sharp4f434472012-11-15 14:58:04 -08002646 /* The port state is unknown until the reset completes. */
Sarah Sharp470f0be2012-12-11 10:14:03 -08002647 if (!(portstatus & USB_PORT_STAT_RESET))
2648 break;
Sarah Sharp4f434472012-11-15 14:58:04 -08002649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 /* switch to the long delay after two short delay failures */
2651 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2652 delay = HUB_LONG_RESET_TIME;
2653
Dan Williamsd99f6b42014-05-20 18:08:17 -07002654 dev_dbg(&hub->ports[port1 - 1]->dev,
2655 "not %sreset yet, waiting %dms\n",
2656 warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
2658
Sarah Sharp470f0be2012-12-11 10:14:03 -08002659 if ((portstatus & USB_PORT_STAT_RESET))
2660 return -EBUSY;
2661
Dan Williams3cd12f92014-05-29 12:58:46 -07002662 if (hub_port_warm_reset_required(hub, port1, portstatus))
Sarah Sharp470f0be2012-12-11 10:14:03 -08002663 return -ENOTCONN;
2664
2665 /* Device went away? */
2666 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2667 return -ENOTCONN;
2668
2669 /* bomb out completely if the connection bounced. A USB 3.0
2670 * connection may bounce if multiple warm resets were issued,
2671 * but the device may have successfully re-connected. Ignore it.
2672 */
2673 if (!hub_is_superspeed(hub->hdev) &&
2674 (portchange & USB_PORT_STAT_C_CONNECTION))
2675 return -ENOTCONN;
2676
2677 if (!(portstatus & USB_PORT_STAT_ENABLE))
2678 return -EBUSY;
2679
2680 if (!udev)
2681 return 0;
2682
2683 if (hub_is_wusb(hub))
2684 udev->speed = USB_SPEED_WIRELESS;
2685 else if (hub_is_superspeed(hub->hdev))
2686 udev->speed = USB_SPEED_SUPER;
2687 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2688 udev->speed = USB_SPEED_HIGH;
2689 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2690 udev->speed = USB_SPEED_LOW;
2691 else
2692 udev->speed = USB_SPEED_FULL;
2693 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
2695
Andiry Xu75d7cf72011-09-13 16:41:11 -07002696static void hub_port_finish_reset(struct usb_hub *hub, int port1,
Sarah Sharpa24a6072012-11-01 11:20:44 -07002697 struct usb_device *udev, int *status)
Andiry Xu75d7cf72011-09-13 16:41:11 -07002698{
2699 switch (*status) {
2700 case 0:
Sarah Sharpa24a6072012-11-01 11:20:44 -07002701 /* TRSTRCY = 10 ms; plus some extra */
2702 msleep(10 + 40);
2703 if (udev) {
2704 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2705
2706 update_devnum(udev, 0);
2707 /* The xHC may think the device is already reset,
2708 * so ignore the status.
2709 */
2710 if (hcd->driver->reset_device)
2711 hcd->driver->reset_device(hcd, udev);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002712 }
2713 /* FALL THROUGH */
2714 case -ENOTCONN:
2715 case -ENODEV:
Lan Tianyuad493e52013-01-23 04:26:30 +08002716 usb_clear_port_feature(hub->hdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002717 port1, USB_PORT_FEAT_C_RESET);
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002718 if (hub_is_superspeed(hub->hdev)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002719 usb_clear_port_feature(hub->hdev, port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002720 USB_PORT_FEAT_C_BH_PORT_RESET);
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_PORT_LINK_STATE);
Lan Tianyuad493e52013-01-23 04:26:30 +08002723 usb_clear_port_feature(hub->hdev, port1,
Sarah Sharpa24a6072012-11-01 11:20:44 -07002724 USB_PORT_FEAT_C_CONNECTION);
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002725 }
Sarah Sharpa24a6072012-11-01 11:20:44 -07002726 if (udev)
Andiry Xu75d7cf72011-09-13 16:41:11 -07002727 usb_set_device_state(udev, *status
2728 ? USB_STATE_NOTATTACHED
2729 : USB_STATE_DEFAULT);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002730 break;
2731 }
2732}
2733
2734/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002736 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
2738 int i, status;
Sarah Sharpa24a6072012-11-01 11:20:44 -07002739 u16 portchange, portstatus;
Dan Williamsd99f6b42014-05-20 18:08:17 -07002740 struct usb_port *port_dev = hub->ports[port1 - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002742 if (!hub_is_superspeed(hub->hdev)) {
2743 if (warm) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002744 dev_err(hub->intfdev, "only USB3 hub support "
2745 "warm reset\n");
2746 return -EINVAL;
2747 }
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002748 /* Block EHCI CF initialization during the port reset.
2749 * Some companion controllers don't like it when they mix.
2750 */
2751 down_read(&ehci_cf_port_reset_rwsem);
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07002752 } else if (!warm) {
2753 /*
2754 * If the caller hasn't explicitly requested a warm reset,
2755 * double check and see if one is needed.
2756 */
2757 status = hub_port_status(hub, port1,
2758 &portstatus, &portchange);
2759 if (status < 0)
2760 goto done;
2761
Dan Williams3cd12f92014-05-29 12:58:46 -07002762 if (hub_port_warm_reset_required(hub, port1, portstatus))
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07002763 warm = true;
Andiry Xu75d7cf72011-09-13 16:41:11 -07002764 }
Dan Williams3cd12f92014-05-29 12:58:46 -07002765 clear_bit(port1, hub->warm_reset_bits);
Alan Stern32fe0192007-10-10 16:27:07 -04002766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 /* Reset the port */
2768 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002769 status = set_port_feature(hub->hdev, port1, (warm ?
2770 USB_PORT_FEAT_BH_PORT_RESET :
2771 USB_PORT_FEAT_RESET));
Alan Sterne9e88fb2013-03-27 16:14:01 -04002772 if (status == -ENODEV) {
2773 ; /* The hub is gone */
2774 } else if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002775 dev_err(&port_dev->dev,
2776 "cannot %sreset (err = %d)\n",
2777 warm ? "warm " : "", status);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002778 } else {
2779 status = hub_port_wait_reset(hub, port1, udev, delay,
2780 warm);
Alan Sterne9e88fb2013-03-27 16:14:01 -04002781 if (status && status != -ENOTCONN && status != -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 dev_dbg(hub->intfdev,
2783 "port_wait_reset: err = %d\n",
2784 status);
2785 }
2786
Sarah Sharpa24a6072012-11-01 11:20:44 -07002787 /* Check for disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002788 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
Sarah Sharpa24a6072012-11-01 11:20:44 -07002789 hub_port_finish_reset(hub, port1, udev, &status);
2790
2791 if (!hub_is_superspeed(hub->hdev))
2792 goto done;
2793
2794 /*
2795 * If a USB 3.0 device migrates from reset to an error
2796 * state, re-issue the warm reset.
2797 */
2798 if (hub_port_status(hub, port1,
2799 &portstatus, &portchange) < 0)
2800 goto done;
2801
Dan Williams3cd12f92014-05-29 12:58:46 -07002802 if (!hub_port_warm_reset_required(hub, port1,
2803 portstatus))
Sarah Sharpa24a6072012-11-01 11:20:44 -07002804 goto done;
2805
2806 /*
2807 * If the port is in SS.Inactive or Compliance Mode, the
2808 * hot or warm reset failed. Try another warm reset.
2809 */
2810 if (!warm) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002811 dev_dbg(&port_dev->dev,
2812 "hot reset failed, warm reset\n");
Sarah Sharpa24a6072012-11-01 11:20:44 -07002813 warm = true;
2814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
2816
Dan Williamsd99f6b42014-05-20 18:08:17 -07002817 dev_dbg(&port_dev->dev,
2818 "not enabled, trying %sreset again...\n",
2819 warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 delay = HUB_LONG_RESET_TIME;
2821 }
2822
Dan Williamsd99f6b42014-05-20 18:08:17 -07002823 dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Andiry Xu75d7cf72011-09-13 16:41:11 -07002825done:
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002826 if (!hub_is_superspeed(hub->hdev))
Andiry Xu75d7cf72011-09-13 16:41:11 -07002827 up_read(&ehci_cf_port_reset_rwsem);
2828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 return status;
2830}
2831
Andiry Xu0ed9a572011-04-27 18:07:43 +08002832/* Check if a port is power on */
2833static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2834{
2835 int ret = 0;
2836
2837 if (hub_is_superspeed(hub->hdev)) {
2838 if (portstatus & USB_SS_PORT_STAT_POWER)
2839 ret = 1;
2840 } else {
2841 if (portstatus & USB_PORT_STAT_POWER)
2842 ret = 1;
2843 }
2844
2845 return ret;
2846}
2847
Dan Williams5c79a1e2014-05-20 18:09:26 -07002848static void usb_lock_port(struct usb_port *port_dev)
2849 __acquires(&port_dev->status_lock)
2850{
2851 mutex_lock(&port_dev->status_lock);
2852 __acquire(&port_dev->status_lock);
2853}
2854
2855static void usb_unlock_port(struct usb_port *port_dev)
2856 __releases(&port_dev->status_lock)
2857{
2858 mutex_unlock(&port_dev->status_lock);
2859 __release(&port_dev->status_lock);
2860}
2861
Alan Sternd388dab2006-07-01 22:14:24 -04002862#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Andiry Xu0ed9a572011-04-27 18:07:43 +08002864/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2865static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2866{
2867 int ret = 0;
2868
2869 if (hub_is_superspeed(hub->hdev)) {
2870 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2871 == USB_SS_PORT_LS_U3)
2872 ret = 1;
2873 } else {
2874 if (portstatus & USB_PORT_STAT_SUSPEND)
2875 ret = 1;
2876 }
2877
2878 return ret;
2879}
Alan Sternb01b03f2008-04-28 11:06:11 -04002880
2881/* Determine whether the device on a port is ready for a normal resume,
2882 * is ready for a reset-resume, or should be disconnected.
2883 */
2884static int check_port_resume_type(struct usb_device *udev,
2885 struct usb_hub *hub, int port1,
2886 int status, unsigned portchange, unsigned portstatus)
2887{
Dan Williamsd99f6b42014-05-20 18:08:17 -07002888 struct usb_port *port_dev = hub->ports[port1 - 1];
2889
Dan Williams3cd12f92014-05-29 12:58:46 -07002890 /* Is a warm reset needed to recover the connection? */
2891 if (status == 0 && udev->reset_resume
2892 && hub_port_warm_reset_required(hub, port1, portstatus)) {
2893 /* pass */;
2894 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002895 /* Is the device still present? */
Dan Williams3cd12f92014-05-29 12:58:46 -07002896 else if (status || port_is_suspended(hub, portstatus) ||
Andiry Xu0ed9a572011-04-27 18:07:43 +08002897 !port_is_power_on(hub, portstatus) ||
2898 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002899 if (status >= 0)
2900 status = -ENODEV;
2901 }
2902
Alan Stern86c57ed2008-06-30 11:14:43 -04002903 /* Can't do a normal resume if the port isn't enabled,
2904 * so try a reset-resume instead.
2905 */
2906 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2907 if (udev->persist_enabled)
2908 udev->reset_resume = 1;
2909 else
2910 status = -ENODEV;
2911 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002912
2913 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002914 dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n",
2915 portchange, portstatus, status);
Alan Sternb01b03f2008-04-28 11:06:11 -04002916 } else if (udev->reset_resume) {
2917
2918 /* Late port handoff can set status-change bits */
2919 if (portchange & USB_PORT_STAT_C_CONNECTION)
Lan Tianyuad493e52013-01-23 04:26:30 +08002920 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04002921 USB_PORT_FEAT_C_CONNECTION);
2922 if (portchange & USB_PORT_STAT_C_ENABLE)
Lan Tianyuad493e52013-01-23 04:26:30 +08002923 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04002924 USB_PORT_FEAT_C_ENABLE);
2925 }
2926
2927 return status;
2928}
2929
Sarah Sharpf74631e2012-06-25 12:08:08 -07002930int usb_disable_ltm(struct usb_device *udev)
2931{
2932 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2933
2934 /* Check if the roothub and device supports LTM. */
2935 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2936 !usb_device_supports_ltm(udev))
2937 return 0;
2938
2939 /* Clear Feature LTM Enable can only be sent if the device is
2940 * configured.
2941 */
2942 if (!udev->actconfig)
2943 return 0;
2944
2945 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2946 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2947 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2948 USB_CTRL_SET_TIMEOUT);
2949}
2950EXPORT_SYMBOL_GPL(usb_disable_ltm);
2951
2952void usb_enable_ltm(struct usb_device *udev)
2953{
2954 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2955
2956 /* Check if the roothub and device supports LTM. */
2957 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2958 !usb_device_supports_ltm(udev))
2959 return;
2960
2961 /* Set Feature LTM Enable can only be sent if the device is
2962 * configured.
2963 */
2964 if (!udev->actconfig)
2965 return;
2966
2967 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2968 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2969 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2970 USB_CTRL_SET_TIMEOUT);
2971}
2972EXPORT_SYMBOL_GPL(usb_enable_ltm);
2973
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002974/*
Alan Stern28e861652013-07-30 15:37:33 -04002975 * usb_enable_remote_wakeup - enable remote wakeup for a device
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002976 * @udev: target device
2977 *
Alan Stern28e861652013-07-30 15:37:33 -04002978 * For USB-2 devices: Set the device's remote wakeup feature.
2979 *
2980 * For USB-3 devices: Assume there's only one function on the device and
2981 * enable remote wake for the first interface. FIXME if the interface
2982 * association descriptor shows there's more than one function.
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002983 */
Alan Stern28e861652013-07-30 15:37:33 -04002984static int usb_enable_remote_wakeup(struct usb_device *udev)
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002985{
Alan Stern28e861652013-07-30 15:37:33 -04002986 if (udev->speed < USB_SPEED_SUPER)
2987 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2988 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2989 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
2990 USB_CTRL_SET_TIMEOUT);
2991 else
2992 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2993 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
2994 USB_INTRF_FUNC_SUSPEND,
2995 USB_INTRF_FUNC_SUSPEND_RW |
2996 USB_INTRF_FUNC_SUSPEND_LP,
2997 NULL, 0, USB_CTRL_SET_TIMEOUT);
2998}
2999
3000/*
3001 * usb_disable_remote_wakeup - disable remote wakeup for a device
3002 * @udev: target device
3003 *
3004 * For USB-2 devices: Clear the device's remote wakeup feature.
3005 *
3006 * For USB-3 devices: Assume there's only one function on the device and
3007 * disable remote wake for the first interface. FIXME if the interface
3008 * association descriptor shows there's more than one function.
3009 */
3010static int usb_disable_remote_wakeup(struct usb_device *udev)
3011{
3012 if (udev->speed < USB_SPEED_SUPER)
3013 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3014 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
3015 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
3016 USB_CTRL_SET_TIMEOUT);
3017 else
3018 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003019 USB_REQ_CLEAR_FEATURE, USB_RECIP_INTERFACE,
3020 USB_INTRF_FUNC_SUSPEND, 0, NULL, 0,
3021 USB_CTRL_SET_TIMEOUT);
3022}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Alan Sterne583d9d2013-07-11 14:58:04 -04003024/* Count of wakeup-enabled devices at or below udev */
3025static unsigned wakeup_enabled_descendants(struct usb_device *udev)
3026{
3027 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
3028
3029 return udev->do_remote_wakeup +
3030 (hub ? hub->wakeup_enabled_descendants : 0);
3031}
3032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033/*
Alan Stern140d8f62006-07-01 22:07:21 -04003034 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04003035 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07003036 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 *
3038 * Suspends a USB device that isn't in active use, conserving power.
3039 * Devices may wake out of a suspend, if anything important happens,
3040 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04003041 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 * to disconnect devices while they are suspended.
3043 *
David Brownell390a8c32005-09-13 19:57:27 -07003044 * This only affects the USB hardware for a device; its interfaces
3045 * (and, for hubs, child devices) must already have been suspended.
3046 *
Alan Stern624d6c02007-05-30 15:35:16 -04003047 * Selective port suspend reduces power; most suspended devices draw
3048 * less than 500 uA. It's also used in OTG, along with remote wakeup.
3049 * All devices below the suspended port are also suspended.
3050 *
3051 * Devices leave suspend state when the host wakes them up. Some devices
3052 * also support "remote wakeup", where the device can activate the USB
3053 * tree above them to deliver data, such as a keypress or packet. In
3054 * some cases, this wakes the USB host.
3055 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 * Suspending OTG devices may trigger HNP, if that's been enabled
3057 * between a pair of dual-role devices. That will change roles, such
3058 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
3059 *
Alan Stern4956ecc2007-05-30 16:51:28 -04003060 * Devices on USB hub ports have only one "suspend" state, corresponding
3061 * to ACPI D2, "may cause the device to lose some context".
3062 * State transitions include:
3063 *
3064 * - suspend, resume ... when the VBUS power link stays live
3065 * - suspend, disconnect ... VBUS lost
3066 *
3067 * Once VBUS drop breaks the circuit, the port it's using has to go through
3068 * normal re-enumeration procedures, starting with enabling VBUS power.
3069 * Other than re-initializing the hub (plug/unplug, except for root hubs),
3070 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
3071 * timer, no SRP, no requests through sysfs.
3072 *
Alan Sterne583d9d2013-07-11 14:58:04 -04003073 * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
3074 * suspended until their bus goes into global suspend (i.e., the root
Alan Stern0aa28322013-03-27 16:14:19 -04003075 * hub is suspended). Nevertheless, we change @udev->state to
3076 * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual
3077 * upstream port setting is stored in @udev->port_is_suspended.
Alan Stern4956ecc2007-05-30 16:51:28 -04003078 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 * Returns 0 on success, else negative errno.
3080 */
Alan Stern65bfd292008-11-25 16:39:18 -05003081int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
Lan Tianyuad493e52013-01-23 04:26:30 +08003083 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3084 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003085 int port1 = udev->portnum;
3086 int status;
Alan Stern0aa28322013-03-27 16:14:19 -04003087 bool really_suspend = true;
Alan Stern4956ecc2007-05-30 16:51:28 -04003088
Dan Williams5c79a1e2014-05-20 18:09:26 -07003089 usb_lock_port(port_dev);
3090
Alan Stern624d6c02007-05-30 15:35:16 -04003091 /* enable remote wakeup when appropriate; this lets the device
3092 * wake up the upstream hub (including maybe the root hub).
3093 *
3094 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
3095 * we don't explicitly enable it here.
3096 */
3097 if (udev->do_remote_wakeup) {
Alan Stern28e861652013-07-30 15:37:33 -04003098 status = usb_enable_remote_wakeup(udev);
Oliver Neukum0c487202009-10-19 13:19:41 +02003099 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003100 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
3101 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02003102 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02003103 if (PMSG_IS_AUTO(msg))
Alan Stern4fae6f02013-07-30 15:39:02 -04003104 goto err_wakeup;
Oliver Neukum0c487202009-10-19 13:19:41 +02003105 }
Alan Stern624d6c02007-05-30 15:35:16 -04003106 }
3107
Andiry Xu65580b432011-09-23 14:19:52 -07003108 /* disable USB2 hardware LPM */
3109 if (udev->usb2_hw_lpm_enabled == 1)
3110 usb_set_usb2_hardware_lpm(udev, 0);
3111
Sarah Sharpf74631e2012-06-25 12:08:08 -07003112 if (usb_disable_ltm(udev)) {
Alan Stern4fae6f02013-07-30 15:39:02 -04003113 dev_err(&udev->dev, "Failed to disable LTM before suspend\n.");
3114 status = -ENOMEM;
3115 if (PMSG_IS_AUTO(msg))
3116 goto err_ltm;
Sarah Sharpf74631e2012-06-25 12:08:08 -07003117 }
Sarah Sharp83060952012-05-02 14:25:52 -07003118 if (usb_unlocked_disable_lpm(udev)) {
Alan Stern4fae6f02013-07-30 15:39:02 -04003119 dev_err(&udev->dev, "Failed to disable LPM before suspend\n.");
3120 status = -ENOMEM;
3121 if (PMSG_IS_AUTO(msg))
3122 goto err_lpm3;
Sarah Sharp83060952012-05-02 14:25:52 -07003123 }
3124
Alan Stern624d6c02007-05-30 15:35:16 -04003125 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08003126 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003127 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
Alan Sterne583d9d2013-07-11 14:58:04 -04003128
Alan Stern0aa28322013-03-27 16:14:19 -04003129 /*
3130 * For system suspend, we do not need to enable the suspend feature
3131 * on individual USB-2 ports. The devices will automatically go
3132 * into suspend a few ms after the root hub stops sending packets.
3133 * The USB 2.0 spec calls this "global suspend".
Alan Sterne583d9d2013-07-11 14:58:04 -04003134 *
3135 * However, many USB hubs have a bug: They don't relay wakeup requests
3136 * from a downstream port if the port's suspend feature isn't on.
3137 * Therefore we will turn on the suspend feature if udev or any of its
3138 * descendants is enabled for remote wakeup.
Alan Stern0aa28322013-03-27 16:14:19 -04003139 */
Alan Sterne583d9d2013-07-11 14:58:04 -04003140 else if (PMSG_IS_AUTO(msg) || wakeup_enabled_descendants(udev) > 0)
3141 status = set_port_feature(hub->hdev, port1,
3142 USB_PORT_FEAT_SUSPEND);
Alan Stern0aa28322013-03-27 16:14:19 -04003143 else {
3144 really_suspend = false;
3145 status = 0;
3146 }
Alan Stern624d6c02007-05-30 15:35:16 -04003147 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003148 dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status);
Alan Sterncbb33002011-06-15 16:29:16 -04003149
Alan Stern4fae6f02013-07-30 15:39:02 -04003150 /* Try to enable USB3 LPM and LTM again */
3151 usb_unlocked_enable_lpm(udev);
3152 err_lpm3:
3153 usb_enable_ltm(udev);
3154 err_ltm:
Andiry Xuc3e751e2012-05-05 00:50:10 +08003155 /* Try to enable USB2 hardware LPM again */
3156 if (udev->usb2_hw_lpm_capable == 1)
3157 usb_set_usb2_hardware_lpm(udev, 1);
3158
Alan Stern4fae6f02013-07-30 15:39:02 -04003159 if (udev->do_remote_wakeup)
3160 (void) usb_disable_remote_wakeup(udev);
3161 err_wakeup:
Sarah Sharp83060952012-05-02 14:25:52 -07003162
Alan Sterncbb33002011-06-15 16:29:16 -04003163 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02003164 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003165 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04003166 } else {
Alan Stern30b1a7a2011-09-27 21:54:22 +02003167 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
3168 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
3169 udev->do_remote_wakeup);
Alan Stern0aa28322013-03-27 16:14:19 -04003170 if (really_suspend) {
3171 udev->port_is_suspended = 1;
Alan Sterne583d9d2013-07-11 14:58:04 -04003172
3173 /* device has up to 10 msec to fully suspend */
Alan Stern0aa28322013-03-27 16:14:19 -04003174 msleep(10);
3175 }
Alan Sterne583d9d2013-07-11 14:58:04 -04003176 usb_set_device_state(udev, USB_STATE_SUSPENDED);
Alan Stern624d6c02007-05-30 15:35:16 -04003177 }
Lan Tianyuad493e52013-01-23 04:26:30 +08003178
Dan Williamsd5c38342014-05-20 18:08:52 -07003179 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled
3180 && test_and_clear_bit(port1, hub->child_usage_bits))
Lan Tianyu98a4f1f2013-07-03 22:17:54 +08003181 pm_runtime_put_sync(&port_dev->dev);
Lan Tianyuad493e52013-01-23 04:26:30 +08003182
Alan Sternc08512c2010-11-15 15:57:58 -05003183 usb_mark_last_busy(hub->hdev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07003184
3185 usb_unlock_port(port_dev);
Alan Stern4956ecc2007-05-30 16:51:28 -04003186 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
David Brownellf3f32532005-09-22 22:37:29 -07003188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189/*
David Brownell390a8c32005-09-13 19:57:27 -07003190 * If the USB "suspend" state is in use (rather than "global suspend"),
3191 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04003192 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 * hardware resume signaling is finished, either because of selective
3194 * resume (by host) or remote wakeup (by device) ... now see what changed
3195 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04003196 *
3197 * If @udev->reset_resume is set then the device is reset before the
3198 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 */
Alan Stern140d8f62006-07-01 22:07:21 -04003200static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
Alan Stern54515fe2007-05-30 15:38:58 -04003202 int status = 0;
Oliver Neukum07e72b92012-11-29 15:05:57 +01003203 u16 devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
3205 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003206 dev_dbg(&udev->dev, "%s\n",
3207 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
3209 /* usb ch9 identifies four variants of SUSPENDED, based on what
3210 * state the device resumes to. Linux currently won't see the
3211 * first two on the host side; they'd be inside hub_port_init()
3212 * during many timeouts, but khubd can't suspend until later.
3213 */
3214 usb_set_device_state(udev, udev->actconfig
3215 ? USB_STATE_CONFIGURED
3216 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Alan Stern54515fe2007-05-30 15:38:58 -04003218 /* 10.5.4.5 says not to reset a suspended port if the attached
3219 * device is enabled for remote wakeup. Hence the reset
3220 * operation is carried out here, after the port has been
3221 * resumed.
3222 */
Alan Stern1d102552014-03-18 10:39:05 -04003223 if (udev->reset_resume) {
3224 /*
3225 * If the device morphs or switches modes when it is reset,
3226 * we don't want to perform a reset-resume. We'll fail the
3227 * resume, which will cause a logical disconnect, and then
3228 * the device will be rediscovered.
3229 */
Alan Stern86c57ed2008-06-30 11:14:43 -04003230 retry_reset_resume:
Alan Stern1d102552014-03-18 10:39:05 -04003231 if (udev->quirks & USB_QUIRK_RESET)
3232 status = -ENODEV;
3233 else
3234 status = usb_reset_and_verify_device(udev);
3235 }
Alan Stern54515fe2007-05-30 15:38:58 -04003236
Matthias Beyer469271f2013-10-10 23:41:27 +02003237 /* 10.5.4.5 says be sure devices in the tree are still there.
3238 * For now let's assume the device didn't go crazy on resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 * and device drivers will know about any resume quirks.
3240 */
Alan Stern54515fe2007-05-30 15:38:58 -04003241 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04003242 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04003243 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
Alan Stern86c57ed2008-06-30 11:14:43 -04003244
3245 /* If a normal resume failed, try doing a reset-resume */
3246 if (status && !udev->reset_resume && udev->persist_enabled) {
3247 dev_dbg(&udev->dev, "retry with reset-resume\n");
3248 udev->reset_resume = 1;
3249 goto retry_reset_resume;
3250 }
Alan Stern54515fe2007-05-30 15:38:58 -04003251 }
Alan Sternb40b7a92006-06-19 15:12:38 -04003252
Alan Stern624d6c02007-05-30 15:35:16 -04003253 if (status) {
3254 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
3255 status);
Oliver Neukum07e72b92012-11-29 15:05:57 +01003256 /*
3257 * There are a few quirky devices which violate the standard
3258 * by claiming to have remote wakeup enabled after a reset,
3259 * which crash if the feature is cleared, hence check for
3260 * udev->reset_resume
3261 */
3262 } else if (udev->actconfig && !udev->reset_resume) {
Alan Stern28e861652013-07-30 15:37:33 -04003263 if (udev->speed < USB_SPEED_SUPER) {
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003264 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
Alan Stern28e861652013-07-30 15:37:33 -04003265 status = usb_disable_remote_wakeup(udev);
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003266 } else {
3267 status = usb_get_status(udev, USB_RECIP_INTERFACE, 0,
3268 &devstatus);
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003269 if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP
3270 | USB_INTRF_STAT_FUNC_RW))
Alan Stern28e861652013-07-30 15:37:33 -04003271 status = usb_disable_remote_wakeup(udev);
Alan Stern4bf0ba82005-11-21 11:58:07 -05003272 }
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003273
3274 if (status)
3275 dev_dbg(&udev->dev,
3276 "disable remote wakeup, status %d\n",
3277 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 }
3280 return status;
3281}
3282
Alan Stern624d6c02007-05-30 15:35:16 -04003283/*
Pratyush Ananda40178b2014-07-18 12:37:10 +05303284 * There are some SS USB devices which take longer time for link training.
3285 * XHCI specs 4.19.4 says that when Link training is successful, port
3286 * sets CSC bit to 1. So if SW reads port status before successful link
3287 * training, then it will not find device to be present.
3288 * USB Analyzer log with such buggy devices show that in some cases
3289 * device switch on the RX termination after long delay of host enabling
3290 * the VBUS. In few other cases it has been seen that device fails to
3291 * negotiate link training in first attempt. It has been
3292 * reported till now that few devices take as long as 2000 ms to train
3293 * the link after host enabling its VBUS and termination. Following
3294 * routine implements a 2000 ms timeout for link training. If in a case
3295 * link trains before timeout, loop will exit earlier.
3296 *
3297 * FIXME: If a device was connected before suspend, but was removed
3298 * while system was asleep, then the loop in the following routine will
3299 * only exit at timeout.
3300 *
3301 * This routine should only be called when persist is enabled for a SS
3302 * device.
3303 */
3304static int wait_for_ss_port_enable(struct usb_device *udev,
3305 struct usb_hub *hub, int *port1,
3306 u16 *portchange, u16 *portstatus)
3307{
3308 int status = 0, delay_ms = 0;
3309
3310 while (delay_ms < 2000) {
3311 if (status || *portstatus & USB_PORT_STAT_CONNECTION)
3312 break;
3313 msleep(20);
3314 delay_ms += 20;
3315 status = hub_port_status(hub, *port1, portstatus, portchange);
3316 }
3317 return status;
3318}
3319
3320/*
Alan Stern624d6c02007-05-30 15:35:16 -04003321 * usb_port_resume - re-activate a suspended usb device's upstream port
3322 * @udev: device to re-activate, not a root hub
3323 * Context: must be able to sleep; device not locked; pm locks held
3324 *
3325 * This will re-activate the suspended device, increasing power usage
3326 * while letting drivers communicate again with its endpoints.
3327 * USB resume explicitly guarantees that the power session between
3328 * the host and the device is the same as it was when the device
3329 * suspended.
3330 *
Alan Sternfeccc302008-03-03 15:15:59 -05003331 * If @udev->reset_resume is set then this routine won't check that the
3332 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04003333 * reset @udev. The end result is that a broken power session can be
3334 * recovered and @udev will appear to persist across a loss of VBUS power.
3335 *
3336 * For example, if a host controller doesn't maintain VBUS suspend current
3337 * during a system sleep or is reset when the system wakes up, all the USB
3338 * power sessions below it will be broken. This is especially troublesome
3339 * for mass-storage devices containing mounted filesystems, since the
3340 * device will appear to have disconnected and all the memory mappings
3341 * to it will be lost. Using the USB_PERSIST facility, the device can be
3342 * made to appear as if it had not disconnected.
3343 *
Ming Lei742120c2008-06-18 22:00:29 +08003344 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05003345 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04003346 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3347 * quite possible for a device to remain unaltered but its media to be
3348 * changed. If the user replaces a flash memory card while the system is
3349 * asleep, he will have only himself to blame when the filesystem on the
3350 * new card is corrupted and the system crashes.
3351 *
Alan Stern624d6c02007-05-30 15:35:16 -04003352 * Returns 0 on success, else negative errno.
3353 */
Alan Stern65bfd292008-11-25 16:39:18 -05003354int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
Lan Tianyuad493e52013-01-23 04:26:30 +08003356 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3357 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003358 int port1 = udev->portnum;
3359 int status;
3360 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003361
Dan Williamsd5c38342014-05-20 18:08:52 -07003362 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08003363 status = pm_runtime_get_sync(&port_dev->dev);
Lan Tianyuad493e52013-01-23 04:26:30 +08003364 if (status < 0) {
3365 dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
3366 status);
3367 return status;
3368 }
3369 }
3370
Dan Williams5c79a1e2014-05-20 18:09:26 -07003371 usb_lock_port(port_dev);
3372
Alan Sternd25450c2006-11-20 11:14:30 -05003373 /* Skip the initial Clear-Suspend step for a remote wakeup */
3374 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003375 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003376 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003379 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003380 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
Andiry Xua7114232011-04-27 18:07:50 +08003381 else
Lan Tianyuad493e52013-01-23 04:26:30 +08003382 status = usb_clear_port_feature(hub->hdev,
Andiry Xua7114232011-04-27 18:07:50 +08003383 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003385 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04003388 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003389 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 msleep(25);
3391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3393 * stop resume signaling. Then finish the resume
3394 * sequence.
3395 */
Alan Sternd25450c2006-11-20 11:14:30 -05003396 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003397
Alan Sternb01b03f2008-04-28 11:06:11 -04003398 /* TRSMRCY = 10 msec */
3399 msleep(10);
3400 }
Alan Stern54515fe2007-05-30 15:38:58 -04003401
Alan Sternb01b03f2008-04-28 11:06:11 -04003402 SuspendCleared:
3403 if (status == 0) {
Alan Sternbfd1e912012-10-19 11:03:39 -04003404 udev->port_is_suspended = 0;
Andiry Xua7114232011-04-27 18:07:50 +08003405 if (hub_is_superspeed(hub->hdev)) {
3406 if (portchange & USB_PORT_STAT_C_LINK_STATE)
Lan Tianyuad493e52013-01-23 04:26:30 +08003407 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003408 USB_PORT_FEAT_C_PORT_LINK_STATE);
3409 } else {
3410 if (portchange & USB_PORT_STAT_C_SUSPEND)
Lan Tianyuad493e52013-01-23 04:26:30 +08003411 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003412 USB_PORT_FEAT_C_SUSPEND);
3413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
Pratyush Ananda40178b2014-07-18 12:37:10 +05303416 if (udev->persist_enabled && hub_is_superspeed(hub->hdev))
3417 status = wait_for_ss_port_enable(udev, hub, &port1, &portchange,
3418 &portstatus);
3419
Alan Sternb01b03f2008-04-28 11:06:11 -04003420 status = check_port_resume_type(udev,
3421 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003422 if (status == 0)
3423 status = finish_port_resume(udev);
3424 if (status < 0) {
3425 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3426 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003427 } else {
3428 /* Try to enable USB2 hardware LPM */
3429 if (udev->usb2_hw_lpm_capable == 1)
3430 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003431
Sarah Sharpf74631e2012-06-25 12:08:08 -07003432 /* Try to enable USB3 LTM and LPM */
3433 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003434 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003435 }
Andiry Xu65580b432011-09-23 14:19:52 -07003436
Dan Williams5c79a1e2014-05-20 18:09:26 -07003437 usb_unlock_port(port_dev);
3438
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 return status;
3440}
3441
Alan Stern84ebc102013-03-27 16:14:46 -04003442#ifdef CONFIG_PM_RUNTIME
3443
Alan Stern0534d462010-01-08 12:56:30 -05003444int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445{
3446 int status = 0;
3447
Dan Williams5c79a1e2014-05-20 18:09:26 -07003448 usb_lock_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003450 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003451 status = usb_autoresume_device(udev);
3452 if (status == 0) {
3453 /* Let the drivers do their thing, then... */
3454 usb_autosuspend_device(udev);
3455 }
Alan Sternd25450c2006-11-20 11:14:30 -05003456 }
Dan Williams5c79a1e2014-05-20 18:09:26 -07003457 usb_unlock_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 return status;
3459}
3460
Dan Williams7e73be22014-05-20 18:09:31 -07003461/* Returns 1 if there was a remote wakeup and a connect status change. */
3462static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
3463 u16 portstatus, u16 portchange)
3464 __must_hold(&port_dev->status_lock)
3465{
3466 struct usb_port *port_dev = hub->ports[port - 1];
3467 struct usb_device *hdev;
3468 struct usb_device *udev;
3469 int connect_change = 0;
3470 int ret;
3471
3472 hdev = hub->hdev;
3473 udev = port_dev->child;
3474 if (!hub_is_superspeed(hdev)) {
3475 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
3476 return 0;
3477 usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
3478 } else {
3479 if (!udev || udev->state != USB_STATE_SUSPENDED ||
3480 (portstatus & USB_PORT_STAT_LINK_STATE) !=
3481 USB_SS_PORT_LS_U0)
3482 return 0;
3483 }
3484
3485 if (udev) {
3486 /* TRSMRCY = 10 msec */
3487 msleep(10);
3488
3489 usb_unlock_port(port_dev);
3490 ret = usb_remote_wakeup(udev);
3491 usb_lock_port(port_dev);
3492 if (ret < 0)
3493 connect_change = 1;
3494 } else {
3495 ret = -ENODEV;
3496 hub_port_disable(hub, port, 1);
3497 }
3498 dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
3499 return connect_change;
3500}
3501
3502#else
3503
3504static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
3505 u16 portstatus, u16 portchange)
3506{
3507 return 0;
3508}
3509
Alan Sternd388dab2006-07-01 22:14:24 -04003510#endif
3511
Ming Leie6f30de2012-10-24 11:59:24 +08003512static int check_ports_changed(struct usb_hub *hub)
3513{
3514 int port1;
3515
3516 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3517 u16 portstatus, portchange;
3518 int status;
3519
3520 status = hub_port_status(hub, port1, &portstatus, &portchange);
3521 if (!status && portchange)
3522 return 1;
3523 }
3524 return 0;
3525}
3526
David Brownelldb690872005-09-13 19:56:33 -07003527static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
3529 struct usb_hub *hub = usb_get_intfdata (intf);
3530 struct usb_device *hdev = hub->hdev;
3531 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003532 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Alan Sterne583d9d2013-07-11 14:58:04 -04003534 /*
3535 * Warn if children aren't already suspended.
3536 * Also, add up the number of wakeup-enabled descendants.
3537 */
3538 hub->wakeup_enabled_descendants = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003540 struct usb_port *port_dev = hub->ports[port1 - 1];
3541 struct usb_device *udev = port_dev->child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Alan Stern6840d252007-09-10 11:34:26 -04003543 if (udev && udev->can_submit) {
Dan Williamsb658b8f2014-06-17 16:16:22 -07003544 dev_warn(&port_dev->dev, "device %s not suspended yet\n",
3545 dev_name(&udev->dev));
Alan Stern5b1b0b82011-08-19 23:49:48 +02003546 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003547 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003548 }
Alan Sterne583d9d2013-07-11 14:58:04 -04003549 if (udev)
3550 hub->wakeup_enabled_descendants +=
3551 wakeup_enabled_descendants(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 }
Ming Leie6f30de2012-10-24 11:59:24 +08003553
3554 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3555 /* check if there are changes pending on hub ports */
3556 if (check_ports_changed(hub)) {
3557 if (PMSG_IS_AUTO(msg))
3558 return -EBUSY;
3559 pm_wakeup_event(&hdev->dev, 2000);
3560 }
3561 }
3562
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003563 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3564 /* Enable hub to send remote wakeup for all ports. */
3565 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3566 status = set_port_feature(hdev,
3567 port1 |
3568 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3569 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3570 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3571 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3572 }
3573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Harvey Harrison441b62c2008-03-03 16:08:34 -08003575 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003576
David Brownellc9f89fa2005-09-13 19:57:04 -07003577 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003578 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580}
3581
3582static int hub_resume(struct usb_interface *intf)
3583{
Alan Stern5e6effa2008-03-03 15:15:51 -05003584 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
Alan Stern5e6effa2008-03-03 15:15:51 -05003586 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003587 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 return 0;
3589}
3590
Alan Sternb41a60e2007-05-30 15:39:33 -04003591static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003592{
Alan Sternb41a60e2007-05-30 15:39:33 -04003593 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003594
Alan Stern5e6effa2008-03-03 15:15:51 -05003595 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003596 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003597 return 0;
3598}
3599
Alan Stern54515fe2007-05-30 15:38:58 -04003600/**
3601 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3602 * @rhdev: struct usb_device for the root hub
3603 *
3604 * The USB host controller driver calls this function when its root hub
3605 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003606 * has been reset. The routine marks @rhdev as having lost power.
3607 * When the hub driver is resumed it will take notice and carry out
3608 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3609 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003610 */
3611void usb_root_hub_lost_power(struct usb_device *rhdev)
3612{
3613 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3614 rhdev->reset_resume = 1;
3615}
3616EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3617
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003618static const char * const usb3_lpm_names[] = {
3619 "U0",
3620 "U1",
3621 "U2",
3622 "U3",
3623};
3624
3625/*
3626 * Send a Set SEL control transfer to the device, prior to enabling
3627 * device-initiated U1 or U2. This lets the device know the exit latencies from
3628 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3629 * packet from the host.
3630 *
3631 * This function will fail if the SEL or PEL values for udev are greater than
3632 * the maximum allowed values for the link state to be enabled.
3633 */
3634static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3635{
3636 struct usb_set_sel_req *sel_values;
3637 unsigned long long u1_sel;
3638 unsigned long long u1_pel;
3639 unsigned long long u2_sel;
3640 unsigned long long u2_pel;
3641 int ret;
3642
Xenia Ragiadakou38d7f682013-09-04 17:24:45 +03003643 if (udev->state != USB_STATE_CONFIGURED)
3644 return 0;
3645
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003646 /* Convert SEL and PEL stored in ns to us */
3647 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3648 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3649 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3650 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3651
3652 /*
3653 * Make sure that the calculated SEL and PEL values for the link
3654 * state we're enabling aren't bigger than the max SEL/PEL
3655 * value that will fit in the SET SEL control transfer.
3656 * Otherwise the device would get an incorrect idea of the exit
3657 * latency for the link state, and could start a device-initiated
3658 * U1/U2 when the exit latencies are too high.
3659 */
3660 if ((state == USB3_LPM_U1 &&
3661 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3662 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3663 (state == USB3_LPM_U2 &&
3664 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3665 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003666 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 -07003667 usb3_lpm_names[state], u1_sel, u1_pel);
3668 return -EINVAL;
3669 }
3670
3671 /*
3672 * If we're enabling device-initiated LPM for one link state,
3673 * but the other link state has a too high SEL or PEL value,
3674 * just set those values to the max in the Set SEL request.
3675 */
3676 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3677 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3678
3679 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3680 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3681
3682 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3683 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3684
3685 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3686 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3687
3688 /*
3689 * usb_enable_lpm() can be called as part of a failed device reset,
3690 * which may be initiated by an error path of a mass storage driver.
3691 * Therefore, use GFP_NOIO.
3692 */
3693 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3694 if (!sel_values)
3695 return -ENOMEM;
3696
3697 sel_values->u1_sel = u1_sel;
3698 sel_values->u1_pel = u1_pel;
3699 sel_values->u2_sel = cpu_to_le16(u2_sel);
3700 sel_values->u2_pel = cpu_to_le16(u2_pel);
3701
3702 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3703 USB_REQ_SET_SEL,
3704 USB_RECIP_DEVICE,
3705 0, 0,
3706 sel_values, sizeof *(sel_values),
3707 USB_CTRL_SET_TIMEOUT);
3708 kfree(sel_values);
3709 return ret;
3710}
3711
3712/*
3713 * Enable or disable device-initiated U1 or U2 transitions.
3714 */
3715static int usb_set_device_initiated_lpm(struct usb_device *udev,
3716 enum usb3_link_state state, bool enable)
3717{
3718 int ret;
3719 int feature;
3720
3721 switch (state) {
3722 case USB3_LPM_U1:
3723 feature = USB_DEVICE_U1_ENABLE;
3724 break;
3725 case USB3_LPM_U2:
3726 feature = USB_DEVICE_U2_ENABLE;
3727 break;
3728 default:
3729 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3730 __func__, enable ? "enable" : "disable");
3731 return -EINVAL;
3732 }
3733
3734 if (udev->state != USB_STATE_CONFIGURED) {
3735 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3736 "for unconfigured device.\n",
3737 __func__, enable ? "enable" : "disable",
3738 usb3_lpm_names[state]);
3739 return 0;
3740 }
3741
3742 if (enable) {
3743 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003744 * Now send the control transfer to enable device-initiated LPM
3745 * for either U1 or U2.
3746 */
3747 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3748 USB_REQ_SET_FEATURE,
3749 USB_RECIP_DEVICE,
3750 feature,
3751 0, NULL, 0,
3752 USB_CTRL_SET_TIMEOUT);
3753 } else {
3754 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3755 USB_REQ_CLEAR_FEATURE,
3756 USB_RECIP_DEVICE,
3757 feature,
3758 0, NULL, 0,
3759 USB_CTRL_SET_TIMEOUT);
3760 }
3761 if (ret < 0) {
3762 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3763 enable ? "Enable" : "Disable",
3764 usb3_lpm_names[state]);
3765 return -EBUSY;
3766 }
3767 return 0;
3768}
3769
3770static int usb_set_lpm_timeout(struct usb_device *udev,
3771 enum usb3_link_state state, int timeout)
3772{
3773 int ret;
3774 int feature;
3775
3776 switch (state) {
3777 case USB3_LPM_U1:
3778 feature = USB_PORT_FEAT_U1_TIMEOUT;
3779 break;
3780 case USB3_LPM_U2:
3781 feature = USB_PORT_FEAT_U2_TIMEOUT;
3782 break;
3783 default:
3784 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3785 __func__);
3786 return -EINVAL;
3787 }
3788
3789 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3790 timeout != USB3_LPM_DEVICE_INITIATED) {
3791 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3792 "which is a reserved value.\n",
3793 usb3_lpm_names[state], timeout);
3794 return -EINVAL;
3795 }
3796
3797 ret = set_port_feature(udev->parent,
3798 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3799 feature);
3800 if (ret < 0) {
3801 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3802 "error code %i\n", usb3_lpm_names[state],
3803 timeout, ret);
3804 return -EBUSY;
3805 }
3806 if (state == USB3_LPM_U1)
3807 udev->u1_params.timeout = timeout;
3808 else
3809 udev->u2_params.timeout = timeout;
3810 return 0;
3811}
3812
3813/*
3814 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3815 * U1/U2 entry.
3816 *
3817 * We will attempt to enable U1 or U2, but there are no guarantees that the
3818 * control transfers to set the hub timeout or enable device-initiated U1/U2
3819 * will be successful.
3820 *
3821 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3822 * driver know about it. If that call fails, it should be harmless, and just
3823 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3824 */
3825static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3826 enum usb3_link_state state)
3827{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003828 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003829 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3830 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3831
3832 /* If the device says it doesn't have *any* exit latency to come out of
3833 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3834 * state.
3835 */
3836 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3837 (state == USB3_LPM_U2 && u2_mel == 0))
3838 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003839
Sarah Sharp65a95b72012-10-05 10:32:07 -07003840 /*
3841 * First, let the device know about the exit latencies
3842 * associated with the link state we're about to enable.
3843 */
3844 ret = usb_req_set_sel(udev, state);
3845 if (ret < 0) {
3846 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3847 usb3_lpm_names[state]);
3848 return;
3849 }
3850
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003851 /* We allow the host controller to set the U1/U2 timeout internally
3852 * first, so that it can change its schedule to account for the
3853 * additional latency to send data to a device in a lower power
3854 * link state.
3855 */
3856 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3857
3858 /* xHCI host controller doesn't want to enable this LPM state. */
3859 if (timeout == 0)
3860 return;
3861
3862 if (timeout < 0) {
3863 dev_warn(&udev->dev, "Could not enable %s link state, "
3864 "xHCI error %i.\n", usb3_lpm_names[state],
3865 timeout);
3866 return;
3867 }
3868
3869 if (usb_set_lpm_timeout(udev, state, timeout))
3870 /* If we can't set the parent hub U1/U2 timeout,
3871 * device-initiated LPM won't be allowed either, so let the xHCI
3872 * host know that this link state won't be enabled.
3873 */
3874 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3875
3876 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3877 else if (udev->actconfig)
3878 usb_set_device_initiated_lpm(udev, state, true);
3879
3880}
3881
3882/*
3883 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3884 * U1/U2 entry.
3885 *
3886 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3887 * If zero is returned, the parent will not allow the link to go into U1/U2.
3888 *
3889 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3890 * it won't have an effect on the bus link state because the parent hub will
3891 * still disallow device-initiated U1/U2 entry.
3892 *
3893 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3894 * possible. The result will be slightly more bus bandwidth will be taken up
3895 * (to account for U1/U2 exit latency), but it should be harmless.
3896 */
3897static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3898 enum usb3_link_state state)
3899{
3900 int feature;
3901
3902 switch (state) {
3903 case USB3_LPM_U1:
3904 feature = USB_PORT_FEAT_U1_TIMEOUT;
3905 break;
3906 case USB3_LPM_U2:
3907 feature = USB_PORT_FEAT_U2_TIMEOUT;
3908 break;
3909 default:
3910 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3911 __func__);
3912 return -EINVAL;
3913 }
3914
3915 if (usb_set_lpm_timeout(udev, state, 0))
3916 return -EBUSY;
3917
3918 usb_set_device_initiated_lpm(udev, state, false);
3919
3920 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3921 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3922 "bus schedule bandwidth may be impacted.\n",
3923 usb3_lpm_names[state]);
3924 return 0;
3925}
3926
3927/*
3928 * Disable hub-initiated and device-initiated U1 and U2 entry.
3929 * Caller must own the bandwidth_mutex.
3930 *
3931 * This will call usb_enable_lpm() on failure, which will decrement
3932 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3933 */
3934int usb_disable_lpm(struct usb_device *udev)
3935{
3936 struct usb_hcd *hcd;
3937
3938 if (!udev || !udev->parent ||
3939 udev->speed != USB_SPEED_SUPER ||
Pratyush Anand51df62f2014-07-04 17:01:27 +03003940 !udev->lpm_capable ||
3941 udev->state < USB_STATE_DEFAULT)
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003942 return 0;
3943
3944 hcd = bus_to_hcd(udev->bus);
3945 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3946 return 0;
3947
3948 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003949 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003950 return 0;
3951
3952 /* If LPM is enabled, attempt to disable it. */
3953 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3954 goto enable_lpm;
3955 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3956 goto enable_lpm;
3957
3958 return 0;
3959
3960enable_lpm:
3961 usb_enable_lpm(udev);
3962 return -EBUSY;
3963}
3964EXPORT_SYMBOL_GPL(usb_disable_lpm);
3965
3966/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3967int usb_unlocked_disable_lpm(struct usb_device *udev)
3968{
3969 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3970 int ret;
3971
3972 if (!hcd)
3973 return -EINVAL;
3974
3975 mutex_lock(hcd->bandwidth_mutex);
3976 ret = usb_disable_lpm(udev);
3977 mutex_unlock(hcd->bandwidth_mutex);
3978
3979 return ret;
3980}
3981EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3982
3983/*
3984 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3985 * xHCI host policy may prevent U1 or U2 from being enabled.
3986 *
3987 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3988 * until the lpm_disable_count drops to zero. Caller must own the
3989 * bandwidth_mutex.
3990 */
3991void usb_enable_lpm(struct usb_device *udev)
3992{
3993 struct usb_hcd *hcd;
3994
3995 if (!udev || !udev->parent ||
3996 udev->speed != USB_SPEED_SUPER ||
Pratyush Anand51df62f2014-07-04 17:01:27 +03003997 !udev->lpm_capable ||
3998 udev->state < USB_STATE_DEFAULT)
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003999 return;
4000
4001 udev->lpm_disable_count--;
4002 hcd = bus_to_hcd(udev->bus);
4003 /* Double check that we can both enable and disable LPM.
4004 * Device must be configured to accept set feature U1/U2 timeout.
4005 */
4006 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
4007 !hcd->driver->disable_usb3_lpm_timeout)
4008 return;
4009
4010 if (udev->lpm_disable_count > 0)
4011 return;
4012
4013 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
4014 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
4015}
4016EXPORT_SYMBOL_GPL(usb_enable_lpm);
4017
4018/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
4019void usb_unlocked_enable_lpm(struct usb_device *udev)
4020{
4021 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4022
4023 if (!hcd)
4024 return;
4025
4026 mutex_lock(hcd->bandwidth_mutex);
4027 usb_enable_lpm(udev);
4028 mutex_unlock(hcd->bandwidth_mutex);
4029}
4030EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
4031
4032
Alan Sternd388dab2006-07-01 22:14:24 -04004033#else /* CONFIG_PM */
4034
Alan Sternf07600c2007-05-30 15:38:16 -04004035#define hub_suspend NULL
4036#define hub_resume NULL
4037#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004038
4039int usb_disable_lpm(struct usb_device *udev)
4040{
4041 return 0;
4042}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004043EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004044
4045void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004046EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004047
4048int usb_unlocked_disable_lpm(struct usb_device *udev)
4049{
4050 return 0;
4051}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004052EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004053
4054void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004055EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07004056
4057int usb_disable_ltm(struct usb_device *udev)
4058{
4059 return 0;
4060}
4061EXPORT_SYMBOL_GPL(usb_disable_ltm);
4062
4063void usb_enable_ltm(struct usb_device *udev) { }
4064EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternc4b51a42013-07-11 14:58:23 -04004065
Stephen Rothwell4a95b1f2014-05-29 18:55:06 +10004066static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
4067 u16 portstatus, u16 portchange)
4068{
4069 return 0;
4070}
4071
Alan Sternc4b51a42013-07-11 14:58:23 -04004072#endif /* CONFIG_PM */
Alan Sternd388dab2006-07-01 22:14:24 -04004073
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
4075/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
4076 *
4077 * Between connect detection and reset signaling there must be a delay
4078 * of 100ms at least for debounce and power-settling. The corresponding
4079 * timer shall restart whenever the downstream port detects a disconnect.
Matthias Beyer469271f2013-10-10 23:41:27 +02004080 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 * Apparently there are some bluetooth and irda-dongles and a number of
4082 * low-speed devices for which this debounce period may last over a second.
4083 * Not covered by the spec - but easy to deal with.
4084 *
4085 * This implementation uses a 1500ms total debounce timeout; if the
4086 * connection isn't stable by then it returns -ETIMEDOUT. It checks
4087 * every 25ms for transient disconnects. When the port status has been
4088 * unchanged for 100ms it returns the port status.
4089 */
Lan Tianyuad493e52013-01-23 04:26:30 +08004090int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091{
4092 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 u16 portchange, portstatus;
4094 unsigned connection = 0xffff;
Dan Williamsd99f6b42014-05-20 18:08:17 -07004095 int total_time, stable_time = 0;
4096 struct usb_port *port_dev = hub->ports[port1 - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
4098 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
4099 ret = hub_port_status(hub, port1, &portstatus, &portchange);
4100 if (ret < 0)
4101 return ret;
4102
4103 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
4104 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004105 if (!must_be_connected ||
4106 (connection == USB_PORT_STAT_CONNECTION))
4107 stable_time += HUB_DEBOUNCE_STEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 if (stable_time >= HUB_DEBOUNCE_STABLE)
4109 break;
4110 } else {
4111 stable_time = 0;
4112 connection = portstatus & USB_PORT_STAT_CONNECTION;
4113 }
4114
4115 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004116 usb_clear_port_feature(hub->hdev, port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 USB_PORT_FEAT_C_CONNECTION);
4118 }
4119
4120 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
4121 break;
4122 msleep(HUB_DEBOUNCE_STEP);
4123 }
4124
Dan Williamsd99f6b42014-05-20 18:08:17 -07004125 dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n",
4126 total_time, stable_time, portstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
4128 if (stable_time < HUB_DEBOUNCE_STABLE)
4129 return -ETIMEDOUT;
4130 return portstatus;
4131}
4132
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004133void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134{
Alan Sternddeac4e2009-01-15 17:03:33 -05004135 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
4136 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05004137 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004139EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
4141#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
4142#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
4143
Alan Stern4326ed02007-07-30 17:08:43 -04004144static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145{
4146 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07004147 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
Sarah Sharpc6515272009-04-27 19:57:26 -07004149 /*
4150 * The host controller will choose the device address,
4151 * instead of the core having chosen it earlier
4152 */
4153 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 return -EINVAL;
4155 if (udev->state == USB_STATE_ADDRESS)
4156 return 0;
4157 if (udev->state != USB_STATE_DEFAULT)
4158 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07004159 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07004160 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004161 else
Sarah Sharpc6515272009-04-27 19:57:26 -07004162 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
4163 USB_REQ_SET_ADDRESS, 0, devnum, 0,
4164 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05004166 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07004167 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004169 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 }
4171 return retval;
4172}
4173
Mathias Nyman890dae82013-09-30 17:26:31 +03004174/*
4175 * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM
4176 * when they're plugged into a USB 2.0 port, but they don't work when LPM is
4177 * enabled.
4178 *
4179 * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the
4180 * device says it supports the new USB 2.0 Link PM errata by setting the BESL
4181 * support bit in the BOS descriptor.
4182 */
4183static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev)
4184{
Dan Williamsd99f6b42014-05-20 18:08:17 -07004185 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
4186 int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
Mathias Nyman890dae82013-09-30 17:26:31 +03004187
4188 if (!udev->usb2_hw_lpm_capable)
4189 return;
4190
Dan Williamsd99f6b42014-05-20 18:08:17 -07004191 if (hub)
4192 connect_type = hub->ports[udev->portnum - 1]->connect_type;
Mathias Nyman890dae82013-09-30 17:26:31 +03004193
Bjørn Mork23c05822014-02-27 14:23:55 +01004194 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) ||
Mathias Nyman890dae82013-09-30 17:26:31 +03004195 connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
4196 udev->usb2_hw_lpm_allowed = 1;
4197 usb_set_usb2_hardware_lpm(udev, 1);
4198 }
4199}
4200
Dan Williams48fc7db2013-12-05 17:07:27 -08004201static int hub_enable_device(struct usb_device *udev)
4202{
4203 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4204
4205 if (!hcd->driver->enable_device)
4206 return 0;
4207 if (udev->state == USB_STATE_ADDRESS)
4208 return 0;
4209 if (udev->state != USB_STATE_DEFAULT)
4210 return -EINVAL;
4211
4212 return hcd->driver->enable_device(hcd, udev);
4213}
4214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215/* Reset device, (re)assign address, get device descriptor.
4216 * Device connection must be stable, no more debouncing needed.
4217 * Returns device in USB_STATE_ADDRESS, except on error.
4218 *
4219 * If this is called for an already-existing device (as part of
Dan Williams5c79a1e2014-05-20 18:09:26 -07004220 * usb_reset_and_verify_device), the caller must own the device lock and
4221 * the port lock. For a newly detected device that is not accessible
4222 * through any global pointers, it's not necessary to lock the device,
4223 * but it is still necessary to lock the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 */
4225static int
4226hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
4227 int retry_counter)
4228{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07004230 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 int i, j, retval;
4232 unsigned delay = HUB_SHORT_RESET_TIME;
4233 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004234 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04004235 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
4237 /* root hub ports have a slightly longer reset period
4238 * (from USB 2.0 spec, section 7.1.7.5)
4239 */
4240 if (!hdev->parent) {
4241 delay = HUB_ROOT_RESET_TIME;
4242 if (port1 == hdev->bus->otg_port)
4243 hdev->bus->b_hnp_enable = 0;
4244 }
4245
4246 /* Some low speed devices have problems with the quick delay, so */
4247 /* be a bit pessimistic with those devices. RHbug #23670 */
4248 if (oldspeed == USB_SPEED_LOW)
4249 delay = HUB_LONG_RESET_TIME;
4250
Todd E Brandt6fecd4f2014-05-19 10:55:32 -07004251 mutex_lock(&hdev->bus->usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
Luben Tuikov07194ab2011-02-11 11:33:10 -08004253 /* Reset the device; full speed may morph to high speed */
4254 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07004255 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08004256 if (retval < 0) /* error or disconnect */
4257 goto fail;
4258 /* success, speed is known */
4259
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 retval = -ENODEV;
4261
4262 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
4263 dev_dbg(&udev->dev, "device reset changed speed!\n");
4264 goto fail;
4265 }
4266 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04004267
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
4269 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004270 * For Wireless USB devices, ep0 max packet is always 512 (tho
4271 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 */
4273 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07004274 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08004275 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004276 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004279 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 break;
4281 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
4282 /* to determine the ep0 maxpacket size, try to read
4283 * the device descriptor to get bMaxPacketSize0 and
4284 * then correct our initial guess.
4285 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004286 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 break;
4288 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004289 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 break;
4291 default:
4292 goto fail;
4293 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004294
4295 if (udev->speed == USB_SPEED_WIRELESS)
4296 speed = "variable speed Wireless";
4297 else
4298 speed = usb_speed_string(udev->speed);
4299
Sarah Sharpc6515272009-04-27 19:57:26 -07004300 if (udev->speed != USB_SPEED_SUPER)
4301 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004302 "%s %s USB device number %d using %s\n",
4303 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05004304 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
4306 /* Set up TT records, if needed */
4307 if (hdev->tt) {
4308 udev->tt = hdev->tt;
4309 udev->ttport = hdev->ttport;
4310 } else if (udev->speed != USB_SPEED_HIGH
4311 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05004312 if (!hub->tt.hub) {
4313 dev_err(&udev->dev, "parent hub has no TT\n");
4314 retval = -EINVAL;
4315 goto fail;
4316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 udev->tt = &hub->tt;
4318 udev->ttport = port1;
4319 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
4322 * Because device hardware and firmware is sometimes buggy in
4323 * this area, and this is how Linux has done it for ages.
4324 * Change it cautiously.
4325 *
Dan Williams48fc7db2013-12-05 17:07:27 -08004326 * NOTE: If use_new_scheme() is true we will start by issuing
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
4328 * so it may help with some non-standards-compliant devices.
4329 * Otherwise we start with SET_ADDRESS and then try to read the
4330 * first 8 bytes of the device descriptor to get the ep0 maxpacket
4331 * value.
4332 */
4333 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Dan Williams48fc7db2013-12-05 17:07:27 -08004334 bool did_new_scheme = false;
4335
4336 if (use_new_scheme(udev, retry_counter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 struct usb_device_descriptor *buf;
4338 int r = 0;
4339
Dan Williams48fc7db2013-12-05 17:07:27 -08004340 did_new_scheme = true;
4341 retval = hub_enable_device(udev);
Oliver Neukum938569e2014-02-27 10:57:10 +01004342 if (retval < 0) {
4343 dev_err(&udev->dev,
4344 "hub failed to enable device, error %d\n",
4345 retval);
Dan Williams48fc7db2013-12-05 17:07:27 -08004346 goto fail;
Oliver Neukum938569e2014-02-27 10:57:10 +01004347 }
Dan Williams48fc7db2013-12-05 17:07:27 -08004348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349#define GET_DESCRIPTOR_BUFSIZE 64
4350 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
4351 if (!buf) {
4352 retval = -ENOMEM;
4353 continue;
4354 }
4355
Alan Sternb89ee192007-05-11 10:19:04 -04004356 /* Retry on all errors; some devices are flakey.
4357 * 255 is for WUSB devices, we actually need to use
4358 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 */
4360 for (j = 0; j < 3; ++j) {
4361 buf->bMaxPacketSize0 = 0;
4362 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
4363 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
4364 USB_DT_DEVICE << 8, 0,
4365 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02004366 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004368 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 if (buf->bDescriptorType ==
4370 USB_DT_DEVICE) {
4371 r = 0;
4372 break;
4373 }
4374 /* FALL THROUGH */
4375 default:
4376 if (r == 0)
4377 r = -EPROTO;
4378 break;
4379 }
4380 if (r == 0)
4381 break;
4382 }
4383 udev->descriptor.bMaxPacketSize0 =
4384 buf->bMaxPacketSize0;
4385 kfree(buf);
4386
Andiry Xu75d7cf72011-09-13 16:41:11 -07004387 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 if (retval < 0) /* error or disconnect */
4389 goto fail;
4390 if (oldspeed != udev->speed) {
4391 dev_dbg(&udev->dev,
4392 "device reset changed speed!\n");
4393 retval = -ENODEV;
4394 goto fail;
4395 }
4396 if (r) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004397 if (r != -ENODEV)
4398 dev_err(&udev->dev, "device descriptor read/64, error %d\n",
4399 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 retval = -EMSGSIZE;
4401 continue;
4402 }
4403#undef GET_DESCRIPTOR_BUFSIZE
4404 }
4405
Matthias Beyer469271f2013-10-10 23:41:27 +02004406 /*
4407 * If device is WUSB, we already assigned an
4408 * unauthorized address in the Connect Ack sequence;
4409 * authorization will assign the final address.
4410 */
Sarah Sharpc6515272009-04-27 19:57:26 -07004411 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004412 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
4413 retval = hub_set_address(udev, devnum);
4414 if (retval >= 0)
4415 break;
4416 msleep(200);
4417 }
4418 if (retval < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004419 if (retval != -ENODEV)
4420 dev_err(&udev->dev, "device not accepting address %d, error %d\n",
4421 devnum, retval);
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004422 goto fail;
4423 }
Sarah Sharpc6515272009-04-27 19:57:26 -07004424 if (udev->speed == USB_SPEED_SUPER) {
4425 devnum = udev->devnum;
4426 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05004427 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07004428 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05004429 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07004430 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004431
4432 /* cope with hardware quirkiness:
4433 * - let SET_ADDRESS settle, some device hardware wants it
4434 * - read ep0 maxpacket even for high and low speed,
4435 */
4436 msleep(10);
Dan Williams48fc7db2013-12-05 17:07:27 -08004437 /* use_new_scheme() checks the speed which may have
4438 * changed since the initial look so we cache the result
4439 * in did_new_scheme
4440 */
4441 if (did_new_scheme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 break;
Matthias Beyer469271f2013-10-10 23:41:27 +02004443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
4445 retval = usb_get_device_descriptor(udev, 8);
4446 if (retval < 8) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004447 if (retval != -ENODEV)
4448 dev_err(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004449 "device descriptor read/8, error %d\n",
4450 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 if (retval >= 0)
4452 retval = -EMSGSIZE;
4453 } else {
4454 retval = 0;
4455 break;
4456 }
4457 }
4458 if (retval)
4459 goto fail;
4460
Peter Chenb76baa82012-11-09 09:44:43 +08004461 if (hcd->phy && !hdev->parent)
Peter Chenac965112012-11-09 09:44:44 +08004462 usb_phy_notify_connect(hcd->phy, udev->speed);
Peter Chenb76baa82012-11-09 09:44:43 +08004463
Elric Fud8aec3d2012-03-26 21:16:02 +08004464 /*
4465 * Some superspeed devices have finished the link training process
4466 * and attached to a superspeed hub port, but the device descriptor
4467 * got from those devices show they aren't superspeed devices. Warm
4468 * reset the port attached by the devices can fix them.
4469 */
4470 if ((udev->speed == USB_SPEED_SUPER) &&
4471 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4472 dev_err(&udev->dev, "got a wrong device descriptor, "
4473 "warm reset device\n");
4474 hub_port_reset(hub, port1, udev,
4475 HUB_BH_RESET_TIME, true);
4476 retval = -EINVAL;
4477 goto fail;
4478 }
4479
Sarah Sharp6b403b02009-04-27 19:54:10 -07004480 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
4481 udev->speed == USB_SPEED_SUPER)
4482 i = 512;
4483 else
4484 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07004485 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04004486 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04004488 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 retval = -EMSGSIZE;
4490 goto fail;
4491 }
Alan Stern56626a72010-10-14 15:25:21 -04004492 if (udev->speed == USB_SPEED_FULL)
4493 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4494 else
4495 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004497 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4501 if (retval < (signed)sizeof(udev->descriptor)) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004502 if (retval != -ENODEV)
4503 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4504 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 if (retval >= 0)
4506 retval = -ENOMSG;
4507 goto fail;
4508 }
4509
Andiry Xu1ff4df52011-09-23 14:19:48 -07004510 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4511 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004512 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004513 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004514 usb_set_lpm_parameters(udev);
4515 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004516 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004517
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004519 /* notify HCD that we have a device connected and addressed */
4520 if (hcd->driver->update_device)
4521 hcd->driver->update_device(hcd, udev);
Mathias Nyman890dae82013-09-30 17:26:31 +03004522 hub_set_initial_usb2_lpm_policy(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004524 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004526 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004527 }
Todd E Brandt6fecd4f2014-05-19 10:55:32 -07004528 mutex_unlock(&hdev->bus->usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 return retval;
4530}
4531
4532static void
4533check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4534{
4535 struct usb_qualifier_descriptor *qual;
4536 int status;
4537
Johan Hovold2a159382014-08-25 17:51:26 +02004538 if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER)
4539 return;
4540
Christoph Lametere94b1762006-12-06 20:33:17 -08004541 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 if (qual == NULL)
4543 return;
4544
4545 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4546 qual, sizeof *qual);
4547 if (status == sizeof *qual) {
4548 dev_info(&udev->dev, "not running at top speed; "
4549 "connect to a high speed hub\n");
4550 /* hub LEDs are probably harder to miss than syslog */
4551 if (hub->has_indicators) {
4552 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08004553 queue_delayed_work(system_power_efficient_wq,
4554 &hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 }
4556 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004557 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558}
4559
4560static unsigned
4561hub_power_remaining (struct usb_hub *hub)
4562{
4563 struct usb_device *hdev = hub->hdev;
4564 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004565 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
Alan Stern55c52712005-11-23 12:03:12 -05004567 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 return 0;
4569
Alan Stern55c52712005-11-23 12:03:12 -05004570 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4571 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004572 struct usb_port *port_dev = hub->ports[port1 - 1];
4573 struct usb_device *udev = port_dev->child;
4574 unsigned unit_load;
4575 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
4577 if (!udev)
4578 continue;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004579 if (hub_is_superspeed(udev))
4580 unit_load = 150;
4581 else
4582 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004584 /*
4585 * Unconfigured devices may not use more than one unit load,
4586 * or 8mA for OTG ports
4587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 if (udev->actconfig)
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +01004589 delta = usb_get_max_power(udev, udev->actconfig);
Alan Stern55c52712005-11-23 12:03:12 -05004590 else if (port1 != udev->bus->otg_port || hdev->parent)
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004591 delta = unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 else
Alan Stern55c52712005-11-23 12:03:12 -05004593 delta = 8;
4594 if (delta > hub->mA_per_port)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004595 dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n",
4596 delta, hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 remaining -= delta;
4598 }
4599 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004600 dev_warn(hub->intfdev, "%dmA over power budget!\n",
Matthias Beyer469271f2013-10-10 23:41:27 +02004601 -remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 remaining = 0;
4603 }
4604 return remaining;
4605}
4606
Dan Williamsaf376a42014-05-20 18:09:15 -07004607static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
4608 u16 portchange)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 int status, i;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004611 unsigned unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 struct usb_device *hdev = hub->hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004614 struct usb_port *port_dev = hub->ports[port1 - 1];
Dan Williamsaf376a42014-05-20 18:09:15 -07004615 struct usb_device *udev = port_dev->child;
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004616 static int unreliable_port = -1;
Alan Stern8808f002008-04-28 11:06:55 -04004617
Alan Stern24618b02008-04-28 11:06:28 -04004618 /* Disconnect any existing devices under this port */
Peter Chenb76baa82012-11-09 09:44:43 +08004619 if (udev) {
4620 if (hcd->phy && !hdev->parent &&
4621 !(portstatus & USB_PORT_STAT_CONNECTION))
Peter Chenac965112012-11-09 09:44:44 +08004622 usb_phy_notify_disconnect(hcd->phy, udev->speed);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004623 usb_disconnect(&port_dev->child);
Peter Chenb76baa82012-11-09 09:44:43 +08004624 }
Alan Stern24618b02008-04-28 11:06:28 -04004625
Alan Stern253e0572009-10-27 15:20:13 -04004626 /* We can forget about a "removed" device when there's a physical
4627 * disconnect or the connect status changes.
4628 */
4629 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4630 (portchange & USB_PORT_STAT_C_CONNECTION))
4631 clear_bit(port1, hub->removed_bits);
4632
Alan Stern5257d972008-09-22 14:43:08 -04004633 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4634 USB_PORT_STAT_C_ENABLE)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004635 status = hub_port_debounce_be_stable(hub, port1);
Alan Stern5257d972008-09-22 14:43:08 -04004636 if (status < 0) {
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004637 if (status != -ENODEV &&
4638 port1 != unreliable_port &&
4639 printk_ratelimit())
Takashi Iwaidd5f5002014-08-19 17:37:55 +02004640 dev_err(&port_dev->dev, "connect-debounce failed\n");
Alan Stern5257d972008-09-22 14:43:08 -04004641 portstatus &= ~USB_PORT_STAT_CONNECTION;
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004642 unreliable_port = port1;
Alan Stern5257d972008-09-22 14:43:08 -04004643 } else {
4644 portstatus = status;
4645 }
4646 }
4647
Alan Stern253e0572009-10-27 15:20:13 -04004648 /* Return now if debouncing failed or nothing is connected or
4649 * the device was "removed".
4650 */
4651 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4652 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
4654 /* maybe switch power back on (e.g. root hub was reset) */
Dan Williams9262c192014-05-20 18:08:12 -07004655 if (hub_is_port_power_switchable(hub)
Andiry Xu0ed9a572011-04-27 18:07:43 +08004656 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004658
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 if (portstatus & USB_PORT_STAT_ENABLE)
Matthias Beyer469271f2013-10-10 23:41:27 +02004660 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 return;
4662 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004663 if (hub_is_superspeed(hub->hdev))
4664 unit_load = 150;
4665 else
4666 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Alan Sterne9e88fb2013-03-27 16:14:01 -04004668 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
4671 /* reallocate for each attempt, since references
4672 * to the previous one can escape in various ways
4673 */
4674 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4675 if (!udev) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004676 dev_err(&port_dev->dev,
4677 "couldn't allocate usb_device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 goto done;
4679 }
4680
4681 usb_set_device_state(udev, USB_STATE_POWERED);
Matthias Beyer469271f2013-10-10 23:41:27 +02004682 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004683 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004684 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004685
Sarah Sharp131dec32010-12-06 21:00:19 -08004686 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4687 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004688 udev->speed = USB_SPEED_SUPER;
4689 else
4690 udev->speed = USB_SPEED_UNKNOWN;
4691
Alan Stern3b29b682011-02-22 09:53:41 -05004692 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004693 if (udev->devnum <= 0) {
4694 status = -ENOTCONN; /* Don't retry */
4695 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004696 }
4697
Sarah Sharpe7b77172009-04-27 19:54:26 -07004698 /* reset (non-USB 3.0 devices) and get descriptor */
Dan Williams5c79a1e2014-05-20 18:09:26 -07004699 usb_lock_port(port_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 status = hub_port_init(hub, udev, port1, i);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004701 usb_unlock_port(port_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 if (status < 0)
4703 goto loop;
4704
Phil Dibowitz93362a82010-07-22 00:05:01 +02004705 usb_detect_quirks(udev);
4706 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4707 msleep(1000);
4708
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 /* consecutive bus-powered hubs aren't reliable; they can
4710 * violate the voltage drop budget. if the new child has
4711 * a "powered" LED, users should notice we didn't enable it
4712 * (without reading syslog), even without per-port LEDs
4713 * on the parent.
4714 */
4715 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004716 && udev->bus_mA <= unit_load) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 u16 devstat;
4718
4719 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4720 &devstat);
Alan Stern15b73362013-07-30 15:35:40 -04004721 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 dev_dbg(&udev->dev, "get status %d ?\n", status);
4723 goto loop_disable;
4724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4726 dev_err(&udev->dev,
4727 "can't connect bus-powered hub "
4728 "to this port\n");
4729 if (hub->has_indicators) {
4730 hub->indicator[port1-1] =
4731 INDICATOR_AMBER_BLINK;
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08004732 queue_delayed_work(
4733 system_power_efficient_wq,
4734 &hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 }
4736 status = -ENOTCONN; /* Don't retry */
4737 goto loop_disable;
4738 }
4739 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004740
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 /* check for devices running slower than they could */
4742 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4743 && udev->speed == USB_SPEED_FULL
4744 && highspeed_hubs != 0)
4745 check_highspeed (hub, udev, port1);
4746
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004747 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 * udev becomes globally accessible, although presumably
4749 * no one will look at it until hdev is unlocked.
4750 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 status = 0;
4752
Dan Williamsd8521af2014-05-20 18:08:28 -07004753 mutex_lock(&usb_port_peer_mutex);
4754
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 /* We mustn't add new devices if the parent hub has
4756 * been disconnected; we would race with the
4757 * recursively_mark_NOTATTACHED() routine.
4758 */
4759 spin_lock_irq(&device_state_lock);
4760 if (hdev->state == USB_STATE_NOTATTACHED)
4761 status = -ENOTCONN;
4762 else
Dan Williamsd99f6b42014-05-20 18:08:17 -07004763 port_dev->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 spin_unlock_irq(&device_state_lock);
Dan Williamsd8521af2014-05-20 18:08:28 -07004765 mutex_unlock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
4767 /* Run it through the hoops (find a driver, etc) */
4768 if (!status) {
4769 status = usb_new_device(udev);
4770 if (status) {
Dan Williamsd8521af2014-05-20 18:08:28 -07004771 mutex_lock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 spin_lock_irq(&device_state_lock);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004773 port_dev->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 spin_unlock_irq(&device_state_lock);
Dan Williamsd8521af2014-05-20 18:08:28 -07004775 mutex_unlock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 }
4777 }
4778
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 if (status)
4780 goto loop_disable;
4781
4782 status = hub_power_remaining(hub);
4783 if (status)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004784 dev_dbg(hub->intfdev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
4786 return;
4787
4788loop_disable:
4789 hub_port_disable(hub, port1, 1);
4790loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004791 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004792 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004793 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004795 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 break;
4797 }
Alan Stern3a311552008-05-20 16:58:29 -04004798 if (hub->hdev->parent ||
4799 !hcd->driver->port_handed_over ||
Alan Sterne9e88fb2013-03-27 16:14:01 -04004800 !(hcd->driver->port_handed_over)(hcd, port1)) {
4801 if (status != -ENOTCONN && status != -ENODEV)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004802 dev_err(&port_dev->dev,
4803 "unable to enumerate USB device\n");
Alan Sterne9e88fb2013-03-27 16:14:01 -04004804 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004805
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806done:
4807 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304808 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4809 hcd->driver->relinquish_port(hcd, port1);
Dan Williamsaf376a42014-05-20 18:09:15 -07004810
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811}
4812
Dan Williamsaf376a42014-05-20 18:09:15 -07004813/* Handle physical or logical connection change events.
4814 * This routine is called when:
4815 * a port connection-change occurs;
4816 * a port enable-change occurs (often caused by EMI);
4817 * usb_reset_and_verify_device() encounters changed descriptors (as from
4818 * a firmware download)
4819 * caller already locked the hub
4820 */
4821static void hub_port_connect_change(struct usb_hub *hub, int port1,
4822 u16 portstatus, u16 portchange)
Dan Williams5c79a1e2014-05-20 18:09:26 -07004823 __must_hold(&port_dev->status_lock)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004824{
Dan Williamsaf376a42014-05-20 18:09:15 -07004825 struct usb_port *port_dev = hub->ports[port1 - 1];
4826 struct usb_device *udev = port_dev->child;
4827 int status = -ENODEV;
Sarah Sharp714b07b2012-01-24 13:53:18 -08004828
Dan Williamsaf376a42014-05-20 18:09:15 -07004829 dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus,
4830 portchange, portspeed(hub, portstatus));
4831
4832 if (hub->has_indicators) {
4833 set_port_led(hub, port1, HUB_LED_AUTO);
4834 hub->indicator[port1-1] = INDICATOR_AUTO;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004835 }
4836
Dan Williamsaf376a42014-05-20 18:09:15 -07004837#ifdef CONFIG_USB_OTG
4838 /* during HNP, don't repeat the debounce */
4839 if (hub->hdev->bus->is_b_host)
4840 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4841 USB_PORT_STAT_C_ENABLE);
4842#endif
Sarah Sharp714b07b2012-01-24 13:53:18 -08004843
Dan Williamsaf376a42014-05-20 18:09:15 -07004844 /* Try to resuscitate an existing device */
4845 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4846 udev->state != USB_STATE_NOTATTACHED) {
4847 if (portstatus & USB_PORT_STAT_ENABLE) {
4848 status = 0; /* Nothing to do */
4849#ifdef CONFIG_PM_RUNTIME
4850 } else if (udev->state == USB_STATE_SUSPENDED &&
4851 udev->persist_enabled) {
4852 /* For a suspended device, treat this as a
4853 * remote wakeup event.
4854 */
Dan Williams5c79a1e2014-05-20 18:09:26 -07004855 usb_unlock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004856 status = usb_remote_wakeup(udev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004857 usb_lock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004858#endif
4859 } else {
4860 /* Don't resuscitate */;
4861 }
Sarah Sharp714b07b2012-01-24 13:53:18 -08004862 }
Dan Williamsaf376a42014-05-20 18:09:15 -07004863 clear_bit(port1, hub->change_bits);
4864
Dan Williams5c79a1e2014-05-20 18:09:26 -07004865 /* successfully revalidated the connection */
Dan Williamsaf376a42014-05-20 18:09:15 -07004866 if (status == 0)
4867 return;
4868
Dan Williams5c79a1e2014-05-20 18:09:26 -07004869 usb_unlock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004870 hub_port_connect(hub, port1, portstatus, portchange);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004871 usb_lock_port(port_dev);
Sarah Sharp714b07b2012-01-24 13:53:18 -08004872}
4873
Dan Williamsaf376a42014-05-20 18:09:15 -07004874static void port_event(struct usb_hub *hub, int port1)
Dan Williams5c79a1e2014-05-20 18:09:26 -07004875 __must_hold(&port_dev->status_lock)
Dan Williamsaf376a42014-05-20 18:09:15 -07004876{
4877 int connect_change, reset_device = 0;
4878 struct usb_port *port_dev = hub->ports[port1 - 1];
4879 struct usb_device *udev = port_dev->child;
4880 struct usb_device *hdev = hub->hdev;
4881 u16 portstatus, portchange;
4882
4883 connect_change = test_bit(port1, hub->change_bits);
4884 clear_bit(port1, hub->event_bits);
4885 clear_bit(port1, hub->wakeup_bits);
4886
4887 if (hub_port_status(hub, port1, &portstatus, &portchange) < 0)
4888 return;
4889
4890 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4891 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
4892 connect_change = 1;
4893 }
4894
4895 if (portchange & USB_PORT_STAT_C_ENABLE) {
4896 if (!connect_change)
4897 dev_dbg(&port_dev->dev, "enable change, status %08x\n",
4898 portstatus);
4899 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
4900
4901 /*
4902 * EM interference sometimes causes badly shielded USB devices
4903 * to be shutdown by the hub, this hack enables them again.
4904 * Works at least with mouse driver.
4905 */
4906 if (!(portstatus & USB_PORT_STAT_ENABLE)
4907 && !connect_change && udev) {
4908 dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n");
4909 connect_change = 1;
4910 }
4911 }
4912
4913 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
4914 u16 status = 0, unused;
4915
4916 dev_dbg(&port_dev->dev, "over-current change\n");
4917 usb_clear_port_feature(hdev, port1,
4918 USB_PORT_FEAT_C_OVER_CURRENT);
4919 msleep(100); /* Cool down */
4920 hub_power_on(hub, true);
4921 hub_port_status(hub, port1, &status, &unused);
4922 if (status & USB_PORT_STAT_OVERCURRENT)
4923 dev_err(&port_dev->dev, "over-current condition\n");
4924 }
4925
4926 if (portchange & USB_PORT_STAT_C_RESET) {
4927 dev_dbg(&port_dev->dev, "reset change\n");
4928 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET);
4929 }
4930 if ((portchange & USB_PORT_STAT_C_BH_RESET)
4931 && hub_is_superspeed(hdev)) {
4932 dev_dbg(&port_dev->dev, "warm reset change\n");
4933 usb_clear_port_feature(hdev, port1,
4934 USB_PORT_FEAT_C_BH_PORT_RESET);
4935 }
4936 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4937 dev_dbg(&port_dev->dev, "link state change\n");
4938 usb_clear_port_feature(hdev, port1,
4939 USB_PORT_FEAT_C_PORT_LINK_STATE);
4940 }
4941 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4942 dev_warn(&port_dev->dev, "config error\n");
4943 usb_clear_port_feature(hdev, port1,
4944 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4945 }
4946
Dan Williams097a1552014-05-20 18:09:20 -07004947 /* skip port actions that require the port to be powered on */
4948 if (!pm_runtime_active(&port_dev->dev))
4949 return;
4950
Dan Williamsaf376a42014-05-20 18:09:15 -07004951 if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange))
4952 connect_change = 1;
4953
4954 /*
4955 * Warm reset a USB3 protocol port if it's in
4956 * SS.Inactive state.
4957 */
Dan Williams3cd12f92014-05-29 12:58:46 -07004958 if (hub_port_warm_reset_required(hub, port1, portstatus)) {
Dan Williamsaf376a42014-05-20 18:09:15 -07004959 dev_dbg(&port_dev->dev, "do warm reset\n");
4960 if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION)
4961 || udev->state == USB_STATE_NOTATTACHED) {
4962 if (hub_port_reset(hub, port1, NULL,
4963 HUB_BH_RESET_TIME, true) < 0)
4964 hub_port_disable(hub, port1, 1);
4965 } else
4966 reset_device = 1;
4967 }
4968
4969 /*
4970 * On disconnect USB3 protocol ports transit from U0 to
4971 * SS.Inactive to Rx.Detect. If this happens a warm-
4972 * reset is not needed, but a (re)connect may happen
4973 * before khubd runs and sees the disconnect, and the
4974 * device may be an unknown state.
4975 *
4976 * If the port went through SS.Inactive without khubd
4977 * seeing it the C_LINK_STATE change flag will be set,
4978 * and we reset the dev to put it in a known state.
4979 */
4980 if (reset_device || (udev && hub_is_superspeed(hub->hdev)
4981 && (portchange & USB_PORT_STAT_C_LINK_STATE)
4982 && (portstatus & USB_PORT_STAT_CONNECTION))) {
Dan Williams5c79a1e2014-05-20 18:09:26 -07004983 usb_unlock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004984 usb_lock_device(udev);
4985 usb_reset_device(udev);
4986 usb_unlock_device(udev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004987 usb_lock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004988 connect_change = 0;
4989 }
4990
4991 if (connect_change)
4992 hub_port_connect_change(hub, port1, portstatus, portchange);
4993}
4994
4995
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996static void hub_events(void)
4997{
4998 struct list_head *tmp;
4999 struct usb_device *hdev;
5000 struct usb_interface *intf;
5001 struct usb_hub *hub;
5002 struct device *hub_dev;
5003 u16 hubstatus;
5004 u16 hubchange;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
5007 /*
5008 * We restart the list every time to avoid a deadlock with
5009 * deleting hubs downstream from this one. This should be
5010 * safe since we delete the hub from the event list.
5011 * Not the most efficient, but avoids deadlocks.
5012 */
5013 while (1) {
5014
5015 /* Grab the first entry at the beginning of the list */
5016 spin_lock_irq(&hub_event_lock);
5017 if (list_empty(&hub_event_list)) {
5018 spin_unlock_irq(&hub_event_lock);
5019 break;
5020 }
5021
5022 tmp = hub_event_list.next;
5023 list_del_init(tmp);
5024
5025 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04005026 kref_get(&hub->kref);
Joe Lawrencec605f3c2014-09-10 15:07:50 -04005027 hdev = hub->hdev;
5028 usb_get_dev(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04005029 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
Alan Sterne8054852007-05-04 11:55:11 -04005031 hub_dev = hub->intfdev;
5032 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05005033 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +02005034 hdev->state, hdev->maxchild,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 /* NOTE: expects max 15 ports... */
5036 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05005037 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 /* Lock the device, then check to see if we were
5040 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04005041 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04005042 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05005043 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
5045 /* If the hub has died, clean up after it */
5046 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04005047 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04005048 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 goto loop;
5050 }
5051
Alan Stern40f122f2006-11-09 14:44:33 -05005052 /* Autoresume */
5053 ret = usb_autopm_get_interface(intf);
5054 if (ret) {
5055 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05005057 }
5058
5059 /* If this is an inactive hub, do nothing */
5060 if (hub->quiescing)
5061 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
5063 if (hub->error) {
5064 dev_dbg (hub_dev, "resetting for error %d\n",
5065 hub->error);
5066
Ming Lei742120c2008-06-18 22:00:29 +08005067 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 if (ret) {
5069 dev_dbg (hub_dev,
5070 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05005071 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 }
5073
5074 hub->nerrors = 0;
5075 hub->error = 0;
5076 }
5077
5078 /* deal with port status changes */
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +02005079 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williams097a1552014-05-20 18:09:20 -07005080 struct usb_port *port_dev = hub->ports[i - 1];
Hans de Goedea82b76f2013-11-08 13:41:05 +01005081
Dan Williams5c79a1e2014-05-20 18:09:26 -07005082 if (test_bit(i, hub->event_bits)
5083 || test_bit(i, hub->change_bits)
5084 || test_bit(i, hub->wakeup_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 /*
Dan Williams097a1552014-05-20 18:09:20 -07005086 * The get_noresume and barrier ensure that if
5087 * the port was in the process of resuming, we
5088 * flush that work and keep the port active for
5089 * the duration of the port_event(). However,
5090 * if the port is runtime pm suspended
5091 * (powered-off), we leave it in that state, run
5092 * an abbreviated port_event(), and move on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 */
Dan Williams097a1552014-05-20 18:09:20 -07005094 pm_runtime_get_noresume(&port_dev->dev);
5095 pm_runtime_barrier(&port_dev->dev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07005096 usb_lock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07005097 port_event(hub, i);
Dan Williams5c79a1e2014-05-20 18:09:26 -07005098 usb_unlock_port(port_dev);
Dan Williams097a1552014-05-20 18:09:20 -07005099 pm_runtime_put_sync(&port_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 }
Dan Williamsaf376a42014-05-20 18:09:15 -07005101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102
5103 /* deal with hub status changes */
5104 if (test_and_clear_bit(0, hub->event_bits) == 0)
5105 ; /* do nothing */
5106 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
5107 dev_err (hub_dev, "get_hub_status failed\n");
5108 else {
5109 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
5110 dev_dbg (hub_dev, "power change\n");
5111 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05005112 if (hubstatus & HUB_STATUS_LOCAL_POWER)
5113 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05005114 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08005115 else
5116 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 }
5118 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01005119 u16 status = 0;
5120 u16 unused;
5121
5122 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01005124 msleep(500); /* Cool down */
Matthias Beyer469271f2013-10-10 23:41:27 +02005125 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01005126 hub_hub_status(hub, &status, &unused);
5127 if (status & HUB_STATUS_OVERCURRENT)
5128 dev_err(hub_dev, "over-current "
5129 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 }
5131 }
5132
Alan Stern8e4ceb32009-12-07 13:01:37 -05005133 loop_autopm:
5134 /* Balance the usb_autopm_get_interface() above */
5135 usb_autopm_put_interface_no_suspend(intf);
5136 loop:
5137 /* Balance the usb_autopm_get_interface_no_resume() in
5138 * kick_khubd() and allow autosuspend.
5139 */
5140 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05005141 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 usb_unlock_device(hdev);
Joe Lawrencec605f3c2014-09-10 15:07:50 -04005143 usb_put_dev(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04005144 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145
Matthias Beyer469271f2013-10-10 23:41:27 +02005146 } /* end while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147}
5148
5149static int hub_thread(void *__unused)
5150{
Rahul Bedarkar025d4432014-01-04 11:24:41 +05305151 /* khubd needs to be freezable to avoid interfering with USB-PERSIST
Alan Stern3bb1af52008-03-03 15:15:36 -05005152 * port handover. Otherwise it might see that a full-speed device
5153 * was gone before the EHCI controller had handed its port over to
5154 * the companion full-speed controller.
5155 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07005156 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05005157
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 do {
5159 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07005160 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07005161 !list_empty(&hub_event_list) ||
5162 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07005163 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07005165 pr_debug("%s: khubd exiting\n", usbcore_name);
5166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167}
5168
Németh Márton1e927d92010-01-10 15:34:53 +01005169static const struct usb_device_id hub_id_table[] = {
Ming Leie6f30de2012-10-24 11:59:24 +08005170 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
Matthias Beyer469271f2013-10-10 23:41:27 +02005171 | USB_DEVICE_ID_MATCH_INT_CLASS,
Ming Leie6f30de2012-10-24 11:59:24 +08005172 .idVendor = USB_VENDOR_GENESYS_LOGIC,
5173 .bInterfaceClass = USB_CLASS_HUB,
5174 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
5176 .bDeviceClass = USB_CLASS_HUB},
5177 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
5178 .bInterfaceClass = USB_CLASS_HUB},
5179 { } /* Terminating entry */
5180};
5181
5182MODULE_DEVICE_TABLE (usb, hub_id_table);
5183
5184static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 .name = "hub",
5186 .probe = hub_probe,
5187 .disconnect = hub_disconnect,
5188 .suspend = hub_suspend,
5189 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04005190 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04005191 .pre_reset = hub_pre_reset,
5192 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02005193 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05005195 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196};
5197
5198int usb_hub_init(void)
5199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 if (usb_register(&hub_driver) < 0) {
5201 printk(KERN_ERR "%s: can't register hub driver\n",
5202 usbcore_name);
5203 return -1;
5204 }
5205
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07005206 khubd_task = kthread_run(hub_thread, NULL, "khubd");
5207 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209
5210 /* Fall through if kernel_thread failed */
5211 usb_deregister(&hub_driver);
5212 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
5213
5214 return -1;
5215}
5216
5217void usb_hub_cleanup(void)
5218{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07005219 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
5221 /*
5222 * Hub resources are freed for us by usb_deregister. It calls
5223 * usb_driver_purge on every device which in turn calls that
5224 * devices disconnect function if it is using this driver.
5225 * The hub_disconnect function takes care of releasing the
5226 * individual hub resources. -greg
5227 */
5228 usb_deregister(&hub_driver);
5229} /* usb_hub_cleanup() */
5230
Alan Sterneb764c42008-03-03 15:16:04 -05005231static int descriptors_changed(struct usb_device *udev,
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005232 struct usb_device_descriptor *old_device_descriptor,
5233 struct usb_host_bos *old_bos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234{
Alan Sterneb764c42008-03-03 15:16:04 -05005235 int changed = 0;
5236 unsigned index;
5237 unsigned serial_len = 0;
5238 unsigned len;
5239 unsigned old_length;
5240 int length;
5241 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242
Alan Sterneb764c42008-03-03 15:16:04 -05005243 if (memcmp(&udev->descriptor, old_device_descriptor,
5244 sizeof(*old_device_descriptor)) != 0)
5245 return 1;
5246
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005247 if ((old_bos && !udev->bos) || (!old_bos && udev->bos))
5248 return 1;
5249 if (udev->bos) {
Xenia Ragiadakoub9a10482013-08-31 04:40:49 +03005250 len = le16_to_cpu(udev->bos->desc->wTotalLength);
5251 if (len != le16_to_cpu(old_bos->desc->wTotalLength))
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005252 return 1;
Xenia Ragiadakoub9a10482013-08-31 04:40:49 +03005253 if (memcmp(udev->bos->desc, old_bos->desc, len))
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005254 return 1;
5255 }
5256
Alan Sterneb764c42008-03-03 15:16:04 -05005257 /* Since the idVendor, idProduct, and bcdDevice values in the
5258 * device descriptor haven't changed, we will assume the
5259 * Manufacturer and Product strings haven't changed either.
5260 * But the SerialNumber string could be different (e.g., a
5261 * different flash card of the same brand).
5262 */
5263 if (udev->serial)
5264 serial_len = strlen(udev->serial) + 1;
5265
5266 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05005268 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
5269 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 }
Alan Sterneb764c42008-03-03 15:16:04 -05005271
Oliver Neukum0cc1a512008-01-10 10:31:48 +01005272 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 if (buf == NULL) {
5274 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
5275 /* assume the worst */
5276 return 1;
5277 }
5278 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05005279 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
5281 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05005282 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 dev_dbg(&udev->dev, "config index %d, error %d\n",
5284 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05005285 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 break;
5287 }
5288 if (memcmp (buf, udev->rawdescriptors[index], old_length)
5289 != 0) {
5290 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05005291 index,
5292 ((struct usb_config_descriptor *) buf)->
5293 bConfigurationValue);
5294 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 break;
5296 }
5297 }
Alan Sterneb764c42008-03-03 15:16:04 -05005298
5299 if (!changed && serial_len) {
5300 length = usb_string(udev, udev->descriptor.iSerialNumber,
5301 buf, serial_len);
5302 if (length + 1 != serial_len) {
5303 dev_dbg(&udev->dev, "serial string error %d\n",
5304 length);
5305 changed = 1;
5306 } else if (memcmp(buf, udev->serial, length) != 0) {
5307 dev_dbg(&udev->dev, "serial string changed\n");
5308 changed = 1;
5309 }
5310 }
5311
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05005313 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314}
5315
5316/**
Ming Lei742120c2008-06-18 22:00:29 +08005317 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5319 *
Alan Stern79efa092006-06-01 13:33:42 -04005320 * WARNING - don't use this routine to reset a composite device
5321 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08005322 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 *
5324 * Do a port reset, reassign the device's address, and establish its
5325 * former operating configuration. If the reset fails, or the device's
5326 * descriptors change from their values before the reset, or the original
5327 * configuration and altsettings cannot be restored, a flag will be set
5328 * telling khubd to pretend the device has been disconnected and then
5329 * re-connected. All drivers will be unbound, and the device will be
5330 * re-enumerated and probed all over again.
5331 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005332 * Return: 0 if the reset succeeded, -ENODEV if the device has been
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 * flagged for logical disconnection, or some other negative error code
5334 * if the reset wasn't even attempted.
5335 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005336 * Note:
Dan Williams5c79a1e2014-05-20 18:09:26 -07005337 * The caller must own the device lock and the port lock, the latter is
5338 * taken by usb_reset_device(). For example, it's safe to use
5339 * usb_reset_device() from a driver probe() routine after downloading
5340 * new firmware. For calls that might not occur during probe(), drivers
5341 * should lock the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04005342 *
5343 * Locking exception: This routine may also be called from within an
5344 * autoresume handler. Such usage won't conflict with other tasks
5345 * holding the device lock because these tasks should always call
Dan Williams5c79a1e2014-05-20 18:09:26 -07005346 * usb_autopm_resume_device(), thereby preventing any unwanted
5347 * autoresume. The autoresume handler is expected to have already
5348 * acquired the port lock before calling this routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 */
Ming Lei742120c2008-06-18 22:00:29 +08005350static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351{
5352 struct usb_device *parent_hdev = udev->parent;
5353 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005354 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 struct usb_device_descriptor descriptor = udev->descriptor;
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005356 struct usb_host_bos *bos;
Hans de Goede7a7b5622013-11-08 16:37:26 +01005357 int i, j, ret = 0;
Alan Stern12c3da32005-11-23 12:09:52 -05005358 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
5360 if (udev->state == USB_STATE_NOTATTACHED ||
5361 udev->state == USB_STATE_SUSPENDED) {
5362 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5363 udev->state);
5364 return -EINVAL;
5365 }
5366
Dan Williams5c79a1e2014-05-20 18:09:26 -07005367 if (!parent_hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 return -EISDIR;
Dan Williams5c79a1e2014-05-20 18:09:26 -07005369
Lan Tianyuad493e52013-01-23 04:26:30 +08005370 parent_hub = usb_hub_to_struct_hub(parent_hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371
Sarah Sharpdcc01c02013-09-30 17:26:29 +03005372 /* Disable USB2 hardware LPM.
5373 * It will be re-enabled by the enumeration process.
5374 */
5375 if (udev->usb2_hw_lpm_enabled == 1)
5376 usb_set_usb2_hardware_lpm(udev, 0);
5377
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005378 bos = udev->bos;
5379 udev->bos = NULL;
5380
Sarah Sharpf74631e2012-06-25 12:08:08 -07005381 /* Disable LPM and LTM while we reset the device and reinstall the alt
5382 * settings. Device-initiated LPM settings, and system exit latency
5383 * settings are cleared when the device is reset, so we have to set
5384 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005385 */
5386 ret = usb_unlocked_disable_lpm(udev);
5387 if (ret) {
5388 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
5389 goto re_enumerate;
5390 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07005391 ret = usb_disable_ltm(udev);
5392 if (ret) {
5393 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
5394 __func__);
5395 goto re_enumerate;
5396 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
5399
5400 /* ep0 maxpacket size may change; let the HCD know about it.
5401 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07005402 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04005404 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 break;
5406 }
Alan Sternd5cbad42006-08-11 16:52:39 -04005407
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 if (ret < 0)
5409 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005410
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 /* Device might have changed firmware (DFU or similar) */
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005412 if (descriptors_changed(udev, &descriptor, bos)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 dev_info(&udev->dev, "device firmware changed\n");
5414 udev->descriptor = descriptor; /* for disconnect() calls */
5415 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005416 }
Alan Stern4fe03872009-02-26 10:21:02 -05005417
5418 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 if (!udev->actconfig)
5420 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005421
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005422 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005423 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
5424 if (ret < 0) {
5425 dev_warn(&udev->dev,
5426 "Busted HC? Not enough HCD resources for "
5427 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005428 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005429 goto re_enumerate;
5430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
5432 USB_REQ_SET_CONFIGURATION, 0,
5433 udev->actconfig->desc.bConfigurationValue, 0,
5434 NULL, 0, USB_CTRL_SET_TIMEOUT);
5435 if (ret < 0) {
5436 dev_err(&udev->dev,
5437 "can't restore configuration #%d (error=%d)\n",
5438 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005439 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005441 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005442 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 usb_set_device_state(udev, USB_STATE_CONFIGURED);
5444
Alan Stern4fe03872009-02-26 10:21:02 -05005445 /* Put interfaces back into the same altsettings as before.
5446 * Don't bother to send the Set-Interface request for interfaces
5447 * that were already in altsetting 0; besides being unnecessary,
5448 * many devices can't handle it. Instead just reset the host-side
5449 * endpoint state.
5450 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005452 struct usb_host_config *config = udev->actconfig;
5453 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 struct usb_interface_descriptor *desc;
5455
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05005457 if (desc->bAlternateSetting == 0) {
5458 usb_disable_interface(udev, intf, true);
5459 usb_enable_interface(udev, intf, true);
5460 ret = 0;
5461 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08005462 /* Let the bandwidth allocation function know that this
5463 * device has been reset, and it will have to use
5464 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005465 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08005466 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05005467 ret = usb_set_interface(udev, desc->bInterfaceNumber,
5468 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08005469 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05005470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 if (ret < 0) {
5472 dev_err(&udev->dev, "failed to restore interface %d "
5473 "altsetting %d (error=%d)\n",
5474 desc->bInterfaceNumber,
5475 desc->bAlternateSetting,
5476 ret);
5477 goto re_enumerate;
5478 }
Hans de Goede7a7b5622013-11-08 16:37:26 +01005479 /* Resetting also frees any allocated streams */
5480 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++)
5481 intf->cur_altsetting->endpoint[j].streams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 }
5483
5484done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07005485 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Sarah Sharpde68bab2013-09-30 17:26:28 +03005486 usb_set_usb2_hardware_lpm(udev, 1);
Alan Stern78d9a482008-06-23 16:00:40 -04005487 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07005488 usb_enable_ltm(udev);
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005489 usb_release_bos_descriptor(udev);
5490 udev->bos = bos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 return 0;
Matthias Beyer469271f2013-10-10 23:41:27 +02005492
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005494 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 hub_port_logical_disconnect(parent_hub, port1);
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005496 usb_release_bos_descriptor(udev);
5497 udev->bos = bos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499}
5500
5501/**
5502 * usb_reset_device - warn interface drivers and perform a USB port reset
5503 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5504 *
Alan Stern79efa092006-06-01 13:33:42 -04005505 * Warns all drivers bound to registered interfaces (using their pre_reset
5506 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08005507 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04005508 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005509 * Return: The same as for usb_reset_and_verify_device().
Alan Stern79efa092006-06-01 13:33:42 -04005510 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005511 * Note:
Alan Stern79efa092006-06-01 13:33:42 -04005512 * The caller must own the device lock. For example, it's safe to use
5513 * this from a driver probe() routine after downloading new firmware.
5514 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08005515 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04005516 *
5517 * If an interface is currently being probed or disconnected, we assume
5518 * its driver knows how to handle resets. For all other interfaces,
5519 * if the driver doesn't have pre_reset and post_reset methods then
5520 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04005521 */
Ming Lei742120c2008-06-18 22:00:29 +08005522int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04005523{
5524 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05005525 int i;
Ming Lei4d769de2013-02-22 16:34:22 -08005526 unsigned int noio_flag;
Dan Williams5c79a1e2014-05-20 18:09:26 -07005527 struct usb_port *port_dev;
Alan Stern79efa092006-06-01 13:33:42 -04005528 struct usb_host_config *config = udev->actconfig;
Dan Williams5c79a1e2014-05-20 18:09:26 -07005529 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
Alan Stern79efa092006-06-01 13:33:42 -04005530
5531 if (udev->state == USB_STATE_NOTATTACHED ||
5532 udev->state == USB_STATE_SUSPENDED) {
5533 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5534 udev->state);
5535 return -EINVAL;
5536 }
5537
Dan Williams5c79a1e2014-05-20 18:09:26 -07005538 if (!udev->parent) {
5539 /* this requires hcd-specific logic; see ohci_restart() */
5540 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
5541 return -EISDIR;
5542 }
5543
5544 port_dev = hub->ports[udev->portnum - 1];
5545
Ming Lei4d769de2013-02-22 16:34:22 -08005546 /*
5547 * Don't allocate memory with GFP_KERNEL in current
5548 * context to avoid possible deadlock if usb mass
5549 * storage interface or usbnet interface(iSCSI case)
5550 * is included in current configuration. The easist
5551 * approach is to do it for every device reset,
5552 * because the device 'memalloc_noio' flag may have
5553 * not been set before reseting the usb device.
5554 */
5555 noio_flag = memalloc_noio_save();
5556
Alan Stern645daaa2006-08-30 15:47:02 -04005557 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05005558 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04005559
Alan Stern79efa092006-06-01 13:33:42 -04005560 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005561 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005562 struct usb_interface *cintf = config->interface[i];
5563 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005564 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05005565
5566 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005567 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04005568 if (drv->pre_reset && drv->post_reset)
5569 unbind = (drv->pre_reset)(cintf);
5570 else if (cintf->condition ==
5571 USB_INTERFACE_BOUND)
5572 unbind = 1;
5573 if (unbind)
5574 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005575 }
5576 }
5577 }
5578
Dan Williams5c79a1e2014-05-20 18:09:26 -07005579 usb_lock_port(port_dev);
Ming Lei742120c2008-06-18 22:00:29 +08005580 ret = usb_reset_and_verify_device(udev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07005581 usb_unlock_port(port_dev);
Alan Stern79efa092006-06-01 13:33:42 -04005582
5583 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005584 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005585 struct usb_interface *cintf = config->interface[i];
5586 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005587 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05005588
Alan Stern78d9a482008-06-23 16:00:40 -04005589 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005590 drv = to_usb_driver(cintf->dev.driver);
5591 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04005592 rebind = (drv->post_reset)(cintf);
5593 else if (cintf->condition ==
5594 USB_INTERFACE_BOUND)
5595 rebind = 1;
Alan Stern6aec0442014-03-12 11:30:38 -04005596 if (rebind)
5597 cintf->needs_binding = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005598 }
Alan Stern79efa092006-06-01 13:33:42 -04005599 }
Alan Stern6aec0442014-03-12 11:30:38 -04005600 usb_unbind_and_rebind_marked_interfaces(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005601 }
5602
Alan Stern94fcda12006-11-20 11:38:46 -05005603 usb_autosuspend_device(udev);
Ming Lei4d769de2013-02-22 16:34:22 -08005604 memalloc_noio_restore(noio_flag);
Alan Stern79efa092006-06-01 13:33:42 -04005605 return ret;
5606}
Ming Lei742120c2008-06-18 22:00:29 +08005607EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005608
5609
5610/**
5611 * usb_queue_reset_device - Reset a USB device from an atomic context
5612 * @iface: USB interface belonging to the device to reset
5613 *
5614 * This function can be used to reset a USB device from an atomic
5615 * context, where usb_reset_device() won't work (as it blocks).
5616 *
5617 * Doing a reset via this method is functionally equivalent to calling
5618 * usb_reset_device(), except for the fact that it is delayed to a
5619 * workqueue. This means that any drivers bound to other interfaces
5620 * might be unbound, as well as users from usbfs in user space.
5621 *
5622 * Corner cases:
5623 *
5624 * - Scheduling two resets at the same time from two different drivers
5625 * attached to two different interfaces of the same device is
5626 * possible; depending on how the driver attached to each interface
5627 * handles ->pre_reset(), the second reset might happen or not.
5628 *
5629 * - If a driver is unbound and it had a pending reset, the reset will
5630 * be cancelled.
5631 *
5632 * - This function can be called during .probe() or .disconnect()
5633 * times. On return from .disconnect(), any pending resets will be
5634 * cancelled.
5635 *
5636 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5637 * does its own.
5638 *
5639 * NOTE: We don't do any reference count tracking because it is not
5640 * needed. The lifecycle of the work_struct is tied to the
5641 * usb_interface. Before destroying the interface we cancel the
5642 * work_struct, so the fact that work_struct is queued and or
5643 * running means the interface (and thus, the device) exist and
5644 * are referenced.
5645 */
5646void usb_queue_reset_device(struct usb_interface *iface)
5647{
5648 schedule_work(&iface->reset_ws);
5649}
5650EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c792012-09-05 13:44:32 +08005651
5652/**
5653 * usb_hub_find_child - Get the pointer of child device
5654 * attached to the port which is specified by @port1.
5655 * @hdev: USB device belonging to the usb hub
5656 * @port1: port num to indicate which port the child device
5657 * is attached to.
5658 *
5659 * USB drivers call this function to get hub's child device
5660 * pointer.
5661 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005662 * Return: %NULL if input param is invalid and
Lan Tianyuff823c792012-09-05 13:44:32 +08005663 * child's usb_device pointer if non-NULL.
5664 */
5665struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5666 int port1)
5667{
Lan Tianyuad493e52013-01-23 04:26:30 +08005668 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyuff823c792012-09-05 13:44:32 +08005669
5670 if (port1 < 1 || port1 > hdev->maxchild)
5671 return NULL;
5672 return hub->ports[port1 - 1]->child;
5673}
5674EXPORT_SYMBOL_GPL(usb_hub_find_child);
Lan Tianyud5575422012-09-05 13:44:33 +08005675
Lan Tianyud2123fd2013-01-21 22:18:00 +08005676void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
5677 struct usb_hub_descriptor *desc)
5678{
Dan Williamsd99f6b42014-05-20 18:08:17 -07005679 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyud2123fd2013-01-21 22:18:00 +08005680 enum usb_port_connect_type connect_type;
5681 int i;
5682
Dan Williamsd99f6b42014-05-20 18:08:17 -07005683 if (!hub)
5684 return;
5685
Lan Tianyud2123fd2013-01-21 22:18:00 +08005686 if (!hub_is_superspeed(hdev)) {
5687 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005688 struct usb_port *port_dev = hub->ports[i - 1];
Lan Tianyud2123fd2013-01-21 22:18:00 +08005689
Dan Williamsd99f6b42014-05-20 18:08:17 -07005690 connect_type = port_dev->connect_type;
Lan Tianyud2123fd2013-01-21 22:18:00 +08005691 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5692 u8 mask = 1 << (i%8);
5693
5694 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005695 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
Lan Tianyud2123fd2013-01-21 22:18:00 +08005696 desc->u.hs.DeviceRemovable[i/8] |= mask;
5697 }
5698 }
5699 }
5700 } else {
5701 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable);
5702
5703 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005704 struct usb_port *port_dev = hub->ports[i - 1];
Lan Tianyud2123fd2013-01-21 22:18:00 +08005705
Dan Williamsd99f6b42014-05-20 18:08:17 -07005706 connect_type = port_dev->connect_type;
Lan Tianyud2123fd2013-01-21 22:18:00 +08005707 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5708 u16 mask = 1 << i;
5709
5710 if (!(port_removable & mask)) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005711 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
Lan Tianyud2123fd2013-01-21 22:18:00 +08005712 port_removable |= mask;
5713 }
5714 }
5715 }
5716
5717 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
5718 }
5719}
5720
Lan Tianyud5575422012-09-05 13:44:33 +08005721#ifdef CONFIG_ACPI
5722/**
5723 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5724 * @hdev: USB device belonging to the usb hub
5725 * @port1: port num of the port
5726 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005727 * Return: Port's acpi handle if successful, %NULL if params are
5728 * invalid.
Lan Tianyud5575422012-09-05 13:44:33 +08005729 */
5730acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5731 int port1)
5732{
Lan Tianyuad493e52013-01-23 04:26:30 +08005733 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyud5575422012-09-05 13:44:33 +08005734
Mathias Nyman413412612013-06-18 17:28:48 +03005735 if (!hub)
5736 return NULL;
5737
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +01005738 return ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
Lan Tianyud5575422012-09-05 13:44:33 +08005739}
5740#endif