blob: 25379bd6b46475fbde896585569b0e0035c14274 [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
Hemant Kumar3aa7ecd2016-11-08 15:25:25 -080051static bool skip_extended_resume_delay = 1;
52module_param(skip_extended_resume_delay, bool, S_IRUGO | S_IWUSR);
53MODULE_PARM_DESC(skip_extended_resume_delay,
54 "removes extra delay added to finish bus resume");
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/* cycle leds on hubs that aren't blinking for attention */
Saurabh Sengara44007a42015-10-26 09:25:36 +053057static bool blinkenlights;
Chase Metzger166b8632015-08-08 01:03:34 -070058module_param(blinkenlights, bool, S_IRUGO);
59MODULE_PARM_DESC(blinkenlights, "true to cycle leds on hubs");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020062 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
63 * 10 seconds to send reply for the initial 64-byte descriptor request.
64 */
65/* define initial 64-byte descriptor request timeout in milliseconds */
66static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
67module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +080068MODULE_PARM_DESC(initial_descriptor_timeout,
69 "initial 64-byte descriptor request timeout in milliseconds "
70 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020071
72/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * As of 2.6.10 we introduce a new USB device initialization scheme which
74 * closely resembles the way Windows works. Hopefully it will be compatible
75 * with a wider range of devices than the old scheme. However some previously
76 * working devices may start giving rise to "device not accepting address"
77 * errors; if that happens the user can try the old scheme by adjusting the
78 * following module parameters.
79 *
80 * For maximum flexibility there are two boolean parameters to control the
81 * hub driver's behavior. On the first initialization attempt, if the
82 * "old_scheme_first" parameter is set then the old scheme will be used,
83 * otherwise the new scheme is used. If that fails and "use_both_schemes"
84 * is set, then the driver will make another attempt, using the other scheme.
85 */
Saurabh Sengara44007a42015-10-26 09:25:36 +053086static bool old_scheme_first;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
88MODULE_PARM_DESC(old_scheme_first,
89 "start with the old device initialization scheme");
90
Rusty Russell90ab5ee2012-01-13 09:32:20 +103091static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
93MODULE_PARM_DESC(use_both_schemes,
94 "try the other device initialization scheme if the "
95 "first one fails");
96
Alan Stern32fe0192007-10-10 16:27:07 -040097/* Mutual exclusion for EHCI CF initialization. This interferes with
98 * port reset on some companion controllers.
99 */
100DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
101EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
102
Lan Tianyuad493e52013-01-23 04:26:30 +0800103#define HUB_DEBOUNCE_TIMEOUT 2000
Alan Stern948fea32008-04-28 11:07:07 -0400104#define HUB_DEBOUNCE_STEP 25
105#define HUB_DEBOUNCE_STABLE 100
106
Petr Mladek32a695892014-09-19 17:32:21 +0200107static void hub_release(struct kref *kref);
Ming Lei742120c2008-06-18 22:00:29 +0800108static int usb_reset_and_verify_device(struct usb_device *udev);
Geert Uytterhoeven8ecf70f2016-12-14 15:37:30 +0100109static int hub_port_disable(struct usb_hub *hub, int port1, int set_state);
Jan-Marek Glogowskic4b857a2019-02-01 13:52:31 +0100110static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
111 u16 portstatus);
Ming Lei742120c2008-06-18 22:00:29 +0800112
Sarah Sharp131dec32010-12-06 21:00:19 -0800113static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Oliver Neukumd64aab02016-04-20 16:28:09 +0200115 if (hub_is_superspeedplus(hub->hdev))
116 return "10.0 Gb/s";
Sarah Sharp131dec32010-12-06 21:00:19 -0800117 if (hub_is_superspeed(hub->hdev))
118 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500119 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Matthias Beyer469271f2013-10-10 23:41:27 +0200120 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500121 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return "1.5 Mb/s";
123 else
124 return "12 Mb/s";
125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/* Note that hdev or one of its children must be locked! */
Lan Tianyuad493e52013-01-23 04:26:30 +0800128struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Lan Tianyuff823c72012-09-05 13:44:32 +0800130 if (!hdev || !hdev->actconfig || !hdev->maxchild)
Alan Stern251180842009-07-22 14:41:18 -0400131 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return usb_get_intfdata(hdev->actconfig->interface[0]);
133}
134
Lu Baolu2d2a3162015-06-16 09:08:26 +0800135int usb_device_supports_lpm(struct usb_device *udev)
Sarah Sharpd9b20992012-02-20 08:31:26 -0800136{
Alan Sternad87e032015-12-10 15:27:21 -0500137 /* Some devices have trouble with LPM */
138 if (udev->quirks & USB_QUIRK_NO_LPM)
139 return 0;
140
Sarah Sharpd9b20992012-02-20 08:31:26 -0800141 /* USB 2.1 (and greater) devices indicate LPM support through
142 * their USB 2.0 Extended Capabilities BOS descriptor.
143 */
Rupesh Tatiyaa8425292015-04-14 16:36:55 +0530144 if (udev->speed == USB_SPEED_HIGH || udev->speed == USB_SPEED_FULL) {
Sarah Sharpd9b20992012-02-20 08:31:26 -0800145 if (udev->bos->ext_cap &&
146 (USB_LPM_SUPPORT &
147 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
148 return 1;
149 return 0;
150 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800151
Sarah Sharp25cd2882014-01-17 14:15:44 -0800152 /*
153 * According to the USB 3.0 spec, all USB 3.0 devices must support LPM.
154 * However, there are some that don't, and they set the U1/U2 exit
155 * latencies to zero.
Sarah Sharp51e0a012012-02-20 12:02:19 -0800156 */
157 if (!udev->bos->ss_cap) {
Sarah Sharp25cd2882014-01-17 14:15:44 -0800158 dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n");
Sarah Sharp51e0a012012-02-20 12:02:19 -0800159 return 0;
160 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800161
Sarah Sharp25cd2882014-01-17 14:15:44 -0800162 if (udev->bos->ss_cap->bU1devExitLat == 0 &&
163 udev->bos->ss_cap->bU2DevExitLat == 0) {
164 if (udev->parent)
165 dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n");
166 else
167 dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n");
168 return 0;
169 }
170
171 if (!udev->parent || udev->parent->lpm_capable)
172 return 1;
Sarah Sharpd9b20992012-02-20 08:31:26 -0800173 return 0;
174}
175
Sarah Sharp51e0a012012-02-20 12:02:19 -0800176/*
177 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
178 * either U1 or U2.
179 */
180static void usb_set_lpm_mel(struct usb_device *udev,
181 struct usb3_lpm_parameters *udev_lpm_params,
182 unsigned int udev_exit_latency,
183 struct usb_hub *hub,
184 struct usb3_lpm_parameters *hub_lpm_params,
185 unsigned int hub_exit_latency)
186{
187 unsigned int total_mel;
188 unsigned int device_mel;
189 unsigned int hub_mel;
190
191 /*
192 * Calculate the time it takes to transition all links from the roothub
193 * to the parent hub into U0. The parent hub must then decode the
194 * packet (hub header decode latency) to figure out which port it was
195 * bound for.
196 *
197 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
198 * means 0.1us). Multiply that by 100 to get nanoseconds.
199 */
200 total_mel = hub_lpm_params->mel +
201 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
202
203 /*
204 * How long will it take to transition the downstream hub's port into
205 * U0? The greater of either the hub exit latency or the device exit
206 * latency.
207 *
208 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
209 * Multiply that by 1000 to get nanoseconds.
210 */
211 device_mel = udev_exit_latency * 1000;
212 hub_mel = hub_exit_latency * 1000;
213 if (device_mel > hub_mel)
214 total_mel += device_mel;
215 else
216 total_mel += hub_mel;
217
218 udev_lpm_params->mel = total_mel;
219}
220
221/*
222 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
223 * a transition from either U1 or U2.
224 */
225static void usb_set_lpm_pel(struct usb_device *udev,
226 struct usb3_lpm_parameters *udev_lpm_params,
227 unsigned int udev_exit_latency,
228 struct usb_hub *hub,
229 struct usb3_lpm_parameters *hub_lpm_params,
230 unsigned int hub_exit_latency,
231 unsigned int port_to_port_exit_latency)
232{
233 unsigned int first_link_pel;
234 unsigned int hub_pel;
235
236 /*
237 * First, the device sends an LFPS to transition the link between the
238 * device and the parent hub into U0. The exit latency is the bigger of
239 * the device exit latency or the hub exit latency.
240 */
241 if (udev_exit_latency > hub_exit_latency)
242 first_link_pel = udev_exit_latency * 1000;
243 else
244 first_link_pel = hub_exit_latency * 1000;
245
246 /*
247 * When the hub starts to receive the LFPS, there is a slight delay for
248 * it to figure out that one of the ports is sending an LFPS. Then it
249 * will forward the LFPS to its upstream link. The exit latency is the
250 * delay, plus the PEL that we calculated for this hub.
251 */
252 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
253
254 /*
255 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
256 * is the greater of the two exit latencies.
257 */
258 if (first_link_pel > hub_pel)
259 udev_lpm_params->pel = first_link_pel;
260 else
261 udev_lpm_params->pel = hub_pel;
262}
263
264/*
265 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
266 * when a device initiates a transition to U0, until when it will receive the
267 * first packet from the host controller.
268 *
269 * Section C.1.5.1 describes the four components to this:
270 * - t1: device PEL
271 * - t2: time for the ERDY to make it from the device to the host.
272 * - t3: a host-specific delay to process the ERDY.
273 * - t4: time for the packet to make it from the host to the device.
274 *
275 * t3 is specific to both the xHCI host and the platform the host is integrated
276 * into. The Intel HW folks have said it's negligible, FIXME if a different
277 * vendor says otherwise.
278 */
279static void usb_set_lpm_sel(struct usb_device *udev,
280 struct usb3_lpm_parameters *udev_lpm_params)
281{
282 struct usb_device *parent;
283 unsigned int num_hubs;
284 unsigned int total_sel;
285
286 /* t1 = device PEL */
287 total_sel = udev_lpm_params->pel;
288 /* How many external hubs are in between the device & the root port. */
289 for (parent = udev->parent, num_hubs = 0; parent->parent;
290 parent = parent->parent)
291 num_hubs++;
292 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
293 if (num_hubs > 0)
294 total_sel += 2100 + 250 * (num_hubs - 1);
295
296 /* t4 = 250ns * num_hubs */
297 total_sel += 250 * num_hubs;
298
299 udev_lpm_params->sel = total_sel;
300}
301
302static void usb_set_lpm_parameters(struct usb_device *udev)
303{
304 struct usb_hub *hub;
305 unsigned int port_to_port_delay;
306 unsigned int udev_u1_del;
307 unsigned int udev_u2_del;
308 unsigned int hub_u1_del;
309 unsigned int hub_u2_del;
310
Mathias Nyman8a1b2722015-12-10 09:59:25 +0200311 if (!udev->lpm_capable || udev->speed < USB_SPEED_SUPER)
Sarah Sharp51e0a012012-02-20 12:02:19 -0800312 return;
313
Lan Tianyuad493e52013-01-23 04:26:30 +0800314 hub = usb_hub_to_struct_hub(udev->parent);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800315 /* It doesn't take time to transition the roothub into U0, since it
316 * doesn't have an upstream link.
317 */
318 if (!hub)
319 return;
320
321 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
Xenia Ragiadakou4d967992013-08-31 18:09:13 +0300322 udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800323 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
Xenia Ragiadakou4d967992013-08-31 18:09:13 +0300324 hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800325
326 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
327 hub, &udev->parent->u1_params, hub_u1_del);
328
329 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
330 hub, &udev->parent->u2_params, hub_u2_del);
331
332 /*
333 * Appendix C, section C.2.2.2, says that there is a slight delay from
334 * when the parent hub notices the downstream port is trying to
335 * transition to U0 to when the hub initiates a U0 transition on its
336 * upstream port. The section says the delays are tPort2PortU1EL and
337 * tPort2PortU2EL, but it doesn't define what they are.
338 *
339 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
340 * about the same delays. Use the maximum delay calculations from those
341 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
342 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
343 * assume the device exit latencies they are talking about are the hub
344 * exit latencies.
345 *
346 * What do we do if the U2 exit latency is less than the U1 exit
347 * latency? It's possible, although not likely...
348 */
349 port_to_port_delay = 1;
350
351 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
352 hub, &udev->parent->u1_params, hub_u1_del,
353 port_to_port_delay);
354
355 if (hub_u2_del > hub_u1_del)
356 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
357 else
358 port_to_port_delay = 1 + hub_u1_del;
359
360 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
361 hub, &udev->parent->u2_params, hub_u2_del,
362 port_to_port_delay);
363
364 /* Now that we've got PEL, calculate SEL. */
365 usb_set_lpm_sel(udev, &udev->u1_params);
366 usb_set_lpm_sel(udev, &udev->u2_params);
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/* USB 2.0 spec Section 11.24.4.5 */
Johan Hovoldc67e87a2017-05-10 18:18:28 +0200370static int get_hub_descriptor(struct usb_device *hdev,
371 struct usb_hub_descriptor *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
John Youndbe79bb2001-09-17 00:00:00 -0700373 int i, ret, size;
374 unsigned dtype;
375
376 if (hub_is_superspeed(hdev)) {
377 dtype = USB_DT_SS_HUB;
378 size = USB_DT_SS_HUB_SIZE;
379 } else {
380 dtype = USB_DT_HUB;
381 size = sizeof(struct usb_hub_descriptor);
382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 for (i = 0; i < 3; i++) {
385 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
386 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
Johan Hovoldc67e87a2017-05-10 18:18:28 +0200387 dtype << 8, 0, desc, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 USB_CTRL_GET_TIMEOUT);
Johan Hovold3e4a4e62017-05-10 18:18:27 +0200389 if (hub_is_superspeed(hdev)) {
390 if (ret == size)
391 return ret;
Johan Hovoldc67e87a2017-05-10 18:18:28 +0200392 } else if (ret >= USB_DT_HUB_NONVAR_SIZE + 2) {
393 /* Make sure we have the DeviceRemovable field. */
394 size = USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts / 8 + 1;
395 if (ret < size)
396 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return ret;
Johan Hovold3e4a4e62017-05-10 18:18:27 +0200398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400 return -EINVAL;
401}
402
403/*
404 * USB 2.0 spec Section 11.24.2.1
405 */
406static int clear_hub_feature(struct usb_device *hdev, int feature)
407{
408 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
409 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
410}
411
412/*
413 * USB 2.0 spec Section 11.24.2.2
414 */
Lan Tianyuad493e52013-01-23 04:26:30 +0800415int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
418 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
419 NULL, 0, 1000);
420}
421
422/*
423 * USB 2.0 spec Section 11.24.2.13
424 */
425static int set_port_feature(struct usb_device *hdev, int port1, int feature)
426{
427 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
428 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
429 NULL, 0, 1000);
430}
431
Dan Williamsd99f6b42014-05-20 18:08:17 -0700432static char *to_led_name(int selector)
433{
434 switch (selector) {
435 case HUB_LED_AMBER:
436 return "amber";
437 case HUB_LED_GREEN:
438 return "green";
439 case HUB_LED_OFF:
440 return "off";
441 case HUB_LED_AUTO:
442 return "auto";
443 default:
444 return "??";
445 }
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/*
449 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
450 * for info about using port indicators
451 */
Dan Williamsd99f6b42014-05-20 18:08:17 -0700452static void set_port_led(struct usb_hub *hub, int port1, int selector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Dan Williamsd99f6b42014-05-20 18:08:17 -0700454 struct usb_port *port_dev = hub->ports[port1 - 1];
455 int status;
456
457 status = set_port_feature(hub->hdev, (selector << 8) | port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 USB_PORT_FEAT_INDICATOR);
Dan Williamsd99f6b42014-05-20 18:08:17 -0700459 dev_dbg(&port_dev->dev, "indicator %s status %d\n",
460 to_led_name(selector), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463#define LED_CYCLE_PERIOD ((2*HZ)/3)
464
Chase Metzger166b8632015-08-08 01:03:34 -0700465static void led_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
David Howellsc4028952006-11-22 14:57:56 +0000467 struct usb_hub *hub =
468 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct usb_device *hdev = hub->hdev;
470 unsigned i;
471 unsigned changed = 0;
472 int cursor = -1;
473
474 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
475 return;
476
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +0200477 for (i = 0; i < hdev->maxchild; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 unsigned selector, mode;
479
480 /* 30%-50% duty cycle */
481
482 switch (hub->indicator[i]) {
483 /* cycle marker */
484 case INDICATOR_CYCLE:
485 cursor = i;
486 selector = HUB_LED_AUTO;
487 mode = INDICATOR_AUTO;
488 break;
489 /* blinking green = sw attention */
490 case INDICATOR_GREEN_BLINK:
491 selector = HUB_LED_GREEN;
492 mode = INDICATOR_GREEN_BLINK_OFF;
493 break;
494 case INDICATOR_GREEN_BLINK_OFF:
495 selector = HUB_LED_OFF;
496 mode = INDICATOR_GREEN_BLINK;
497 break;
498 /* blinking amber = hw attention */
499 case INDICATOR_AMBER_BLINK:
500 selector = HUB_LED_AMBER;
501 mode = INDICATOR_AMBER_BLINK_OFF;
502 break;
503 case INDICATOR_AMBER_BLINK_OFF:
504 selector = HUB_LED_OFF;
505 mode = INDICATOR_AMBER_BLINK;
506 break;
507 /* blink green/amber = reserved */
508 case INDICATOR_ALT_BLINK:
509 selector = HUB_LED_GREEN;
510 mode = INDICATOR_ALT_BLINK_OFF;
511 break;
512 case INDICATOR_ALT_BLINK_OFF:
513 selector = HUB_LED_AMBER;
514 mode = INDICATOR_ALT_BLINK;
515 break;
516 default:
517 continue;
518 }
519 if (selector != HUB_LED_AUTO)
520 changed = 1;
521 set_port_led(hub, i + 1, selector);
522 hub->indicator[i] = mode;
523 }
524 if (!changed && blinkenlights) {
525 cursor++;
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +0200526 cursor %= hdev->maxchild;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
528 hub->indicator[cursor] = INDICATOR_CYCLE;
529 changed++;
530 }
531 if (changed)
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -0800532 queue_delayed_work(system_power_efficient_wq,
533 &hub->leds, LED_CYCLE_PERIOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/* use a short timeout for hub/port status fetches */
537#define USB_STS_TIMEOUT 1000
538#define USB_STS_RETRIES 5
539
540/*
541 * USB 2.0 spec Section 11.24.2.6
542 */
543static int get_hub_status(struct usb_device *hdev,
544 struct usb_hub_status *data)
545{
546 int i, status = -ETIMEDOUT;
547
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200548 for (i = 0; i < USB_STS_RETRIES &&
549 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
551 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
552 data, sizeof(*data), USB_STS_TIMEOUT);
553 }
554 return status;
555}
556
557/*
558 * USB 2.0 spec Section 11.24.2.7
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200559 * USB 3.1 takes into use the wValue and wLength fields, spec Section 10.16.2.6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
561static int get_port_status(struct usb_device *hdev, int port1,
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200562 void *data, u16 value, u16 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 int i, status = -ETIMEDOUT;
565
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200566 for (i = 0; i < USB_STS_RETRIES &&
567 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200569 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, value,
570 port1, data, length, USB_STS_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 return status;
573}
574
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200575static int hub_ext_port_status(struct usb_hub *hub, int port1, int type,
576 u16 *status, u16 *change, u32 *ext_status)
Alan Stern3eb14912008-03-03 15:15:43 -0500577{
578 int ret;
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200579 int len = 4;
580
581 if (type != HUB_PORT_STATUS)
582 len = 8;
Alan Stern3eb14912008-03-03 15:15:43 -0500583
584 mutex_lock(&hub->status_mutex);
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200585 ret = get_port_status(hub->hdev, port1, &hub->status->port, type, len);
586 if (ret < len) {
Alan Sterne9e88fb2013-03-27 16:14:01 -0400587 if (ret != -ENODEV)
588 dev_err(hub->intfdev,
589 "%s failed (err = %d)\n", __func__, ret);
Alan Stern3eb14912008-03-03 15:15:43 -0500590 if (ret >= 0)
591 ret = -EIO;
592 } else {
593 *status = le16_to_cpu(hub->status->port.wPortStatus);
594 *change = le16_to_cpu(hub->status->port.wPortChange);
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200595 if (type != HUB_PORT_STATUS && ext_status)
596 *ext_status = le32_to_cpu(
597 hub->status->port.dwExtPortStatus);
Alan Stern3eb14912008-03-03 15:15:43 -0500598 ret = 0;
599 }
600 mutex_unlock(&hub->status_mutex);
601 return ret;
602}
603
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200604static int hub_port_status(struct usb_hub *hub, int port1,
605 u16 *status, u16 *change)
606{
607 return hub_ext_port_status(hub, port1, HUB_PORT_STATUS,
608 status, change, NULL);
609}
610
Petr Mladek32a695892014-09-19 17:32:21 +0200611static void kick_hub_wq(struct usb_hub *hub)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Petr Mladek32a695892014-09-19 17:32:21 +0200613 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Petr Mladek32a695892014-09-19 17:32:21 +0200615 if (hub->disconnected || work_pending(&hub->events))
616 return;
Alan Stern8e4ceb32009-12-07 13:01:37 -0500617
Petr Mladek32a695892014-09-19 17:32:21 +0200618 /*
619 * Suppress autosuspend until the event is proceed.
620 *
621 * Be careful and make sure that the symmetric operation is
622 * always called. We are here only when there is no pending
623 * work for this hub. Therefore put the interface either when
624 * the new work is called or when it is canceled.
625 */
626 intf = to_usb_interface(hub->intfdev);
627 usb_autopm_get_interface_no_resume(intf);
628 kref_get(&hub->kref);
629
630 if (queue_work(hub_wq, &hub->events))
631 return;
632
633 /* the work has already been scheduled */
634 usb_autopm_put_interface_async(intf);
635 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Petr Mladek59d48b32014-09-19 17:32:22 +0200638void usb_kick_hub_wq(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Lan Tianyuad493e52013-01-23 04:26:30 +0800640 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Alan Stern251180842009-07-22 14:41:18 -0400641
642 if (hub)
Petr Mladek32a695892014-09-19 17:32:21 +0200643 kick_hub_wq(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Hemant Kumarc909c082016-10-03 11:32:10 -0700646void usb_flush_hub_wq(void)
647{
648 flush_workqueue(hub_wq);
649}
650EXPORT_SYMBOL(usb_flush_hub_wq);
651
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800652/*
653 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
654 * Notification, which indicates it had initiated remote wakeup.
655 *
656 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
657 * device initiates resume, so the USB core will not receive notice of the
658 * resume through the normal hub interrupt URB.
659 */
660void usb_wakeup_notification(struct usb_device *hdev,
661 unsigned int portnum)
662{
663 struct usb_hub *hub;
Ravi Chandra Sadineni00bc3482018-04-20 11:08:21 -0700664 struct usb_port *port_dev;
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800665
666 if (!hdev)
667 return;
668
Lan Tianyuad493e52013-01-23 04:26:30 +0800669 hub = usb_hub_to_struct_hub(hdev);
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800670 if (hub) {
Ravi Chandra Sadineni00bc3482018-04-20 11:08:21 -0700671 port_dev = hub->ports[portnum - 1];
672 if (port_dev && port_dev->child)
673 pm_wakeup_event(&port_dev->child->dev, 0);
674
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800675 set_bit(portnum, hub->wakeup_bits);
Petr Mladek32a695892014-09-19 17:32:21 +0200676 kick_hub_wq(hub);
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800677 }
678}
679EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100682static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200684 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400685 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100686 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 unsigned long bits;
688
Alan Sterne0152682007-08-24 15:42:52 -0400689 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 case -ENOENT: /* synchronous unlink */
691 case -ECONNRESET: /* async unlink */
692 case -ESHUTDOWN: /* hardware going away */
693 return;
694
695 default: /* presumably an error */
696 /* Cause a hub reset after 10 consecutive errors */
Chase Metzger166b8632015-08-08 01:03:34 -0700697 dev_dbg(hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if ((++hub->nerrors < 10) || hub->error)
699 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400700 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200702
Petr Mladek37ebb542014-09-19 17:32:23 +0200703 /* let hub_wq handle things */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 case 0: /* we got data: port status changed */
705 bits = 0;
706 for (i = 0; i < urb->actual_length; ++i)
707 bits |= ((unsigned long) ((*hub->buffer)[i]))
708 << (i*8);
709 hub->event_bits[0] = bits;
710 break;
711 }
712
713 hub->nerrors = 0;
714
Petr Mladek37ebb542014-09-19 17:32:23 +0200715 /* Something happened, let hub_wq figure it out */
Petr Mladek32a695892014-09-19 17:32:21 +0200716 kick_hub_wq(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718resubmit:
719 if (hub->quiescing)
720 return;
721
Kris Borer088a3da2015-08-11 11:12:45 -0400722 status = usb_submit_urb(hub->urb, GFP_ATOMIC);
723 if (status != 0 && status != -ENODEV && status != -EPERM)
Chase Metzger166b8632015-08-08 01:03:34 -0700724 dev_err(hub->intfdev, "resubmit --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727/* USB 2.0 spec Section 11.24.2.3 */
728static inline int
Chase Metzger166b8632015-08-08 01:03:34 -0700729hub_clear_tt_buffer(struct usb_device *hdev, u16 devinfo, u16 tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
William Gulland2c7b8712013-06-27 16:10:20 -0700731 /* Need to clear both directions for control ep */
732 if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) ==
733 USB_ENDPOINT_XFER_CONTROL) {
734 int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
735 HUB_CLEAR_TT_BUFFER, USB_RT_PORT,
736 devinfo ^ 0x8000, tt, NULL, 0, 1000);
737 if (status)
738 return status;
739 }
Alan Sternc2f65952009-11-18 11:37:15 -0500740 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
742 tt, NULL, 0, 1000);
743}
744
745/*
Petr Mladek37ebb542014-09-19 17:32:23 +0200746 * enumeration blocks hub_wq for a long time. we use keventd instead, since
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 * long blocking there is the exception, not the rule. accordingly, HCDs
748 * talking to TTs must queue control transfers (not just bulk and iso), so
749 * both can talk to the same hub concurrently.
750 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400751static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
David Howellsc4028952006-11-22 14:57:56 +0000753 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400754 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 unsigned long flags;
756
Chase Metzger166b8632015-08-08 01:03:34 -0700757 spin_lock_irqsave(&hub->tt.lock, flags);
Octavian Purdila3b6054d2012-10-01 22:21:12 +0300758 while (!list_empty(&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400759 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 struct usb_tt_clear *clear;
761 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400762 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 int status;
764
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400765 next = hub->tt.clear_list.next;
Chase Metzger166b8632015-08-08 01:03:34 -0700766 clear = list_entry(next, struct usb_tt_clear, clear_list);
767 list_del(&clear->clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 /* drop lock so HCD can concurrently report other TT errors */
Chase Metzger166b8632015-08-08 01:03:34 -0700770 spin_unlock_irqrestore(&hub->tt.lock, flags);
771 status = hub_clear_tt_buffer(hdev, clear->devinfo, clear->tt);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400772 if (status && status != -ENODEV)
Chase Metzger166b8632015-08-08 01:03:34 -0700773 dev_err(&hdev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 "clear tt %d (%04x) error %d\n",
775 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400776
777 /* Tell the HCD, even if the operation failed */
778 drv = clear->hcd->driver;
779 if (drv->clear_tt_buffer_complete)
780 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
781
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700782 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400783 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
Chase Metzger166b8632015-08-08 01:03:34 -0700785 spin_unlock_irqrestore(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
788/**
Lan Tianyu971fcd42013-01-23 04:26:29 +0800789 * usb_hub_set_port_power - control hub port's power state
Mathias Nyman41341262013-06-18 17:28:48 +0300790 * @hdev: USB device belonging to the usb hub
791 * @hub: target hub
Lan Tianyu971fcd42013-01-23 04:26:29 +0800792 * @port1: port index
793 * @set: expected status
794 *
795 * call this function to control port's power via setting or
796 * clearing the port's PORT_POWER feature.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200797 *
798 * Return: 0 if successful. A negative error code otherwise.
Lan Tianyu971fcd42013-01-23 04:26:29 +0800799 */
Mathias Nyman41341262013-06-18 17:28:48 +0300800int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub,
801 int port1, bool set)
Lan Tianyu971fcd42013-01-23 04:26:29 +0800802{
803 int ret;
804
805 if (set)
806 ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
807 else
Lan Tianyuad493e52013-01-23 04:26:30 +0800808 ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
809
Dan Williamsd5c38342014-05-20 18:08:52 -0700810 if (ret)
811 return ret;
812
813 if (set)
814 set_bit(port1, hub->power_bits);
815 else
816 clear_bit(port1, hub->power_bits);
817 return 0;
Lan Tianyu971fcd42013-01-23 04:26:29 +0800818}
819
820/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400821 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
822 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 *
824 * High speed HCDs use this to tell the hub driver that some split control or
825 * bulk transaction failed in a way that requires clearing internal state of
826 * a transaction translator. This is normally detected (and reported) from
827 * interrupt context.
828 *
829 * It may not be possible for that hub to handle additional full (or low)
830 * speed transactions until that state is fully cleared out.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200831 *
832 * Return: 0 if successful. A negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400834int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400836 struct usb_device *udev = urb->dev;
837 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 struct usb_tt *tt = udev->tt;
839 unsigned long flags;
840 struct usb_tt_clear *clear;
841
842 /* we've got to cope with an arbitrary number of pending TT clears,
843 * since each TT has "at least two" buffers that can need it (and
844 * there can be many TTs per hub). even if they're uncommon.
845 */
Greg Kroah-Hartmand544d272015-04-30 11:32:53 +0200846 clear = kmalloc(sizeof *clear, GFP_ATOMIC);
847 if (clear == NULL) {
Chase Metzger166b8632015-08-08 01:03:34 -0700848 dev_err(&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400850 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
853 /* info that CLEAR_TT_BUFFER needs */
854 clear->tt = tt->multi ? udev->ttport : 1;
855 clear->devinfo = usb_pipeendpoint (pipe);
856 clear->devinfo |= udev->devnum << 4;
Chase Metzger166b8632015-08-08 01:03:34 -0700857 clear->devinfo |= usb_pipecontrol(pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 ? (USB_ENDPOINT_XFER_CONTROL << 11)
859 : (USB_ENDPOINT_XFER_BULK << 11);
Chase Metzger166b8632015-08-08 01:03:34 -0700860 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400862
863 /* info for completion callback */
864 clear->hcd = bus_to_hcd(udev->bus);
865 clear->ep = urb->ep;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /* tell keventd to clear state for this TT */
Chase Metzger166b8632015-08-08 01:03:34 -0700868 spin_lock_irqsave(&tt->lock, flags);
869 list_add_tail(&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400870 schedule_work(&tt->clear_work);
Chase Metzger166b8632015-08-08 01:03:34 -0700871 spin_unlock_irqrestore(&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400874EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Dan Williams7ad3c472014-05-20 18:08:57 -0700876static void hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 int port1;
879
Alan Stern4489a572006-04-27 15:54:22 -0400880 /* Enable power on each port. Some hubs have reserved values
881 * of LPSM (> 2) in their descriptors, even though they are
882 * USB 2.0 hubs. Some hubs do not implement port-power switching
883 * but only emulate it. In all cases, the ports won't work
884 * unless we send these messages to the hub.
885 */
Dan Williams9262c192014-05-20 18:08:12 -0700886 if (hub_is_port_power_switchable(hub))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400888 else
889 dev_dbg(hub->intfdev, "trying to enable port power on "
890 "non-switchable hub\n");
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +0200891 for (port1 = 1; port1 <= hub->hdev->maxchild; port1++)
Dan Williamsd5c38342014-05-20 18:08:52 -0700892 if (test_bit(port1, hub->power_bits))
Lan Tianyuad493e52013-01-23 04:26:30 +0800893 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
894 else
895 usb_clear_port_feature(hub->hdev, port1,
896 USB_PORT_FEAT_POWER);
Alan Stern8520f382008-09-22 14:44:26 -0400897 if (do_delay)
Dan Williams7ad3c472014-05-20 18:08:57 -0700898 msleep(hub_power_on_good_delay(hub));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901static int hub_hub_status(struct usb_hub *hub,
902 u16 *status, u16 *change)
903{
904 int ret;
905
Alan Sterndb90e7a2007-02-05 09:56:15 -0500906 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 ret = get_hub_status(hub->hdev, &hub->status->hub);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400908 if (ret < 0) {
909 if (ret != -ENODEV)
910 dev_err(hub->intfdev,
911 "%s failed (err = %d)\n", __func__, ret);
912 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 *status = le16_to_cpu(hub->status->hub.wHubStatus);
Matthias Beyer469271f2013-10-10 23:41:27 +0200914 *change = le16_to_cpu(hub->status->hub.wHubChange);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 ret = 0;
916 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500917 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return ret;
919}
920
Sarah Sharp41e7e052012-11-14 16:42:32 -0800921static int hub_set_port_link_state(struct usb_hub *hub, int port1,
922 unsigned int link_status)
923{
924 return set_port_feature(hub->hdev,
925 port1 | (link_status << 3),
926 USB_PORT_FEAT_LINK_STATE);
927}
928
929/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800930 * Disable a port and mark a logical connect-change event, so that some
Petr Mladek37ebb542014-09-19 17:32:23 +0200931 * time later hub_wq will disconnect() any existing usb_device on the port
Alan Stern0458d5b2007-05-04 11:52:20 -0400932 * and will re-enumerate if there actually is a device attached.
933 */
934static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
935{
Dan Williamsd99f6b42014-05-20 18:08:17 -0700936 dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n");
Alan Stern0458d5b2007-05-04 11:52:20 -0400937 hub_port_disable(hub, port1, 1);
938
939 /* FIXME let caller ask to power down the port:
940 * - some devices won't enumerate without a VBUS power cycle
941 * - SRP saves power that way
942 * - ... new call, TBD ...
943 * That's easy if this hub can switch power per-port, and
Petr Mladek37ebb542014-09-19 17:32:23 +0200944 * hub_wq reactivates the port later (timer, SRP, etc).
Alan Stern0458d5b2007-05-04 11:52:20 -0400945 * Powerdown must be optional, because of reset/DFU.
946 */
947
948 set_bit(port1, hub->change_bits);
Petr Mladek32a695892014-09-19 17:32:21 +0200949 kick_hub_wq(hub);
Alan Stern0458d5b2007-05-04 11:52:20 -0400950}
951
Alan Stern253e0572009-10-27 15:20:13 -0400952/**
953 * usb_remove_device - disable a device's port on its parent hub
954 * @udev: device to be disabled and removed
955 * Context: @udev locked, must be able to sleep.
956 *
Petr Mladek37ebb542014-09-19 17:32:23 +0200957 * After @udev's port has been disabled, hub_wq is notified and it will
Alan Stern253e0572009-10-27 15:20:13 -0400958 * see that the device has been disconnected. When the device is
959 * physically unplugged and something is plugged in, the events will
960 * be received and processed normally.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200961 *
962 * Return: 0 if successful. A negative error code otherwise.
Alan Stern253e0572009-10-27 15:20:13 -0400963 */
964int usb_remove_device(struct usb_device *udev)
965{
966 struct usb_hub *hub;
967 struct usb_interface *intf;
Eugeniu Roscaefccc9c2020-02-26 18:50:35 +0100968 int ret;
Alan Stern253e0572009-10-27 15:20:13 -0400969
970 if (!udev->parent) /* Can't remove a root hub */
971 return -EINVAL;
Lan Tianyuad493e52013-01-23 04:26:30 +0800972 hub = usb_hub_to_struct_hub(udev->parent);
Alan Stern253e0572009-10-27 15:20:13 -0400973 intf = to_usb_interface(hub->intfdev);
974
Eugeniu Roscaefccc9c2020-02-26 18:50:35 +0100975 ret = usb_autopm_get_interface(intf);
976 if (ret < 0)
977 return ret;
978
Alan Stern253e0572009-10-27 15:20:13 -0400979 set_bit(udev->portnum, hub->removed_bits);
980 hub_port_logical_disconnect(hub, udev->portnum);
981 usb_autopm_put_interface(intf);
982 return 0;
983}
984
Alan Stern6ee0b272008-04-28 11:06:42 -0400985enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -0500986 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -0400987 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -0400988};
Alan Stern5e6effa2008-03-03 15:15:51 -0500989
Alan Stern8520f382008-09-22 14:44:26 -0400990static void hub_init_func2(struct work_struct *ws);
991static void hub_init_func3(struct work_struct *ws);
992
Alan Sternf2835212008-04-28 11:07:17 -0400993static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500994{
995 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -0800996 struct usb_hcd *hcd;
997 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -0500998 int port1;
Alan Sternf2835212008-04-28 11:07:17 -0400999 int status;
Alan Stern948fea32008-04-28 11:07:07 -04001000 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -04001001 unsigned delay;
1002
1003 /* Continue a partial initialization */
Alan Sterne50293e2015-12-16 13:32:38 -05001004 if (type == HUB_INIT2 || type == HUB_INIT3) {
Alan Stern07d316a2016-08-05 11:51:30 -04001005 device_lock(&hdev->dev);
Alan Sterne50293e2015-12-16 13:32:38 -05001006
1007 /* Was the hub disconnected while we were waiting? */
Alan Sternca5cbc82016-08-05 11:49:45 -04001008 if (hub->disconnected)
1009 goto disconnected;
Alan Sterne50293e2015-12-16 13:32:38 -05001010 if (type == HUB_INIT2)
1011 goto init2;
Alan Stern8520f382008-09-22 14:44:26 -04001012 goto init3;
Alan Sterne50293e2015-12-16 13:32:38 -05001013 }
1014 kref_get(&hub->kref);
Alan Stern5e6effa2008-03-03 15:15:51 -05001015
Elric Fua45aa3b2012-02-18 13:32:27 +08001016 /* The superspeed hub except for root hub has to use Hub Depth
1017 * value as an offset into the route string to locate the bits
1018 * it uses to determine the downstream port number. So hub driver
1019 * should send a set hub depth request to superspeed hub after
1020 * the superspeed hub is set configuration in initialization or
1021 * reset procedure.
1022 *
1023 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -04001024 * For any other type of activation, turn it on.
1025 */
Alan Stern8520f382008-09-22 14:44:26 -04001026 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +08001027 if (hdev->parent && hub_is_superspeed(hdev)) {
1028 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1029 HUB_SET_DEPTH, USB_RT_HUB,
1030 hdev->level - 1, 0, NULL, 0,
1031 USB_CTRL_SET_TIMEOUT);
1032 if (ret < 0)
1033 dev_err(hub->intfdev,
1034 "set hub depth failed\n");
1035 }
Alan Stern8520f382008-09-22 14:44:26 -04001036
1037 /* Speed up system boot by using a delayed_work for the
1038 * hub's initial power-up delays. This is pretty awkward
1039 * and the implementation looks like a home-brewed sort of
1040 * setjmp/longjmp, but it saves at least 100 ms for each
1041 * root hub (assuming usbcore is compiled into the kernel
1042 * rather than as a module). It adds up.
1043 *
1044 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
1045 * because for those activation types the ports have to be
1046 * operational when we return. In theory this could be done
1047 * for HUB_POST_RESET, but it's easier not to.
1048 */
1049 if (type == HUB_INIT) {
Kris Borer17a364e2015-08-21 10:47:46 +05301050 delay = hub_power_on_good_delay(hub);
Dan Williams7ad3c472014-05-20 18:08:57 -07001051
1052 hub_power_on(hub, false);
Tejun Heo77fa83c2014-03-07 10:24:48 -05001053 INIT_DELAYED_WORK(&hub->init_work, hub_init_func2);
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08001054 queue_delayed_work(system_power_efficient_wq,
1055 &hub->init_work,
Alan Stern8520f382008-09-22 14:44:26 -04001056 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -04001057
1058 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -05001059 usb_autopm_get_interface_no_resume(
1060 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -04001061 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001062 } else if (type == HUB_RESET_RESUME) {
1063 /* The internal host controller state for the hub device
1064 * may be gone after a host power loss on system resume.
1065 * Update the device's info so the HW knows it's a hub.
1066 */
1067 hcd = bus_to_hcd(hdev->bus);
1068 if (hcd->driver->update_hub_device) {
1069 ret = hcd->driver->update_hub_device(hcd, hdev,
1070 &hub->tt, GFP_NOIO);
1071 if (ret < 0) {
1072 dev_err(hub->intfdev, "Host not "
1073 "accepting hub info "
1074 "update.\n");
1075 dev_err(hub->intfdev, "LS/FS devices "
1076 "and hubs may not work "
1077 "under this hub\n.");
1078 }
1079 }
1080 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001081 } else {
1082 hub_power_on(hub, true);
1083 }
1084 }
1085 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001086
Dan Williamsd99f6b42014-05-20 18:08:17 -07001087 /*
Petr Mladek37ebb542014-09-19 17:32:23 +02001088 * Check each port and set hub->change_bits to let hub_wq know
Alan Stern6ee0b272008-04-28 11:06:42 -04001089 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001090 */
1091 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07001092 struct usb_port *port_dev = hub->ports[port1 - 1];
1093 struct usb_device *udev = port_dev->child;
Alan Stern5e6effa2008-03-03 15:15:51 -05001094 u16 portstatus, portchange;
1095
Alan Stern6ee0b272008-04-28 11:06:42 -04001096 portstatus = portchange = 0;
1097 status = hub_port_status(hub, port1, &portstatus, &portchange);
Guenter Roeck181b0de2017-03-20 11:16:11 -07001098 if (status)
1099 goto abort;
1100
Alan Stern6ee0b272008-04-28 11:06:42 -04001101 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
Dan Williamsd99f6b42014-05-20 18:08:17 -07001102 dev_dbg(&port_dev->dev, "status %04x change %04x\n",
1103 portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001104
Dan Williamsd99f6b42014-05-20 18:08:17 -07001105 /*
1106 * After anything other than HUB_RESUME (i.e., initialization
Alan Stern6ee0b272008-04-28 11:06:42 -04001107 * or any sort of reset), every port should be disabled.
1108 * Unconnected ports should likewise be disabled (paranoia),
1109 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001110 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001111 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1112 type != HUB_RESUME ||
1113 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1114 !udev ||
1115 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001116 /*
1117 * USB3 protocol ports will automatically transition
1118 * to Enabled state when detect an USB3.0 device attach.
Dan Williamsfd1ac4c2013-10-07 11:58:20 -07001119 * Do not disable USB3 protocol ports, just pretend
1120 * power was lost
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001121 */
Dan Williamsfd1ac4c2013-10-07 11:58:20 -07001122 portstatus &= ~USB_PORT_STAT_ENABLE;
1123 if (!hub_is_superspeed(hdev))
Lan Tianyuad493e52013-01-23 04:26:30 +08001124 usb_clear_port_feature(hdev, port1,
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001125 USB_PORT_FEAT_ENABLE);
Alan Stern6ee0b272008-04-28 11:06:42 -04001126 }
1127
Jan-Marek Glogowskic4b857a2019-02-01 13:52:31 +01001128 /* Make sure a warm-reset request is handled by port_event */
1129 if (type == HUB_RESUME &&
1130 hub_port_warm_reset_required(hub, port1, portstatus))
1131 set_bit(port1, hub->event_bits);
1132
Mathias Nyman1fbecf82018-11-28 15:55:21 +02001133 /*
1134 * Add debounce if USB3 link is in polling/link training state.
1135 * Link will automatically transition to Enabled state after
1136 * link training completes.
1137 */
1138 if (hub_is_superspeed(hdev) &&
1139 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
1140 USB_SS_PORT_LS_POLLING))
1141 need_debounce_delay = true;
1142
Alan Stern948fea32008-04-28 11:07:07 -04001143 /* Clear status-change flags; we'll debounce later */
1144 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1145 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001146 usb_clear_port_feature(hub->hdev, port1,
Alan Stern948fea32008-04-28 11:07:07 -04001147 USB_PORT_FEAT_C_CONNECTION);
1148 }
1149 if (portchange & USB_PORT_STAT_C_ENABLE) {
1150 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001151 usb_clear_port_feature(hub->hdev, port1,
Alan Stern948fea32008-04-28 11:07:07 -04001152 USB_PORT_FEAT_C_ENABLE);
1153 }
Julius Wernere92aee32013-10-15 17:45:00 -07001154 if (portchange & USB_PORT_STAT_C_RESET) {
1155 need_debounce_delay = true;
1156 usb_clear_port_feature(hub->hdev, port1,
1157 USB_PORT_FEAT_C_RESET);
1158 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001159 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1160 hub_is_superspeed(hub->hdev)) {
1161 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001162 usb_clear_port_feature(hub->hdev, port1,
Don Zickus79c3dd82011-11-03 09:07:18 -04001163 USB_PORT_FEAT_C_BH_PORT_RESET);
1164 }
Alan Stern253e0572009-10-27 15:20:13 -04001165 /* We can forget about a "removed" device when there's a
1166 * physical disconnect or the connect status changes.
1167 */
1168 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1169 (portchange & USB_PORT_STAT_C_CONNECTION))
1170 clear_bit(port1, hub->removed_bits);
1171
Alan Stern6ee0b272008-04-28 11:06:42 -04001172 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
Petr Mladek37ebb542014-09-19 17:32:23 +02001173 /* Tell hub_wq to disconnect the device or
Bin Liue2996cf2018-07-19 14:39:37 -05001174 * check for a new connection or over current condition.
1175 * Based on USB2.0 Spec Section 11.12.5,
1176 * C_PORT_OVER_CURRENT could be set while
1177 * PORT_OVER_CURRENT is not. So check for any of them.
Alan Stern6ee0b272008-04-28 11:06:42 -04001178 */
Shen Guang08d1dec2014-01-08 14:45:42 +08001179 if (udev || (portstatus & USB_PORT_STAT_CONNECTION) ||
Keiya Nobuta940c0982020-01-09 14:14:48 +09001180 (portchange & USB_PORT_STAT_C_CONNECTION) ||
Bin Liue2996cf2018-07-19 14:39:37 -05001181 (portstatus & USB_PORT_STAT_OVERCURRENT) ||
1182 (portchange & USB_PORT_STAT_C_OVERCURRENT))
Alan Stern6ee0b272008-04-28 11:06:42 -04001183 set_bit(port1, hub->change_bits);
1184
1185 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001186 bool port_resumed = (portstatus &
1187 USB_PORT_STAT_LINK_STATE) ==
1188 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001189 /* The power session apparently survived the resume.
1190 * If there was an overcurrent or suspend change
Petr Mladek37ebb542014-09-19 17:32:23 +02001191 * (i.e., remote wakeup request), have hub_wq
Sarah Sharp72937e12012-01-24 11:46:50 -08001192 * take care of it. Look at the port link state
1193 * for USB 3.0 hubs, since they don't have a suspend
1194 * change bit, and they don't set the port link change
1195 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001196 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001197 if (portchange || (hub_is_superspeed(hub->hdev) &&
1198 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001199 set_bit(port1, hub->change_bits);
1200
1201 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001202#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001203 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001204#endif
Alan Stern8808f002008-04-28 11:06:55 -04001205
Alan Stern6ee0b272008-04-28 11:06:42 -04001206 } else {
Petr Mladek37ebb542014-09-19 17:32:23 +02001207 /* The power session is gone; tell hub_wq */
Alan Stern6ee0b272008-04-28 11:06:42 -04001208 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1209 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001210 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001211 }
1212
Alan Stern948fea32008-04-28 11:07:07 -04001213 /* If no port-status-change flags were set, we don't need any
1214 * debouncing. If flags were set we can try to debounce the
Petr Mladek37ebb542014-09-19 17:32:23 +02001215 * ports all at once right now, instead of letting hub_wq do them
Alan Stern948fea32008-04-28 11:07:07 -04001216 * one at a time later on.
1217 *
Petr Mladek37ebb542014-09-19 17:32:23 +02001218 * If any port-status changes do occur during this delay, hub_wq
Alan Stern948fea32008-04-28 11:07:07 -04001219 * will see them later and handle them normally.
1220 */
Alan Stern8520f382008-09-22 14:44:26 -04001221 if (need_debounce_delay) {
1222 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001223
Alan Stern8520f382008-09-22 14:44:26 -04001224 /* Don't do a long sleep inside a workqueue routine */
1225 if (type == HUB_INIT2) {
Tejun Heo77fa83c2014-03-07 10:24:48 -05001226 INIT_DELAYED_WORK(&hub->init_work, hub_init_func3);
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08001227 queue_delayed_work(system_power_efficient_wq,
1228 &hub->init_work,
Alan Stern8520f382008-09-22 14:44:26 -04001229 msecs_to_jiffies(delay));
Alan Stern07d316a2016-08-05 11:51:30 -04001230 device_unlock(&hdev->dev);
Alan Stern8520f382008-09-22 14:44:26 -04001231 return; /* Continues at init3: below */
1232 } else {
1233 msleep(delay);
1234 }
1235 }
1236 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001237 hub->quiescing = 0;
1238
1239 status = usb_submit_urb(hub->urb, GFP_NOIO);
1240 if (status < 0)
1241 dev_err(hub->intfdev, "activate --> %d\n", status);
1242 if (hub->has_indicators && blinkenlights)
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08001243 queue_delayed_work(system_power_efficient_wq,
1244 &hub->leds, LED_CYCLE_PERIOD);
Alan Sternf2835212008-04-28 11:07:17 -04001245
1246 /* Scan all ports that need attention */
Petr Mladek32a695892014-09-19 17:32:21 +02001247 kick_hub_wq(hub);
Guenter Roeck181b0de2017-03-20 11:16:11 -07001248 abort:
Alan Sternca5cbc82016-08-05 11:49:45 -04001249 if (type == HUB_INIT2 || type == HUB_INIT3) {
1250 /* Allow autosuspend if it was suppressed */
1251 disconnected:
Alan Stern8e4ceb32009-12-07 13:01:37 -05001252 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern07d316a2016-08-05 11:51:30 -04001253 device_unlock(&hdev->dev);
Alan Sternca5cbc82016-08-05 11:49:45 -04001254 }
Alan Sterne50293e2015-12-16 13:32:38 -05001255
1256 kref_put(&hub->kref, hub_release);
Alan Stern5e6effa2008-03-03 15:15:51 -05001257}
1258
Alan Stern8520f382008-09-22 14:44:26 -04001259/* Implement the continuations for the delays above */
1260static void hub_init_func2(struct work_struct *ws)
1261{
1262 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1263
1264 hub_activate(hub, HUB_INIT2);
1265}
1266
1267static void hub_init_func3(struct work_struct *ws)
1268{
1269 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1270
1271 hub_activate(hub, HUB_INIT3);
1272}
1273
Alan Stern43303542008-04-28 11:07:31 -04001274enum hub_quiescing_type {
1275 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1276};
1277
1278static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1279{
1280 struct usb_device *hdev = hub->hdev;
1281 int i;
1282
Petr Mladek37ebb542014-09-19 17:32:23 +02001283 /* hub_wq and related activity won't re-trigger */
Alan Stern43303542008-04-28 11:07:31 -04001284 hub->quiescing = 1;
1285
1286 if (type != HUB_SUSPEND) {
1287 /* Disconnect all the children */
1288 for (i = 0; i < hdev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001289 if (hub->ports[i]->child)
1290 usb_disconnect(&hub->ports[i]->child);
Alan Stern43303542008-04-28 11:07:31 -04001291 }
1292 }
1293
Petr Mladek37ebb542014-09-19 17:32:23 +02001294 /* Stop hub_wq and related activity */
Alan Stern43303542008-04-28 11:07:31 -04001295 usb_kill_urb(hub->urb);
1296 if (hub->has_indicators)
1297 cancel_delayed_work_sync(&hub->leds);
1298 if (hub->tt.hub)
Octavian Purdila036546b2012-10-23 11:33:12 +03001299 flush_work(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001300}
1301
Alan Stern600856c2014-05-20 18:08:07 -07001302static void hub_pm_barrier_for_all_ports(struct usb_hub *hub)
1303{
1304 int i;
1305
1306 for (i = 0; i < hub->hdev->maxchild; ++i)
1307 pm_runtime_barrier(&hub->ports[i]->dev);
1308}
1309
Alan Stern3eb14912008-03-03 15:15:43 -05001310/* caller has locked the hub device */
1311static int hub_pre_reset(struct usb_interface *intf)
1312{
1313 struct usb_hub *hub = usb_get_intfdata(intf);
1314
Alan Stern43303542008-04-28 11:07:31 -04001315 hub_quiesce(hub, HUB_PRE_RESET);
Alan Stern600856c2014-05-20 18:08:07 -07001316 hub->in_reset = 1;
1317 hub_pm_barrier_for_all_ports(hub);
Alan Sternf07600c2007-05-30 15:38:16 -04001318 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001319}
1320
1321/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001322static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001323{
Alan Stern7de18d82006-06-01 13:37:24 -04001324 struct usb_hub *hub = usb_get_intfdata(intf);
1325
Alan Stern600856c2014-05-20 18:08:07 -07001326 hub->in_reset = 0;
1327 hub_pm_barrier_for_all_ports(hub);
Alan Sternf2835212008-04-28 11:07:17 -04001328 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001329 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001330}
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332static int hub_configure(struct usb_hub *hub,
1333 struct usb_endpoint_descriptor *endpoint)
1334{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001335 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 struct usb_device *hdev = hub->hdev;
1337 struct device *hub_dev = hub->intfdev;
1338 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001339 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 unsigned int pipe;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001341 int maxp, ret, i;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001342 char *message = "out of memory";
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001343 unsigned unit_load;
1344 unsigned full_load;
Dan Williamsd8521af2014-05-20 18:08:28 -07001345 unsigned maxchild;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Alan Sternd697cdd2009-10-27 15:18:46 -04001347 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 ret = -ENOMEM;
1350 goto fail;
1351 }
1352
1353 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1354 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 ret = -ENOMEM;
1356 goto fail;
1357 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001358 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Johan Hovoldc67e87a2017-05-10 18:18:28 +02001360 hub->descriptor = kzalloc(sizeof(*hub->descriptor), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 ret = -ENOMEM;
1363 goto fail;
1364 }
1365
1366 /* Request the entire hub descriptor.
1367 * hub->descriptor can handle USB_MAXCHILDREN ports,
Johan Hovold3e4a4e62017-05-10 18:18:27 +02001368 * but a (non-SS) hub can/will return fewer bytes here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
John Youndbe79bb2001-09-17 00:00:00 -07001370 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (ret < 0) {
1372 message = "can't read hub descriptor";
1373 goto fail;
Johan Hovold12bfbe12017-05-10 18:18:29 +02001374 }
1375
1376 maxchild = USB_MAXCHILDREN;
1377 if (hub_is_superspeed(hdev))
1378 maxchild = min_t(unsigned, maxchild, USB_SS_MAXPORTS);
1379
1380 if (hub->descriptor->bNbrPorts > maxchild) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 message = "hub has too many ports!";
1382 ret = -ENODEV;
1383 goto fail;
David Linares769d73682013-03-25 10:50:27 +00001384 } else if (hub->descriptor->bNbrPorts == 0) {
1385 message = "hub doesn't have any ports!";
1386 ret = -ENODEV;
1387 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389
Dan Williamsd8521af2014-05-20 18:08:28 -07001390 maxchild = hub->descriptor->bNbrPorts;
1391 dev_info(hub_dev, "%d port%s detected\n", maxchild,
1392 (maxchild == 1) ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Dan Williamsd8521af2014-05-20 18:08:28 -07001394 hub->ports = kzalloc(maxchild * sizeof(struct usb_port *), GFP_KERNEL);
Lan Tianyuff823c72012-09-05 13:44:32 +08001395 if (!hub->ports) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001396 ret = -ENOMEM;
1397 goto fail;
1398 }
1399
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001400 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001401 if (hub_is_superspeed(hdev)) {
1402 unit_load = 150;
1403 full_load = 900;
1404 } else {
1405 unit_load = 100;
1406 full_load = 500;
1407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
John Youndbe79bb2001-09-17 00:00:00 -07001409 /* FIXME for USB 3.0, skip for now */
1410 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1411 !(hub_is_superspeed(hdev))) {
Matthias Beyer469271f2013-10-10 23:41:27 +02001412 char portstr[USB_MAXCHILDREN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Dan Williamsd8521af2014-05-20 18:08:28 -07001414 for (i = 0; i < maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001415 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1417 ? 'F' : 'R';
Dan Williamsd8521af2014-05-20 18:08:28 -07001418 portstr[maxchild] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1420 } else
1421 dev_dbg(hub_dev, "standalone hub\n");
1422
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001423 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301424 case HUB_CHAR_COMMON_LPSM:
1425 dev_dbg(hub_dev, "ganged power switching\n");
1426 break;
1427 case HUB_CHAR_INDV_PORT_LPSM:
1428 dev_dbg(hub_dev, "individual port power switching\n");
1429 break;
1430 case HUB_CHAR_NO_LPSM:
1431 case HUB_CHAR_LPSM:
1432 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1433 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001436 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301437 case HUB_CHAR_COMMON_OCPM:
1438 dev_dbg(hub_dev, "global over-current protection\n");
1439 break;
1440 case HUB_CHAR_INDV_PORT_OCPM:
1441 dev_dbg(hub_dev, "individual port over-current protection\n");
1442 break;
1443 case HUB_CHAR_NO_OCPM:
1444 case HUB_CHAR_OCPM:
1445 dev_dbg(hub_dev, "no over-current protection\n");
1446 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
1448
Chase Metzger17248562015-08-11 21:34:37 -07001449 spin_lock_init(&hub->tt.lock);
1450 INIT_LIST_HEAD(&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001451 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301453 case USB_HUB_PR_FS:
1454 break;
1455 case USB_HUB_PR_HS_SINGLE_TT:
1456 dev_dbg(hub_dev, "Single TT\n");
1457 hub->tt.hub = hdev;
1458 break;
1459 case USB_HUB_PR_HS_MULTI_TT:
1460 ret = usb_set_interface(hdev, 0, 1);
1461 if (ret == 0) {
1462 dev_dbg(hub_dev, "TT per port\n");
1463 hub->tt.multi = 1;
1464 } else
1465 dev_err(hub_dev, "Using single TT (err %d)\n",
1466 ret);
1467 hub->tt.hub = hdev;
1468 break;
1469 case USB_HUB_PR_SS:
1470 /* USB 3.0 hubs don't have a TT */
1471 break;
1472 default:
1473 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1474 hdev->descriptor.bDeviceProtocol);
1475 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
1477
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001478 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001479 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
Matthias Beyer469271f2013-10-10 23:41:27 +02001480 case HUB_TTTT_8_BITS:
1481 if (hdev->descriptor.bDeviceProtocol != 0) {
1482 hub->tt.think_time = 666;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001483 dev_dbg(hub_dev, "TT requires at most %d "
1484 "FS bit times (%d ns)\n",
Matthias Beyer469271f2013-10-10 23:41:27 +02001485 8, hub->tt.think_time);
1486 }
1487 break;
1488 case HUB_TTTT_16_BITS:
1489 hub->tt.think_time = 666 * 2;
1490 dev_dbg(hub_dev, "TT requires at most %d "
1491 "FS bit times (%d ns)\n",
1492 16, hub->tt.think_time);
1493 break;
1494 case HUB_TTTT_24_BITS:
1495 hub->tt.think_time = 666 * 3;
1496 dev_dbg(hub_dev, "TT requires at most %d "
1497 "FS bit times (%d ns)\n",
1498 24, hub->tt.think_time);
1499 break;
1500 case HUB_TTTT_32_BITS:
1501 hub->tt.think_time = 666 * 4;
1502 dev_dbg(hub_dev, "TT requires at most %d "
1503 "FS bit times (%d ns)\n",
1504 32, hub->tt.think_time);
1505 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507
1508 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001509 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 hub->has_indicators = 1;
1511 dev_dbg(hub_dev, "Port indicators are supported\n");
1512 }
1513
1514 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1515 hub->descriptor->bPwrOn2PwrGood * 2);
1516
1517 /* power budgeting mostly matters with bus-powered hubs,
1518 * and battery-powered root hubs (may provide just 8 mA).
1519 */
1520 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern15b73362013-07-30 15:35:40 -04001521 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 message = "can't get hub status";
1523 goto fail;
1524 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001525 hcd = bus_to_hcd(hdev->bus);
Alan Stern7d35b922005-04-25 11:18:32 -04001526 if (hdev == hdev->bus->root_hub) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001527 if (hcd->power_budget > 0)
1528 hdev->bus_mA = hcd->power_budget;
1529 else
Dan Williamsd8521af2014-05-20 18:08:28 -07001530 hdev->bus_mA = full_load * maxchild;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001531 if (hdev->bus_mA >= full_load)
1532 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001533 else {
1534 hub->mA_per_port = hdev->bus_mA;
1535 hub->limited_power = 1;
1536 }
Alan Stern7d35b922005-04-25 11:18:32 -04001537 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001538 int remaining = hdev->bus_mA -
1539 hub->descriptor->bHubContrCurrent;
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1542 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001543 hub->limited_power = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Dan Williamsd8521af2014-05-20 18:08:28 -07001545 if (remaining < maxchild * unit_load)
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001546 dev_warn(hub_dev,
Alan Stern55c52712005-11-23 12:03:12 -05001547 "insufficient power available "
1548 "to use all downstream ports\n");
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001549 hub->mA_per_port = unit_load; /* 7.2.1 */
1550
Alan Stern55c52712005-11-23 12:03:12 -05001551 } else { /* Self-powered external hub */
1552 /* FIXME: What about battery-powered external hubs that
1553 * provide less current per port? */
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001554 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001555 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001556 if (hub->mA_per_port < full_load)
Alan Stern55c52712005-11-23 12:03:12 -05001557 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1558 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1561 if (ret < 0) {
1562 message = "can't get hub status";
1563 goto fail;
1564 }
1565
1566 /* local power status reports aren't always correct */
1567 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1568 dev_dbg(hub_dev, "local power source is %s\n",
1569 (hubstatus & HUB_STATUS_LOCAL_POWER)
1570 ? "lost (inactive)" : "good");
1571
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001572 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 dev_dbg(hub_dev, "%sover-current condition exists\n",
1574 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1575
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001576 /* set up the interrupt endpoint
1577 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1578 * bytes as USB2.0[11.12.3] says because some hubs are known
1579 * to send more data (and thus cause overflow). For root hubs,
1580 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1581 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1583 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1584
1585 if (maxp > sizeof(*hub->buffer))
1586 maxp = sizeof(*hub->buffer);
1587
1588 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1589 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 ret = -ENOMEM;
1591 goto fail;
1592 }
1593
1594 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1595 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 /* maybe cycle the hub leds */
1598 if (hub->has_indicators && blinkenlights)
Matthias Beyer469271f2013-10-10 23:41:27 +02001599 hub->indicator[0] = INDICATOR_CYCLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Dan Williamsd8521af2014-05-20 18:08:28 -07001601 mutex_lock(&usb_port_peer_mutex);
1602 for (i = 0; i < maxchild; i++) {
Krzysztof Mazure58547e2013-08-22 14:49:39 +02001603 ret = usb_hub_create_port_device(hub, i + 1);
1604 if (ret < 0) {
Lan Tianyufa2a9562012-09-05 13:44:31 +08001605 dev_err(hub->intfdev,
1606 "couldn't create port%d device.\n", i + 1);
Dan Williamsd8521af2014-05-20 18:08:28 -07001607 break;
Krzysztof Mazure58547e2013-08-22 14:49:39 +02001608 }
1609 }
Dan Williamsd8521af2014-05-20 18:08:28 -07001610 hdev->maxchild = i;
Dan Williamse3d10502014-06-17 16:16:32 -07001611 for (i = 0; i < hdev->maxchild; i++) {
1612 struct usb_port *port_dev = hub->ports[i];
1613
1614 pm_runtime_put(&port_dev->dev);
1615 }
1616
Dan Williamsd8521af2014-05-20 18:08:28 -07001617 mutex_unlock(&usb_port_peer_mutex);
1618 if (ret < 0)
1619 goto fail;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001620
Petr Mladek37ebb542014-09-19 17:32:23 +02001621 /* Update the HCD's internal representation of this hub before hub_wq
Dan Williamse3d95582014-06-05 14:23:04 -07001622 * starts getting port status changes for devices under the hub.
1623 */
1624 if (hcd->driver->update_hub_device) {
1625 ret = hcd->driver->update_hub_device(hcd, hdev,
1626 &hub->tt, GFP_KERNEL);
1627 if (ret < 0) {
1628 message = "can't update HCD hub info";
1629 goto fail;
1630 }
1631 }
1632
Lan Tianyud2123fd2013-01-21 22:18:00 +08001633 usb_hub_adjust_deviceremovable(hdev, hub->descriptor);
1634
Alan Sternf2835212008-04-28 11:07:17 -04001635 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return 0;
1637
1638fail:
Chase Metzger17248562015-08-11 21:34:37 -07001639 dev_err(hub_dev, "config failed, %s (err %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 message, ret);
1641 /* hub_disconnect() frees urb and descriptor */
1642 return ret;
1643}
1644
Alan Sterne8054852007-05-04 11:55:11 -04001645static void hub_release(struct kref *kref)
1646{
1647 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1648
Petr Mladek5d14f322014-09-19 17:32:19 +02001649 usb_put_dev(hub->hdev);
Alan Sterne8054852007-05-04 11:55:11 -04001650 usb_put_intf(to_usb_interface(hub->intfdev));
1651 kfree(hub);
1652}
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654static unsigned highspeed_hubs;
1655
1656static void hub_disconnect(struct usb_interface *intf)
1657{
Huajun Li88162302012-03-12 21:00:19 +08001658 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001659 struct usb_device *hdev = interface_to_usbdev(intf);
Alan Stern543d7782014-01-07 10:43:02 -05001660 int port1;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001661
Petr Mladek32a695892014-09-19 17:32:21 +02001662 /*
1663 * Stop adding new hub events. We do not want to block here and thus
1664 * will not try to remove any pending work item.
1665 */
Alan Sterne8054852007-05-04 11:55:11 -04001666 hub->disconnected = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Alan Stern7de18d82006-06-01 13:37:24 -04001668 /* Disconnect all children and quiesce the hub */
1669 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001670 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001671
Dan Williamsd8521af2014-05-20 18:08:28 -07001672 mutex_lock(&usb_port_peer_mutex);
1673
Alan Stern543d7782014-01-07 10:43:02 -05001674 /* Avoid races with recursively_mark_NOTATTACHED() */
1675 spin_lock_irq(&device_state_lock);
1676 port1 = hdev->maxchild;
1677 hdev->maxchild = 0;
1678 usb_set_intfdata(intf, NULL);
1679 spin_unlock_irq(&device_state_lock);
Alexander Shishkin1f2235b2012-09-12 14:48:31 +03001680
Alan Stern543d7782014-01-07 10:43:02 -05001681 for (; port1 > 0; --port1)
1682 usb_hub_remove_port_device(hub, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Dan Williamsd8521af2014-05-20 18:08:28 -07001684 mutex_unlock(&usb_port_peer_mutex);
1685
Alan Sterne8054852007-05-04 11:55:11 -04001686 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 highspeed_hubs--;
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 usb_free_urb(hub->urb);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001690 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001691 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001692 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001693 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Lan Tianyu971fcd42013-01-23 04:26:29 +08001695 pm_suspend_ignore_children(&intf->dev, false);
Alan Sterne8054852007-05-04 11:55:11 -04001696 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
1699static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1700{
1701 struct usb_host_interface *desc;
1702 struct usb_endpoint_descriptor *endpoint;
1703 struct usb_device *hdev;
1704 struct usb_hub *hub;
1705
1706 desc = intf->cur_altsetting;
1707 hdev = interface_to_usbdev(intf);
1708
Ming Lei596d7892012-10-24 11:59:25 +08001709 /*
1710 * Set default autosuspend delay as 0 to speedup bus suspend,
1711 * based on the below considerations:
1712 *
1713 * - Unlike other drivers, the hub driver does not rely on the
1714 * autosuspend delay to provide enough time to handle a wakeup
1715 * event, and the submitted status URB is just to check future
1716 * change on hub downstream ports, so it is safe to do it.
1717 *
1718 * - The patch might cause one or more auto supend/resume for
1719 * below very rare devices when they are plugged into hub
1720 * first time:
1721 *
1722 * devices having trouble initializing, and disconnect
1723 * themselves from the bus and then reconnect a second
1724 * or so later
1725 *
1726 * devices just for downloading firmware, and disconnects
1727 * themselves after completing it
1728 *
1729 * For these quite rare devices, their drivers may change the
1730 * autosuspend delay of their parent hub in the probe() to one
1731 * appropriate value to avoid the subtle problem if someone
1732 * does care it.
1733 *
1734 * - The patch may cause one or more auto suspend/resume on
1735 * hub during running 'lsusb', but it is probably too
1736 * infrequent to worry about.
1737 *
1738 * - Change autosuspend delay of hub can avoid unnecessary auto
1739 * suspend timer for hub, also may decrease power consumption
1740 * of USB bus.
Roger Quadrosbdd405d2014-08-04 12:44:46 +03001741 *
1742 * - If user has indicated to prevent autosuspend by passing
1743 * usbcore.autosuspend = -1 then keep autosuspend disabled.
Ming Lei596d7892012-10-24 11:59:25 +08001744 */
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01001745#ifdef CONFIG_PM
Roger Quadrosbdd405d2014-08-04 12:44:46 +03001746 if (hdev->dev.power.autosuspend_delay >= 0)
1747 pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
Greg Kroah-Hartmana9ef8032014-08-27 16:55:29 -07001748#endif
Ming Lei596d7892012-10-24 11:59:25 +08001749
Alan Stern8ef42dd2014-05-23 10:45:54 -04001750 /*
1751 * Hubs have proper suspend/resume support, except for root hubs
1752 * where the controller driver doesn't have bus_suspend and
1753 * bus_resume methods.
1754 */
1755 if (hdev->parent) { /* normal device */
1756 usb_enable_autosuspend(hdev);
1757 } else { /* root hub */
1758 const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver;
1759
1760 if (drv->bus_suspend && drv->bus_resume)
1761 usb_enable_autosuspend(hdev);
1762 }
Alan Stern088f7fe2010-01-08 12:56:54 -05001763
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001764 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001765 dev_err(&intf->dev,
1766 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001767 return -E2BIG;
1768 }
1769
David Brownell89ccbdc2006-04-02 10:18:09 -08001770#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1771 if (hdev->parent) {
1772 dev_warn(&intf->dev, "ignoring external hub\n");
1773 return -ENODEV;
1774 }
1775#endif
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 /* Some hubs have a subclass of 1, which AFAICT according to the */
1778 /* specs is not defined, but it works */
1779 if ((desc->desc.bInterfaceSubClass != 0) &&
1780 (desc->desc.bInterfaceSubClass != 1)) {
1781descriptor_error:
Chase Metzger17248562015-08-11 21:34:37 -07001782 dev_err(&intf->dev, "bad descriptor, ignoring hub\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return -EIO;
1784 }
1785
1786 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1787 if (desc->desc.bNumEndpoints != 1)
1788 goto descriptor_error;
1789
1790 endpoint = &desc->endpoint[0].desc;
1791
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001792 /* If it's not an interrupt in endpoint, we'd better punt! */
1793 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 goto descriptor_error;
1795
1796 /* We found a hub */
Chase Metzger17248562015-08-11 21:34:37 -07001797 dev_info(&intf->dev, "USB hub found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001799 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Wolfram Sangb74e7062016-08-25 19:38:59 +02001800 if (!hub)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Alan Sterne8054852007-05-04 11:55:11 -04001803 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 hub->intfdev = &intf->dev;
1805 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001806 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001807 INIT_DELAYED_WORK(&hub->init_work, NULL);
Petr Mladek32a695892014-09-19 17:32:21 +02001808 INIT_WORK(&hub->events, hub_event);
Alan Sterne8054852007-05-04 11:55:11 -04001809 usb_get_intf(intf);
Petr Mladek5d14f322014-09-19 17:32:19 +02001810 usb_get_dev(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Chase Metzger17248562015-08-11 21:34:37 -07001812 usb_set_intfdata(intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001813 intf->needs_remote_wakeup = 1;
Lan Tianyu971fcd42013-01-23 04:26:29 +08001814 pm_suspend_ignore_children(&intf->dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 if (hdev->speed == USB_SPEED_HIGH)
1817 highspeed_hubs++;
1818
Ming Leie6f30de2012-10-24 11:59:24 +08001819 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1820 hub->quirk_check_port_auto_suspend = 1;
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (hub_configure(hub, endpoint) >= 0)
1823 return 0;
1824
Chase Metzger17248562015-08-11 21:34:37 -07001825 hub_disconnect(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return -ENODEV;
1827}
1828
1829static int
1830hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1831{
Chase Metzger17248562015-08-11 21:34:37 -07001832 struct usb_device *hdev = interface_to_usbdev(intf);
Lan Tianyuad493e52013-01-23 04:26:30 +08001833 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 /* assert ifno == 0 (part of hub spec) */
1836 switch (code) {
1837 case USBDEVFS_HUB_PORTINFO: {
1838 struct usbdevfs_hub_portinfo *info = user_data;
1839 int i;
1840
1841 spin_lock_irq(&device_state_lock);
1842 if (hdev->devnum <= 0)
1843 info->nports = 0;
1844 else {
1845 info->nports = hdev->maxchild;
1846 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001847 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 info->port[i] = 0;
1849 else
1850 info->port[i] =
Lan Tianyuff823c72012-09-05 13:44:32 +08001851 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 }
1853 }
1854 spin_unlock_irq(&device_state_lock);
1855
1856 return info->nports + 1;
1857 }
1858
1859 default:
1860 return -ENOSYS;
1861 }
1862}
1863
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001864/*
1865 * Allow user programs to claim ports on a hub. When a device is attached
1866 * to one of these "claimed" ports, the program will "own" the device.
1867 */
1868static int find_port_owner(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001869 struct usb_dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001870{
Mathias Nyman41341262013-06-18 17:28:48 +03001871 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
1872
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001873 if (hdev->state == USB_STATE_NOTATTACHED)
1874 return -ENODEV;
1875 if (port1 == 0 || port1 > hdev->maxchild)
1876 return -EINVAL;
1877
Mathias Nyman41341262013-06-18 17:28:48 +03001878 /* Devices not managed by the hub driver
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001879 * will always have maxchild equal to 0.
1880 */
Mathias Nyman41341262013-06-18 17:28:48 +03001881 *ppowner = &(hub->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001882 return 0;
1883}
1884
1885/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001886int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001887 struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001888{
1889 int rc;
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001890 struct usb_dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001891
1892 rc = find_port_owner(hdev, port1, &powner);
1893 if (rc)
1894 return rc;
1895 if (*powner)
1896 return -EBUSY;
1897 *powner = owner;
1898 return rc;
1899}
Valentina Manea6080cd02014-03-08 14:53:34 +02001900EXPORT_SYMBOL_GPL(usb_hub_claim_port);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001901
Lan Tianyu336c5c32012-07-06 14:13:52 +08001902int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001903 struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001904{
1905 int rc;
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001906 struct usb_dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001907
1908 rc = find_port_owner(hdev, port1, &powner);
1909 if (rc)
1910 return rc;
1911 if (*powner != owner)
1912 return -ENOENT;
1913 *powner = NULL;
1914 return rc;
1915}
Valentina Manea6080cd02014-03-08 14:53:34 +02001916EXPORT_SYMBOL_GPL(usb_hub_release_port);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001917
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001918void usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001919{
Lan Tianyuad493e52013-01-23 04:26:30 +08001920 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001921 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001922
Lan Tianyufa2a9562012-09-05 13:44:31 +08001923 for (n = 0; n < hdev->maxchild; n++) {
1924 if (hub->ports[n]->port_owner == owner)
1925 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001926 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001927
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001928}
1929
1930/* The caller must hold udev's lock */
1931bool usb_device_is_owned(struct usb_device *udev)
1932{
1933 struct usb_hub *hub;
1934
1935 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1936 return false;
Lan Tianyuad493e52013-01-23 04:26:30 +08001937 hub = usb_hub_to_struct_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001938 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001939}
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1942{
Lan Tianyuad493e52013-01-23 04:26:30 +08001943 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 int i;
1945
1946 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001947 if (hub->ports[i]->child)
1948 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001950 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001951 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 udev->state = USB_STATE_NOTATTACHED;
1953}
1954
1955/**
1956 * usb_set_device_state - change a device's current state (usbcore, hcds)
1957 * @udev: pointer to device whose state should be changed
1958 * @new_state: new state value to be stored
1959 *
1960 * udev->state is _not_ fully protected by the device lock. Although
1961 * most transitions are made only while holding the lock, the state can
1962 * can change to USB_STATE_NOTATTACHED at almost any time. This
1963 * is so that devices can be marked as disconnected as soon as possible,
1964 * without having to wait for any semaphores to be released. As a result,
1965 * all changes to any device's state must be protected by the
1966 * device_state_lock spinlock.
1967 *
1968 * Once a device has been added to the device tree, all changes to its state
1969 * should be made using this routine. The state should _not_ be set directly.
1970 *
1971 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1972 * Otherwise udev->state is set to new_state, and if new_state is
1973 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1974 * to USB_STATE_NOTATTACHED.
1975 */
1976void usb_set_device_state(struct usb_device *udev,
1977 enum usb_device_state new_state)
1978{
1979 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001980 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 spin_lock_irqsave(&device_state_lock, flags);
1983 if (udev->state == USB_STATE_NOTATTACHED)
1984 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001985 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001986
1987 /* root hub wakeup capabilities are managed out-of-band
1988 * and may involve silicon errata ... ignore them here.
1989 */
1990 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001991 if (udev->state == USB_STATE_SUSPENDED
1992 || new_state == USB_STATE_SUSPENDED)
1993 ; /* No change to wakeup settings */
1994 else if (new_state == USB_STATE_CONFIGURED)
Lu Baoluddbe1fc2014-09-19 10:13:50 +08001995 wakeup = (udev->quirks &
1996 USB_QUIRK_IGNORE_REMOTE_WAKEUP) ? 0 :
1997 udev->actconfig->desc.bmAttributes &
1998 USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001999 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01002000 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08002001 }
Sarah Sharp15123002007-12-21 16:54:15 -08002002 if (udev->state == USB_STATE_SUSPENDED &&
2003 new_state != USB_STATE_SUSPENDED)
2004 udev->active_duration -= jiffies;
2005 else if (new_state == USB_STATE_SUSPENDED &&
2006 udev->state != USB_STATE_SUSPENDED)
2007 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04002008 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07002009 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 recursively_mark_NOTATTACHED(udev);
2011 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01002012 if (wakeup >= 0)
2013 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
David Vrabel6da9c992009-02-18 14:43:47 +00002015EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002017/*
Alan Stern3b29b682011-02-22 09:53:41 -05002018 * Choose a device number.
2019 *
2020 * Device numbers are used as filenames in usbfs. On USB-1.1 and
2021 * USB-2.0 buses they are also used as device addresses, however on
2022 * USB-3.0 buses the address is assigned by the controller hardware
2023 * and it usually is not the same as the device number.
2024 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002025 * WUSB devices are simple: they have no hubs behind, so the mapping
2026 * device <-> virtual port number becomes 1:1. Why? to simplify the
2027 * life of the device connection logic in
2028 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
2029 * handshake we need to assign a temporary address in the unauthorized
2030 * space. For simplicity we use the first virtual port number found to
2031 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
2032 * and that becomes it's address [X < 128] or its unauthorized address
2033 * [X | 0x80].
2034 *
2035 * We add 1 as an offset to the one-based USB-stack port number
2036 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
2037 * 0 is reserved by USB for default address; (b) Linux's USB stack
2038 * uses always #1 for the root hub of the controller. So USB stack's
2039 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07002040 *
2041 * Devices connected under xHCI are not as simple. The host controller
2042 * supports virtualization, so the hardware assigns device addresses and
2043 * the HCD must setup data structures before issuing a set address
2044 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002045 */
Alan Stern3b29b682011-02-22 09:53:41 -05002046static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 int devnum;
2049 struct usb_bus *bus = udev->bus;
2050
Petr Mladek638139e2014-09-19 17:32:24 +02002051 /* be safe when more hub events are proceed in parallel */
Chris Bainbridgefeb26ac2016-04-25 13:48:38 +01002052 mutex_lock(&bus->devnum_next_mutex);
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002053 if (udev->wusb) {
2054 devnum = udev->portnum + 1;
2055 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
2056 } else {
2057 /* Try to allocate the next devnum beginning at
2058 * bus->devnum_next. */
2059 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
2060 bus->devnum_next);
2061 if (devnum >= 128)
2062 devnum = find_next_zero_bit(bus->devmap.devicemap,
2063 128, 1);
Matthias Beyer469271f2013-10-10 23:41:27 +02002064 bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1);
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (devnum < 128) {
2067 set_bit(devnum, bus->devmap.devicemap);
2068 udev->devnum = devnum;
2069 }
Chris Bainbridgefeb26ac2016-04-25 13:48:38 +01002070 mutex_unlock(&bus->devnum_next_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
Alan Stern3b29b682011-02-22 09:53:41 -05002073static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
2075 if (udev->devnum > 0) {
2076 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
2077 udev->devnum = -1;
2078 }
2079}
2080
Alan Stern3b29b682011-02-22 09:53:41 -05002081static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07002082{
2083 /* The address for a WUSB device is managed by wusbcore. */
2084 if (!udev->wusb)
2085 udev->devnum = devnum;
2086}
2087
Herbert Xuf7410ce2010-01-10 20:15:03 +11002088static void hub_free_dev(struct usb_device *udev)
2089{
2090 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2091
2092 /* Root hubs aren't real devices, so don't free HCD resources */
2093 if (hcd->driver->free_dev && udev->parent)
2094 hcd->driver->free_dev(hcd, udev);
2095}
2096
Dan Williams7027df32014-05-20 18:09:36 -07002097static void hub_disconnect_children(struct usb_device *udev)
2098{
2099 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
2100 int i;
2101
2102 /* Free up all the children before we remove this device */
2103 for (i = 0; i < udev->maxchild; i++) {
2104 if (hub->ports[i]->child)
2105 usb_disconnect(&hub->ports[i]->child);
2106 }
2107}
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109/**
2110 * usb_disconnect - disconnect a device (usbcore-internal)
2111 * @pdev: pointer to device being disconnected
2112 * Context: !in_interrupt ()
2113 *
2114 * Something got disconnected. Get rid of it and all of its children.
2115 *
2116 * If *pdev is a normal device then the parent hub must already be locked.
Heiner Kallweita4b5d602016-02-03 23:35:23 +01002117 * If *pdev is a root hub then the caller must hold the usb_bus_idr_lock,
Bjorn Helgaasdb8f2aa2013-09-13 13:57:34 -06002118 * which protects the set of root hubs as well as the list of buses.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 *
2120 * Only hub drivers (including virtual root hub drivers for host
2121 * controllers) should ever call this.
2122 *
2123 * This call is synchronous, and may not be used in an interrupt context.
2124 */
2125void usb_disconnect(struct usb_device **pdev)
2126{
Dan Williams7027df32014-05-20 18:09:36 -07002127 struct usb_port *port_dev = NULL;
2128 struct usb_device *udev = *pdev;
Peter Chen5b1dc202014-08-19 08:56:21 +08002129 struct usb_hub *hub = NULL;
2130 int port1 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 /* mark the device as inactive, so any further urb submissions for
2133 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002134 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 */
2136 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05002137 dev_info(&udev->dev, "USB disconnect, device number %d\n",
2138 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Guenter Roeck219a99d2017-03-20 14:30:50 -07002140 /*
2141 * Ensure that the pm runtime code knows that the USB device
2142 * is in the process of being disconnected.
2143 */
2144 pm_runtime_barrier(&udev->dev);
2145
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002146 usb_lock_device(udev);
2147
Dan Williams7027df32014-05-20 18:09:36 -07002148 hub_disconnect_children(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 /* deallocate hcd/hardware state ... nuking all pending urbs and
2151 * cleaning up all state associated with the current configuration
2152 * so that the hardware is now fully quiesced.
2153 */
Chase Metzger17248562015-08-11 21:34:37 -07002154 dev_dbg(&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04002156 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Lan Tianyufde26382013-01-20 01:53:33 +08002158 if (udev->parent) {
Dan Williams7027df32014-05-20 18:09:36 -07002159 port1 = udev->portnum;
2160 hub = usb_hub_to_struct_hub(udev->parent);
2161 port_dev = hub->ports[port1 - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002162
2163 sysfs_remove_link(&udev->dev.kobj, "port");
2164 sysfs_remove_link(&port_dev->dev.kobj, "device");
Lan Tianyu971fcd42013-01-23 04:26:29 +08002165
Dan Williams7027df32014-05-20 18:09:36 -07002166 /*
2167 * As usb_port_runtime_resume() de-references udev, make
2168 * sure no resumes occur during removal
2169 */
2170 if (!test_and_set_bit(port1, hub->child_usage_bits))
2171 pm_runtime_get_sync(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002172 }
2173
Alan Stern3b23dd62008-12-05 14:10:34 -05002174 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04002175 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07002176
Alan Stern782da722006-07-01 22:09:35 -04002177 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002178 * for de-configuring the device and invoking the remove-device
2179 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002180 */
2181 device_del(&udev->dev);
2182
2183 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 * (or root_hub) pointer.
2185 */
Alan Stern3b29b682011-02-22 09:53:41 -05002186 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 /* Avoid races with recursively_mark_NOTATTACHED() */
2189 spin_lock_irq(&device_state_lock);
2190 *pdev = NULL;
2191 spin_unlock_irq(&device_state_lock);
2192
Dan Williams7027df32014-05-20 18:09:36 -07002193 if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits))
2194 pm_runtime_put(&port_dev->dev);
2195
Herbert Xuf7410ce2010-01-10 20:15:03 +11002196 hub_free_dev(udev);
2197
Alan Stern782da722006-07-01 22:09:35 -04002198 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199}
2200
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002201#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202static void show_string(struct usb_device *udev, char *id, char *string)
2203{
2204 if (!string)
2205 return;
Joe Perchesf2ec5222012-10-28 01:05:51 -07002206 dev_info(&udev->dev, "%s: %s\n", id, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
2208
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002209static void announce_device(struct usb_device *udev)
2210{
2211 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2212 le16_to_cpu(udev->descriptor.idVendor),
2213 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002214 dev_info(&udev->dev,
2215 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002216 udev->descriptor.iManufacturer,
2217 udev->descriptor.iProduct,
2218 udev->descriptor.iSerialNumber);
2219 show_string(udev, "Product", udev->product);
2220 show_string(udev, "Manufacturer", udev->manufacturer);
2221 show_string(udev, "SerialNumber", udev->serial);
2222}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002224static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225#endif
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Oliver Neukum3ede7602007-01-11 14:35:50 +01002228/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002229 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002230 * @udev: newly addressed device (in ADDRESS state)
2231 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002232 * Finish enumeration for On-The-Go devices
Yacine Belkadi626f0902013-08-02 20:10:04 +02002233 *
2234 * Return: 0 if successful. A negative error code otherwise.
Oliver Neukum3ede7602007-01-11 14:35:50 +01002235 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002236static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002238 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
2240#ifdef CONFIG_USB_OTG
2241 /*
2242 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2243 * to wake us after we've powered off VBUS; and HNP, switching roles
2244 * "host" to "peripheral". The OTG descriptor helps figure this out.
2245 */
2246 if (!udev->bus->is_b_host
2247 && udev->config
2248 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002249 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 struct usb_bus *bus = udev->bus;
Li Jun7d2d6412015-08-31 16:20:49 +08002251 unsigned port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 /* descriptor may appear anywhere in config */
Li Jun7d2d6412015-08-31 16:20:49 +08002254 err = __usb_get_extra_descriptor(udev->rawdescriptors[0],
2255 le16_to_cpu(udev->config[0].desc.wTotalLength),
Mathias Payerfe26b8d2018-12-05 21:19:59 +01002256 USB_DT_OTG, (void **) &desc, sizeof(*desc));
Li Jun7d2d6412015-08-31 16:20:49 +08002257 if (err || !(desc->bmAttributes & USB_OTG_HNP))
2258 return 0;
David Brownellfc849b82006-09-18 16:53:26 -07002259
Li Jun7d2d6412015-08-31 16:20:49 +08002260 dev_info(&udev->dev, "Dual-Role OTG device on %sHNP port\n",
2261 (port1 == bus->otg_port) ? "" : "non-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Li Jun7d2d6412015-08-31 16:20:49 +08002263 /* enable HNP before suspend, it's simpler */
2264 if (port1 == bus->otg_port) {
2265 bus->b_hnp_enable = 1;
2266 err = usb_control_msg(udev,
2267 usb_sndctrlpipe(udev, 0),
2268 USB_REQ_SET_FEATURE, 0,
2269 USB_DEVICE_B_HNP_ENABLE,
2270 0, NULL, 0,
2271 USB_CTRL_SET_TIMEOUT);
2272 if (err < 0) {
2273 /*
2274 * OTG MESSAGE: report errors here,
2275 * customize to match your product.
2276 */
2277 dev_err(&udev->dev, "can't set HNP mode: %d\n",
2278 err);
2279 bus->b_hnp_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
Li Jun7d2d6412015-08-31 16:20:49 +08002281 } else if (desc->bLength == sizeof
2282 (struct usb_otg_descriptor)) {
2283 /* Set a_alt_hnp_support for legacy otg device */
2284 err = usb_control_msg(udev,
2285 usb_sndctrlpipe(udev, 0),
2286 USB_REQ_SET_FEATURE, 0,
2287 USB_DEVICE_A_ALT_HNP_SUPPORT,
2288 0, NULL, 0,
2289 USB_CTRL_SET_TIMEOUT);
2290 if (err < 0)
2291 dev_err(&udev->dev,
2292 "set a_alt_hnp_support failed: %d\n",
2293 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
2295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002297 return err;
2298}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002300
2301/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002302 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002303 * @udev: newly addressed device (in ADDRESS state)
2304 *
2305 * This is only called by usb_new_device() and usb_authorize_device()
2306 * and FIXME -- all comments that apply to them apply here wrt to
2307 * environment.
2308 *
2309 * If the device is WUSB and not authorized, we don't attempt to read
2310 * the string descriptors, as they will be errored out by the device
2311 * until it has been authorized.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002312 *
2313 * Return: 0 if successful. A negative error code otherwise.
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002314 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002315static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002316{
2317 int err;
Peter Chen026f3fc2014-08-19 09:51:53 +08002318 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002319
2320 if (udev->config == NULL) {
2321 err = usb_get_configuration(udev);
2322 if (err < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04002323 if (err != -ENODEV)
2324 dev_err(&udev->dev, "can't read configurations, error %d\n",
2325 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002326 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002327 }
2328 }
Thomas Pugliese83e83ec2013-12-09 13:40:29 -06002329
2330 /* read the standard strings and cache them if present */
2331 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2332 udev->manufacturer = usb_cache_string(udev,
2333 udev->descriptor.iManufacturer);
2334 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2335
Alan Stern8d8558d2009-12-08 15:50:41 -05002336 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002337 if (err < 0)
2338 return err;
2339
Peter Chen026f3fc2014-08-19 09:51:53 +08002340 if (IS_ENABLED(CONFIG_USB_OTG_WHITELIST) && hcd->tpl_support &&
Peter Chene5a9d622014-09-29 10:09:31 +08002341 !is_targeted(udev)) {
Peter Chen026f3fc2014-08-19 09:51:53 +08002342 /* Maybe it can talk to us, though we can't talk to it.
2343 * (Includes HNP test device.)
2344 */
Peter Chene5a9d622014-09-29 10:09:31 +08002345 if (IS_ENABLED(CONFIG_USB_OTG) && (udev->bus->b_hnp_enable
2346 || udev->bus->is_b_host)) {
Peter Chen026f3fc2014-08-19 09:51:53 +08002347 err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND);
2348 if (err < 0)
2349 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2350 }
2351 return -ENOTSUPP;
2352 }
2353
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002354 usb_detect_interface_quirks(udev);
2355
2356 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002357}
2358
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002359static void set_usb_port_removable(struct usb_device *udev)
2360{
2361 struct usb_device *hdev = udev->parent;
2362 struct usb_hub *hub;
2363 u8 port = udev->portnum;
2364 u16 wHubCharacteristics;
2365 bool removable = true;
2366
2367 if (!hdev)
2368 return;
2369
Lan Tianyuad493e52013-01-23 04:26:30 +08002370 hub = usb_hub_to_struct_hub(udev->parent);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002371
Matthew Garrett92bfbf72015-04-08 16:36:00 -07002372 /*
2373 * If the platform firmware has provided information about a port,
2374 * use that to determine whether it's removable.
2375 */
2376 switch (hub->ports[udev->portnum - 1]->connect_type) {
2377 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
2378 udev->removable = USB_DEVICE_REMOVABLE;
2379 return;
2380 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
Matthew Garrett58339c22015-04-08 16:36:01 -07002381 case USB_PORT_NOT_USED:
Matthew Garrett92bfbf72015-04-08 16:36:00 -07002382 udev->removable = USB_DEVICE_FIXED;
2383 return;
Matthew Garrett58339c22015-04-08 16:36:01 -07002384 default:
2385 break;
Matthew Garrett92bfbf72015-04-08 16:36:00 -07002386 }
2387
2388 /*
2389 * Otherwise, check whether the hub knows whether a port is removable
2390 * or not
2391 */
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002392 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2393
2394 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2395 return;
2396
2397 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002398 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2399 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002400 removable = false;
2401 } else {
2402 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2403 removable = false;
2404 }
2405
2406 if (removable)
2407 udev->removable = USB_DEVICE_REMOVABLE;
2408 else
2409 udev->removable = USB_DEVICE_FIXED;
Dan Williamsa4204ff2014-05-20 18:08:22 -07002410
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002411}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002412
2413/**
2414 * usb_new_device - perform initial device setup (usbcore-internal)
2415 * @udev: newly addressed device (in ADDRESS state)
2416 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002417 * This is called with devices which have been detected but not fully
2418 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002419 * for any device configuration. The caller must have locked either
2420 * the parent hub (if udev is a normal device) or else the
Heiner Kallweita4b5d602016-02-03 23:35:23 +01002421 * usb_bus_idr_lock (if udev is a root hub). The parent's pointer to
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002422 * udev has already been installed, but udev is not yet visible through
2423 * sysfs or other filesystem code.
2424 *
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002425 * This call is synchronous, and may not be used in an interrupt context.
2426 *
2427 * Only the hub driver or root-hub registrar should ever call this.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002428 *
2429 * Return: Whether the device is configured properly or not. Zero if the
2430 * interface was registered with the driver core; else a negative errno
2431 * value.
2432 *
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002433 */
2434int usb_new_device(struct usb_device *udev)
2435{
2436 int err;
2437
Dan Streetman16985402010-01-06 09:56:53 -05002438 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002439 /* Initialize non-root-hub device wakeup to disabled;
2440 * device (un)configuration controls wakeup capable
2441 * sysfs power/wakeup controls wakeup enabled/disabled
2442 */
2443 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002444 }
2445
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002446 /* Tell the runtime-PM framework the device is active */
2447 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002448 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002449 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002450 pm_runtime_enable(&udev->dev);
2451
Alan Sternc08512c2010-11-15 15:57:58 -05002452 /* By default, forbid autosuspend for all devices. It will be
2453 * allowed for hubs during binding.
2454 */
2455 usb_disable_autosuspend(udev);
2456
Alan Stern8d8558d2009-12-08 15:50:41 -05002457 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002458 if (err < 0)
2459 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002460 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2461 udev->devnum, udev->bus->busnum,
2462 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002463 /* export the usbdev device-node for libusb */
2464 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2465 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2466
Alan Stern6cd13202008-11-13 15:08:30 -05002467 /* Tell the world! */
2468 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002469
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002470 if (udev->serial)
2471 add_device_randomness(udev->serial, strlen(udev->serial));
2472 if (udev->product)
2473 add_device_randomness(udev->product, strlen(udev->product));
2474 if (udev->manufacturer)
2475 add_device_randomness(udev->manufacturer,
2476 strlen(udev->manufacturer));
2477
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002478 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002479
Dan Williamsa4204ff2014-05-20 18:08:22 -07002480 /* check whether the hub or firmware marks this port as non-removable */
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002481 if (udev->parent)
2482 set_usb_port_removable(udev);
2483
Alan Stern782da722006-07-01 22:09:35 -04002484 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002485 * for configuring the device and invoking the add-device
2486 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002487 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002488 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 if (err) {
2490 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2491 goto fail;
2492 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002493
Lan Tianyufde26382013-01-20 01:53:33 +08002494 /* Create link files between child device and usb port device. */
2495 if (udev->parent) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002496 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
Dan Williamsd5c38342014-05-20 18:08:52 -07002497 int port1 = udev->portnum;
2498 struct usb_port *port_dev = hub->ports[port1 - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002499
2500 err = sysfs_create_link(&udev->dev.kobj,
2501 &port_dev->dev.kobj, "port");
2502 if (err)
2503 goto fail;
2504
2505 err = sysfs_create_link(&port_dev->dev.kobj,
2506 &udev->dev.kobj, "device");
2507 if (err) {
2508 sysfs_remove_link(&udev->dev.kobj, "port");
2509 goto fail;
2510 }
Lan Tianyu971fcd42013-01-23 04:26:29 +08002511
Dan Williamsd5c38342014-05-20 18:08:52 -07002512 if (!test_and_set_bit(port1, hub->child_usage_bits))
2513 pm_runtime_get_sync(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002514 }
2515
Alan Stern3b23dd62008-12-05 14:10:34 -05002516 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002517 usb_mark_last_busy(udev);
2518 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002519 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521fail:
2522 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002523 pm_runtime_disable(&udev->dev);
2524 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002525 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002528
2529/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002530 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2531 * @usb_dev: USB device
2532 *
2533 * Move the USB device to a very basic state where interfaces are disabled
2534 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002535 *
2536 * We share a lock (that we have) with device_del(), so we need to
2537 * defer its call.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002538 *
2539 * Return: 0.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002540 */
2541int usb_deauthorize_device(struct usb_device *usb_dev)
2542{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002543 usb_lock_device(usb_dev);
2544 if (usb_dev->authorized == 0)
2545 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002546
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002547 usb_dev->authorized = 0;
2548 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002549
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002550out_unauthorized:
2551 usb_unlock_device(usb_dev);
2552 return 0;
2553}
2554
2555
2556int usb_authorize_device(struct usb_device *usb_dev)
2557{
2558 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002559
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002560 usb_lock_device(usb_dev);
2561 if (usb_dev->authorized == 1)
2562 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002563
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002564 result = usb_autoresume_device(usb_dev);
2565 if (result < 0) {
2566 dev_err(&usb_dev->dev,
2567 "can't autoresume for authorization: %d\n", result);
2568 goto error_autoresume;
2569 }
Josef Gajduseke50a3222014-10-09 15:47:54 +02002570
2571 if (usb_dev->wusb) {
2572 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2573 if (result < 0) {
2574 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2575 "authorization: %d\n", result);
2576 goto error_device_descriptor;
2577 }
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002578 }
Alan Sternda307122009-12-08 15:54:44 -05002579
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002580 usb_dev->authorized = 1;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002581 /* Choose and set the configuration. This registers the interfaces
2582 * with the driver core and lets interface drivers bind to them.
2583 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002584 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002585 if (c >= 0) {
2586 result = usb_set_configuration(usb_dev, c);
2587 if (result) {
2588 dev_err(&usb_dev->dev,
2589 "can't set config #%d, error %d\n", c, result);
2590 /* This need not be fatal. The user can try to
2591 * set other configurations. */
2592 }
2593 }
2594 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002595
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002596error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002597 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002598error_autoresume:
2599out_authorized:
Matthias Beyer781b2132013-10-10 23:41:29 +02002600 usb_unlock_device(usb_dev); /* complements locktree */
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002601 return result;
2602}
2603
Mathias Nyman0cdd49a2015-12-10 09:59:29 +02002604/*
2605 * Return 1 if port speed is SuperSpeedPlus, 0 otherwise
2606 * check it from the link protocol field of the current speed ID attribute.
2607 * current speed ID is got from ext port status request. Sublink speed attribute
2608 * table is returned with the hub BOS SSP device capability descriptor
2609 */
2610static int port_speed_is_ssp(struct usb_device *hdev, int speed_id)
2611{
2612 int ssa_count;
2613 u32 ss_attr;
2614 int i;
2615 struct usb_ssp_cap_descriptor *ssp_cap = hdev->bos->ssp_cap;
2616
2617 if (!ssp_cap)
2618 return 0;
2619
2620 ssa_count = le32_to_cpu(ssp_cap->bmAttributes) &
2621 USB_SSP_SUBLINK_SPEED_ATTRIBS;
2622
2623 for (i = 0; i <= ssa_count; i++) {
2624 ss_attr = le32_to_cpu(ssp_cap->bmSublinkSpeedAttr[i]);
2625 if (speed_id == (ss_attr & USB_SSP_SUBLINK_SPEED_SSID))
2626 return !!(ss_attr & USB_SSP_SUBLINK_SPEED_LP);
2627 }
2628 return 0;
2629}
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002630
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002631/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2632static unsigned hub_is_wusb(struct usb_hub *hub)
2633{
2634 struct usb_hcd *hcd;
2635 if (hub->hdev->parent != NULL) /* not a root hub? */
2636 return 0;
Geliang Tang6ae706a2015-12-23 21:26:51 +08002637 hcd = bus_to_hcd(hub->hdev->bus);
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002638 return hcd->wireless;
2639}
2640
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642#define PORT_RESET_TRIES 5
2643#define SET_ADDRESS_TRIES 2
2644#define GET_DESCRIPTOR_TRIES 2
2645#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302646#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2649#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002650#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651#define HUB_LONG_RESET_TIME 200
Sarah Sharp77c7f072012-11-14 17:16:52 -08002652#define HUB_RESET_TIMEOUT 800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Dan Williams48fc7db2013-12-05 17:07:27 -08002654/*
2655 * "New scheme" enumeration causes an extra state transition to be
2656 * exposed to an xhci host and causes USB3 devices to receive control
2657 * commands in the default state. This has been seen to cause
2658 * enumeration failures, so disable this enumeration scheme for USB3
2659 * devices.
2660 */
2661static bool use_new_scheme(struct usb_device *udev, int retry)
2662{
Mathias Nyman8a1b2722015-12-10 09:59:25 +02002663 if (udev->speed >= USB_SPEED_SUPER)
Dan Williams48fc7db2013-12-05 17:07:27 -08002664 return false;
2665
2666 return USE_NEW_SCHEME(retry);
2667}
2668
Rahul Bedarkar025d4432014-01-04 11:24:41 +05302669/* Is a USB 3.0 port in the Inactive or Compliance Mode state?
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002670 * Port worm reset is required to recover
2671 */
Dan Williams3cd12f92014-05-29 12:58:46 -07002672static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
2673 u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002674{
Dan Williams3cd12f92014-05-29 12:58:46 -07002675 u16 link_state;
2676
2677 if (!hub_is_superspeed(hub->hdev))
2678 return false;
2679
2680 if (test_bit(port1, hub->warm_reset_bits))
2681 return true;
2682
2683 link_state = portstatus & USB_PORT_STAT_LINK_STATE;
2684 return link_state == USB_SS_PORT_LS_SS_INACTIVE
2685 || link_state == USB_SS_PORT_LS_COMP_MOD;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002686}
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002689 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
2691 int delay_time, ret;
2692 u16 portstatus;
2693 u16 portchange;
Mathias Nyman0cdd49a2015-12-10 09:59:29 +02002694 u32 ext_portstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
2696 for (delay_time = 0;
2697 delay_time < HUB_RESET_TIMEOUT;
2698 delay_time += delay) {
2699 /* wait to give the device a chance to reset */
2700 msleep(delay);
2701
2702 /* read and decode port status */
Mathias Nyman0cdd49a2015-12-10 09:59:29 +02002703 if (hub_is_superspeedplus(hub->hdev))
2704 ret = hub_ext_port_status(hub, port1,
2705 HUB_EXT_PORT_STATUS,
2706 &portstatus, &portchange,
2707 &ext_portstatus);
2708 else
2709 ret = hub_port_status(hub, port1, &portstatus,
2710 &portchange);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (ret < 0)
2712 return ret;
2713
Guenter Roeckec0c5f02016-12-01 13:49:59 -08002714 /*
2715 * The port state is unknown until the reset completes.
2716 *
2717 * On top of that, some chips may require additional time
2718 * to re-establish a connection after the reset is complete,
2719 * so also wait for the connection to be re-established.
2720 */
2721 if (!(portstatus & USB_PORT_STAT_RESET) &&
2722 (portstatus & USB_PORT_STAT_CONNECTION))
Sarah Sharp470f0be2012-12-11 10:14:03 -08002723 break;
Sarah Sharp4f434472012-11-15 14:58:04 -08002724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 /* switch to the long delay after two short delay failures */
2726 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2727 delay = HUB_LONG_RESET_TIME;
2728
Dan Williamsd99f6b42014-05-20 18:08:17 -07002729 dev_dbg(&hub->ports[port1 - 1]->dev,
2730 "not %sreset yet, waiting %dms\n",
2731 warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 }
2733
Sarah Sharp470f0be2012-12-11 10:14:03 -08002734 if ((portstatus & USB_PORT_STAT_RESET))
2735 return -EBUSY;
2736
Dan Williams3cd12f92014-05-29 12:58:46 -07002737 if (hub_port_warm_reset_required(hub, port1, portstatus))
Sarah Sharp470f0be2012-12-11 10:14:03 -08002738 return -ENOTCONN;
2739
2740 /* Device went away? */
2741 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2742 return -ENOTCONN;
2743
Mathias Nymand15fc532017-10-17 16:07:33 +03002744 /* Retry if connect change is set but status is still connected.
2745 * A USB 3.0 connection may bounce if multiple warm resets were issued,
Sarah Sharp470f0be2012-12-11 10:14:03 -08002746 * but the device may have successfully re-connected. Ignore it.
2747 */
2748 if (!hub_is_superspeed(hub->hdev) &&
Mathias Nymand15fc532017-10-17 16:07:33 +03002749 (portchange & USB_PORT_STAT_C_CONNECTION)) {
2750 usb_clear_port_feature(hub->hdev, port1,
2751 USB_PORT_FEAT_C_CONNECTION);
2752 return -EAGAIN;
2753 }
Sarah Sharp470f0be2012-12-11 10:14:03 -08002754
2755 if (!(portstatus & USB_PORT_STAT_ENABLE))
2756 return -EBUSY;
2757
2758 if (!udev)
2759 return 0;
2760
2761 if (hub_is_wusb(hub))
2762 udev->speed = USB_SPEED_WIRELESS;
Mathias Nyman0cdd49a2015-12-10 09:59:29 +02002763 else if (hub_is_superspeedplus(hub->hdev) &&
2764 port_speed_is_ssp(hub->hdev, ext_portstatus &
2765 USB_EXT_PORT_STAT_RX_SPEED_ID))
2766 udev->speed = USB_SPEED_SUPER_PLUS;
Sarah Sharp470f0be2012-12-11 10:14:03 -08002767 else if (hub_is_superspeed(hub->hdev))
2768 udev->speed = USB_SPEED_SUPER;
2769 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2770 udev->speed = USB_SPEED_HIGH;
2771 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2772 udev->speed = USB_SPEED_LOW;
2773 else
2774 udev->speed = USB_SPEED_FULL;
2775 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776}
2777
Andiry Xu75d7cf72011-09-13 16:41:11 -07002778/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002780 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
2782 int i, status;
Sarah Sharpa24a6072012-11-01 11:20:44 -07002783 u16 portchange, portstatus;
Dan Williamsd99f6b42014-05-20 18:08:17 -07002784 struct usb_port *port_dev = hub->ports[port1 - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002786 if (!hub_is_superspeed(hub->hdev)) {
2787 if (warm) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002788 dev_err(hub->intfdev, "only USB3 hub support "
2789 "warm reset\n");
2790 return -EINVAL;
2791 }
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002792 /* Block EHCI CF initialization during the port reset.
2793 * Some companion controllers don't like it when they mix.
2794 */
2795 down_read(&ehci_cf_port_reset_rwsem);
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07002796 } else if (!warm) {
2797 /*
2798 * If the caller hasn't explicitly requested a warm reset,
2799 * double check and see if one is needed.
2800 */
Robert Schlabbachfb6d1f72015-05-26 00:27:30 +02002801 if (hub_port_status(hub, port1, &portstatus, &portchange) == 0)
2802 if (hub_port_warm_reset_required(hub, port1,
2803 portstatus))
2804 warm = true;
Andiry Xu75d7cf72011-09-13 16:41:11 -07002805 }
Dan Williams3cd12f92014-05-29 12:58:46 -07002806 clear_bit(port1, hub->warm_reset_bits);
Alan Stern32fe0192007-10-10 16:27:07 -04002807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 /* Reset the port */
2809 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002810 status = set_port_feature(hub->hdev, port1, (warm ?
2811 USB_PORT_FEAT_BH_PORT_RESET :
2812 USB_PORT_FEAT_RESET));
Alan Sterne9e88fb2013-03-27 16:14:01 -04002813 if (status == -ENODEV) {
2814 ; /* The hub is gone */
2815 } else if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002816 dev_err(&port_dev->dev,
2817 "cannot %sreset (err = %d)\n",
2818 warm ? "warm " : "", status);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002819 } else {
2820 status = hub_port_wait_reset(hub, port1, udev, delay,
2821 warm);
Alan Sterne9e88fb2013-03-27 16:14:01 -04002822 if (status && status != -ENOTCONN && status != -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 dev_dbg(hub->intfdev,
2824 "port_wait_reset: err = %d\n",
2825 status);
2826 }
2827
Sarah Sharpa24a6072012-11-01 11:20:44 -07002828 /* Check for disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002829 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
Robert Schlabbachfb6d1f72015-05-26 00:27:30 +02002830 usb_clear_port_feature(hub->hdev, port1,
2831 USB_PORT_FEAT_C_RESET);
Sarah Sharpa24a6072012-11-01 11:20:44 -07002832
2833 if (!hub_is_superspeed(hub->hdev))
2834 goto done;
2835
Robert Schlabbachfb6d1f72015-05-26 00:27:30 +02002836 usb_clear_port_feature(hub->hdev, port1,
2837 USB_PORT_FEAT_C_BH_PORT_RESET);
2838 usb_clear_port_feature(hub->hdev, port1,
2839 USB_PORT_FEAT_C_PORT_LINK_STATE);
Dennis Wassenbergb7e04482018-11-13 14:40:34 +01002840
2841 if (udev)
2842 usb_clear_port_feature(hub->hdev, port1,
Robert Schlabbachfb6d1f72015-05-26 00:27:30 +02002843 USB_PORT_FEAT_C_CONNECTION);
2844
Sarah Sharpa24a6072012-11-01 11:20:44 -07002845 /*
2846 * If a USB 3.0 device migrates from reset to an error
2847 * state, re-issue the warm reset.
2848 */
2849 if (hub_port_status(hub, port1,
2850 &portstatus, &portchange) < 0)
2851 goto done;
2852
Dan Williams3cd12f92014-05-29 12:58:46 -07002853 if (!hub_port_warm_reset_required(hub, port1,
2854 portstatus))
Sarah Sharpa24a6072012-11-01 11:20:44 -07002855 goto done;
2856
2857 /*
2858 * If the port is in SS.Inactive or Compliance Mode, the
2859 * hot or warm reset failed. Try another warm reset.
2860 */
2861 if (!warm) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002862 dev_dbg(&port_dev->dev,
2863 "hot reset failed, warm reset\n");
Sarah Sharpa24a6072012-11-01 11:20:44 -07002864 warm = true;
2865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 }
2867
Dan Williamsd99f6b42014-05-20 18:08:17 -07002868 dev_dbg(&port_dev->dev,
2869 "not enabled, trying %sreset again...\n",
2870 warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 delay = HUB_LONG_RESET_TIME;
2872 }
2873
Dan Williamsd99f6b42014-05-20 18:08:17 -07002874 dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Andiry Xu75d7cf72011-09-13 16:41:11 -07002876done:
Robert Schlabbachfb6d1f72015-05-26 00:27:30 +02002877 if (status == 0) {
2878 /* TRSTRCY = 10 ms; plus some extra */
2879 msleep(10 + 40);
2880 if (udev) {
2881 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2882
2883 update_devnum(udev, 0);
2884 /* The xHC may think the device is already reset,
2885 * so ignore the status.
2886 */
2887 if (hcd->driver->reset_device)
2888 hcd->driver->reset_device(hcd, udev);
2889
2890 usb_set_device_state(udev, USB_STATE_DEFAULT);
2891 }
2892 } else {
2893 if (udev)
2894 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
2895 }
2896
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002897 if (!hub_is_superspeed(hub->hdev))
Andiry Xu75d7cf72011-09-13 16:41:11 -07002898 up_read(&ehci_cf_port_reset_rwsem);
2899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 return status;
2901}
2902
Andiry Xu0ed9a572011-04-27 18:07:43 +08002903/* Check if a port is power on */
2904static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2905{
2906 int ret = 0;
2907
2908 if (hub_is_superspeed(hub->hdev)) {
2909 if (portstatus & USB_SS_PORT_STAT_POWER)
2910 ret = 1;
2911 } else {
2912 if (portstatus & USB_PORT_STAT_POWER)
2913 ret = 1;
2914 }
2915
2916 return ret;
2917}
2918
Dan Williams5c79a1e2014-05-20 18:09:26 -07002919static void usb_lock_port(struct usb_port *port_dev)
2920 __acquires(&port_dev->status_lock)
2921{
2922 mutex_lock(&port_dev->status_lock);
2923 __acquire(&port_dev->status_lock);
2924}
2925
2926static void usb_unlock_port(struct usb_port *port_dev)
2927 __releases(&port_dev->status_lock)
2928{
2929 mutex_unlock(&port_dev->status_lock);
2930 __release(&port_dev->status_lock);
2931}
2932
Alan Sternd388dab2006-07-01 22:14:24 -04002933#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Andiry Xu0ed9a572011-04-27 18:07:43 +08002935/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2936static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2937{
2938 int ret = 0;
2939
2940 if (hub_is_superspeed(hub->hdev)) {
2941 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2942 == USB_SS_PORT_LS_U3)
2943 ret = 1;
2944 } else {
2945 if (portstatus & USB_PORT_STAT_SUSPEND)
2946 ret = 1;
2947 }
2948
2949 return ret;
2950}
Alan Sternb01b03f2008-04-28 11:06:11 -04002951
2952/* Determine whether the device on a port is ready for a normal resume,
2953 * is ready for a reset-resume, or should be disconnected.
2954 */
2955static int check_port_resume_type(struct usb_device *udev,
2956 struct usb_hub *hub, int port1,
Julius Werner7fa40912015-01-27 13:20:12 -08002957 int status, u16 portchange, u16 portstatus)
Alan Sternb01b03f2008-04-28 11:06:11 -04002958{
Dan Williamsd99f6b42014-05-20 18:08:17 -07002959 struct usb_port *port_dev = hub->ports[port1 - 1];
Julius Werner7fa40912015-01-27 13:20:12 -08002960 int retries = 3;
Dan Williamsd99f6b42014-05-20 18:08:17 -07002961
Julius Werner7fa40912015-01-27 13:20:12 -08002962 retry:
Dan Williams3cd12f92014-05-29 12:58:46 -07002963 /* Is a warm reset needed to recover the connection? */
2964 if (status == 0 && udev->reset_resume
2965 && hub_port_warm_reset_required(hub, port1, portstatus)) {
2966 /* pass */;
2967 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002968 /* Is the device still present? */
Dan Williams3cd12f92014-05-29 12:58:46 -07002969 else if (status || port_is_suspended(hub, portstatus) ||
Julius Werner7fa40912015-01-27 13:20:12 -08002970 !port_is_power_on(hub, portstatus)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002971 if (status >= 0)
2972 status = -ENODEV;
Julius Werner7fa40912015-01-27 13:20:12 -08002973 } else if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
2974 if (retries--) {
2975 usleep_range(200, 300);
2976 status = hub_port_status(hub, port1, &portstatus,
2977 &portchange);
2978 goto retry;
2979 }
2980 status = -ENODEV;
Alan Sternb01b03f2008-04-28 11:06:11 -04002981 }
2982
Alan Stern86c57ed2008-06-30 11:14:43 -04002983 /* Can't do a normal resume if the port isn't enabled,
2984 * so try a reset-resume instead.
2985 */
2986 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2987 if (udev->persist_enabled)
2988 udev->reset_resume = 1;
2989 else
2990 status = -ENODEV;
2991 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002992
2993 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002994 dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n",
2995 portchange, portstatus, status);
Alan Sternb01b03f2008-04-28 11:06:11 -04002996 } else if (udev->reset_resume) {
2997
2998 /* Late port handoff can set status-change bits */
2999 if (portchange & USB_PORT_STAT_C_CONNECTION)
Lan Tianyuad493e52013-01-23 04:26:30 +08003000 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04003001 USB_PORT_FEAT_C_CONNECTION);
3002 if (portchange & USB_PORT_STAT_C_ENABLE)
Lan Tianyuad493e52013-01-23 04:26:30 +08003003 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04003004 USB_PORT_FEAT_C_ENABLE);
3005 }
3006
3007 return status;
3008}
3009
Sarah Sharpf74631e2012-06-25 12:08:08 -07003010int usb_disable_ltm(struct usb_device *udev)
3011{
3012 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3013
3014 /* Check if the roothub and device supports LTM. */
3015 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
3016 !usb_device_supports_ltm(udev))
3017 return 0;
3018
3019 /* Clear Feature LTM Enable can only be sent if the device is
3020 * configured.
3021 */
3022 if (!udev->actconfig)
3023 return 0;
3024
3025 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3026 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
3027 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
3028 USB_CTRL_SET_TIMEOUT);
3029}
3030EXPORT_SYMBOL_GPL(usb_disable_ltm);
3031
3032void usb_enable_ltm(struct usb_device *udev)
3033{
3034 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3035
3036 /* Check if the roothub and device supports LTM. */
3037 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
3038 !usb_device_supports_ltm(udev))
3039 return;
3040
3041 /* Set Feature LTM Enable can only be sent if the device is
3042 * configured.
3043 */
3044 if (!udev->actconfig)
3045 return;
3046
3047 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3048 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
3049 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
3050 USB_CTRL_SET_TIMEOUT);
3051}
3052EXPORT_SYMBOL_GPL(usb_enable_ltm);
3053
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003054/*
Alan Stern28e86162013-07-30 15:37:33 -04003055 * usb_enable_remote_wakeup - enable remote wakeup for a device
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003056 * @udev: target device
3057 *
Alan Stern28e86162013-07-30 15:37:33 -04003058 * For USB-2 devices: Set the device's remote wakeup feature.
3059 *
3060 * For USB-3 devices: Assume there's only one function on the device and
3061 * enable remote wake for the first interface. FIXME if the interface
3062 * association descriptor shows there's more than one function.
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003063 */
Alan Stern28e86162013-07-30 15:37:33 -04003064static int usb_enable_remote_wakeup(struct usb_device *udev)
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003065{
Alan Stern28e86162013-07-30 15:37:33 -04003066 if (udev->speed < USB_SPEED_SUPER)
3067 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3068 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
3069 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
3070 USB_CTRL_SET_TIMEOUT);
3071 else
3072 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3073 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
3074 USB_INTRF_FUNC_SUSPEND,
3075 USB_INTRF_FUNC_SUSPEND_RW |
3076 USB_INTRF_FUNC_SUSPEND_LP,
3077 NULL, 0, USB_CTRL_SET_TIMEOUT);
3078}
3079
3080/*
3081 * usb_disable_remote_wakeup - disable remote wakeup for a device
3082 * @udev: target device
3083 *
3084 * For USB-2 devices: Clear the device's remote wakeup feature.
3085 *
3086 * For USB-3 devices: Assume there's only one function on the device and
3087 * disable remote wake for the first interface. FIXME if the interface
3088 * association descriptor shows there's more than one function.
3089 */
3090static int usb_disable_remote_wakeup(struct usb_device *udev)
3091{
3092 if (udev->speed < USB_SPEED_SUPER)
3093 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3094 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
3095 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
3096 USB_CTRL_SET_TIMEOUT);
3097 else
3098 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Yonglong Wu4e248002016-08-19 11:37:26 +08003099 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003100 USB_INTRF_FUNC_SUSPEND, 0, NULL, 0,
3101 USB_CTRL_SET_TIMEOUT);
3102}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Alan Sterne583d9d2013-07-11 14:58:04 -04003104/* Count of wakeup-enabled devices at or below udev */
3105static unsigned wakeup_enabled_descendants(struct usb_device *udev)
3106{
3107 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
3108
3109 return udev->do_remote_wakeup +
3110 (hub ? hub->wakeup_enabled_descendants : 0);
3111}
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113/*
Alan Stern140d8f62006-07-01 22:07:21 -04003114 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04003115 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07003116 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 *
3118 * Suspends a USB device that isn't in active use, conserving power.
3119 * Devices may wake out of a suspend, if anything important happens,
3120 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04003121 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 * to disconnect devices while they are suspended.
3123 *
David Brownell390a8c32005-09-13 19:57:27 -07003124 * This only affects the USB hardware for a device; its interfaces
3125 * (and, for hubs, child devices) must already have been suspended.
3126 *
Alan Stern624d6c02007-05-30 15:35:16 -04003127 * Selective port suspend reduces power; most suspended devices draw
3128 * less than 500 uA. It's also used in OTG, along with remote wakeup.
3129 * All devices below the suspended port are also suspended.
3130 *
3131 * Devices leave suspend state when the host wakes them up. Some devices
3132 * also support "remote wakeup", where the device can activate the USB
3133 * tree above them to deliver data, such as a keypress or packet. In
3134 * some cases, this wakes the USB host.
3135 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 * Suspending OTG devices may trigger HNP, if that's been enabled
3137 * between a pair of dual-role devices. That will change roles, such
3138 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
3139 *
Alan Stern4956ecc2007-05-30 16:51:28 -04003140 * Devices on USB hub ports have only one "suspend" state, corresponding
3141 * to ACPI D2, "may cause the device to lose some context".
3142 * State transitions include:
3143 *
3144 * - suspend, resume ... when the VBUS power link stays live
3145 * - suspend, disconnect ... VBUS lost
3146 *
3147 * Once VBUS drop breaks the circuit, the port it's using has to go through
3148 * normal re-enumeration procedures, starting with enabling VBUS power.
3149 * Other than re-initializing the hub (plug/unplug, except for root hubs),
Petr Mladek37ebb542014-09-19 17:32:23 +02003150 * Linux (2.6) currently has NO mechanisms to initiate that: no hub_wq
Alan Stern4956ecc2007-05-30 16:51:28 -04003151 * timer, no SRP, no requests through sysfs.
3152 *
Alan Sterne583d9d2013-07-11 14:58:04 -04003153 * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
3154 * suspended until their bus goes into global suspend (i.e., the root
Alan Stern0aa28322013-03-27 16:14:19 -04003155 * hub is suspended). Nevertheless, we change @udev->state to
3156 * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual
3157 * upstream port setting is stored in @udev->port_is_suspended.
Alan Stern4956ecc2007-05-30 16:51:28 -04003158 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 * Returns 0 on success, else negative errno.
3160 */
Alan Stern65bfd292008-11-25 16:39:18 -05003161int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
Lan Tianyuad493e52013-01-23 04:26:30 +08003163 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3164 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003165 int port1 = udev->portnum;
3166 int status;
Alan Stern0aa28322013-03-27 16:14:19 -04003167 bool really_suspend = true;
Alan Stern4956ecc2007-05-30 16:51:28 -04003168
Dan Williams5c79a1e2014-05-20 18:09:26 -07003169 usb_lock_port(port_dev);
3170
Alan Stern624d6c02007-05-30 15:35:16 -04003171 /* enable remote wakeup when appropriate; this lets the device
3172 * wake up the upstream hub (including maybe the root hub).
3173 *
3174 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
3175 * we don't explicitly enable it here.
3176 */
3177 if (udev->do_remote_wakeup) {
Alan Stern28e86162013-07-30 15:37:33 -04003178 status = usb_enable_remote_wakeup(udev);
Oliver Neukum0c487202009-10-19 13:19:41 +02003179 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003180 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
3181 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02003182 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02003183 if (PMSG_IS_AUTO(msg))
Alan Stern4fae6f02013-07-30 15:39:02 -04003184 goto err_wakeup;
Oliver Neukum0c487202009-10-19 13:19:41 +02003185 }
Alan Stern624d6c02007-05-30 15:35:16 -04003186 }
3187
Andiry Xu65580b432011-09-23 14:19:52 -07003188 /* disable USB2 hardware LPM */
Kai-Heng Feng9b916af2019-01-12 03:54:25 +08003189 usb_disable_usb2_hardware_lpm(udev);
Andiry Xu65580b432011-09-23 14:19:52 -07003190
Sarah Sharpf74631e2012-06-25 12:08:08 -07003191 if (usb_disable_ltm(udev)) {
Alan Stern4fae6f02013-07-30 15:39:02 -04003192 dev_err(&udev->dev, "Failed to disable LTM before suspend\n.");
3193 status = -ENOMEM;
3194 if (PMSG_IS_AUTO(msg))
3195 goto err_ltm;
Sarah Sharpf74631e2012-06-25 12:08:08 -07003196 }
Sarah Sharp83060952012-05-02 14:25:52 -07003197 if (usb_unlocked_disable_lpm(udev)) {
Alan Stern4fae6f02013-07-30 15:39:02 -04003198 dev_err(&udev->dev, "Failed to disable LPM before suspend\n.");
3199 status = -ENOMEM;
3200 if (PMSG_IS_AUTO(msg))
3201 goto err_lpm3;
Sarah Sharp83060952012-05-02 14:25:52 -07003202 }
3203
Alan Stern624d6c02007-05-30 15:35:16 -04003204 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08003205 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003206 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
Alan Sterne583d9d2013-07-11 14:58:04 -04003207
Alan Stern0aa28322013-03-27 16:14:19 -04003208 /*
3209 * For system suspend, we do not need to enable the suspend feature
3210 * on individual USB-2 ports. The devices will automatically go
3211 * into suspend a few ms after the root hub stops sending packets.
3212 * The USB 2.0 spec calls this "global suspend".
Alan Sterne583d9d2013-07-11 14:58:04 -04003213 *
3214 * However, many USB hubs have a bug: They don't relay wakeup requests
3215 * from a downstream port if the port's suspend feature isn't on.
3216 * Therefore we will turn on the suspend feature if udev or any of its
3217 * descendants is enabled for remote wakeup.
Alan Stern0aa28322013-03-27 16:14:19 -04003218 */
Alan Sterne583d9d2013-07-11 14:58:04 -04003219 else if (PMSG_IS_AUTO(msg) || wakeup_enabled_descendants(udev) > 0)
3220 status = set_port_feature(hub->hdev, port1,
3221 USB_PORT_FEAT_SUSPEND);
Alan Stern0aa28322013-03-27 16:14:19 -04003222 else {
3223 really_suspend = false;
3224 status = 0;
3225 }
Alan Stern624d6c02007-05-30 15:35:16 -04003226 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003227 dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status);
Alan Sterncbb33002011-06-15 16:29:16 -04003228
Alan Stern4fae6f02013-07-30 15:39:02 -04003229 /* Try to enable USB3 LPM and LTM again */
3230 usb_unlocked_enable_lpm(udev);
3231 err_lpm3:
3232 usb_enable_ltm(udev);
3233 err_ltm:
Andiry Xuc3e751e2012-05-05 00:50:10 +08003234 /* Try to enable USB2 hardware LPM again */
Kai-Heng Feng9b916af2019-01-12 03:54:25 +08003235 usb_enable_usb2_hardware_lpm(udev);
Andiry Xuc3e751e2012-05-05 00:50:10 +08003236
Alan Stern4fae6f02013-07-30 15:39:02 -04003237 if (udev->do_remote_wakeup)
3238 (void) usb_disable_remote_wakeup(udev);
3239 err_wakeup:
Sarah Sharp83060952012-05-02 14:25:52 -07003240
Alan Sterncbb33002011-06-15 16:29:16 -04003241 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02003242 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003243 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04003244 } else {
Alan Stern30b1a7a2011-09-27 21:54:22 +02003245 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
3246 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
3247 udev->do_remote_wakeup);
Alan Stern0aa28322013-03-27 16:14:19 -04003248 if (really_suspend) {
3249 udev->port_is_suspended = 1;
Alan Sterne583d9d2013-07-11 14:58:04 -04003250
3251 /* device has up to 10 msec to fully suspend */
Alan Stern0aa28322013-03-27 16:14:19 -04003252 msleep(10);
3253 }
Alan Sterne583d9d2013-07-11 14:58:04 -04003254 usb_set_device_state(udev, USB_STATE_SUSPENDED);
Alan Stern624d6c02007-05-30 15:35:16 -04003255 }
Lan Tianyuad493e52013-01-23 04:26:30 +08003256
Dan Williamsd5c38342014-05-20 18:08:52 -07003257 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled
3258 && test_and_clear_bit(port1, hub->child_usage_bits))
Lan Tianyu98a4f1f2013-07-03 22:17:54 +08003259 pm_runtime_put_sync(&port_dev->dev);
Lan Tianyuad493e52013-01-23 04:26:30 +08003260
Alan Sternc08512c2010-11-15 15:57:58 -05003261 usb_mark_last_busy(hub->hdev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07003262
3263 usb_unlock_port(port_dev);
Alan Stern4956ecc2007-05-30 16:51:28 -04003264 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265}
David Brownellf3f32532005-09-22 22:37:29 -07003266
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267/*
David Brownell390a8c32005-09-13 19:57:27 -07003268 * If the USB "suspend" state is in use (rather than "global suspend"),
3269 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04003270 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 * hardware resume signaling is finished, either because of selective
3272 * resume (by host) or remote wakeup (by device) ... now see what changed
3273 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04003274 *
3275 * If @udev->reset_resume is set then the device is reset before the
3276 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 */
Alan Stern140d8f62006-07-01 22:07:21 -04003278static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279{
Alan Stern54515fe2007-05-30 15:38:58 -04003280 int status = 0;
Oliver Neukum07e72b92012-11-29 15:05:57 +01003281 u16 devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
3283 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003284 dev_dbg(&udev->dev, "%s\n",
3285 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
3287 /* usb ch9 identifies four variants of SUSPENDED, based on what
3288 * state the device resumes to. Linux currently won't see the
3289 * first two on the host side; they'd be inside hub_port_init()
Petr Mladek37ebb542014-09-19 17:32:23 +02003290 * during many timeouts, but hub_wq can't suspend until later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 */
3292 usb_set_device_state(udev, udev->actconfig
3293 ? USB_STATE_CONFIGURED
3294 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
Alan Stern54515fe2007-05-30 15:38:58 -04003296 /* 10.5.4.5 says not to reset a suspended port if the attached
3297 * device is enabled for remote wakeup. Hence the reset
3298 * operation is carried out here, after the port has been
3299 * resumed.
3300 */
Alan Stern1d102552014-03-18 10:39:05 -04003301 if (udev->reset_resume) {
3302 /*
3303 * If the device morphs or switches modes when it is reset,
3304 * we don't want to perform a reset-resume. We'll fail the
3305 * resume, which will cause a logical disconnect, and then
3306 * the device will be rediscovered.
3307 */
Alan Stern86c57ed2008-06-30 11:14:43 -04003308 retry_reset_resume:
Alan Stern1d102552014-03-18 10:39:05 -04003309 if (udev->quirks & USB_QUIRK_RESET)
3310 status = -ENODEV;
3311 else
3312 status = usb_reset_and_verify_device(udev);
3313 }
Alan Stern54515fe2007-05-30 15:38:58 -04003314
Matthias Beyer469271f2013-10-10 23:41:27 +02003315 /* 10.5.4.5 says be sure devices in the tree are still there.
3316 * For now let's assume the device didn't go crazy on resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 * and device drivers will know about any resume quirks.
3318 */
Alan Stern54515fe2007-05-30 15:38:58 -04003319 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04003320 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04003321 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
Alan Stern86c57ed2008-06-30 11:14:43 -04003322
3323 /* If a normal resume failed, try doing a reset-resume */
3324 if (status && !udev->reset_resume && udev->persist_enabled) {
3325 dev_dbg(&udev->dev, "retry with reset-resume\n");
3326 udev->reset_resume = 1;
3327 goto retry_reset_resume;
3328 }
Alan Stern54515fe2007-05-30 15:38:58 -04003329 }
Alan Sternb40b7a92006-06-19 15:12:38 -04003330
Alan Stern624d6c02007-05-30 15:35:16 -04003331 if (status) {
3332 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
3333 status);
Oliver Neukum07e72b92012-11-29 15:05:57 +01003334 /*
3335 * There are a few quirky devices which violate the standard
3336 * by claiming to have remote wakeup enabled after a reset,
3337 * which crash if the feature is cleared, hence check for
3338 * udev->reset_resume
3339 */
3340 } else if (udev->actconfig && !udev->reset_resume) {
Alan Stern28e86162013-07-30 15:37:33 -04003341 if (udev->speed < USB_SPEED_SUPER) {
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003342 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
Alan Stern28e86162013-07-30 15:37:33 -04003343 status = usb_disable_remote_wakeup(udev);
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003344 } else {
3345 status = usb_get_status(udev, USB_RECIP_INTERFACE, 0,
3346 &devstatus);
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003347 if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP
3348 | USB_INTRF_STAT_FUNC_RW))
Alan Stern28e86162013-07-30 15:37:33 -04003349 status = usb_disable_remote_wakeup(udev);
Alan Stern4bf0ba82005-11-21 11:58:07 -05003350 }
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003351
3352 if (status)
3353 dev_dbg(&udev->dev,
3354 "disable remote wakeup, status %d\n",
3355 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 }
3358 return status;
3359}
3360
Alan Stern624d6c02007-05-30 15:35:16 -04003361/*
Pratyush Ananda40178b2014-07-18 12:37:10 +05303362 * There are some SS USB devices which take longer time for link training.
3363 * XHCI specs 4.19.4 says that when Link training is successful, port
Al Cooper6b82b122015-12-02 14:55:07 -05003364 * sets CCS bit to 1. So if SW reads port status before successful link
Pratyush Ananda40178b2014-07-18 12:37:10 +05303365 * training, then it will not find device to be present.
3366 * USB Analyzer log with such buggy devices show that in some cases
3367 * device switch on the RX termination after long delay of host enabling
3368 * the VBUS. In few other cases it has been seen that device fails to
3369 * negotiate link training in first attempt. It has been
3370 * reported till now that few devices take as long as 2000 ms to train
3371 * the link after host enabling its VBUS and termination. Following
3372 * routine implements a 2000 ms timeout for link training. If in a case
3373 * link trains before timeout, loop will exit earlier.
3374 *
Al Cooper6b82b122015-12-02 14:55:07 -05003375 * There are also some 2.0 hard drive based devices and 3.0 thumb
3376 * drives that, when plugged into a 2.0 only port, take a long
3377 * time to set CCS after VBUS enable.
3378 *
Pratyush Ananda40178b2014-07-18 12:37:10 +05303379 * FIXME: If a device was connected before suspend, but was removed
3380 * while system was asleep, then the loop in the following routine will
3381 * only exit at timeout.
3382 *
Al Cooper6b82b122015-12-02 14:55:07 -05003383 * This routine should only be called when persist is enabled.
Pratyush Ananda40178b2014-07-18 12:37:10 +05303384 */
Al Cooper6b82b122015-12-02 14:55:07 -05003385static int wait_for_connected(struct usb_device *udev,
Pratyush Ananda40178b2014-07-18 12:37:10 +05303386 struct usb_hub *hub, int *port1,
3387 u16 *portchange, u16 *portstatus)
3388{
3389 int status = 0, delay_ms = 0;
3390
3391 while (delay_ms < 2000) {
3392 if (status || *portstatus & USB_PORT_STAT_CONNECTION)
3393 break;
Dominik Bozek399e5492018-04-13 10:42:31 -07003394 if (!port_is_power_on(hub, *portstatus)) {
3395 status = -ENODEV;
3396 break;
3397 }
Pratyush Ananda40178b2014-07-18 12:37:10 +05303398 msleep(20);
3399 delay_ms += 20;
3400 status = hub_port_status(hub, *port1, portstatus, portchange);
3401 }
Al Cooper6b82b122015-12-02 14:55:07 -05003402 dev_dbg(&udev->dev, "Waited %dms for CONNECT\n", delay_ms);
Pratyush Ananda40178b2014-07-18 12:37:10 +05303403 return status;
3404}
3405
3406/*
Alan Stern624d6c02007-05-30 15:35:16 -04003407 * usb_port_resume - re-activate a suspended usb device's upstream port
3408 * @udev: device to re-activate, not a root hub
3409 * Context: must be able to sleep; device not locked; pm locks held
3410 *
3411 * This will re-activate the suspended device, increasing power usage
3412 * while letting drivers communicate again with its endpoints.
3413 * USB resume explicitly guarantees that the power session between
3414 * the host and the device is the same as it was when the device
3415 * suspended.
3416 *
Alan Sternfeccc302008-03-03 15:15:59 -05003417 * If @udev->reset_resume is set then this routine won't check that the
3418 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04003419 * reset @udev. The end result is that a broken power session can be
3420 * recovered and @udev will appear to persist across a loss of VBUS power.
3421 *
3422 * For example, if a host controller doesn't maintain VBUS suspend current
3423 * during a system sleep or is reset when the system wakes up, all the USB
3424 * power sessions below it will be broken. This is especially troublesome
3425 * for mass-storage devices containing mounted filesystems, since the
3426 * device will appear to have disconnected and all the memory mappings
3427 * to it will be lost. Using the USB_PERSIST facility, the device can be
3428 * made to appear as if it had not disconnected.
3429 *
Ming Lei742120c2008-06-18 22:00:29 +08003430 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05003431 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04003432 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3433 * quite possible for a device to remain unaltered but its media to be
3434 * changed. If the user replaces a flash memory card while the system is
3435 * asleep, he will have only himself to blame when the filesystem on the
3436 * new card is corrupted and the system crashes.
3437 *
Alan Stern624d6c02007-05-30 15:35:16 -04003438 * Returns 0 on success, else negative errno.
3439 */
Alan Stern65bfd292008-11-25 16:39:18 -05003440int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441{
Lan Tianyuad493e52013-01-23 04:26:30 +08003442 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3443 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003444 int port1 = udev->portnum;
3445 int status;
3446 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003447
Dan Williamsd5c38342014-05-20 18:08:52 -07003448 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08003449 status = pm_runtime_get_sync(&port_dev->dev);
Lan Tianyuad493e52013-01-23 04:26:30 +08003450 if (status < 0) {
3451 dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
3452 status);
3453 return status;
3454 }
3455 }
3456
Dan Williams5c79a1e2014-05-20 18:09:26 -07003457 usb_lock_port(port_dev);
3458
Alan Sternd25450c2006-11-20 11:14:30 -05003459 /* Skip the initial Clear-Suspend step for a remote wakeup */
3460 status = hub_port_status(hub, port1, &portstatus, &portchange);
Ravi Chandra Sadineni00bc3482018-04-20 11:08:21 -07003461 if (status == 0 && !port_is_suspended(hub, portstatus)) {
3462 if (portchange & USB_PORT_STAT_C_SUSPEND)
3463 pm_wakeup_event(&udev->dev, 0);
Alan Sternd25450c2006-11-20 11:14:30 -05003464 goto SuspendCleared;
Ravi Chandra Sadineni00bc3482018-04-20 11:08:21 -07003465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003468 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003469 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
Andiry Xua7114232011-04-27 18:07:50 +08003470 else
Lan Tianyuad493e52013-01-23 04:26:30 +08003471 status = usb_clear_port_feature(hub->hdev,
Andiry Xua7114232011-04-27 18:07:50 +08003472 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003474 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 } else {
Felipe Balbibbc78c02015-02-13 15:38:33 -06003476 /* drive resume for USB_RESUME_TIMEOUT msec */
Alan Stern686314c2007-05-30 15:34:36 -04003477 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003478 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Hemant Kumar3aa7ecd2016-11-08 15:25:25 -08003479 if (!skip_extended_resume_delay)
3480 usleep_range(USB_RESUME_TIMEOUT * 1000,
3481 (USB_RESUME_TIMEOUT + 1) * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3484 * stop resume signaling. Then finish the resume
3485 * sequence.
3486 */
Alan Sternd25450c2006-11-20 11:14:30 -05003487 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003488
Alan Sternb01b03f2008-04-28 11:06:11 -04003489 /* TRSMRCY = 10 msec */
Hemant Kumar3f66f542016-11-08 15:42:13 -08003490 usleep_range(10000, 10500);
Alan Sternb01b03f2008-04-28 11:06:11 -04003491 }
Alan Stern54515fe2007-05-30 15:38:58 -04003492
Alan Sternb01b03f2008-04-28 11:06:11 -04003493 SuspendCleared:
3494 if (status == 0) {
Alan Sternbfd1e912012-10-19 11:03:39 -04003495 udev->port_is_suspended = 0;
Andiry Xua7114232011-04-27 18:07:50 +08003496 if (hub_is_superspeed(hub->hdev)) {
3497 if (portchange & USB_PORT_STAT_C_LINK_STATE)
Lan Tianyuad493e52013-01-23 04:26:30 +08003498 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003499 USB_PORT_FEAT_C_PORT_LINK_STATE);
3500 } else {
3501 if (portchange & USB_PORT_STAT_C_SUSPEND)
Lan Tianyuad493e52013-01-23 04:26:30 +08003502 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003503 USB_PORT_FEAT_C_SUSPEND);
3504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Al Cooper6b82b122015-12-02 14:55:07 -05003507 if (udev->persist_enabled)
3508 status = wait_for_connected(udev, hub, &port1, &portchange,
Pratyush Ananda40178b2014-07-18 12:37:10 +05303509 &portstatus);
3510
Alan Sternb01b03f2008-04-28 11:06:11 -04003511 status = check_port_resume_type(udev,
3512 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003513 if (status == 0)
3514 status = finish_port_resume(udev);
3515 if (status < 0) {
3516 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3517 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003518 } else {
3519 /* Try to enable USB2 hardware LPM */
Kai-Heng Feng9b916af2019-01-12 03:54:25 +08003520 usb_enable_usb2_hardware_lpm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003521
Sarah Sharpf74631e2012-06-25 12:08:08 -07003522 /* Try to enable USB3 LTM and LPM */
3523 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003524 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003525 }
Andiry Xu65580b432011-09-23 14:19:52 -07003526
Dan Williams5c79a1e2014-05-20 18:09:26 -07003527 usb_unlock_port(port_dev);
3528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 return status;
3530}
3531
Alan Stern0534d462010-01-08 12:56:30 -05003532int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533{
3534 int status = 0;
3535
Dan Williams5c79a1e2014-05-20 18:09:26 -07003536 usb_lock_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003538 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003539 status = usb_autoresume_device(udev);
3540 if (status == 0) {
3541 /* Let the drivers do their thing, then... */
3542 usb_autosuspend_device(udev);
3543 }
Alan Sternd25450c2006-11-20 11:14:30 -05003544 }
Dan Williams5c79a1e2014-05-20 18:09:26 -07003545 usb_unlock_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 return status;
3547}
3548
Dan Williams7e73be22014-05-20 18:09:31 -07003549/* Returns 1 if there was a remote wakeup and a connect status change. */
3550static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
3551 u16 portstatus, u16 portchange)
3552 __must_hold(&port_dev->status_lock)
3553{
3554 struct usb_port *port_dev = hub->ports[port - 1];
3555 struct usb_device *hdev;
3556 struct usb_device *udev;
3557 int connect_change = 0;
Lee, Chiasheng769ebef2019-06-20 10:56:04 +03003558 u16 link_state;
Dan Williams7e73be22014-05-20 18:09:31 -07003559 int ret;
3560
3561 hdev = hub->hdev;
3562 udev = port_dev->child;
3563 if (!hub_is_superspeed(hdev)) {
3564 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
3565 return 0;
3566 usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
3567 } else {
Lee, Chiasheng769ebef2019-06-20 10:56:04 +03003568 link_state = portstatus & USB_PORT_STAT_LINK_STATE;
Dan Williams7e73be22014-05-20 18:09:31 -07003569 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Lee, Chiasheng769ebef2019-06-20 10:56:04 +03003570 (link_state != USB_SS_PORT_LS_U0 &&
3571 link_state != USB_SS_PORT_LS_U1 &&
3572 link_state != USB_SS_PORT_LS_U2))
Dan Williams7e73be22014-05-20 18:09:31 -07003573 return 0;
3574 }
3575
3576 if (udev) {
3577 /* TRSMRCY = 10 msec */
3578 msleep(10);
3579
3580 usb_unlock_port(port_dev);
3581 ret = usb_remote_wakeup(udev);
3582 usb_lock_port(port_dev);
3583 if (ret < 0)
3584 connect_change = 1;
3585 } else {
3586 ret = -ENODEV;
3587 hub_port_disable(hub, port, 1);
3588 }
3589 dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
3590 return connect_change;
3591}
3592
Ming Leie6f30de2012-10-24 11:59:24 +08003593static int check_ports_changed(struct usb_hub *hub)
3594{
3595 int port1;
3596
3597 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3598 u16 portstatus, portchange;
3599 int status;
3600
3601 status = hub_port_status(hub, port1, &portstatus, &portchange);
3602 if (!status && portchange)
3603 return 1;
3604 }
3605 return 0;
3606}
3607
David Brownelldb690872005-09-13 19:56:33 -07003608static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
Chase Metzger17248562015-08-11 21:34:37 -07003610 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 struct usb_device *hdev = hub->hdev;
3612 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003613 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Alan Sterne583d9d2013-07-11 14:58:04 -04003615 /*
3616 * Warn if children aren't already suspended.
3617 * Also, add up the number of wakeup-enabled descendants.
3618 */
3619 hub->wakeup_enabled_descendants = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003621 struct usb_port *port_dev = hub->ports[port1 - 1];
3622 struct usb_device *udev = port_dev->child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
Alan Stern6840d252007-09-10 11:34:26 -04003624 if (udev && udev->can_submit) {
Dan Williamsb658b8f2014-06-17 16:16:22 -07003625 dev_warn(&port_dev->dev, "device %s not suspended yet\n",
3626 dev_name(&udev->dev));
Alan Stern5b1b0b82011-08-19 23:49:48 +02003627 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003628 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003629 }
Alan Sterne583d9d2013-07-11 14:58:04 -04003630 if (udev)
3631 hub->wakeup_enabled_descendants +=
3632 wakeup_enabled_descendants(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 }
Ming Leie6f30de2012-10-24 11:59:24 +08003634
3635 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3636 /* check if there are changes pending on hub ports */
3637 if (check_ports_changed(hub)) {
3638 if (PMSG_IS_AUTO(msg))
3639 return -EBUSY;
3640 pm_wakeup_event(&hdev->dev, 2000);
3641 }
3642 }
3643
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003644 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3645 /* Enable hub to send remote wakeup for all ports. */
3646 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3647 status = set_port_feature(hdev,
3648 port1 |
3649 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3650 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3651 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3652 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3653 }
3654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
Harvey Harrison441b62c2008-03-03 16:08:34 -08003656 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003657
Petr Mladek37ebb542014-09-19 17:32:23 +02003658 /* stop hub_wq and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003659 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003660 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661}
3662
3663static int hub_resume(struct usb_interface *intf)
3664{
Alan Stern5e6effa2008-03-03 15:15:51 -05003665 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
Alan Stern5e6effa2008-03-03 15:15:51 -05003667 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003668 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 return 0;
3670}
3671
Alan Sternb41a60e2007-05-30 15:39:33 -04003672static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003673{
Alan Sternb41a60e2007-05-30 15:39:33 -04003674 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003675
Alan Stern5e6effa2008-03-03 15:15:51 -05003676 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003677 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003678 return 0;
3679}
3680
Alan Stern54515fe2007-05-30 15:38:58 -04003681/**
3682 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3683 * @rhdev: struct usb_device for the root hub
3684 *
3685 * The USB host controller driver calls this function when its root hub
3686 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003687 * has been reset. The routine marks @rhdev as having lost power.
3688 * When the hub driver is resumed it will take notice and carry out
3689 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3690 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003691 */
3692void usb_root_hub_lost_power(struct usb_device *rhdev)
3693{
3694 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3695 rhdev->reset_resume = 1;
3696}
3697EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3698
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003699static const char * const usb3_lpm_names[] = {
3700 "U0",
3701 "U1",
3702 "U2",
3703 "U3",
3704};
3705
3706/*
3707 * Send a Set SEL control transfer to the device, prior to enabling
3708 * device-initiated U1 or U2. This lets the device know the exit latencies from
3709 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3710 * packet from the host.
3711 *
3712 * This function will fail if the SEL or PEL values for udev are greater than
3713 * the maximum allowed values for the link state to be enabled.
3714 */
3715static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3716{
3717 struct usb_set_sel_req *sel_values;
3718 unsigned long long u1_sel;
3719 unsigned long long u1_pel;
3720 unsigned long long u2_sel;
3721 unsigned long long u2_pel;
3722 int ret;
3723
Xenia Ragiadakou38d7f682013-09-04 17:24:45 +03003724 if (udev->state != USB_STATE_CONFIGURED)
3725 return 0;
3726
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003727 /* Convert SEL and PEL stored in ns to us */
3728 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3729 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3730 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3731 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3732
3733 /*
3734 * Make sure that the calculated SEL and PEL values for the link
3735 * state we're enabling aren't bigger than the max SEL/PEL
3736 * value that will fit in the SET SEL control transfer.
3737 * Otherwise the device would get an incorrect idea of the exit
3738 * latency for the link state, and could start a device-initiated
3739 * U1/U2 when the exit latencies are too high.
3740 */
3741 if ((state == USB3_LPM_U1 &&
3742 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3743 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3744 (state == USB3_LPM_U2 &&
3745 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3746 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003747 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 -07003748 usb3_lpm_names[state], u1_sel, u1_pel);
3749 return -EINVAL;
3750 }
3751
3752 /*
3753 * If we're enabling device-initiated LPM for one link state,
3754 * but the other link state has a too high SEL or PEL value,
3755 * just set those values to the max in the Set SEL request.
3756 */
3757 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3758 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3759
3760 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3761 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3762
3763 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3764 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3765
3766 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3767 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3768
3769 /*
3770 * usb_enable_lpm() can be called as part of a failed device reset,
3771 * which may be initiated by an error path of a mass storage driver.
3772 * Therefore, use GFP_NOIO.
3773 */
3774 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3775 if (!sel_values)
3776 return -ENOMEM;
3777
3778 sel_values->u1_sel = u1_sel;
3779 sel_values->u1_pel = u1_pel;
3780 sel_values->u2_sel = cpu_to_le16(u2_sel);
3781 sel_values->u2_pel = cpu_to_le16(u2_pel);
3782
3783 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3784 USB_REQ_SET_SEL,
3785 USB_RECIP_DEVICE,
3786 0, 0,
3787 sel_values, sizeof *(sel_values),
3788 USB_CTRL_SET_TIMEOUT);
3789 kfree(sel_values);
3790 return ret;
3791}
3792
3793/*
3794 * Enable or disable device-initiated U1 or U2 transitions.
3795 */
3796static int usb_set_device_initiated_lpm(struct usb_device *udev,
3797 enum usb3_link_state state, bool enable)
3798{
3799 int ret;
3800 int feature;
3801
3802 switch (state) {
3803 case USB3_LPM_U1:
3804 feature = USB_DEVICE_U1_ENABLE;
3805 break;
3806 case USB3_LPM_U2:
3807 feature = USB_DEVICE_U2_ENABLE;
3808 break;
3809 default:
3810 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3811 __func__, enable ? "enable" : "disable");
3812 return -EINVAL;
3813 }
3814
3815 if (udev->state != USB_STATE_CONFIGURED) {
3816 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3817 "for unconfigured device.\n",
3818 __func__, enable ? "enable" : "disable",
3819 usb3_lpm_names[state]);
3820 return 0;
3821 }
3822
3823 if (enable) {
3824 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003825 * Now send the control transfer to enable device-initiated LPM
3826 * for either U1 or U2.
3827 */
3828 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3829 USB_REQ_SET_FEATURE,
3830 USB_RECIP_DEVICE,
3831 feature,
3832 0, NULL, 0,
3833 USB_CTRL_SET_TIMEOUT);
3834 } else {
3835 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3836 USB_REQ_CLEAR_FEATURE,
3837 USB_RECIP_DEVICE,
3838 feature,
3839 0, NULL, 0,
3840 USB_CTRL_SET_TIMEOUT);
3841 }
3842 if (ret < 0) {
3843 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3844 enable ? "Enable" : "Disable",
3845 usb3_lpm_names[state]);
3846 return -EBUSY;
3847 }
3848 return 0;
3849}
3850
3851static int usb_set_lpm_timeout(struct usb_device *udev,
3852 enum usb3_link_state state, int timeout)
3853{
3854 int ret;
3855 int feature;
3856
3857 switch (state) {
3858 case USB3_LPM_U1:
3859 feature = USB_PORT_FEAT_U1_TIMEOUT;
3860 break;
3861 case USB3_LPM_U2:
3862 feature = USB_PORT_FEAT_U2_TIMEOUT;
3863 break;
3864 default:
3865 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3866 __func__);
3867 return -EINVAL;
3868 }
3869
3870 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3871 timeout != USB3_LPM_DEVICE_INITIATED) {
3872 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3873 "which is a reserved value.\n",
3874 usb3_lpm_names[state], timeout);
3875 return -EINVAL;
3876 }
3877
3878 ret = set_port_feature(udev->parent,
3879 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3880 feature);
3881 if (ret < 0) {
3882 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3883 "error code %i\n", usb3_lpm_names[state],
3884 timeout, ret);
3885 return -EBUSY;
3886 }
3887 if (state == USB3_LPM_U1)
3888 udev->u1_params.timeout = timeout;
3889 else
3890 udev->u2_params.timeout = timeout;
3891 return 0;
3892}
3893
3894/*
3895 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3896 * U1/U2 entry.
3897 *
3898 * We will attempt to enable U1 or U2, but there are no guarantees that the
3899 * control transfers to set the hub timeout or enable device-initiated U1/U2
3900 * will be successful.
3901 *
Thinh Nguyen790af992019-05-14 14:38:38 -07003902 * If the control transfer to enable device-initiated U1/U2 entry fails, then
3903 * hub-initiated U1/U2 will be disabled.
3904 *
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003905 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3906 * driver know about it. If that call fails, it should be harmless, and just
3907 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3908 */
3909static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3910 enum usb3_link_state state)
3911{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003912 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003913 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3914 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3915
3916 /* If the device says it doesn't have *any* exit latency to come out of
3917 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3918 * state.
3919 */
3920 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3921 (state == USB3_LPM_U2 && u2_mel == 0))
3922 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003923
Sarah Sharp65a95b72012-10-05 10:32:07 -07003924 /*
3925 * First, let the device know about the exit latencies
3926 * associated with the link state we're about to enable.
3927 */
3928 ret = usb_req_set_sel(udev, state);
3929 if (ret < 0) {
3930 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3931 usb3_lpm_names[state]);
3932 return;
3933 }
3934
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003935 /* We allow the host controller to set the U1/U2 timeout internally
3936 * first, so that it can change its schedule to account for the
3937 * additional latency to send data to a device in a lower power
3938 * link state.
3939 */
3940 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3941
3942 /* xHCI host controller doesn't want to enable this LPM state. */
3943 if (timeout == 0)
3944 return;
3945
3946 if (timeout < 0) {
3947 dev_warn(&udev->dev, "Could not enable %s link state, "
3948 "xHCI error %i.\n", usb3_lpm_names[state],
3949 timeout);
3950 return;
3951 }
3952
Lu Baolubf5ce5b2015-11-14 16:26:32 +08003953 if (usb_set_lpm_timeout(udev, state, timeout)) {
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003954 /* If we can't set the parent hub U1/U2 timeout,
3955 * device-initiated LPM won't be allowed either, so let the xHCI
3956 * host know that this link state won't be enabled.
3957 */
3958 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
Thinh Nguyen790af992019-05-14 14:38:38 -07003959 return;
3960 }
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003961
Thinh Nguyen790af992019-05-14 14:38:38 -07003962 /* Only a configured device will accept the Set Feature
3963 * U1/U2_ENABLE
3964 */
3965 if (udev->actconfig &&
3966 usb_set_device_initiated_lpm(udev, state, true) == 0) {
Lu Baolubf5ce5b2015-11-14 16:26:32 +08003967 if (state == USB3_LPM_U1)
3968 udev->usb3_lpm_u1_enabled = 1;
3969 else if (state == USB3_LPM_U2)
3970 udev->usb3_lpm_u2_enabled = 1;
Thinh Nguyen790af992019-05-14 14:38:38 -07003971 } else {
3972 /* Don't request U1/U2 entry if the device
3973 * cannot transition to U1/U2.
3974 */
3975 usb_set_lpm_timeout(udev, state, 0);
3976 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
Lu Baolubf5ce5b2015-11-14 16:26:32 +08003977 }
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003978}
3979
3980/*
3981 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3982 * U1/U2 entry.
3983 *
3984 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3985 * If zero is returned, the parent will not allow the link to go into U1/U2.
3986 *
3987 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3988 * it won't have an effect on the bus link state because the parent hub will
3989 * still disallow device-initiated U1/U2 entry.
3990 *
3991 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3992 * possible. The result will be slightly more bus bandwidth will be taken up
3993 * (to account for U1/U2 exit latency), but it should be harmless.
3994 */
3995static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3996 enum usb3_link_state state)
3997{
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003998 switch (state) {
3999 case USB3_LPM_U1:
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004000 case USB3_LPM_U2:
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004001 break;
4002 default:
4003 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
4004 __func__);
4005 return -EINVAL;
4006 }
4007
4008 if (usb_set_lpm_timeout(udev, state, 0))
4009 return -EBUSY;
4010
4011 usb_set_device_initiated_lpm(udev, state, false);
4012
4013 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
4014 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
4015 "bus schedule bandwidth may be impacted.\n",
4016 usb3_lpm_names[state]);
Lu Baolubf5ce5b2015-11-14 16:26:32 +08004017
4018 /* As soon as usb_set_lpm_timeout(0) return 0, hub initiated LPM
4019 * is disabled. Hub will disallows link to enter U1/U2 as well,
4020 * even device is initiating LPM. Hence LPM is disabled if hub LPM
4021 * timeout set to 0, no matter device-initiated LPM is disabled or
4022 * not.
4023 */
4024 if (state == USB3_LPM_U1)
4025 udev->usb3_lpm_u1_enabled = 0;
4026 else if (state == USB3_LPM_U2)
4027 udev->usb3_lpm_u2_enabled = 0;
4028
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004029 return 0;
4030}
4031
4032/*
4033 * Disable hub-initiated and device-initiated U1 and U2 entry.
4034 * Caller must own the bandwidth_mutex.
4035 *
4036 * This will call usb_enable_lpm() on failure, which will decrement
4037 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
4038 */
4039int usb_disable_lpm(struct usb_device *udev)
4040{
4041 struct usb_hcd *hcd;
4042
4043 if (!udev || !udev->parent ||
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004044 udev->speed < USB_SPEED_SUPER ||
Pratyush Anand51df62f2014-07-04 17:01:27 +03004045 !udev->lpm_capable ||
4046 udev->state < USB_STATE_DEFAULT)
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004047 return 0;
4048
4049 hcd = bus_to_hcd(udev->bus);
4050 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
4051 return 0;
4052
4053 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03004054 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004055 return 0;
4056
4057 /* If LPM is enabled, attempt to disable it. */
4058 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
4059 goto enable_lpm;
4060 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
4061 goto enable_lpm;
4062
4063 return 0;
4064
4065enable_lpm:
4066 usb_enable_lpm(udev);
4067 return -EBUSY;
4068}
4069EXPORT_SYMBOL_GPL(usb_disable_lpm);
4070
4071/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
4072int usb_unlocked_disable_lpm(struct usb_device *udev)
4073{
4074 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4075 int ret;
4076
4077 if (!hcd)
4078 return -EINVAL;
4079
4080 mutex_lock(hcd->bandwidth_mutex);
4081 ret = usb_disable_lpm(udev);
4082 mutex_unlock(hcd->bandwidth_mutex);
4083
4084 return ret;
4085}
4086EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
4087
4088/*
4089 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
4090 * xHCI host policy may prevent U1 or U2 from being enabled.
4091 *
4092 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
4093 * until the lpm_disable_count drops to zero. Caller must own the
4094 * bandwidth_mutex.
4095 */
4096void usb_enable_lpm(struct usb_device *udev)
4097{
4098 struct usb_hcd *hcd;
Lu Baolu513072d2015-11-14 16:26:33 +08004099 struct usb_hub *hub;
4100 struct usb_port *port_dev;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004101
4102 if (!udev || !udev->parent ||
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004103 udev->speed < USB_SPEED_SUPER ||
Pratyush Anand51df62f2014-07-04 17:01:27 +03004104 !udev->lpm_capable ||
4105 udev->state < USB_STATE_DEFAULT)
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004106 return;
4107
4108 udev->lpm_disable_count--;
4109 hcd = bus_to_hcd(udev->bus);
4110 /* Double check that we can both enable and disable LPM.
4111 * Device must be configured to accept set feature U1/U2 timeout.
4112 */
4113 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
4114 !hcd->driver->disable_usb3_lpm_timeout)
4115 return;
4116
4117 if (udev->lpm_disable_count > 0)
4118 return;
4119
Lu Baolu513072d2015-11-14 16:26:33 +08004120 hub = usb_hub_to_struct_hub(udev->parent);
4121 if (!hub)
4122 return;
4123
4124 port_dev = hub->ports[udev->portnum - 1];
4125
4126 if (port_dev->usb3_lpm_u1_permit)
4127 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
4128
4129 if (port_dev->usb3_lpm_u2_permit)
4130 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004131}
4132EXPORT_SYMBOL_GPL(usb_enable_lpm);
4133
4134/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
4135void usb_unlocked_enable_lpm(struct usb_device *udev)
4136{
4137 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4138
4139 if (!hcd)
4140 return;
4141
4142 mutex_lock(hcd->bandwidth_mutex);
4143 usb_enable_lpm(udev);
4144 mutex_unlock(hcd->bandwidth_mutex);
4145}
4146EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
4147
Mathias Nyman32a35352016-11-17 11:14:14 +02004148/* usb3 devices use U3 for disabled, make sure remote wakeup is disabled */
4149static void hub_usb3_port_prepare_disable(struct usb_hub *hub,
4150 struct usb_port *port_dev)
4151{
4152 struct usb_device *udev = port_dev->child;
4153 int ret;
4154
4155 if (udev && udev->port_is_suspended && udev->do_remote_wakeup) {
4156 ret = hub_set_port_link_state(hub, port_dev->portnum,
4157 USB_SS_PORT_LS_U0);
4158 if (!ret) {
4159 msleep(USB_RESUME_TIMEOUT);
4160 ret = usb_disable_remote_wakeup(udev);
4161 }
4162 if (ret)
4163 dev_warn(&udev->dev,
4164 "Port disable: can't disable remote wake\n");
4165 udev->do_remote_wakeup = 0;
4166 }
4167}
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004168
Alan Sternd388dab2006-07-01 22:14:24 -04004169#else /* CONFIG_PM */
4170
Alan Sternf07600c2007-05-30 15:38:16 -04004171#define hub_suspend NULL
4172#define hub_resume NULL
4173#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004174
Mathias Nyman32a35352016-11-17 11:14:14 +02004175static inline void hub_usb3_port_prepare_disable(struct usb_hub *hub,
4176 struct usb_port *port_dev) { }
4177
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004178int usb_disable_lpm(struct usb_device *udev)
4179{
4180 return 0;
4181}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004182EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004183
4184void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004185EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004186
4187int usb_unlocked_disable_lpm(struct usb_device *udev)
4188{
4189 return 0;
4190}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004191EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004192
4193void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004194EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07004195
4196int usb_disable_ltm(struct usb_device *udev)
4197{
4198 return 0;
4199}
4200EXPORT_SYMBOL_GPL(usb_disable_ltm);
4201
4202void usb_enable_ltm(struct usb_device *udev) { }
4203EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternc4b51a42013-07-11 14:58:23 -04004204
Stephen Rothwell4a95b1f2014-05-29 18:55:06 +10004205static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
4206 u16 portstatus, u16 portchange)
4207{
4208 return 0;
4209}
4210
Alan Sternc4b51a42013-07-11 14:58:23 -04004211#endif /* CONFIG_PM */
Alan Sternd388dab2006-07-01 22:14:24 -04004212
Geert Uytterhoeven8ecf70f2016-12-14 15:37:30 +01004213/*
4214 * USB-3 does not have a similar link state as USB-2 that will avoid negotiating
4215 * a connection with a plugged-in cable but will signal the host when the cable
4216 * is unplugged. Disable remote wake and set link state to U3 for USB-3 devices
4217 */
4218static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
4219{
4220 struct usb_port *port_dev = hub->ports[port1 - 1];
4221 struct usb_device *hdev = hub->hdev;
4222 int ret = 0;
4223
4224 if (!hub->error) {
4225 if (hub_is_superspeed(hub->hdev)) {
4226 hub_usb3_port_prepare_disable(hub, port_dev);
4227 ret = hub_set_port_link_state(hub, port_dev->portnum,
4228 USB_SS_PORT_LS_U3);
4229 } else {
4230 ret = usb_clear_port_feature(hdev, port1,
4231 USB_PORT_FEAT_ENABLE);
4232 }
4233 }
4234 if (port_dev->child && set_state)
4235 usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED);
4236 if (ret && ret != -ENODEV)
4237 dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret);
4238 return ret;
4239}
4240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
4242/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
4243 *
4244 * Between connect detection and reset signaling there must be a delay
4245 * of 100ms at least for debounce and power-settling. The corresponding
4246 * timer shall restart whenever the downstream port detects a disconnect.
Matthias Beyer469271f2013-10-10 23:41:27 +02004247 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 * Apparently there are some bluetooth and irda-dongles and a number of
4249 * low-speed devices for which this debounce period may last over a second.
4250 * Not covered by the spec - but easy to deal with.
4251 *
4252 * This implementation uses a 1500ms total debounce timeout; if the
4253 * connection isn't stable by then it returns -ETIMEDOUT. It checks
4254 * every 25ms for transient disconnects. When the port status has been
4255 * unchanged for 100ms it returns the port status.
4256 */
Lan Tianyuad493e52013-01-23 04:26:30 +08004257int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258{
4259 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 u16 portchange, portstatus;
4261 unsigned connection = 0xffff;
Dan Williamsd99f6b42014-05-20 18:08:17 -07004262 int total_time, stable_time = 0;
4263 struct usb_port *port_dev = hub->ports[port1 - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
4265 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
4266 ret = hub_port_status(hub, port1, &portstatus, &portchange);
4267 if (ret < 0)
4268 return ret;
4269
4270 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
4271 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004272 if (!must_be_connected ||
4273 (connection == USB_PORT_STAT_CONNECTION))
4274 stable_time += HUB_DEBOUNCE_STEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 if (stable_time >= HUB_DEBOUNCE_STABLE)
4276 break;
4277 } else {
4278 stable_time = 0;
4279 connection = portstatus & USB_PORT_STAT_CONNECTION;
4280 }
4281
4282 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004283 usb_clear_port_feature(hub->hdev, port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 USB_PORT_FEAT_C_CONNECTION);
4285 }
4286
4287 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
4288 break;
4289 msleep(HUB_DEBOUNCE_STEP);
4290 }
4291
Dan Williamsd99f6b42014-05-20 18:08:17 -07004292 dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n",
4293 total_time, stable_time, portstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
4295 if (stable_time < HUB_DEBOUNCE_STABLE)
4296 return -ETIMEDOUT;
4297 return portstatus;
4298}
4299
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004300void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301{
Alan Sternddeac4e72009-01-15 17:03:33 -05004302 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
4303 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05004304 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004306EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
4308#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
4309#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
4310
Alan Stern4326ed02007-07-30 17:08:43 -04004311static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312{
4313 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07004314 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Sarah Sharpc6515272009-04-27 19:57:26 -07004316 /*
4317 * The host controller will choose the device address,
4318 * instead of the core having chosen it earlier
4319 */
4320 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 return -EINVAL;
4322 if (udev->state == USB_STATE_ADDRESS)
4323 return 0;
4324 if (udev->state != USB_STATE_DEFAULT)
4325 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07004326 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07004327 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004328 else
Sarah Sharpc6515272009-04-27 19:57:26 -07004329 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
4330 USB_REQ_SET_ADDRESS, 0, devnum, 0,
4331 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05004333 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07004334 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004336 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 }
4338 return retval;
4339}
4340
Mathias Nyman890dae82013-09-30 17:26:31 +03004341/*
4342 * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM
4343 * when they're plugged into a USB 2.0 port, but they don't work when LPM is
4344 * enabled.
4345 *
4346 * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the
4347 * device says it supports the new USB 2.0 Link PM errata by setting the BESL
4348 * support bit in the BOS descriptor.
4349 */
4350static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev)
4351{
Dan Williamsd99f6b42014-05-20 18:08:17 -07004352 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
4353 int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
Mathias Nyman890dae82013-09-30 17:26:31 +03004354
Guenter Roeck4e615882017-03-08 10:19:36 -08004355 if (!udev->usb2_hw_lpm_capable || !udev->bos)
Mathias Nyman890dae82013-09-30 17:26:31 +03004356 return;
4357
Dan Williamsd99f6b42014-05-20 18:08:17 -07004358 if (hub)
4359 connect_type = hub->ports[udev->portnum - 1]->connect_type;
Mathias Nyman890dae82013-09-30 17:26:31 +03004360
Bjørn Mork23c05822014-02-27 14:23:55 +01004361 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) ||
Mathias Nyman890dae82013-09-30 17:26:31 +03004362 connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
4363 udev->usb2_hw_lpm_allowed = 1;
Kai-Heng Fengc73982b2019-01-12 03:54:24 +08004364 usb_enable_usb2_hardware_lpm(udev);
Mathias Nyman890dae82013-09-30 17:26:31 +03004365 }
4366}
4367
Dan Williams48fc7db2013-12-05 17:07:27 -08004368static int hub_enable_device(struct usb_device *udev)
4369{
4370 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4371
4372 if (!hcd->driver->enable_device)
4373 return 0;
4374 if (udev->state == USB_STATE_ADDRESS)
4375 return 0;
4376 if (udev->state != USB_STATE_DEFAULT)
4377 return -EINVAL;
4378
4379 return hcd->driver->enable_device(hcd, udev);
4380}
4381
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382/* Reset device, (re)assign address, get device descriptor.
4383 * Device connection must be stable, no more debouncing needed.
4384 * Returns device in USB_STATE_ADDRESS, except on error.
4385 *
4386 * If this is called for an already-existing device (as part of
Dan Williams5c79a1e2014-05-20 18:09:26 -07004387 * usb_reset_and_verify_device), the caller must own the device lock and
4388 * the port lock. For a newly detected device that is not accessible
4389 * through any global pointers, it's not necessary to lock the device,
4390 * but it is still necessary to lock the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 */
4392static int
Chase Metzger039b3302015-08-18 20:36:58 -07004393hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 int retry_counter)
4395{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07004397 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Oliver Neukum0d5ce772016-02-17 11:52:43 +01004398 int retries, operations, retval, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 unsigned delay = HUB_SHORT_RESET_TIME;
4400 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004401 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04004402 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
4404 /* root hub ports have a slightly longer reset period
4405 * (from USB 2.0 spec, section 7.1.7.5)
4406 */
4407 if (!hdev->parent) {
4408 delay = HUB_ROOT_RESET_TIME;
4409 if (port1 == hdev->bus->otg_port)
4410 hdev->bus->b_hnp_enable = 0;
4411 }
4412
4413 /* Some low speed devices have problems with the quick delay, so */
4414 /* be a bit pessimistic with those devices. RHbug #23670 */
4415 if (oldspeed == USB_SPEED_LOW)
4416 delay = HUB_LONG_RESET_TIME;
4417
Chris Bainbridgefeb26ac2016-04-25 13:48:38 +01004418 mutex_lock(hcd->address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Luben Tuikov07194ab2011-02-11 11:33:10 -08004420 /* Reset the device; full speed may morph to high speed */
4421 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07004422 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08004423 if (retval < 0) /* error or disconnect */
4424 goto fail;
4425 /* success, speed is known */
4426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 retval = -ENODEV;
4428
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004429 /* Don't allow speed changes at reset, except usb 3.0 to faster */
4430 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed &&
4431 !(oldspeed == USB_SPEED_SUPER && udev->speed > oldspeed)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 dev_dbg(&udev->dev, "device reset changed speed!\n");
4433 goto fail;
4434 }
4435 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04004436
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
4438 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004439 * For Wireless USB devices, ep0 max packet is always 512 (tho
4440 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 */
4442 switch (udev->speed) {
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004443 case USB_SPEED_SUPER_PLUS:
Sarah Sharp6b403b02009-04-27 19:54:10 -07004444 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08004445 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004446 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004447 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004449 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 break;
4451 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
4452 /* to determine the ep0 maxpacket size, try to read
4453 * the device descriptor to get bMaxPacketSize0 and
4454 * then correct our initial guess.
4455 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004456 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 break;
4458 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004459 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 break;
4461 default:
4462 goto fail;
4463 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004464
4465 if (udev->speed == USB_SPEED_WIRELESS)
4466 speed = "variable speed Wireless";
4467 else
4468 speed = usb_speed_string(udev->speed);
4469
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004470 if (udev->speed < USB_SPEED_SUPER)
Sarah Sharpc6515272009-04-27 19:57:26 -07004471 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004472 "%s %s USB device number %d using %s\n",
4473 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05004474 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
4476 /* Set up TT records, if needed */
4477 if (hdev->tt) {
4478 udev->tt = hdev->tt;
4479 udev->ttport = hdev->ttport;
4480 } else if (udev->speed != USB_SPEED_HIGH
4481 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05004482 if (!hub->tt.hub) {
4483 dev_err(&udev->dev, "parent hub has no TT\n");
4484 retval = -EINVAL;
4485 goto fail;
4486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 udev->tt = &hub->tt;
4488 udev->ttport = port1;
4489 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004490
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
4492 * Because device hardware and firmware is sometimes buggy in
4493 * this area, and this is how Linux has done it for ages.
4494 * Change it cautiously.
4495 *
Dan Williams48fc7db2013-12-05 17:07:27 -08004496 * NOTE: If use_new_scheme() is true we will start by issuing
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
4498 * so it may help with some non-standards-compliant devices.
4499 * Otherwise we start with SET_ADDRESS and then try to read the
4500 * first 8 bytes of the device descriptor to get the ep0 maxpacket
4501 * value.
4502 */
Oliver Neukum0d5ce772016-02-17 11:52:43 +01004503 for (retries = 0; retries < GET_DESCRIPTOR_TRIES; (++retries, msleep(100))) {
Dan Williams48fc7db2013-12-05 17:07:27 -08004504 bool did_new_scheme = false;
4505
4506 if (use_new_scheme(udev, retry_counter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 struct usb_device_descriptor *buf;
4508 int r = 0;
4509
Dan Williams48fc7db2013-12-05 17:07:27 -08004510 did_new_scheme = true;
4511 retval = hub_enable_device(udev);
Oliver Neukum938569e2014-02-27 10:57:10 +01004512 if (retval < 0) {
4513 dev_err(&udev->dev,
4514 "hub failed to enable device, error %d\n",
4515 retval);
Dan Williams48fc7db2013-12-05 17:07:27 -08004516 goto fail;
Oliver Neukum938569e2014-02-27 10:57:10 +01004517 }
Dan Williams48fc7db2013-12-05 17:07:27 -08004518
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519#define GET_DESCRIPTOR_BUFSIZE 64
4520 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
4521 if (!buf) {
4522 retval = -ENOMEM;
4523 continue;
4524 }
4525
Alan Sternb89ee192007-05-11 10:19:04 -04004526 /* Retry on all errors; some devices are flakey.
4527 * 255 is for WUSB devices, we actually need to use
4528 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 */
Oliver Neukum0d5ce772016-02-17 11:52:43 +01004530 for (operations = 0; operations < 3; ++operations) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 buf->bMaxPacketSize0 = 0;
4532 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
4533 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
4534 USB_DT_DEVICE << 8, 0,
4535 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02004536 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004538 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 if (buf->bDescriptorType ==
4540 USB_DT_DEVICE) {
4541 r = 0;
4542 break;
4543 }
4544 /* FALL THROUGH */
4545 default:
4546 if (r == 0)
4547 r = -EPROTO;
4548 break;
4549 }
Oliver Neukum264904c2016-02-10 11:33:18 +01004550 /*
4551 * Some devices time out if they are powered on
4552 * when already connected. They need a second
4553 * reset. But only on the first attempt,
4554 * lest we get into a time out/reset loop
4555 */
Maxim Moseychuk55365ad2018-01-04 21:43:03 +03004556 if (r == 0 || (r == -ETIMEDOUT &&
4557 retries == 0 &&
4558 udev->speed > USB_SPEED_FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 break;
4560 }
4561 udev->descriptor.bMaxPacketSize0 =
4562 buf->bMaxPacketSize0;
4563 kfree(buf);
4564
Andiry Xu75d7cf72011-09-13 16:41:11 -07004565 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 if (retval < 0) /* error or disconnect */
4567 goto fail;
4568 if (oldspeed != udev->speed) {
4569 dev_dbg(&udev->dev,
4570 "device reset changed speed!\n");
4571 retval = -ENODEV;
4572 goto fail;
4573 }
4574 if (r) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004575 if (r != -ENODEV)
4576 dev_err(&udev->dev, "device descriptor read/64, error %d\n",
4577 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 retval = -EMSGSIZE;
4579 continue;
4580 }
4581#undef GET_DESCRIPTOR_BUFSIZE
4582 }
4583
Matthias Beyer469271f2013-10-10 23:41:27 +02004584 /*
4585 * If device is WUSB, we already assigned an
4586 * unauthorized address in the Connect Ack sequence;
4587 * authorization will assign the final address.
4588 */
Sarah Sharpc6515272009-04-27 19:57:26 -07004589 if (udev->wusb == 0) {
Oliver Neukum0d5ce772016-02-17 11:52:43 +01004590 for (operations = 0; operations < SET_ADDRESS_TRIES; ++operations) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004591 retval = hub_set_address(udev, devnum);
4592 if (retval >= 0)
4593 break;
4594 msleep(200);
4595 }
4596 if (retval < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004597 if (retval != -ENODEV)
4598 dev_err(&udev->dev, "device not accepting address %d, error %d\n",
4599 devnum, retval);
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004600 goto fail;
4601 }
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004602 if (udev->speed >= USB_SPEED_SUPER) {
Sarah Sharpc6515272009-04-27 19:57:26 -07004603 devnum = udev->devnum;
4604 dev_info(&udev->dev,
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004605 "%s SuperSpeed%s USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07004606 (udev->config) ? "reset" : "new",
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004607 (udev->speed == USB_SPEED_SUPER_PLUS) ? "Plus" : "",
Alan Stern3b29b682011-02-22 09:53:41 -05004608 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07004609 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004610
4611 /* cope with hardware quirkiness:
4612 * - let SET_ADDRESS settle, some device hardware wants it
4613 * - read ep0 maxpacket even for high and low speed,
4614 */
4615 msleep(10);
Dan Williams48fc7db2013-12-05 17:07:27 -08004616 /* use_new_scheme() checks the speed which may have
4617 * changed since the initial look so we cache the result
4618 * in did_new_scheme
4619 */
4620 if (did_new_scheme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 break;
Matthias Beyer469271f2013-10-10 23:41:27 +02004622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
4624 retval = usb_get_device_descriptor(udev, 8);
4625 if (retval < 8) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004626 if (retval != -ENODEV)
4627 dev_err(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004628 "device descriptor read/8, error %d\n",
4629 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 if (retval >= 0)
4631 retval = -EMSGSIZE;
4632 } else {
4633 retval = 0;
4634 break;
4635 }
4636 }
4637 if (retval)
4638 goto fail;
4639
Elric Fud8aec3d2012-03-26 21:16:02 +08004640 /*
4641 * Some superspeed devices have finished the link training process
4642 * and attached to a superspeed hub port, but the device descriptor
4643 * got from those devices show they aren't superspeed devices. Warm
4644 * reset the port attached by the devices can fix them.
4645 */
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004646 if ((udev->speed >= USB_SPEED_SUPER) &&
Elric Fud8aec3d2012-03-26 21:16:02 +08004647 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4648 dev_err(&udev->dev, "got a wrong device descriptor, "
4649 "warm reset device\n");
4650 hub_port_reset(hub, port1, udev,
4651 HUB_BH_RESET_TIME, true);
4652 retval = -EINVAL;
4653 goto fail;
4654 }
4655
Sarah Sharp6b403b02009-04-27 19:54:10 -07004656 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004657 udev->speed >= USB_SPEED_SUPER)
Sarah Sharp6b403b02009-04-27 19:54:10 -07004658 i = 512;
4659 else
4660 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07004661 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04004662 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04004664 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 retval = -EMSGSIZE;
4666 goto fail;
4667 }
Alan Stern56626a72010-10-14 15:25:21 -04004668 if (udev->speed == USB_SPEED_FULL)
4669 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4670 else
4671 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004673 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004675
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4677 if (retval < (signed)sizeof(udev->descriptor)) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004678 if (retval != -ENODEV)
4679 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4680 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 if (retval >= 0)
4682 retval = -ENOMSG;
4683 goto fail;
4684 }
4685
Alan Sternad87e032015-12-10 15:27:21 -05004686 usb_detect_quirks(udev);
4687
Andiry Xu1ff4df52011-09-23 14:19:48 -07004688 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4689 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004690 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004691 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004692 usb_set_lpm_parameters(udev);
4693 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004694 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004695
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004697 /* notify HCD that we have a device connected and addressed */
4698 if (hcd->driver->update_device)
4699 hcd->driver->update_device(hcd, udev);
Mathias Nyman890dae82013-09-30 17:26:31 +03004700 hub_set_initial_usb2_lpm_policy(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004702 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004704 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004705 }
Chris Bainbridgefeb26ac2016-04-25 13:48:38 +01004706 mutex_unlock(hcd->address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 return retval;
4708}
4709
4710static void
Chase Metzger039b3302015-08-18 20:36:58 -07004711check_highspeed(struct usb_hub *hub, struct usb_device *udev, int port1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712{
4713 struct usb_qualifier_descriptor *qual;
4714 int status;
4715
Johan Hovold2a159382014-08-25 17:51:26 +02004716 if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER)
4717 return;
4718
Chase Metzger039b3302015-08-18 20:36:58 -07004719 qual = kmalloc(sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 if (qual == NULL)
4721 return;
4722
Chase Metzger039b3302015-08-18 20:36:58 -07004723 status = usb_get_descriptor(udev, USB_DT_DEVICE_QUALIFIER, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 qual, sizeof *qual);
4725 if (status == sizeof *qual) {
4726 dev_info(&udev->dev, "not running at top speed; "
4727 "connect to a high speed hub\n");
4728 /* hub LEDs are probably harder to miss than syslog */
4729 if (hub->has_indicators) {
4730 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08004731 queue_delayed_work(system_power_efficient_wq,
4732 &hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 }
4734 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004735 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736}
4737
4738static unsigned
Chase Metzger039b3302015-08-18 20:36:58 -07004739hub_power_remaining(struct usb_hub *hub)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740{
4741 struct usb_device *hdev = hub->hdev;
4742 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004743 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
Alan Stern55c52712005-11-23 12:03:12 -05004745 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 return 0;
4747
Alan Stern55c52712005-11-23 12:03:12 -05004748 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4749 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004750 struct usb_port *port_dev = hub->ports[port1 - 1];
4751 struct usb_device *udev = port_dev->child;
4752 unsigned unit_load;
4753 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
4755 if (!udev)
4756 continue;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004757 if (hub_is_superspeed(udev))
4758 unit_load = 150;
4759 else
4760 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004762 /*
4763 * Unconfigured devices may not use more than one unit load,
4764 * or 8mA for OTG ports
4765 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 if (udev->actconfig)
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +01004767 delta = usb_get_max_power(udev, udev->actconfig);
Alan Stern55c52712005-11-23 12:03:12 -05004768 else if (port1 != udev->bus->otg_port || hdev->parent)
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004769 delta = unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 else
Alan Stern55c52712005-11-23 12:03:12 -05004771 delta = 8;
4772 if (delta > hub->mA_per_port)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004773 dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n",
4774 delta, hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 remaining -= delta;
4776 }
4777 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004778 dev_warn(hub->intfdev, "%dmA over power budget!\n",
Matthias Beyer469271f2013-10-10 23:41:27 +02004779 -remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 remaining = 0;
4781 }
4782 return remaining;
4783}
4784
Dan Williamsaf376a42014-05-20 18:09:15 -07004785static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
4786 u16 portchange)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787{
Hemant Kumarcad6bda2017-11-20 20:07:16 -08004788 int ret, status = -ENODEV;
Alan Stern7c2beb12017-08-01 10:41:56 -04004789 int i;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004790 unsigned unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 struct usb_device *hdev = hub->hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004793 struct usb_port *port_dev = hub->ports[port1 - 1];
Dan Williamsaf376a42014-05-20 18:09:15 -07004794 struct usb_device *udev = port_dev->child;
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004795 static int unreliable_port = -1;
Hemant Kumarcad6bda2017-11-20 20:07:16 -08004796 enum usb_device_speed dev_speed = USB_SPEED_UNKNOWN;
Alan Stern8808f002008-04-28 11:06:55 -04004797
Alan Stern24618b02008-04-28 11:06:28 -04004798 /* Disconnect any existing devices under this port */
Peter Chenb76baa82012-11-09 09:44:43 +08004799 if (udev) {
Peter Chenb2108f12014-11-04 11:14:31 +08004800 if (hcd->usb_phy && !hdev->parent)
Antoine Tenart3d46e732014-09-24 23:05:50 +04004801 usb_phy_notify_disconnect(hcd->usb_phy, udev->speed);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004802 usb_disconnect(&port_dev->child);
Peter Chenb76baa82012-11-09 09:44:43 +08004803 }
Alan Stern24618b02008-04-28 11:06:28 -04004804
Alan Stern253e0572009-10-27 15:20:13 -04004805 /* We can forget about a "removed" device when there's a physical
4806 * disconnect or the connect status changes.
4807 */
4808 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4809 (portchange & USB_PORT_STAT_C_CONNECTION))
4810 clear_bit(port1, hub->removed_bits);
4811
Alan Stern5257d972008-09-22 14:43:08 -04004812 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4813 USB_PORT_STAT_C_ENABLE)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004814 status = hub_port_debounce_be_stable(hub, port1);
Alan Stern5257d972008-09-22 14:43:08 -04004815 if (status < 0) {
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004816 if (status != -ENODEV &&
4817 port1 != unreliable_port &&
4818 printk_ratelimit())
Takashi Iwaidd5f5002014-08-19 17:37:55 +02004819 dev_err(&port_dev->dev, "connect-debounce failed\n");
Alan Stern5257d972008-09-22 14:43:08 -04004820 portstatus &= ~USB_PORT_STAT_CONNECTION;
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004821 unreliable_port = port1;
Alan Stern5257d972008-09-22 14:43:08 -04004822 } else {
4823 portstatus = status;
4824 }
4825 }
4826
Alan Stern253e0572009-10-27 15:20:13 -04004827 /* Return now if debouncing failed or nothing is connected or
4828 * the device was "removed".
4829 */
4830 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4831 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832
Deepak Dasfbaecff2015-01-21 23:39:58 +05304833 /*
4834 * maybe switch power back on (e.g. root hub was reset)
4835 * but only if the port isn't owned by someone else.
4836 */
Dan Williams9262c192014-05-20 18:08:12 -07004837 if (hub_is_port_power_switchable(hub)
Deepak Dasfbaecff2015-01-21 23:39:58 +05304838 && !port_is_power_on(hub, portstatus)
4839 && !port_dev->port_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004841
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 if (portstatus & USB_PORT_STAT_ENABLE)
Matthias Beyer469271f2013-10-10 23:41:27 +02004843 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 return;
4845 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004846 if (hub_is_superspeed(hub->hdev))
4847 unit_load = 150;
4848 else
4849 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
Hemant Kumarcad6bda2017-11-20 20:07:16 -08004851retry_enum:
Alan Sterne9e88fb2013-03-27 16:14:01 -04004852 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
4855 /* reallocate for each attempt, since references
4856 * to the previous one can escape in various ways
4857 */
4858 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4859 if (!udev) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004860 dev_err(&port_dev->dev,
4861 "couldn't allocate usb_device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 goto done;
4863 }
4864
4865 usb_set_device_state(udev, USB_STATE_POWERED);
Matthias Beyer469271f2013-10-10 23:41:27 +02004866 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004867 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004868 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004869
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004870 /* Devices connected to SuperSpeed hubs are USB 3.0 or later */
Sarah Sharp131dec32010-12-06 21:00:19 -08004871 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004872 udev->speed = USB_SPEED_SUPER;
4873 else
4874 udev->speed = USB_SPEED_UNKNOWN;
4875
Alan Stern3b29b682011-02-22 09:53:41 -05004876 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004877 if (udev->devnum <= 0) {
4878 status = -ENOTCONN; /* Don't retry */
4879 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004880 }
4881
Sarah Sharpe7b77172009-04-27 19:54:26 -07004882 /* reset (non-USB 3.0 devices) and get descriptor */
Dan Williams5c79a1e2014-05-20 18:09:26 -07004883 usb_lock_port(port_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 status = hub_port_init(hub, udev, port1, i);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004885 usb_unlock_port(port_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 if (status < 0)
4887 goto loop;
4888
Hemant Kumarcad6bda2017-11-20 20:07:16 -08004889 dev_speed = udev->speed;
4890 if (udev->speed > USB_SPEED_UNKNOWN &&
4891 udev->speed <= USB_SPEED_HIGH && hcd->usb_phy
4892 && hcd->usb_phy->disable_chirp)
4893 hcd->usb_phy->disable_chirp(hcd->usb_phy,
4894 false);
4895
Phil Dibowitz93362a82010-07-22 00:05:01 +02004896 if (udev->quirks & USB_QUIRK_DELAY_INIT)
Dmitry Fleytman43feb292017-09-05 11:40:56 +03004897 msleep(2000);
Phil Dibowitz93362a82010-07-22 00:05:01 +02004898
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 /* consecutive bus-powered hubs aren't reliable; they can
4900 * violate the voltage drop budget. if the new child has
4901 * a "powered" LED, users should notice we didn't enable it
4902 * (without reading syslog), even without per-port LEDs
4903 * on the parent.
4904 */
4905 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004906 && udev->bus_mA <= unit_load) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 u16 devstat;
4908
4909 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4910 &devstat);
Alan Stern15b73362013-07-30 15:35:40 -04004911 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 dev_dbg(&udev->dev, "get status %d ?\n", status);
4913 goto loop_disable;
4914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4916 dev_err(&udev->dev,
4917 "can't connect bus-powered hub "
4918 "to this port\n");
4919 if (hub->has_indicators) {
4920 hub->indicator[port1-1] =
4921 INDICATOR_AMBER_BLINK;
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08004922 queue_delayed_work(
4923 system_power_efficient_wq,
4924 &hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 }
4926 status = -ENOTCONN; /* Don't retry */
4927 goto loop_disable;
4928 }
4929 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004930
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 /* check for devices running slower than they could */
4932 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4933 && udev->speed == USB_SPEED_FULL
4934 && highspeed_hubs != 0)
Chase Metzger039b3302015-08-18 20:36:58 -07004935 check_highspeed(hub, udev, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004937 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 * udev becomes globally accessible, although presumably
4939 * no one will look at it until hdev is unlocked.
4940 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 status = 0;
4942
Dan Williamsd8521af2014-05-20 18:08:28 -07004943 mutex_lock(&usb_port_peer_mutex);
4944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 /* We mustn't add new devices if the parent hub has
4946 * been disconnected; we would race with the
4947 * recursively_mark_NOTATTACHED() routine.
4948 */
4949 spin_lock_irq(&device_state_lock);
4950 if (hdev->state == USB_STATE_NOTATTACHED)
4951 status = -ENOTCONN;
4952 else
Dan Williamsd99f6b42014-05-20 18:08:17 -07004953 port_dev->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 spin_unlock_irq(&device_state_lock);
Dan Williamsd8521af2014-05-20 18:08:28 -07004955 mutex_unlock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
4957 /* Run it through the hoops (find a driver, etc) */
4958 if (!status) {
4959 status = usb_new_device(udev);
4960 if (status) {
Dan Williamsd8521af2014-05-20 18:08:28 -07004961 mutex_lock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 spin_lock_irq(&device_state_lock);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004963 port_dev->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 spin_unlock_irq(&device_state_lock);
Dan Williamsd8521af2014-05-20 18:08:28 -07004965 mutex_unlock(&usb_port_peer_mutex);
Tony Zheng01ed67d2014-10-17 19:43:02 +08004966 } else {
4967 if (hcd->usb_phy && !hdev->parent)
4968 usb_phy_notify_connect(hcd->usb_phy,
4969 udev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 }
4971 }
4972
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 if (status)
4974 goto loop_disable;
4975
4976 status = hub_power_remaining(hub);
4977 if (status)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004978 dev_dbg(hub->intfdev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
4980 return;
4981
4982loop_disable:
4983 hub_port_disable(hub, port1, 1);
4984loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004985 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004986 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004987 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004989 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 break;
Mike Looijmanse328dff2017-11-09 13:16:46 +01004991
4992 /* When halfway through our retry count, power-cycle the port */
4993 if (i == (SET_CONFIG_TRIES / 2) - 1) {
4994 dev_info(&port_dev->dev, "attempt power cycle\n");
4995 usb_hub_set_port_power(hdev, hub, port1, false);
4996 msleep(2 * hub_power_on_good_delay(hub));
4997 usb_hub_set_port_power(hdev, hub, port1, true);
4998 msleep(hub_power_on_good_delay(hub));
4999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 }
Alan Stern3a311552008-05-20 16:58:29 -04005001 if (hub->hdev->parent ||
5002 !hcd->driver->port_handed_over ||
Alan Sterne9e88fb2013-03-27 16:14:01 -04005003 !(hcd->driver->port_handed_over)(hcd, port1)) {
5004 if (status != -ENOTCONN && status != -ENODEV)
Dan Williamsd99f6b42014-05-20 18:08:17 -07005005 dev_err(&port_dev->dev,
5006 "unable to enumerate USB device\n");
Hemant Kumarcad6bda2017-11-20 20:07:16 -08005007 if (!hub->hdev->parent && dev_speed == USB_SPEED_UNKNOWN
5008 && hcd->usb_phy && hcd->usb_phy->disable_chirp) {
5009 ret = hcd->usb_phy->disable_chirp(hcd->usb_phy, true);
5010 if (!ret) {
5011 dev_dbg(&port_dev->dev,
5012 "chirp disabled re-try enum\n");
5013 goto retry_enum;
5014 } else {
5015 /* bail out and re-enable chirping */
5016 hcd->usb_phy->disable_chirp(hcd->usb_phy,
5017 false);
5018 }
5019 }
Alan Sterne9e88fb2013-03-27 16:14:01 -04005020 }
Matthias Beyer469271f2013-10-10 23:41:27 +02005021
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022done:
5023 hub_port_disable(hub, port1, 1);
Alan Stern7c2beb12017-08-01 10:41:56 -04005024 if (hcd->driver->relinquish_port && !hub->hdev->parent) {
5025 if (status != -ENOTCONN && status != -ENODEV)
5026 hcd->driver->relinquish_port(hcd, port1);
5027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028}
5029
Dan Williamsaf376a42014-05-20 18:09:15 -07005030/* Handle physical or logical connection change events.
5031 * This routine is called when:
5032 * a port connection-change occurs;
5033 * a port enable-change occurs (often caused by EMI);
5034 * usb_reset_and_verify_device() encounters changed descriptors (as from
5035 * a firmware download)
5036 * caller already locked the hub
5037 */
5038static void hub_port_connect_change(struct usb_hub *hub, int port1,
5039 u16 portstatus, u16 portchange)
Dan Williams5c79a1e2014-05-20 18:09:26 -07005040 __must_hold(&port_dev->status_lock)
Sarah Sharp714b07b2012-01-24 13:53:18 -08005041{
Dan Williamsaf376a42014-05-20 18:09:15 -07005042 struct usb_port *port_dev = hub->ports[port1 - 1];
5043 struct usb_device *udev = port_dev->child;
5044 int status = -ENODEV;
Sarah Sharp714b07b2012-01-24 13:53:18 -08005045
Dan Williamsaf376a42014-05-20 18:09:15 -07005046 dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus,
5047 portchange, portspeed(hub, portstatus));
5048
5049 if (hub->has_indicators) {
5050 set_port_led(hub, port1, HUB_LED_AUTO);
5051 hub->indicator[port1-1] = INDICATOR_AUTO;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08005052 }
5053
Dan Williamsaf376a42014-05-20 18:09:15 -07005054#ifdef CONFIG_USB_OTG
5055 /* during HNP, don't repeat the debounce */
5056 if (hub->hdev->bus->is_b_host)
5057 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
5058 USB_PORT_STAT_C_ENABLE);
5059#endif
Sarah Sharp714b07b2012-01-24 13:53:18 -08005060
Dan Williamsaf376a42014-05-20 18:09:15 -07005061 /* Try to resuscitate an existing device */
5062 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
5063 udev->state != USB_STATE_NOTATTACHED) {
5064 if (portstatus & USB_PORT_STAT_ENABLE) {
5065 status = 0; /* Nothing to do */
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01005066#ifdef CONFIG_PM
Dan Williamsaf376a42014-05-20 18:09:15 -07005067 } else if (udev->state == USB_STATE_SUSPENDED &&
5068 udev->persist_enabled) {
5069 /* For a suspended device, treat this as a
5070 * remote wakeup event.
5071 */
Dan Williams5c79a1e2014-05-20 18:09:26 -07005072 usb_unlock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07005073 status = usb_remote_wakeup(udev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07005074 usb_lock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07005075#endif
5076 } else {
5077 /* Don't resuscitate */;
5078 }
Sarah Sharp714b07b2012-01-24 13:53:18 -08005079 }
Dan Williamsaf376a42014-05-20 18:09:15 -07005080 clear_bit(port1, hub->change_bits);
5081
Dan Williams5c79a1e2014-05-20 18:09:26 -07005082 /* successfully revalidated the connection */
Dan Williamsaf376a42014-05-20 18:09:15 -07005083 if (status == 0)
5084 return;
5085
Dan Williams5c79a1e2014-05-20 18:09:26 -07005086 usb_unlock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07005087 hub_port_connect(hub, port1, portstatus, portchange);
Dan Williams5c79a1e2014-05-20 18:09:26 -07005088 usb_lock_port(port_dev);
Sarah Sharp714b07b2012-01-24 13:53:18 -08005089}
5090
Dan Williamsaf376a42014-05-20 18:09:15 -07005091static void port_event(struct usb_hub *hub, int port1)
Dan Williams5c79a1e2014-05-20 18:09:26 -07005092 __must_hold(&port_dev->status_lock)
Dan Williamsaf376a42014-05-20 18:09:15 -07005093{
Zhuang Jin Can7671bd12015-01-26 23:30:39 +08005094 int connect_change;
Dan Williamsaf376a42014-05-20 18:09:15 -07005095 struct usb_port *port_dev = hub->ports[port1 - 1];
5096 struct usb_device *udev = port_dev->child;
5097 struct usb_device *hdev = hub->hdev;
5098 u16 portstatus, portchange;
5099
5100 connect_change = test_bit(port1, hub->change_bits);
5101 clear_bit(port1, hub->event_bits);
5102 clear_bit(port1, hub->wakeup_bits);
5103
5104 if (hub_port_status(hub, port1, &portstatus, &portchange) < 0)
5105 return;
5106
5107 if (portchange & USB_PORT_STAT_C_CONNECTION) {
5108 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
5109 connect_change = 1;
5110 }
5111
5112 if (portchange & USB_PORT_STAT_C_ENABLE) {
5113 if (!connect_change)
5114 dev_dbg(&port_dev->dev, "enable change, status %08x\n",
5115 portstatus);
5116 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
5117
5118 /*
5119 * EM interference sometimes causes badly shielded USB devices
5120 * to be shutdown by the hub, this hack enables them again.
5121 * Works at least with mouse driver.
5122 */
5123 if (!(portstatus & USB_PORT_STAT_ENABLE)
5124 && !connect_change && udev) {
5125 dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n");
5126 connect_change = 1;
5127 }
5128 }
5129
5130 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
5131 u16 status = 0, unused;
5132
5133 dev_dbg(&port_dev->dev, "over-current change\n");
5134 usb_clear_port_feature(hdev, port1,
5135 USB_PORT_FEAT_C_OVER_CURRENT);
5136 msleep(100); /* Cool down */
5137 hub_power_on(hub, true);
5138 hub_port_status(hub, port1, &status, &unused);
5139 if (status & USB_PORT_STAT_OVERCURRENT)
5140 dev_err(&port_dev->dev, "over-current condition\n");
5141 }
5142
5143 if (portchange & USB_PORT_STAT_C_RESET) {
5144 dev_dbg(&port_dev->dev, "reset change\n");
5145 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET);
5146 }
5147 if ((portchange & USB_PORT_STAT_C_BH_RESET)
5148 && hub_is_superspeed(hdev)) {
5149 dev_dbg(&port_dev->dev, "warm reset change\n");
5150 usb_clear_port_feature(hdev, port1,
5151 USB_PORT_FEAT_C_BH_PORT_RESET);
5152 }
5153 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
5154 dev_dbg(&port_dev->dev, "link state change\n");
5155 usb_clear_port_feature(hdev, port1,
5156 USB_PORT_FEAT_C_PORT_LINK_STATE);
5157 }
5158 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
5159 dev_warn(&port_dev->dev, "config error\n");
5160 usb_clear_port_feature(hdev, port1,
5161 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
5162 }
5163
Dan Williams097a1552014-05-20 18:09:20 -07005164 /* skip port actions that require the port to be powered on */
5165 if (!pm_runtime_active(&port_dev->dev))
5166 return;
5167
Dan Williamsaf376a42014-05-20 18:09:15 -07005168 if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange))
5169 connect_change = 1;
5170
5171 /*
5172 * Warm reset a USB3 protocol port if it's in
5173 * SS.Inactive state.
5174 */
Dan Williams3cd12f92014-05-29 12:58:46 -07005175 if (hub_port_warm_reset_required(hub, port1, portstatus)) {
Dan Williamsaf376a42014-05-20 18:09:15 -07005176 dev_dbg(&port_dev->dev, "do warm reset\n");
5177 if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION)
5178 || udev->state == USB_STATE_NOTATTACHED) {
5179 if (hub_port_reset(hub, port1, NULL,
5180 HUB_BH_RESET_TIME, true) < 0)
5181 hub_port_disable(hub, port1, 1);
Zhuang Jin Can7671bd12015-01-26 23:30:39 +08005182 } else {
5183 usb_unlock_port(port_dev);
5184 usb_lock_device(udev);
5185 usb_reset_device(udev);
5186 usb_unlock_device(udev);
5187 usb_lock_port(port_dev);
5188 connect_change = 0;
5189 }
Dan Williamsaf376a42014-05-20 18:09:15 -07005190 }
5191
5192 if (connect_change)
5193 hub_port_connect_change(hub, port1, portstatus, portchange);
5194}
5195
Petr Mladek32a695892014-09-19 17:32:21 +02005196static void hub_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 struct usb_device *hdev;
5199 struct usb_interface *intf;
5200 struct usb_hub *hub;
5201 struct device *hub_dev;
5202 u16 hubstatus;
5203 u16 hubchange;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205
Petr Mladek32a695892014-09-19 17:32:21 +02005206 hub = container_of(work, struct usb_hub, events);
Petr Mladekeb6e2922014-09-19 17:32:20 +02005207 hdev = hub->hdev;
5208 hub_dev = hub->intfdev;
5209 intf = to_usb_interface(hub_dev);
Petr Mladek32a695892014-09-19 17:32:21 +02005210
Petr Mladekeb6e2922014-09-19 17:32:20 +02005211 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
5212 hdev->state, hdev->maxchild,
5213 /* NOTE: expects max 15 ports... */
5214 (u16) hub->change_bits[0],
5215 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216
Petr Mladekeb6e2922014-09-19 17:32:20 +02005217 /* Lock the device, then check to see if we were
5218 * disconnected while waiting for the lock to succeed. */
5219 usb_lock_device(hdev);
5220 if (unlikely(hub->disconnected))
Petr Mladek32a695892014-09-19 17:32:21 +02005221 goto out_hdev_lock;
Petr Mladekeb6e2922014-09-19 17:32:20 +02005222
5223 /* If the hub has died, clean up after it */
5224 if (hdev->state == USB_STATE_NOTATTACHED) {
5225 hub->error = -ENODEV;
5226 hub_quiesce(hub, HUB_DISCONNECT);
Petr Mladek32a695892014-09-19 17:32:21 +02005227 goto out_hdev_lock;
Petr Mladekeb6e2922014-09-19 17:32:20 +02005228 }
5229
5230 /* Autoresume */
5231 ret = usb_autopm_get_interface(intf);
5232 if (ret) {
5233 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Petr Mladek32a695892014-09-19 17:32:21 +02005234 goto out_hdev_lock;
Petr Mladekeb6e2922014-09-19 17:32:20 +02005235 }
5236
5237 /* If this is an inactive hub, do nothing */
5238 if (hub->quiescing)
5239 goto out_autopm;
5240
5241 if (hub->error) {
5242 dev_dbg(hub_dev, "resetting for error %d\n", hub->error);
5243
5244 ret = usb_reset_device(hdev);
Alan Stern40f122f2006-11-09 14:44:33 -05005245 if (ret) {
Petr Mladekeb6e2922014-09-19 17:32:20 +02005246 dev_dbg(hub_dev, "error resetting hub: %d\n", ret);
5247 goto out_autopm;
Alan Stern40f122f2006-11-09 14:44:33 -05005248 }
5249
Petr Mladekeb6e2922014-09-19 17:32:20 +02005250 hub->nerrors = 0;
5251 hub->error = 0;
5252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
Petr Mladekeb6e2922014-09-19 17:32:20 +02005254 /* deal with port status changes */
5255 for (i = 1; i <= hdev->maxchild; i++) {
5256 struct usb_port *port_dev = hub->ports[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
Petr Mladekeb6e2922014-09-19 17:32:20 +02005258 if (test_bit(i, hub->event_bits)
5259 || test_bit(i, hub->change_bits)
5260 || test_bit(i, hub->wakeup_bits)) {
5261 /*
5262 * The get_noresume and barrier ensure that if
5263 * the port was in the process of resuming, we
5264 * flush that work and keep the port active for
5265 * the duration of the port_event(). However,
5266 * if the port is runtime pm suspended
5267 * (powered-off), we leave it in that state, run
5268 * an abbreviated port_event(), and move on.
5269 */
5270 pm_runtime_get_noresume(&port_dev->dev);
5271 pm_runtime_barrier(&port_dev->dev);
5272 usb_lock_port(port_dev);
5273 port_event(hub, i);
5274 usb_unlock_port(port_dev);
5275 pm_runtime_put_sync(&port_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 }
Petr Mladekeb6e2922014-09-19 17:32:20 +02005277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278
Petr Mladekeb6e2922014-09-19 17:32:20 +02005279 /* deal with hub status changes */
5280 if (test_and_clear_bit(0, hub->event_bits) == 0)
5281 ; /* do nothing */
5282 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
5283 dev_err(hub_dev, "get_hub_status failed\n");
5284 else {
5285 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
5286 dev_dbg(hub_dev, "power change\n");
5287 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
5288 if (hubstatus & HUB_STATUS_LOCAL_POWER)
5289 /* FIXME: Is this always true? */
5290 hub->limited_power = 1;
5291 else
5292 hub->limited_power = 0;
Dan Williamsaf376a42014-05-20 18:09:15 -07005293 }
Petr Mladekeb6e2922014-09-19 17:32:20 +02005294 if (hubchange & HUB_CHANGE_OVERCURRENT) {
5295 u16 status = 0;
5296 u16 unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297
Petr Mladekeb6e2922014-09-19 17:32:20 +02005298 dev_dbg(hub_dev, "over-current change\n");
5299 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
5300 msleep(500); /* Cool down */
5301 hub_power_on(hub, true);
5302 hub_hub_status(hub, &status, &unused);
5303 if (status & HUB_STATUS_OVERCURRENT)
5304 dev_err(hub_dev, "over-current condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 }
Petr Mladekeb6e2922014-09-19 17:32:20 +02005306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
Petr Mladekeb6e2922014-09-19 17:32:20 +02005308out_autopm:
5309 /* Balance the usb_autopm_get_interface() above */
5310 usb_autopm_put_interface_no_suspend(intf);
Petr Mladek32a695892014-09-19 17:32:21 +02005311out_hdev_lock:
Petr Mladekeb6e2922014-09-19 17:32:20 +02005312 usb_unlock_device(hdev);
Petr Mladek32a695892014-09-19 17:32:21 +02005313
5314 /* Balance the stuff in kick_hub_wq() and allow autosuspend */
5315 usb_autopm_put_interface(intf);
Petr Mladekeb6e2922014-09-19 17:32:20 +02005316 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317}
5318
Németh Márton1e927d92010-01-10 15:34:53 +01005319static const struct usb_device_id hub_id_table[] = {
Ming Leie6f30de2012-10-24 11:59:24 +08005320 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
Matthias Beyer469271f2013-10-10 23:41:27 +02005321 | USB_DEVICE_ID_MATCH_INT_CLASS,
Ming Leie6f30de2012-10-24 11:59:24 +08005322 .idVendor = USB_VENDOR_GENESYS_LOGIC,
5323 .bInterfaceClass = USB_CLASS_HUB,
5324 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
5326 .bDeviceClass = USB_CLASS_HUB},
5327 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
5328 .bInterfaceClass = USB_CLASS_HUB},
5329 { } /* Terminating entry */
5330};
5331
Chase Metzger039b3302015-08-18 20:36:58 -07005332MODULE_DEVICE_TABLE(usb, hub_id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333
5334static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 .name = "hub",
5336 .probe = hub_probe,
5337 .disconnect = hub_disconnect,
5338 .suspend = hub_suspend,
5339 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04005340 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04005341 .pre_reset = hub_pre_reset,
5342 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02005343 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05005345 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346};
5347
5348int usb_hub_init(void)
5349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 if (usb_register(&hub_driver) < 0) {
5351 printk(KERN_ERR "%s: can't register hub driver\n",
5352 usbcore_name);
5353 return -1;
5354 }
5355
Petr Mladek32a695892014-09-19 17:32:21 +02005356 /*
5357 * The workqueue needs to be freezable to avoid interfering with
5358 * USB-PERSIST port handover. Otherwise it might see that a full-speed
5359 * device was gone before the EHCI controller had handed its port
5360 * over to the companion full-speed controller.
Petr Mladek32a695892014-09-19 17:32:21 +02005361 */
Petr Mladek638139e2014-09-19 17:32:24 +02005362 hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0);
Petr Mladek32a695892014-09-19 17:32:21 +02005363 if (hub_wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
5366 /* Fall through if kernel_thread failed */
5367 usb_deregister(&hub_driver);
Petr Mladek32a695892014-09-19 17:32:21 +02005368 pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
5370 return -1;
5371}
5372
5373void usb_hub_cleanup(void)
5374{
Petr Mladek32a695892014-09-19 17:32:21 +02005375 destroy_workqueue(hub_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
5377 /*
5378 * Hub resources are freed for us by usb_deregister. It calls
5379 * usb_driver_purge on every device which in turn calls that
5380 * devices disconnect function if it is using this driver.
5381 * The hub_disconnect function takes care of releasing the
5382 * individual hub resources. -greg
5383 */
5384 usb_deregister(&hub_driver);
5385} /* usb_hub_cleanup() */
5386
Alan Sterneb764c42008-03-03 15:16:04 -05005387static int descriptors_changed(struct usb_device *udev,
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005388 struct usb_device_descriptor *old_device_descriptor,
5389 struct usb_host_bos *old_bos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390{
Alan Sterneb764c42008-03-03 15:16:04 -05005391 int changed = 0;
5392 unsigned index;
5393 unsigned serial_len = 0;
5394 unsigned len;
5395 unsigned old_length;
5396 int length;
5397 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
Alan Sterneb764c42008-03-03 15:16:04 -05005399 if (memcmp(&udev->descriptor, old_device_descriptor,
5400 sizeof(*old_device_descriptor)) != 0)
5401 return 1;
5402
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005403 if ((old_bos && !udev->bos) || (!old_bos && udev->bos))
5404 return 1;
5405 if (udev->bos) {
Xenia Ragiadakoub9a10482013-08-31 04:40:49 +03005406 len = le16_to_cpu(udev->bos->desc->wTotalLength);
5407 if (len != le16_to_cpu(old_bos->desc->wTotalLength))
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005408 return 1;
Xenia Ragiadakoub9a10482013-08-31 04:40:49 +03005409 if (memcmp(udev->bos->desc, old_bos->desc, len))
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005410 return 1;
5411 }
5412
Alan Sterneb764c42008-03-03 15:16:04 -05005413 /* Since the idVendor, idProduct, and bcdDevice values in the
5414 * device descriptor haven't changed, we will assume the
5415 * Manufacturer and Product strings haven't changed either.
5416 * But the SerialNumber string could be different (e.g., a
5417 * different flash card of the same brand).
5418 */
5419 if (udev->serial)
5420 serial_len = strlen(udev->serial) + 1;
5421
5422 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05005424 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
5425 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 }
Alan Sterneb764c42008-03-03 15:16:04 -05005427
Oliver Neukum0cc1a512008-01-10 10:31:48 +01005428 buf = kmalloc(len, GFP_NOIO);
Wolfram Sangb74e7062016-08-25 19:38:59 +02005429 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 /* assume the worst */
5431 return 1;
Wolfram Sangb74e7062016-08-25 19:38:59 +02005432
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05005434 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
5436 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05005437 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 dev_dbg(&udev->dev, "config index %d, error %d\n",
5439 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05005440 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 break;
5442 }
Chase Metzger039b3302015-08-18 20:36:58 -07005443 if (memcmp(buf, udev->rawdescriptors[index], old_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 != 0) {
5445 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05005446 index,
5447 ((struct usb_config_descriptor *) buf)->
5448 bConfigurationValue);
5449 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 break;
5451 }
5452 }
Alan Sterneb764c42008-03-03 15:16:04 -05005453
5454 if (!changed && serial_len) {
5455 length = usb_string(udev, udev->descriptor.iSerialNumber,
5456 buf, serial_len);
5457 if (length + 1 != serial_len) {
5458 dev_dbg(&udev->dev, "serial string error %d\n",
5459 length);
5460 changed = 1;
5461 } else if (memcmp(buf, udev->serial, length) != 0) {
5462 dev_dbg(&udev->dev, "serial string changed\n");
5463 changed = 1;
5464 }
5465 }
5466
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05005468 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469}
5470
5471/**
Ming Lei742120c2008-06-18 22:00:29 +08005472 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5474 *
Alan Stern79efa092006-06-01 13:33:42 -04005475 * WARNING - don't use this routine to reset a composite device
5476 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08005477 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 *
5479 * Do a port reset, reassign the device's address, and establish its
5480 * former operating configuration. If the reset fails, or the device's
5481 * descriptors change from their values before the reset, or the original
5482 * configuration and altsettings cannot be restored, a flag will be set
Petr Mladek37ebb542014-09-19 17:32:23 +02005483 * telling hub_wq to pretend the device has been disconnected and then
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 * re-connected. All drivers will be unbound, and the device will be
5485 * re-enumerated and probed all over again.
5486 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005487 * Return: 0 if the reset succeeded, -ENODEV if the device has been
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 * flagged for logical disconnection, or some other negative error code
5489 * if the reset wasn't even attempted.
5490 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005491 * Note:
Dan Williams5c79a1e2014-05-20 18:09:26 -07005492 * The caller must own the device lock and the port lock, the latter is
5493 * taken by usb_reset_device(). For example, it's safe to use
5494 * usb_reset_device() from a driver probe() routine after downloading
5495 * new firmware. For calls that might not occur during probe(), drivers
5496 * should lock the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04005497 *
5498 * Locking exception: This routine may also be called from within an
5499 * autoresume handler. Such usage won't conflict with other tasks
5500 * holding the device lock because these tasks should always call
Dan Williams5c79a1e2014-05-20 18:09:26 -07005501 * usb_autopm_resume_device(), thereby preventing any unwanted
5502 * autoresume. The autoresume handler is expected to have already
5503 * acquired the port lock before calling this routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 */
Ming Lei742120c2008-06-18 22:00:29 +08005505static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506{
5507 struct usb_device *parent_hdev = udev->parent;
5508 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005509 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 struct usb_device_descriptor descriptor = udev->descriptor;
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005511 struct usb_host_bos *bos;
Hans de Goede7a7b5622013-11-08 16:37:26 +01005512 int i, j, ret = 0;
Alan Stern12c3da32005-11-23 12:09:52 -05005513 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514
5515 if (udev->state == USB_STATE_NOTATTACHED ||
5516 udev->state == USB_STATE_SUSPENDED) {
5517 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5518 udev->state);
5519 return -EINVAL;
5520 }
5521
Dan Williams5c79a1e2014-05-20 18:09:26 -07005522 if (!parent_hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 return -EISDIR;
Dan Williams5c79a1e2014-05-20 18:09:26 -07005524
Lan Tianyuad493e52013-01-23 04:26:30 +08005525 parent_hub = usb_hub_to_struct_hub(parent_hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526
Sarah Sharpdcc01c02013-09-30 17:26:29 +03005527 /* Disable USB2 hardware LPM.
5528 * It will be re-enabled by the enumeration process.
5529 */
Kai-Heng Feng9b916af2019-01-12 03:54:25 +08005530 usb_disable_usb2_hardware_lpm(udev);
Sarah Sharpdcc01c02013-09-30 17:26:29 +03005531
Sarah Sharpf74631e2012-06-25 12:08:08 -07005532 /* Disable LPM and LTM while we reset the device and reinstall the alt
5533 * settings. Device-initiated LPM settings, and system exit latency
5534 * settings are cleared when the device is reset, so we have to set
5535 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005536 */
5537 ret = usb_unlocked_disable_lpm(udev);
5538 if (ret) {
5539 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
Hans Yang464ad8c2015-12-01 16:54:59 +08005540 goto re_enumerate_no_bos;
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005541 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07005542 ret = usb_disable_ltm(udev);
5543 if (ret) {
5544 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
5545 __func__);
Hans Yang464ad8c2015-12-01 16:54:59 +08005546 goto re_enumerate_no_bos;
Sarah Sharpf74631e2012-06-25 12:08:08 -07005547 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005548
Hans Yang464ad8c2015-12-01 16:54:59 +08005549 bos = udev->bos;
Greg Kroah-Hartmane5bdfd52016-02-20 14:19:34 -08005550 udev->bos = NULL;
Hans Yang464ad8c2015-12-01 16:54:59 +08005551
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
5553
5554 /* ep0 maxpacket size may change; let the HCD know about it.
5555 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07005556 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04005558 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 break;
5560 }
Alan Sternd5cbad42006-08-11 16:52:39 -04005561
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 if (ret < 0)
5563 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005564
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 /* Device might have changed firmware (DFU or similar) */
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005566 if (descriptors_changed(udev, &descriptor, bos)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 dev_info(&udev->dev, "device firmware changed\n");
5568 udev->descriptor = descriptor; /* for disconnect() calls */
5569 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005570 }
Alan Stern4fe03872009-02-26 10:21:02 -05005571
5572 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 if (!udev->actconfig)
5574 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005575
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005576 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005577 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
5578 if (ret < 0) {
5579 dev_warn(&udev->dev,
5580 "Busted HC? Not enough HCD resources for "
5581 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005582 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005583 goto re_enumerate;
5584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
5586 USB_REQ_SET_CONFIGURATION, 0,
5587 udev->actconfig->desc.bConfigurationValue, 0,
5588 NULL, 0, USB_CTRL_SET_TIMEOUT);
5589 if (ret < 0) {
5590 dev_err(&udev->dev,
5591 "can't restore configuration #%d (error=%d)\n",
5592 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005593 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005595 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005596 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 usb_set_device_state(udev, USB_STATE_CONFIGURED);
5598
Alan Stern4fe03872009-02-26 10:21:02 -05005599 /* Put interfaces back into the same altsettings as before.
5600 * Don't bother to send the Set-Interface request for interfaces
5601 * that were already in altsetting 0; besides being unnecessary,
5602 * many devices can't handle it. Instead just reset the host-side
5603 * endpoint state.
5604 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005606 struct usb_host_config *config = udev->actconfig;
5607 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 struct usb_interface_descriptor *desc;
5609
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05005611 if (desc->bAlternateSetting == 0) {
5612 usb_disable_interface(udev, intf, true);
5613 usb_enable_interface(udev, intf, true);
5614 ret = 0;
5615 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08005616 /* Let the bandwidth allocation function know that this
5617 * device has been reset, and it will have to use
5618 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005619 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08005620 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05005621 ret = usb_set_interface(udev, desc->bInterfaceNumber,
5622 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08005623 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05005624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 if (ret < 0) {
5626 dev_err(&udev->dev, "failed to restore interface %d "
5627 "altsetting %d (error=%d)\n",
5628 desc->bInterfaceNumber,
5629 desc->bAlternateSetting,
5630 ret);
5631 goto re_enumerate;
5632 }
Hans de Goede7a7b5622013-11-08 16:37:26 +01005633 /* Resetting also frees any allocated streams */
5634 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++)
5635 intf->cur_altsetting->endpoint[j].streams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 }
5637
5638done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07005639 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Kai-Heng Fengc73982b2019-01-12 03:54:24 +08005640 usb_enable_usb2_hardware_lpm(udev);
Alan Stern78d9a482008-06-23 16:00:40 -04005641 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07005642 usb_enable_ltm(udev);
Greg Kroah-Hartmane5bdfd52016-02-20 14:19:34 -08005643 usb_release_bos_descriptor(udev);
5644 udev->bos = bos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 return 0;
Matthias Beyer469271f2013-10-10 23:41:27 +02005646
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647re_enumerate:
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005648 usb_release_bos_descriptor(udev);
5649 udev->bos = bos;
Hans Yang464ad8c2015-12-01 16:54:59 +08005650re_enumerate_no_bos:
5651 /* LPM state doesn't matter when we're about to destroy the device. */
5652 hub_port_logical_disconnect(parent_hub, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654}
5655
5656/**
5657 * usb_reset_device - warn interface drivers and perform a USB port reset
Kai-Heng Feng6a80ee62019-11-06 14:27:10 +08005658 * @udev: device to reset (not in NOTATTACHED state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 *
Alan Stern79efa092006-06-01 13:33:42 -04005660 * Warns all drivers bound to registered interfaces (using their pre_reset
5661 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08005662 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04005663 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005664 * Return: The same as for usb_reset_and_verify_device().
Alan Stern79efa092006-06-01 13:33:42 -04005665 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005666 * Note:
Alan Stern79efa092006-06-01 13:33:42 -04005667 * The caller must own the device lock. For example, it's safe to use
5668 * this from a driver probe() routine after downloading new firmware.
5669 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08005670 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04005671 *
5672 * If an interface is currently being probed or disconnected, we assume
5673 * its driver knows how to handle resets. For all other interfaces,
5674 * if the driver doesn't have pre_reset and post_reset methods then
5675 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04005676 */
Ming Lei742120c2008-06-18 22:00:29 +08005677int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04005678{
5679 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05005680 int i;
Ming Lei4d769de2013-02-22 16:34:22 -08005681 unsigned int noio_flag;
Dan Williams5c79a1e2014-05-20 18:09:26 -07005682 struct usb_port *port_dev;
Alan Stern79efa092006-06-01 13:33:42 -04005683 struct usb_host_config *config = udev->actconfig;
Dan Williams5c79a1e2014-05-20 18:09:26 -07005684 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
Alan Stern79efa092006-06-01 13:33:42 -04005685
Kai-Heng Feng6a80ee62019-11-06 14:27:10 +08005686 if (udev->state == USB_STATE_NOTATTACHED) {
Alan Stern79efa092006-06-01 13:33:42 -04005687 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5688 udev->state);
5689 return -EINVAL;
5690 }
5691
Dan Williams5c79a1e2014-05-20 18:09:26 -07005692 if (!udev->parent) {
5693 /* this requires hcd-specific logic; see ohci_restart() */
5694 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
5695 return -EISDIR;
5696 }
5697
5698 port_dev = hub->ports[udev->portnum - 1];
5699
Ming Lei4d769de2013-02-22 16:34:22 -08005700 /*
5701 * Don't allocate memory with GFP_KERNEL in current
5702 * context to avoid possible deadlock if usb mass
5703 * storage interface or usbnet interface(iSCSI case)
5704 * is included in current configuration. The easist
5705 * approach is to do it for every device reset,
5706 * because the device 'memalloc_noio' flag may have
5707 * not been set before reseting the usb device.
5708 */
5709 noio_flag = memalloc_noio_save();
5710
Alan Stern645daaa2006-08-30 15:47:02 -04005711 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05005712 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04005713
Alan Stern79efa092006-06-01 13:33:42 -04005714 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005715 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005716 struct usb_interface *cintf = config->interface[i];
5717 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005718 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05005719
5720 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005721 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04005722 if (drv->pre_reset && drv->post_reset)
5723 unbind = (drv->pre_reset)(cintf);
5724 else if (cintf->condition ==
5725 USB_INTERFACE_BOUND)
5726 unbind = 1;
5727 if (unbind)
5728 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005729 }
5730 }
5731 }
5732
Dan Williams5c79a1e2014-05-20 18:09:26 -07005733 usb_lock_port(port_dev);
Ming Lei742120c2008-06-18 22:00:29 +08005734 ret = usb_reset_and_verify_device(udev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07005735 usb_unlock_port(port_dev);
Alan Stern79efa092006-06-01 13:33:42 -04005736
5737 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005738 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005739 struct usb_interface *cintf = config->interface[i];
5740 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005741 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05005742
Alan Stern78d9a482008-06-23 16:00:40 -04005743 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005744 drv = to_usb_driver(cintf->dev.driver);
5745 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04005746 rebind = (drv->post_reset)(cintf);
5747 else if (cintf->condition ==
5748 USB_INTERFACE_BOUND)
5749 rebind = 1;
Alan Stern6aec0442014-03-12 11:30:38 -04005750 if (rebind)
5751 cintf->needs_binding = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005752 }
Alan Stern79efa092006-06-01 13:33:42 -04005753 }
Alan Stern9105d112019-04-16 10:50:01 -04005754
5755 /* If the reset failed, hub_wq will unbind drivers later */
5756 if (ret == 0)
5757 usb_unbind_and_rebind_marked_interfaces(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005758 }
5759
Alan Stern94fcda12006-11-20 11:38:46 -05005760 usb_autosuspend_device(udev);
Ming Lei4d769de2013-02-22 16:34:22 -08005761 memalloc_noio_restore(noio_flag);
Alan Stern79efa092006-06-01 13:33:42 -04005762 return ret;
5763}
Ming Lei742120c2008-06-18 22:00:29 +08005764EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005765
5766
5767/**
5768 * usb_queue_reset_device - Reset a USB device from an atomic context
5769 * @iface: USB interface belonging to the device to reset
5770 *
5771 * This function can be used to reset a USB device from an atomic
5772 * context, where usb_reset_device() won't work (as it blocks).
5773 *
5774 * Doing a reset via this method is functionally equivalent to calling
5775 * usb_reset_device(), except for the fact that it is delayed to a
5776 * workqueue. This means that any drivers bound to other interfaces
5777 * might be unbound, as well as users from usbfs in user space.
5778 *
5779 * Corner cases:
5780 *
5781 * - Scheduling two resets at the same time from two different drivers
5782 * attached to two different interfaces of the same device is
5783 * possible; depending on how the driver attached to each interface
5784 * handles ->pre_reset(), the second reset might happen or not.
5785 *
Alan Stern524134d2015-01-21 14:02:43 -05005786 * - If the reset is delayed so long that the interface is unbound from
5787 * its driver, the reset will be skipped.
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005788 *
Alan Stern524134d2015-01-21 14:02:43 -05005789 * - This function can be called during .probe(). It can also be called
5790 * during .disconnect(), but doing so is pointless because the reset
5791 * will not occur. If you really want to reset the device during
5792 * .disconnect(), call usb_reset_device() directly -- but watch out
5793 * for nested unbinding issues!
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005794 */
5795void usb_queue_reset_device(struct usb_interface *iface)
5796{
Alan Stern524134d2015-01-21 14:02:43 -05005797 if (schedule_work(&iface->reset_ws))
5798 usb_get_intf(iface);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005799}
5800EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c72012-09-05 13:44:32 +08005801
5802/**
5803 * usb_hub_find_child - Get the pointer of child device
5804 * attached to the port which is specified by @port1.
5805 * @hdev: USB device belonging to the usb hub
5806 * @port1: port num to indicate which port the child device
5807 * is attached to.
5808 *
5809 * USB drivers call this function to get hub's child device
5810 * pointer.
5811 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005812 * Return: %NULL if input param is invalid and
Lan Tianyuff823c72012-09-05 13:44:32 +08005813 * child's usb_device pointer if non-NULL.
5814 */
5815struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5816 int port1)
5817{
Lan Tianyuad493e52013-01-23 04:26:30 +08005818 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyuff823c72012-09-05 13:44:32 +08005819
5820 if (port1 < 1 || port1 > hdev->maxchild)
5821 return NULL;
5822 return hub->ports[port1 - 1]->child;
5823}
5824EXPORT_SYMBOL_GPL(usb_hub_find_child);
Lan Tianyud5575422012-09-05 13:44:33 +08005825
Lan Tianyud2123fd2013-01-21 22:18:00 +08005826void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
5827 struct usb_hub_descriptor *desc)
5828{
Dan Williamsd99f6b42014-05-20 18:08:17 -07005829 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyud2123fd2013-01-21 22:18:00 +08005830 enum usb_port_connect_type connect_type;
5831 int i;
5832
Dan Williamsd99f6b42014-05-20 18:08:17 -07005833 if (!hub)
5834 return;
5835
Lan Tianyud2123fd2013-01-21 22:18:00 +08005836 if (!hub_is_superspeed(hdev)) {
5837 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005838 struct usb_port *port_dev = hub->ports[i - 1];
Lan Tianyud2123fd2013-01-21 22:18:00 +08005839
Dan Williamsd99f6b42014-05-20 18:08:17 -07005840 connect_type = port_dev->connect_type;
Lan Tianyud2123fd2013-01-21 22:18:00 +08005841 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5842 u8 mask = 1 << (i%8);
5843
5844 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005845 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
Lan Tianyud2123fd2013-01-21 22:18:00 +08005846 desc->u.hs.DeviceRemovable[i/8] |= mask;
5847 }
5848 }
5849 }
5850 } else {
5851 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable);
5852
5853 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005854 struct usb_port *port_dev = hub->ports[i - 1];
Lan Tianyud2123fd2013-01-21 22:18:00 +08005855
Dan Williamsd99f6b42014-05-20 18:08:17 -07005856 connect_type = port_dev->connect_type;
Lan Tianyud2123fd2013-01-21 22:18:00 +08005857 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5858 u16 mask = 1 << i;
5859
5860 if (!(port_removable & mask)) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005861 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
Lan Tianyud2123fd2013-01-21 22:18:00 +08005862 port_removable |= mask;
5863 }
5864 }
5865 }
5866
5867 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
5868 }
5869}
5870
Lan Tianyud5575422012-09-05 13:44:33 +08005871#ifdef CONFIG_ACPI
5872/**
5873 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5874 * @hdev: USB device belonging to the usb hub
5875 * @port1: port num of the port
5876 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005877 * Return: Port's acpi handle if successful, %NULL if params are
5878 * invalid.
Lan Tianyud5575422012-09-05 13:44:33 +08005879 */
5880acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5881 int port1)
5882{
Lan Tianyuad493e52013-01-23 04:26:30 +08005883 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyud5575422012-09-05 13:44:33 +08005884
Mathias Nyman41341262013-06-18 17:28:48 +03005885 if (!hub)
5886 return NULL;
5887
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +01005888 return ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
Lan Tianyud5575422012-09-05 13:44:33 +08005889}
5890#endif