blob: d8d992b73e88232aea4c59de1055c8e8ac12247a [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>
Petr Mladek32a695892014-09-19 17:32:21 +020025#include <linux/workqueue.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010026#include <linux/mutex.h>
Theodore Ts'ob04b3152012-07-04 11:22:20 -040027#include <linux/random.h>
Lan Tianyuad493e52013-01-23 04:26:30 +080028#include <linux/pm_qos.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
31#include <asm/byteorder.h>
32
Lan Tianyu6e30d7c2013-01-11 20:10:38 +080033#include "hub.h"
Peter Chen026f3fc2014-08-19 09:51:53 +080034#include "otg_whitelist.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Ming Leie6f30de2012-10-24 11:59:24 +080036#define USB_VENDOR_GENESYS_LOGIC 0x05e3
37#define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01
38
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -070039/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -050040 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
42static DEFINE_SPINLOCK(device_state_lock);
43
Petr Mladek32a695892014-09-19 17:32:21 +020044/* workqueue to process hub events */
45static struct workqueue_struct *hub_wq;
46static void hub_event(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Dan Williamsd8521af2014-05-20 18:08:28 -070048/* synchronize hub-port add/remove and peering operations */
49DEFINE_MUTEX(usb_port_peer_mutex);
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/* cycle leds on hubs that aren't blinking for attention */
Saurabh Sengara44007a42015-10-26 09:25:36 +053052static bool blinkenlights;
Chase Metzger166b8632015-08-08 01:03:34 -070053module_param(blinkenlights, bool, S_IRUGO);
54MODULE_PARM_DESC(blinkenlights, "true to cycle leds on hubs");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020057 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
58 * 10 seconds to send reply for the initial 64-byte descriptor request.
59 */
60/* define initial 64-byte descriptor request timeout in milliseconds */
61static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
62module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +080063MODULE_PARM_DESC(initial_descriptor_timeout,
64 "initial 64-byte descriptor request timeout in milliseconds "
65 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020066
67/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * As of 2.6.10 we introduce a new USB device initialization scheme which
69 * closely resembles the way Windows works. Hopefully it will be compatible
70 * with a wider range of devices than the old scheme. However some previously
71 * working devices may start giving rise to "device not accepting address"
72 * errors; if that happens the user can try the old scheme by adjusting the
73 * following module parameters.
74 *
75 * For maximum flexibility there are two boolean parameters to control the
76 * hub driver's behavior. On the first initialization attempt, if the
77 * "old_scheme_first" parameter is set then the old scheme will be used,
78 * otherwise the new scheme is used. If that fails and "use_both_schemes"
79 * is set, then the driver will make another attempt, using the other scheme.
80 */
Saurabh Sengara44007a42015-10-26 09:25:36 +053081static bool old_scheme_first;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
83MODULE_PARM_DESC(old_scheme_first,
84 "start with the old device initialization scheme");
85
Rusty Russell90ab5ee2012-01-13 09:32:20 +103086static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
88MODULE_PARM_DESC(use_both_schemes,
89 "try the other device initialization scheme if the "
90 "first one fails");
91
Alan Stern32fe0192007-10-10 16:27:07 -040092/* Mutual exclusion for EHCI CF initialization. This interferes with
93 * port reset on some companion controllers.
94 */
95DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
96EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
97
Lan Tianyuad493e52013-01-23 04:26:30 +080098#define HUB_DEBOUNCE_TIMEOUT 2000
Alan Stern948fea32008-04-28 11:07:07 -040099#define HUB_DEBOUNCE_STEP 25
100#define HUB_DEBOUNCE_STABLE 100
101
Petr Mladek32a695892014-09-19 17:32:21 +0200102static void hub_release(struct kref *kref);
Ming Lei742120c2008-06-18 22:00:29 +0800103static int usb_reset_and_verify_device(struct usb_device *udev);
Geert Uytterhoeven8ecf70f2016-12-14 15:37:30 +0100104static int hub_port_disable(struct usb_hub *hub, int port1, int set_state);
Ming Lei742120c2008-06-18 22:00:29 +0800105
Sarah Sharp131dec32010-12-06 21:00:19 -0800106static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Oliver Neukumd64aab02016-04-20 16:28:09 +0200108 if (hub_is_superspeedplus(hub->hdev))
109 return "10.0 Gb/s";
Sarah Sharp131dec32010-12-06 21:00:19 -0800110 if (hub_is_superspeed(hub->hdev))
111 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500112 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Matthias Beyer469271f2013-10-10 23:41:27 +0200113 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500114 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return "1.5 Mb/s";
116 else
117 return "12 Mb/s";
118}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/* Note that hdev or one of its children must be locked! */
Lan Tianyuad493e52013-01-23 04:26:30 +0800121struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Lan Tianyuff823c72012-09-05 13:44:32 +0800123 if (!hdev || !hdev->actconfig || !hdev->maxchild)
Alan Stern251180842009-07-22 14:41:18 -0400124 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return usb_get_intfdata(hdev->actconfig->interface[0]);
126}
127
Lu Baolu2d2a3162015-06-16 09:08:26 +0800128int usb_device_supports_lpm(struct usb_device *udev)
Sarah Sharpd9b20992012-02-20 08:31:26 -0800129{
Alan Sternad87e032015-12-10 15:27:21 -0500130 /* Some devices have trouble with LPM */
131 if (udev->quirks & USB_QUIRK_NO_LPM)
132 return 0;
133
Sarah Sharpd9b20992012-02-20 08:31:26 -0800134 /* USB 2.1 (and greater) devices indicate LPM support through
135 * their USB 2.0 Extended Capabilities BOS descriptor.
136 */
Rupesh Tatiyaa8425292015-04-14 16:36:55 +0530137 if (udev->speed == USB_SPEED_HIGH || udev->speed == USB_SPEED_FULL) {
Sarah Sharpd9b20992012-02-20 08:31:26 -0800138 if (udev->bos->ext_cap &&
139 (USB_LPM_SUPPORT &
140 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
141 return 1;
142 return 0;
143 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800144
Sarah Sharp25cd2882014-01-17 14:15:44 -0800145 /*
146 * According to the USB 3.0 spec, all USB 3.0 devices must support LPM.
147 * However, there are some that don't, and they set the U1/U2 exit
148 * latencies to zero.
Sarah Sharp51e0a012012-02-20 12:02:19 -0800149 */
150 if (!udev->bos->ss_cap) {
Sarah Sharp25cd2882014-01-17 14:15:44 -0800151 dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n");
Sarah Sharp51e0a012012-02-20 12:02:19 -0800152 return 0;
153 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800154
Sarah Sharp25cd2882014-01-17 14:15:44 -0800155 if (udev->bos->ss_cap->bU1devExitLat == 0 &&
156 udev->bos->ss_cap->bU2DevExitLat == 0) {
157 if (udev->parent)
158 dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n");
159 else
160 dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n");
161 return 0;
162 }
163
164 if (!udev->parent || udev->parent->lpm_capable)
165 return 1;
Sarah Sharpd9b20992012-02-20 08:31:26 -0800166 return 0;
167}
168
Sarah Sharp51e0a012012-02-20 12:02:19 -0800169/*
170 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
171 * either U1 or U2.
172 */
173static void usb_set_lpm_mel(struct usb_device *udev,
174 struct usb3_lpm_parameters *udev_lpm_params,
175 unsigned int udev_exit_latency,
176 struct usb_hub *hub,
177 struct usb3_lpm_parameters *hub_lpm_params,
178 unsigned int hub_exit_latency)
179{
180 unsigned int total_mel;
181 unsigned int device_mel;
182 unsigned int hub_mel;
183
184 /*
185 * Calculate the time it takes to transition all links from the roothub
186 * to the parent hub into U0. The parent hub must then decode the
187 * packet (hub header decode latency) to figure out which port it was
188 * bound for.
189 *
190 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
191 * means 0.1us). Multiply that by 100 to get nanoseconds.
192 */
193 total_mel = hub_lpm_params->mel +
194 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
195
196 /*
197 * How long will it take to transition the downstream hub's port into
198 * U0? The greater of either the hub exit latency or the device exit
199 * latency.
200 *
201 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
202 * Multiply that by 1000 to get nanoseconds.
203 */
204 device_mel = udev_exit_latency * 1000;
205 hub_mel = hub_exit_latency * 1000;
206 if (device_mel > hub_mel)
207 total_mel += device_mel;
208 else
209 total_mel += hub_mel;
210
211 udev_lpm_params->mel = total_mel;
212}
213
214/*
215 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
216 * a transition from either U1 or U2.
217 */
218static void usb_set_lpm_pel(struct usb_device *udev,
219 struct usb3_lpm_parameters *udev_lpm_params,
220 unsigned int udev_exit_latency,
221 struct usb_hub *hub,
222 struct usb3_lpm_parameters *hub_lpm_params,
223 unsigned int hub_exit_latency,
224 unsigned int port_to_port_exit_latency)
225{
226 unsigned int first_link_pel;
227 unsigned int hub_pel;
228
229 /*
230 * First, the device sends an LFPS to transition the link between the
231 * device and the parent hub into U0. The exit latency is the bigger of
232 * the device exit latency or the hub exit latency.
233 */
234 if (udev_exit_latency > hub_exit_latency)
235 first_link_pel = udev_exit_latency * 1000;
236 else
237 first_link_pel = hub_exit_latency * 1000;
238
239 /*
240 * When the hub starts to receive the LFPS, there is a slight delay for
241 * it to figure out that one of the ports is sending an LFPS. Then it
242 * will forward the LFPS to its upstream link. The exit latency is the
243 * delay, plus the PEL that we calculated for this hub.
244 */
245 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
246
247 /*
248 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
249 * is the greater of the two exit latencies.
250 */
251 if (first_link_pel > hub_pel)
252 udev_lpm_params->pel = first_link_pel;
253 else
254 udev_lpm_params->pel = hub_pel;
255}
256
257/*
258 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
259 * when a device initiates a transition to U0, until when it will receive the
260 * first packet from the host controller.
261 *
262 * Section C.1.5.1 describes the four components to this:
263 * - t1: device PEL
264 * - t2: time for the ERDY to make it from the device to the host.
265 * - t3: a host-specific delay to process the ERDY.
266 * - t4: time for the packet to make it from the host to the device.
267 *
268 * t3 is specific to both the xHCI host and the platform the host is integrated
269 * into. The Intel HW folks have said it's negligible, FIXME if a different
270 * vendor says otherwise.
271 */
272static void usb_set_lpm_sel(struct usb_device *udev,
273 struct usb3_lpm_parameters *udev_lpm_params)
274{
275 struct usb_device *parent;
276 unsigned int num_hubs;
277 unsigned int total_sel;
278
279 /* t1 = device PEL */
280 total_sel = udev_lpm_params->pel;
281 /* How many external hubs are in between the device & the root port. */
282 for (parent = udev->parent, num_hubs = 0; parent->parent;
283 parent = parent->parent)
284 num_hubs++;
285 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
286 if (num_hubs > 0)
287 total_sel += 2100 + 250 * (num_hubs - 1);
288
289 /* t4 = 250ns * num_hubs */
290 total_sel += 250 * num_hubs;
291
292 udev_lpm_params->sel = total_sel;
293}
294
295static void usb_set_lpm_parameters(struct usb_device *udev)
296{
297 struct usb_hub *hub;
298 unsigned int port_to_port_delay;
299 unsigned int udev_u1_del;
300 unsigned int udev_u2_del;
301 unsigned int hub_u1_del;
302 unsigned int hub_u2_del;
303
Mathias Nyman8a1b2722015-12-10 09:59:25 +0200304 if (!udev->lpm_capable || udev->speed < USB_SPEED_SUPER)
Sarah Sharp51e0a012012-02-20 12:02:19 -0800305 return;
306
Lan Tianyuad493e52013-01-23 04:26:30 +0800307 hub = usb_hub_to_struct_hub(udev->parent);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800308 /* It doesn't take time to transition the roothub into U0, since it
309 * doesn't have an upstream link.
310 */
311 if (!hub)
312 return;
313
314 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
Xenia Ragiadakou4d967992013-08-31 18:09:13 +0300315 udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800316 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
Xenia Ragiadakou4d967992013-08-31 18:09:13 +0300317 hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800318
319 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
320 hub, &udev->parent->u1_params, hub_u1_del);
321
322 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
323 hub, &udev->parent->u2_params, hub_u2_del);
324
325 /*
326 * Appendix C, section C.2.2.2, says that there is a slight delay from
327 * when the parent hub notices the downstream port is trying to
328 * transition to U0 to when the hub initiates a U0 transition on its
329 * upstream port. The section says the delays are tPort2PortU1EL and
330 * tPort2PortU2EL, but it doesn't define what they are.
331 *
332 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
333 * about the same delays. Use the maximum delay calculations from those
334 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
335 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
336 * assume the device exit latencies they are talking about are the hub
337 * exit latencies.
338 *
339 * What do we do if the U2 exit latency is less than the U1 exit
340 * latency? It's possible, although not likely...
341 */
342 port_to_port_delay = 1;
343
344 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
345 hub, &udev->parent->u1_params, hub_u1_del,
346 port_to_port_delay);
347
348 if (hub_u2_del > hub_u1_del)
349 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
350 else
351 port_to_port_delay = 1 + hub_u1_del;
352
353 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
354 hub, &udev->parent->u2_params, hub_u2_del,
355 port_to_port_delay);
356
357 /* Now that we've got PEL, calculate SEL. */
358 usb_set_lpm_sel(udev, &udev->u1_params);
359 usb_set_lpm_sel(udev, &udev->u2_params);
360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/* USB 2.0 spec Section 11.24.4.5 */
Johan Hovoldc67e87a2017-05-10 18:18:28 +0200363static int get_hub_descriptor(struct usb_device *hdev,
364 struct usb_hub_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
John Youndbe79bb2001-09-17 00:00:00 -0700366 int i, ret, size;
367 unsigned dtype;
368
369 if (hub_is_superspeed(hdev)) {
370 dtype = USB_DT_SS_HUB;
371 size = USB_DT_SS_HUB_SIZE;
372 } else {
373 dtype = USB_DT_HUB;
374 size = sizeof(struct usb_hub_descriptor);
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 for (i = 0; i < 3; i++) {
378 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
379 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
Johan Hovoldc67e87a2017-05-10 18:18:28 +0200380 dtype << 8, 0, desc, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 USB_CTRL_GET_TIMEOUT);
Johan Hovold3e4a4e62017-05-10 18:18:27 +0200382 if (hub_is_superspeed(hdev)) {
383 if (ret == size)
384 return ret;
Johan Hovoldc67e87a2017-05-10 18:18:28 +0200385 } else if (ret >= USB_DT_HUB_NONVAR_SIZE + 2) {
386 /* Make sure we have the DeviceRemovable field. */
387 size = USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts / 8 + 1;
388 if (ret < size)
389 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return ret;
Johan Hovold3e4a4e62017-05-10 18:18:27 +0200391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393 return -EINVAL;
394}
395
396/*
397 * USB 2.0 spec Section 11.24.2.1
398 */
399static int clear_hub_feature(struct usb_device *hdev, int feature)
400{
401 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
402 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
403}
404
405/*
406 * USB 2.0 spec Section 11.24.2.2
407 */
Lan Tianyuad493e52013-01-23 04:26:30 +0800408int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
411 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
412 NULL, 0, 1000);
413}
414
415/*
416 * USB 2.0 spec Section 11.24.2.13
417 */
418static int set_port_feature(struct usb_device *hdev, int port1, int feature)
419{
420 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
421 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
422 NULL, 0, 1000);
423}
424
Dan Williamsd99f6b42014-05-20 18:08:17 -0700425static char *to_led_name(int selector)
426{
427 switch (selector) {
428 case HUB_LED_AMBER:
429 return "amber";
430 case HUB_LED_GREEN:
431 return "green";
432 case HUB_LED_OFF:
433 return "off";
434 case HUB_LED_AUTO:
435 return "auto";
436 default:
437 return "??";
438 }
439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/*
442 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
443 * for info about using port indicators
444 */
Dan Williamsd99f6b42014-05-20 18:08:17 -0700445static void set_port_led(struct usb_hub *hub, int port1, int selector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Dan Williamsd99f6b42014-05-20 18:08:17 -0700447 struct usb_port *port_dev = hub->ports[port1 - 1];
448 int status;
449
450 status = set_port_feature(hub->hdev, (selector << 8) | port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 USB_PORT_FEAT_INDICATOR);
Dan Williamsd99f6b42014-05-20 18:08:17 -0700452 dev_dbg(&port_dev->dev, "indicator %s status %d\n",
453 to_led_name(selector), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456#define LED_CYCLE_PERIOD ((2*HZ)/3)
457
Chase Metzger166b8632015-08-08 01:03:34 -0700458static void led_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
David Howellsc4028952006-11-22 14:57:56 +0000460 struct usb_hub *hub =
461 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct usb_device *hdev = hub->hdev;
463 unsigned i;
464 unsigned changed = 0;
465 int cursor = -1;
466
467 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
468 return;
469
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +0200470 for (i = 0; i < hdev->maxchild; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 unsigned selector, mode;
472
473 /* 30%-50% duty cycle */
474
475 switch (hub->indicator[i]) {
476 /* cycle marker */
477 case INDICATOR_CYCLE:
478 cursor = i;
479 selector = HUB_LED_AUTO;
480 mode = INDICATOR_AUTO;
481 break;
482 /* blinking green = sw attention */
483 case INDICATOR_GREEN_BLINK:
484 selector = HUB_LED_GREEN;
485 mode = INDICATOR_GREEN_BLINK_OFF;
486 break;
487 case INDICATOR_GREEN_BLINK_OFF:
488 selector = HUB_LED_OFF;
489 mode = INDICATOR_GREEN_BLINK;
490 break;
491 /* blinking amber = hw attention */
492 case INDICATOR_AMBER_BLINK:
493 selector = HUB_LED_AMBER;
494 mode = INDICATOR_AMBER_BLINK_OFF;
495 break;
496 case INDICATOR_AMBER_BLINK_OFF:
497 selector = HUB_LED_OFF;
498 mode = INDICATOR_AMBER_BLINK;
499 break;
500 /* blink green/amber = reserved */
501 case INDICATOR_ALT_BLINK:
502 selector = HUB_LED_GREEN;
503 mode = INDICATOR_ALT_BLINK_OFF;
504 break;
505 case INDICATOR_ALT_BLINK_OFF:
506 selector = HUB_LED_AMBER;
507 mode = INDICATOR_ALT_BLINK;
508 break;
509 default:
510 continue;
511 }
512 if (selector != HUB_LED_AUTO)
513 changed = 1;
514 set_port_led(hub, i + 1, selector);
515 hub->indicator[i] = mode;
516 }
517 if (!changed && blinkenlights) {
518 cursor++;
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +0200519 cursor %= hdev->maxchild;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
521 hub->indicator[cursor] = INDICATOR_CYCLE;
522 changed++;
523 }
524 if (changed)
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -0800525 queue_delayed_work(system_power_efficient_wq,
526 &hub->leds, LED_CYCLE_PERIOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529/* use a short timeout for hub/port status fetches */
530#define USB_STS_TIMEOUT 1000
531#define USB_STS_RETRIES 5
532
533/*
534 * USB 2.0 spec Section 11.24.2.6
535 */
536static int get_hub_status(struct usb_device *hdev,
537 struct usb_hub_status *data)
538{
539 int i, status = -ETIMEDOUT;
540
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200541 for (i = 0; i < USB_STS_RETRIES &&
542 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
544 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
545 data, sizeof(*data), USB_STS_TIMEOUT);
546 }
547 return status;
548}
549
550/*
551 * USB 2.0 spec Section 11.24.2.7
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200552 * USB 3.1 takes into use the wValue and wLength fields, spec Section 10.16.2.6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 */
554static int get_port_status(struct usb_device *hdev, int port1,
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200555 void *data, u16 value, u16 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 int i, status = -ETIMEDOUT;
558
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200559 for (i = 0; i < USB_STS_RETRIES &&
560 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200562 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, value,
563 port1, data, length, USB_STS_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 return status;
566}
567
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200568static int hub_ext_port_status(struct usb_hub *hub, int port1, int type,
569 u16 *status, u16 *change, u32 *ext_status)
Alan Stern3eb14912008-03-03 15:15:43 -0500570{
571 int ret;
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200572 int len = 4;
573
574 if (type != HUB_PORT_STATUS)
575 len = 8;
Alan Stern3eb14912008-03-03 15:15:43 -0500576
577 mutex_lock(&hub->status_mutex);
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200578 ret = get_port_status(hub->hdev, port1, &hub->status->port, type, len);
579 if (ret < len) {
Alan Sterne9e88fb2013-03-27 16:14:01 -0400580 if (ret != -ENODEV)
581 dev_err(hub->intfdev,
582 "%s failed (err = %d)\n", __func__, ret);
Alan Stern3eb14912008-03-03 15:15:43 -0500583 if (ret >= 0)
584 ret = -EIO;
585 } else {
586 *status = le16_to_cpu(hub->status->port.wPortStatus);
587 *change = le16_to_cpu(hub->status->port.wPortChange);
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200588 if (type != HUB_PORT_STATUS && ext_status)
589 *ext_status = le32_to_cpu(
590 hub->status->port.dwExtPortStatus);
Alan Stern3eb14912008-03-03 15:15:43 -0500591 ret = 0;
592 }
593 mutex_unlock(&hub->status_mutex);
594 return ret;
595}
596
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200597static int hub_port_status(struct usb_hub *hub, int port1,
598 u16 *status, u16 *change)
599{
600 return hub_ext_port_status(hub, port1, HUB_PORT_STATUS,
601 status, change, NULL);
602}
603
Petr Mladek32a695892014-09-19 17:32:21 +0200604static void kick_hub_wq(struct usb_hub *hub)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Petr Mladek32a695892014-09-19 17:32:21 +0200606 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Petr Mladek32a695892014-09-19 17:32:21 +0200608 if (hub->disconnected || work_pending(&hub->events))
609 return;
Alan Stern8e4ceb32009-12-07 13:01:37 -0500610
Petr Mladek32a695892014-09-19 17:32:21 +0200611 /*
612 * Suppress autosuspend until the event is proceed.
613 *
614 * Be careful and make sure that the symmetric operation is
615 * always called. We are here only when there is no pending
616 * work for this hub. Therefore put the interface either when
617 * the new work is called or when it is canceled.
618 */
619 intf = to_usb_interface(hub->intfdev);
620 usb_autopm_get_interface_no_resume(intf);
621 kref_get(&hub->kref);
622
623 if (queue_work(hub_wq, &hub->events))
624 return;
625
626 /* the work has already been scheduled */
627 usb_autopm_put_interface_async(intf);
628 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Petr Mladek59d48b32014-09-19 17:32:22 +0200631void usb_kick_hub_wq(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Lan Tianyuad493e52013-01-23 04:26:30 +0800633 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Alan Stern251180842009-07-22 14:41:18 -0400634
635 if (hub)
Petr Mladek32a695892014-09-19 17:32:21 +0200636 kick_hub_wq(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800639/*
640 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
641 * Notification, which indicates it had initiated remote wakeup.
642 *
643 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
644 * device initiates resume, so the USB core will not receive notice of the
645 * resume through the normal hub interrupt URB.
646 */
647void usb_wakeup_notification(struct usb_device *hdev,
648 unsigned int portnum)
649{
650 struct usb_hub *hub;
Ravi Chandra Sadineni00bc3482018-04-20 11:08:21 -0700651 struct usb_port *port_dev;
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800652
653 if (!hdev)
654 return;
655
Lan Tianyuad493e52013-01-23 04:26:30 +0800656 hub = usb_hub_to_struct_hub(hdev);
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800657 if (hub) {
Ravi Chandra Sadineni00bc3482018-04-20 11:08:21 -0700658 port_dev = hub->ports[portnum - 1];
659 if (port_dev && port_dev->child)
660 pm_wakeup_event(&port_dev->child->dev, 0);
661
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800662 set_bit(portnum, hub->wakeup_bits);
Petr Mladek32a695892014-09-19 17:32:21 +0200663 kick_hub_wq(hub);
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800664 }
665}
666EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100669static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200671 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400672 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100673 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 unsigned long bits;
675
Alan Sterne0152682007-08-24 15:42:52 -0400676 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 case -ENOENT: /* synchronous unlink */
678 case -ECONNRESET: /* async unlink */
679 case -ESHUTDOWN: /* hardware going away */
680 return;
681
682 default: /* presumably an error */
683 /* Cause a hub reset after 10 consecutive errors */
Chase Metzger166b8632015-08-08 01:03:34 -0700684 dev_dbg(hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if ((++hub->nerrors < 10) || hub->error)
686 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400687 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200689
Petr Mladek37ebb542014-09-19 17:32:23 +0200690 /* let hub_wq handle things */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 case 0: /* we got data: port status changed */
692 bits = 0;
693 for (i = 0; i < urb->actual_length; ++i)
694 bits |= ((unsigned long) ((*hub->buffer)[i]))
695 << (i*8);
696 hub->event_bits[0] = bits;
697 break;
698 }
699
700 hub->nerrors = 0;
701
Petr Mladek37ebb542014-09-19 17:32:23 +0200702 /* Something happened, let hub_wq figure it out */
Petr Mladek32a695892014-09-19 17:32:21 +0200703 kick_hub_wq(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705resubmit:
706 if (hub->quiescing)
707 return;
708
Kris Borer088a3da2015-08-11 11:12:45 -0400709 status = usb_submit_urb(hub->urb, GFP_ATOMIC);
710 if (status != 0 && status != -ENODEV && status != -EPERM)
Chase Metzger166b8632015-08-08 01:03:34 -0700711 dev_err(hub->intfdev, "resubmit --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714/* USB 2.0 spec Section 11.24.2.3 */
715static inline int
Chase Metzger166b8632015-08-08 01:03:34 -0700716hub_clear_tt_buffer(struct usb_device *hdev, u16 devinfo, u16 tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
William Gulland2c7b8712013-06-27 16:10:20 -0700718 /* Need to clear both directions for control ep */
719 if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) ==
720 USB_ENDPOINT_XFER_CONTROL) {
721 int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
722 HUB_CLEAR_TT_BUFFER, USB_RT_PORT,
723 devinfo ^ 0x8000, tt, NULL, 0, 1000);
724 if (status)
725 return status;
726 }
Alan Sternc2f65952009-11-18 11:37:15 -0500727 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
729 tt, NULL, 0, 1000);
730}
731
732/*
Petr Mladek37ebb542014-09-19 17:32:23 +0200733 * enumeration blocks hub_wq for a long time. we use keventd instead, since
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 * long blocking there is the exception, not the rule. accordingly, HCDs
735 * talking to TTs must queue control transfers (not just bulk and iso), so
736 * both can talk to the same hub concurrently.
737 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400738static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
David Howellsc4028952006-11-22 14:57:56 +0000740 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400741 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 unsigned long flags;
743
Chase Metzger166b8632015-08-08 01:03:34 -0700744 spin_lock_irqsave(&hub->tt.lock, flags);
Octavian Purdila3b6054d2012-10-01 22:21:12 +0300745 while (!list_empty(&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400746 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 struct usb_tt_clear *clear;
748 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400749 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 int status;
751
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400752 next = hub->tt.clear_list.next;
Chase Metzger166b8632015-08-08 01:03:34 -0700753 clear = list_entry(next, struct usb_tt_clear, clear_list);
754 list_del(&clear->clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /* drop lock so HCD can concurrently report other TT errors */
Chase Metzger166b8632015-08-08 01:03:34 -0700757 spin_unlock_irqrestore(&hub->tt.lock, flags);
758 status = hub_clear_tt_buffer(hdev, clear->devinfo, clear->tt);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400759 if (status && status != -ENODEV)
Chase Metzger166b8632015-08-08 01:03:34 -0700760 dev_err(&hdev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 "clear tt %d (%04x) error %d\n",
762 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400763
764 /* Tell the HCD, even if the operation failed */
765 drv = clear->hcd->driver;
766 if (drv->clear_tt_buffer_complete)
767 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
768
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700769 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400770 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Chase Metzger166b8632015-08-08 01:03:34 -0700772 spin_unlock_irqrestore(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775/**
Lan Tianyu971fcd42013-01-23 04:26:29 +0800776 * usb_hub_set_port_power - control hub port's power state
Mathias Nyman41341262013-06-18 17:28:48 +0300777 * @hdev: USB device belonging to the usb hub
778 * @hub: target hub
Lan Tianyu971fcd42013-01-23 04:26:29 +0800779 * @port1: port index
780 * @set: expected status
781 *
782 * call this function to control port's power via setting or
783 * clearing the port's PORT_POWER feature.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200784 *
785 * Return: 0 if successful. A negative error code otherwise.
Lan Tianyu971fcd42013-01-23 04:26:29 +0800786 */
Mathias Nyman41341262013-06-18 17:28:48 +0300787int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub,
788 int port1, bool set)
Lan Tianyu971fcd42013-01-23 04:26:29 +0800789{
790 int ret;
791
792 if (set)
793 ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
794 else
Lan Tianyuad493e52013-01-23 04:26:30 +0800795 ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
796
Dan Williamsd5c38342014-05-20 18:08:52 -0700797 if (ret)
798 return ret;
799
800 if (set)
801 set_bit(port1, hub->power_bits);
802 else
803 clear_bit(port1, hub->power_bits);
804 return 0;
Lan Tianyu971fcd42013-01-23 04:26:29 +0800805}
806
807/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400808 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
809 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 *
811 * High speed HCDs use this to tell the hub driver that some split control or
812 * bulk transaction failed in a way that requires clearing internal state of
813 * a transaction translator. This is normally detected (and reported) from
814 * interrupt context.
815 *
816 * It may not be possible for that hub to handle additional full (or low)
817 * speed transactions until that state is fully cleared out.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200818 *
819 * Return: 0 if successful. A negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400821int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400823 struct usb_device *udev = urb->dev;
824 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 struct usb_tt *tt = udev->tt;
826 unsigned long flags;
827 struct usb_tt_clear *clear;
828
829 /* we've got to cope with an arbitrary number of pending TT clears,
830 * since each TT has "at least two" buffers that can need it (and
831 * there can be many TTs per hub). even if they're uncommon.
832 */
Greg Kroah-Hartmand544d272015-04-30 11:32:53 +0200833 clear = kmalloc(sizeof *clear, GFP_ATOMIC);
834 if (clear == NULL) {
Chase Metzger166b8632015-08-08 01:03:34 -0700835 dev_err(&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400837 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
840 /* info that CLEAR_TT_BUFFER needs */
841 clear->tt = tt->multi ? udev->ttport : 1;
842 clear->devinfo = usb_pipeendpoint (pipe);
843 clear->devinfo |= udev->devnum << 4;
Chase Metzger166b8632015-08-08 01:03:34 -0700844 clear->devinfo |= usb_pipecontrol(pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 ? (USB_ENDPOINT_XFER_CONTROL << 11)
846 : (USB_ENDPOINT_XFER_BULK << 11);
Chase Metzger166b8632015-08-08 01:03:34 -0700847 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400849
850 /* info for completion callback */
851 clear->hcd = bus_to_hcd(udev->bus);
852 clear->ep = urb->ep;
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* tell keventd to clear state for this TT */
Chase Metzger166b8632015-08-08 01:03:34 -0700855 spin_lock_irqsave(&tt->lock, flags);
856 list_add_tail(&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400857 schedule_work(&tt->clear_work);
Chase Metzger166b8632015-08-08 01:03:34 -0700858 spin_unlock_irqrestore(&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400861EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Dan Williams7ad3c472014-05-20 18:08:57 -0700863static void hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 int port1;
866
Alan Stern4489a572006-04-27 15:54:22 -0400867 /* Enable power on each port. Some hubs have reserved values
868 * of LPSM (> 2) in their descriptors, even though they are
869 * USB 2.0 hubs. Some hubs do not implement port-power switching
870 * but only emulate it. In all cases, the ports won't work
871 * unless we send these messages to the hub.
872 */
Dan Williams9262c192014-05-20 18:08:12 -0700873 if (hub_is_port_power_switchable(hub))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400875 else
876 dev_dbg(hub->intfdev, "trying to enable port power on "
877 "non-switchable hub\n");
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +0200878 for (port1 = 1; port1 <= hub->hdev->maxchild; port1++)
Dan Williamsd5c38342014-05-20 18:08:52 -0700879 if (test_bit(port1, hub->power_bits))
Lan Tianyuad493e52013-01-23 04:26:30 +0800880 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
881 else
882 usb_clear_port_feature(hub->hdev, port1,
883 USB_PORT_FEAT_POWER);
Alan Stern8520f382008-09-22 14:44:26 -0400884 if (do_delay)
Dan Williams7ad3c472014-05-20 18:08:57 -0700885 msleep(hub_power_on_good_delay(hub));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888static int hub_hub_status(struct usb_hub *hub,
889 u16 *status, u16 *change)
890{
891 int ret;
892
Alan Sterndb90e7a2007-02-05 09:56:15 -0500893 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 ret = get_hub_status(hub->hdev, &hub->status->hub);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400895 if (ret < 0) {
896 if (ret != -ENODEV)
897 dev_err(hub->intfdev,
898 "%s failed (err = %d)\n", __func__, ret);
899 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 *status = le16_to_cpu(hub->status->hub.wHubStatus);
Matthias Beyer469271f2013-10-10 23:41:27 +0200901 *change = le16_to_cpu(hub->status->hub.wHubChange);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 ret = 0;
903 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500904 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return ret;
906}
907
Sarah Sharp41e7e052012-11-14 16:42:32 -0800908static int hub_set_port_link_state(struct usb_hub *hub, int port1,
909 unsigned int link_status)
910{
911 return set_port_feature(hub->hdev,
912 port1 | (link_status << 3),
913 USB_PORT_FEAT_LINK_STATE);
914}
915
916/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800917 * Disable a port and mark a logical connect-change event, so that some
Petr Mladek37ebb542014-09-19 17:32:23 +0200918 * time later hub_wq will disconnect() any existing usb_device on the port
Alan Stern0458d5b2007-05-04 11:52:20 -0400919 * and will re-enumerate if there actually is a device attached.
920 */
921static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
922{
Dan Williamsd99f6b42014-05-20 18:08:17 -0700923 dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n");
Alan Stern0458d5b2007-05-04 11:52:20 -0400924 hub_port_disable(hub, port1, 1);
925
926 /* FIXME let caller ask to power down the port:
927 * - some devices won't enumerate without a VBUS power cycle
928 * - SRP saves power that way
929 * - ... new call, TBD ...
930 * That's easy if this hub can switch power per-port, and
Petr Mladek37ebb542014-09-19 17:32:23 +0200931 * hub_wq reactivates the port later (timer, SRP, etc).
Alan Stern0458d5b2007-05-04 11:52:20 -0400932 * Powerdown must be optional, because of reset/DFU.
933 */
934
935 set_bit(port1, hub->change_bits);
Petr Mladek32a695892014-09-19 17:32:21 +0200936 kick_hub_wq(hub);
Alan Stern0458d5b2007-05-04 11:52:20 -0400937}
938
Alan Stern253e0572009-10-27 15:20:13 -0400939/**
940 * usb_remove_device - disable a device's port on its parent hub
941 * @udev: device to be disabled and removed
942 * Context: @udev locked, must be able to sleep.
943 *
Petr Mladek37ebb542014-09-19 17:32:23 +0200944 * After @udev's port has been disabled, hub_wq is notified and it will
Alan Stern253e0572009-10-27 15:20:13 -0400945 * see that the device has been disconnected. When the device is
946 * physically unplugged and something is plugged in, the events will
947 * be received and processed normally.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200948 *
949 * Return: 0 if successful. A negative error code otherwise.
Alan Stern253e0572009-10-27 15:20:13 -0400950 */
951int usb_remove_device(struct usb_device *udev)
952{
953 struct usb_hub *hub;
954 struct usb_interface *intf;
955
956 if (!udev->parent) /* Can't remove a root hub */
957 return -EINVAL;
Lan Tianyuad493e52013-01-23 04:26:30 +0800958 hub = usb_hub_to_struct_hub(udev->parent);
Alan Stern253e0572009-10-27 15:20:13 -0400959 intf = to_usb_interface(hub->intfdev);
960
961 usb_autopm_get_interface(intf);
962 set_bit(udev->portnum, hub->removed_bits);
963 hub_port_logical_disconnect(hub, udev->portnum);
964 usb_autopm_put_interface(intf);
965 return 0;
966}
967
Alan Stern6ee0b272008-04-28 11:06:42 -0400968enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -0500969 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -0400970 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -0400971};
Alan Stern5e6effa2008-03-03 15:15:51 -0500972
Alan Stern8520f382008-09-22 14:44:26 -0400973static void hub_init_func2(struct work_struct *ws);
974static void hub_init_func3(struct work_struct *ws);
975
Alan Sternf2835212008-04-28 11:07:17 -0400976static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500977{
978 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -0800979 struct usb_hcd *hcd;
980 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -0500981 int port1;
Alan Sternf2835212008-04-28 11:07:17 -0400982 int status;
Alan Stern948fea32008-04-28 11:07:07 -0400983 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -0400984 unsigned delay;
985
986 /* Continue a partial initialization */
Alan Sterne50293e2015-12-16 13:32:38 -0500987 if (type == HUB_INIT2 || type == HUB_INIT3) {
Alan Stern07d316a2016-08-05 11:51:30 -0400988 device_lock(&hdev->dev);
Alan Sterne50293e2015-12-16 13:32:38 -0500989
990 /* Was the hub disconnected while we were waiting? */
Alan Sternca5cbc82016-08-05 11:49:45 -0400991 if (hub->disconnected)
992 goto disconnected;
Alan Sterne50293e2015-12-16 13:32:38 -0500993 if (type == HUB_INIT2)
994 goto init2;
Alan Stern8520f382008-09-22 14:44:26 -0400995 goto init3;
Alan Sterne50293e2015-12-16 13:32:38 -0500996 }
997 kref_get(&hub->kref);
Alan Stern5e6effa2008-03-03 15:15:51 -0500998
Elric Fua45aa3b2012-02-18 13:32:27 +0800999 /* The superspeed hub except for root hub has to use Hub Depth
1000 * value as an offset into the route string to locate the bits
1001 * it uses to determine the downstream port number. So hub driver
1002 * should send a set hub depth request to superspeed hub after
1003 * the superspeed hub is set configuration in initialization or
1004 * reset procedure.
1005 *
1006 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -04001007 * For any other type of activation, turn it on.
1008 */
Alan Stern8520f382008-09-22 14:44:26 -04001009 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +08001010 if (hdev->parent && hub_is_superspeed(hdev)) {
1011 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1012 HUB_SET_DEPTH, USB_RT_HUB,
1013 hdev->level - 1, 0, NULL, 0,
1014 USB_CTRL_SET_TIMEOUT);
1015 if (ret < 0)
1016 dev_err(hub->intfdev,
1017 "set hub depth failed\n");
1018 }
Alan Stern8520f382008-09-22 14:44:26 -04001019
1020 /* Speed up system boot by using a delayed_work for the
1021 * hub's initial power-up delays. This is pretty awkward
1022 * and the implementation looks like a home-brewed sort of
1023 * setjmp/longjmp, but it saves at least 100 ms for each
1024 * root hub (assuming usbcore is compiled into the kernel
1025 * rather than as a module). It adds up.
1026 *
1027 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
1028 * because for those activation types the ports have to be
1029 * operational when we return. In theory this could be done
1030 * for HUB_POST_RESET, but it's easier not to.
1031 */
1032 if (type == HUB_INIT) {
Kris Borer17a364e2015-08-21 10:47:46 +05301033 delay = hub_power_on_good_delay(hub);
Dan Williams7ad3c472014-05-20 18:08:57 -07001034
1035 hub_power_on(hub, false);
Tejun Heo77fa83c2014-03-07 10:24:48 -05001036 INIT_DELAYED_WORK(&hub->init_work, hub_init_func2);
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08001037 queue_delayed_work(system_power_efficient_wq,
1038 &hub->init_work,
Alan Stern8520f382008-09-22 14:44:26 -04001039 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -04001040
1041 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -05001042 usb_autopm_get_interface_no_resume(
1043 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -04001044 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001045 } else if (type == HUB_RESET_RESUME) {
1046 /* The internal host controller state for the hub device
1047 * may be gone after a host power loss on system resume.
1048 * Update the device's info so the HW knows it's a hub.
1049 */
1050 hcd = bus_to_hcd(hdev->bus);
1051 if (hcd->driver->update_hub_device) {
1052 ret = hcd->driver->update_hub_device(hcd, hdev,
1053 &hub->tt, GFP_NOIO);
1054 if (ret < 0) {
1055 dev_err(hub->intfdev, "Host not "
1056 "accepting hub info "
1057 "update.\n");
1058 dev_err(hub->intfdev, "LS/FS devices "
1059 "and hubs may not work "
1060 "under this hub\n.");
1061 }
1062 }
1063 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001064 } else {
1065 hub_power_on(hub, true);
1066 }
1067 }
1068 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001069
Dan Williamsd99f6b42014-05-20 18:08:17 -07001070 /*
Petr Mladek37ebb542014-09-19 17:32:23 +02001071 * Check each port and set hub->change_bits to let hub_wq know
Alan Stern6ee0b272008-04-28 11:06:42 -04001072 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001073 */
1074 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07001075 struct usb_port *port_dev = hub->ports[port1 - 1];
1076 struct usb_device *udev = port_dev->child;
Alan Stern5e6effa2008-03-03 15:15:51 -05001077 u16 portstatus, portchange;
1078
Alan Stern6ee0b272008-04-28 11:06:42 -04001079 portstatus = portchange = 0;
1080 status = hub_port_status(hub, port1, &portstatus, &portchange);
Guenter Roeck181b0de2017-03-20 11:16:11 -07001081 if (status)
1082 goto abort;
1083
Alan Stern6ee0b272008-04-28 11:06:42 -04001084 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
Dan Williamsd99f6b42014-05-20 18:08:17 -07001085 dev_dbg(&port_dev->dev, "status %04x change %04x\n",
1086 portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001087
Dan Williamsd99f6b42014-05-20 18:08:17 -07001088 /*
1089 * After anything other than HUB_RESUME (i.e., initialization
Alan Stern6ee0b272008-04-28 11:06:42 -04001090 * or any sort of reset), every port should be disabled.
1091 * Unconnected ports should likewise be disabled (paranoia),
1092 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001093 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001094 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1095 type != HUB_RESUME ||
1096 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1097 !udev ||
1098 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001099 /*
1100 * USB3 protocol ports will automatically transition
1101 * to Enabled state when detect an USB3.0 device attach.
Dan Williamsfd1ac4c2013-10-07 11:58:20 -07001102 * Do not disable USB3 protocol ports, just pretend
1103 * power was lost
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001104 */
Dan Williamsfd1ac4c2013-10-07 11:58:20 -07001105 portstatus &= ~USB_PORT_STAT_ENABLE;
1106 if (!hub_is_superspeed(hdev))
Lan Tianyuad493e52013-01-23 04:26:30 +08001107 usb_clear_port_feature(hdev, port1,
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001108 USB_PORT_FEAT_ENABLE);
Alan Stern6ee0b272008-04-28 11:06:42 -04001109 }
1110
Alan Stern948fea32008-04-28 11:07:07 -04001111 /* Clear status-change flags; we'll debounce later */
1112 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1113 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001114 usb_clear_port_feature(hub->hdev, port1,
Alan Stern948fea32008-04-28 11:07:07 -04001115 USB_PORT_FEAT_C_CONNECTION);
1116 }
1117 if (portchange & USB_PORT_STAT_C_ENABLE) {
1118 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001119 usb_clear_port_feature(hub->hdev, port1,
Alan Stern948fea32008-04-28 11:07:07 -04001120 USB_PORT_FEAT_C_ENABLE);
1121 }
Julius Wernere92aee32013-10-15 17:45:00 -07001122 if (portchange & USB_PORT_STAT_C_RESET) {
1123 need_debounce_delay = true;
1124 usb_clear_port_feature(hub->hdev, port1,
1125 USB_PORT_FEAT_C_RESET);
1126 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001127 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1128 hub_is_superspeed(hub->hdev)) {
1129 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001130 usb_clear_port_feature(hub->hdev, port1,
Don Zickus79c3dd82011-11-03 09:07:18 -04001131 USB_PORT_FEAT_C_BH_PORT_RESET);
1132 }
Alan Stern253e0572009-10-27 15:20:13 -04001133 /* We can forget about a "removed" device when there's a
1134 * physical disconnect or the connect status changes.
1135 */
1136 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1137 (portchange & USB_PORT_STAT_C_CONNECTION))
1138 clear_bit(port1, hub->removed_bits);
1139
Alan Stern6ee0b272008-04-28 11:06:42 -04001140 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
Petr Mladek37ebb542014-09-19 17:32:23 +02001141 /* Tell hub_wq to disconnect the device or
Alan Stern6ee0b272008-04-28 11:06:42 -04001142 * check for a new connection
1143 */
Shen Guang08d1dec2014-01-08 14:45:42 +08001144 if (udev || (portstatus & USB_PORT_STAT_CONNECTION) ||
1145 (portstatus & USB_PORT_STAT_OVERCURRENT))
Alan Stern6ee0b272008-04-28 11:06:42 -04001146 set_bit(port1, hub->change_bits);
1147
1148 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001149 bool port_resumed = (portstatus &
1150 USB_PORT_STAT_LINK_STATE) ==
1151 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001152 /* The power session apparently survived the resume.
1153 * If there was an overcurrent or suspend change
Petr Mladek37ebb542014-09-19 17:32:23 +02001154 * (i.e., remote wakeup request), have hub_wq
Sarah Sharp72937e12012-01-24 11:46:50 -08001155 * take care of it. Look at the port link state
1156 * for USB 3.0 hubs, since they don't have a suspend
1157 * change bit, and they don't set the port link change
1158 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001159 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001160 if (portchange || (hub_is_superspeed(hub->hdev) &&
1161 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001162 set_bit(port1, hub->change_bits);
1163
1164 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001165#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001166 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001167#endif
Lan Tianyuad493e52013-01-23 04:26:30 +08001168 /* Don't set the change_bits when the device
1169 * was powered off.
1170 */
Dan Williamsd5c38342014-05-20 18:08:52 -07001171 if (test_bit(port1, hub->power_bits))
Lan Tianyuad493e52013-01-23 04:26:30 +08001172 set_bit(port1, hub->change_bits);
Alan Stern8808f002008-04-28 11:06:55 -04001173
Alan Stern6ee0b272008-04-28 11:06:42 -04001174 } else {
Petr Mladek37ebb542014-09-19 17:32:23 +02001175 /* The power session is gone; tell hub_wq */
Alan Stern6ee0b272008-04-28 11:06:42 -04001176 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1177 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001178 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001179 }
1180
Alan Stern948fea32008-04-28 11:07:07 -04001181 /* If no port-status-change flags were set, we don't need any
1182 * debouncing. If flags were set we can try to debounce the
Petr Mladek37ebb542014-09-19 17:32:23 +02001183 * ports all at once right now, instead of letting hub_wq do them
Alan Stern948fea32008-04-28 11:07:07 -04001184 * one at a time later on.
1185 *
Petr Mladek37ebb542014-09-19 17:32:23 +02001186 * If any port-status changes do occur during this delay, hub_wq
Alan Stern948fea32008-04-28 11:07:07 -04001187 * will see them later and handle them normally.
1188 */
Alan Stern8520f382008-09-22 14:44:26 -04001189 if (need_debounce_delay) {
1190 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001191
Alan Stern8520f382008-09-22 14:44:26 -04001192 /* Don't do a long sleep inside a workqueue routine */
1193 if (type == HUB_INIT2) {
Tejun Heo77fa83c2014-03-07 10:24:48 -05001194 INIT_DELAYED_WORK(&hub->init_work, hub_init_func3);
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08001195 queue_delayed_work(system_power_efficient_wq,
1196 &hub->init_work,
Alan Stern8520f382008-09-22 14:44:26 -04001197 msecs_to_jiffies(delay));
Alan Stern07d316a2016-08-05 11:51:30 -04001198 device_unlock(&hdev->dev);
Alan Stern8520f382008-09-22 14:44:26 -04001199 return; /* Continues at init3: below */
1200 } else {
1201 msleep(delay);
1202 }
1203 }
1204 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001205 hub->quiescing = 0;
1206
1207 status = usb_submit_urb(hub->urb, GFP_NOIO);
1208 if (status < 0)
1209 dev_err(hub->intfdev, "activate --> %d\n", status);
1210 if (hub->has_indicators && blinkenlights)
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08001211 queue_delayed_work(system_power_efficient_wq,
1212 &hub->leds, LED_CYCLE_PERIOD);
Alan Sternf2835212008-04-28 11:07:17 -04001213
1214 /* Scan all ports that need attention */
Petr Mladek32a695892014-09-19 17:32:21 +02001215 kick_hub_wq(hub);
Guenter Roeck181b0de2017-03-20 11:16:11 -07001216 abort:
Alan Sternca5cbc82016-08-05 11:49:45 -04001217 if (type == HUB_INIT2 || type == HUB_INIT3) {
1218 /* Allow autosuspend if it was suppressed */
1219 disconnected:
Alan Stern8e4ceb32009-12-07 13:01:37 -05001220 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern07d316a2016-08-05 11:51:30 -04001221 device_unlock(&hdev->dev);
Alan Sternca5cbc82016-08-05 11:49:45 -04001222 }
Alan Sterne50293e2015-12-16 13:32:38 -05001223
1224 kref_put(&hub->kref, hub_release);
Alan Stern5e6effa2008-03-03 15:15:51 -05001225}
1226
Alan Stern8520f382008-09-22 14:44:26 -04001227/* Implement the continuations for the delays above */
1228static void hub_init_func2(struct work_struct *ws)
1229{
1230 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1231
1232 hub_activate(hub, HUB_INIT2);
1233}
1234
1235static void hub_init_func3(struct work_struct *ws)
1236{
1237 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1238
1239 hub_activate(hub, HUB_INIT3);
1240}
1241
Alan Stern43303542008-04-28 11:07:31 -04001242enum hub_quiescing_type {
1243 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1244};
1245
1246static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1247{
1248 struct usb_device *hdev = hub->hdev;
1249 int i;
1250
Petr Mladek37ebb542014-09-19 17:32:23 +02001251 /* hub_wq and related activity won't re-trigger */
Alan Stern43303542008-04-28 11:07:31 -04001252 hub->quiescing = 1;
1253
1254 if (type != HUB_SUSPEND) {
1255 /* Disconnect all the children */
1256 for (i = 0; i < hdev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001257 if (hub->ports[i]->child)
1258 usb_disconnect(&hub->ports[i]->child);
Alan Stern43303542008-04-28 11:07:31 -04001259 }
1260 }
1261
Petr Mladek37ebb542014-09-19 17:32:23 +02001262 /* Stop hub_wq and related activity */
Alan Stern43303542008-04-28 11:07:31 -04001263 usb_kill_urb(hub->urb);
1264 if (hub->has_indicators)
1265 cancel_delayed_work_sync(&hub->leds);
1266 if (hub->tt.hub)
Octavian Purdila036546b2012-10-23 11:33:12 +03001267 flush_work(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001268}
1269
Alan Stern600856c2014-05-20 18:08:07 -07001270static void hub_pm_barrier_for_all_ports(struct usb_hub *hub)
1271{
1272 int i;
1273
1274 for (i = 0; i < hub->hdev->maxchild; ++i)
1275 pm_runtime_barrier(&hub->ports[i]->dev);
1276}
1277
Alan Stern3eb14912008-03-03 15:15:43 -05001278/* caller has locked the hub device */
1279static int hub_pre_reset(struct usb_interface *intf)
1280{
1281 struct usb_hub *hub = usb_get_intfdata(intf);
1282
Alan Stern43303542008-04-28 11:07:31 -04001283 hub_quiesce(hub, HUB_PRE_RESET);
Alan Stern600856c2014-05-20 18:08:07 -07001284 hub->in_reset = 1;
1285 hub_pm_barrier_for_all_ports(hub);
Alan Sternf07600c2007-05-30 15:38:16 -04001286 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001287}
1288
1289/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001290static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001291{
Alan Stern7de18d82006-06-01 13:37:24 -04001292 struct usb_hub *hub = usb_get_intfdata(intf);
1293
Alan Stern600856c2014-05-20 18:08:07 -07001294 hub->in_reset = 0;
1295 hub_pm_barrier_for_all_ports(hub);
Alan Sternf2835212008-04-28 11:07:17 -04001296 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001297 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001298}
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300static int hub_configure(struct usb_hub *hub,
1301 struct usb_endpoint_descriptor *endpoint)
1302{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001303 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 struct usb_device *hdev = hub->hdev;
1305 struct device *hub_dev = hub->intfdev;
1306 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001307 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 unsigned int pipe;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001309 int maxp, ret, i;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001310 char *message = "out of memory";
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001311 unsigned unit_load;
1312 unsigned full_load;
Dan Williamsd8521af2014-05-20 18:08:28 -07001313 unsigned maxchild;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Alan Sternd697cdd2009-10-27 15:18:46 -04001315 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 ret = -ENOMEM;
1318 goto fail;
1319 }
1320
1321 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1322 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 ret = -ENOMEM;
1324 goto fail;
1325 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001326 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Johan Hovoldc67e87a2017-05-10 18:18:28 +02001328 hub->descriptor = kzalloc(sizeof(*hub->descriptor), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 ret = -ENOMEM;
1331 goto fail;
1332 }
1333
1334 /* Request the entire hub descriptor.
1335 * hub->descriptor can handle USB_MAXCHILDREN ports,
Johan Hovold3e4a4e62017-05-10 18:18:27 +02001336 * but a (non-SS) hub can/will return fewer bytes here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 */
John Youndbe79bb2001-09-17 00:00:00 -07001338 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (ret < 0) {
1340 message = "can't read hub descriptor";
1341 goto fail;
Johan Hovold12bfbe12017-05-10 18:18:29 +02001342 }
1343
1344 maxchild = USB_MAXCHILDREN;
1345 if (hub_is_superspeed(hdev))
1346 maxchild = min_t(unsigned, maxchild, USB_SS_MAXPORTS);
1347
1348 if (hub->descriptor->bNbrPorts > maxchild) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 message = "hub has too many ports!";
1350 ret = -ENODEV;
1351 goto fail;
David Linares769d73682013-03-25 10:50:27 +00001352 } else if (hub->descriptor->bNbrPorts == 0) {
1353 message = "hub doesn't have any ports!";
1354 ret = -ENODEV;
1355 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357
Dan Williamsd8521af2014-05-20 18:08:28 -07001358 maxchild = hub->descriptor->bNbrPorts;
1359 dev_info(hub_dev, "%d port%s detected\n", maxchild,
1360 (maxchild == 1) ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Dan Williamsd8521af2014-05-20 18:08:28 -07001362 hub->ports = kzalloc(maxchild * sizeof(struct usb_port *), GFP_KERNEL);
Lan Tianyuff823c72012-09-05 13:44:32 +08001363 if (!hub->ports) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001364 ret = -ENOMEM;
1365 goto fail;
1366 }
1367
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001368 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001369 if (hub_is_superspeed(hdev)) {
1370 unit_load = 150;
1371 full_load = 900;
1372 } else {
1373 unit_load = 100;
1374 full_load = 500;
1375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
John Youndbe79bb2001-09-17 00:00:00 -07001377 /* FIXME for USB 3.0, skip for now */
1378 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1379 !(hub_is_superspeed(hdev))) {
Matthias Beyer469271f2013-10-10 23:41:27 +02001380 char portstr[USB_MAXCHILDREN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Dan Williamsd8521af2014-05-20 18:08:28 -07001382 for (i = 0; i < maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001383 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1385 ? 'F' : 'R';
Dan Williamsd8521af2014-05-20 18:08:28 -07001386 portstr[maxchild] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1388 } else
1389 dev_dbg(hub_dev, "standalone hub\n");
1390
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001391 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301392 case HUB_CHAR_COMMON_LPSM:
1393 dev_dbg(hub_dev, "ganged power switching\n");
1394 break;
1395 case HUB_CHAR_INDV_PORT_LPSM:
1396 dev_dbg(hub_dev, "individual port power switching\n");
1397 break;
1398 case HUB_CHAR_NO_LPSM:
1399 case HUB_CHAR_LPSM:
1400 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1401 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001404 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301405 case HUB_CHAR_COMMON_OCPM:
1406 dev_dbg(hub_dev, "global over-current protection\n");
1407 break;
1408 case HUB_CHAR_INDV_PORT_OCPM:
1409 dev_dbg(hub_dev, "individual port over-current protection\n");
1410 break;
1411 case HUB_CHAR_NO_OCPM:
1412 case HUB_CHAR_OCPM:
1413 dev_dbg(hub_dev, "no over-current protection\n");
1414 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
Chase Metzger17248562015-08-11 21:34:37 -07001417 spin_lock_init(&hub->tt.lock);
1418 INIT_LIST_HEAD(&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001419 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301421 case USB_HUB_PR_FS:
1422 break;
1423 case USB_HUB_PR_HS_SINGLE_TT:
1424 dev_dbg(hub_dev, "Single TT\n");
1425 hub->tt.hub = hdev;
1426 break;
1427 case USB_HUB_PR_HS_MULTI_TT:
1428 ret = usb_set_interface(hdev, 0, 1);
1429 if (ret == 0) {
1430 dev_dbg(hub_dev, "TT per port\n");
1431 hub->tt.multi = 1;
1432 } else
1433 dev_err(hub_dev, "Using single TT (err %d)\n",
1434 ret);
1435 hub->tt.hub = hdev;
1436 break;
1437 case USB_HUB_PR_SS:
1438 /* USB 3.0 hubs don't have a TT */
1439 break;
1440 default:
1441 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1442 hdev->descriptor.bDeviceProtocol);
1443 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001446 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001447 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
Matthias Beyer469271f2013-10-10 23:41:27 +02001448 case HUB_TTTT_8_BITS:
1449 if (hdev->descriptor.bDeviceProtocol != 0) {
1450 hub->tt.think_time = 666;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001451 dev_dbg(hub_dev, "TT requires at most %d "
1452 "FS bit times (%d ns)\n",
Matthias Beyer469271f2013-10-10 23:41:27 +02001453 8, hub->tt.think_time);
1454 }
1455 break;
1456 case HUB_TTTT_16_BITS:
1457 hub->tt.think_time = 666 * 2;
1458 dev_dbg(hub_dev, "TT requires at most %d "
1459 "FS bit times (%d ns)\n",
1460 16, hub->tt.think_time);
1461 break;
1462 case HUB_TTTT_24_BITS:
1463 hub->tt.think_time = 666 * 3;
1464 dev_dbg(hub_dev, "TT requires at most %d "
1465 "FS bit times (%d ns)\n",
1466 24, hub->tt.think_time);
1467 break;
1468 case HUB_TTTT_32_BITS:
1469 hub->tt.think_time = 666 * 4;
1470 dev_dbg(hub_dev, "TT requires at most %d "
1471 "FS bit times (%d ns)\n",
1472 32, hub->tt.think_time);
1473 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475
1476 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001477 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 hub->has_indicators = 1;
1479 dev_dbg(hub_dev, "Port indicators are supported\n");
1480 }
1481
1482 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1483 hub->descriptor->bPwrOn2PwrGood * 2);
1484
1485 /* power budgeting mostly matters with bus-powered hubs,
1486 * and battery-powered root hubs (may provide just 8 mA).
1487 */
1488 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern15b73362013-07-30 15:35:40 -04001489 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 message = "can't get hub status";
1491 goto fail;
1492 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001493 hcd = bus_to_hcd(hdev->bus);
Alan Stern7d35b922005-04-25 11:18:32 -04001494 if (hdev == hdev->bus->root_hub) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001495 if (hcd->power_budget > 0)
1496 hdev->bus_mA = hcd->power_budget;
1497 else
Dan Williamsd8521af2014-05-20 18:08:28 -07001498 hdev->bus_mA = full_load * maxchild;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001499 if (hdev->bus_mA >= full_load)
1500 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001501 else {
1502 hub->mA_per_port = hdev->bus_mA;
1503 hub->limited_power = 1;
1504 }
Alan Stern7d35b922005-04-25 11:18:32 -04001505 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001506 int remaining = hdev->bus_mA -
1507 hub->descriptor->bHubContrCurrent;
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1510 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001511 hub->limited_power = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Dan Williamsd8521af2014-05-20 18:08:28 -07001513 if (remaining < maxchild * unit_load)
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001514 dev_warn(hub_dev,
Alan Stern55c52712005-11-23 12:03:12 -05001515 "insufficient power available "
1516 "to use all downstream ports\n");
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001517 hub->mA_per_port = unit_load; /* 7.2.1 */
1518
Alan Stern55c52712005-11-23 12:03:12 -05001519 } else { /* Self-powered external hub */
1520 /* FIXME: What about battery-powered external hubs that
1521 * provide less current per port? */
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001522 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001523 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001524 if (hub->mA_per_port < full_load)
Alan Stern55c52712005-11-23 12:03:12 -05001525 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1526 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1529 if (ret < 0) {
1530 message = "can't get hub status";
1531 goto fail;
1532 }
1533
1534 /* local power status reports aren't always correct */
1535 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1536 dev_dbg(hub_dev, "local power source is %s\n",
1537 (hubstatus & HUB_STATUS_LOCAL_POWER)
1538 ? "lost (inactive)" : "good");
1539
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001540 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 dev_dbg(hub_dev, "%sover-current condition exists\n",
1542 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1543
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001544 /* set up the interrupt endpoint
1545 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1546 * bytes as USB2.0[11.12.3] says because some hubs are known
1547 * to send more data (and thus cause overflow). For root hubs,
1548 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1549 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1551 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1552
1553 if (maxp > sizeof(*hub->buffer))
1554 maxp = sizeof(*hub->buffer);
1555
1556 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1557 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 ret = -ENOMEM;
1559 goto fail;
1560 }
1561
1562 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1563 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 /* maybe cycle the hub leds */
1566 if (hub->has_indicators && blinkenlights)
Matthias Beyer469271f2013-10-10 23:41:27 +02001567 hub->indicator[0] = INDICATOR_CYCLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Dan Williamsd8521af2014-05-20 18:08:28 -07001569 mutex_lock(&usb_port_peer_mutex);
1570 for (i = 0; i < maxchild; i++) {
Krzysztof Mazure58547e2013-08-22 14:49:39 +02001571 ret = usb_hub_create_port_device(hub, i + 1);
1572 if (ret < 0) {
Lan Tianyufa2a9562012-09-05 13:44:31 +08001573 dev_err(hub->intfdev,
1574 "couldn't create port%d device.\n", i + 1);
Dan Williamsd8521af2014-05-20 18:08:28 -07001575 break;
Krzysztof Mazure58547e2013-08-22 14:49:39 +02001576 }
1577 }
Dan Williamsd8521af2014-05-20 18:08:28 -07001578 hdev->maxchild = i;
Dan Williamse3d10502014-06-17 16:16:32 -07001579 for (i = 0; i < hdev->maxchild; i++) {
1580 struct usb_port *port_dev = hub->ports[i];
1581
1582 pm_runtime_put(&port_dev->dev);
1583 }
1584
Dan Williamsd8521af2014-05-20 18:08:28 -07001585 mutex_unlock(&usb_port_peer_mutex);
1586 if (ret < 0)
1587 goto fail;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001588
Petr Mladek37ebb542014-09-19 17:32:23 +02001589 /* Update the HCD's internal representation of this hub before hub_wq
Dan Williamse3d95582014-06-05 14:23:04 -07001590 * starts getting port status changes for devices under the hub.
1591 */
1592 if (hcd->driver->update_hub_device) {
1593 ret = hcd->driver->update_hub_device(hcd, hdev,
1594 &hub->tt, GFP_KERNEL);
1595 if (ret < 0) {
1596 message = "can't update HCD hub info";
1597 goto fail;
1598 }
1599 }
1600
Lan Tianyud2123fd2013-01-21 22:18:00 +08001601 usb_hub_adjust_deviceremovable(hdev, hub->descriptor);
1602
Alan Sternf2835212008-04-28 11:07:17 -04001603 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return 0;
1605
1606fail:
Chase Metzger17248562015-08-11 21:34:37 -07001607 dev_err(hub_dev, "config failed, %s (err %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 message, ret);
1609 /* hub_disconnect() frees urb and descriptor */
1610 return ret;
1611}
1612
Alan Sterne8054852007-05-04 11:55:11 -04001613static void hub_release(struct kref *kref)
1614{
1615 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1616
Petr Mladek5d14f322014-09-19 17:32:19 +02001617 usb_put_dev(hub->hdev);
Alan Sterne8054852007-05-04 11:55:11 -04001618 usb_put_intf(to_usb_interface(hub->intfdev));
1619 kfree(hub);
1620}
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622static unsigned highspeed_hubs;
1623
1624static void hub_disconnect(struct usb_interface *intf)
1625{
Huajun Li88162302012-03-12 21:00:19 +08001626 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001627 struct usb_device *hdev = interface_to_usbdev(intf);
Alan Stern543d7782014-01-07 10:43:02 -05001628 int port1;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001629
Petr Mladek32a695892014-09-19 17:32:21 +02001630 /*
1631 * Stop adding new hub events. We do not want to block here and thus
1632 * will not try to remove any pending work item.
1633 */
Alan Sterne8054852007-05-04 11:55:11 -04001634 hub->disconnected = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Alan Stern7de18d82006-06-01 13:37:24 -04001636 /* Disconnect all children and quiesce the hub */
1637 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001638 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001639
Dan Williamsd8521af2014-05-20 18:08:28 -07001640 mutex_lock(&usb_port_peer_mutex);
1641
Alan Stern543d7782014-01-07 10:43:02 -05001642 /* Avoid races with recursively_mark_NOTATTACHED() */
1643 spin_lock_irq(&device_state_lock);
1644 port1 = hdev->maxchild;
1645 hdev->maxchild = 0;
1646 usb_set_intfdata(intf, NULL);
1647 spin_unlock_irq(&device_state_lock);
Alexander Shishkin1f2235b2012-09-12 14:48:31 +03001648
Alan Stern543d7782014-01-07 10:43:02 -05001649 for (; port1 > 0; --port1)
1650 usb_hub_remove_port_device(hub, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Dan Williamsd8521af2014-05-20 18:08:28 -07001652 mutex_unlock(&usb_port_peer_mutex);
1653
Alan Sterne8054852007-05-04 11:55:11 -04001654 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 highspeed_hubs--;
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 usb_free_urb(hub->urb);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001658 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001659 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001660 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001661 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Lan Tianyu971fcd42013-01-23 04:26:29 +08001663 pm_suspend_ignore_children(&intf->dev, false);
Alan Sterne8054852007-05-04 11:55:11 -04001664 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
1667static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1668{
1669 struct usb_host_interface *desc;
1670 struct usb_endpoint_descriptor *endpoint;
1671 struct usb_device *hdev;
1672 struct usb_hub *hub;
1673
1674 desc = intf->cur_altsetting;
1675 hdev = interface_to_usbdev(intf);
1676
Ming Lei596d7892012-10-24 11:59:25 +08001677 /*
1678 * Set default autosuspend delay as 0 to speedup bus suspend,
1679 * based on the below considerations:
1680 *
1681 * - Unlike other drivers, the hub driver does not rely on the
1682 * autosuspend delay to provide enough time to handle a wakeup
1683 * event, and the submitted status URB is just to check future
1684 * change on hub downstream ports, so it is safe to do it.
1685 *
1686 * - The patch might cause one or more auto supend/resume for
1687 * below very rare devices when they are plugged into hub
1688 * first time:
1689 *
1690 * devices having trouble initializing, and disconnect
1691 * themselves from the bus and then reconnect a second
1692 * or so later
1693 *
1694 * devices just for downloading firmware, and disconnects
1695 * themselves after completing it
1696 *
1697 * For these quite rare devices, their drivers may change the
1698 * autosuspend delay of their parent hub in the probe() to one
1699 * appropriate value to avoid the subtle problem if someone
1700 * does care it.
1701 *
1702 * - The patch may cause one or more auto suspend/resume on
1703 * hub during running 'lsusb', but it is probably too
1704 * infrequent to worry about.
1705 *
1706 * - Change autosuspend delay of hub can avoid unnecessary auto
1707 * suspend timer for hub, also may decrease power consumption
1708 * of USB bus.
Roger Quadrosbdd405d2014-08-04 12:44:46 +03001709 *
1710 * - If user has indicated to prevent autosuspend by passing
1711 * usbcore.autosuspend = -1 then keep autosuspend disabled.
Ming Lei596d7892012-10-24 11:59:25 +08001712 */
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01001713#ifdef CONFIG_PM
Roger Quadrosbdd405d2014-08-04 12:44:46 +03001714 if (hdev->dev.power.autosuspend_delay >= 0)
1715 pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
Greg Kroah-Hartmana9ef8032014-08-27 16:55:29 -07001716#endif
Ming Lei596d7892012-10-24 11:59:25 +08001717
Alan Stern8ef42dd2014-05-23 10:45:54 -04001718 /*
1719 * Hubs have proper suspend/resume support, except for root hubs
1720 * where the controller driver doesn't have bus_suspend and
1721 * bus_resume methods.
1722 */
1723 if (hdev->parent) { /* normal device */
1724 usb_enable_autosuspend(hdev);
1725 } else { /* root hub */
1726 const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver;
1727
1728 if (drv->bus_suspend && drv->bus_resume)
1729 usb_enable_autosuspend(hdev);
1730 }
Alan Stern088f7fe2010-01-08 12:56:54 -05001731
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001732 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001733 dev_err(&intf->dev,
1734 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001735 return -E2BIG;
1736 }
1737
David Brownell89ccbdc2006-04-02 10:18:09 -08001738#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1739 if (hdev->parent) {
1740 dev_warn(&intf->dev, "ignoring external hub\n");
1741 return -ENODEV;
1742 }
1743#endif
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 /* Some hubs have a subclass of 1, which AFAICT according to the */
1746 /* specs is not defined, but it works */
1747 if ((desc->desc.bInterfaceSubClass != 0) &&
1748 (desc->desc.bInterfaceSubClass != 1)) {
1749descriptor_error:
Chase Metzger17248562015-08-11 21:34:37 -07001750 dev_err(&intf->dev, "bad descriptor, ignoring hub\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return -EIO;
1752 }
1753
1754 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1755 if (desc->desc.bNumEndpoints != 1)
1756 goto descriptor_error;
1757
1758 endpoint = &desc->endpoint[0].desc;
1759
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001760 /* If it's not an interrupt in endpoint, we'd better punt! */
1761 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 goto descriptor_error;
1763
1764 /* We found a hub */
Chase Metzger17248562015-08-11 21:34:37 -07001765 dev_info(&intf->dev, "USB hub found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001767 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Wolfram Sangb74e7062016-08-25 19:38:59 +02001768 if (!hub)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Alan Sterne8054852007-05-04 11:55:11 -04001771 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 hub->intfdev = &intf->dev;
1773 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001774 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001775 INIT_DELAYED_WORK(&hub->init_work, NULL);
Petr Mladek32a695892014-09-19 17:32:21 +02001776 INIT_WORK(&hub->events, hub_event);
Alan Sterne8054852007-05-04 11:55:11 -04001777 usb_get_intf(intf);
Petr Mladek5d14f322014-09-19 17:32:19 +02001778 usb_get_dev(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Chase Metzger17248562015-08-11 21:34:37 -07001780 usb_set_intfdata(intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001781 intf->needs_remote_wakeup = 1;
Lan Tianyu971fcd42013-01-23 04:26:29 +08001782 pm_suspend_ignore_children(&intf->dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 if (hdev->speed == USB_SPEED_HIGH)
1785 highspeed_hubs++;
1786
Ming Leie6f30de2012-10-24 11:59:24 +08001787 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1788 hub->quirk_check_port_auto_suspend = 1;
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (hub_configure(hub, endpoint) >= 0)
1791 return 0;
1792
Chase Metzger17248562015-08-11 21:34:37 -07001793 hub_disconnect(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return -ENODEV;
1795}
1796
1797static int
1798hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1799{
Chase Metzger17248562015-08-11 21:34:37 -07001800 struct usb_device *hdev = interface_to_usbdev(intf);
Lan Tianyuad493e52013-01-23 04:26:30 +08001801 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 /* assert ifno == 0 (part of hub spec) */
1804 switch (code) {
1805 case USBDEVFS_HUB_PORTINFO: {
1806 struct usbdevfs_hub_portinfo *info = user_data;
1807 int i;
1808
1809 spin_lock_irq(&device_state_lock);
1810 if (hdev->devnum <= 0)
1811 info->nports = 0;
1812 else {
1813 info->nports = hdev->maxchild;
1814 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001815 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 info->port[i] = 0;
1817 else
1818 info->port[i] =
Lan Tianyuff823c72012-09-05 13:44:32 +08001819 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
1821 }
1822 spin_unlock_irq(&device_state_lock);
1823
1824 return info->nports + 1;
1825 }
1826
1827 default:
1828 return -ENOSYS;
1829 }
1830}
1831
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001832/*
1833 * Allow user programs to claim ports on a hub. When a device is attached
1834 * to one of these "claimed" ports, the program will "own" the device.
1835 */
1836static int find_port_owner(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001837 struct usb_dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001838{
Mathias Nyman41341262013-06-18 17:28:48 +03001839 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
1840
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001841 if (hdev->state == USB_STATE_NOTATTACHED)
1842 return -ENODEV;
1843 if (port1 == 0 || port1 > hdev->maxchild)
1844 return -EINVAL;
1845
Mathias Nyman41341262013-06-18 17:28:48 +03001846 /* Devices not managed by the hub driver
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001847 * will always have maxchild equal to 0.
1848 */
Mathias Nyman41341262013-06-18 17:28:48 +03001849 *ppowner = &(hub->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001850 return 0;
1851}
1852
1853/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001854int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001855 struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001856{
1857 int rc;
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001858 struct usb_dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001859
1860 rc = find_port_owner(hdev, port1, &powner);
1861 if (rc)
1862 return rc;
1863 if (*powner)
1864 return -EBUSY;
1865 *powner = owner;
1866 return rc;
1867}
Valentina Manea6080cd02014-03-08 14:53:34 +02001868EXPORT_SYMBOL_GPL(usb_hub_claim_port);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001869
Lan Tianyu336c5c32012-07-06 14:13:52 +08001870int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001871 struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001872{
1873 int rc;
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001874 struct usb_dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001875
1876 rc = find_port_owner(hdev, port1, &powner);
1877 if (rc)
1878 return rc;
1879 if (*powner != owner)
1880 return -ENOENT;
1881 *powner = NULL;
1882 return rc;
1883}
Valentina Manea6080cd02014-03-08 14:53:34 +02001884EXPORT_SYMBOL_GPL(usb_hub_release_port);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001885
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001886void usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001887{
Lan Tianyuad493e52013-01-23 04:26:30 +08001888 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001889 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001890
Lan Tianyufa2a9562012-09-05 13:44:31 +08001891 for (n = 0; n < hdev->maxchild; n++) {
1892 if (hub->ports[n]->port_owner == owner)
1893 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001894 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001895
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001896}
1897
1898/* The caller must hold udev's lock */
1899bool usb_device_is_owned(struct usb_device *udev)
1900{
1901 struct usb_hub *hub;
1902
1903 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1904 return false;
Lan Tianyuad493e52013-01-23 04:26:30 +08001905 hub = usb_hub_to_struct_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001906 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001907}
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1910{
Lan Tianyuad493e52013-01-23 04:26:30 +08001911 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 int i;
1913
1914 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001915 if (hub->ports[i]->child)
1916 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001918 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001919 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 udev->state = USB_STATE_NOTATTACHED;
1921}
1922
1923/**
1924 * usb_set_device_state - change a device's current state (usbcore, hcds)
1925 * @udev: pointer to device whose state should be changed
1926 * @new_state: new state value to be stored
1927 *
1928 * udev->state is _not_ fully protected by the device lock. Although
1929 * most transitions are made only while holding the lock, the state can
1930 * can change to USB_STATE_NOTATTACHED at almost any time. This
1931 * is so that devices can be marked as disconnected as soon as possible,
1932 * without having to wait for any semaphores to be released. As a result,
1933 * all changes to any device's state must be protected by the
1934 * device_state_lock spinlock.
1935 *
1936 * Once a device has been added to the device tree, all changes to its state
1937 * should be made using this routine. The state should _not_ be set directly.
1938 *
1939 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1940 * Otherwise udev->state is set to new_state, and if new_state is
1941 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1942 * to USB_STATE_NOTATTACHED.
1943 */
1944void usb_set_device_state(struct usb_device *udev,
1945 enum usb_device_state new_state)
1946{
1947 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001948 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 spin_lock_irqsave(&device_state_lock, flags);
1951 if (udev->state == USB_STATE_NOTATTACHED)
1952 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001953 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001954
1955 /* root hub wakeup capabilities are managed out-of-band
1956 * and may involve silicon errata ... ignore them here.
1957 */
1958 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001959 if (udev->state == USB_STATE_SUSPENDED
1960 || new_state == USB_STATE_SUSPENDED)
1961 ; /* No change to wakeup settings */
1962 else if (new_state == USB_STATE_CONFIGURED)
Lu Baoluddbe1fc2014-09-19 10:13:50 +08001963 wakeup = (udev->quirks &
1964 USB_QUIRK_IGNORE_REMOTE_WAKEUP) ? 0 :
1965 udev->actconfig->desc.bmAttributes &
1966 USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001967 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001968 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001969 }
Sarah Sharp15123002007-12-21 16:54:15 -08001970 if (udev->state == USB_STATE_SUSPENDED &&
1971 new_state != USB_STATE_SUSPENDED)
1972 udev->active_duration -= jiffies;
1973 else if (new_state == USB_STATE_SUSPENDED &&
1974 udev->state != USB_STATE_SUSPENDED)
1975 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001976 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001977 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 recursively_mark_NOTATTACHED(udev);
1979 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001980 if (wakeup >= 0)
1981 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
David Vrabel6da9c992009-02-18 14:43:47 +00001983EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001985/*
Alan Stern3b29b682011-02-22 09:53:41 -05001986 * Choose a device number.
1987 *
1988 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1989 * USB-2.0 buses they are also used as device addresses, however on
1990 * USB-3.0 buses the address is assigned by the controller hardware
1991 * and it usually is not the same as the device number.
1992 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001993 * WUSB devices are simple: they have no hubs behind, so the mapping
1994 * device <-> virtual port number becomes 1:1. Why? to simplify the
1995 * life of the device connection logic in
1996 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1997 * handshake we need to assign a temporary address in the unauthorized
1998 * space. For simplicity we use the first virtual port number found to
1999 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
2000 * and that becomes it's address [X < 128] or its unauthorized address
2001 * [X | 0x80].
2002 *
2003 * We add 1 as an offset to the one-based USB-stack port number
2004 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
2005 * 0 is reserved by USB for default address; (b) Linux's USB stack
2006 * uses always #1 for the root hub of the controller. So USB stack's
2007 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07002008 *
2009 * Devices connected under xHCI are not as simple. The host controller
2010 * supports virtualization, so the hardware assigns device addresses and
2011 * the HCD must setup data structures before issuing a set address
2012 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002013 */
Alan Stern3b29b682011-02-22 09:53:41 -05002014static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
2016 int devnum;
2017 struct usb_bus *bus = udev->bus;
2018
Petr Mladek638139e2014-09-19 17:32:24 +02002019 /* be safe when more hub events are proceed in parallel */
Chris Bainbridgefeb26ac2016-04-25 13:48:38 +01002020 mutex_lock(&bus->devnum_next_mutex);
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002021 if (udev->wusb) {
2022 devnum = udev->portnum + 1;
2023 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
2024 } else {
2025 /* Try to allocate the next devnum beginning at
2026 * bus->devnum_next. */
2027 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
2028 bus->devnum_next);
2029 if (devnum >= 128)
2030 devnum = find_next_zero_bit(bus->devmap.devicemap,
2031 128, 1);
Matthias Beyer469271f2013-10-10 23:41:27 +02002032 bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1);
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (devnum < 128) {
2035 set_bit(devnum, bus->devmap.devicemap);
2036 udev->devnum = devnum;
2037 }
Chris Bainbridgefeb26ac2016-04-25 13:48:38 +01002038 mutex_unlock(&bus->devnum_next_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
Alan Stern3b29b682011-02-22 09:53:41 -05002041static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 if (udev->devnum > 0) {
2044 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
2045 udev->devnum = -1;
2046 }
2047}
2048
Alan Stern3b29b682011-02-22 09:53:41 -05002049static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07002050{
2051 /* The address for a WUSB device is managed by wusbcore. */
2052 if (!udev->wusb)
2053 udev->devnum = devnum;
2054}
2055
Herbert Xuf7410ce2010-01-10 20:15:03 +11002056static void hub_free_dev(struct usb_device *udev)
2057{
2058 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2059
2060 /* Root hubs aren't real devices, so don't free HCD resources */
2061 if (hcd->driver->free_dev && udev->parent)
2062 hcd->driver->free_dev(hcd, udev);
2063}
2064
Dan Williams7027df32014-05-20 18:09:36 -07002065static void hub_disconnect_children(struct usb_device *udev)
2066{
2067 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
2068 int i;
2069
2070 /* Free up all the children before we remove this device */
2071 for (i = 0; i < udev->maxchild; i++) {
2072 if (hub->ports[i]->child)
2073 usb_disconnect(&hub->ports[i]->child);
2074 }
2075}
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077/**
2078 * usb_disconnect - disconnect a device (usbcore-internal)
2079 * @pdev: pointer to device being disconnected
2080 * Context: !in_interrupt ()
2081 *
2082 * Something got disconnected. Get rid of it and all of its children.
2083 *
2084 * If *pdev is a normal device then the parent hub must already be locked.
Heiner Kallweita4b5d602016-02-03 23:35:23 +01002085 * If *pdev is a root hub then the caller must hold the usb_bus_idr_lock,
Bjorn Helgaasdb8f2aa2013-09-13 13:57:34 -06002086 * which protects the set of root hubs as well as the list of buses.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 *
2088 * Only hub drivers (including virtual root hub drivers for host
2089 * controllers) should ever call this.
2090 *
2091 * This call is synchronous, and may not be used in an interrupt context.
2092 */
2093void usb_disconnect(struct usb_device **pdev)
2094{
Dan Williams7027df32014-05-20 18:09:36 -07002095 struct usb_port *port_dev = NULL;
2096 struct usb_device *udev = *pdev;
Peter Chen5b1dc202014-08-19 08:56:21 +08002097 struct usb_hub *hub = NULL;
2098 int port1 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 /* mark the device as inactive, so any further urb submissions for
2101 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002102 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 */
2104 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05002105 dev_info(&udev->dev, "USB disconnect, device number %d\n",
2106 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Guenter Roeck219a99d2017-03-20 14:30:50 -07002108 /*
2109 * Ensure that the pm runtime code knows that the USB device
2110 * is in the process of being disconnected.
2111 */
2112 pm_runtime_barrier(&udev->dev);
2113
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002114 usb_lock_device(udev);
2115
Dan Williams7027df32014-05-20 18:09:36 -07002116 hub_disconnect_children(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 /* deallocate hcd/hardware state ... nuking all pending urbs and
2119 * cleaning up all state associated with the current configuration
2120 * so that the hardware is now fully quiesced.
2121 */
Chase Metzger17248562015-08-11 21:34:37 -07002122 dev_dbg(&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04002124 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Lan Tianyufde26382013-01-20 01:53:33 +08002126 if (udev->parent) {
Dan Williams7027df32014-05-20 18:09:36 -07002127 port1 = udev->portnum;
2128 hub = usb_hub_to_struct_hub(udev->parent);
2129 port_dev = hub->ports[port1 - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002130
2131 sysfs_remove_link(&udev->dev.kobj, "port");
2132 sysfs_remove_link(&port_dev->dev.kobj, "device");
Lan Tianyu971fcd42013-01-23 04:26:29 +08002133
Dan Williams7027df32014-05-20 18:09:36 -07002134 /*
2135 * As usb_port_runtime_resume() de-references udev, make
2136 * sure no resumes occur during removal
2137 */
2138 if (!test_and_set_bit(port1, hub->child_usage_bits))
2139 pm_runtime_get_sync(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002140 }
2141
Alan Stern3b23dd62008-12-05 14:10:34 -05002142 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04002143 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07002144
Alan Stern782da722006-07-01 22:09:35 -04002145 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002146 * for de-configuring the device and invoking the remove-device
2147 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002148 */
2149 device_del(&udev->dev);
2150
2151 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 * (or root_hub) pointer.
2153 */
Alan Stern3b29b682011-02-22 09:53:41 -05002154 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 /* Avoid races with recursively_mark_NOTATTACHED() */
2157 spin_lock_irq(&device_state_lock);
2158 *pdev = NULL;
2159 spin_unlock_irq(&device_state_lock);
2160
Dan Williams7027df32014-05-20 18:09:36 -07002161 if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits))
2162 pm_runtime_put(&port_dev->dev);
2163
Herbert Xuf7410ce2010-01-10 20:15:03 +11002164 hub_free_dev(udev);
2165
Alan Stern782da722006-07-01 22:09:35 -04002166 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002169#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170static void show_string(struct usb_device *udev, char *id, char *string)
2171{
2172 if (!string)
2173 return;
Joe Perchesf2ec5222012-10-28 01:05:51 -07002174 dev_info(&udev->dev, "%s: %s\n", id, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002177static void announce_device(struct usb_device *udev)
2178{
2179 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2180 le16_to_cpu(udev->descriptor.idVendor),
2181 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002182 dev_info(&udev->dev,
2183 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002184 udev->descriptor.iManufacturer,
2185 udev->descriptor.iProduct,
2186 udev->descriptor.iSerialNumber);
2187 show_string(udev, "Product", udev->product);
2188 show_string(udev, "Manufacturer", udev->manufacturer);
2189 show_string(udev, "SerialNumber", udev->serial);
2190}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002192static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193#endif
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Oliver Neukum3ede7602007-01-11 14:35:50 +01002196/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002197 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002198 * @udev: newly addressed device (in ADDRESS state)
2199 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002200 * Finish enumeration for On-The-Go devices
Yacine Belkadi626f0902013-08-02 20:10:04 +02002201 *
2202 * Return: 0 if successful. A negative error code otherwise.
Oliver Neukum3ede7602007-01-11 14:35:50 +01002203 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002204static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002206 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208#ifdef CONFIG_USB_OTG
2209 /*
2210 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2211 * to wake us after we've powered off VBUS; and HNP, switching roles
2212 * "host" to "peripheral". The OTG descriptor helps figure this out.
2213 */
2214 if (!udev->bus->is_b_host
2215 && udev->config
2216 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002217 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 struct usb_bus *bus = udev->bus;
Li Jun7d2d6412015-08-31 16:20:49 +08002219 unsigned port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 /* descriptor may appear anywhere in config */
Li Jun7d2d6412015-08-31 16:20:49 +08002222 err = __usb_get_extra_descriptor(udev->rawdescriptors[0],
2223 le16_to_cpu(udev->config[0].desc.wTotalLength),
2224 USB_DT_OTG, (void **) &desc);
2225 if (err || !(desc->bmAttributes & USB_OTG_HNP))
2226 return 0;
David Brownellfc849b82006-09-18 16:53:26 -07002227
Li Jun7d2d6412015-08-31 16:20:49 +08002228 dev_info(&udev->dev, "Dual-Role OTG device on %sHNP port\n",
2229 (port1 == bus->otg_port) ? "" : "non-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Li Jun7d2d6412015-08-31 16:20:49 +08002231 /* enable HNP before suspend, it's simpler */
2232 if (port1 == bus->otg_port) {
2233 bus->b_hnp_enable = 1;
2234 err = usb_control_msg(udev,
2235 usb_sndctrlpipe(udev, 0),
2236 USB_REQ_SET_FEATURE, 0,
2237 USB_DEVICE_B_HNP_ENABLE,
2238 0, NULL, 0,
2239 USB_CTRL_SET_TIMEOUT);
2240 if (err < 0) {
2241 /*
2242 * OTG MESSAGE: report errors here,
2243 * customize to match your product.
2244 */
2245 dev_err(&udev->dev, "can't set HNP mode: %d\n",
2246 err);
2247 bus->b_hnp_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
Li Jun7d2d6412015-08-31 16:20:49 +08002249 } else if (desc->bLength == sizeof
2250 (struct usb_otg_descriptor)) {
2251 /* Set a_alt_hnp_support for legacy otg device */
2252 err = usb_control_msg(udev,
2253 usb_sndctrlpipe(udev, 0),
2254 USB_REQ_SET_FEATURE, 0,
2255 USB_DEVICE_A_ALT_HNP_SUPPORT,
2256 0, NULL, 0,
2257 USB_CTRL_SET_TIMEOUT);
2258 if (err < 0)
2259 dev_err(&udev->dev,
2260 "set a_alt_hnp_support failed: %d\n",
2261 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
2263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002265 return err;
2266}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002268
2269/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002270 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002271 * @udev: newly addressed device (in ADDRESS state)
2272 *
2273 * This is only called by usb_new_device() and usb_authorize_device()
2274 * and FIXME -- all comments that apply to them apply here wrt to
2275 * environment.
2276 *
2277 * If the device is WUSB and not authorized, we don't attempt to read
2278 * the string descriptors, as they will be errored out by the device
2279 * until it has been authorized.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002280 *
2281 * Return: 0 if successful. A negative error code otherwise.
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002282 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002283static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002284{
2285 int err;
Peter Chen026f3fc2014-08-19 09:51:53 +08002286 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002287
2288 if (udev->config == NULL) {
2289 err = usb_get_configuration(udev);
2290 if (err < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04002291 if (err != -ENODEV)
2292 dev_err(&udev->dev, "can't read configurations, error %d\n",
2293 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002294 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002295 }
2296 }
Thomas Pugliese83e83ec2013-12-09 13:40:29 -06002297
2298 /* read the standard strings and cache them if present */
2299 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2300 udev->manufacturer = usb_cache_string(udev,
2301 udev->descriptor.iManufacturer);
2302 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2303
Alan Stern8d8558d2009-12-08 15:50:41 -05002304 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002305 if (err < 0)
2306 return err;
2307
Peter Chen026f3fc2014-08-19 09:51:53 +08002308 if (IS_ENABLED(CONFIG_USB_OTG_WHITELIST) && hcd->tpl_support &&
Peter Chene5a9d622014-09-29 10:09:31 +08002309 !is_targeted(udev)) {
Peter Chen026f3fc2014-08-19 09:51:53 +08002310 /* Maybe it can talk to us, though we can't talk to it.
2311 * (Includes HNP test device.)
2312 */
Peter Chene5a9d622014-09-29 10:09:31 +08002313 if (IS_ENABLED(CONFIG_USB_OTG) && (udev->bus->b_hnp_enable
2314 || udev->bus->is_b_host)) {
Peter Chen026f3fc2014-08-19 09:51:53 +08002315 err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND);
2316 if (err < 0)
2317 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2318 }
2319 return -ENOTSUPP;
2320 }
2321
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002322 usb_detect_interface_quirks(udev);
2323
2324 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002325}
2326
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002327static void set_usb_port_removable(struct usb_device *udev)
2328{
2329 struct usb_device *hdev = udev->parent;
2330 struct usb_hub *hub;
2331 u8 port = udev->portnum;
2332 u16 wHubCharacteristics;
2333 bool removable = true;
2334
2335 if (!hdev)
2336 return;
2337
Lan Tianyuad493e52013-01-23 04:26:30 +08002338 hub = usb_hub_to_struct_hub(udev->parent);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002339
Matthew Garrett92bfbf72015-04-08 16:36:00 -07002340 /*
2341 * If the platform firmware has provided information about a port,
2342 * use that to determine whether it's removable.
2343 */
2344 switch (hub->ports[udev->portnum - 1]->connect_type) {
2345 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
2346 udev->removable = USB_DEVICE_REMOVABLE;
2347 return;
2348 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
Matthew Garrett58339c22015-04-08 16:36:01 -07002349 case USB_PORT_NOT_USED:
Matthew Garrett92bfbf72015-04-08 16:36:00 -07002350 udev->removable = USB_DEVICE_FIXED;
2351 return;
Matthew Garrett58339c22015-04-08 16:36:01 -07002352 default:
2353 break;
Matthew Garrett92bfbf72015-04-08 16:36:00 -07002354 }
2355
2356 /*
2357 * Otherwise, check whether the hub knows whether a port is removable
2358 * or not
2359 */
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002360 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2361
2362 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2363 return;
2364
2365 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002366 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2367 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002368 removable = false;
2369 } else {
2370 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2371 removable = false;
2372 }
2373
2374 if (removable)
2375 udev->removable = USB_DEVICE_REMOVABLE;
2376 else
2377 udev->removable = USB_DEVICE_FIXED;
Dan Williamsa4204ff2014-05-20 18:08:22 -07002378
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002379}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002380
2381/**
2382 * usb_new_device - perform initial device setup (usbcore-internal)
2383 * @udev: newly addressed device (in ADDRESS state)
2384 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002385 * This is called with devices which have been detected but not fully
2386 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002387 * for any device configuration. The caller must have locked either
2388 * the parent hub (if udev is a normal device) or else the
Heiner Kallweita4b5d602016-02-03 23:35:23 +01002389 * usb_bus_idr_lock (if udev is a root hub). The parent's pointer to
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002390 * udev has already been installed, but udev is not yet visible through
2391 * sysfs or other filesystem code.
2392 *
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002393 * This call is synchronous, and may not be used in an interrupt context.
2394 *
2395 * Only the hub driver or root-hub registrar should ever call this.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002396 *
2397 * Return: Whether the device is configured properly or not. Zero if the
2398 * interface was registered with the driver core; else a negative errno
2399 * value.
2400 *
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002401 */
2402int usb_new_device(struct usb_device *udev)
2403{
2404 int err;
2405
Dan Streetman16985402010-01-06 09:56:53 -05002406 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002407 /* Initialize non-root-hub device wakeup to disabled;
2408 * device (un)configuration controls wakeup capable
2409 * sysfs power/wakeup controls wakeup enabled/disabled
2410 */
2411 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002412 }
2413
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002414 /* Tell the runtime-PM framework the device is active */
2415 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002416 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002417 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002418 pm_runtime_enable(&udev->dev);
2419
Alan Sternc08512c2010-11-15 15:57:58 -05002420 /* By default, forbid autosuspend for all devices. It will be
2421 * allowed for hubs during binding.
2422 */
2423 usb_disable_autosuspend(udev);
2424
Alan Stern8d8558d2009-12-08 15:50:41 -05002425 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002426 if (err < 0)
2427 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002428 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2429 udev->devnum, udev->bus->busnum,
2430 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002431 /* export the usbdev device-node for libusb */
2432 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2433 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2434
Alan Stern6cd13202008-11-13 15:08:30 -05002435 /* Tell the world! */
2436 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002437
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002438 if (udev->serial)
2439 add_device_randomness(udev->serial, strlen(udev->serial));
2440 if (udev->product)
2441 add_device_randomness(udev->product, strlen(udev->product));
2442 if (udev->manufacturer)
2443 add_device_randomness(udev->manufacturer,
2444 strlen(udev->manufacturer));
2445
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002446 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002447
Dan Williamsa4204ff2014-05-20 18:08:22 -07002448 /* check whether the hub or firmware marks this port as non-removable */
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002449 if (udev->parent)
2450 set_usb_port_removable(udev);
2451
Alan Stern782da722006-07-01 22:09:35 -04002452 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002453 * for configuring the device and invoking the add-device
2454 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002455 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002456 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (err) {
2458 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2459 goto fail;
2460 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002461
Lan Tianyufde26382013-01-20 01:53:33 +08002462 /* Create link files between child device and usb port device. */
2463 if (udev->parent) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002464 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
Dan Williamsd5c38342014-05-20 18:08:52 -07002465 int port1 = udev->portnum;
2466 struct usb_port *port_dev = hub->ports[port1 - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002467
2468 err = sysfs_create_link(&udev->dev.kobj,
2469 &port_dev->dev.kobj, "port");
2470 if (err)
2471 goto fail;
2472
2473 err = sysfs_create_link(&port_dev->dev.kobj,
2474 &udev->dev.kobj, "device");
2475 if (err) {
2476 sysfs_remove_link(&udev->dev.kobj, "port");
2477 goto fail;
2478 }
Lan Tianyu971fcd42013-01-23 04:26:29 +08002479
Dan Williamsd5c38342014-05-20 18:08:52 -07002480 if (!test_and_set_bit(port1, hub->child_usage_bits))
2481 pm_runtime_get_sync(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002482 }
2483
Alan Stern3b23dd62008-12-05 14:10:34 -05002484 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002485 usb_mark_last_busy(udev);
2486 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002487 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489fail:
2490 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002491 pm_runtime_disable(&udev->dev);
2492 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002493 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002496
2497/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002498 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2499 * @usb_dev: USB device
2500 *
2501 * Move the USB device to a very basic state where interfaces are disabled
2502 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002503 *
2504 * We share a lock (that we have) with device_del(), so we need to
2505 * defer its call.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002506 *
2507 * Return: 0.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002508 */
2509int usb_deauthorize_device(struct usb_device *usb_dev)
2510{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002511 usb_lock_device(usb_dev);
2512 if (usb_dev->authorized == 0)
2513 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002514
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002515 usb_dev->authorized = 0;
2516 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002517
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002518out_unauthorized:
2519 usb_unlock_device(usb_dev);
2520 return 0;
2521}
2522
2523
2524int usb_authorize_device(struct usb_device *usb_dev)
2525{
2526 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002527
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002528 usb_lock_device(usb_dev);
2529 if (usb_dev->authorized == 1)
2530 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002531
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002532 result = usb_autoresume_device(usb_dev);
2533 if (result < 0) {
2534 dev_err(&usb_dev->dev,
2535 "can't autoresume for authorization: %d\n", result);
2536 goto error_autoresume;
2537 }
Josef Gajduseke50a3222014-10-09 15:47:54 +02002538
2539 if (usb_dev->wusb) {
2540 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2541 if (result < 0) {
2542 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2543 "authorization: %d\n", result);
2544 goto error_device_descriptor;
2545 }
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002546 }
Alan Sternda307122009-12-08 15:54:44 -05002547
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002548 usb_dev->authorized = 1;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002549 /* Choose and set the configuration. This registers the interfaces
2550 * with the driver core and lets interface drivers bind to them.
2551 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002552 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002553 if (c >= 0) {
2554 result = usb_set_configuration(usb_dev, c);
2555 if (result) {
2556 dev_err(&usb_dev->dev,
2557 "can't set config #%d, error %d\n", c, result);
2558 /* This need not be fatal. The user can try to
2559 * set other configurations. */
2560 }
2561 }
2562 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002563
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002564error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002565 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002566error_autoresume:
2567out_authorized:
Matthias Beyer781b2132013-10-10 23:41:29 +02002568 usb_unlock_device(usb_dev); /* complements locktree */
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002569 return result;
2570}
2571
Mathias Nyman0cdd49a2015-12-10 09:59:29 +02002572/*
2573 * Return 1 if port speed is SuperSpeedPlus, 0 otherwise
2574 * check it from the link protocol field of the current speed ID attribute.
2575 * current speed ID is got from ext port status request. Sublink speed attribute
2576 * table is returned with the hub BOS SSP device capability descriptor
2577 */
2578static int port_speed_is_ssp(struct usb_device *hdev, int speed_id)
2579{
2580 int ssa_count;
2581 u32 ss_attr;
2582 int i;
2583 struct usb_ssp_cap_descriptor *ssp_cap = hdev->bos->ssp_cap;
2584
2585 if (!ssp_cap)
2586 return 0;
2587
2588 ssa_count = le32_to_cpu(ssp_cap->bmAttributes) &
2589 USB_SSP_SUBLINK_SPEED_ATTRIBS;
2590
2591 for (i = 0; i <= ssa_count; i++) {
2592 ss_attr = le32_to_cpu(ssp_cap->bmSublinkSpeedAttr[i]);
2593 if (speed_id == (ss_attr & USB_SSP_SUBLINK_SPEED_SSID))
2594 return !!(ss_attr & USB_SSP_SUBLINK_SPEED_LP);
2595 }
2596 return 0;
2597}
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002598
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002599/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2600static unsigned hub_is_wusb(struct usb_hub *hub)
2601{
2602 struct usb_hcd *hcd;
2603 if (hub->hdev->parent != NULL) /* not a root hub? */
2604 return 0;
Geliang Tang6ae706a2015-12-23 21:26:51 +08002605 hcd = bus_to_hcd(hub->hdev->bus);
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002606 return hcd->wireless;
2607}
2608
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610#define PORT_RESET_TRIES 5
2611#define SET_ADDRESS_TRIES 2
2612#define GET_DESCRIPTOR_TRIES 2
2613#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302614#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2617#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002618#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619#define HUB_LONG_RESET_TIME 200
Sarah Sharp77c7f072012-11-14 17:16:52 -08002620#define HUB_RESET_TIMEOUT 800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Dan Williams48fc7db2013-12-05 17:07:27 -08002622/*
2623 * "New scheme" enumeration causes an extra state transition to be
2624 * exposed to an xhci host and causes USB3 devices to receive control
2625 * commands in the default state. This has been seen to cause
2626 * enumeration failures, so disable this enumeration scheme for USB3
2627 * devices.
2628 */
2629static bool use_new_scheme(struct usb_device *udev, int retry)
2630{
Mathias Nyman8a1b2722015-12-10 09:59:25 +02002631 if (udev->speed >= USB_SPEED_SUPER)
Dan Williams48fc7db2013-12-05 17:07:27 -08002632 return false;
2633
2634 return USE_NEW_SCHEME(retry);
2635}
2636
Rahul Bedarkar025d4432014-01-04 11:24:41 +05302637/* Is a USB 3.0 port in the Inactive or Compliance Mode state?
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002638 * Port worm reset is required to recover
2639 */
Dan Williams3cd12f92014-05-29 12:58:46 -07002640static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
2641 u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002642{
Dan Williams3cd12f92014-05-29 12:58:46 -07002643 u16 link_state;
2644
2645 if (!hub_is_superspeed(hub->hdev))
2646 return false;
2647
2648 if (test_bit(port1, hub->warm_reset_bits))
2649 return true;
2650
2651 link_state = portstatus & USB_PORT_STAT_LINK_STATE;
2652 return link_state == USB_SS_PORT_LS_SS_INACTIVE
2653 || link_state == USB_SS_PORT_LS_COMP_MOD;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002654}
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002657 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
2659 int delay_time, ret;
2660 u16 portstatus;
2661 u16 portchange;
Mathias Nyman0cdd49a2015-12-10 09:59:29 +02002662 u32 ext_portstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
2664 for (delay_time = 0;
2665 delay_time < HUB_RESET_TIMEOUT;
2666 delay_time += delay) {
2667 /* wait to give the device a chance to reset */
2668 msleep(delay);
2669
2670 /* read and decode port status */
Mathias Nyman0cdd49a2015-12-10 09:59:29 +02002671 if (hub_is_superspeedplus(hub->hdev))
2672 ret = hub_ext_port_status(hub, port1,
2673 HUB_EXT_PORT_STATUS,
2674 &portstatus, &portchange,
2675 &ext_portstatus);
2676 else
2677 ret = hub_port_status(hub, port1, &portstatus,
2678 &portchange);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 if (ret < 0)
2680 return ret;
2681
Guenter Roeckec0c5f02016-12-01 13:49:59 -08002682 /*
2683 * The port state is unknown until the reset completes.
2684 *
2685 * On top of that, some chips may require additional time
2686 * to re-establish a connection after the reset is complete,
2687 * so also wait for the connection to be re-established.
2688 */
2689 if (!(portstatus & USB_PORT_STAT_RESET) &&
2690 (portstatus & USB_PORT_STAT_CONNECTION))
Sarah Sharp470f0be2012-12-11 10:14:03 -08002691 break;
Sarah Sharp4f434472012-11-15 14:58:04 -08002692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 /* switch to the long delay after two short delay failures */
2694 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2695 delay = HUB_LONG_RESET_TIME;
2696
Dan Williamsd99f6b42014-05-20 18:08:17 -07002697 dev_dbg(&hub->ports[port1 - 1]->dev,
2698 "not %sreset yet, waiting %dms\n",
2699 warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
2701
Sarah Sharp470f0be2012-12-11 10:14:03 -08002702 if ((portstatus & USB_PORT_STAT_RESET))
2703 return -EBUSY;
2704
Dan Williams3cd12f92014-05-29 12:58:46 -07002705 if (hub_port_warm_reset_required(hub, port1, portstatus))
Sarah Sharp470f0be2012-12-11 10:14:03 -08002706 return -ENOTCONN;
2707
2708 /* Device went away? */
2709 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2710 return -ENOTCONN;
2711
Mathias Nymand15fc532017-10-17 16:07:33 +03002712 /* Retry if connect change is set but status is still connected.
2713 * A USB 3.0 connection may bounce if multiple warm resets were issued,
Sarah Sharp470f0be2012-12-11 10:14:03 -08002714 * but the device may have successfully re-connected. Ignore it.
2715 */
2716 if (!hub_is_superspeed(hub->hdev) &&
Mathias Nymand15fc532017-10-17 16:07:33 +03002717 (portchange & USB_PORT_STAT_C_CONNECTION)) {
2718 usb_clear_port_feature(hub->hdev, port1,
2719 USB_PORT_FEAT_C_CONNECTION);
2720 return -EAGAIN;
2721 }
Sarah Sharp470f0be2012-12-11 10:14:03 -08002722
2723 if (!(portstatus & USB_PORT_STAT_ENABLE))
2724 return -EBUSY;
2725
2726 if (!udev)
2727 return 0;
2728
2729 if (hub_is_wusb(hub))
2730 udev->speed = USB_SPEED_WIRELESS;
Mathias Nyman0cdd49a2015-12-10 09:59:29 +02002731 else if (hub_is_superspeedplus(hub->hdev) &&
2732 port_speed_is_ssp(hub->hdev, ext_portstatus &
2733 USB_EXT_PORT_STAT_RX_SPEED_ID))
2734 udev->speed = USB_SPEED_SUPER_PLUS;
Sarah Sharp470f0be2012-12-11 10:14:03 -08002735 else if (hub_is_superspeed(hub->hdev))
2736 udev->speed = USB_SPEED_SUPER;
2737 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2738 udev->speed = USB_SPEED_HIGH;
2739 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2740 udev->speed = USB_SPEED_LOW;
2741 else
2742 udev->speed = USB_SPEED_FULL;
2743 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744}
2745
Andiry Xu75d7cf72011-09-13 16:41:11 -07002746/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002748 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
2750 int i, status;
Sarah Sharpa24a6072012-11-01 11:20:44 -07002751 u16 portchange, portstatus;
Dan Williamsd99f6b42014-05-20 18:08:17 -07002752 struct usb_port *port_dev = hub->ports[port1 - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002754 if (!hub_is_superspeed(hub->hdev)) {
2755 if (warm) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002756 dev_err(hub->intfdev, "only USB3 hub support "
2757 "warm reset\n");
2758 return -EINVAL;
2759 }
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002760 /* Block EHCI CF initialization during the port reset.
2761 * Some companion controllers don't like it when they mix.
2762 */
2763 down_read(&ehci_cf_port_reset_rwsem);
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07002764 } else if (!warm) {
2765 /*
2766 * If the caller hasn't explicitly requested a warm reset,
2767 * double check and see if one is needed.
2768 */
Robert Schlabbachfb6d1f72015-05-26 00:27:30 +02002769 if (hub_port_status(hub, port1, &portstatus, &portchange) == 0)
2770 if (hub_port_warm_reset_required(hub, port1,
2771 portstatus))
2772 warm = true;
Andiry Xu75d7cf72011-09-13 16:41:11 -07002773 }
Dan Williams3cd12f92014-05-29 12:58:46 -07002774 clear_bit(port1, hub->warm_reset_bits);
Alan Stern32fe0192007-10-10 16:27:07 -04002775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 /* Reset the port */
2777 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002778 status = set_port_feature(hub->hdev, port1, (warm ?
2779 USB_PORT_FEAT_BH_PORT_RESET :
2780 USB_PORT_FEAT_RESET));
Alan Sterne9e88fb2013-03-27 16:14:01 -04002781 if (status == -ENODEV) {
2782 ; /* The hub is gone */
2783 } else if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002784 dev_err(&port_dev->dev,
2785 "cannot %sreset (err = %d)\n",
2786 warm ? "warm " : "", status);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002787 } else {
2788 status = hub_port_wait_reset(hub, port1, udev, delay,
2789 warm);
Alan Sterne9e88fb2013-03-27 16:14:01 -04002790 if (status && status != -ENOTCONN && status != -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 dev_dbg(hub->intfdev,
2792 "port_wait_reset: err = %d\n",
2793 status);
2794 }
2795
Sarah Sharpa24a6072012-11-01 11:20:44 -07002796 /* Check for disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002797 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
Robert Schlabbachfb6d1f72015-05-26 00:27:30 +02002798 usb_clear_port_feature(hub->hdev, port1,
2799 USB_PORT_FEAT_C_RESET);
Sarah Sharpa24a6072012-11-01 11:20:44 -07002800
2801 if (!hub_is_superspeed(hub->hdev))
2802 goto done;
2803
Robert Schlabbachfb6d1f72015-05-26 00:27:30 +02002804 usb_clear_port_feature(hub->hdev, port1,
2805 USB_PORT_FEAT_C_BH_PORT_RESET);
2806 usb_clear_port_feature(hub->hdev, port1,
2807 USB_PORT_FEAT_C_PORT_LINK_STATE);
2808 usb_clear_port_feature(hub->hdev, port1,
2809 USB_PORT_FEAT_C_CONNECTION);
2810
Sarah Sharpa24a6072012-11-01 11:20:44 -07002811 /*
2812 * If a USB 3.0 device migrates from reset to an error
2813 * state, re-issue the warm reset.
2814 */
2815 if (hub_port_status(hub, port1,
2816 &portstatus, &portchange) < 0)
2817 goto done;
2818
Dan Williams3cd12f92014-05-29 12:58:46 -07002819 if (!hub_port_warm_reset_required(hub, port1,
2820 portstatus))
Sarah Sharpa24a6072012-11-01 11:20:44 -07002821 goto done;
2822
2823 /*
2824 * If the port is in SS.Inactive or Compliance Mode, the
2825 * hot or warm reset failed. Try another warm reset.
2826 */
2827 if (!warm) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002828 dev_dbg(&port_dev->dev,
2829 "hot reset failed, warm reset\n");
Sarah Sharpa24a6072012-11-01 11:20:44 -07002830 warm = true;
2831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 }
2833
Dan Williamsd99f6b42014-05-20 18:08:17 -07002834 dev_dbg(&port_dev->dev,
2835 "not enabled, trying %sreset again...\n",
2836 warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 delay = HUB_LONG_RESET_TIME;
2838 }
2839
Dan Williamsd99f6b42014-05-20 18:08:17 -07002840 dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Andiry Xu75d7cf72011-09-13 16:41:11 -07002842done:
Robert Schlabbachfb6d1f72015-05-26 00:27:30 +02002843 if (status == 0) {
2844 /* TRSTRCY = 10 ms; plus some extra */
2845 msleep(10 + 40);
2846 if (udev) {
2847 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2848
2849 update_devnum(udev, 0);
2850 /* The xHC may think the device is already reset,
2851 * so ignore the status.
2852 */
2853 if (hcd->driver->reset_device)
2854 hcd->driver->reset_device(hcd, udev);
2855
2856 usb_set_device_state(udev, USB_STATE_DEFAULT);
2857 }
2858 } else {
2859 if (udev)
2860 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
2861 }
2862
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002863 if (!hub_is_superspeed(hub->hdev))
Andiry Xu75d7cf72011-09-13 16:41:11 -07002864 up_read(&ehci_cf_port_reset_rwsem);
2865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 return status;
2867}
2868
Andiry Xu0ed9a572011-04-27 18:07:43 +08002869/* Check if a port is power on */
2870static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2871{
2872 int ret = 0;
2873
2874 if (hub_is_superspeed(hub->hdev)) {
2875 if (portstatus & USB_SS_PORT_STAT_POWER)
2876 ret = 1;
2877 } else {
2878 if (portstatus & USB_PORT_STAT_POWER)
2879 ret = 1;
2880 }
2881
2882 return ret;
2883}
2884
Dan Williams5c79a1e2014-05-20 18:09:26 -07002885static void usb_lock_port(struct usb_port *port_dev)
2886 __acquires(&port_dev->status_lock)
2887{
2888 mutex_lock(&port_dev->status_lock);
2889 __acquire(&port_dev->status_lock);
2890}
2891
2892static void usb_unlock_port(struct usb_port *port_dev)
2893 __releases(&port_dev->status_lock)
2894{
2895 mutex_unlock(&port_dev->status_lock);
2896 __release(&port_dev->status_lock);
2897}
2898
Alan Sternd388dab2006-07-01 22:14:24 -04002899#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Andiry Xu0ed9a572011-04-27 18:07:43 +08002901/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2902static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2903{
2904 int ret = 0;
2905
2906 if (hub_is_superspeed(hub->hdev)) {
2907 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2908 == USB_SS_PORT_LS_U3)
2909 ret = 1;
2910 } else {
2911 if (portstatus & USB_PORT_STAT_SUSPEND)
2912 ret = 1;
2913 }
2914
2915 return ret;
2916}
Alan Sternb01b03f2008-04-28 11:06:11 -04002917
2918/* Determine whether the device on a port is ready for a normal resume,
2919 * is ready for a reset-resume, or should be disconnected.
2920 */
2921static int check_port_resume_type(struct usb_device *udev,
2922 struct usb_hub *hub, int port1,
Julius Werner7fa40912015-01-27 13:20:12 -08002923 int status, u16 portchange, u16 portstatus)
Alan Sternb01b03f2008-04-28 11:06:11 -04002924{
Dan Williamsd99f6b42014-05-20 18:08:17 -07002925 struct usb_port *port_dev = hub->ports[port1 - 1];
Julius Werner7fa40912015-01-27 13:20:12 -08002926 int retries = 3;
Dan Williamsd99f6b42014-05-20 18:08:17 -07002927
Julius Werner7fa40912015-01-27 13:20:12 -08002928 retry:
Dan Williams3cd12f92014-05-29 12:58:46 -07002929 /* Is a warm reset needed to recover the connection? */
2930 if (status == 0 && udev->reset_resume
2931 && hub_port_warm_reset_required(hub, port1, portstatus)) {
2932 /* pass */;
2933 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002934 /* Is the device still present? */
Dan Williams3cd12f92014-05-29 12:58:46 -07002935 else if (status || port_is_suspended(hub, portstatus) ||
Julius Werner7fa40912015-01-27 13:20:12 -08002936 !port_is_power_on(hub, portstatus)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002937 if (status >= 0)
2938 status = -ENODEV;
Julius Werner7fa40912015-01-27 13:20:12 -08002939 } else if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
2940 if (retries--) {
2941 usleep_range(200, 300);
2942 status = hub_port_status(hub, port1, &portstatus,
2943 &portchange);
2944 goto retry;
2945 }
2946 status = -ENODEV;
Alan Sternb01b03f2008-04-28 11:06:11 -04002947 }
2948
Alan Stern86c57ed2008-06-30 11:14:43 -04002949 /* Can't do a normal resume if the port isn't enabled,
2950 * so try a reset-resume instead.
2951 */
2952 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2953 if (udev->persist_enabled)
2954 udev->reset_resume = 1;
2955 else
2956 status = -ENODEV;
2957 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002958
2959 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002960 dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n",
2961 portchange, portstatus, status);
Alan Sternb01b03f2008-04-28 11:06:11 -04002962 } else if (udev->reset_resume) {
2963
2964 /* Late port handoff can set status-change bits */
2965 if (portchange & USB_PORT_STAT_C_CONNECTION)
Lan Tianyuad493e52013-01-23 04:26:30 +08002966 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04002967 USB_PORT_FEAT_C_CONNECTION);
2968 if (portchange & USB_PORT_STAT_C_ENABLE)
Lan Tianyuad493e52013-01-23 04:26:30 +08002969 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04002970 USB_PORT_FEAT_C_ENABLE);
2971 }
2972
2973 return status;
2974}
2975
Sarah Sharpf74631e2012-06-25 12:08:08 -07002976int usb_disable_ltm(struct usb_device *udev)
2977{
2978 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2979
2980 /* Check if the roothub and device supports LTM. */
2981 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2982 !usb_device_supports_ltm(udev))
2983 return 0;
2984
2985 /* Clear Feature LTM Enable can only be sent if the device is
2986 * configured.
2987 */
2988 if (!udev->actconfig)
2989 return 0;
2990
2991 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2992 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2993 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2994 USB_CTRL_SET_TIMEOUT);
2995}
2996EXPORT_SYMBOL_GPL(usb_disable_ltm);
2997
2998void usb_enable_ltm(struct usb_device *udev)
2999{
3000 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3001
3002 /* Check if the roothub and device supports LTM. */
3003 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
3004 !usb_device_supports_ltm(udev))
3005 return;
3006
3007 /* Set Feature LTM Enable can only be sent if the device is
3008 * configured.
3009 */
3010 if (!udev->actconfig)
3011 return;
3012
3013 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3014 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
3015 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
3016 USB_CTRL_SET_TIMEOUT);
3017}
3018EXPORT_SYMBOL_GPL(usb_enable_ltm);
3019
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003020/*
Alan Stern28e86162013-07-30 15:37:33 -04003021 * usb_enable_remote_wakeup - enable remote wakeup for a device
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003022 * @udev: target device
3023 *
Alan Stern28e86162013-07-30 15:37:33 -04003024 * For USB-2 devices: Set the device's remote wakeup feature.
3025 *
3026 * For USB-3 devices: Assume there's only one function on the device and
3027 * enable remote wake for the first interface. FIXME if the interface
3028 * association descriptor shows there's more than one function.
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003029 */
Alan Stern28e86162013-07-30 15:37:33 -04003030static int usb_enable_remote_wakeup(struct usb_device *udev)
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003031{
Alan Stern28e86162013-07-30 15:37:33 -04003032 if (udev->speed < USB_SPEED_SUPER)
3033 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3034 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
3035 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
3036 USB_CTRL_SET_TIMEOUT);
3037 else
3038 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3039 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
3040 USB_INTRF_FUNC_SUSPEND,
3041 USB_INTRF_FUNC_SUSPEND_RW |
3042 USB_INTRF_FUNC_SUSPEND_LP,
3043 NULL, 0, USB_CTRL_SET_TIMEOUT);
3044}
3045
3046/*
3047 * usb_disable_remote_wakeup - disable remote wakeup for a device
3048 * @udev: target device
3049 *
3050 * For USB-2 devices: Clear the device's remote wakeup feature.
3051 *
3052 * For USB-3 devices: Assume there's only one function on the device and
3053 * disable remote wake for the first interface. FIXME if the interface
3054 * association descriptor shows there's more than one function.
3055 */
3056static int usb_disable_remote_wakeup(struct usb_device *udev)
3057{
3058 if (udev->speed < USB_SPEED_SUPER)
3059 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3060 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
3061 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
3062 USB_CTRL_SET_TIMEOUT);
3063 else
3064 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Yonglong Wu4e248002016-08-19 11:37:26 +08003065 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003066 USB_INTRF_FUNC_SUSPEND, 0, NULL, 0,
3067 USB_CTRL_SET_TIMEOUT);
3068}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Alan Sterne583d9d2013-07-11 14:58:04 -04003070/* Count of wakeup-enabled devices at or below udev */
3071static unsigned wakeup_enabled_descendants(struct usb_device *udev)
3072{
3073 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
3074
3075 return udev->do_remote_wakeup +
3076 (hub ? hub->wakeup_enabled_descendants : 0);
3077}
3078
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079/*
Alan Stern140d8f62006-07-01 22:07:21 -04003080 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04003081 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07003082 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 *
3084 * Suspends a USB device that isn't in active use, conserving power.
3085 * Devices may wake out of a suspend, if anything important happens,
3086 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04003087 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 * to disconnect devices while they are suspended.
3089 *
David Brownell390a8c32005-09-13 19:57:27 -07003090 * This only affects the USB hardware for a device; its interfaces
3091 * (and, for hubs, child devices) must already have been suspended.
3092 *
Alan Stern624d6c02007-05-30 15:35:16 -04003093 * Selective port suspend reduces power; most suspended devices draw
3094 * less than 500 uA. It's also used in OTG, along with remote wakeup.
3095 * All devices below the suspended port are also suspended.
3096 *
3097 * Devices leave suspend state when the host wakes them up. Some devices
3098 * also support "remote wakeup", where the device can activate the USB
3099 * tree above them to deliver data, such as a keypress or packet. In
3100 * some cases, this wakes the USB host.
3101 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 * Suspending OTG devices may trigger HNP, if that's been enabled
3103 * between a pair of dual-role devices. That will change roles, such
3104 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
3105 *
Alan Stern4956ecc2007-05-30 16:51:28 -04003106 * Devices on USB hub ports have only one "suspend" state, corresponding
3107 * to ACPI D2, "may cause the device to lose some context".
3108 * State transitions include:
3109 *
3110 * - suspend, resume ... when the VBUS power link stays live
3111 * - suspend, disconnect ... VBUS lost
3112 *
3113 * Once VBUS drop breaks the circuit, the port it's using has to go through
3114 * normal re-enumeration procedures, starting with enabling VBUS power.
3115 * Other than re-initializing the hub (plug/unplug, except for root hubs),
Petr Mladek37ebb542014-09-19 17:32:23 +02003116 * Linux (2.6) currently has NO mechanisms to initiate that: no hub_wq
Alan Stern4956ecc2007-05-30 16:51:28 -04003117 * timer, no SRP, no requests through sysfs.
3118 *
Alan Sterne583d9d2013-07-11 14:58:04 -04003119 * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
3120 * suspended until their bus goes into global suspend (i.e., the root
Alan Stern0aa28322013-03-27 16:14:19 -04003121 * hub is suspended). Nevertheless, we change @udev->state to
3122 * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual
3123 * upstream port setting is stored in @udev->port_is_suspended.
Alan Stern4956ecc2007-05-30 16:51:28 -04003124 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 * Returns 0 on success, else negative errno.
3126 */
Alan Stern65bfd292008-11-25 16:39:18 -05003127int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
Lan Tianyuad493e52013-01-23 04:26:30 +08003129 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3130 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003131 int port1 = udev->portnum;
3132 int status;
Alan Stern0aa28322013-03-27 16:14:19 -04003133 bool really_suspend = true;
Alan Stern4956ecc2007-05-30 16:51:28 -04003134
Dan Williams5c79a1e2014-05-20 18:09:26 -07003135 usb_lock_port(port_dev);
3136
Alan Stern624d6c02007-05-30 15:35:16 -04003137 /* enable remote wakeup when appropriate; this lets the device
3138 * wake up the upstream hub (including maybe the root hub).
3139 *
3140 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
3141 * we don't explicitly enable it here.
3142 */
3143 if (udev->do_remote_wakeup) {
Alan Stern28e86162013-07-30 15:37:33 -04003144 status = usb_enable_remote_wakeup(udev);
Oliver Neukum0c487202009-10-19 13:19:41 +02003145 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003146 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
3147 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02003148 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02003149 if (PMSG_IS_AUTO(msg))
Alan Stern4fae6f02013-07-30 15:39:02 -04003150 goto err_wakeup;
Oliver Neukum0c487202009-10-19 13:19:41 +02003151 }
Alan Stern624d6c02007-05-30 15:35:16 -04003152 }
3153
Andiry Xu65580b432011-09-23 14:19:52 -07003154 /* disable USB2 hardware LPM */
3155 if (udev->usb2_hw_lpm_enabled == 1)
3156 usb_set_usb2_hardware_lpm(udev, 0);
3157
Sarah Sharpf74631e2012-06-25 12:08:08 -07003158 if (usb_disable_ltm(udev)) {
Alan Stern4fae6f02013-07-30 15:39:02 -04003159 dev_err(&udev->dev, "Failed to disable LTM before suspend\n.");
3160 status = -ENOMEM;
3161 if (PMSG_IS_AUTO(msg))
3162 goto err_ltm;
Sarah Sharpf74631e2012-06-25 12:08:08 -07003163 }
Sarah Sharp83060952012-05-02 14:25:52 -07003164 if (usb_unlocked_disable_lpm(udev)) {
Alan Stern4fae6f02013-07-30 15:39:02 -04003165 dev_err(&udev->dev, "Failed to disable LPM before suspend\n.");
3166 status = -ENOMEM;
3167 if (PMSG_IS_AUTO(msg))
3168 goto err_lpm3;
Sarah Sharp83060952012-05-02 14:25:52 -07003169 }
3170
Alan Stern624d6c02007-05-30 15:35:16 -04003171 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08003172 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003173 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
Alan Sterne583d9d2013-07-11 14:58:04 -04003174
Alan Stern0aa28322013-03-27 16:14:19 -04003175 /*
3176 * For system suspend, we do not need to enable the suspend feature
3177 * on individual USB-2 ports. The devices will automatically go
3178 * into suspend a few ms after the root hub stops sending packets.
3179 * The USB 2.0 spec calls this "global suspend".
Alan Sterne583d9d2013-07-11 14:58:04 -04003180 *
3181 * However, many USB hubs have a bug: They don't relay wakeup requests
3182 * from a downstream port if the port's suspend feature isn't on.
3183 * Therefore we will turn on the suspend feature if udev or any of its
3184 * descendants is enabled for remote wakeup.
Alan Stern0aa28322013-03-27 16:14:19 -04003185 */
Alan Sterne583d9d2013-07-11 14:58:04 -04003186 else if (PMSG_IS_AUTO(msg) || wakeup_enabled_descendants(udev) > 0)
3187 status = set_port_feature(hub->hdev, port1,
3188 USB_PORT_FEAT_SUSPEND);
Alan Stern0aa28322013-03-27 16:14:19 -04003189 else {
3190 really_suspend = false;
3191 status = 0;
3192 }
Alan Stern624d6c02007-05-30 15:35:16 -04003193 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003194 dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status);
Alan Sterncbb33002011-06-15 16:29:16 -04003195
Alan Stern4fae6f02013-07-30 15:39:02 -04003196 /* Try to enable USB3 LPM and LTM again */
3197 usb_unlocked_enable_lpm(udev);
3198 err_lpm3:
3199 usb_enable_ltm(udev);
3200 err_ltm:
Andiry Xuc3e751e2012-05-05 00:50:10 +08003201 /* Try to enable USB2 hardware LPM again */
3202 if (udev->usb2_hw_lpm_capable == 1)
3203 usb_set_usb2_hardware_lpm(udev, 1);
3204
Alan Stern4fae6f02013-07-30 15:39:02 -04003205 if (udev->do_remote_wakeup)
3206 (void) usb_disable_remote_wakeup(udev);
3207 err_wakeup:
Sarah Sharp83060952012-05-02 14:25:52 -07003208
Alan Sterncbb33002011-06-15 16:29:16 -04003209 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02003210 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003211 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04003212 } else {
Alan Stern30b1a7a2011-09-27 21:54:22 +02003213 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
3214 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
3215 udev->do_remote_wakeup);
Alan Stern0aa28322013-03-27 16:14:19 -04003216 if (really_suspend) {
3217 udev->port_is_suspended = 1;
Alan Sterne583d9d2013-07-11 14:58:04 -04003218
3219 /* device has up to 10 msec to fully suspend */
Alan Stern0aa28322013-03-27 16:14:19 -04003220 msleep(10);
3221 }
Alan Sterne583d9d2013-07-11 14:58:04 -04003222 usb_set_device_state(udev, USB_STATE_SUSPENDED);
Alan Stern624d6c02007-05-30 15:35:16 -04003223 }
Lan Tianyuad493e52013-01-23 04:26:30 +08003224
Dan Williamsd5c38342014-05-20 18:08:52 -07003225 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled
3226 && test_and_clear_bit(port1, hub->child_usage_bits))
Lan Tianyu98a4f1f2013-07-03 22:17:54 +08003227 pm_runtime_put_sync(&port_dev->dev);
Lan Tianyuad493e52013-01-23 04:26:30 +08003228
Alan Sternc08512c2010-11-15 15:57:58 -05003229 usb_mark_last_busy(hub->hdev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07003230
3231 usb_unlock_port(port_dev);
Alan Stern4956ecc2007-05-30 16:51:28 -04003232 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233}
David Brownellf3f32532005-09-22 22:37:29 -07003234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235/*
David Brownell390a8c32005-09-13 19:57:27 -07003236 * If the USB "suspend" state is in use (rather than "global suspend"),
3237 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04003238 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 * hardware resume signaling is finished, either because of selective
3240 * resume (by host) or remote wakeup (by device) ... now see what changed
3241 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04003242 *
3243 * If @udev->reset_resume is set then the device is reset before the
3244 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 */
Alan Stern140d8f62006-07-01 22:07:21 -04003246static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
Alan Stern54515fe2007-05-30 15:38:58 -04003248 int status = 0;
Oliver Neukum07e72b92012-11-29 15:05:57 +01003249 u16 devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
3251 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003252 dev_dbg(&udev->dev, "%s\n",
3253 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
3255 /* usb ch9 identifies four variants of SUSPENDED, based on what
3256 * state the device resumes to. Linux currently won't see the
3257 * first two on the host side; they'd be inside hub_port_init()
Petr Mladek37ebb542014-09-19 17:32:23 +02003258 * during many timeouts, but hub_wq can't suspend until later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 */
3260 usb_set_device_state(udev, udev->actconfig
3261 ? USB_STATE_CONFIGURED
3262 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Alan Stern54515fe2007-05-30 15:38:58 -04003264 /* 10.5.4.5 says not to reset a suspended port if the attached
3265 * device is enabled for remote wakeup. Hence the reset
3266 * operation is carried out here, after the port has been
3267 * resumed.
3268 */
Alan Stern1d102552014-03-18 10:39:05 -04003269 if (udev->reset_resume) {
3270 /*
3271 * If the device morphs or switches modes when it is reset,
3272 * we don't want to perform a reset-resume. We'll fail the
3273 * resume, which will cause a logical disconnect, and then
3274 * the device will be rediscovered.
3275 */
Alan Stern86c57ed2008-06-30 11:14:43 -04003276 retry_reset_resume:
Alan Stern1d102552014-03-18 10:39:05 -04003277 if (udev->quirks & USB_QUIRK_RESET)
3278 status = -ENODEV;
3279 else
3280 status = usb_reset_and_verify_device(udev);
3281 }
Alan Stern54515fe2007-05-30 15:38:58 -04003282
Matthias Beyer469271f2013-10-10 23:41:27 +02003283 /* 10.5.4.5 says be sure devices in the tree are still there.
3284 * For now let's assume the device didn't go crazy on resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 * and device drivers will know about any resume quirks.
3286 */
Alan Stern54515fe2007-05-30 15:38:58 -04003287 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04003288 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04003289 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
Alan Stern86c57ed2008-06-30 11:14:43 -04003290
3291 /* If a normal resume failed, try doing a reset-resume */
3292 if (status && !udev->reset_resume && udev->persist_enabled) {
3293 dev_dbg(&udev->dev, "retry with reset-resume\n");
3294 udev->reset_resume = 1;
3295 goto retry_reset_resume;
3296 }
Alan Stern54515fe2007-05-30 15:38:58 -04003297 }
Alan Sternb40b7a92006-06-19 15:12:38 -04003298
Alan Stern624d6c02007-05-30 15:35:16 -04003299 if (status) {
3300 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
3301 status);
Oliver Neukum07e72b92012-11-29 15:05:57 +01003302 /*
3303 * There are a few quirky devices which violate the standard
3304 * by claiming to have remote wakeup enabled after a reset,
3305 * which crash if the feature is cleared, hence check for
3306 * udev->reset_resume
3307 */
3308 } else if (udev->actconfig && !udev->reset_resume) {
Alan Stern28e86162013-07-30 15:37:33 -04003309 if (udev->speed < USB_SPEED_SUPER) {
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003310 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
Alan Stern28e86162013-07-30 15:37:33 -04003311 status = usb_disable_remote_wakeup(udev);
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003312 } else {
3313 status = usb_get_status(udev, USB_RECIP_INTERFACE, 0,
3314 &devstatus);
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003315 if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP
3316 | USB_INTRF_STAT_FUNC_RW))
Alan Stern28e86162013-07-30 15:37:33 -04003317 status = usb_disable_remote_wakeup(udev);
Alan Stern4bf0ba82005-11-21 11:58:07 -05003318 }
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003319
3320 if (status)
3321 dev_dbg(&udev->dev,
3322 "disable remote wakeup, status %d\n",
3323 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 }
3326 return status;
3327}
3328
Alan Stern624d6c02007-05-30 15:35:16 -04003329/*
Pratyush Ananda40178b2014-07-18 12:37:10 +05303330 * There are some SS USB devices which take longer time for link training.
3331 * XHCI specs 4.19.4 says that when Link training is successful, port
Al Cooper6b82b122015-12-02 14:55:07 -05003332 * sets CCS bit to 1. So if SW reads port status before successful link
Pratyush Ananda40178b2014-07-18 12:37:10 +05303333 * training, then it will not find device to be present.
3334 * USB Analyzer log with such buggy devices show that in some cases
3335 * device switch on the RX termination after long delay of host enabling
3336 * the VBUS. In few other cases it has been seen that device fails to
3337 * negotiate link training in first attempt. It has been
3338 * reported till now that few devices take as long as 2000 ms to train
3339 * the link after host enabling its VBUS and termination. Following
3340 * routine implements a 2000 ms timeout for link training. If in a case
3341 * link trains before timeout, loop will exit earlier.
3342 *
Al Cooper6b82b122015-12-02 14:55:07 -05003343 * There are also some 2.0 hard drive based devices and 3.0 thumb
3344 * drives that, when plugged into a 2.0 only port, take a long
3345 * time to set CCS after VBUS enable.
3346 *
Pratyush Ananda40178b2014-07-18 12:37:10 +05303347 * FIXME: If a device was connected before suspend, but was removed
3348 * while system was asleep, then the loop in the following routine will
3349 * only exit at timeout.
3350 *
Al Cooper6b82b122015-12-02 14:55:07 -05003351 * This routine should only be called when persist is enabled.
Pratyush Ananda40178b2014-07-18 12:37:10 +05303352 */
Al Cooper6b82b122015-12-02 14:55:07 -05003353static int wait_for_connected(struct usb_device *udev,
Pratyush Ananda40178b2014-07-18 12:37:10 +05303354 struct usb_hub *hub, int *port1,
3355 u16 *portchange, u16 *portstatus)
3356{
3357 int status = 0, delay_ms = 0;
3358
3359 while (delay_ms < 2000) {
3360 if (status || *portstatus & USB_PORT_STAT_CONNECTION)
3361 break;
3362 msleep(20);
3363 delay_ms += 20;
3364 status = hub_port_status(hub, *port1, portstatus, portchange);
3365 }
Al Cooper6b82b122015-12-02 14:55:07 -05003366 dev_dbg(&udev->dev, "Waited %dms for CONNECT\n", delay_ms);
Pratyush Ananda40178b2014-07-18 12:37:10 +05303367 return status;
3368}
3369
3370/*
Alan Stern624d6c02007-05-30 15:35:16 -04003371 * usb_port_resume - re-activate a suspended usb device's upstream port
3372 * @udev: device to re-activate, not a root hub
3373 * Context: must be able to sleep; device not locked; pm locks held
3374 *
3375 * This will re-activate the suspended device, increasing power usage
3376 * while letting drivers communicate again with its endpoints.
3377 * USB resume explicitly guarantees that the power session between
3378 * the host and the device is the same as it was when the device
3379 * suspended.
3380 *
Alan Sternfeccc302008-03-03 15:15:59 -05003381 * If @udev->reset_resume is set then this routine won't check that the
3382 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04003383 * reset @udev. The end result is that a broken power session can be
3384 * recovered and @udev will appear to persist across a loss of VBUS power.
3385 *
3386 * For example, if a host controller doesn't maintain VBUS suspend current
3387 * during a system sleep or is reset when the system wakes up, all the USB
3388 * power sessions below it will be broken. This is especially troublesome
3389 * for mass-storage devices containing mounted filesystems, since the
3390 * device will appear to have disconnected and all the memory mappings
3391 * to it will be lost. Using the USB_PERSIST facility, the device can be
3392 * made to appear as if it had not disconnected.
3393 *
Ming Lei742120c2008-06-18 22:00:29 +08003394 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05003395 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04003396 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3397 * quite possible for a device to remain unaltered but its media to be
3398 * changed. If the user replaces a flash memory card while the system is
3399 * asleep, he will have only himself to blame when the filesystem on the
3400 * new card is corrupted and the system crashes.
3401 *
Alan Stern624d6c02007-05-30 15:35:16 -04003402 * Returns 0 on success, else negative errno.
3403 */
Alan Stern65bfd292008-11-25 16:39:18 -05003404int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405{
Lan Tianyuad493e52013-01-23 04:26:30 +08003406 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3407 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003408 int port1 = udev->portnum;
3409 int status;
3410 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003411
Dan Williamsd5c38342014-05-20 18:08:52 -07003412 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08003413 status = pm_runtime_get_sync(&port_dev->dev);
Lan Tianyuad493e52013-01-23 04:26:30 +08003414 if (status < 0) {
3415 dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
3416 status);
3417 return status;
3418 }
3419 }
3420
Dan Williams5c79a1e2014-05-20 18:09:26 -07003421 usb_lock_port(port_dev);
3422
Alan Sternd25450c2006-11-20 11:14:30 -05003423 /* Skip the initial Clear-Suspend step for a remote wakeup */
3424 status = hub_port_status(hub, port1, &portstatus, &portchange);
Ravi Chandra Sadineni00bc3482018-04-20 11:08:21 -07003425 if (status == 0 && !port_is_suspended(hub, portstatus)) {
3426 if (portchange & USB_PORT_STAT_C_SUSPEND)
3427 pm_wakeup_event(&udev->dev, 0);
Alan Sternd25450c2006-11-20 11:14:30 -05003428 goto SuspendCleared;
Ravi Chandra Sadineni00bc3482018-04-20 11:08:21 -07003429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003432 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003433 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
Andiry Xua7114232011-04-27 18:07:50 +08003434 else
Lan Tianyuad493e52013-01-23 04:26:30 +08003435 status = usb_clear_port_feature(hub->hdev,
Andiry Xua7114232011-04-27 18:07:50 +08003436 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003438 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 } else {
Felipe Balbibbc78c02015-02-13 15:38:33 -06003440 /* drive resume for USB_RESUME_TIMEOUT msec */
Alan Stern686314c2007-05-30 15:34:36 -04003441 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003442 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Felipe Balbibbc78c02015-02-13 15:38:33 -06003443 msleep(USB_RESUME_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3446 * stop resume signaling. Then finish the resume
3447 * sequence.
3448 */
Alan Sternd25450c2006-11-20 11:14:30 -05003449 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003450
Alan Sternb01b03f2008-04-28 11:06:11 -04003451 /* TRSMRCY = 10 msec */
3452 msleep(10);
3453 }
Alan Stern54515fe2007-05-30 15:38:58 -04003454
Alan Sternb01b03f2008-04-28 11:06:11 -04003455 SuspendCleared:
3456 if (status == 0) {
Alan Sternbfd1e912012-10-19 11:03:39 -04003457 udev->port_is_suspended = 0;
Andiry Xua7114232011-04-27 18:07:50 +08003458 if (hub_is_superspeed(hub->hdev)) {
3459 if (portchange & USB_PORT_STAT_C_LINK_STATE)
Lan Tianyuad493e52013-01-23 04:26:30 +08003460 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003461 USB_PORT_FEAT_C_PORT_LINK_STATE);
3462 } else {
3463 if (portchange & USB_PORT_STAT_C_SUSPEND)
Lan Tianyuad493e52013-01-23 04:26:30 +08003464 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003465 USB_PORT_FEAT_C_SUSPEND);
3466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Al Cooper6b82b122015-12-02 14:55:07 -05003469 if (udev->persist_enabled)
3470 status = wait_for_connected(udev, hub, &port1, &portchange,
Pratyush Ananda40178b2014-07-18 12:37:10 +05303471 &portstatus);
3472
Alan Sternb01b03f2008-04-28 11:06:11 -04003473 status = check_port_resume_type(udev,
3474 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003475 if (status == 0)
3476 status = finish_port_resume(udev);
3477 if (status < 0) {
3478 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3479 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003480 } else {
3481 /* Try to enable USB2 hardware LPM */
3482 if (udev->usb2_hw_lpm_capable == 1)
3483 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003484
Sarah Sharpf74631e2012-06-25 12:08:08 -07003485 /* Try to enable USB3 LTM and LPM */
3486 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003487 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003488 }
Andiry Xu65580b432011-09-23 14:19:52 -07003489
Dan Williams5c79a1e2014-05-20 18:09:26 -07003490 usb_unlock_port(port_dev);
3491
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 return status;
3493}
3494
Alan Stern0534d462010-01-08 12:56:30 -05003495int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
3497 int status = 0;
3498
Dan Williams5c79a1e2014-05-20 18:09:26 -07003499 usb_lock_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003501 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003502 status = usb_autoresume_device(udev);
3503 if (status == 0) {
3504 /* Let the drivers do their thing, then... */
3505 usb_autosuspend_device(udev);
3506 }
Alan Sternd25450c2006-11-20 11:14:30 -05003507 }
Dan Williams5c79a1e2014-05-20 18:09:26 -07003508 usb_unlock_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 return status;
3510}
3511
Dan Williams7e73be22014-05-20 18:09:31 -07003512/* Returns 1 if there was a remote wakeup and a connect status change. */
3513static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
3514 u16 portstatus, u16 portchange)
3515 __must_hold(&port_dev->status_lock)
3516{
3517 struct usb_port *port_dev = hub->ports[port - 1];
3518 struct usb_device *hdev;
3519 struct usb_device *udev;
3520 int connect_change = 0;
3521 int ret;
3522
3523 hdev = hub->hdev;
3524 udev = port_dev->child;
3525 if (!hub_is_superspeed(hdev)) {
3526 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
3527 return 0;
3528 usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
3529 } else {
3530 if (!udev || udev->state != USB_STATE_SUSPENDED ||
3531 (portstatus & USB_PORT_STAT_LINK_STATE) !=
3532 USB_SS_PORT_LS_U0)
3533 return 0;
3534 }
3535
3536 if (udev) {
3537 /* TRSMRCY = 10 msec */
3538 msleep(10);
3539
3540 usb_unlock_port(port_dev);
3541 ret = usb_remote_wakeup(udev);
3542 usb_lock_port(port_dev);
3543 if (ret < 0)
3544 connect_change = 1;
3545 } else {
3546 ret = -ENODEV;
3547 hub_port_disable(hub, port, 1);
3548 }
3549 dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
3550 return connect_change;
3551}
3552
Ming Leie6f30de2012-10-24 11:59:24 +08003553static int check_ports_changed(struct usb_hub *hub)
3554{
3555 int port1;
3556
3557 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3558 u16 portstatus, portchange;
3559 int status;
3560
3561 status = hub_port_status(hub, port1, &portstatus, &portchange);
3562 if (!status && portchange)
3563 return 1;
3564 }
3565 return 0;
3566}
3567
David Brownelldb690872005-09-13 19:56:33 -07003568static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569{
Chase Metzger17248562015-08-11 21:34:37 -07003570 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 struct usb_device *hdev = hub->hdev;
3572 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003573 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Alan Sterne583d9d2013-07-11 14:58:04 -04003575 /*
3576 * Warn if children aren't already suspended.
3577 * Also, add up the number of wakeup-enabled descendants.
3578 */
3579 hub->wakeup_enabled_descendants = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003581 struct usb_port *port_dev = hub->ports[port1 - 1];
3582 struct usb_device *udev = port_dev->child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Alan Stern6840d252007-09-10 11:34:26 -04003584 if (udev && udev->can_submit) {
Dan Williamsb658b8f2014-06-17 16:16:22 -07003585 dev_warn(&port_dev->dev, "device %s not suspended yet\n",
3586 dev_name(&udev->dev));
Alan Stern5b1b0b82011-08-19 23:49:48 +02003587 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003588 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003589 }
Alan Sterne583d9d2013-07-11 14:58:04 -04003590 if (udev)
3591 hub->wakeup_enabled_descendants +=
3592 wakeup_enabled_descendants(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 }
Ming Leie6f30de2012-10-24 11:59:24 +08003594
3595 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3596 /* check if there are changes pending on hub ports */
3597 if (check_ports_changed(hub)) {
3598 if (PMSG_IS_AUTO(msg))
3599 return -EBUSY;
3600 pm_wakeup_event(&hdev->dev, 2000);
3601 }
3602 }
3603
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003604 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3605 /* Enable hub to send remote wakeup for all ports. */
3606 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3607 status = set_port_feature(hdev,
3608 port1 |
3609 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3610 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3611 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3612 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3613 }
3614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Harvey Harrison441b62c2008-03-03 16:08:34 -08003616 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003617
Petr Mladek37ebb542014-09-19 17:32:23 +02003618 /* stop hub_wq and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003619 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
3622
3623static int hub_resume(struct usb_interface *intf)
3624{
Alan Stern5e6effa2008-03-03 15:15:51 -05003625 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Alan Stern5e6effa2008-03-03 15:15:51 -05003627 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003628 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 return 0;
3630}
3631
Alan Sternb41a60e2007-05-30 15:39:33 -04003632static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003633{
Alan Sternb41a60e2007-05-30 15:39:33 -04003634 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003635
Alan Stern5e6effa2008-03-03 15:15:51 -05003636 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003637 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003638 return 0;
3639}
3640
Alan Stern54515fe2007-05-30 15:38:58 -04003641/**
3642 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3643 * @rhdev: struct usb_device for the root hub
3644 *
3645 * The USB host controller driver calls this function when its root hub
3646 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003647 * has been reset. The routine marks @rhdev as having lost power.
3648 * When the hub driver is resumed it will take notice and carry out
3649 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3650 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003651 */
3652void usb_root_hub_lost_power(struct usb_device *rhdev)
3653{
3654 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3655 rhdev->reset_resume = 1;
3656}
3657EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3658
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003659static const char * const usb3_lpm_names[] = {
3660 "U0",
3661 "U1",
3662 "U2",
3663 "U3",
3664};
3665
3666/*
3667 * Send a Set SEL control transfer to the device, prior to enabling
3668 * device-initiated U1 or U2. This lets the device know the exit latencies from
3669 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3670 * packet from the host.
3671 *
3672 * This function will fail if the SEL or PEL values for udev are greater than
3673 * the maximum allowed values for the link state to be enabled.
3674 */
3675static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3676{
3677 struct usb_set_sel_req *sel_values;
3678 unsigned long long u1_sel;
3679 unsigned long long u1_pel;
3680 unsigned long long u2_sel;
3681 unsigned long long u2_pel;
3682 int ret;
3683
Xenia Ragiadakou38d7f682013-09-04 17:24:45 +03003684 if (udev->state != USB_STATE_CONFIGURED)
3685 return 0;
3686
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003687 /* Convert SEL and PEL stored in ns to us */
3688 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3689 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3690 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3691 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3692
3693 /*
3694 * Make sure that the calculated SEL and PEL values for the link
3695 * state we're enabling aren't bigger than the max SEL/PEL
3696 * value that will fit in the SET SEL control transfer.
3697 * Otherwise the device would get an incorrect idea of the exit
3698 * latency for the link state, and could start a device-initiated
3699 * U1/U2 when the exit latencies are too high.
3700 */
3701 if ((state == USB3_LPM_U1 &&
3702 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3703 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3704 (state == USB3_LPM_U2 &&
3705 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3706 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003707 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 -07003708 usb3_lpm_names[state], u1_sel, u1_pel);
3709 return -EINVAL;
3710 }
3711
3712 /*
3713 * If we're enabling device-initiated LPM for one link state,
3714 * but the other link state has a too high SEL or PEL value,
3715 * just set those values to the max in the Set SEL request.
3716 */
3717 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3718 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3719
3720 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3721 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3722
3723 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3724 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3725
3726 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3727 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3728
3729 /*
3730 * usb_enable_lpm() can be called as part of a failed device reset,
3731 * which may be initiated by an error path of a mass storage driver.
3732 * Therefore, use GFP_NOIO.
3733 */
3734 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3735 if (!sel_values)
3736 return -ENOMEM;
3737
3738 sel_values->u1_sel = u1_sel;
3739 sel_values->u1_pel = u1_pel;
3740 sel_values->u2_sel = cpu_to_le16(u2_sel);
3741 sel_values->u2_pel = cpu_to_le16(u2_pel);
3742
3743 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3744 USB_REQ_SET_SEL,
3745 USB_RECIP_DEVICE,
3746 0, 0,
3747 sel_values, sizeof *(sel_values),
3748 USB_CTRL_SET_TIMEOUT);
3749 kfree(sel_values);
3750 return ret;
3751}
3752
3753/*
3754 * Enable or disable device-initiated U1 or U2 transitions.
3755 */
3756static int usb_set_device_initiated_lpm(struct usb_device *udev,
3757 enum usb3_link_state state, bool enable)
3758{
3759 int ret;
3760 int feature;
3761
3762 switch (state) {
3763 case USB3_LPM_U1:
3764 feature = USB_DEVICE_U1_ENABLE;
3765 break;
3766 case USB3_LPM_U2:
3767 feature = USB_DEVICE_U2_ENABLE;
3768 break;
3769 default:
3770 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3771 __func__, enable ? "enable" : "disable");
3772 return -EINVAL;
3773 }
3774
3775 if (udev->state != USB_STATE_CONFIGURED) {
3776 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3777 "for unconfigured device.\n",
3778 __func__, enable ? "enable" : "disable",
3779 usb3_lpm_names[state]);
3780 return 0;
3781 }
3782
3783 if (enable) {
3784 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003785 * Now send the control transfer to enable device-initiated LPM
3786 * for either U1 or U2.
3787 */
3788 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3789 USB_REQ_SET_FEATURE,
3790 USB_RECIP_DEVICE,
3791 feature,
3792 0, NULL, 0,
3793 USB_CTRL_SET_TIMEOUT);
3794 } else {
3795 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3796 USB_REQ_CLEAR_FEATURE,
3797 USB_RECIP_DEVICE,
3798 feature,
3799 0, NULL, 0,
3800 USB_CTRL_SET_TIMEOUT);
3801 }
3802 if (ret < 0) {
3803 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3804 enable ? "Enable" : "Disable",
3805 usb3_lpm_names[state]);
3806 return -EBUSY;
3807 }
3808 return 0;
3809}
3810
3811static int usb_set_lpm_timeout(struct usb_device *udev,
3812 enum usb3_link_state state, int timeout)
3813{
3814 int ret;
3815 int feature;
3816
3817 switch (state) {
3818 case USB3_LPM_U1:
3819 feature = USB_PORT_FEAT_U1_TIMEOUT;
3820 break;
3821 case USB3_LPM_U2:
3822 feature = USB_PORT_FEAT_U2_TIMEOUT;
3823 break;
3824 default:
3825 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3826 __func__);
3827 return -EINVAL;
3828 }
3829
3830 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3831 timeout != USB3_LPM_DEVICE_INITIATED) {
3832 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3833 "which is a reserved value.\n",
3834 usb3_lpm_names[state], timeout);
3835 return -EINVAL;
3836 }
3837
3838 ret = set_port_feature(udev->parent,
3839 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3840 feature);
3841 if (ret < 0) {
3842 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3843 "error code %i\n", usb3_lpm_names[state],
3844 timeout, ret);
3845 return -EBUSY;
3846 }
3847 if (state == USB3_LPM_U1)
3848 udev->u1_params.timeout = timeout;
3849 else
3850 udev->u2_params.timeout = timeout;
3851 return 0;
3852}
3853
3854/*
3855 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3856 * U1/U2 entry.
3857 *
3858 * We will attempt to enable U1 or U2, but there are no guarantees that the
3859 * control transfers to set the hub timeout or enable device-initiated U1/U2
3860 * will be successful.
3861 *
3862 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3863 * driver know about it. If that call fails, it should be harmless, and just
3864 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3865 */
3866static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3867 enum usb3_link_state state)
3868{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003869 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003870 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3871 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3872
3873 /* If the device says it doesn't have *any* exit latency to come out of
3874 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3875 * state.
3876 */
3877 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3878 (state == USB3_LPM_U2 && u2_mel == 0))
3879 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003880
Sarah Sharp65a95b72012-10-05 10:32:07 -07003881 /*
3882 * First, let the device know about the exit latencies
3883 * associated with the link state we're about to enable.
3884 */
3885 ret = usb_req_set_sel(udev, state);
3886 if (ret < 0) {
3887 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3888 usb3_lpm_names[state]);
3889 return;
3890 }
3891
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003892 /* We allow the host controller to set the U1/U2 timeout internally
3893 * first, so that it can change its schedule to account for the
3894 * additional latency to send data to a device in a lower power
3895 * link state.
3896 */
3897 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3898
3899 /* xHCI host controller doesn't want to enable this LPM state. */
3900 if (timeout == 0)
3901 return;
3902
3903 if (timeout < 0) {
3904 dev_warn(&udev->dev, "Could not enable %s link state, "
3905 "xHCI error %i.\n", usb3_lpm_names[state],
3906 timeout);
3907 return;
3908 }
3909
Lu Baolubf5ce5b2015-11-14 16:26:32 +08003910 if (usb_set_lpm_timeout(udev, state, timeout)) {
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003911 /* If we can't set the parent hub U1/U2 timeout,
3912 * device-initiated LPM won't be allowed either, so let the xHCI
3913 * host know that this link state won't be enabled.
3914 */
3915 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
Lu Baolubf5ce5b2015-11-14 16:26:32 +08003916 } else {
3917 /* Only a configured device will accept the Set Feature
3918 * U1/U2_ENABLE
3919 */
3920 if (udev->actconfig)
3921 usb_set_device_initiated_lpm(udev, state, true);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003922
Lu Baolubf5ce5b2015-11-14 16:26:32 +08003923 /* As soon as usb_set_lpm_timeout(timeout) returns 0, the
3924 * hub-initiated LPM is enabled. Thus, LPM is enabled no
3925 * matter the result of usb_set_device_initiated_lpm().
3926 * The only difference is whether device is able to initiate
3927 * LPM.
3928 */
3929 if (state == USB3_LPM_U1)
3930 udev->usb3_lpm_u1_enabled = 1;
3931 else if (state == USB3_LPM_U2)
3932 udev->usb3_lpm_u2_enabled = 1;
3933 }
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003934}
3935
3936/*
3937 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3938 * U1/U2 entry.
3939 *
3940 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3941 * If zero is returned, the parent will not allow the link to go into U1/U2.
3942 *
3943 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3944 * it won't have an effect on the bus link state because the parent hub will
3945 * still disallow device-initiated U1/U2 entry.
3946 *
3947 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3948 * possible. The result will be slightly more bus bandwidth will be taken up
3949 * (to account for U1/U2 exit latency), but it should be harmless.
3950 */
3951static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3952 enum usb3_link_state state)
3953{
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003954 switch (state) {
3955 case USB3_LPM_U1:
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003956 case USB3_LPM_U2:
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003957 break;
3958 default:
3959 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3960 __func__);
3961 return -EINVAL;
3962 }
3963
3964 if (usb_set_lpm_timeout(udev, state, 0))
3965 return -EBUSY;
3966
3967 usb_set_device_initiated_lpm(udev, state, false);
3968
3969 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3970 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3971 "bus schedule bandwidth may be impacted.\n",
3972 usb3_lpm_names[state]);
Lu Baolubf5ce5b2015-11-14 16:26:32 +08003973
3974 /* As soon as usb_set_lpm_timeout(0) return 0, hub initiated LPM
3975 * is disabled. Hub will disallows link to enter U1/U2 as well,
3976 * even device is initiating LPM. Hence LPM is disabled if hub LPM
3977 * timeout set to 0, no matter device-initiated LPM is disabled or
3978 * not.
3979 */
3980 if (state == USB3_LPM_U1)
3981 udev->usb3_lpm_u1_enabled = 0;
3982 else if (state == USB3_LPM_U2)
3983 udev->usb3_lpm_u2_enabled = 0;
3984
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003985 return 0;
3986}
3987
3988/*
3989 * Disable hub-initiated and device-initiated U1 and U2 entry.
3990 * Caller must own the bandwidth_mutex.
3991 *
3992 * This will call usb_enable_lpm() on failure, which will decrement
3993 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3994 */
3995int usb_disable_lpm(struct usb_device *udev)
3996{
3997 struct usb_hcd *hcd;
3998
3999 if (!udev || !udev->parent ||
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004000 udev->speed < USB_SPEED_SUPER ||
Pratyush Anand51df62f2014-07-04 17:01:27 +03004001 !udev->lpm_capable ||
4002 udev->state < USB_STATE_DEFAULT)
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004003 return 0;
4004
4005 hcd = bus_to_hcd(udev->bus);
4006 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
4007 return 0;
4008
4009 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03004010 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004011 return 0;
4012
4013 /* If LPM is enabled, attempt to disable it. */
4014 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
4015 goto enable_lpm;
4016 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
4017 goto enable_lpm;
4018
4019 return 0;
4020
4021enable_lpm:
4022 usb_enable_lpm(udev);
4023 return -EBUSY;
4024}
4025EXPORT_SYMBOL_GPL(usb_disable_lpm);
4026
4027/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
4028int usb_unlocked_disable_lpm(struct usb_device *udev)
4029{
4030 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4031 int ret;
4032
4033 if (!hcd)
4034 return -EINVAL;
4035
4036 mutex_lock(hcd->bandwidth_mutex);
4037 ret = usb_disable_lpm(udev);
4038 mutex_unlock(hcd->bandwidth_mutex);
4039
4040 return ret;
4041}
4042EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
4043
4044/*
4045 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
4046 * xHCI host policy may prevent U1 or U2 from being enabled.
4047 *
4048 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
4049 * until the lpm_disable_count drops to zero. Caller must own the
4050 * bandwidth_mutex.
4051 */
4052void usb_enable_lpm(struct usb_device *udev)
4053{
4054 struct usb_hcd *hcd;
Lu Baolu513072d2015-11-14 16:26:33 +08004055 struct usb_hub *hub;
4056 struct usb_port *port_dev;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004057
4058 if (!udev || !udev->parent ||
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004059 udev->speed < USB_SPEED_SUPER ||
Pratyush Anand51df62f2014-07-04 17:01:27 +03004060 !udev->lpm_capable ||
4061 udev->state < USB_STATE_DEFAULT)
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004062 return;
4063
4064 udev->lpm_disable_count--;
4065 hcd = bus_to_hcd(udev->bus);
4066 /* Double check that we can both enable and disable LPM.
4067 * Device must be configured to accept set feature U1/U2 timeout.
4068 */
4069 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
4070 !hcd->driver->disable_usb3_lpm_timeout)
4071 return;
4072
4073 if (udev->lpm_disable_count > 0)
4074 return;
4075
Lu Baolu513072d2015-11-14 16:26:33 +08004076 hub = usb_hub_to_struct_hub(udev->parent);
4077 if (!hub)
4078 return;
4079
4080 port_dev = hub->ports[udev->portnum - 1];
4081
4082 if (port_dev->usb3_lpm_u1_permit)
4083 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
4084
4085 if (port_dev->usb3_lpm_u2_permit)
4086 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004087}
4088EXPORT_SYMBOL_GPL(usb_enable_lpm);
4089
4090/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
4091void usb_unlocked_enable_lpm(struct usb_device *udev)
4092{
4093 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4094
4095 if (!hcd)
4096 return;
4097
4098 mutex_lock(hcd->bandwidth_mutex);
4099 usb_enable_lpm(udev);
4100 mutex_unlock(hcd->bandwidth_mutex);
4101}
4102EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
4103
Mathias Nyman32a35352016-11-17 11:14:14 +02004104/* usb3 devices use U3 for disabled, make sure remote wakeup is disabled */
4105static void hub_usb3_port_prepare_disable(struct usb_hub *hub,
4106 struct usb_port *port_dev)
4107{
4108 struct usb_device *udev = port_dev->child;
4109 int ret;
4110
4111 if (udev && udev->port_is_suspended && udev->do_remote_wakeup) {
4112 ret = hub_set_port_link_state(hub, port_dev->portnum,
4113 USB_SS_PORT_LS_U0);
4114 if (!ret) {
4115 msleep(USB_RESUME_TIMEOUT);
4116 ret = usb_disable_remote_wakeup(udev);
4117 }
4118 if (ret)
4119 dev_warn(&udev->dev,
4120 "Port disable: can't disable remote wake\n");
4121 udev->do_remote_wakeup = 0;
4122 }
4123}
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004124
Alan Sternd388dab2006-07-01 22:14:24 -04004125#else /* CONFIG_PM */
4126
Alan Sternf07600c2007-05-30 15:38:16 -04004127#define hub_suspend NULL
4128#define hub_resume NULL
4129#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004130
Mathias Nyman32a35352016-11-17 11:14:14 +02004131static inline void hub_usb3_port_prepare_disable(struct usb_hub *hub,
4132 struct usb_port *port_dev) { }
4133
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004134int usb_disable_lpm(struct usb_device *udev)
4135{
4136 return 0;
4137}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004138EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004139
4140void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004141EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004142
4143int usb_unlocked_disable_lpm(struct usb_device *udev)
4144{
4145 return 0;
4146}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004147EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004148
4149void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004150EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07004151
4152int usb_disable_ltm(struct usb_device *udev)
4153{
4154 return 0;
4155}
4156EXPORT_SYMBOL_GPL(usb_disable_ltm);
4157
4158void usb_enable_ltm(struct usb_device *udev) { }
4159EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternc4b51a42013-07-11 14:58:23 -04004160
Stephen Rothwell4a95b1f2014-05-29 18:55:06 +10004161static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
4162 u16 portstatus, u16 portchange)
4163{
4164 return 0;
4165}
4166
Alan Sternc4b51a42013-07-11 14:58:23 -04004167#endif /* CONFIG_PM */
Alan Sternd388dab2006-07-01 22:14:24 -04004168
Geert Uytterhoeven8ecf70f2016-12-14 15:37:30 +01004169/*
4170 * USB-3 does not have a similar link state as USB-2 that will avoid negotiating
4171 * a connection with a plugged-in cable but will signal the host when the cable
4172 * is unplugged. Disable remote wake and set link state to U3 for USB-3 devices
4173 */
4174static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
4175{
4176 struct usb_port *port_dev = hub->ports[port1 - 1];
4177 struct usb_device *hdev = hub->hdev;
4178 int ret = 0;
4179
4180 if (!hub->error) {
4181 if (hub_is_superspeed(hub->hdev)) {
4182 hub_usb3_port_prepare_disable(hub, port_dev);
4183 ret = hub_set_port_link_state(hub, port_dev->portnum,
4184 USB_SS_PORT_LS_U3);
4185 } else {
4186 ret = usb_clear_port_feature(hdev, port1,
4187 USB_PORT_FEAT_ENABLE);
4188 }
4189 }
4190 if (port_dev->child && set_state)
4191 usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED);
4192 if (ret && ret != -ENODEV)
4193 dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret);
4194 return ret;
4195}
4196
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
4198/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
4199 *
4200 * Between connect detection and reset signaling there must be a delay
4201 * of 100ms at least for debounce and power-settling. The corresponding
4202 * timer shall restart whenever the downstream port detects a disconnect.
Matthias Beyer469271f2013-10-10 23:41:27 +02004203 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 * Apparently there are some bluetooth and irda-dongles and a number of
4205 * low-speed devices for which this debounce period may last over a second.
4206 * Not covered by the spec - but easy to deal with.
4207 *
4208 * This implementation uses a 1500ms total debounce timeout; if the
4209 * connection isn't stable by then it returns -ETIMEDOUT. It checks
4210 * every 25ms for transient disconnects. When the port status has been
4211 * unchanged for 100ms it returns the port status.
4212 */
Lan Tianyuad493e52013-01-23 04:26:30 +08004213int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214{
4215 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 u16 portchange, portstatus;
4217 unsigned connection = 0xffff;
Dan Williamsd99f6b42014-05-20 18:08:17 -07004218 int total_time, stable_time = 0;
4219 struct usb_port *port_dev = hub->ports[port1 - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
4221 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
4222 ret = hub_port_status(hub, port1, &portstatus, &portchange);
4223 if (ret < 0)
4224 return ret;
4225
4226 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
4227 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004228 if (!must_be_connected ||
4229 (connection == USB_PORT_STAT_CONNECTION))
4230 stable_time += HUB_DEBOUNCE_STEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 if (stable_time >= HUB_DEBOUNCE_STABLE)
4232 break;
4233 } else {
4234 stable_time = 0;
4235 connection = portstatus & USB_PORT_STAT_CONNECTION;
4236 }
4237
4238 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004239 usb_clear_port_feature(hub->hdev, port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 USB_PORT_FEAT_C_CONNECTION);
4241 }
4242
4243 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
4244 break;
4245 msleep(HUB_DEBOUNCE_STEP);
4246 }
4247
Dan Williamsd99f6b42014-05-20 18:08:17 -07004248 dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n",
4249 total_time, stable_time, portstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
4251 if (stable_time < HUB_DEBOUNCE_STABLE)
4252 return -ETIMEDOUT;
4253 return portstatus;
4254}
4255
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004256void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257{
Alan Sternddeac4e72009-01-15 17:03:33 -05004258 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
4259 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05004260 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004262EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
4264#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
4265#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
4266
Alan Stern4326ed02007-07-30 17:08:43 -04004267static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268{
4269 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07004270 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Sarah Sharpc6515272009-04-27 19:57:26 -07004272 /*
4273 * The host controller will choose the device address,
4274 * instead of the core having chosen it earlier
4275 */
4276 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 return -EINVAL;
4278 if (udev->state == USB_STATE_ADDRESS)
4279 return 0;
4280 if (udev->state != USB_STATE_DEFAULT)
4281 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07004282 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07004283 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004284 else
Sarah Sharpc6515272009-04-27 19:57:26 -07004285 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
4286 USB_REQ_SET_ADDRESS, 0, devnum, 0,
4287 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05004289 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07004290 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004292 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 }
4294 return retval;
4295}
4296
Mathias Nyman890dae82013-09-30 17:26:31 +03004297/*
4298 * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM
4299 * when they're plugged into a USB 2.0 port, but they don't work when LPM is
4300 * enabled.
4301 *
4302 * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the
4303 * device says it supports the new USB 2.0 Link PM errata by setting the BESL
4304 * support bit in the BOS descriptor.
4305 */
4306static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev)
4307{
Dan Williamsd99f6b42014-05-20 18:08:17 -07004308 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
4309 int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
Mathias Nyman890dae82013-09-30 17:26:31 +03004310
Guenter Roeck4e615882017-03-08 10:19:36 -08004311 if (!udev->usb2_hw_lpm_capable || !udev->bos)
Mathias Nyman890dae82013-09-30 17:26:31 +03004312 return;
4313
Dan Williamsd99f6b42014-05-20 18:08:17 -07004314 if (hub)
4315 connect_type = hub->ports[udev->portnum - 1]->connect_type;
Mathias Nyman890dae82013-09-30 17:26:31 +03004316
Bjørn Mork23c05822014-02-27 14:23:55 +01004317 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) ||
Mathias Nyman890dae82013-09-30 17:26:31 +03004318 connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
4319 udev->usb2_hw_lpm_allowed = 1;
4320 usb_set_usb2_hardware_lpm(udev, 1);
4321 }
4322}
4323
Dan Williams48fc7db2013-12-05 17:07:27 -08004324static int hub_enable_device(struct usb_device *udev)
4325{
4326 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4327
4328 if (!hcd->driver->enable_device)
4329 return 0;
4330 if (udev->state == USB_STATE_ADDRESS)
4331 return 0;
4332 if (udev->state != USB_STATE_DEFAULT)
4333 return -EINVAL;
4334
4335 return hcd->driver->enable_device(hcd, udev);
4336}
4337
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338/* Reset device, (re)assign address, get device descriptor.
4339 * Device connection must be stable, no more debouncing needed.
4340 * Returns device in USB_STATE_ADDRESS, except on error.
4341 *
4342 * If this is called for an already-existing device (as part of
Dan Williams5c79a1e2014-05-20 18:09:26 -07004343 * usb_reset_and_verify_device), the caller must own the device lock and
4344 * the port lock. For a newly detected device that is not accessible
4345 * through any global pointers, it's not necessary to lock the device,
4346 * but it is still necessary to lock the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 */
4348static int
Chase Metzger039b3302015-08-18 20:36:58 -07004349hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 int retry_counter)
4351{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07004353 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Oliver Neukum0d5ce772016-02-17 11:52:43 +01004354 int retries, operations, retval, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 unsigned delay = HUB_SHORT_RESET_TIME;
4356 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004357 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04004358 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
4360 /* root hub ports have a slightly longer reset period
4361 * (from USB 2.0 spec, section 7.1.7.5)
4362 */
4363 if (!hdev->parent) {
4364 delay = HUB_ROOT_RESET_TIME;
4365 if (port1 == hdev->bus->otg_port)
4366 hdev->bus->b_hnp_enable = 0;
4367 }
4368
4369 /* Some low speed devices have problems with the quick delay, so */
4370 /* be a bit pessimistic with those devices. RHbug #23670 */
4371 if (oldspeed == USB_SPEED_LOW)
4372 delay = HUB_LONG_RESET_TIME;
4373
Chris Bainbridgefeb26ac2016-04-25 13:48:38 +01004374 mutex_lock(hcd->address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
Luben Tuikov07194ab2011-02-11 11:33:10 -08004376 /* Reset the device; full speed may morph to high speed */
4377 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07004378 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08004379 if (retval < 0) /* error or disconnect */
4380 goto fail;
4381 /* success, speed is known */
4382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 retval = -ENODEV;
4384
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004385 /* Don't allow speed changes at reset, except usb 3.0 to faster */
4386 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed &&
4387 !(oldspeed == USB_SPEED_SUPER && udev->speed > oldspeed)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 dev_dbg(&udev->dev, "device reset changed speed!\n");
4389 goto fail;
4390 }
4391 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04004392
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
4394 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004395 * For Wireless USB devices, ep0 max packet is always 512 (tho
4396 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 */
4398 switch (udev->speed) {
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004399 case USB_SPEED_SUPER_PLUS:
Sarah Sharp6b403b02009-04-27 19:54:10 -07004400 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08004401 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004402 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004403 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004405 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 break;
4407 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
4408 /* to determine the ep0 maxpacket size, try to read
4409 * the device descriptor to get bMaxPacketSize0 and
4410 * then correct our initial guess.
4411 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004412 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 break;
4414 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004415 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 break;
4417 default:
4418 goto fail;
4419 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004420
4421 if (udev->speed == USB_SPEED_WIRELESS)
4422 speed = "variable speed Wireless";
4423 else
4424 speed = usb_speed_string(udev->speed);
4425
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004426 if (udev->speed < USB_SPEED_SUPER)
Sarah Sharpc6515272009-04-27 19:57:26 -07004427 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004428 "%s %s USB device number %d using %s\n",
4429 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05004430 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
4432 /* Set up TT records, if needed */
4433 if (hdev->tt) {
4434 udev->tt = hdev->tt;
4435 udev->ttport = hdev->ttport;
4436 } else if (udev->speed != USB_SPEED_HIGH
4437 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05004438 if (!hub->tt.hub) {
4439 dev_err(&udev->dev, "parent hub has no TT\n");
4440 retval = -EINVAL;
4441 goto fail;
4442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 udev->tt = &hub->tt;
4444 udev->ttport = port1;
4445 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
4448 * Because device hardware and firmware is sometimes buggy in
4449 * this area, and this is how Linux has done it for ages.
4450 * Change it cautiously.
4451 *
Dan Williams48fc7db2013-12-05 17:07:27 -08004452 * NOTE: If use_new_scheme() is true we will start by issuing
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
4454 * so it may help with some non-standards-compliant devices.
4455 * Otherwise we start with SET_ADDRESS and then try to read the
4456 * first 8 bytes of the device descriptor to get the ep0 maxpacket
4457 * value.
4458 */
Oliver Neukum0d5ce772016-02-17 11:52:43 +01004459 for (retries = 0; retries < GET_DESCRIPTOR_TRIES; (++retries, msleep(100))) {
Dan Williams48fc7db2013-12-05 17:07:27 -08004460 bool did_new_scheme = false;
4461
4462 if (use_new_scheme(udev, retry_counter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 struct usb_device_descriptor *buf;
4464 int r = 0;
4465
Dan Williams48fc7db2013-12-05 17:07:27 -08004466 did_new_scheme = true;
4467 retval = hub_enable_device(udev);
Oliver Neukum938569e2014-02-27 10:57:10 +01004468 if (retval < 0) {
4469 dev_err(&udev->dev,
4470 "hub failed to enable device, error %d\n",
4471 retval);
Dan Williams48fc7db2013-12-05 17:07:27 -08004472 goto fail;
Oliver Neukum938569e2014-02-27 10:57:10 +01004473 }
Dan Williams48fc7db2013-12-05 17:07:27 -08004474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475#define GET_DESCRIPTOR_BUFSIZE 64
4476 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
4477 if (!buf) {
4478 retval = -ENOMEM;
4479 continue;
4480 }
4481
Alan Sternb89ee192007-05-11 10:19:04 -04004482 /* Retry on all errors; some devices are flakey.
4483 * 255 is for WUSB devices, we actually need to use
4484 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 */
Oliver Neukum0d5ce772016-02-17 11:52:43 +01004486 for (operations = 0; operations < 3; ++operations) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 buf->bMaxPacketSize0 = 0;
4488 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
4489 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
4490 USB_DT_DEVICE << 8, 0,
4491 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02004492 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004494 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 if (buf->bDescriptorType ==
4496 USB_DT_DEVICE) {
4497 r = 0;
4498 break;
4499 }
4500 /* FALL THROUGH */
4501 default:
4502 if (r == 0)
4503 r = -EPROTO;
4504 break;
4505 }
Oliver Neukum264904c2016-02-10 11:33:18 +01004506 /*
4507 * Some devices time out if they are powered on
4508 * when already connected. They need a second
4509 * reset. But only on the first attempt,
4510 * lest we get into a time out/reset loop
4511 */
Oliver Neukum0d5ce772016-02-17 11:52:43 +01004512 if (r == 0 || (r == -ETIMEDOUT && retries == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 break;
4514 }
4515 udev->descriptor.bMaxPacketSize0 =
4516 buf->bMaxPacketSize0;
4517 kfree(buf);
4518
Andiry Xu75d7cf72011-09-13 16:41:11 -07004519 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 if (retval < 0) /* error or disconnect */
4521 goto fail;
4522 if (oldspeed != udev->speed) {
4523 dev_dbg(&udev->dev,
4524 "device reset changed speed!\n");
4525 retval = -ENODEV;
4526 goto fail;
4527 }
4528 if (r) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004529 if (r != -ENODEV)
4530 dev_err(&udev->dev, "device descriptor read/64, error %d\n",
4531 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 retval = -EMSGSIZE;
4533 continue;
4534 }
4535#undef GET_DESCRIPTOR_BUFSIZE
4536 }
4537
Matthias Beyer469271f2013-10-10 23:41:27 +02004538 /*
4539 * If device is WUSB, we already assigned an
4540 * unauthorized address in the Connect Ack sequence;
4541 * authorization will assign the final address.
4542 */
Sarah Sharpc6515272009-04-27 19:57:26 -07004543 if (udev->wusb == 0) {
Oliver Neukum0d5ce772016-02-17 11:52:43 +01004544 for (operations = 0; operations < SET_ADDRESS_TRIES; ++operations) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004545 retval = hub_set_address(udev, devnum);
4546 if (retval >= 0)
4547 break;
4548 msleep(200);
4549 }
4550 if (retval < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004551 if (retval != -ENODEV)
4552 dev_err(&udev->dev, "device not accepting address %d, error %d\n",
4553 devnum, retval);
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004554 goto fail;
4555 }
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004556 if (udev->speed >= USB_SPEED_SUPER) {
Sarah Sharpc6515272009-04-27 19:57:26 -07004557 devnum = udev->devnum;
4558 dev_info(&udev->dev,
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004559 "%s SuperSpeed%s USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07004560 (udev->config) ? "reset" : "new",
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004561 (udev->speed == USB_SPEED_SUPER_PLUS) ? "Plus" : "",
Alan Stern3b29b682011-02-22 09:53:41 -05004562 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07004563 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004564
4565 /* cope with hardware quirkiness:
4566 * - let SET_ADDRESS settle, some device hardware wants it
4567 * - read ep0 maxpacket even for high and low speed,
4568 */
4569 msleep(10);
Dan Williams48fc7db2013-12-05 17:07:27 -08004570 /* use_new_scheme() checks the speed which may have
4571 * changed since the initial look so we cache the result
4572 * in did_new_scheme
4573 */
4574 if (did_new_scheme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 break;
Matthias Beyer469271f2013-10-10 23:41:27 +02004576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577
4578 retval = usb_get_device_descriptor(udev, 8);
4579 if (retval < 8) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004580 if (retval != -ENODEV)
4581 dev_err(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004582 "device descriptor read/8, error %d\n",
4583 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 if (retval >= 0)
4585 retval = -EMSGSIZE;
4586 } else {
4587 retval = 0;
4588 break;
4589 }
4590 }
4591 if (retval)
4592 goto fail;
4593
Elric Fud8aec3d2012-03-26 21:16:02 +08004594 /*
4595 * Some superspeed devices have finished the link training process
4596 * and attached to a superspeed hub port, but the device descriptor
4597 * got from those devices show they aren't superspeed devices. Warm
4598 * reset the port attached by the devices can fix them.
4599 */
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004600 if ((udev->speed >= USB_SPEED_SUPER) &&
Elric Fud8aec3d2012-03-26 21:16:02 +08004601 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4602 dev_err(&udev->dev, "got a wrong device descriptor, "
4603 "warm reset device\n");
4604 hub_port_reset(hub, port1, udev,
4605 HUB_BH_RESET_TIME, true);
4606 retval = -EINVAL;
4607 goto fail;
4608 }
4609
Sarah Sharp6b403b02009-04-27 19:54:10 -07004610 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004611 udev->speed >= USB_SPEED_SUPER)
Sarah Sharp6b403b02009-04-27 19:54:10 -07004612 i = 512;
4613 else
4614 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07004615 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04004616 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04004618 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 retval = -EMSGSIZE;
4620 goto fail;
4621 }
Alan Stern56626a72010-10-14 15:25:21 -04004622 if (udev->speed == USB_SPEED_FULL)
4623 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4624 else
4625 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004627 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004629
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4631 if (retval < (signed)sizeof(udev->descriptor)) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004632 if (retval != -ENODEV)
4633 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4634 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 if (retval >= 0)
4636 retval = -ENOMSG;
4637 goto fail;
4638 }
4639
Alan Sternad87e032015-12-10 15:27:21 -05004640 usb_detect_quirks(udev);
4641
Andiry Xu1ff4df52011-09-23 14:19:48 -07004642 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4643 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004644 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004645 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004646 usb_set_lpm_parameters(udev);
4647 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004648 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004649
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004651 /* notify HCD that we have a device connected and addressed */
4652 if (hcd->driver->update_device)
4653 hcd->driver->update_device(hcd, udev);
Mathias Nyman890dae82013-09-30 17:26:31 +03004654 hub_set_initial_usb2_lpm_policy(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004656 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004658 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004659 }
Chris Bainbridgefeb26ac2016-04-25 13:48:38 +01004660 mutex_unlock(hcd->address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 return retval;
4662}
4663
4664static void
Chase Metzger039b3302015-08-18 20:36:58 -07004665check_highspeed(struct usb_hub *hub, struct usb_device *udev, int port1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666{
4667 struct usb_qualifier_descriptor *qual;
4668 int status;
4669
Johan Hovold2a159382014-08-25 17:51:26 +02004670 if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER)
4671 return;
4672
Chase Metzger039b3302015-08-18 20:36:58 -07004673 qual = kmalloc(sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 if (qual == NULL)
4675 return;
4676
Chase Metzger039b3302015-08-18 20:36:58 -07004677 status = usb_get_descriptor(udev, USB_DT_DEVICE_QUALIFIER, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 qual, sizeof *qual);
4679 if (status == sizeof *qual) {
4680 dev_info(&udev->dev, "not running at top speed; "
4681 "connect to a high speed hub\n");
4682 /* hub LEDs are probably harder to miss than syslog */
4683 if (hub->has_indicators) {
4684 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08004685 queue_delayed_work(system_power_efficient_wq,
4686 &hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 }
4688 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004689 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690}
4691
4692static unsigned
Chase Metzger039b3302015-08-18 20:36:58 -07004693hub_power_remaining(struct usb_hub *hub)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694{
4695 struct usb_device *hdev = hub->hdev;
4696 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004697 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
Alan Stern55c52712005-11-23 12:03:12 -05004699 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 return 0;
4701
Alan Stern55c52712005-11-23 12:03:12 -05004702 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4703 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004704 struct usb_port *port_dev = hub->ports[port1 - 1];
4705 struct usb_device *udev = port_dev->child;
4706 unsigned unit_load;
4707 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
4709 if (!udev)
4710 continue;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004711 if (hub_is_superspeed(udev))
4712 unit_load = 150;
4713 else
4714 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004716 /*
4717 * Unconfigured devices may not use more than one unit load,
4718 * or 8mA for OTG ports
4719 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 if (udev->actconfig)
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +01004721 delta = usb_get_max_power(udev, udev->actconfig);
Alan Stern55c52712005-11-23 12:03:12 -05004722 else if (port1 != udev->bus->otg_port || hdev->parent)
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004723 delta = unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 else
Alan Stern55c52712005-11-23 12:03:12 -05004725 delta = 8;
4726 if (delta > hub->mA_per_port)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004727 dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n",
4728 delta, hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 remaining -= delta;
4730 }
4731 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004732 dev_warn(hub->intfdev, "%dmA over power budget!\n",
Matthias Beyer469271f2013-10-10 23:41:27 +02004733 -remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 remaining = 0;
4735 }
4736 return remaining;
4737}
4738
Dan Williamsaf376a42014-05-20 18:09:15 -07004739static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
4740 u16 portchange)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741{
Alan Stern7c2beb12017-08-01 10:41:56 -04004742 int status = -ENODEV;
4743 int i;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004744 unsigned unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 struct usb_device *hdev = hub->hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004747 struct usb_port *port_dev = hub->ports[port1 - 1];
Dan Williamsaf376a42014-05-20 18:09:15 -07004748 struct usb_device *udev = port_dev->child;
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004749 static int unreliable_port = -1;
Alan Stern8808f002008-04-28 11:06:55 -04004750
Alan Stern24618b02008-04-28 11:06:28 -04004751 /* Disconnect any existing devices under this port */
Peter Chenb76baa82012-11-09 09:44:43 +08004752 if (udev) {
Peter Chenb2108f12014-11-04 11:14:31 +08004753 if (hcd->usb_phy && !hdev->parent)
Antoine Tenart3d46e732014-09-24 23:05:50 +04004754 usb_phy_notify_disconnect(hcd->usb_phy, udev->speed);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004755 usb_disconnect(&port_dev->child);
Peter Chenb76baa82012-11-09 09:44:43 +08004756 }
Alan Stern24618b02008-04-28 11:06:28 -04004757
Alan Stern253e0572009-10-27 15:20:13 -04004758 /* We can forget about a "removed" device when there's a physical
4759 * disconnect or the connect status changes.
4760 */
4761 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4762 (portchange & USB_PORT_STAT_C_CONNECTION))
4763 clear_bit(port1, hub->removed_bits);
4764
Alan Stern5257d972008-09-22 14:43:08 -04004765 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4766 USB_PORT_STAT_C_ENABLE)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004767 status = hub_port_debounce_be_stable(hub, port1);
Alan Stern5257d972008-09-22 14:43:08 -04004768 if (status < 0) {
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004769 if (status != -ENODEV &&
4770 port1 != unreliable_port &&
4771 printk_ratelimit())
Takashi Iwaidd5f5002014-08-19 17:37:55 +02004772 dev_err(&port_dev->dev, "connect-debounce failed\n");
Alan Stern5257d972008-09-22 14:43:08 -04004773 portstatus &= ~USB_PORT_STAT_CONNECTION;
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004774 unreliable_port = port1;
Alan Stern5257d972008-09-22 14:43:08 -04004775 } else {
4776 portstatus = status;
4777 }
4778 }
4779
Alan Stern253e0572009-10-27 15:20:13 -04004780 /* Return now if debouncing failed or nothing is connected or
4781 * the device was "removed".
4782 */
4783 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4784 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
Deepak Dasfbaecff2015-01-21 23:39:58 +05304786 /*
4787 * maybe switch power back on (e.g. root hub was reset)
4788 * but only if the port isn't owned by someone else.
4789 */
Dan Williams9262c192014-05-20 18:08:12 -07004790 if (hub_is_port_power_switchable(hub)
Deepak Dasfbaecff2015-01-21 23:39:58 +05304791 && !port_is_power_on(hub, portstatus)
4792 && !port_dev->port_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 if (portstatus & USB_PORT_STAT_ENABLE)
Matthias Beyer469271f2013-10-10 23:41:27 +02004796 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 return;
4798 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004799 if (hub_is_superspeed(hub->hdev))
4800 unit_load = 150;
4801 else
4802 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
Alan Sterne9e88fb2013-03-27 16:14:01 -04004804 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
4807 /* reallocate for each attempt, since references
4808 * to the previous one can escape in various ways
4809 */
4810 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4811 if (!udev) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004812 dev_err(&port_dev->dev,
4813 "couldn't allocate usb_device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 goto done;
4815 }
4816
4817 usb_set_device_state(udev, USB_STATE_POWERED);
Matthias Beyer469271f2013-10-10 23:41:27 +02004818 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004819 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004820 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004821
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004822 /* Devices connected to SuperSpeed hubs are USB 3.0 or later */
Sarah Sharp131dec32010-12-06 21:00:19 -08004823 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004824 udev->speed = USB_SPEED_SUPER;
4825 else
4826 udev->speed = USB_SPEED_UNKNOWN;
4827
Alan Stern3b29b682011-02-22 09:53:41 -05004828 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004829 if (udev->devnum <= 0) {
4830 status = -ENOTCONN; /* Don't retry */
4831 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004832 }
4833
Sarah Sharpe7b77172009-04-27 19:54:26 -07004834 /* reset (non-USB 3.0 devices) and get descriptor */
Dan Williams5c79a1e2014-05-20 18:09:26 -07004835 usb_lock_port(port_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 status = hub_port_init(hub, udev, port1, i);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004837 usb_unlock_port(port_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 if (status < 0)
4839 goto loop;
4840
Phil Dibowitz93362a82010-07-22 00:05:01 +02004841 if (udev->quirks & USB_QUIRK_DELAY_INIT)
Dmitry Fleytman43feb292017-09-05 11:40:56 +03004842 msleep(2000);
Phil Dibowitz93362a82010-07-22 00:05:01 +02004843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 /* consecutive bus-powered hubs aren't reliable; they can
4845 * violate the voltage drop budget. if the new child has
4846 * a "powered" LED, users should notice we didn't enable it
4847 * (without reading syslog), even without per-port LEDs
4848 * on the parent.
4849 */
4850 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004851 && udev->bus_mA <= unit_load) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 u16 devstat;
4853
4854 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4855 &devstat);
Alan Stern15b73362013-07-30 15:35:40 -04004856 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 dev_dbg(&udev->dev, "get status %d ?\n", status);
4858 goto loop_disable;
4859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4861 dev_err(&udev->dev,
4862 "can't connect bus-powered hub "
4863 "to this port\n");
4864 if (hub->has_indicators) {
4865 hub->indicator[port1-1] =
4866 INDICATOR_AMBER_BLINK;
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08004867 queue_delayed_work(
4868 system_power_efficient_wq,
4869 &hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 }
4871 status = -ENOTCONN; /* Don't retry */
4872 goto loop_disable;
4873 }
4874 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004875
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 /* check for devices running slower than they could */
4877 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4878 && udev->speed == USB_SPEED_FULL
4879 && highspeed_hubs != 0)
Chase Metzger039b3302015-08-18 20:36:58 -07004880 check_highspeed(hub, udev, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004882 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 * udev becomes globally accessible, although presumably
4884 * no one will look at it until hdev is unlocked.
4885 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 status = 0;
4887
Dan Williamsd8521af2014-05-20 18:08:28 -07004888 mutex_lock(&usb_port_peer_mutex);
4889
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 /* We mustn't add new devices if the parent hub has
4891 * been disconnected; we would race with the
4892 * recursively_mark_NOTATTACHED() routine.
4893 */
4894 spin_lock_irq(&device_state_lock);
4895 if (hdev->state == USB_STATE_NOTATTACHED)
4896 status = -ENOTCONN;
4897 else
Dan Williamsd99f6b42014-05-20 18:08:17 -07004898 port_dev->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 spin_unlock_irq(&device_state_lock);
Dan Williamsd8521af2014-05-20 18:08:28 -07004900 mutex_unlock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
4902 /* Run it through the hoops (find a driver, etc) */
4903 if (!status) {
4904 status = usb_new_device(udev);
4905 if (status) {
Dan Williamsd8521af2014-05-20 18:08:28 -07004906 mutex_lock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 spin_lock_irq(&device_state_lock);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004908 port_dev->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 spin_unlock_irq(&device_state_lock);
Dan Williamsd8521af2014-05-20 18:08:28 -07004910 mutex_unlock(&usb_port_peer_mutex);
Tony Zheng01ed67d2014-10-17 19:43:02 +08004911 } else {
4912 if (hcd->usb_phy && !hdev->parent)
4913 usb_phy_notify_connect(hcd->usb_phy,
4914 udev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 }
4916 }
4917
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 if (status)
4919 goto loop_disable;
4920
4921 status = hub_power_remaining(hub);
4922 if (status)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004923 dev_dbg(hub->intfdev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
4925 return;
4926
4927loop_disable:
4928 hub_port_disable(hub, port1, 1);
4929loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004930 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004931 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004932 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004934 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 break;
Mike Looijmanse328dff2017-11-09 13:16:46 +01004936
4937 /* When halfway through our retry count, power-cycle the port */
4938 if (i == (SET_CONFIG_TRIES / 2) - 1) {
4939 dev_info(&port_dev->dev, "attempt power cycle\n");
4940 usb_hub_set_port_power(hdev, hub, port1, false);
4941 msleep(2 * hub_power_on_good_delay(hub));
4942 usb_hub_set_port_power(hdev, hub, port1, true);
4943 msleep(hub_power_on_good_delay(hub));
4944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 }
Alan Stern3a311552008-05-20 16:58:29 -04004946 if (hub->hdev->parent ||
4947 !hcd->driver->port_handed_over ||
Alan Sterne9e88fb2013-03-27 16:14:01 -04004948 !(hcd->driver->port_handed_over)(hcd, port1)) {
4949 if (status != -ENOTCONN && status != -ENODEV)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004950 dev_err(&port_dev->dev,
4951 "unable to enumerate USB device\n");
Alan Sterne9e88fb2013-03-27 16:14:01 -04004952 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004953
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954done:
4955 hub_port_disable(hub, port1, 1);
Alan Stern7c2beb12017-08-01 10:41:56 -04004956 if (hcd->driver->relinquish_port && !hub->hdev->parent) {
4957 if (status != -ENOTCONN && status != -ENODEV)
4958 hcd->driver->relinquish_port(hcd, port1);
4959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960}
4961
Dan Williamsaf376a42014-05-20 18:09:15 -07004962/* Handle physical or logical connection change events.
4963 * This routine is called when:
4964 * a port connection-change occurs;
4965 * a port enable-change occurs (often caused by EMI);
4966 * usb_reset_and_verify_device() encounters changed descriptors (as from
4967 * a firmware download)
4968 * caller already locked the hub
4969 */
4970static void hub_port_connect_change(struct usb_hub *hub, int port1,
4971 u16 portstatus, u16 portchange)
Dan Williams5c79a1e2014-05-20 18:09:26 -07004972 __must_hold(&port_dev->status_lock)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004973{
Dan Williamsaf376a42014-05-20 18:09:15 -07004974 struct usb_port *port_dev = hub->ports[port1 - 1];
4975 struct usb_device *udev = port_dev->child;
4976 int status = -ENODEV;
Sarah Sharp714b07b2012-01-24 13:53:18 -08004977
Dan Williamsaf376a42014-05-20 18:09:15 -07004978 dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus,
4979 portchange, portspeed(hub, portstatus));
4980
4981 if (hub->has_indicators) {
4982 set_port_led(hub, port1, HUB_LED_AUTO);
4983 hub->indicator[port1-1] = INDICATOR_AUTO;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004984 }
4985
Dan Williamsaf376a42014-05-20 18:09:15 -07004986#ifdef CONFIG_USB_OTG
4987 /* during HNP, don't repeat the debounce */
4988 if (hub->hdev->bus->is_b_host)
4989 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4990 USB_PORT_STAT_C_ENABLE);
4991#endif
Sarah Sharp714b07b2012-01-24 13:53:18 -08004992
Dan Williamsaf376a42014-05-20 18:09:15 -07004993 /* Try to resuscitate an existing device */
4994 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4995 udev->state != USB_STATE_NOTATTACHED) {
4996 if (portstatus & USB_PORT_STAT_ENABLE) {
4997 status = 0; /* Nothing to do */
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01004998#ifdef CONFIG_PM
Dan Williamsaf376a42014-05-20 18:09:15 -07004999 } else if (udev->state == USB_STATE_SUSPENDED &&
5000 udev->persist_enabled) {
5001 /* For a suspended device, treat this as a
5002 * remote wakeup event.
5003 */
Dan Williams5c79a1e2014-05-20 18:09:26 -07005004 usb_unlock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07005005 status = usb_remote_wakeup(udev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07005006 usb_lock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07005007#endif
5008 } else {
5009 /* Don't resuscitate */;
5010 }
Sarah Sharp714b07b2012-01-24 13:53:18 -08005011 }
Dan Williamsaf376a42014-05-20 18:09:15 -07005012 clear_bit(port1, hub->change_bits);
5013
Dan Williams5c79a1e2014-05-20 18:09:26 -07005014 /* successfully revalidated the connection */
Dan Williamsaf376a42014-05-20 18:09:15 -07005015 if (status == 0)
5016 return;
5017
Dan Williams5c79a1e2014-05-20 18:09:26 -07005018 usb_unlock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07005019 hub_port_connect(hub, port1, portstatus, portchange);
Dan Williams5c79a1e2014-05-20 18:09:26 -07005020 usb_lock_port(port_dev);
Sarah Sharp714b07b2012-01-24 13:53:18 -08005021}
5022
Dan Williamsaf376a42014-05-20 18:09:15 -07005023static void port_event(struct usb_hub *hub, int port1)
Dan Williams5c79a1e2014-05-20 18:09:26 -07005024 __must_hold(&port_dev->status_lock)
Dan Williamsaf376a42014-05-20 18:09:15 -07005025{
Zhuang Jin Can7671bd12015-01-26 23:30:39 +08005026 int connect_change;
Dan Williamsaf376a42014-05-20 18:09:15 -07005027 struct usb_port *port_dev = hub->ports[port1 - 1];
5028 struct usb_device *udev = port_dev->child;
5029 struct usb_device *hdev = hub->hdev;
5030 u16 portstatus, portchange;
5031
5032 connect_change = test_bit(port1, hub->change_bits);
5033 clear_bit(port1, hub->event_bits);
5034 clear_bit(port1, hub->wakeup_bits);
5035
5036 if (hub_port_status(hub, port1, &portstatus, &portchange) < 0)
5037 return;
5038
5039 if (portchange & USB_PORT_STAT_C_CONNECTION) {
5040 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
5041 connect_change = 1;
5042 }
5043
5044 if (portchange & USB_PORT_STAT_C_ENABLE) {
5045 if (!connect_change)
5046 dev_dbg(&port_dev->dev, "enable change, status %08x\n",
5047 portstatus);
5048 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
5049
5050 /*
5051 * EM interference sometimes causes badly shielded USB devices
5052 * to be shutdown by the hub, this hack enables them again.
5053 * Works at least with mouse driver.
5054 */
5055 if (!(portstatus & USB_PORT_STAT_ENABLE)
5056 && !connect_change && udev) {
5057 dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n");
5058 connect_change = 1;
5059 }
5060 }
5061
5062 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
5063 u16 status = 0, unused;
5064
5065 dev_dbg(&port_dev->dev, "over-current change\n");
5066 usb_clear_port_feature(hdev, port1,
5067 USB_PORT_FEAT_C_OVER_CURRENT);
5068 msleep(100); /* Cool down */
5069 hub_power_on(hub, true);
5070 hub_port_status(hub, port1, &status, &unused);
5071 if (status & USB_PORT_STAT_OVERCURRENT)
5072 dev_err(&port_dev->dev, "over-current condition\n");
5073 }
5074
5075 if (portchange & USB_PORT_STAT_C_RESET) {
5076 dev_dbg(&port_dev->dev, "reset change\n");
5077 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET);
5078 }
5079 if ((portchange & USB_PORT_STAT_C_BH_RESET)
5080 && hub_is_superspeed(hdev)) {
5081 dev_dbg(&port_dev->dev, "warm reset change\n");
5082 usb_clear_port_feature(hdev, port1,
5083 USB_PORT_FEAT_C_BH_PORT_RESET);
5084 }
5085 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
5086 dev_dbg(&port_dev->dev, "link state change\n");
5087 usb_clear_port_feature(hdev, port1,
5088 USB_PORT_FEAT_C_PORT_LINK_STATE);
5089 }
5090 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
5091 dev_warn(&port_dev->dev, "config error\n");
5092 usb_clear_port_feature(hdev, port1,
5093 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
5094 }
5095
Dan Williams097a1552014-05-20 18:09:20 -07005096 /* skip port actions that require the port to be powered on */
5097 if (!pm_runtime_active(&port_dev->dev))
5098 return;
5099
Dan Williamsaf376a42014-05-20 18:09:15 -07005100 if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange))
5101 connect_change = 1;
5102
5103 /*
5104 * Warm reset a USB3 protocol port if it's in
5105 * SS.Inactive state.
5106 */
Dan Williams3cd12f92014-05-29 12:58:46 -07005107 if (hub_port_warm_reset_required(hub, port1, portstatus)) {
Dan Williamsaf376a42014-05-20 18:09:15 -07005108 dev_dbg(&port_dev->dev, "do warm reset\n");
5109 if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION)
5110 || udev->state == USB_STATE_NOTATTACHED) {
5111 if (hub_port_reset(hub, port1, NULL,
5112 HUB_BH_RESET_TIME, true) < 0)
5113 hub_port_disable(hub, port1, 1);
Zhuang Jin Can7671bd12015-01-26 23:30:39 +08005114 } else {
5115 usb_unlock_port(port_dev);
5116 usb_lock_device(udev);
5117 usb_reset_device(udev);
5118 usb_unlock_device(udev);
5119 usb_lock_port(port_dev);
5120 connect_change = 0;
5121 }
Dan Williamsaf376a42014-05-20 18:09:15 -07005122 }
5123
5124 if (connect_change)
5125 hub_port_connect_change(hub, port1, portstatus, portchange);
5126}
5127
Petr Mladek32a695892014-09-19 17:32:21 +02005128static void hub_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 struct usb_device *hdev;
5131 struct usb_interface *intf;
5132 struct usb_hub *hub;
5133 struct device *hub_dev;
5134 u16 hubstatus;
5135 u16 hubchange;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
Petr Mladek32a695892014-09-19 17:32:21 +02005138 hub = container_of(work, struct usb_hub, events);
Petr Mladekeb6e2922014-09-19 17:32:20 +02005139 hdev = hub->hdev;
5140 hub_dev = hub->intfdev;
5141 intf = to_usb_interface(hub_dev);
Petr Mladek32a695892014-09-19 17:32:21 +02005142
Petr Mladekeb6e2922014-09-19 17:32:20 +02005143 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
5144 hdev->state, hdev->maxchild,
5145 /* NOTE: expects max 15 ports... */
5146 (u16) hub->change_bits[0],
5147 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
Petr Mladekeb6e2922014-09-19 17:32:20 +02005149 /* Lock the device, then check to see if we were
5150 * disconnected while waiting for the lock to succeed. */
5151 usb_lock_device(hdev);
5152 if (unlikely(hub->disconnected))
Petr Mladek32a695892014-09-19 17:32:21 +02005153 goto out_hdev_lock;
Petr Mladekeb6e2922014-09-19 17:32:20 +02005154
5155 /* If the hub has died, clean up after it */
5156 if (hdev->state == USB_STATE_NOTATTACHED) {
5157 hub->error = -ENODEV;
5158 hub_quiesce(hub, HUB_DISCONNECT);
Petr Mladek32a695892014-09-19 17:32:21 +02005159 goto out_hdev_lock;
Petr Mladekeb6e2922014-09-19 17:32:20 +02005160 }
5161
5162 /* Autoresume */
5163 ret = usb_autopm_get_interface(intf);
5164 if (ret) {
5165 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Petr Mladek32a695892014-09-19 17:32:21 +02005166 goto out_hdev_lock;
Petr Mladekeb6e2922014-09-19 17:32:20 +02005167 }
5168
5169 /* If this is an inactive hub, do nothing */
5170 if (hub->quiescing)
5171 goto out_autopm;
5172
5173 if (hub->error) {
5174 dev_dbg(hub_dev, "resetting for error %d\n", hub->error);
5175
5176 ret = usb_reset_device(hdev);
Alan Stern40f122f2006-11-09 14:44:33 -05005177 if (ret) {
Petr Mladekeb6e2922014-09-19 17:32:20 +02005178 dev_dbg(hub_dev, "error resetting hub: %d\n", ret);
5179 goto out_autopm;
Alan Stern40f122f2006-11-09 14:44:33 -05005180 }
5181
Petr Mladekeb6e2922014-09-19 17:32:20 +02005182 hub->nerrors = 0;
5183 hub->error = 0;
5184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
Petr Mladekeb6e2922014-09-19 17:32:20 +02005186 /* deal with port status changes */
5187 for (i = 1; i <= hdev->maxchild; i++) {
5188 struct usb_port *port_dev = hub->ports[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189
Petr Mladekeb6e2922014-09-19 17:32:20 +02005190 if (test_bit(i, hub->event_bits)
5191 || test_bit(i, hub->change_bits)
5192 || test_bit(i, hub->wakeup_bits)) {
5193 /*
5194 * The get_noresume and barrier ensure that if
5195 * the port was in the process of resuming, we
5196 * flush that work and keep the port active for
5197 * the duration of the port_event(). However,
5198 * if the port is runtime pm suspended
5199 * (powered-off), we leave it in that state, run
5200 * an abbreviated port_event(), and move on.
5201 */
5202 pm_runtime_get_noresume(&port_dev->dev);
5203 pm_runtime_barrier(&port_dev->dev);
5204 usb_lock_port(port_dev);
5205 port_event(hub, i);
5206 usb_unlock_port(port_dev);
5207 pm_runtime_put_sync(&port_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 }
Petr Mladekeb6e2922014-09-19 17:32:20 +02005209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210
Petr Mladekeb6e2922014-09-19 17:32:20 +02005211 /* deal with hub status changes */
5212 if (test_and_clear_bit(0, hub->event_bits) == 0)
5213 ; /* do nothing */
5214 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
5215 dev_err(hub_dev, "get_hub_status failed\n");
5216 else {
5217 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
5218 dev_dbg(hub_dev, "power change\n");
5219 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
5220 if (hubstatus & HUB_STATUS_LOCAL_POWER)
5221 /* FIXME: Is this always true? */
5222 hub->limited_power = 1;
5223 else
5224 hub->limited_power = 0;
Dan Williamsaf376a42014-05-20 18:09:15 -07005225 }
Petr Mladekeb6e2922014-09-19 17:32:20 +02005226 if (hubchange & HUB_CHANGE_OVERCURRENT) {
5227 u16 status = 0;
5228 u16 unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229
Petr Mladekeb6e2922014-09-19 17:32:20 +02005230 dev_dbg(hub_dev, "over-current change\n");
5231 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
5232 msleep(500); /* Cool down */
5233 hub_power_on(hub, true);
5234 hub_hub_status(hub, &status, &unused);
5235 if (status & HUB_STATUS_OVERCURRENT)
5236 dev_err(hub_dev, "over-current condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 }
Petr Mladekeb6e2922014-09-19 17:32:20 +02005238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239
Petr Mladekeb6e2922014-09-19 17:32:20 +02005240out_autopm:
5241 /* Balance the usb_autopm_get_interface() above */
5242 usb_autopm_put_interface_no_suspend(intf);
Petr Mladek32a695892014-09-19 17:32:21 +02005243out_hdev_lock:
Petr Mladekeb6e2922014-09-19 17:32:20 +02005244 usb_unlock_device(hdev);
Petr Mladek32a695892014-09-19 17:32:21 +02005245
5246 /* Balance the stuff in kick_hub_wq() and allow autosuspend */
5247 usb_autopm_put_interface(intf);
Petr Mladekeb6e2922014-09-19 17:32:20 +02005248 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249}
5250
Németh Márton1e927d92010-01-10 15:34:53 +01005251static const struct usb_device_id hub_id_table[] = {
Ming Leie6f30de2012-10-24 11:59:24 +08005252 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
Matthias Beyer469271f2013-10-10 23:41:27 +02005253 | USB_DEVICE_ID_MATCH_INT_CLASS,
Ming Leie6f30de2012-10-24 11:59:24 +08005254 .idVendor = USB_VENDOR_GENESYS_LOGIC,
5255 .bInterfaceClass = USB_CLASS_HUB,
5256 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
5258 .bDeviceClass = USB_CLASS_HUB},
5259 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
5260 .bInterfaceClass = USB_CLASS_HUB},
5261 { } /* Terminating entry */
5262};
5263
Chase Metzger039b3302015-08-18 20:36:58 -07005264MODULE_DEVICE_TABLE(usb, hub_id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265
5266static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 .name = "hub",
5268 .probe = hub_probe,
5269 .disconnect = hub_disconnect,
5270 .suspend = hub_suspend,
5271 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04005272 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04005273 .pre_reset = hub_pre_reset,
5274 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02005275 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05005277 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278};
5279
5280int usb_hub_init(void)
5281{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 if (usb_register(&hub_driver) < 0) {
5283 printk(KERN_ERR "%s: can't register hub driver\n",
5284 usbcore_name);
5285 return -1;
5286 }
5287
Petr Mladek32a695892014-09-19 17:32:21 +02005288 /*
5289 * The workqueue needs to be freezable to avoid interfering with
5290 * USB-PERSIST port handover. Otherwise it might see that a full-speed
5291 * device was gone before the EHCI controller had handed its port
5292 * over to the companion full-speed controller.
Petr Mladek32a695892014-09-19 17:32:21 +02005293 */
Petr Mladek638139e2014-09-19 17:32:24 +02005294 hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0);
Petr Mladek32a695892014-09-19 17:32:21 +02005295 if (hub_wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297
5298 /* Fall through if kernel_thread failed */
5299 usb_deregister(&hub_driver);
Petr Mladek32a695892014-09-19 17:32:21 +02005300 pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301
5302 return -1;
5303}
5304
5305void usb_hub_cleanup(void)
5306{
Petr Mladek32a695892014-09-19 17:32:21 +02005307 destroy_workqueue(hub_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308
5309 /*
5310 * Hub resources are freed for us by usb_deregister. It calls
5311 * usb_driver_purge on every device which in turn calls that
5312 * devices disconnect function if it is using this driver.
5313 * The hub_disconnect function takes care of releasing the
5314 * individual hub resources. -greg
5315 */
5316 usb_deregister(&hub_driver);
5317} /* usb_hub_cleanup() */
5318
Alan Sterneb764c42008-03-03 15:16:04 -05005319static int descriptors_changed(struct usb_device *udev,
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005320 struct usb_device_descriptor *old_device_descriptor,
5321 struct usb_host_bos *old_bos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322{
Alan Sterneb764c42008-03-03 15:16:04 -05005323 int changed = 0;
5324 unsigned index;
5325 unsigned serial_len = 0;
5326 unsigned len;
5327 unsigned old_length;
5328 int length;
5329 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330
Alan Sterneb764c42008-03-03 15:16:04 -05005331 if (memcmp(&udev->descriptor, old_device_descriptor,
5332 sizeof(*old_device_descriptor)) != 0)
5333 return 1;
5334
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005335 if ((old_bos && !udev->bos) || (!old_bos && udev->bos))
5336 return 1;
5337 if (udev->bos) {
Xenia Ragiadakoub9a10482013-08-31 04:40:49 +03005338 len = le16_to_cpu(udev->bos->desc->wTotalLength);
5339 if (len != le16_to_cpu(old_bos->desc->wTotalLength))
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005340 return 1;
Xenia Ragiadakoub9a10482013-08-31 04:40:49 +03005341 if (memcmp(udev->bos->desc, old_bos->desc, len))
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005342 return 1;
5343 }
5344
Alan Sterneb764c42008-03-03 15:16:04 -05005345 /* Since the idVendor, idProduct, and bcdDevice values in the
5346 * device descriptor haven't changed, we will assume the
5347 * Manufacturer and Product strings haven't changed either.
5348 * But the SerialNumber string could be different (e.g., a
5349 * different flash card of the same brand).
5350 */
5351 if (udev->serial)
5352 serial_len = strlen(udev->serial) + 1;
5353
5354 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05005356 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
5357 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 }
Alan Sterneb764c42008-03-03 15:16:04 -05005359
Oliver Neukum0cc1a512008-01-10 10:31:48 +01005360 buf = kmalloc(len, GFP_NOIO);
Wolfram Sangb74e7062016-08-25 19:38:59 +02005361 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 /* assume the worst */
5363 return 1;
Wolfram Sangb74e7062016-08-25 19:38:59 +02005364
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05005366 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
5368 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05005369 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 dev_dbg(&udev->dev, "config index %d, error %d\n",
5371 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05005372 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 break;
5374 }
Chase Metzger039b3302015-08-18 20:36:58 -07005375 if (memcmp(buf, udev->rawdescriptors[index], old_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 != 0) {
5377 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05005378 index,
5379 ((struct usb_config_descriptor *) buf)->
5380 bConfigurationValue);
5381 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 break;
5383 }
5384 }
Alan Sterneb764c42008-03-03 15:16:04 -05005385
5386 if (!changed && serial_len) {
5387 length = usb_string(udev, udev->descriptor.iSerialNumber,
5388 buf, serial_len);
5389 if (length + 1 != serial_len) {
5390 dev_dbg(&udev->dev, "serial string error %d\n",
5391 length);
5392 changed = 1;
5393 } else if (memcmp(buf, udev->serial, length) != 0) {
5394 dev_dbg(&udev->dev, "serial string changed\n");
5395 changed = 1;
5396 }
5397 }
5398
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05005400 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401}
5402
5403/**
Ming Lei742120c2008-06-18 22:00:29 +08005404 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5406 *
Alan Stern79efa092006-06-01 13:33:42 -04005407 * WARNING - don't use this routine to reset a composite device
5408 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08005409 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 *
5411 * Do a port reset, reassign the device's address, and establish its
5412 * former operating configuration. If the reset fails, or the device's
5413 * descriptors change from their values before the reset, or the original
5414 * configuration and altsettings cannot be restored, a flag will be set
Petr Mladek37ebb542014-09-19 17:32:23 +02005415 * telling hub_wq to pretend the device has been disconnected and then
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 * re-connected. All drivers will be unbound, and the device will be
5417 * re-enumerated and probed all over again.
5418 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005419 * Return: 0 if the reset succeeded, -ENODEV if the device has been
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 * flagged for logical disconnection, or some other negative error code
5421 * if the reset wasn't even attempted.
5422 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005423 * Note:
Dan Williams5c79a1e2014-05-20 18:09:26 -07005424 * The caller must own the device lock and the port lock, the latter is
5425 * taken by usb_reset_device(). For example, it's safe to use
5426 * usb_reset_device() from a driver probe() routine after downloading
5427 * new firmware. For calls that might not occur during probe(), drivers
5428 * should lock the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04005429 *
5430 * Locking exception: This routine may also be called from within an
5431 * autoresume handler. Such usage won't conflict with other tasks
5432 * holding the device lock because these tasks should always call
Dan Williams5c79a1e2014-05-20 18:09:26 -07005433 * usb_autopm_resume_device(), thereby preventing any unwanted
5434 * autoresume. The autoresume handler is expected to have already
5435 * acquired the port lock before calling this routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 */
Ming Lei742120c2008-06-18 22:00:29 +08005437static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438{
5439 struct usb_device *parent_hdev = udev->parent;
5440 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005441 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 struct usb_device_descriptor descriptor = udev->descriptor;
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005443 struct usb_host_bos *bos;
Hans de Goede7a7b5622013-11-08 16:37:26 +01005444 int i, j, ret = 0;
Alan Stern12c3da32005-11-23 12:09:52 -05005445 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
5447 if (udev->state == USB_STATE_NOTATTACHED ||
5448 udev->state == USB_STATE_SUSPENDED) {
5449 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5450 udev->state);
5451 return -EINVAL;
5452 }
5453
Dan Williams5c79a1e2014-05-20 18:09:26 -07005454 if (!parent_hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 return -EISDIR;
Dan Williams5c79a1e2014-05-20 18:09:26 -07005456
Lan Tianyuad493e52013-01-23 04:26:30 +08005457 parent_hub = usb_hub_to_struct_hub(parent_hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Sarah Sharpdcc01c02013-09-30 17:26:29 +03005459 /* Disable USB2 hardware LPM.
5460 * It will be re-enabled by the enumeration process.
5461 */
5462 if (udev->usb2_hw_lpm_enabled == 1)
5463 usb_set_usb2_hardware_lpm(udev, 0);
5464
Sarah Sharpf74631e2012-06-25 12:08:08 -07005465 /* Disable LPM and LTM while we reset the device and reinstall the alt
5466 * settings. Device-initiated LPM settings, and system exit latency
5467 * settings are cleared when the device is reset, so we have to set
5468 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005469 */
5470 ret = usb_unlocked_disable_lpm(udev);
5471 if (ret) {
5472 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
Hans Yang464ad8c2015-12-01 16:54:59 +08005473 goto re_enumerate_no_bos;
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005474 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07005475 ret = usb_disable_ltm(udev);
5476 if (ret) {
5477 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
5478 __func__);
Hans Yang464ad8c2015-12-01 16:54:59 +08005479 goto re_enumerate_no_bos;
Sarah Sharpf74631e2012-06-25 12:08:08 -07005480 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005481
Hans Yang464ad8c2015-12-01 16:54:59 +08005482 bos = udev->bos;
Greg Kroah-Hartmane5bdfd52016-02-20 14:19:34 -08005483 udev->bos = NULL;
Hans Yang464ad8c2015-12-01 16:54:59 +08005484
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
5486
5487 /* ep0 maxpacket size may change; let the HCD know about it.
5488 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07005489 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04005491 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 break;
5493 }
Alan Sternd5cbad42006-08-11 16:52:39 -04005494
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 if (ret < 0)
5496 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005497
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 /* Device might have changed firmware (DFU or similar) */
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005499 if (descriptors_changed(udev, &descriptor, bos)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 dev_info(&udev->dev, "device firmware changed\n");
5501 udev->descriptor = descriptor; /* for disconnect() calls */
5502 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005503 }
Alan Stern4fe03872009-02-26 10:21:02 -05005504
5505 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 if (!udev->actconfig)
5507 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005508
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005509 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005510 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
5511 if (ret < 0) {
5512 dev_warn(&udev->dev,
5513 "Busted HC? Not enough HCD resources for "
5514 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005515 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005516 goto re_enumerate;
5517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
5519 USB_REQ_SET_CONFIGURATION, 0,
5520 udev->actconfig->desc.bConfigurationValue, 0,
5521 NULL, 0, USB_CTRL_SET_TIMEOUT);
5522 if (ret < 0) {
5523 dev_err(&udev->dev,
5524 "can't restore configuration #%d (error=%d)\n",
5525 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005526 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005528 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005529 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 usb_set_device_state(udev, USB_STATE_CONFIGURED);
5531
Alan Stern4fe03872009-02-26 10:21:02 -05005532 /* Put interfaces back into the same altsettings as before.
5533 * Don't bother to send the Set-Interface request for interfaces
5534 * that were already in altsetting 0; besides being unnecessary,
5535 * many devices can't handle it. Instead just reset the host-side
5536 * endpoint state.
5537 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005539 struct usb_host_config *config = udev->actconfig;
5540 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 struct usb_interface_descriptor *desc;
5542
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05005544 if (desc->bAlternateSetting == 0) {
5545 usb_disable_interface(udev, intf, true);
5546 usb_enable_interface(udev, intf, true);
5547 ret = 0;
5548 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08005549 /* Let the bandwidth allocation function know that this
5550 * device has been reset, and it will have to use
5551 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005552 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08005553 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05005554 ret = usb_set_interface(udev, desc->bInterfaceNumber,
5555 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08005556 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05005557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 if (ret < 0) {
5559 dev_err(&udev->dev, "failed to restore interface %d "
5560 "altsetting %d (error=%d)\n",
5561 desc->bInterfaceNumber,
5562 desc->bAlternateSetting,
5563 ret);
5564 goto re_enumerate;
5565 }
Hans de Goede7a7b5622013-11-08 16:37:26 +01005566 /* Resetting also frees any allocated streams */
5567 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++)
5568 intf->cur_altsetting->endpoint[j].streams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 }
5570
5571done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07005572 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Sarah Sharpde68bab2013-09-30 17:26:28 +03005573 usb_set_usb2_hardware_lpm(udev, 1);
Alan Stern78d9a482008-06-23 16:00:40 -04005574 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07005575 usb_enable_ltm(udev);
Greg Kroah-Hartmane5bdfd52016-02-20 14:19:34 -08005576 usb_release_bos_descriptor(udev);
5577 udev->bos = bos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 return 0;
Matthias Beyer469271f2013-10-10 23:41:27 +02005579
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580re_enumerate:
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005581 usb_release_bos_descriptor(udev);
5582 udev->bos = bos;
Hans Yang464ad8c2015-12-01 16:54:59 +08005583re_enumerate_no_bos:
5584 /* LPM state doesn't matter when we're about to destroy the device. */
5585 hub_port_logical_disconnect(parent_hub, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587}
5588
5589/**
5590 * usb_reset_device - warn interface drivers and perform a USB port reset
5591 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5592 *
Alan Stern79efa092006-06-01 13:33:42 -04005593 * Warns all drivers bound to registered interfaces (using their pre_reset
5594 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08005595 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04005596 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005597 * Return: The same as for usb_reset_and_verify_device().
Alan Stern79efa092006-06-01 13:33:42 -04005598 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005599 * Note:
Alan Stern79efa092006-06-01 13:33:42 -04005600 * The caller must own the device lock. For example, it's safe to use
5601 * this from a driver probe() routine after downloading new firmware.
5602 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08005603 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04005604 *
5605 * If an interface is currently being probed or disconnected, we assume
5606 * its driver knows how to handle resets. For all other interfaces,
5607 * if the driver doesn't have pre_reset and post_reset methods then
5608 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04005609 */
Ming Lei742120c2008-06-18 22:00:29 +08005610int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04005611{
5612 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05005613 int i;
Ming Lei4d769de2013-02-22 16:34:22 -08005614 unsigned int noio_flag;
Dan Williams5c79a1e2014-05-20 18:09:26 -07005615 struct usb_port *port_dev;
Alan Stern79efa092006-06-01 13:33:42 -04005616 struct usb_host_config *config = udev->actconfig;
Dan Williams5c79a1e2014-05-20 18:09:26 -07005617 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
Alan Stern79efa092006-06-01 13:33:42 -04005618
5619 if (udev->state == USB_STATE_NOTATTACHED ||
5620 udev->state == USB_STATE_SUSPENDED) {
5621 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5622 udev->state);
5623 return -EINVAL;
5624 }
5625
Dan Williams5c79a1e2014-05-20 18:09:26 -07005626 if (!udev->parent) {
5627 /* this requires hcd-specific logic; see ohci_restart() */
5628 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
5629 return -EISDIR;
5630 }
5631
5632 port_dev = hub->ports[udev->portnum - 1];
5633
Ming Lei4d769de2013-02-22 16:34:22 -08005634 /*
5635 * Don't allocate memory with GFP_KERNEL in current
5636 * context to avoid possible deadlock if usb mass
5637 * storage interface or usbnet interface(iSCSI case)
5638 * is included in current configuration. The easist
5639 * approach is to do it for every device reset,
5640 * because the device 'memalloc_noio' flag may have
5641 * not been set before reseting the usb device.
5642 */
5643 noio_flag = memalloc_noio_save();
5644
Alan Stern645daaa2006-08-30 15:47:02 -04005645 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05005646 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04005647
Alan Stern79efa092006-06-01 13:33:42 -04005648 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005649 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005650 struct usb_interface *cintf = config->interface[i];
5651 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005652 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05005653
5654 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005655 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04005656 if (drv->pre_reset && drv->post_reset)
5657 unbind = (drv->pre_reset)(cintf);
5658 else if (cintf->condition ==
5659 USB_INTERFACE_BOUND)
5660 unbind = 1;
5661 if (unbind)
5662 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005663 }
5664 }
5665 }
5666
Dan Williams5c79a1e2014-05-20 18:09:26 -07005667 usb_lock_port(port_dev);
Ming Lei742120c2008-06-18 22:00:29 +08005668 ret = usb_reset_and_verify_device(udev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07005669 usb_unlock_port(port_dev);
Alan Stern79efa092006-06-01 13:33:42 -04005670
5671 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005672 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005673 struct usb_interface *cintf = config->interface[i];
5674 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005675 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05005676
Alan Stern78d9a482008-06-23 16:00:40 -04005677 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005678 drv = to_usb_driver(cintf->dev.driver);
5679 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04005680 rebind = (drv->post_reset)(cintf);
5681 else if (cintf->condition ==
5682 USB_INTERFACE_BOUND)
5683 rebind = 1;
Alan Stern6aec0442014-03-12 11:30:38 -04005684 if (rebind)
5685 cintf->needs_binding = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005686 }
Alan Stern79efa092006-06-01 13:33:42 -04005687 }
Alan Stern6aec0442014-03-12 11:30:38 -04005688 usb_unbind_and_rebind_marked_interfaces(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005689 }
5690
Alan Stern94fcda12006-11-20 11:38:46 -05005691 usb_autosuspend_device(udev);
Ming Lei4d769de2013-02-22 16:34:22 -08005692 memalloc_noio_restore(noio_flag);
Alan Stern79efa092006-06-01 13:33:42 -04005693 return ret;
5694}
Ming Lei742120c2008-06-18 22:00:29 +08005695EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005696
5697
5698/**
5699 * usb_queue_reset_device - Reset a USB device from an atomic context
5700 * @iface: USB interface belonging to the device to reset
5701 *
5702 * This function can be used to reset a USB device from an atomic
5703 * context, where usb_reset_device() won't work (as it blocks).
5704 *
5705 * Doing a reset via this method is functionally equivalent to calling
5706 * usb_reset_device(), except for the fact that it is delayed to a
5707 * workqueue. This means that any drivers bound to other interfaces
5708 * might be unbound, as well as users from usbfs in user space.
5709 *
5710 * Corner cases:
5711 *
5712 * - Scheduling two resets at the same time from two different drivers
5713 * attached to two different interfaces of the same device is
5714 * possible; depending on how the driver attached to each interface
5715 * handles ->pre_reset(), the second reset might happen or not.
5716 *
Alan Stern524134d2015-01-21 14:02:43 -05005717 * - If the reset is delayed so long that the interface is unbound from
5718 * its driver, the reset will be skipped.
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005719 *
Alan Stern524134d2015-01-21 14:02:43 -05005720 * - This function can be called during .probe(). It can also be called
5721 * during .disconnect(), but doing so is pointless because the reset
5722 * will not occur. If you really want to reset the device during
5723 * .disconnect(), call usb_reset_device() directly -- but watch out
5724 * for nested unbinding issues!
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005725 */
5726void usb_queue_reset_device(struct usb_interface *iface)
5727{
Alan Stern524134d2015-01-21 14:02:43 -05005728 if (schedule_work(&iface->reset_ws))
5729 usb_get_intf(iface);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005730}
5731EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c72012-09-05 13:44:32 +08005732
5733/**
5734 * usb_hub_find_child - Get the pointer of child device
5735 * attached to the port which is specified by @port1.
5736 * @hdev: USB device belonging to the usb hub
5737 * @port1: port num to indicate which port the child device
5738 * is attached to.
5739 *
5740 * USB drivers call this function to get hub's child device
5741 * pointer.
5742 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005743 * Return: %NULL if input param is invalid and
Lan Tianyuff823c72012-09-05 13:44:32 +08005744 * child's usb_device pointer if non-NULL.
5745 */
5746struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5747 int port1)
5748{
Lan Tianyuad493e52013-01-23 04:26:30 +08005749 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyuff823c72012-09-05 13:44:32 +08005750
5751 if (port1 < 1 || port1 > hdev->maxchild)
5752 return NULL;
5753 return hub->ports[port1 - 1]->child;
5754}
5755EXPORT_SYMBOL_GPL(usb_hub_find_child);
Lan Tianyud5575422012-09-05 13:44:33 +08005756
Lan Tianyud2123fd2013-01-21 22:18:00 +08005757void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
5758 struct usb_hub_descriptor *desc)
5759{
Dan Williamsd99f6b42014-05-20 18:08:17 -07005760 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyud2123fd2013-01-21 22:18:00 +08005761 enum usb_port_connect_type connect_type;
5762 int i;
5763
Dan Williamsd99f6b42014-05-20 18:08:17 -07005764 if (!hub)
5765 return;
5766
Lan Tianyud2123fd2013-01-21 22:18:00 +08005767 if (!hub_is_superspeed(hdev)) {
5768 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005769 struct usb_port *port_dev = hub->ports[i - 1];
Lan Tianyud2123fd2013-01-21 22:18:00 +08005770
Dan Williamsd99f6b42014-05-20 18:08:17 -07005771 connect_type = port_dev->connect_type;
Lan Tianyud2123fd2013-01-21 22:18:00 +08005772 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5773 u8 mask = 1 << (i%8);
5774
5775 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005776 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
Lan Tianyud2123fd2013-01-21 22:18:00 +08005777 desc->u.hs.DeviceRemovable[i/8] |= mask;
5778 }
5779 }
5780 }
5781 } else {
5782 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable);
5783
5784 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005785 struct usb_port *port_dev = hub->ports[i - 1];
Lan Tianyud2123fd2013-01-21 22:18:00 +08005786
Dan Williamsd99f6b42014-05-20 18:08:17 -07005787 connect_type = port_dev->connect_type;
Lan Tianyud2123fd2013-01-21 22:18:00 +08005788 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5789 u16 mask = 1 << i;
5790
5791 if (!(port_removable & mask)) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005792 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
Lan Tianyud2123fd2013-01-21 22:18:00 +08005793 port_removable |= mask;
5794 }
5795 }
5796 }
5797
5798 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
5799 }
5800}
5801
Lan Tianyud5575422012-09-05 13:44:33 +08005802#ifdef CONFIG_ACPI
5803/**
5804 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5805 * @hdev: USB device belonging to the usb hub
5806 * @port1: port num of the port
5807 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005808 * Return: Port's acpi handle if successful, %NULL if params are
5809 * invalid.
Lan Tianyud5575422012-09-05 13:44:33 +08005810 */
5811acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5812 int port1)
5813{
Lan Tianyuad493e52013-01-23 04:26:30 +08005814 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyud5575422012-09-05 13:44:33 +08005815
Mathias Nyman41341262013-06-18 17:28:48 +03005816 if (!hub)
5817 return NULL;
5818
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +01005819 return ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
Lan Tianyud5575422012-09-05 13:44:33 +08005820}
5821#endif