blob: 3c9b22eb78cacd1df0250d9b1c3347f7b4f367ef [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 Mladek32a69582014-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 Mladek32a69582014-09-19 17:32:21 +020044/* workqueue to process hub events */
45static struct workqueue_struct *hub_wq;
46static void hub_event(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Dan Williamsd8521af2014-05-20 18:08:28 -070048/* synchronize hub-port add/remove and peering operations */
49DEFINE_MUTEX(usb_port_peer_mutex);
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/* cycle leds on hubs that aren't blinking for attention */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103052static bool blinkenlights = 0;
Chase Metzger166b8632015-08-08 01:03:34 -070053module_param(blinkenlights, bool, S_IRUGO);
54MODULE_PARM_DESC(blinkenlights, "true to cycle leds on hubs");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020057 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
58 * 10 seconds to send reply for the initial 64-byte descriptor request.
59 */
60/* define initial 64-byte descriptor request timeout in milliseconds */
61static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
62module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +080063MODULE_PARM_DESC(initial_descriptor_timeout,
64 "initial 64-byte descriptor request timeout in milliseconds "
65 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020066
67/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * As of 2.6.10 we introduce a new USB device initialization scheme which
69 * closely resembles the way Windows works. Hopefully it will be compatible
70 * with a wider range of devices than the old scheme. However some previously
71 * working devices may start giving rise to "device not accepting address"
72 * errors; if that happens the user can try the old scheme by adjusting the
73 * following module parameters.
74 *
75 * For maximum flexibility there are two boolean parameters to control the
76 * hub driver's behavior. On the first initialization attempt, if the
77 * "old_scheme_first" parameter is set then the old scheme will be used,
78 * otherwise the new scheme is used. If that fails and "use_both_schemes"
79 * is set, then the driver will make another attempt, using the other scheme.
80 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103081static bool old_scheme_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
83MODULE_PARM_DESC(old_scheme_first,
84 "start with the old device initialization scheme");
85
Rusty Russell90ab5ee2012-01-13 09:32:20 +103086static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
88MODULE_PARM_DESC(use_both_schemes,
89 "try the other device initialization scheme if the "
90 "first one fails");
91
Alan Stern32fe0192007-10-10 16:27:07 -040092/* Mutual exclusion for EHCI CF initialization. This interferes with
93 * port reset on some companion controllers.
94 */
95DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
96EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
97
Lan Tianyuad493e52013-01-23 04:26:30 +080098#define HUB_DEBOUNCE_TIMEOUT 2000
Alan Stern948fea32008-04-28 11:07:07 -040099#define HUB_DEBOUNCE_STEP 25
100#define HUB_DEBOUNCE_STABLE 100
101
Petr Mladek32a69582014-09-19 17:32:21 +0200102static void hub_release(struct kref *kref);
Ming Lei742120c2008-06-18 22:00:29 +0800103static int usb_reset_and_verify_device(struct usb_device *udev);
104
Sarah Sharp131dec32010-12-06 21:00:19 -0800105static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Sarah Sharp131dec32010-12-06 21:00:19 -0800107 if (hub_is_superspeed(hub->hdev))
108 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500109 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Matthias Beyer469271f2013-10-10 23:41:27 +0200110 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500111 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return "1.5 Mb/s";
113 else
114 return "12 Mb/s";
115}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/* Note that hdev or one of its children must be locked! */
Lan Tianyuad493e52013-01-23 04:26:30 +0800118struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Lan Tianyuff823c792012-09-05 13:44:32 +0800120 if (!hdev || !hdev->actconfig || !hdev->maxchild)
Alan Stern251180842009-07-22 14:41:18 -0400121 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return usb_get_intfdata(hdev->actconfig->interface[0]);
123}
124
Lu Baolu2d2a3162015-06-16 09:08:26 +0800125int usb_device_supports_lpm(struct usb_device *udev)
Sarah Sharpd9b20992012-02-20 08:31:26 -0800126{
Alan Sternad87e032015-12-10 15:27:21 -0500127 /* Some devices have trouble with LPM */
128 if (udev->quirks & USB_QUIRK_NO_LPM)
129 return 0;
130
Sarah Sharpd9b20992012-02-20 08:31:26 -0800131 /* USB 2.1 (and greater) devices indicate LPM support through
132 * their USB 2.0 Extended Capabilities BOS descriptor.
133 */
Rupesh Tatiyaa8425292015-04-14 16:36:55 +0530134 if (udev->speed == USB_SPEED_HIGH || udev->speed == USB_SPEED_FULL) {
Sarah Sharpd9b20992012-02-20 08:31:26 -0800135 if (udev->bos->ext_cap &&
136 (USB_LPM_SUPPORT &
137 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
138 return 1;
139 return 0;
140 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800141
Sarah Sharp25cd2882014-01-17 14:15:44 -0800142 /*
143 * According to the USB 3.0 spec, all USB 3.0 devices must support LPM.
144 * However, there are some that don't, and they set the U1/U2 exit
145 * latencies to zero.
Sarah Sharp51e0a012012-02-20 12:02:19 -0800146 */
147 if (!udev->bos->ss_cap) {
Sarah Sharp25cd2882014-01-17 14:15:44 -0800148 dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n");
Sarah Sharp51e0a012012-02-20 12:02:19 -0800149 return 0;
150 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800151
Sarah Sharp25cd2882014-01-17 14:15:44 -0800152 if (udev->bos->ss_cap->bU1devExitLat == 0 &&
153 udev->bos->ss_cap->bU2DevExitLat == 0) {
154 if (udev->parent)
155 dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n");
156 else
157 dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n");
158 return 0;
159 }
160
161 if (!udev->parent || udev->parent->lpm_capable)
162 return 1;
Sarah Sharpd9b20992012-02-20 08:31:26 -0800163 return 0;
164}
165
Sarah Sharp51e0a012012-02-20 12:02:19 -0800166/*
167 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
168 * either U1 or U2.
169 */
170static void usb_set_lpm_mel(struct usb_device *udev,
171 struct usb3_lpm_parameters *udev_lpm_params,
172 unsigned int udev_exit_latency,
173 struct usb_hub *hub,
174 struct usb3_lpm_parameters *hub_lpm_params,
175 unsigned int hub_exit_latency)
176{
177 unsigned int total_mel;
178 unsigned int device_mel;
179 unsigned int hub_mel;
180
181 /*
182 * Calculate the time it takes to transition all links from the roothub
183 * to the parent hub into U0. The parent hub must then decode the
184 * packet (hub header decode latency) to figure out which port it was
185 * bound for.
186 *
187 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
188 * means 0.1us). Multiply that by 100 to get nanoseconds.
189 */
190 total_mel = hub_lpm_params->mel +
191 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
192
193 /*
194 * How long will it take to transition the downstream hub's port into
195 * U0? The greater of either the hub exit latency or the device exit
196 * latency.
197 *
198 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
199 * Multiply that by 1000 to get nanoseconds.
200 */
201 device_mel = udev_exit_latency * 1000;
202 hub_mel = hub_exit_latency * 1000;
203 if (device_mel > hub_mel)
204 total_mel += device_mel;
205 else
206 total_mel += hub_mel;
207
208 udev_lpm_params->mel = total_mel;
209}
210
211/*
212 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
213 * a transition from either U1 or U2.
214 */
215static void usb_set_lpm_pel(struct usb_device *udev,
216 struct usb3_lpm_parameters *udev_lpm_params,
217 unsigned int udev_exit_latency,
218 struct usb_hub *hub,
219 struct usb3_lpm_parameters *hub_lpm_params,
220 unsigned int hub_exit_latency,
221 unsigned int port_to_port_exit_latency)
222{
223 unsigned int first_link_pel;
224 unsigned int hub_pel;
225
226 /*
227 * First, the device sends an LFPS to transition the link between the
228 * device and the parent hub into U0. The exit latency is the bigger of
229 * the device exit latency or the hub exit latency.
230 */
231 if (udev_exit_latency > hub_exit_latency)
232 first_link_pel = udev_exit_latency * 1000;
233 else
234 first_link_pel = hub_exit_latency * 1000;
235
236 /*
237 * When the hub starts to receive the LFPS, there is a slight delay for
238 * it to figure out that one of the ports is sending an LFPS. Then it
239 * will forward the LFPS to its upstream link. The exit latency is the
240 * delay, plus the PEL that we calculated for this hub.
241 */
242 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
243
244 /*
245 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
246 * is the greater of the two exit latencies.
247 */
248 if (first_link_pel > hub_pel)
249 udev_lpm_params->pel = first_link_pel;
250 else
251 udev_lpm_params->pel = hub_pel;
252}
253
254/*
255 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
256 * when a device initiates a transition to U0, until when it will receive the
257 * first packet from the host controller.
258 *
259 * Section C.1.5.1 describes the four components to this:
260 * - t1: device PEL
261 * - t2: time for the ERDY to make it from the device to the host.
262 * - t3: a host-specific delay to process the ERDY.
263 * - t4: time for the packet to make it from the host to the device.
264 *
265 * t3 is specific to both the xHCI host and the platform the host is integrated
266 * into. The Intel HW folks have said it's negligible, FIXME if a different
267 * vendor says otherwise.
268 */
269static void usb_set_lpm_sel(struct usb_device *udev,
270 struct usb3_lpm_parameters *udev_lpm_params)
271{
272 struct usb_device *parent;
273 unsigned int num_hubs;
274 unsigned int total_sel;
275
276 /* t1 = device PEL */
277 total_sel = udev_lpm_params->pel;
278 /* How many external hubs are in between the device & the root port. */
279 for (parent = udev->parent, num_hubs = 0; parent->parent;
280 parent = parent->parent)
281 num_hubs++;
282 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
283 if (num_hubs > 0)
284 total_sel += 2100 + 250 * (num_hubs - 1);
285
286 /* t4 = 250ns * num_hubs */
287 total_sel += 250 * num_hubs;
288
289 udev_lpm_params->sel = total_sel;
290}
291
292static void usb_set_lpm_parameters(struct usb_device *udev)
293{
294 struct usb_hub *hub;
295 unsigned int port_to_port_delay;
296 unsigned int udev_u1_del;
297 unsigned int udev_u2_del;
298 unsigned int hub_u1_del;
299 unsigned int hub_u2_del;
300
Mathias Nyman8a1b2722015-12-10 09:59:25 +0200301 if (!udev->lpm_capable || udev->speed < USB_SPEED_SUPER)
Sarah Sharp51e0a012012-02-20 12:02:19 -0800302 return;
303
Lan Tianyuad493e52013-01-23 04:26:30 +0800304 hub = usb_hub_to_struct_hub(udev->parent);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800305 /* It doesn't take time to transition the roothub into U0, since it
306 * doesn't have an upstream link.
307 */
308 if (!hub)
309 return;
310
311 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
Xenia Ragiadakou4d967992013-08-31 18:09:13 +0300312 udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800313 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
Xenia Ragiadakou4d967992013-08-31 18:09:13 +0300314 hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800315
316 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
317 hub, &udev->parent->u1_params, hub_u1_del);
318
319 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
320 hub, &udev->parent->u2_params, hub_u2_del);
321
322 /*
323 * Appendix C, section C.2.2.2, says that there is a slight delay from
324 * when the parent hub notices the downstream port is trying to
325 * transition to U0 to when the hub initiates a U0 transition on its
326 * upstream port. The section says the delays are tPort2PortU1EL and
327 * tPort2PortU2EL, but it doesn't define what they are.
328 *
329 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
330 * about the same delays. Use the maximum delay calculations from those
331 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
332 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
333 * assume the device exit latencies they are talking about are the hub
334 * exit latencies.
335 *
336 * What do we do if the U2 exit latency is less than the U1 exit
337 * latency? It's possible, although not likely...
338 */
339 port_to_port_delay = 1;
340
341 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
342 hub, &udev->parent->u1_params, hub_u1_del,
343 port_to_port_delay);
344
345 if (hub_u2_del > hub_u1_del)
346 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
347 else
348 port_to_port_delay = 1 + hub_u1_del;
349
350 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
351 hub, &udev->parent->u2_params, hub_u2_del,
352 port_to_port_delay);
353
354 /* Now that we've got PEL, calculate SEL. */
355 usb_set_lpm_sel(udev, &udev->u1_params);
356 usb_set_lpm_sel(udev, &udev->u2_params);
357}
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359/* USB 2.0 spec Section 11.24.4.5 */
John Youndbe79bb2001-09-17 00:00:00 -0700360static int get_hub_descriptor(struct usb_device *hdev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
John Youndbe79bb2001-09-17 00:00:00 -0700362 int i, ret, size;
363 unsigned dtype;
364
365 if (hub_is_superspeed(hdev)) {
366 dtype = USB_DT_SS_HUB;
367 size = USB_DT_SS_HUB_SIZE;
368 } else {
369 dtype = USB_DT_HUB;
370 size = sizeof(struct usb_hub_descriptor);
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 for (i = 0; i < 3; i++) {
374 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
375 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
John Youndbe79bb2001-09-17 00:00:00 -0700376 dtype << 8, 0, data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 USB_CTRL_GET_TIMEOUT);
378 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
379 return ret;
380 }
381 return -EINVAL;
382}
383
384/*
385 * USB 2.0 spec Section 11.24.2.1
386 */
387static int clear_hub_feature(struct usb_device *hdev, int feature)
388{
389 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
390 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
391}
392
393/*
394 * USB 2.0 spec Section 11.24.2.2
395 */
Lan Tianyuad493e52013-01-23 04:26:30 +0800396int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
399 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
400 NULL, 0, 1000);
401}
402
403/*
404 * USB 2.0 spec Section 11.24.2.13
405 */
406static int set_port_feature(struct usb_device *hdev, int port1, int feature)
407{
408 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
409 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
410 NULL, 0, 1000);
411}
412
Dan Williamsd99f6b42014-05-20 18:08:17 -0700413static char *to_led_name(int selector)
414{
415 switch (selector) {
416 case HUB_LED_AMBER:
417 return "amber";
418 case HUB_LED_GREEN:
419 return "green";
420 case HUB_LED_OFF:
421 return "off";
422 case HUB_LED_AUTO:
423 return "auto";
424 default:
425 return "??";
426 }
427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429/*
430 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
431 * for info about using port indicators
432 */
Dan Williamsd99f6b42014-05-20 18:08:17 -0700433static void set_port_led(struct usb_hub *hub, int port1, int selector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Dan Williamsd99f6b42014-05-20 18:08:17 -0700435 struct usb_port *port_dev = hub->ports[port1 - 1];
436 int status;
437
438 status = set_port_feature(hub->hdev, (selector << 8) | port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 USB_PORT_FEAT_INDICATOR);
Dan Williamsd99f6b42014-05-20 18:08:17 -0700440 dev_dbg(&port_dev->dev, "indicator %s status %d\n",
441 to_led_name(selector), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444#define LED_CYCLE_PERIOD ((2*HZ)/3)
445
Chase Metzger166b8632015-08-08 01:03:34 -0700446static void led_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
David Howellsc4028952006-11-22 14:57:56 +0000448 struct usb_hub *hub =
449 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 struct usb_device *hdev = hub->hdev;
451 unsigned i;
452 unsigned changed = 0;
453 int cursor = -1;
454
455 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
456 return;
457
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +0200458 for (i = 0; i < hdev->maxchild; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 unsigned selector, mode;
460
461 /* 30%-50% duty cycle */
462
463 switch (hub->indicator[i]) {
464 /* cycle marker */
465 case INDICATOR_CYCLE:
466 cursor = i;
467 selector = HUB_LED_AUTO;
468 mode = INDICATOR_AUTO;
469 break;
470 /* blinking green = sw attention */
471 case INDICATOR_GREEN_BLINK:
472 selector = HUB_LED_GREEN;
473 mode = INDICATOR_GREEN_BLINK_OFF;
474 break;
475 case INDICATOR_GREEN_BLINK_OFF:
476 selector = HUB_LED_OFF;
477 mode = INDICATOR_GREEN_BLINK;
478 break;
479 /* blinking amber = hw attention */
480 case INDICATOR_AMBER_BLINK:
481 selector = HUB_LED_AMBER;
482 mode = INDICATOR_AMBER_BLINK_OFF;
483 break;
484 case INDICATOR_AMBER_BLINK_OFF:
485 selector = HUB_LED_OFF;
486 mode = INDICATOR_AMBER_BLINK;
487 break;
488 /* blink green/amber = reserved */
489 case INDICATOR_ALT_BLINK:
490 selector = HUB_LED_GREEN;
491 mode = INDICATOR_ALT_BLINK_OFF;
492 break;
493 case INDICATOR_ALT_BLINK_OFF:
494 selector = HUB_LED_AMBER;
495 mode = INDICATOR_ALT_BLINK;
496 break;
497 default:
498 continue;
499 }
500 if (selector != HUB_LED_AUTO)
501 changed = 1;
502 set_port_led(hub, i + 1, selector);
503 hub->indicator[i] = mode;
504 }
505 if (!changed && blinkenlights) {
506 cursor++;
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +0200507 cursor %= hdev->maxchild;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
509 hub->indicator[cursor] = INDICATOR_CYCLE;
510 changed++;
511 }
512 if (changed)
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -0800513 queue_delayed_work(system_power_efficient_wq,
514 &hub->leds, LED_CYCLE_PERIOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
517/* use a short timeout for hub/port status fetches */
518#define USB_STS_TIMEOUT 1000
519#define USB_STS_RETRIES 5
520
521/*
522 * USB 2.0 spec Section 11.24.2.6
523 */
524static int get_hub_status(struct usb_device *hdev,
525 struct usb_hub_status *data)
526{
527 int i, status = -ETIMEDOUT;
528
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200529 for (i = 0; i < USB_STS_RETRIES &&
530 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
532 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
533 data, sizeof(*data), USB_STS_TIMEOUT);
534 }
535 return status;
536}
537
538/*
539 * USB 2.0 spec Section 11.24.2.7
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200540 * USB 3.1 takes into use the wValue and wLength fields, spec Section 10.16.2.6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
542static int get_port_status(struct usb_device *hdev, int port1,
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200543 void *data, u16 value, u16 length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 int i, status = -ETIMEDOUT;
546
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200547 for (i = 0; i < USB_STS_RETRIES &&
548 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200550 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, value,
551 port1, data, length, USB_STS_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553 return status;
554}
555
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200556static int hub_ext_port_status(struct usb_hub *hub, int port1, int type,
557 u16 *status, u16 *change, u32 *ext_status)
Alan Stern3eb14912008-03-03 15:15:43 -0500558{
559 int ret;
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200560 int len = 4;
561
562 if (type != HUB_PORT_STATUS)
563 len = 8;
Alan Stern3eb14912008-03-03 15:15:43 -0500564
565 mutex_lock(&hub->status_mutex);
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200566 ret = get_port_status(hub->hdev, port1, &hub->status->port, type, len);
567 if (ret < len) {
Alan Sterne9e88fb2013-03-27 16:14:01 -0400568 if (ret != -ENODEV)
569 dev_err(hub->intfdev,
570 "%s failed (err = %d)\n", __func__, ret);
Alan Stern3eb14912008-03-03 15:15:43 -0500571 if (ret >= 0)
572 ret = -EIO;
573 } else {
574 *status = le16_to_cpu(hub->status->port.wPortStatus);
575 *change = le16_to_cpu(hub->status->port.wPortChange);
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200576 if (type != HUB_PORT_STATUS && ext_status)
577 *ext_status = le32_to_cpu(
578 hub->status->port.dwExtPortStatus);
Alan Stern3eb14912008-03-03 15:15:43 -0500579 ret = 0;
580 }
581 mutex_unlock(&hub->status_mutex);
582 return ret;
583}
584
Mathias Nyman0cdd49a2015-12-10 09:59:29 +0200585static int hub_port_status(struct usb_hub *hub, int port1,
586 u16 *status, u16 *change)
587{
588 return hub_ext_port_status(hub, port1, HUB_PORT_STATUS,
589 status, change, NULL);
590}
591
Petr Mladek32a69582014-09-19 17:32:21 +0200592static void kick_hub_wq(struct usb_hub *hub)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Petr Mladek32a69582014-09-19 17:32:21 +0200594 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Petr Mladek32a69582014-09-19 17:32:21 +0200596 if (hub->disconnected || work_pending(&hub->events))
597 return;
Alan Stern8e4ceb32009-12-07 13:01:37 -0500598
Petr Mladek32a69582014-09-19 17:32:21 +0200599 /*
600 * Suppress autosuspend until the event is proceed.
601 *
602 * Be careful and make sure that the symmetric operation is
603 * always called. We are here only when there is no pending
604 * work for this hub. Therefore put the interface either when
605 * the new work is called or when it is canceled.
606 */
607 intf = to_usb_interface(hub->intfdev);
608 usb_autopm_get_interface_no_resume(intf);
609 kref_get(&hub->kref);
610
611 if (queue_work(hub_wq, &hub->events))
612 return;
613
614 /* the work has already been scheduled */
615 usb_autopm_put_interface_async(intf);
616 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Petr Mladek59d48b32014-09-19 17:32:22 +0200619void usb_kick_hub_wq(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Lan Tianyuad493e52013-01-23 04:26:30 +0800621 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Alan Stern251180842009-07-22 14:41:18 -0400622
623 if (hub)
Petr Mladek32a69582014-09-19 17:32:21 +0200624 kick_hub_wq(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800627/*
628 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
629 * Notification, which indicates it had initiated remote wakeup.
630 *
631 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
632 * device initiates resume, so the USB core will not receive notice of the
633 * resume through the normal hub interrupt URB.
634 */
635void usb_wakeup_notification(struct usb_device *hdev,
636 unsigned int portnum)
637{
638 struct usb_hub *hub;
639
640 if (!hdev)
641 return;
642
Lan Tianyuad493e52013-01-23 04:26:30 +0800643 hub = usb_hub_to_struct_hub(hdev);
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800644 if (hub) {
645 set_bit(portnum, hub->wakeup_bits);
Petr Mladek32a69582014-09-19 17:32:21 +0200646 kick_hub_wq(hub);
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800647 }
648}
649EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100652static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200654 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400655 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100656 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 unsigned long bits;
658
Alan Sterne0152682007-08-24 15:42:52 -0400659 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 case -ENOENT: /* synchronous unlink */
661 case -ECONNRESET: /* async unlink */
662 case -ESHUTDOWN: /* hardware going away */
663 return;
664
665 default: /* presumably an error */
666 /* Cause a hub reset after 10 consecutive errors */
Chase Metzger166b8632015-08-08 01:03:34 -0700667 dev_dbg(hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if ((++hub->nerrors < 10) || hub->error)
669 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400670 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200672
Petr Mladek37ebb542014-09-19 17:32:23 +0200673 /* let hub_wq handle things */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 case 0: /* we got data: port status changed */
675 bits = 0;
676 for (i = 0; i < urb->actual_length; ++i)
677 bits |= ((unsigned long) ((*hub->buffer)[i]))
678 << (i*8);
679 hub->event_bits[0] = bits;
680 break;
681 }
682
683 hub->nerrors = 0;
684
Petr Mladek37ebb542014-09-19 17:32:23 +0200685 /* Something happened, let hub_wq figure it out */
Petr Mladek32a69582014-09-19 17:32:21 +0200686 kick_hub_wq(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688resubmit:
689 if (hub->quiescing)
690 return;
691
Kris Borer088a3da2015-08-11 11:12:45 -0400692 status = usb_submit_urb(hub->urb, GFP_ATOMIC);
693 if (status != 0 && status != -ENODEV && status != -EPERM)
Chase Metzger166b8632015-08-08 01:03:34 -0700694 dev_err(hub->intfdev, "resubmit --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697/* USB 2.0 spec Section 11.24.2.3 */
698static inline int
Chase Metzger166b8632015-08-08 01:03:34 -0700699hub_clear_tt_buffer(struct usb_device *hdev, u16 devinfo, u16 tt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
William Gulland2c7b8712013-06-27 16:10:20 -0700701 /* Need to clear both directions for control ep */
702 if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) ==
703 USB_ENDPOINT_XFER_CONTROL) {
704 int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
705 HUB_CLEAR_TT_BUFFER, USB_RT_PORT,
706 devinfo ^ 0x8000, tt, NULL, 0, 1000);
707 if (status)
708 return status;
709 }
Alan Sternc2f65952009-11-18 11:37:15 -0500710 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
712 tt, NULL, 0, 1000);
713}
714
715/*
Petr Mladek37ebb542014-09-19 17:32:23 +0200716 * enumeration blocks hub_wq for a long time. we use keventd instead, since
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * long blocking there is the exception, not the rule. accordingly, HCDs
718 * talking to TTs must queue control transfers (not just bulk and iso), so
719 * both can talk to the same hub concurrently.
720 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400721static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
David Howellsc4028952006-11-22 14:57:56 +0000723 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400724 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 unsigned long flags;
726
Chase Metzger166b8632015-08-08 01:03:34 -0700727 spin_lock_irqsave(&hub->tt.lock, flags);
Octavian Purdila3b6054d2012-10-01 22:21:12 +0300728 while (!list_empty(&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400729 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct usb_tt_clear *clear;
731 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400732 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 int status;
734
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400735 next = hub->tt.clear_list.next;
Chase Metzger166b8632015-08-08 01:03:34 -0700736 clear = list_entry(next, struct usb_tt_clear, clear_list);
737 list_del(&clear->clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 /* drop lock so HCD can concurrently report other TT errors */
Chase Metzger166b8632015-08-08 01:03:34 -0700740 spin_unlock_irqrestore(&hub->tt.lock, flags);
741 status = hub_clear_tt_buffer(hdev, clear->devinfo, clear->tt);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400742 if (status && status != -ENODEV)
Chase Metzger166b8632015-08-08 01:03:34 -0700743 dev_err(&hdev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 "clear tt %d (%04x) error %d\n",
745 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400746
747 /* Tell the HCD, even if the operation failed */
748 drv = clear->hcd->driver;
749 if (drv->clear_tt_buffer_complete)
750 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
751
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700752 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400753 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Chase Metzger166b8632015-08-08 01:03:34 -0700755 spin_unlock_irqrestore(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758/**
Lan Tianyu971fcd42013-01-23 04:26:29 +0800759 * usb_hub_set_port_power - control hub port's power state
Mathias Nyman413412612013-06-18 17:28:48 +0300760 * @hdev: USB device belonging to the usb hub
761 * @hub: target hub
Lan Tianyu971fcd42013-01-23 04:26:29 +0800762 * @port1: port index
763 * @set: expected status
764 *
765 * call this function to control port's power via setting or
766 * clearing the port's PORT_POWER feature.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200767 *
768 * Return: 0 if successful. A negative error code otherwise.
Lan Tianyu971fcd42013-01-23 04:26:29 +0800769 */
Mathias Nyman413412612013-06-18 17:28:48 +0300770int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub,
771 int port1, bool set)
Lan Tianyu971fcd42013-01-23 04:26:29 +0800772{
773 int ret;
774
775 if (set)
776 ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
777 else
Lan Tianyuad493e52013-01-23 04:26:30 +0800778 ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
779
Dan Williamsd5c38342014-05-20 18:08:52 -0700780 if (ret)
781 return ret;
782
783 if (set)
784 set_bit(port1, hub->power_bits);
785 else
786 clear_bit(port1, hub->power_bits);
787 return 0;
Lan Tianyu971fcd42013-01-23 04:26:29 +0800788}
789
790/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400791 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
792 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 *
794 * High speed HCDs use this to tell the hub driver that some split control or
795 * bulk transaction failed in a way that requires clearing internal state of
796 * a transaction translator. This is normally detected (and reported) from
797 * interrupt context.
798 *
799 * It may not be possible for that hub to handle additional full (or low)
800 * speed transactions until that state is fully cleared out.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200801 *
802 * Return: 0 if successful. A negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400804int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400806 struct usb_device *udev = urb->dev;
807 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct usb_tt *tt = udev->tt;
809 unsigned long flags;
810 struct usb_tt_clear *clear;
811
812 /* we've got to cope with an arbitrary number of pending TT clears,
813 * since each TT has "at least two" buffers that can need it (and
814 * there can be many TTs per hub). even if they're uncommon.
815 */
Greg Kroah-Hartmand544d272015-04-30 11:32:53 +0200816 clear = kmalloc(sizeof *clear, GFP_ATOMIC);
817 if (clear == NULL) {
Chase Metzger166b8632015-08-08 01:03:34 -0700818 dev_err(&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400820 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
823 /* info that CLEAR_TT_BUFFER needs */
824 clear->tt = tt->multi ? udev->ttport : 1;
825 clear->devinfo = usb_pipeendpoint (pipe);
826 clear->devinfo |= udev->devnum << 4;
Chase Metzger166b8632015-08-08 01:03:34 -0700827 clear->devinfo |= usb_pipecontrol(pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 ? (USB_ENDPOINT_XFER_CONTROL << 11)
829 : (USB_ENDPOINT_XFER_BULK << 11);
Chase Metzger166b8632015-08-08 01:03:34 -0700830 if (usb_pipein(pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400832
833 /* info for completion callback */
834 clear->hcd = bus_to_hcd(udev->bus);
835 clear->ep = urb->ep;
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /* tell keventd to clear state for this TT */
Chase Metzger166b8632015-08-08 01:03:34 -0700838 spin_lock_irqsave(&tt->lock, flags);
839 list_add_tail(&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400840 schedule_work(&tt->clear_work);
Chase Metzger166b8632015-08-08 01:03:34 -0700841 spin_unlock_irqrestore(&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400844EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Dan Williams7ad3c472014-05-20 18:08:57 -0700846static void hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 int port1;
849
Alan Stern4489a572006-04-27 15:54:22 -0400850 /* Enable power on each port. Some hubs have reserved values
851 * of LPSM (> 2) in their descriptors, even though they are
852 * USB 2.0 hubs. Some hubs do not implement port-power switching
853 * but only emulate it. In all cases, the ports won't work
854 * unless we send these messages to the hub.
855 */
Dan Williams9262c192014-05-20 18:08:12 -0700856 if (hub_is_port_power_switchable(hub))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400858 else
859 dev_dbg(hub->intfdev, "trying to enable port power on "
860 "non-switchable hub\n");
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +0200861 for (port1 = 1; port1 <= hub->hdev->maxchild; port1++)
Dan Williamsd5c38342014-05-20 18:08:52 -0700862 if (test_bit(port1, hub->power_bits))
Lan Tianyuad493e52013-01-23 04:26:30 +0800863 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
864 else
865 usb_clear_port_feature(hub->hdev, port1,
866 USB_PORT_FEAT_POWER);
Alan Stern8520f382008-09-22 14:44:26 -0400867 if (do_delay)
Dan Williams7ad3c472014-05-20 18:08:57 -0700868 msleep(hub_power_on_good_delay(hub));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871static int hub_hub_status(struct usb_hub *hub,
872 u16 *status, u16 *change)
873{
874 int ret;
875
Alan Sterndb90e7a2007-02-05 09:56:15 -0500876 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 ret = get_hub_status(hub->hdev, &hub->status->hub);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400878 if (ret < 0) {
879 if (ret != -ENODEV)
880 dev_err(hub->intfdev,
881 "%s failed (err = %d)\n", __func__, ret);
882 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 *status = le16_to_cpu(hub->status->hub.wHubStatus);
Matthias Beyer469271f2013-10-10 23:41:27 +0200884 *change = le16_to_cpu(hub->status->hub.wHubChange);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 ret = 0;
886 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500887 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return ret;
889}
890
Sarah Sharp41e7e052012-11-14 16:42:32 -0800891static int hub_set_port_link_state(struct usb_hub *hub, int port1,
892 unsigned int link_status)
893{
894 return set_port_feature(hub->hdev,
895 port1 | (link_status << 3),
896 USB_PORT_FEAT_LINK_STATE);
897}
898
899/*
900 * If USB 3.0 ports are placed into the Disabled state, they will no longer
901 * detect any device connects or disconnects. This is generally not what the
902 * USB core wants, since it expects a disabled port to produce a port status
903 * change event when a new device connects.
904 *
905 * Instead, set the link state to Disabled, wait for the link to settle into
906 * that state, clear any change bits, and then put the port into the RxDetect
907 * state.
908 */
909static int hub_usb3_port_disable(struct usb_hub *hub, int port1)
910{
911 int ret;
912 int total_time;
913 u16 portchange, portstatus;
914
915 if (!hub_is_superspeed(hub->hdev))
916 return -EINVAL;
917
Gavin Guobb86cf52014-07-18 01:12:13 +0800918 ret = hub_port_status(hub, port1, &portstatus, &portchange);
919 if (ret < 0)
920 return ret;
921
922 /*
923 * USB controller Advanced Micro Devices, Inc. [AMD] FCH USB XHCI
924 * Controller [1022:7814] will have spurious result making the following
925 * usb 3.0 device hotplugging route to the 2.0 root hub and recognized
926 * as high-speed device if we set the usb 3.0 port link state to
927 * Disabled. Since it's already in USB_SS_PORT_LS_RX_DETECT state, we
928 * check the state here to avoid the bug.
929 */
930 if ((portstatus & USB_PORT_STAT_LINK_STATE) ==
931 USB_SS_PORT_LS_RX_DETECT) {
932 dev_dbg(&hub->ports[port1 - 1]->dev,
933 "Not disabling port; link state is RxDetect\n");
934 return ret;
935 }
936
Sarah Sharp41e7e052012-11-14 16:42:32 -0800937 ret = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_SS_DISABLED);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400938 if (ret)
Sarah Sharp41e7e052012-11-14 16:42:32 -0800939 return ret;
Sarah Sharp41e7e052012-11-14 16:42:32 -0800940
941 /* Wait for the link to enter the disabled state. */
942 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
943 ret = hub_port_status(hub, port1, &portstatus, &portchange);
944 if (ret < 0)
945 return ret;
946
947 if ((portstatus & USB_PORT_STAT_LINK_STATE) ==
948 USB_SS_PORT_LS_SS_DISABLED)
949 break;
950 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
951 break;
952 msleep(HUB_DEBOUNCE_STEP);
953 }
954 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
Dan Williamsd99f6b42014-05-20 18:08:17 -0700955 dev_warn(&hub->ports[port1 - 1]->dev,
956 "Could not disable after %d ms\n", total_time);
Sarah Sharp41e7e052012-11-14 16:42:32 -0800957
958 return hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_RX_DETECT);
959}
960
Alan Stern8b28c752005-08-10 17:04:13 -0400961static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
962{
Dan Williamsd99f6b42014-05-20 18:08:17 -0700963 struct usb_port *port_dev = hub->ports[port1 - 1];
Alan Stern8b28c752005-08-10 17:04:13 -0400964 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400965 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400966
Dan Williamsd99f6b42014-05-20 18:08:17 -0700967 if (port_dev->child && set_state)
968 usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED);
Sarah Sharp41e7e052012-11-14 16:42:32 -0800969 if (!hub->error) {
970 if (hub_is_superspeed(hub->hdev))
971 ret = hub_usb3_port_disable(hub, port1);
972 else
Lan Tianyuad493e52013-01-23 04:26:30 +0800973 ret = usb_clear_port_feature(hdev, port1,
Sarah Sharp41e7e052012-11-14 16:42:32 -0800974 USB_PORT_FEAT_ENABLE);
975 }
Alan Sterne9e88fb2013-03-27 16:14:01 -0400976 if (ret && ret != -ENODEV)
Dan Williamsd99f6b42014-05-20 18:08:17 -0700977 dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400978 return ret;
979}
980
Alan Stern0458d5b2007-05-04 11:52:20 -0400981/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800982 * Disable a port and mark a logical connect-change event, so that some
Petr Mladek37ebb542014-09-19 17:32:23 +0200983 * time later hub_wq will disconnect() any existing usb_device on the port
Alan Stern0458d5b2007-05-04 11:52:20 -0400984 * and will re-enumerate if there actually is a device attached.
985 */
986static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
987{
Dan Williamsd99f6b42014-05-20 18:08:17 -0700988 dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n");
Alan Stern0458d5b2007-05-04 11:52:20 -0400989 hub_port_disable(hub, port1, 1);
990
991 /* FIXME let caller ask to power down the port:
992 * - some devices won't enumerate without a VBUS power cycle
993 * - SRP saves power that way
994 * - ... new call, TBD ...
995 * That's easy if this hub can switch power per-port, and
Petr Mladek37ebb542014-09-19 17:32:23 +0200996 * hub_wq reactivates the port later (timer, SRP, etc).
Alan Stern0458d5b2007-05-04 11:52:20 -0400997 * Powerdown must be optional, because of reset/DFU.
998 */
999
1000 set_bit(port1, hub->change_bits);
Petr Mladek32a69582014-09-19 17:32:21 +02001001 kick_hub_wq(hub);
Alan Stern0458d5b2007-05-04 11:52:20 -04001002}
1003
Alan Stern253e0572009-10-27 15:20:13 -04001004/**
1005 * usb_remove_device - disable a device's port on its parent hub
1006 * @udev: device to be disabled and removed
1007 * Context: @udev locked, must be able to sleep.
1008 *
Petr Mladek37ebb542014-09-19 17:32:23 +02001009 * After @udev's port has been disabled, hub_wq is notified and it will
Alan Stern253e0572009-10-27 15:20:13 -04001010 * see that the device has been disconnected. When the device is
1011 * physically unplugged and something is plugged in, the events will
1012 * be received and processed normally.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001013 *
1014 * Return: 0 if successful. A negative error code otherwise.
Alan Stern253e0572009-10-27 15:20:13 -04001015 */
1016int usb_remove_device(struct usb_device *udev)
1017{
1018 struct usb_hub *hub;
1019 struct usb_interface *intf;
1020
1021 if (!udev->parent) /* Can't remove a root hub */
1022 return -EINVAL;
Lan Tianyuad493e52013-01-23 04:26:30 +08001023 hub = usb_hub_to_struct_hub(udev->parent);
Alan Stern253e0572009-10-27 15:20:13 -04001024 intf = to_usb_interface(hub->intfdev);
1025
1026 usb_autopm_get_interface(intf);
1027 set_bit(udev->portnum, hub->removed_bits);
1028 hub_port_logical_disconnect(hub, udev->portnum);
1029 usb_autopm_put_interface(intf);
1030 return 0;
1031}
1032
Alan Stern6ee0b272008-04-28 11:06:42 -04001033enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -05001034 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -04001035 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -04001036};
Alan Stern5e6effa2008-03-03 15:15:51 -05001037
Alan Stern8520f382008-09-22 14:44:26 -04001038static void hub_init_func2(struct work_struct *ws);
1039static void hub_init_func3(struct work_struct *ws);
1040
Alan Sternf2835212008-04-28 11:07:17 -04001041static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -05001042{
1043 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -08001044 struct usb_hcd *hcd;
1045 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -05001046 int port1;
Alan Sternf2835212008-04-28 11:07:17 -04001047 int status;
Alan Stern948fea32008-04-28 11:07:07 -04001048 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -04001049 unsigned delay;
1050
1051 /* Continue a partial initialization */
Alan Sterne50293e2015-12-16 13:32:38 -05001052 if (type == HUB_INIT2 || type == HUB_INIT3) {
1053 device_lock(hub->intfdev);
1054
1055 /* Was the hub disconnected while we were waiting? */
1056 if (hub->disconnected) {
1057 device_unlock(hub->intfdev);
1058 kref_put(&hub->kref, hub_release);
1059 return;
1060 }
1061 if (type == HUB_INIT2)
1062 goto init2;
Alan Stern8520f382008-09-22 14:44:26 -04001063 goto init3;
Alan Sterne50293e2015-12-16 13:32:38 -05001064 }
1065 kref_get(&hub->kref);
Alan Stern5e6effa2008-03-03 15:15:51 -05001066
Elric Fua45aa3b2012-02-18 13:32:27 +08001067 /* The superspeed hub except for root hub has to use Hub Depth
1068 * value as an offset into the route string to locate the bits
1069 * it uses to determine the downstream port number. So hub driver
1070 * should send a set hub depth request to superspeed hub after
1071 * the superspeed hub is set configuration in initialization or
1072 * reset procedure.
1073 *
1074 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -04001075 * For any other type of activation, turn it on.
1076 */
Alan Stern8520f382008-09-22 14:44:26 -04001077 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +08001078 if (hdev->parent && hub_is_superspeed(hdev)) {
1079 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1080 HUB_SET_DEPTH, USB_RT_HUB,
1081 hdev->level - 1, 0, NULL, 0,
1082 USB_CTRL_SET_TIMEOUT);
1083 if (ret < 0)
1084 dev_err(hub->intfdev,
1085 "set hub depth failed\n");
1086 }
Alan Stern8520f382008-09-22 14:44:26 -04001087
1088 /* Speed up system boot by using a delayed_work for the
1089 * hub's initial power-up delays. This is pretty awkward
1090 * and the implementation looks like a home-brewed sort of
1091 * setjmp/longjmp, but it saves at least 100 ms for each
1092 * root hub (assuming usbcore is compiled into the kernel
1093 * rather than as a module). It adds up.
1094 *
1095 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
1096 * because for those activation types the ports have to be
1097 * operational when we return. In theory this could be done
1098 * for HUB_POST_RESET, but it's easier not to.
1099 */
1100 if (type == HUB_INIT) {
Kris Borer17a364e2015-08-21 10:47:46 +05301101 delay = hub_power_on_good_delay(hub);
Dan Williams7ad3c472014-05-20 18:08:57 -07001102
1103 hub_power_on(hub, false);
Tejun Heo77fa83c2014-03-07 10:24:48 -05001104 INIT_DELAYED_WORK(&hub->init_work, hub_init_func2);
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08001105 queue_delayed_work(system_power_efficient_wq,
1106 &hub->init_work,
Alan Stern8520f382008-09-22 14:44:26 -04001107 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -04001108
1109 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -05001110 usb_autopm_get_interface_no_resume(
1111 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -04001112 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001113 } else if (type == HUB_RESET_RESUME) {
1114 /* The internal host controller state for the hub device
1115 * may be gone after a host power loss on system resume.
1116 * Update the device's info so the HW knows it's a hub.
1117 */
1118 hcd = bus_to_hcd(hdev->bus);
1119 if (hcd->driver->update_hub_device) {
1120 ret = hcd->driver->update_hub_device(hcd, hdev,
1121 &hub->tt, GFP_NOIO);
1122 if (ret < 0) {
1123 dev_err(hub->intfdev, "Host not "
1124 "accepting hub info "
1125 "update.\n");
1126 dev_err(hub->intfdev, "LS/FS devices "
1127 "and hubs may not work "
1128 "under this hub\n.");
1129 }
1130 }
1131 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001132 } else {
1133 hub_power_on(hub, true);
1134 }
1135 }
1136 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001137
Dan Williamsd99f6b42014-05-20 18:08:17 -07001138 /*
Petr Mladek37ebb542014-09-19 17:32:23 +02001139 * Check each port and set hub->change_bits to let hub_wq know
Alan Stern6ee0b272008-04-28 11:06:42 -04001140 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001141 */
1142 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07001143 struct usb_port *port_dev = hub->ports[port1 - 1];
1144 struct usb_device *udev = port_dev->child;
Alan Stern5e6effa2008-03-03 15:15:51 -05001145 u16 portstatus, portchange;
1146
Alan Stern6ee0b272008-04-28 11:06:42 -04001147 portstatus = portchange = 0;
1148 status = hub_port_status(hub, port1, &portstatus, &portchange);
1149 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
Dan Williamsd99f6b42014-05-20 18:08:17 -07001150 dev_dbg(&port_dev->dev, "status %04x change %04x\n",
1151 portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001152
Dan Williamsd99f6b42014-05-20 18:08:17 -07001153 /*
1154 * After anything other than HUB_RESUME (i.e., initialization
Alan Stern6ee0b272008-04-28 11:06:42 -04001155 * or any sort of reset), every port should be disabled.
1156 * Unconnected ports should likewise be disabled (paranoia),
1157 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001158 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001159 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1160 type != HUB_RESUME ||
1161 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1162 !udev ||
1163 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001164 /*
1165 * USB3 protocol ports will automatically transition
1166 * to Enabled state when detect an USB3.0 device attach.
Dan Williamsfd1ac4c2013-10-07 11:58:20 -07001167 * Do not disable USB3 protocol ports, just pretend
1168 * power was lost
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001169 */
Dan Williamsfd1ac4c2013-10-07 11:58:20 -07001170 portstatus &= ~USB_PORT_STAT_ENABLE;
1171 if (!hub_is_superspeed(hdev))
Lan Tianyuad493e52013-01-23 04:26:30 +08001172 usb_clear_port_feature(hdev, port1,
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001173 USB_PORT_FEAT_ENABLE);
Alan Stern6ee0b272008-04-28 11:06:42 -04001174 }
1175
Alan Stern948fea32008-04-28 11:07:07 -04001176 /* Clear status-change flags; we'll debounce later */
1177 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1178 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001179 usb_clear_port_feature(hub->hdev, port1,
Alan Stern948fea32008-04-28 11:07:07 -04001180 USB_PORT_FEAT_C_CONNECTION);
1181 }
1182 if (portchange & USB_PORT_STAT_C_ENABLE) {
1183 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001184 usb_clear_port_feature(hub->hdev, port1,
Alan Stern948fea32008-04-28 11:07:07 -04001185 USB_PORT_FEAT_C_ENABLE);
1186 }
Julius Wernere92aee32013-10-15 17:45:00 -07001187 if (portchange & USB_PORT_STAT_C_RESET) {
1188 need_debounce_delay = true;
1189 usb_clear_port_feature(hub->hdev, port1,
1190 USB_PORT_FEAT_C_RESET);
1191 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001192 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1193 hub_is_superspeed(hub->hdev)) {
1194 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001195 usb_clear_port_feature(hub->hdev, port1,
Don Zickus79c3dd82011-11-03 09:07:18 -04001196 USB_PORT_FEAT_C_BH_PORT_RESET);
1197 }
Alan Stern253e0572009-10-27 15:20:13 -04001198 /* We can forget about a "removed" device when there's a
1199 * physical disconnect or the connect status changes.
1200 */
1201 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1202 (portchange & USB_PORT_STAT_C_CONNECTION))
1203 clear_bit(port1, hub->removed_bits);
1204
Alan Stern6ee0b272008-04-28 11:06:42 -04001205 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
Petr Mladek37ebb542014-09-19 17:32:23 +02001206 /* Tell hub_wq to disconnect the device or
Alan Stern6ee0b272008-04-28 11:06:42 -04001207 * check for a new connection
1208 */
Shen Guang08d1dec2014-01-08 14:45:42 +08001209 if (udev || (portstatus & USB_PORT_STAT_CONNECTION) ||
1210 (portstatus & USB_PORT_STAT_OVERCURRENT))
Alan Stern6ee0b272008-04-28 11:06:42 -04001211 set_bit(port1, hub->change_bits);
1212
1213 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001214 bool port_resumed = (portstatus &
1215 USB_PORT_STAT_LINK_STATE) ==
1216 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001217 /* The power session apparently survived the resume.
1218 * If there was an overcurrent or suspend change
Petr Mladek37ebb542014-09-19 17:32:23 +02001219 * (i.e., remote wakeup request), have hub_wq
Sarah Sharp72937e12012-01-24 11:46:50 -08001220 * take care of it. Look at the port link state
1221 * for USB 3.0 hubs, since they don't have a suspend
1222 * change bit, and they don't set the port link change
1223 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001224 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001225 if (portchange || (hub_is_superspeed(hub->hdev) &&
1226 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001227 set_bit(port1, hub->change_bits);
1228
1229 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001230#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001231 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001232#endif
Lan Tianyuad493e52013-01-23 04:26:30 +08001233 /* Don't set the change_bits when the device
1234 * was powered off.
1235 */
Dan Williamsd5c38342014-05-20 18:08:52 -07001236 if (test_bit(port1, hub->power_bits))
Lan Tianyuad493e52013-01-23 04:26:30 +08001237 set_bit(port1, hub->change_bits);
Alan Stern8808f002008-04-28 11:06:55 -04001238
Alan Stern6ee0b272008-04-28 11:06:42 -04001239 } else {
Petr Mladek37ebb542014-09-19 17:32:23 +02001240 /* The power session is gone; tell hub_wq */
Alan Stern6ee0b272008-04-28 11:06:42 -04001241 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1242 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001243 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001244 }
1245
Alan Stern948fea32008-04-28 11:07:07 -04001246 /* If no port-status-change flags were set, we don't need any
1247 * debouncing. If flags were set we can try to debounce the
Petr Mladek37ebb542014-09-19 17:32:23 +02001248 * ports all at once right now, instead of letting hub_wq do them
Alan Stern948fea32008-04-28 11:07:07 -04001249 * one at a time later on.
1250 *
Petr Mladek37ebb542014-09-19 17:32:23 +02001251 * If any port-status changes do occur during this delay, hub_wq
Alan Stern948fea32008-04-28 11:07:07 -04001252 * will see them later and handle them normally.
1253 */
Alan Stern8520f382008-09-22 14:44:26 -04001254 if (need_debounce_delay) {
1255 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001256
Alan Stern8520f382008-09-22 14:44:26 -04001257 /* Don't do a long sleep inside a workqueue routine */
1258 if (type == HUB_INIT2) {
Tejun Heo77fa83c2014-03-07 10:24:48 -05001259 INIT_DELAYED_WORK(&hub->init_work, hub_init_func3);
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08001260 queue_delayed_work(system_power_efficient_wq,
1261 &hub->init_work,
Alan Stern8520f382008-09-22 14:44:26 -04001262 msecs_to_jiffies(delay));
Alan Sterne50293e2015-12-16 13:32:38 -05001263 device_unlock(hub->intfdev);
Alan Stern8520f382008-09-22 14:44:26 -04001264 return; /* Continues at init3: below */
1265 } else {
1266 msleep(delay);
1267 }
1268 }
1269 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001270 hub->quiescing = 0;
1271
1272 status = usb_submit_urb(hub->urb, GFP_NOIO);
1273 if (status < 0)
1274 dev_err(hub->intfdev, "activate --> %d\n", status);
1275 if (hub->has_indicators && blinkenlights)
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08001276 queue_delayed_work(system_power_efficient_wq,
1277 &hub->leds, LED_CYCLE_PERIOD);
Alan Sternf2835212008-04-28 11:07:17 -04001278
1279 /* Scan all ports that need attention */
Petr Mladek32a69582014-09-19 17:32:21 +02001280 kick_hub_wq(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001281
1282 /* Allow autosuspend if it was suppressed */
1283 if (type <= HUB_INIT3)
1284 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Sterne50293e2015-12-16 13:32:38 -05001285
1286 if (type == HUB_INIT2 || type == HUB_INIT3)
1287 device_unlock(hub->intfdev);
1288
1289 kref_put(&hub->kref, hub_release);
Alan Stern5e6effa2008-03-03 15:15:51 -05001290}
1291
Alan Stern8520f382008-09-22 14:44:26 -04001292/* Implement the continuations for the delays above */
1293static void hub_init_func2(struct work_struct *ws)
1294{
1295 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1296
1297 hub_activate(hub, HUB_INIT2);
1298}
1299
1300static void hub_init_func3(struct work_struct *ws)
1301{
1302 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1303
1304 hub_activate(hub, HUB_INIT3);
1305}
1306
Alan Stern43303542008-04-28 11:07:31 -04001307enum hub_quiescing_type {
1308 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1309};
1310
1311static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1312{
1313 struct usb_device *hdev = hub->hdev;
1314 int i;
1315
Alan Stern8520f382008-09-22 14:44:26 -04001316 cancel_delayed_work_sync(&hub->init_work);
1317
Petr Mladek37ebb542014-09-19 17:32:23 +02001318 /* hub_wq and related activity won't re-trigger */
Alan Stern43303542008-04-28 11:07:31 -04001319 hub->quiescing = 1;
1320
1321 if (type != HUB_SUSPEND) {
1322 /* Disconnect all the children */
1323 for (i = 0; i < hdev->maxchild; ++i) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001324 if (hub->ports[i]->child)
1325 usb_disconnect(&hub->ports[i]->child);
Alan Stern43303542008-04-28 11:07:31 -04001326 }
1327 }
1328
Petr Mladek37ebb542014-09-19 17:32:23 +02001329 /* Stop hub_wq and related activity */
Alan Stern43303542008-04-28 11:07:31 -04001330 usb_kill_urb(hub->urb);
1331 if (hub->has_indicators)
1332 cancel_delayed_work_sync(&hub->leds);
1333 if (hub->tt.hub)
Octavian Purdila036546b2012-10-23 11:33:12 +03001334 flush_work(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001335}
1336
Alan Stern600856c2014-05-20 18:08:07 -07001337static void hub_pm_barrier_for_all_ports(struct usb_hub *hub)
1338{
1339 int i;
1340
1341 for (i = 0; i < hub->hdev->maxchild; ++i)
1342 pm_runtime_barrier(&hub->ports[i]->dev);
1343}
1344
Alan Stern3eb14912008-03-03 15:15:43 -05001345/* caller has locked the hub device */
1346static int hub_pre_reset(struct usb_interface *intf)
1347{
1348 struct usb_hub *hub = usb_get_intfdata(intf);
1349
Alan Stern43303542008-04-28 11:07:31 -04001350 hub_quiesce(hub, HUB_PRE_RESET);
Alan Stern600856c2014-05-20 18:08:07 -07001351 hub->in_reset = 1;
1352 hub_pm_barrier_for_all_ports(hub);
Alan Sternf07600c2007-05-30 15:38:16 -04001353 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001354}
1355
1356/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001357static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001358{
Alan Stern7de18d82006-06-01 13:37:24 -04001359 struct usb_hub *hub = usb_get_intfdata(intf);
1360
Alan Stern600856c2014-05-20 18:08:07 -07001361 hub->in_reset = 0;
1362 hub_pm_barrier_for_all_ports(hub);
Alan Sternf2835212008-04-28 11:07:17 -04001363 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001364 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001365}
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367static int hub_configure(struct usb_hub *hub,
1368 struct usb_endpoint_descriptor *endpoint)
1369{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001370 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 struct usb_device *hdev = hub->hdev;
1372 struct device *hub_dev = hub->intfdev;
1373 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001374 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 unsigned int pipe;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001376 int maxp, ret, i;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001377 char *message = "out of memory";
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001378 unsigned unit_load;
1379 unsigned full_load;
Dan Williamsd8521af2014-05-20 18:08:28 -07001380 unsigned maxchild;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Alan Sternd697cdd2009-10-27 15:18:46 -04001382 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 ret = -ENOMEM;
1385 goto fail;
1386 }
1387
1388 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1389 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 ret = -ENOMEM;
1391 goto fail;
1392 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001393 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1396 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 ret = -ENOMEM;
1398 goto fail;
1399 }
1400
1401 /* Request the entire hub descriptor.
1402 * hub->descriptor can handle USB_MAXCHILDREN ports,
1403 * but the hub can/will return fewer bytes here.
1404 */
John Youndbe79bb2001-09-17 00:00:00 -07001405 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (ret < 0) {
1407 message = "can't read hub descriptor";
1408 goto fail;
1409 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1410 message = "hub has too many ports!";
1411 ret = -ENODEV;
1412 goto fail;
David Linares769d73682013-03-25 10:50:27 +00001413 } else if (hub->descriptor->bNbrPorts == 0) {
1414 message = "hub doesn't have any ports!";
1415 ret = -ENODEV;
1416 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418
Dan Williamsd8521af2014-05-20 18:08:28 -07001419 maxchild = hub->descriptor->bNbrPorts;
1420 dev_info(hub_dev, "%d port%s detected\n", maxchild,
1421 (maxchild == 1) ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Dan Williamsd8521af2014-05-20 18:08:28 -07001423 hub->ports = kzalloc(maxchild * sizeof(struct usb_port *), GFP_KERNEL);
Lan Tianyuff823c792012-09-05 13:44:32 +08001424 if (!hub->ports) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001425 ret = -ENOMEM;
1426 goto fail;
1427 }
1428
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001429 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001430 if (hub_is_superspeed(hdev)) {
1431 unit_load = 150;
1432 full_load = 900;
1433 } else {
1434 unit_load = 100;
1435 full_load = 500;
1436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
John Youndbe79bb2001-09-17 00:00:00 -07001438 /* FIXME for USB 3.0, skip for now */
1439 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1440 !(hub_is_superspeed(hdev))) {
Matthias Beyer469271f2013-10-10 23:41:27 +02001441 char portstr[USB_MAXCHILDREN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Dan Williamsd8521af2014-05-20 18:08:28 -07001443 for (i = 0; i < maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001444 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1446 ? 'F' : 'R';
Dan Williamsd8521af2014-05-20 18:08:28 -07001447 portstr[maxchild] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1449 } else
1450 dev_dbg(hub_dev, "standalone hub\n");
1451
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001452 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301453 case HUB_CHAR_COMMON_LPSM:
1454 dev_dbg(hub_dev, "ganged power switching\n");
1455 break;
1456 case HUB_CHAR_INDV_PORT_LPSM:
1457 dev_dbg(hub_dev, "individual port power switching\n");
1458 break;
1459 case HUB_CHAR_NO_LPSM:
1460 case HUB_CHAR_LPSM:
1461 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001465 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301466 case HUB_CHAR_COMMON_OCPM:
1467 dev_dbg(hub_dev, "global over-current protection\n");
1468 break;
1469 case HUB_CHAR_INDV_PORT_OCPM:
1470 dev_dbg(hub_dev, "individual port over-current protection\n");
1471 break;
1472 case HUB_CHAR_NO_OCPM:
1473 case HUB_CHAR_OCPM:
1474 dev_dbg(hub_dev, "no over-current protection\n");
1475 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
1477
Chase Metzger17248562015-08-11 21:34:37 -07001478 spin_lock_init(&hub->tt.lock);
1479 INIT_LIST_HEAD(&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001480 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301482 case USB_HUB_PR_FS:
1483 break;
1484 case USB_HUB_PR_HS_SINGLE_TT:
1485 dev_dbg(hub_dev, "Single TT\n");
1486 hub->tt.hub = hdev;
1487 break;
1488 case USB_HUB_PR_HS_MULTI_TT:
1489 ret = usb_set_interface(hdev, 0, 1);
1490 if (ret == 0) {
1491 dev_dbg(hub_dev, "TT per port\n");
1492 hub->tt.multi = 1;
1493 } else
1494 dev_err(hub_dev, "Using single TT (err %d)\n",
1495 ret);
1496 hub->tt.hub = hdev;
1497 break;
1498 case USB_HUB_PR_SS:
1499 /* USB 3.0 hubs don't have a TT */
1500 break;
1501 default:
1502 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1503 hdev->descriptor.bDeviceProtocol);
1504 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001507 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001508 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
Matthias Beyer469271f2013-10-10 23:41:27 +02001509 case HUB_TTTT_8_BITS:
1510 if (hdev->descriptor.bDeviceProtocol != 0) {
1511 hub->tt.think_time = 666;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001512 dev_dbg(hub_dev, "TT requires at most %d "
1513 "FS bit times (%d ns)\n",
Matthias Beyer469271f2013-10-10 23:41:27 +02001514 8, hub->tt.think_time);
1515 }
1516 break;
1517 case HUB_TTTT_16_BITS:
1518 hub->tt.think_time = 666 * 2;
1519 dev_dbg(hub_dev, "TT requires at most %d "
1520 "FS bit times (%d ns)\n",
1521 16, hub->tt.think_time);
1522 break;
1523 case HUB_TTTT_24_BITS:
1524 hub->tt.think_time = 666 * 3;
1525 dev_dbg(hub_dev, "TT requires at most %d "
1526 "FS bit times (%d ns)\n",
1527 24, hub->tt.think_time);
1528 break;
1529 case HUB_TTTT_32_BITS:
1530 hub->tt.think_time = 666 * 4;
1531 dev_dbg(hub_dev, "TT requires at most %d "
1532 "FS bit times (%d ns)\n",
1533 32, hub->tt.think_time);
1534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
1536
1537 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001538 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 hub->has_indicators = 1;
1540 dev_dbg(hub_dev, "Port indicators are supported\n");
1541 }
1542
1543 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1544 hub->descriptor->bPwrOn2PwrGood * 2);
1545
1546 /* power budgeting mostly matters with bus-powered hubs,
1547 * and battery-powered root hubs (may provide just 8 mA).
1548 */
1549 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern15b73362013-07-30 15:35:40 -04001550 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 message = "can't get hub status";
1552 goto fail;
1553 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001554 hcd = bus_to_hcd(hdev->bus);
Alan Stern7d35b922005-04-25 11:18:32 -04001555 if (hdev == hdev->bus->root_hub) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001556 if (hcd->power_budget > 0)
1557 hdev->bus_mA = hcd->power_budget;
1558 else
Dan Williamsd8521af2014-05-20 18:08:28 -07001559 hdev->bus_mA = full_load * maxchild;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001560 if (hdev->bus_mA >= full_load)
1561 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001562 else {
1563 hub->mA_per_port = hdev->bus_mA;
1564 hub->limited_power = 1;
1565 }
Alan Stern7d35b922005-04-25 11:18:32 -04001566 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001567 int remaining = hdev->bus_mA -
1568 hub->descriptor->bHubContrCurrent;
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1571 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001572 hub->limited_power = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Dan Williamsd8521af2014-05-20 18:08:28 -07001574 if (remaining < maxchild * unit_load)
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001575 dev_warn(hub_dev,
Alan Stern55c52712005-11-23 12:03:12 -05001576 "insufficient power available "
1577 "to use all downstream ports\n");
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001578 hub->mA_per_port = unit_load; /* 7.2.1 */
1579
Alan Stern55c52712005-11-23 12:03:12 -05001580 } else { /* Self-powered external hub */
1581 /* FIXME: What about battery-powered external hubs that
1582 * provide less current per port? */
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001583 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001584 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001585 if (hub->mA_per_port < full_load)
Alan Stern55c52712005-11-23 12:03:12 -05001586 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1587 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1590 if (ret < 0) {
1591 message = "can't get hub status";
1592 goto fail;
1593 }
1594
1595 /* local power status reports aren't always correct */
1596 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1597 dev_dbg(hub_dev, "local power source is %s\n",
1598 (hubstatus & HUB_STATUS_LOCAL_POWER)
1599 ? "lost (inactive)" : "good");
1600
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001601 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 dev_dbg(hub_dev, "%sover-current condition exists\n",
1603 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1604
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001605 /* set up the interrupt endpoint
1606 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1607 * bytes as USB2.0[11.12.3] says because some hubs are known
1608 * to send more data (and thus cause overflow). For root hubs,
1609 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1610 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1612 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1613
1614 if (maxp > sizeof(*hub->buffer))
1615 maxp = sizeof(*hub->buffer);
1616
1617 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1618 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 ret = -ENOMEM;
1620 goto fail;
1621 }
1622
1623 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1624 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 /* maybe cycle the hub leds */
1627 if (hub->has_indicators && blinkenlights)
Matthias Beyer469271f2013-10-10 23:41:27 +02001628 hub->indicator[0] = INDICATOR_CYCLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Dan Williamsd8521af2014-05-20 18:08:28 -07001630 mutex_lock(&usb_port_peer_mutex);
1631 for (i = 0; i < maxchild; i++) {
Krzysztof Mazure58547e2013-08-22 14:49:39 +02001632 ret = usb_hub_create_port_device(hub, i + 1);
1633 if (ret < 0) {
Lan Tianyufa2a9562012-09-05 13:44:31 +08001634 dev_err(hub->intfdev,
1635 "couldn't create port%d device.\n", i + 1);
Dan Williamsd8521af2014-05-20 18:08:28 -07001636 break;
Krzysztof Mazure58547e2013-08-22 14:49:39 +02001637 }
1638 }
Dan Williamsd8521af2014-05-20 18:08:28 -07001639 hdev->maxchild = i;
Dan Williamse3d10502014-06-17 16:16:32 -07001640 for (i = 0; i < hdev->maxchild; i++) {
1641 struct usb_port *port_dev = hub->ports[i];
1642
1643 pm_runtime_put(&port_dev->dev);
1644 }
1645
Dan Williamsd8521af2014-05-20 18:08:28 -07001646 mutex_unlock(&usb_port_peer_mutex);
1647 if (ret < 0)
1648 goto fail;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001649
Petr Mladek37ebb542014-09-19 17:32:23 +02001650 /* Update the HCD's internal representation of this hub before hub_wq
Dan Williamse3d95582014-06-05 14:23:04 -07001651 * starts getting port status changes for devices under the hub.
1652 */
1653 if (hcd->driver->update_hub_device) {
1654 ret = hcd->driver->update_hub_device(hcd, hdev,
1655 &hub->tt, GFP_KERNEL);
1656 if (ret < 0) {
1657 message = "can't update HCD hub info";
1658 goto fail;
1659 }
1660 }
1661
Lan Tianyud2123fd2013-01-21 22:18:00 +08001662 usb_hub_adjust_deviceremovable(hdev, hub->descriptor);
1663
Alan Sternf2835212008-04-28 11:07:17 -04001664 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return 0;
1666
1667fail:
Chase Metzger17248562015-08-11 21:34:37 -07001668 dev_err(hub_dev, "config failed, %s (err %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 message, ret);
1670 /* hub_disconnect() frees urb and descriptor */
1671 return ret;
1672}
1673
Alan Sterne8054852007-05-04 11:55:11 -04001674static void hub_release(struct kref *kref)
1675{
1676 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1677
Petr Mladek5d14f322014-09-19 17:32:19 +02001678 usb_put_dev(hub->hdev);
Alan Sterne8054852007-05-04 11:55:11 -04001679 usb_put_intf(to_usb_interface(hub->intfdev));
1680 kfree(hub);
1681}
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683static unsigned highspeed_hubs;
1684
1685static void hub_disconnect(struct usb_interface *intf)
1686{
Huajun Li88162302012-03-12 21:00:19 +08001687 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001688 struct usb_device *hdev = interface_to_usbdev(intf);
Alan Stern543d7782014-01-07 10:43:02 -05001689 int port1;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001690
Petr Mladek32a69582014-09-19 17:32:21 +02001691 /*
1692 * Stop adding new hub events. We do not want to block here and thus
1693 * will not try to remove any pending work item.
1694 */
Alan Sterne8054852007-05-04 11:55:11 -04001695 hub->disconnected = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Alan Stern7de18d82006-06-01 13:37:24 -04001697 /* Disconnect all children and quiesce the hub */
1698 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001699 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001700
Dan Williamsd8521af2014-05-20 18:08:28 -07001701 mutex_lock(&usb_port_peer_mutex);
1702
Alan Stern543d7782014-01-07 10:43:02 -05001703 /* Avoid races with recursively_mark_NOTATTACHED() */
1704 spin_lock_irq(&device_state_lock);
1705 port1 = hdev->maxchild;
1706 hdev->maxchild = 0;
1707 usb_set_intfdata(intf, NULL);
1708 spin_unlock_irq(&device_state_lock);
Alexander Shishkin1f2235b2012-09-12 14:48:31 +03001709
Alan Stern543d7782014-01-07 10:43:02 -05001710 for (; port1 > 0; --port1)
1711 usb_hub_remove_port_device(hub, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Dan Williamsd8521af2014-05-20 18:08:28 -07001713 mutex_unlock(&usb_port_peer_mutex);
1714
Alan Sterne8054852007-05-04 11:55:11 -04001715 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 highspeed_hubs--;
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 usb_free_urb(hub->urb);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001719 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001720 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001721 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001722 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Lan Tianyu971fcd42013-01-23 04:26:29 +08001724 pm_suspend_ignore_children(&intf->dev, false);
Alan Sterne8054852007-05-04 11:55:11 -04001725 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
1728static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1729{
1730 struct usb_host_interface *desc;
1731 struct usb_endpoint_descriptor *endpoint;
1732 struct usb_device *hdev;
1733 struct usb_hub *hub;
1734
1735 desc = intf->cur_altsetting;
1736 hdev = interface_to_usbdev(intf);
1737
Ming Lei596d7892012-10-24 11:59:25 +08001738 /*
1739 * Set default autosuspend delay as 0 to speedup bus suspend,
1740 * based on the below considerations:
1741 *
1742 * - Unlike other drivers, the hub driver does not rely on the
1743 * autosuspend delay to provide enough time to handle a wakeup
1744 * event, and the submitted status URB is just to check future
1745 * change on hub downstream ports, so it is safe to do it.
1746 *
1747 * - The patch might cause one or more auto supend/resume for
1748 * below very rare devices when they are plugged into hub
1749 * first time:
1750 *
1751 * devices having trouble initializing, and disconnect
1752 * themselves from the bus and then reconnect a second
1753 * or so later
1754 *
1755 * devices just for downloading firmware, and disconnects
1756 * themselves after completing it
1757 *
1758 * For these quite rare devices, their drivers may change the
1759 * autosuspend delay of their parent hub in the probe() to one
1760 * appropriate value to avoid the subtle problem if someone
1761 * does care it.
1762 *
1763 * - The patch may cause one or more auto suspend/resume on
1764 * hub during running 'lsusb', but it is probably too
1765 * infrequent to worry about.
1766 *
1767 * - Change autosuspend delay of hub can avoid unnecessary auto
1768 * suspend timer for hub, also may decrease power consumption
1769 * of USB bus.
Roger Quadrosbdd405d2014-08-04 12:44:46 +03001770 *
1771 * - If user has indicated to prevent autosuspend by passing
1772 * usbcore.autosuspend = -1 then keep autosuspend disabled.
Ming Lei596d7892012-10-24 11:59:25 +08001773 */
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01001774#ifdef CONFIG_PM
Roger Quadrosbdd405d2014-08-04 12:44:46 +03001775 if (hdev->dev.power.autosuspend_delay >= 0)
1776 pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
Greg Kroah-Hartmana9ef8032014-08-27 16:55:29 -07001777#endif
Ming Lei596d7892012-10-24 11:59:25 +08001778
Alan Stern8ef42dd2014-05-23 10:45:54 -04001779 /*
1780 * Hubs have proper suspend/resume support, except for root hubs
1781 * where the controller driver doesn't have bus_suspend and
1782 * bus_resume methods.
1783 */
1784 if (hdev->parent) { /* normal device */
1785 usb_enable_autosuspend(hdev);
1786 } else { /* root hub */
1787 const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver;
1788
1789 if (drv->bus_suspend && drv->bus_resume)
1790 usb_enable_autosuspend(hdev);
1791 }
Alan Stern088f7fe2010-01-08 12:56:54 -05001792
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001793 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001794 dev_err(&intf->dev,
1795 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001796 return -E2BIG;
1797 }
1798
David Brownell89ccbdc2006-04-02 10:18:09 -08001799#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1800 if (hdev->parent) {
1801 dev_warn(&intf->dev, "ignoring external hub\n");
1802 return -ENODEV;
1803 }
1804#endif
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 /* Some hubs have a subclass of 1, which AFAICT according to the */
1807 /* specs is not defined, but it works */
1808 if ((desc->desc.bInterfaceSubClass != 0) &&
1809 (desc->desc.bInterfaceSubClass != 1)) {
1810descriptor_error:
Chase Metzger17248562015-08-11 21:34:37 -07001811 dev_err(&intf->dev, "bad descriptor, ignoring hub\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return -EIO;
1813 }
1814
1815 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1816 if (desc->desc.bNumEndpoints != 1)
1817 goto descriptor_error;
1818
1819 endpoint = &desc->endpoint[0].desc;
1820
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001821 /* If it's not an interrupt in endpoint, we'd better punt! */
1822 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 goto descriptor_error;
1824
1825 /* We found a hub */
Chase Metzger17248562015-08-11 21:34:37 -07001826 dev_info(&intf->dev, "USB hub found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001828 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (!hub) {
Chase Metzger17248562015-08-11 21:34:37 -07001830 dev_dbg(&intf->dev, "couldn't kmalloc hub struct\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return -ENOMEM;
1832 }
1833
Alan Sterne8054852007-05-04 11:55:11 -04001834 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 hub->intfdev = &intf->dev;
1836 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001837 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001838 INIT_DELAYED_WORK(&hub->init_work, NULL);
Petr Mladek32a69582014-09-19 17:32:21 +02001839 INIT_WORK(&hub->events, hub_event);
Alan Sterne8054852007-05-04 11:55:11 -04001840 usb_get_intf(intf);
Petr Mladek5d14f322014-09-19 17:32:19 +02001841 usb_get_dev(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Chase Metzger17248562015-08-11 21:34:37 -07001843 usb_set_intfdata(intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001844 intf->needs_remote_wakeup = 1;
Lan Tianyu971fcd42013-01-23 04:26:29 +08001845 pm_suspend_ignore_children(&intf->dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 if (hdev->speed == USB_SPEED_HIGH)
1848 highspeed_hubs++;
1849
Ming Leie6f30de2012-10-24 11:59:24 +08001850 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1851 hub->quirk_check_port_auto_suspend = 1;
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (hub_configure(hub, endpoint) >= 0)
1854 return 0;
1855
Chase Metzger17248562015-08-11 21:34:37 -07001856 hub_disconnect(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return -ENODEV;
1858}
1859
1860static int
1861hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1862{
Chase Metzger17248562015-08-11 21:34:37 -07001863 struct usb_device *hdev = interface_to_usbdev(intf);
Lan Tianyuad493e52013-01-23 04:26:30 +08001864 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 /* assert ifno == 0 (part of hub spec) */
1867 switch (code) {
1868 case USBDEVFS_HUB_PORTINFO: {
1869 struct usbdevfs_hub_portinfo *info = user_data;
1870 int i;
1871
1872 spin_lock_irq(&device_state_lock);
1873 if (hdev->devnum <= 0)
1874 info->nports = 0;
1875 else {
1876 info->nports = hdev->maxchild;
1877 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001878 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 info->port[i] = 0;
1880 else
1881 info->port[i] =
Lan Tianyuff823c792012-09-05 13:44:32 +08001882 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
1884 }
1885 spin_unlock_irq(&device_state_lock);
1886
1887 return info->nports + 1;
1888 }
1889
1890 default:
1891 return -ENOSYS;
1892 }
1893}
1894
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001895/*
1896 * Allow user programs to claim ports on a hub. When a device is attached
1897 * to one of these "claimed" ports, the program will "own" the device.
1898 */
1899static int find_port_owner(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001900 struct usb_dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001901{
Mathias Nyman413412612013-06-18 17:28:48 +03001902 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
1903
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001904 if (hdev->state == USB_STATE_NOTATTACHED)
1905 return -ENODEV;
1906 if (port1 == 0 || port1 > hdev->maxchild)
1907 return -EINVAL;
1908
Mathias Nyman413412612013-06-18 17:28:48 +03001909 /* Devices not managed by the hub driver
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001910 * will always have maxchild equal to 0.
1911 */
Mathias Nyman413412612013-06-18 17:28:48 +03001912 *ppowner = &(hub->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001913 return 0;
1914}
1915
1916/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001917int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001918 struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001919{
1920 int rc;
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001921 struct usb_dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001922
1923 rc = find_port_owner(hdev, port1, &powner);
1924 if (rc)
1925 return rc;
1926 if (*powner)
1927 return -EBUSY;
1928 *powner = owner;
1929 return rc;
1930}
Valentina Manea6080cd02014-03-08 14:53:34 +02001931EXPORT_SYMBOL_GPL(usb_hub_claim_port);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001932
Lan Tianyu336c5c32012-07-06 14:13:52 +08001933int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001934 struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001935{
1936 int rc;
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001937 struct usb_dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001938
1939 rc = find_port_owner(hdev, port1, &powner);
1940 if (rc)
1941 return rc;
1942 if (*powner != owner)
1943 return -ENOENT;
1944 *powner = NULL;
1945 return rc;
1946}
Valentina Manea6080cd02014-03-08 14:53:34 +02001947EXPORT_SYMBOL_GPL(usb_hub_release_port);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001948
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001949void usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001950{
Lan Tianyuad493e52013-01-23 04:26:30 +08001951 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001952 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001953
Lan Tianyufa2a9562012-09-05 13:44:31 +08001954 for (n = 0; n < hdev->maxchild; n++) {
1955 if (hub->ports[n]->port_owner == owner)
1956 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001957 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001958
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001959}
1960
1961/* The caller must hold udev's lock */
1962bool usb_device_is_owned(struct usb_device *udev)
1963{
1964 struct usb_hub *hub;
1965
1966 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1967 return false;
Lan Tianyuad493e52013-01-23 04:26:30 +08001968 hub = usb_hub_to_struct_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001969 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001970}
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1973{
Lan Tianyuad493e52013-01-23 04:26:30 +08001974 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 int i;
1976
1977 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001978 if (hub->ports[i]->child)
1979 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001981 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001982 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 udev->state = USB_STATE_NOTATTACHED;
1984}
1985
1986/**
1987 * usb_set_device_state - change a device's current state (usbcore, hcds)
1988 * @udev: pointer to device whose state should be changed
1989 * @new_state: new state value to be stored
1990 *
1991 * udev->state is _not_ fully protected by the device lock. Although
1992 * most transitions are made only while holding the lock, the state can
1993 * can change to USB_STATE_NOTATTACHED at almost any time. This
1994 * is so that devices can be marked as disconnected as soon as possible,
1995 * without having to wait for any semaphores to be released. As a result,
1996 * all changes to any device's state must be protected by the
1997 * device_state_lock spinlock.
1998 *
1999 * Once a device has been added to the device tree, all changes to its state
2000 * should be made using this routine. The state should _not_ be set directly.
2001 *
2002 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
2003 * Otherwise udev->state is set to new_state, and if new_state is
2004 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
2005 * to USB_STATE_NOTATTACHED.
2006 */
2007void usb_set_device_state(struct usb_device *udev,
2008 enum usb_device_state new_state)
2009{
2010 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01002011 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 spin_lock_irqsave(&device_state_lock, flags);
2014 if (udev->state == USB_STATE_NOTATTACHED)
2015 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07002016 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08002017
2018 /* root hub wakeup capabilities are managed out-of-band
2019 * and may involve silicon errata ... ignore them here.
2020 */
2021 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04002022 if (udev->state == USB_STATE_SUSPENDED
2023 || new_state == USB_STATE_SUSPENDED)
2024 ; /* No change to wakeup settings */
2025 else if (new_state == USB_STATE_CONFIGURED)
Lu Baoluddbe1fc2014-09-19 10:13:50 +08002026 wakeup = (udev->quirks &
2027 USB_QUIRK_IGNORE_REMOTE_WAKEUP) ? 0 :
2028 udev->actconfig->desc.bmAttributes &
2029 USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04002030 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01002031 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08002032 }
Sarah Sharp15123002007-12-21 16:54:15 -08002033 if (udev->state == USB_STATE_SUSPENDED &&
2034 new_state != USB_STATE_SUSPENDED)
2035 udev->active_duration -= jiffies;
2036 else if (new_state == USB_STATE_SUSPENDED &&
2037 udev->state != USB_STATE_SUSPENDED)
2038 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04002039 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07002040 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 recursively_mark_NOTATTACHED(udev);
2042 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01002043 if (wakeup >= 0)
2044 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
David Vrabel6da9c992009-02-18 14:43:47 +00002046EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002048/*
Alan Stern3b29b682011-02-22 09:53:41 -05002049 * Choose a device number.
2050 *
2051 * Device numbers are used as filenames in usbfs. On USB-1.1 and
2052 * USB-2.0 buses they are also used as device addresses, however on
2053 * USB-3.0 buses the address is assigned by the controller hardware
2054 * and it usually is not the same as the device number.
2055 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002056 * WUSB devices are simple: they have no hubs behind, so the mapping
2057 * device <-> virtual port number becomes 1:1. Why? to simplify the
2058 * life of the device connection logic in
2059 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
2060 * handshake we need to assign a temporary address in the unauthorized
2061 * space. For simplicity we use the first virtual port number found to
2062 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
2063 * and that becomes it's address [X < 128] or its unauthorized address
2064 * [X | 0x80].
2065 *
2066 * We add 1 as an offset to the one-based USB-stack port number
2067 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
2068 * 0 is reserved by USB for default address; (b) Linux's USB stack
2069 * uses always #1 for the root hub of the controller. So USB stack's
2070 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07002071 *
2072 * Devices connected under xHCI are not as simple. The host controller
2073 * supports virtualization, so the hardware assigns device addresses and
2074 * the HCD must setup data structures before issuing a set address
2075 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002076 */
Alan Stern3b29b682011-02-22 09:53:41 -05002077static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
2079 int devnum;
2080 struct usb_bus *bus = udev->bus;
2081
Petr Mladek638139e2014-09-19 17:32:24 +02002082 /* be safe when more hub events are proceed in parallel */
2083 mutex_lock(&bus->usb_address0_mutex);
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002084 if (udev->wusb) {
2085 devnum = udev->portnum + 1;
2086 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
2087 } else {
2088 /* Try to allocate the next devnum beginning at
2089 * bus->devnum_next. */
2090 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
2091 bus->devnum_next);
2092 if (devnum >= 128)
2093 devnum = find_next_zero_bit(bus->devmap.devicemap,
2094 128, 1);
Matthias Beyer469271f2013-10-10 23:41:27 +02002095 bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1);
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (devnum < 128) {
2098 set_bit(devnum, bus->devmap.devicemap);
2099 udev->devnum = devnum;
2100 }
Petr Mladek638139e2014-09-19 17:32:24 +02002101 mutex_unlock(&bus->usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
Alan Stern3b29b682011-02-22 09:53:41 -05002104static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
2106 if (udev->devnum > 0) {
2107 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
2108 udev->devnum = -1;
2109 }
2110}
2111
Alan Stern3b29b682011-02-22 09:53:41 -05002112static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07002113{
2114 /* The address for a WUSB device is managed by wusbcore. */
2115 if (!udev->wusb)
2116 udev->devnum = devnum;
2117}
2118
Herbert Xuf7410ce2010-01-10 20:15:03 +11002119static void hub_free_dev(struct usb_device *udev)
2120{
2121 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2122
2123 /* Root hubs aren't real devices, so don't free HCD resources */
2124 if (hcd->driver->free_dev && udev->parent)
2125 hcd->driver->free_dev(hcd, udev);
2126}
2127
Dan Williams7027df32014-05-20 18:09:36 -07002128static void hub_disconnect_children(struct usb_device *udev)
2129{
2130 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
2131 int i;
2132
2133 /* Free up all the children before we remove this device */
2134 for (i = 0; i < udev->maxchild; i++) {
2135 if (hub->ports[i]->child)
2136 usb_disconnect(&hub->ports[i]->child);
2137 }
2138}
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140/**
2141 * usb_disconnect - disconnect a device (usbcore-internal)
2142 * @pdev: pointer to device being disconnected
2143 * Context: !in_interrupt ()
2144 *
2145 * Something got disconnected. Get rid of it and all of its children.
2146 *
2147 * If *pdev is a normal device then the parent hub must already be locked.
Bjorn Helgaasdb8f2aa2013-09-13 13:57:34 -06002148 * If *pdev is a root hub then the caller must hold the usb_bus_list_lock,
2149 * which protects the set of root hubs as well as the list of buses.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 *
2151 * Only hub drivers (including virtual root hub drivers for host
2152 * controllers) should ever call this.
2153 *
2154 * This call is synchronous, and may not be used in an interrupt context.
2155 */
2156void usb_disconnect(struct usb_device **pdev)
2157{
Dan Williams7027df32014-05-20 18:09:36 -07002158 struct usb_port *port_dev = NULL;
2159 struct usb_device *udev = *pdev;
Peter Chen5b1dc202014-08-19 08:56:21 +08002160 struct usb_hub *hub = NULL;
2161 int port1 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 /* mark the device as inactive, so any further urb submissions for
2164 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002165 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 */
2167 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05002168 dev_info(&udev->dev, "USB disconnect, device number %d\n",
2169 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002171 usb_lock_device(udev);
2172
Dan Williams7027df32014-05-20 18:09:36 -07002173 hub_disconnect_children(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 /* deallocate hcd/hardware state ... nuking all pending urbs and
2176 * cleaning up all state associated with the current configuration
2177 * so that the hardware is now fully quiesced.
2178 */
Chase Metzger17248562015-08-11 21:34:37 -07002179 dev_dbg(&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04002181 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Lan Tianyufde26382013-01-20 01:53:33 +08002183 if (udev->parent) {
Dan Williams7027df32014-05-20 18:09:36 -07002184 port1 = udev->portnum;
2185 hub = usb_hub_to_struct_hub(udev->parent);
2186 port_dev = hub->ports[port1 - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002187
2188 sysfs_remove_link(&udev->dev.kobj, "port");
2189 sysfs_remove_link(&port_dev->dev.kobj, "device");
Lan Tianyu971fcd42013-01-23 04:26:29 +08002190
Dan Williams7027df32014-05-20 18:09:36 -07002191 /*
2192 * As usb_port_runtime_resume() de-references udev, make
2193 * sure no resumes occur during removal
2194 */
2195 if (!test_and_set_bit(port1, hub->child_usage_bits))
2196 pm_runtime_get_sync(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002197 }
2198
Alan Stern3b23dd62008-12-05 14:10:34 -05002199 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04002200 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07002201
Alan Stern782da722006-07-01 22:09:35 -04002202 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002203 * for de-configuring the device and invoking the remove-device
2204 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002205 */
2206 device_del(&udev->dev);
2207
2208 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 * (or root_hub) pointer.
2210 */
Alan Stern3b29b682011-02-22 09:53:41 -05002211 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 /* Avoid races with recursively_mark_NOTATTACHED() */
2214 spin_lock_irq(&device_state_lock);
2215 *pdev = NULL;
2216 spin_unlock_irq(&device_state_lock);
2217
Dan Williams7027df32014-05-20 18:09:36 -07002218 if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits))
2219 pm_runtime_put(&port_dev->dev);
2220
Herbert Xuf7410ce2010-01-10 20:15:03 +11002221 hub_free_dev(udev);
2222
Alan Stern782da722006-07-01 22:09:35 -04002223 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002226#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227static void show_string(struct usb_device *udev, char *id, char *string)
2228{
2229 if (!string)
2230 return;
Joe Perchesf2ec5222012-10-28 01:05:51 -07002231 dev_info(&udev->dev, "%s: %s\n", id, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002234static void announce_device(struct usb_device *udev)
2235{
2236 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2237 le16_to_cpu(udev->descriptor.idVendor),
2238 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002239 dev_info(&udev->dev,
2240 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002241 udev->descriptor.iManufacturer,
2242 udev->descriptor.iProduct,
2243 udev->descriptor.iSerialNumber);
2244 show_string(udev, "Product", udev->product);
2245 show_string(udev, "Manufacturer", udev->manufacturer);
2246 show_string(udev, "SerialNumber", udev->serial);
2247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002249static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250#endif
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Oliver Neukum3ede7602007-01-11 14:35:50 +01002253/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002254 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002255 * @udev: newly addressed device (in ADDRESS state)
2256 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002257 * Finish enumeration for On-The-Go devices
Yacine Belkadi626f0902013-08-02 20:10:04 +02002258 *
2259 * Return: 0 if successful. A negative error code otherwise.
Oliver Neukum3ede7602007-01-11 14:35:50 +01002260 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002261static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002263 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265#ifdef CONFIG_USB_OTG
2266 /*
2267 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2268 * to wake us after we've powered off VBUS; and HNP, switching roles
2269 * "host" to "peripheral". The OTG descriptor helps figure this out.
2270 */
2271 if (!udev->bus->is_b_host
2272 && udev->config
2273 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002274 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 struct usb_bus *bus = udev->bus;
Li Jun7d2d6412015-08-31 16:20:49 +08002276 unsigned port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 /* descriptor may appear anywhere in config */
Li Jun7d2d6412015-08-31 16:20:49 +08002279 err = __usb_get_extra_descriptor(udev->rawdescriptors[0],
2280 le16_to_cpu(udev->config[0].desc.wTotalLength),
2281 USB_DT_OTG, (void **) &desc);
2282 if (err || !(desc->bmAttributes & USB_OTG_HNP))
2283 return 0;
David Brownellfc849b82006-09-18 16:53:26 -07002284
Li Jun7d2d6412015-08-31 16:20:49 +08002285 dev_info(&udev->dev, "Dual-Role OTG device on %sHNP port\n",
2286 (port1 == bus->otg_port) ? "" : "non-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Li Jun7d2d6412015-08-31 16:20:49 +08002288 /* enable HNP before suspend, it's simpler */
2289 if (port1 == bus->otg_port) {
2290 bus->b_hnp_enable = 1;
2291 err = usb_control_msg(udev,
2292 usb_sndctrlpipe(udev, 0),
2293 USB_REQ_SET_FEATURE, 0,
2294 USB_DEVICE_B_HNP_ENABLE,
2295 0, NULL, 0,
2296 USB_CTRL_SET_TIMEOUT);
2297 if (err < 0) {
2298 /*
2299 * OTG MESSAGE: report errors here,
2300 * customize to match your product.
2301 */
2302 dev_err(&udev->dev, "can't set HNP mode: %d\n",
2303 err);
2304 bus->b_hnp_enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
Li Jun7d2d6412015-08-31 16:20:49 +08002306 } else if (desc->bLength == sizeof
2307 (struct usb_otg_descriptor)) {
2308 /* Set a_alt_hnp_support for legacy otg device */
2309 err = usb_control_msg(udev,
2310 usb_sndctrlpipe(udev, 0),
2311 USB_REQ_SET_FEATURE, 0,
2312 USB_DEVICE_A_ALT_HNP_SUPPORT,
2313 0, NULL, 0,
2314 USB_CTRL_SET_TIMEOUT);
2315 if (err < 0)
2316 dev_err(&udev->dev,
2317 "set a_alt_hnp_support failed: %d\n",
2318 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002322 return err;
2323}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002325
2326/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002327 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002328 * @udev: newly addressed device (in ADDRESS state)
2329 *
2330 * This is only called by usb_new_device() and usb_authorize_device()
2331 * and FIXME -- all comments that apply to them apply here wrt to
2332 * environment.
2333 *
2334 * If the device is WUSB and not authorized, we don't attempt to read
2335 * the string descriptors, as they will be errored out by the device
2336 * until it has been authorized.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002337 *
2338 * Return: 0 if successful. A negative error code otherwise.
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002339 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002340static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002341{
2342 int err;
Peter Chen026f3fc2014-08-19 09:51:53 +08002343 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002344
2345 if (udev->config == NULL) {
2346 err = usb_get_configuration(udev);
2347 if (err < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04002348 if (err != -ENODEV)
2349 dev_err(&udev->dev, "can't read configurations, error %d\n",
2350 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002351 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002352 }
2353 }
Thomas Pugliese83e83ec2013-12-09 13:40:29 -06002354
2355 /* read the standard strings and cache them if present */
2356 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2357 udev->manufacturer = usb_cache_string(udev,
2358 udev->descriptor.iManufacturer);
2359 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2360
Alan Stern8d8558d2009-12-08 15:50:41 -05002361 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002362 if (err < 0)
2363 return err;
2364
Peter Chen026f3fc2014-08-19 09:51:53 +08002365 if (IS_ENABLED(CONFIG_USB_OTG_WHITELIST) && hcd->tpl_support &&
Peter Chene5a9d622014-09-29 10:09:31 +08002366 !is_targeted(udev)) {
Peter Chen026f3fc2014-08-19 09:51:53 +08002367 /* Maybe it can talk to us, though we can't talk to it.
2368 * (Includes HNP test device.)
2369 */
Peter Chene5a9d622014-09-29 10:09:31 +08002370 if (IS_ENABLED(CONFIG_USB_OTG) && (udev->bus->b_hnp_enable
2371 || udev->bus->is_b_host)) {
Peter Chen026f3fc2014-08-19 09:51:53 +08002372 err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND);
2373 if (err < 0)
2374 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2375 }
2376 return -ENOTSUPP;
2377 }
2378
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002379 usb_detect_interface_quirks(udev);
2380
2381 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002382}
2383
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002384static void set_usb_port_removable(struct usb_device *udev)
2385{
2386 struct usb_device *hdev = udev->parent;
2387 struct usb_hub *hub;
2388 u8 port = udev->portnum;
2389 u16 wHubCharacteristics;
2390 bool removable = true;
2391
2392 if (!hdev)
2393 return;
2394
Lan Tianyuad493e52013-01-23 04:26:30 +08002395 hub = usb_hub_to_struct_hub(udev->parent);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002396
Matthew Garrett92bfbf72015-04-08 16:36:00 -07002397 /*
2398 * If the platform firmware has provided information about a port,
2399 * use that to determine whether it's removable.
2400 */
2401 switch (hub->ports[udev->portnum - 1]->connect_type) {
2402 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
2403 udev->removable = USB_DEVICE_REMOVABLE;
2404 return;
2405 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
Matthew Garrett58339c22015-04-08 16:36:01 -07002406 case USB_PORT_NOT_USED:
Matthew Garrett92bfbf72015-04-08 16:36:00 -07002407 udev->removable = USB_DEVICE_FIXED;
2408 return;
Matthew Garrett58339c22015-04-08 16:36:01 -07002409 default:
2410 break;
Matthew Garrett92bfbf72015-04-08 16:36:00 -07002411 }
2412
2413 /*
2414 * Otherwise, check whether the hub knows whether a port is removable
2415 * or not
2416 */
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002417 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2418
2419 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2420 return;
2421
2422 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002423 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2424 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002425 removable = false;
2426 } else {
2427 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2428 removable = false;
2429 }
2430
2431 if (removable)
2432 udev->removable = USB_DEVICE_REMOVABLE;
2433 else
2434 udev->removable = USB_DEVICE_FIXED;
Dan Williamsa4204ff2014-05-20 18:08:22 -07002435
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002436}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002437
2438/**
2439 * usb_new_device - perform initial device setup (usbcore-internal)
2440 * @udev: newly addressed device (in ADDRESS state)
2441 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002442 * This is called with devices which have been detected but not fully
2443 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002444 * for any device configuration. The caller must have locked either
2445 * the parent hub (if udev is a normal device) or else the
2446 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2447 * udev has already been installed, but udev is not yet visible through
2448 * sysfs or other filesystem code.
2449 *
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002450 * This call is synchronous, and may not be used in an interrupt context.
2451 *
2452 * Only the hub driver or root-hub registrar should ever call this.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002453 *
2454 * Return: Whether the device is configured properly or not. Zero if the
2455 * interface was registered with the driver core; else a negative errno
2456 * value.
2457 *
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002458 */
2459int usb_new_device(struct usb_device *udev)
2460{
2461 int err;
2462
Dan Streetman16985402010-01-06 09:56:53 -05002463 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002464 /* Initialize non-root-hub device wakeup to disabled;
2465 * device (un)configuration controls wakeup capable
2466 * sysfs power/wakeup controls wakeup enabled/disabled
2467 */
2468 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002469 }
2470
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002471 /* Tell the runtime-PM framework the device is active */
2472 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002473 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002474 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002475 pm_runtime_enable(&udev->dev);
2476
Alan Sternc08512c2010-11-15 15:57:58 -05002477 /* By default, forbid autosuspend for all devices. It will be
2478 * allowed for hubs during binding.
2479 */
2480 usb_disable_autosuspend(udev);
2481
Alan Stern8d8558d2009-12-08 15:50:41 -05002482 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002483 if (err < 0)
2484 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002485 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2486 udev->devnum, udev->bus->busnum,
2487 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002488 /* export the usbdev device-node for libusb */
2489 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2490 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2491
Alan Stern6cd13202008-11-13 15:08:30 -05002492 /* Tell the world! */
2493 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002494
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002495 if (udev->serial)
2496 add_device_randomness(udev->serial, strlen(udev->serial));
2497 if (udev->product)
2498 add_device_randomness(udev->product, strlen(udev->product));
2499 if (udev->manufacturer)
2500 add_device_randomness(udev->manufacturer,
2501 strlen(udev->manufacturer));
2502
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002503 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002504
Dan Williamsa4204ff2014-05-20 18:08:22 -07002505 /* check whether the hub or firmware marks this port as non-removable */
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002506 if (udev->parent)
2507 set_usb_port_removable(udev);
2508
Alan Stern782da722006-07-01 22:09:35 -04002509 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002510 * for configuring the device and invoking the add-device
2511 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002512 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002513 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 if (err) {
2515 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2516 goto fail;
2517 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002518
Lan Tianyufde26382013-01-20 01:53:33 +08002519 /* Create link files between child device and usb port device. */
2520 if (udev->parent) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002521 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
Dan Williamsd5c38342014-05-20 18:08:52 -07002522 int port1 = udev->portnum;
2523 struct usb_port *port_dev = hub->ports[port1 - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002524
2525 err = sysfs_create_link(&udev->dev.kobj,
2526 &port_dev->dev.kobj, "port");
2527 if (err)
2528 goto fail;
2529
2530 err = sysfs_create_link(&port_dev->dev.kobj,
2531 &udev->dev.kobj, "device");
2532 if (err) {
2533 sysfs_remove_link(&udev->dev.kobj, "port");
2534 goto fail;
2535 }
Lan Tianyu971fcd42013-01-23 04:26:29 +08002536
Dan Williamsd5c38342014-05-20 18:08:52 -07002537 if (!test_and_set_bit(port1, hub->child_usage_bits))
2538 pm_runtime_get_sync(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002539 }
2540
Alan Stern3b23dd62008-12-05 14:10:34 -05002541 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002542 usb_mark_last_busy(udev);
2543 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002544 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
2546fail:
2547 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002548 pm_runtime_disable(&udev->dev);
2549 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002550 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002553
2554/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002555 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2556 * @usb_dev: USB device
2557 *
2558 * Move the USB device to a very basic state where interfaces are disabled
2559 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002560 *
2561 * We share a lock (that we have) with device_del(), so we need to
2562 * defer its call.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002563 *
2564 * Return: 0.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002565 */
2566int usb_deauthorize_device(struct usb_device *usb_dev)
2567{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002568 usb_lock_device(usb_dev);
2569 if (usb_dev->authorized == 0)
2570 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002571
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002572 usb_dev->authorized = 0;
2573 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002574
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002575out_unauthorized:
2576 usb_unlock_device(usb_dev);
2577 return 0;
2578}
2579
2580
2581int usb_authorize_device(struct usb_device *usb_dev)
2582{
2583 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002584
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002585 usb_lock_device(usb_dev);
2586 if (usb_dev->authorized == 1)
2587 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002588
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002589 result = usb_autoresume_device(usb_dev);
2590 if (result < 0) {
2591 dev_err(&usb_dev->dev,
2592 "can't autoresume for authorization: %d\n", result);
2593 goto error_autoresume;
2594 }
Josef Gajduseke50a3222014-10-09 15:47:54 +02002595
2596 if (usb_dev->wusb) {
2597 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2598 if (result < 0) {
2599 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2600 "authorization: %d\n", result);
2601 goto error_device_descriptor;
2602 }
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002603 }
Alan Sternda307122009-12-08 15:54:44 -05002604
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002605 usb_dev->authorized = 1;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002606 /* Choose and set the configuration. This registers the interfaces
2607 * with the driver core and lets interface drivers bind to them.
2608 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002609 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002610 if (c >= 0) {
2611 result = usb_set_configuration(usb_dev, c);
2612 if (result) {
2613 dev_err(&usb_dev->dev,
2614 "can't set config #%d, error %d\n", c, result);
2615 /* This need not be fatal. The user can try to
2616 * set other configurations. */
2617 }
2618 }
2619 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002620
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002621error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002622 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002623error_autoresume:
2624out_authorized:
Matthias Beyer781b2132013-10-10 23:41:29 +02002625 usb_unlock_device(usb_dev); /* complements locktree */
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002626 return result;
2627}
2628
Mathias Nyman0cdd49a2015-12-10 09:59:29 +02002629/*
2630 * Return 1 if port speed is SuperSpeedPlus, 0 otherwise
2631 * check it from the link protocol field of the current speed ID attribute.
2632 * current speed ID is got from ext port status request. Sublink speed attribute
2633 * table is returned with the hub BOS SSP device capability descriptor
2634 */
2635static int port_speed_is_ssp(struct usb_device *hdev, int speed_id)
2636{
2637 int ssa_count;
2638 u32 ss_attr;
2639 int i;
2640 struct usb_ssp_cap_descriptor *ssp_cap = hdev->bos->ssp_cap;
2641
2642 if (!ssp_cap)
2643 return 0;
2644
2645 ssa_count = le32_to_cpu(ssp_cap->bmAttributes) &
2646 USB_SSP_SUBLINK_SPEED_ATTRIBS;
2647
2648 for (i = 0; i <= ssa_count; i++) {
2649 ss_attr = le32_to_cpu(ssp_cap->bmSublinkSpeedAttr[i]);
2650 if (speed_id == (ss_attr & USB_SSP_SUBLINK_SPEED_SSID))
2651 return !!(ss_attr & USB_SSP_SUBLINK_SPEED_LP);
2652 }
2653 return 0;
2654}
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002655
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002656/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2657static unsigned hub_is_wusb(struct usb_hub *hub)
2658{
2659 struct usb_hcd *hcd;
2660 if (hub->hdev->parent != NULL) /* not a root hub? */
2661 return 0;
2662 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2663 return hcd->wireless;
2664}
2665
2666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667#define PORT_RESET_TRIES 5
2668#define SET_ADDRESS_TRIES 2
2669#define GET_DESCRIPTOR_TRIES 2
2670#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302671#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2674#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002675#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676#define HUB_LONG_RESET_TIME 200
Sarah Sharp77c7f072012-11-14 17:16:52 -08002677#define HUB_RESET_TIMEOUT 800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Dan Williams48fc7db2013-12-05 17:07:27 -08002679/*
2680 * "New scheme" enumeration causes an extra state transition to be
2681 * exposed to an xhci host and causes USB3 devices to receive control
2682 * commands in the default state. This has been seen to cause
2683 * enumeration failures, so disable this enumeration scheme for USB3
2684 * devices.
2685 */
2686static bool use_new_scheme(struct usb_device *udev, int retry)
2687{
Mathias Nyman8a1b2722015-12-10 09:59:25 +02002688 if (udev->speed >= USB_SPEED_SUPER)
Dan Williams48fc7db2013-12-05 17:07:27 -08002689 return false;
2690
2691 return USE_NEW_SCHEME(retry);
2692}
2693
Rahul Bedarkar025d4432014-01-04 11:24:41 +05302694/* Is a USB 3.0 port in the Inactive or Compliance Mode state?
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002695 * Port worm reset is required to recover
2696 */
Dan Williams3cd12f92014-05-29 12:58:46 -07002697static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1,
2698 u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002699{
Dan Williams3cd12f92014-05-29 12:58:46 -07002700 u16 link_state;
2701
2702 if (!hub_is_superspeed(hub->hdev))
2703 return false;
2704
2705 if (test_bit(port1, hub->warm_reset_bits))
2706 return true;
2707
2708 link_state = portstatus & USB_PORT_STAT_LINK_STATE;
2709 return link_state == USB_SS_PORT_LS_SS_INACTIVE
2710 || link_state == USB_SS_PORT_LS_COMP_MOD;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002711}
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002714 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
2716 int delay_time, ret;
2717 u16 portstatus;
2718 u16 portchange;
Mathias Nyman0cdd49a2015-12-10 09:59:29 +02002719 u32 ext_portstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
2721 for (delay_time = 0;
2722 delay_time < HUB_RESET_TIMEOUT;
2723 delay_time += delay) {
2724 /* wait to give the device a chance to reset */
2725 msleep(delay);
2726
2727 /* read and decode port status */
Mathias Nyman0cdd49a2015-12-10 09:59:29 +02002728 if (hub_is_superspeedplus(hub->hdev))
2729 ret = hub_ext_port_status(hub, port1,
2730 HUB_EXT_PORT_STATUS,
2731 &portstatus, &portchange,
2732 &ext_portstatus);
2733 else
2734 ret = hub_port_status(hub, port1, &portstatus,
2735 &portchange);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 if (ret < 0)
2737 return ret;
2738
Sarah Sharp4f434472012-11-15 14:58:04 -08002739 /* The port state is unknown until the reset completes. */
Sarah Sharp470f0be2012-12-11 10:14:03 -08002740 if (!(portstatus & USB_PORT_STAT_RESET))
2741 break;
Sarah Sharp4f434472012-11-15 14:58:04 -08002742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 /* switch to the long delay after two short delay failures */
2744 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2745 delay = HUB_LONG_RESET_TIME;
2746
Dan Williamsd99f6b42014-05-20 18:08:17 -07002747 dev_dbg(&hub->ports[port1 - 1]->dev,
2748 "not %sreset yet, waiting %dms\n",
2749 warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 }
2751
Sarah Sharp470f0be2012-12-11 10:14:03 -08002752 if ((portstatus & USB_PORT_STAT_RESET))
2753 return -EBUSY;
2754
Dan Williams3cd12f92014-05-29 12:58:46 -07002755 if (hub_port_warm_reset_required(hub, port1, portstatus))
Sarah Sharp470f0be2012-12-11 10:14:03 -08002756 return -ENOTCONN;
2757
2758 /* Device went away? */
2759 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2760 return -ENOTCONN;
2761
2762 /* bomb out completely if the connection bounced. A USB 3.0
2763 * connection may bounce if multiple warm resets were issued,
2764 * but the device may have successfully re-connected. Ignore it.
2765 */
2766 if (!hub_is_superspeed(hub->hdev) &&
2767 (portchange & USB_PORT_STAT_C_CONNECTION))
2768 return -ENOTCONN;
2769
2770 if (!(portstatus & USB_PORT_STAT_ENABLE))
2771 return -EBUSY;
2772
2773 if (!udev)
2774 return 0;
2775
2776 if (hub_is_wusb(hub))
2777 udev->speed = USB_SPEED_WIRELESS;
Mathias Nyman0cdd49a2015-12-10 09:59:29 +02002778 else if (hub_is_superspeedplus(hub->hdev) &&
2779 port_speed_is_ssp(hub->hdev, ext_portstatus &
2780 USB_EXT_PORT_STAT_RX_SPEED_ID))
2781 udev->speed = USB_SPEED_SUPER_PLUS;
Sarah Sharp470f0be2012-12-11 10:14:03 -08002782 else if (hub_is_superspeed(hub->hdev))
2783 udev->speed = USB_SPEED_SUPER;
2784 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2785 udev->speed = USB_SPEED_HIGH;
2786 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2787 udev->speed = USB_SPEED_LOW;
2788 else
2789 udev->speed = USB_SPEED_FULL;
2790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791}
2792
Andiry Xu75d7cf72011-09-13 16:41:11 -07002793/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002795 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
2797 int i, status;
Sarah Sharpa24a6072012-11-01 11:20:44 -07002798 u16 portchange, portstatus;
Dan Williamsd99f6b42014-05-20 18:08:17 -07002799 struct usb_port *port_dev = hub->ports[port1 - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002801 if (!hub_is_superspeed(hub->hdev)) {
2802 if (warm) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002803 dev_err(hub->intfdev, "only USB3 hub support "
2804 "warm reset\n");
2805 return -EINVAL;
2806 }
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002807 /* Block EHCI CF initialization during the port reset.
2808 * Some companion controllers don't like it when they mix.
2809 */
2810 down_read(&ehci_cf_port_reset_rwsem);
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07002811 } else if (!warm) {
2812 /*
2813 * If the caller hasn't explicitly requested a warm reset,
2814 * double check and see if one is needed.
2815 */
Robert Schlabbachfb6d1f7d2015-05-26 00:27:30 +02002816 if (hub_port_status(hub, port1, &portstatus, &portchange) == 0)
2817 if (hub_port_warm_reset_required(hub, port1,
2818 portstatus))
2819 warm = true;
Andiry Xu75d7cf72011-09-13 16:41:11 -07002820 }
Dan Williams3cd12f92014-05-29 12:58:46 -07002821 clear_bit(port1, hub->warm_reset_bits);
Alan Stern32fe0192007-10-10 16:27:07 -04002822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 /* Reset the port */
2824 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002825 status = set_port_feature(hub->hdev, port1, (warm ?
2826 USB_PORT_FEAT_BH_PORT_RESET :
2827 USB_PORT_FEAT_RESET));
Alan Sterne9e88fb2013-03-27 16:14:01 -04002828 if (status == -ENODEV) {
2829 ; /* The hub is gone */
2830 } else if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002831 dev_err(&port_dev->dev,
2832 "cannot %sreset (err = %d)\n",
2833 warm ? "warm " : "", status);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002834 } else {
2835 status = hub_port_wait_reset(hub, port1, udev, delay,
2836 warm);
Alan Sterne9e88fb2013-03-27 16:14:01 -04002837 if (status && status != -ENOTCONN && status != -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 dev_dbg(hub->intfdev,
2839 "port_wait_reset: err = %d\n",
2840 status);
2841 }
2842
Sarah Sharpa24a6072012-11-01 11:20:44 -07002843 /* Check for disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002844 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
Robert Schlabbachfb6d1f7d2015-05-26 00:27:30 +02002845 usb_clear_port_feature(hub->hdev, port1,
2846 USB_PORT_FEAT_C_RESET);
Sarah Sharpa24a6072012-11-01 11:20:44 -07002847
2848 if (!hub_is_superspeed(hub->hdev))
2849 goto done;
2850
Robert Schlabbachfb6d1f7d2015-05-26 00:27:30 +02002851 usb_clear_port_feature(hub->hdev, port1,
2852 USB_PORT_FEAT_C_BH_PORT_RESET);
2853 usb_clear_port_feature(hub->hdev, port1,
2854 USB_PORT_FEAT_C_PORT_LINK_STATE);
2855 usb_clear_port_feature(hub->hdev, port1,
2856 USB_PORT_FEAT_C_CONNECTION);
2857
Sarah Sharpa24a6072012-11-01 11:20:44 -07002858 /*
2859 * If a USB 3.0 device migrates from reset to an error
2860 * state, re-issue the warm reset.
2861 */
2862 if (hub_port_status(hub, port1,
2863 &portstatus, &portchange) < 0)
2864 goto done;
2865
Dan Williams3cd12f92014-05-29 12:58:46 -07002866 if (!hub_port_warm_reset_required(hub, port1,
2867 portstatus))
Sarah Sharpa24a6072012-11-01 11:20:44 -07002868 goto done;
2869
2870 /*
2871 * If the port is in SS.Inactive or Compliance Mode, the
2872 * hot or warm reset failed. Try another warm reset.
2873 */
2874 if (!warm) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002875 dev_dbg(&port_dev->dev,
2876 "hot reset failed, warm reset\n");
Sarah Sharpa24a6072012-11-01 11:20:44 -07002877 warm = true;
2878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
2880
Dan Williamsd99f6b42014-05-20 18:08:17 -07002881 dev_dbg(&port_dev->dev,
2882 "not enabled, trying %sreset again...\n",
2883 warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 delay = HUB_LONG_RESET_TIME;
2885 }
2886
Dan Williamsd99f6b42014-05-20 18:08:17 -07002887 dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Andiry Xu75d7cf72011-09-13 16:41:11 -07002889done:
Robert Schlabbachfb6d1f7d2015-05-26 00:27:30 +02002890 if (status == 0) {
2891 /* TRSTRCY = 10 ms; plus some extra */
2892 msleep(10 + 40);
2893 if (udev) {
2894 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2895
2896 update_devnum(udev, 0);
2897 /* The xHC may think the device is already reset,
2898 * so ignore the status.
2899 */
2900 if (hcd->driver->reset_device)
2901 hcd->driver->reset_device(hcd, udev);
2902
2903 usb_set_device_state(udev, USB_STATE_DEFAULT);
2904 }
2905 } else {
2906 if (udev)
2907 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
2908 }
2909
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002910 if (!hub_is_superspeed(hub->hdev))
Andiry Xu75d7cf72011-09-13 16:41:11 -07002911 up_read(&ehci_cf_port_reset_rwsem);
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 return status;
2914}
2915
Andiry Xu0ed9a572011-04-27 18:07:43 +08002916/* Check if a port is power on */
2917static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2918{
2919 int ret = 0;
2920
2921 if (hub_is_superspeed(hub->hdev)) {
2922 if (portstatus & USB_SS_PORT_STAT_POWER)
2923 ret = 1;
2924 } else {
2925 if (portstatus & USB_PORT_STAT_POWER)
2926 ret = 1;
2927 }
2928
2929 return ret;
2930}
2931
Dan Williams5c79a1e2014-05-20 18:09:26 -07002932static void usb_lock_port(struct usb_port *port_dev)
2933 __acquires(&port_dev->status_lock)
2934{
2935 mutex_lock(&port_dev->status_lock);
2936 __acquire(&port_dev->status_lock);
2937}
2938
2939static void usb_unlock_port(struct usb_port *port_dev)
2940 __releases(&port_dev->status_lock)
2941{
2942 mutex_unlock(&port_dev->status_lock);
2943 __release(&port_dev->status_lock);
2944}
2945
Alan Sternd388dab2006-07-01 22:14:24 -04002946#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Andiry Xu0ed9a572011-04-27 18:07:43 +08002948/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2949static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2950{
2951 int ret = 0;
2952
2953 if (hub_is_superspeed(hub->hdev)) {
2954 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2955 == USB_SS_PORT_LS_U3)
2956 ret = 1;
2957 } else {
2958 if (portstatus & USB_PORT_STAT_SUSPEND)
2959 ret = 1;
2960 }
2961
2962 return ret;
2963}
Alan Sternb01b03f2008-04-28 11:06:11 -04002964
2965/* Determine whether the device on a port is ready for a normal resume,
2966 * is ready for a reset-resume, or should be disconnected.
2967 */
2968static int check_port_resume_type(struct usb_device *udev,
2969 struct usb_hub *hub, int port1,
Julius Werner7fa40912015-01-27 13:20:12 -08002970 int status, u16 portchange, u16 portstatus)
Alan Sternb01b03f2008-04-28 11:06:11 -04002971{
Dan Williamsd99f6b42014-05-20 18:08:17 -07002972 struct usb_port *port_dev = hub->ports[port1 - 1];
Julius Werner7fa40912015-01-27 13:20:12 -08002973 int retries = 3;
Dan Williamsd99f6b42014-05-20 18:08:17 -07002974
Julius Werner7fa40912015-01-27 13:20:12 -08002975 retry:
Dan Williams3cd12f92014-05-29 12:58:46 -07002976 /* Is a warm reset needed to recover the connection? */
2977 if (status == 0 && udev->reset_resume
2978 && hub_port_warm_reset_required(hub, port1, portstatus)) {
2979 /* pass */;
2980 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002981 /* Is the device still present? */
Dan Williams3cd12f92014-05-29 12:58:46 -07002982 else if (status || port_is_suspended(hub, portstatus) ||
Julius Werner7fa40912015-01-27 13:20:12 -08002983 !port_is_power_on(hub, portstatus)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002984 if (status >= 0)
2985 status = -ENODEV;
Julius Werner7fa40912015-01-27 13:20:12 -08002986 } else if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
2987 if (retries--) {
2988 usleep_range(200, 300);
2989 status = hub_port_status(hub, port1, &portstatus,
2990 &portchange);
2991 goto retry;
2992 }
2993 status = -ENODEV;
Alan Sternb01b03f2008-04-28 11:06:11 -04002994 }
2995
Alan Stern86c57ed2008-06-30 11:14:43 -04002996 /* Can't do a normal resume if the port isn't enabled,
2997 * so try a reset-resume instead.
2998 */
2999 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
3000 if (udev->persist_enabled)
3001 udev->reset_resume = 1;
3002 else
3003 status = -ENODEV;
3004 }
Alan Sternb01b03f2008-04-28 11:06:11 -04003005
3006 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003007 dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n",
3008 portchange, portstatus, status);
Alan Sternb01b03f2008-04-28 11:06:11 -04003009 } else if (udev->reset_resume) {
3010
3011 /* Late port handoff can set status-change bits */
3012 if (portchange & USB_PORT_STAT_C_CONNECTION)
Lan Tianyuad493e52013-01-23 04:26:30 +08003013 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04003014 USB_PORT_FEAT_C_CONNECTION);
3015 if (portchange & USB_PORT_STAT_C_ENABLE)
Lan Tianyuad493e52013-01-23 04:26:30 +08003016 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04003017 USB_PORT_FEAT_C_ENABLE);
3018 }
3019
3020 return status;
3021}
3022
Sarah Sharpf74631e2012-06-25 12:08:08 -07003023int usb_disable_ltm(struct usb_device *udev)
3024{
3025 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3026
3027 /* Check if the roothub and device supports LTM. */
3028 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
3029 !usb_device_supports_ltm(udev))
3030 return 0;
3031
3032 /* Clear Feature LTM Enable can only be sent if the device is
3033 * configured.
3034 */
3035 if (!udev->actconfig)
3036 return 0;
3037
3038 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3039 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
3040 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
3041 USB_CTRL_SET_TIMEOUT);
3042}
3043EXPORT_SYMBOL_GPL(usb_disable_ltm);
3044
3045void usb_enable_ltm(struct usb_device *udev)
3046{
3047 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3048
3049 /* Check if the roothub and device supports LTM. */
3050 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
3051 !usb_device_supports_ltm(udev))
3052 return;
3053
3054 /* Set Feature LTM Enable can only be sent if the device is
3055 * configured.
3056 */
3057 if (!udev->actconfig)
3058 return;
3059
3060 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3061 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
3062 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
3063 USB_CTRL_SET_TIMEOUT);
3064}
3065EXPORT_SYMBOL_GPL(usb_enable_ltm);
3066
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003067/*
Alan Stern28e861652013-07-30 15:37:33 -04003068 * usb_enable_remote_wakeup - enable remote wakeup for a device
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003069 * @udev: target device
3070 *
Alan Stern28e861652013-07-30 15:37:33 -04003071 * For USB-2 devices: Set the device's remote wakeup feature.
3072 *
3073 * For USB-3 devices: Assume there's only one function on the device and
3074 * enable remote wake for the first interface. FIXME if the interface
3075 * association descriptor shows there's more than one function.
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003076 */
Alan Stern28e861652013-07-30 15:37:33 -04003077static int usb_enable_remote_wakeup(struct usb_device *udev)
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003078{
Alan Stern28e861652013-07-30 15:37:33 -04003079 if (udev->speed < USB_SPEED_SUPER)
3080 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3081 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
3082 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
3083 USB_CTRL_SET_TIMEOUT);
3084 else
3085 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3086 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
3087 USB_INTRF_FUNC_SUSPEND,
3088 USB_INTRF_FUNC_SUSPEND_RW |
3089 USB_INTRF_FUNC_SUSPEND_LP,
3090 NULL, 0, USB_CTRL_SET_TIMEOUT);
3091}
3092
3093/*
3094 * usb_disable_remote_wakeup - disable remote wakeup for a device
3095 * @udev: target device
3096 *
3097 * For USB-2 devices: Clear the device's remote wakeup feature.
3098 *
3099 * For USB-3 devices: Assume there's only one function on the device and
3100 * disable remote wake for the first interface. FIXME if the interface
3101 * association descriptor shows there's more than one function.
3102 */
3103static int usb_disable_remote_wakeup(struct usb_device *udev)
3104{
3105 if (udev->speed < USB_SPEED_SUPER)
3106 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3107 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
3108 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
3109 USB_CTRL_SET_TIMEOUT);
3110 else
3111 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003112 USB_REQ_CLEAR_FEATURE, USB_RECIP_INTERFACE,
3113 USB_INTRF_FUNC_SUSPEND, 0, NULL, 0,
3114 USB_CTRL_SET_TIMEOUT);
3115}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Alan Sterne583d9d2013-07-11 14:58:04 -04003117/* Count of wakeup-enabled devices at or below udev */
3118static unsigned wakeup_enabled_descendants(struct usb_device *udev)
3119{
3120 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
3121
3122 return udev->do_remote_wakeup +
3123 (hub ? hub->wakeup_enabled_descendants : 0);
3124}
3125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126/*
Alan Stern140d8f62006-07-01 22:07:21 -04003127 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04003128 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07003129 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 *
3131 * Suspends a USB device that isn't in active use, conserving power.
3132 * Devices may wake out of a suspend, if anything important happens,
3133 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04003134 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 * to disconnect devices while they are suspended.
3136 *
David Brownell390a8c32005-09-13 19:57:27 -07003137 * This only affects the USB hardware for a device; its interfaces
3138 * (and, for hubs, child devices) must already have been suspended.
3139 *
Alan Stern624d6c02007-05-30 15:35:16 -04003140 * Selective port suspend reduces power; most suspended devices draw
3141 * less than 500 uA. It's also used in OTG, along with remote wakeup.
3142 * All devices below the suspended port are also suspended.
3143 *
3144 * Devices leave suspend state when the host wakes them up. Some devices
3145 * also support "remote wakeup", where the device can activate the USB
3146 * tree above them to deliver data, such as a keypress or packet. In
3147 * some cases, this wakes the USB host.
3148 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 * Suspending OTG devices may trigger HNP, if that's been enabled
3150 * between a pair of dual-role devices. That will change roles, such
3151 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
3152 *
Alan Stern4956ecc2007-05-30 16:51:28 -04003153 * Devices on USB hub ports have only one "suspend" state, corresponding
3154 * to ACPI D2, "may cause the device to lose some context".
3155 * State transitions include:
3156 *
3157 * - suspend, resume ... when the VBUS power link stays live
3158 * - suspend, disconnect ... VBUS lost
3159 *
3160 * Once VBUS drop breaks the circuit, the port it's using has to go through
3161 * normal re-enumeration procedures, starting with enabling VBUS power.
3162 * Other than re-initializing the hub (plug/unplug, except for root hubs),
Petr Mladek37ebb542014-09-19 17:32:23 +02003163 * Linux (2.6) currently has NO mechanisms to initiate that: no hub_wq
Alan Stern4956ecc2007-05-30 16:51:28 -04003164 * timer, no SRP, no requests through sysfs.
3165 *
Alan Sterne583d9d2013-07-11 14:58:04 -04003166 * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
3167 * suspended until their bus goes into global suspend (i.e., the root
Alan Stern0aa28322013-03-27 16:14:19 -04003168 * hub is suspended). Nevertheless, we change @udev->state to
3169 * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual
3170 * upstream port setting is stored in @udev->port_is_suspended.
Alan Stern4956ecc2007-05-30 16:51:28 -04003171 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 * Returns 0 on success, else negative errno.
3173 */
Alan Stern65bfd292008-11-25 16:39:18 -05003174int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
Lan Tianyuad493e52013-01-23 04:26:30 +08003176 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3177 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003178 int port1 = udev->portnum;
3179 int status;
Alan Stern0aa28322013-03-27 16:14:19 -04003180 bool really_suspend = true;
Alan Stern4956ecc2007-05-30 16:51:28 -04003181
Dan Williams5c79a1e2014-05-20 18:09:26 -07003182 usb_lock_port(port_dev);
3183
Alan Stern624d6c02007-05-30 15:35:16 -04003184 /* enable remote wakeup when appropriate; this lets the device
3185 * wake up the upstream hub (including maybe the root hub).
3186 *
3187 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
3188 * we don't explicitly enable it here.
3189 */
3190 if (udev->do_remote_wakeup) {
Alan Stern28e861652013-07-30 15:37:33 -04003191 status = usb_enable_remote_wakeup(udev);
Oliver Neukum0c487202009-10-19 13:19:41 +02003192 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003193 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
3194 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02003195 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02003196 if (PMSG_IS_AUTO(msg))
Alan Stern4fae6f02013-07-30 15:39:02 -04003197 goto err_wakeup;
Oliver Neukum0c487202009-10-19 13:19:41 +02003198 }
Alan Stern624d6c02007-05-30 15:35:16 -04003199 }
3200
Andiry Xu65580b432011-09-23 14:19:52 -07003201 /* disable USB2 hardware LPM */
3202 if (udev->usb2_hw_lpm_enabled == 1)
3203 usb_set_usb2_hardware_lpm(udev, 0);
3204
Sarah Sharpf74631e2012-06-25 12:08:08 -07003205 if (usb_disable_ltm(udev)) {
Alan Stern4fae6f02013-07-30 15:39:02 -04003206 dev_err(&udev->dev, "Failed to disable LTM before suspend\n.");
3207 status = -ENOMEM;
3208 if (PMSG_IS_AUTO(msg))
3209 goto err_ltm;
Sarah Sharpf74631e2012-06-25 12:08:08 -07003210 }
Sarah Sharp83060952012-05-02 14:25:52 -07003211 if (usb_unlocked_disable_lpm(udev)) {
Alan Stern4fae6f02013-07-30 15:39:02 -04003212 dev_err(&udev->dev, "Failed to disable LPM before suspend\n.");
3213 status = -ENOMEM;
3214 if (PMSG_IS_AUTO(msg))
3215 goto err_lpm3;
Sarah Sharp83060952012-05-02 14:25:52 -07003216 }
3217
Alan Stern624d6c02007-05-30 15:35:16 -04003218 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08003219 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003220 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
Alan Sterne583d9d2013-07-11 14:58:04 -04003221
Alan Stern0aa28322013-03-27 16:14:19 -04003222 /*
3223 * For system suspend, we do not need to enable the suspend feature
3224 * on individual USB-2 ports. The devices will automatically go
3225 * into suspend a few ms after the root hub stops sending packets.
3226 * The USB 2.0 spec calls this "global suspend".
Alan Sterne583d9d2013-07-11 14:58:04 -04003227 *
3228 * However, many USB hubs have a bug: They don't relay wakeup requests
3229 * from a downstream port if the port's suspend feature isn't on.
3230 * Therefore we will turn on the suspend feature if udev or any of its
3231 * descendants is enabled for remote wakeup.
Alan Stern0aa28322013-03-27 16:14:19 -04003232 */
Alan Sterne583d9d2013-07-11 14:58:04 -04003233 else if (PMSG_IS_AUTO(msg) || wakeup_enabled_descendants(udev) > 0)
3234 status = set_port_feature(hub->hdev, port1,
3235 USB_PORT_FEAT_SUSPEND);
Alan Stern0aa28322013-03-27 16:14:19 -04003236 else {
3237 really_suspend = false;
3238 status = 0;
3239 }
Alan Stern624d6c02007-05-30 15:35:16 -04003240 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003241 dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status);
Alan Sterncbb33002011-06-15 16:29:16 -04003242
Alan Stern4fae6f02013-07-30 15:39:02 -04003243 /* Try to enable USB3 LPM and LTM again */
3244 usb_unlocked_enable_lpm(udev);
3245 err_lpm3:
3246 usb_enable_ltm(udev);
3247 err_ltm:
Andiry Xuc3e751e2012-05-05 00:50:10 +08003248 /* Try to enable USB2 hardware LPM again */
3249 if (udev->usb2_hw_lpm_capable == 1)
3250 usb_set_usb2_hardware_lpm(udev, 1);
3251
Alan Stern4fae6f02013-07-30 15:39:02 -04003252 if (udev->do_remote_wakeup)
3253 (void) usb_disable_remote_wakeup(udev);
3254 err_wakeup:
Sarah Sharp83060952012-05-02 14:25:52 -07003255
Alan Sterncbb33002011-06-15 16:29:16 -04003256 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02003257 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003258 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04003259 } else {
Alan Stern30b1a7a2011-09-27 21:54:22 +02003260 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
3261 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
3262 udev->do_remote_wakeup);
Alan Stern0aa28322013-03-27 16:14:19 -04003263 if (really_suspend) {
3264 udev->port_is_suspended = 1;
Alan Sterne583d9d2013-07-11 14:58:04 -04003265
3266 /* device has up to 10 msec to fully suspend */
Alan Stern0aa28322013-03-27 16:14:19 -04003267 msleep(10);
3268 }
Alan Sterne583d9d2013-07-11 14:58:04 -04003269 usb_set_device_state(udev, USB_STATE_SUSPENDED);
Alan Stern624d6c02007-05-30 15:35:16 -04003270 }
Lan Tianyuad493e52013-01-23 04:26:30 +08003271
Dan Williamsd5c38342014-05-20 18:08:52 -07003272 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled
3273 && test_and_clear_bit(port1, hub->child_usage_bits))
Lan Tianyu98a4f1f2013-07-03 22:17:54 +08003274 pm_runtime_put_sync(&port_dev->dev);
Lan Tianyuad493e52013-01-23 04:26:30 +08003275
Alan Sternc08512c2010-11-15 15:57:58 -05003276 usb_mark_last_busy(hub->hdev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07003277
3278 usb_unlock_port(port_dev);
Alan Stern4956ecc2007-05-30 16:51:28 -04003279 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280}
David Brownellf3f32532005-09-22 22:37:29 -07003281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282/*
David Brownell390a8c32005-09-13 19:57:27 -07003283 * If the USB "suspend" state is in use (rather than "global suspend"),
3284 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04003285 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 * hardware resume signaling is finished, either because of selective
3287 * resume (by host) or remote wakeup (by device) ... now see what changed
3288 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04003289 *
3290 * If @udev->reset_resume is set then the device is reset before the
3291 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 */
Alan Stern140d8f62006-07-01 22:07:21 -04003293static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294{
Alan Stern54515fe2007-05-30 15:38:58 -04003295 int status = 0;
Oliver Neukum07e72b92012-11-29 15:05:57 +01003296 u16 devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
3298 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003299 dev_dbg(&udev->dev, "%s\n",
3300 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
3302 /* usb ch9 identifies four variants of SUSPENDED, based on what
3303 * state the device resumes to. Linux currently won't see the
3304 * first two on the host side; they'd be inside hub_port_init()
Petr Mladek37ebb542014-09-19 17:32:23 +02003305 * during many timeouts, but hub_wq can't suspend until later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 */
3307 usb_set_device_state(udev, udev->actconfig
3308 ? USB_STATE_CONFIGURED
3309 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Alan Stern54515fe2007-05-30 15:38:58 -04003311 /* 10.5.4.5 says not to reset a suspended port if the attached
3312 * device is enabled for remote wakeup. Hence the reset
3313 * operation is carried out here, after the port has been
3314 * resumed.
3315 */
Alan Stern1d102552014-03-18 10:39:05 -04003316 if (udev->reset_resume) {
3317 /*
3318 * If the device morphs or switches modes when it is reset,
3319 * we don't want to perform a reset-resume. We'll fail the
3320 * resume, which will cause a logical disconnect, and then
3321 * the device will be rediscovered.
3322 */
Alan Stern86c57ed2008-06-30 11:14:43 -04003323 retry_reset_resume:
Alan Stern1d102552014-03-18 10:39:05 -04003324 if (udev->quirks & USB_QUIRK_RESET)
3325 status = -ENODEV;
3326 else
3327 status = usb_reset_and_verify_device(udev);
3328 }
Alan Stern54515fe2007-05-30 15:38:58 -04003329
Matthias Beyer469271f2013-10-10 23:41:27 +02003330 /* 10.5.4.5 says be sure devices in the tree are still there.
3331 * For now let's assume the device didn't go crazy on resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 * and device drivers will know about any resume quirks.
3333 */
Alan Stern54515fe2007-05-30 15:38:58 -04003334 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04003335 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04003336 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
Alan Stern86c57ed2008-06-30 11:14:43 -04003337
3338 /* If a normal resume failed, try doing a reset-resume */
3339 if (status && !udev->reset_resume && udev->persist_enabled) {
3340 dev_dbg(&udev->dev, "retry with reset-resume\n");
3341 udev->reset_resume = 1;
3342 goto retry_reset_resume;
3343 }
Alan Stern54515fe2007-05-30 15:38:58 -04003344 }
Alan Sternb40b7a92006-06-19 15:12:38 -04003345
Alan Stern624d6c02007-05-30 15:35:16 -04003346 if (status) {
3347 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
3348 status);
Oliver Neukum07e72b92012-11-29 15:05:57 +01003349 /*
3350 * There are a few quirky devices which violate the standard
3351 * by claiming to have remote wakeup enabled after a reset,
3352 * which crash if the feature is cleared, hence check for
3353 * udev->reset_resume
3354 */
3355 } else if (udev->actconfig && !udev->reset_resume) {
Alan Stern28e861652013-07-30 15:37:33 -04003356 if (udev->speed < USB_SPEED_SUPER) {
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003357 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
Alan Stern28e861652013-07-30 15:37:33 -04003358 status = usb_disable_remote_wakeup(udev);
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003359 } else {
3360 status = usb_get_status(udev, USB_RECIP_INTERFACE, 0,
3361 &devstatus);
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003362 if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP
3363 | USB_INTRF_STAT_FUNC_RW))
Alan Stern28e861652013-07-30 15:37:33 -04003364 status = usb_disable_remote_wakeup(udev);
Alan Stern4bf0ba82005-11-21 11:58:07 -05003365 }
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003366
3367 if (status)
3368 dev_dbg(&udev->dev,
3369 "disable remote wakeup, status %d\n",
3370 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 }
3373 return status;
3374}
3375
Alan Stern624d6c02007-05-30 15:35:16 -04003376/*
Pratyush Ananda40178b2014-07-18 12:37:10 +05303377 * There are some SS USB devices which take longer time for link training.
3378 * XHCI specs 4.19.4 says that when Link training is successful, port
Al Cooper6b82b122015-12-02 14:55:07 -05003379 * sets CCS bit to 1. So if SW reads port status before successful link
Pratyush Ananda40178b2014-07-18 12:37:10 +05303380 * training, then it will not find device to be present.
3381 * USB Analyzer log with such buggy devices show that in some cases
3382 * device switch on the RX termination after long delay of host enabling
3383 * the VBUS. In few other cases it has been seen that device fails to
3384 * negotiate link training in first attempt. It has been
3385 * reported till now that few devices take as long as 2000 ms to train
3386 * the link after host enabling its VBUS and termination. Following
3387 * routine implements a 2000 ms timeout for link training. If in a case
3388 * link trains before timeout, loop will exit earlier.
3389 *
Al Cooper6b82b122015-12-02 14:55:07 -05003390 * There are also some 2.0 hard drive based devices and 3.0 thumb
3391 * drives that, when plugged into a 2.0 only port, take a long
3392 * time to set CCS after VBUS enable.
3393 *
Pratyush Ananda40178b2014-07-18 12:37:10 +05303394 * FIXME: If a device was connected before suspend, but was removed
3395 * while system was asleep, then the loop in the following routine will
3396 * only exit at timeout.
3397 *
Al Cooper6b82b122015-12-02 14:55:07 -05003398 * This routine should only be called when persist is enabled.
Pratyush Ananda40178b2014-07-18 12:37:10 +05303399 */
Al Cooper6b82b122015-12-02 14:55:07 -05003400static int wait_for_connected(struct usb_device *udev,
Pratyush Ananda40178b2014-07-18 12:37:10 +05303401 struct usb_hub *hub, int *port1,
3402 u16 *portchange, u16 *portstatus)
3403{
3404 int status = 0, delay_ms = 0;
3405
3406 while (delay_ms < 2000) {
3407 if (status || *portstatus & USB_PORT_STAT_CONNECTION)
3408 break;
3409 msleep(20);
3410 delay_ms += 20;
3411 status = hub_port_status(hub, *port1, portstatus, portchange);
3412 }
Al Cooper6b82b122015-12-02 14:55:07 -05003413 dev_dbg(&udev->dev, "Waited %dms for CONNECT\n", delay_ms);
Pratyush Ananda40178b2014-07-18 12:37:10 +05303414 return status;
3415}
3416
3417/*
Alan Stern624d6c02007-05-30 15:35:16 -04003418 * usb_port_resume - re-activate a suspended usb device's upstream port
3419 * @udev: device to re-activate, not a root hub
3420 * Context: must be able to sleep; device not locked; pm locks held
3421 *
3422 * This will re-activate the suspended device, increasing power usage
3423 * while letting drivers communicate again with its endpoints.
3424 * USB resume explicitly guarantees that the power session between
3425 * the host and the device is the same as it was when the device
3426 * suspended.
3427 *
Alan Sternfeccc302008-03-03 15:15:59 -05003428 * If @udev->reset_resume is set then this routine won't check that the
3429 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04003430 * reset @udev. The end result is that a broken power session can be
3431 * recovered and @udev will appear to persist across a loss of VBUS power.
3432 *
3433 * For example, if a host controller doesn't maintain VBUS suspend current
3434 * during a system sleep or is reset when the system wakes up, all the USB
3435 * power sessions below it will be broken. This is especially troublesome
3436 * for mass-storage devices containing mounted filesystems, since the
3437 * device will appear to have disconnected and all the memory mappings
3438 * to it will be lost. Using the USB_PERSIST facility, the device can be
3439 * made to appear as if it had not disconnected.
3440 *
Ming Lei742120c2008-06-18 22:00:29 +08003441 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05003442 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04003443 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3444 * quite possible for a device to remain unaltered but its media to be
3445 * changed. If the user replaces a flash memory card while the system is
3446 * asleep, he will have only himself to blame when the filesystem on the
3447 * new card is corrupted and the system crashes.
3448 *
Alan Stern624d6c02007-05-30 15:35:16 -04003449 * Returns 0 on success, else negative errno.
3450 */
Alan Stern65bfd292008-11-25 16:39:18 -05003451int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452{
Lan Tianyuad493e52013-01-23 04:26:30 +08003453 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3454 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003455 int port1 = udev->portnum;
3456 int status;
3457 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003458
Dan Williamsd5c38342014-05-20 18:08:52 -07003459 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08003460 status = pm_runtime_get_sync(&port_dev->dev);
Lan Tianyuad493e52013-01-23 04:26:30 +08003461 if (status < 0) {
3462 dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
3463 status);
3464 return status;
3465 }
3466 }
3467
Dan Williams5c79a1e2014-05-20 18:09:26 -07003468 usb_lock_port(port_dev);
3469
Alan Sternd25450c2006-11-20 11:14:30 -05003470 /* Skip the initial Clear-Suspend step for a remote wakeup */
3471 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003472 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003473 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003476 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003477 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
Andiry Xua7114232011-04-27 18:07:50 +08003478 else
Lan Tianyuad493e52013-01-23 04:26:30 +08003479 status = usb_clear_port_feature(hub->hdev,
Andiry Xua7114232011-04-27 18:07:50 +08003480 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003482 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 } else {
Felipe Balbibbc78c02015-02-13 15:38:33 -06003484 /* drive resume for USB_RESUME_TIMEOUT msec */
Alan Stern686314c2007-05-30 15:34:36 -04003485 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003486 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Felipe Balbibbc78c02015-02-13 15:38:33 -06003487 msleep(USB_RESUME_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3490 * stop resume signaling. Then finish the resume
3491 * sequence.
3492 */
Alan Sternd25450c2006-11-20 11:14:30 -05003493 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003494
Alan Sternb01b03f2008-04-28 11:06:11 -04003495 /* TRSMRCY = 10 msec */
3496 msleep(10);
3497 }
Alan Stern54515fe2007-05-30 15:38:58 -04003498
Alan Sternb01b03f2008-04-28 11:06:11 -04003499 SuspendCleared:
3500 if (status == 0) {
Alan Sternbfd1e912012-10-19 11:03:39 -04003501 udev->port_is_suspended = 0;
Andiry Xua7114232011-04-27 18:07:50 +08003502 if (hub_is_superspeed(hub->hdev)) {
3503 if (portchange & USB_PORT_STAT_C_LINK_STATE)
Lan Tianyuad493e52013-01-23 04:26:30 +08003504 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003505 USB_PORT_FEAT_C_PORT_LINK_STATE);
3506 } else {
3507 if (portchange & USB_PORT_STAT_C_SUSPEND)
Lan Tianyuad493e52013-01-23 04:26:30 +08003508 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003509 USB_PORT_FEAT_C_SUSPEND);
3510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
Al Cooper6b82b122015-12-02 14:55:07 -05003513 if (udev->persist_enabled)
3514 status = wait_for_connected(udev, hub, &port1, &portchange,
Pratyush Ananda40178b2014-07-18 12:37:10 +05303515 &portstatus);
3516
Alan Sternb01b03f2008-04-28 11:06:11 -04003517 status = check_port_resume_type(udev,
3518 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003519 if (status == 0)
3520 status = finish_port_resume(udev);
3521 if (status < 0) {
3522 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3523 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003524 } else {
3525 /* Try to enable USB2 hardware LPM */
3526 if (udev->usb2_hw_lpm_capable == 1)
3527 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003528
Sarah Sharpf74631e2012-06-25 12:08:08 -07003529 /* Try to enable USB3 LTM and LPM */
3530 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003531 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003532 }
Andiry Xu65580b432011-09-23 14:19:52 -07003533
Dan Williams5c79a1e2014-05-20 18:09:26 -07003534 usb_unlock_port(port_dev);
3535
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 return status;
3537}
3538
Alan Stern0534d462010-01-08 12:56:30 -05003539int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540{
3541 int status = 0;
3542
Dan Williams5c79a1e2014-05-20 18:09:26 -07003543 usb_lock_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003545 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003546 status = usb_autoresume_device(udev);
3547 if (status == 0) {
3548 /* Let the drivers do their thing, then... */
3549 usb_autosuspend_device(udev);
3550 }
Alan Sternd25450c2006-11-20 11:14:30 -05003551 }
Dan Williams5c79a1e2014-05-20 18:09:26 -07003552 usb_unlock_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 return status;
3554}
3555
Dan Williams7e73be22014-05-20 18:09:31 -07003556/* Returns 1 if there was a remote wakeup and a connect status change. */
3557static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
3558 u16 portstatus, u16 portchange)
3559 __must_hold(&port_dev->status_lock)
3560{
3561 struct usb_port *port_dev = hub->ports[port - 1];
3562 struct usb_device *hdev;
3563 struct usb_device *udev;
3564 int connect_change = 0;
3565 int ret;
3566
3567 hdev = hub->hdev;
3568 udev = port_dev->child;
3569 if (!hub_is_superspeed(hdev)) {
3570 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
3571 return 0;
3572 usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
3573 } else {
3574 if (!udev || udev->state != USB_STATE_SUSPENDED ||
3575 (portstatus & USB_PORT_STAT_LINK_STATE) !=
3576 USB_SS_PORT_LS_U0)
3577 return 0;
3578 }
3579
3580 if (udev) {
3581 /* TRSMRCY = 10 msec */
3582 msleep(10);
3583
3584 usb_unlock_port(port_dev);
3585 ret = usb_remote_wakeup(udev);
3586 usb_lock_port(port_dev);
3587 if (ret < 0)
3588 connect_change = 1;
3589 } else {
3590 ret = -ENODEV;
3591 hub_port_disable(hub, port, 1);
3592 }
3593 dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
3594 return connect_change;
3595}
3596
Ming Leie6f30de2012-10-24 11:59:24 +08003597static int check_ports_changed(struct usb_hub *hub)
3598{
3599 int port1;
3600
3601 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3602 u16 portstatus, portchange;
3603 int status;
3604
3605 status = hub_port_status(hub, port1, &portstatus, &portchange);
3606 if (!status && portchange)
3607 return 1;
3608 }
3609 return 0;
3610}
3611
David Brownelldb690872005-09-13 19:56:33 -07003612static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
Chase Metzger17248562015-08-11 21:34:37 -07003614 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 struct usb_device *hdev = hub->hdev;
3616 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003617 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
Alan Sterne583d9d2013-07-11 14:58:04 -04003619 /*
3620 * Warn if children aren't already suspended.
3621 * Also, add up the number of wakeup-enabled descendants.
3622 */
3623 hub->wakeup_enabled_descendants = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003625 struct usb_port *port_dev = hub->ports[port1 - 1];
3626 struct usb_device *udev = port_dev->child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Alan Stern6840d252007-09-10 11:34:26 -04003628 if (udev && udev->can_submit) {
Dan Williamsb658b8f2014-06-17 16:16:22 -07003629 dev_warn(&port_dev->dev, "device %s not suspended yet\n",
3630 dev_name(&udev->dev));
Alan Stern5b1b0b82011-08-19 23:49:48 +02003631 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003632 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003633 }
Alan Sterne583d9d2013-07-11 14:58:04 -04003634 if (udev)
3635 hub->wakeup_enabled_descendants +=
3636 wakeup_enabled_descendants(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 }
Ming Leie6f30de2012-10-24 11:59:24 +08003638
3639 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3640 /* check if there are changes pending on hub ports */
3641 if (check_ports_changed(hub)) {
3642 if (PMSG_IS_AUTO(msg))
3643 return -EBUSY;
3644 pm_wakeup_event(&hdev->dev, 2000);
3645 }
3646 }
3647
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003648 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3649 /* Enable hub to send remote wakeup for all ports. */
3650 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3651 status = set_port_feature(hdev,
3652 port1 |
3653 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3654 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3655 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3656 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3657 }
3658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659
Harvey Harrison441b62c2008-03-03 16:08:34 -08003660 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003661
Petr Mladek37ebb542014-09-19 17:32:23 +02003662 /* stop hub_wq and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003663 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665}
3666
3667static int hub_resume(struct usb_interface *intf)
3668{
Alan Stern5e6effa2008-03-03 15:15:51 -05003669 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
Alan Stern5e6effa2008-03-03 15:15:51 -05003671 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003672 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 return 0;
3674}
3675
Alan Sternb41a60e2007-05-30 15:39:33 -04003676static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003677{
Alan Sternb41a60e2007-05-30 15:39:33 -04003678 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003679
Alan Stern5e6effa2008-03-03 15:15:51 -05003680 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003681 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003682 return 0;
3683}
3684
Alan Stern54515fe2007-05-30 15:38:58 -04003685/**
3686 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3687 * @rhdev: struct usb_device for the root hub
3688 *
3689 * The USB host controller driver calls this function when its root hub
3690 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003691 * has been reset. The routine marks @rhdev as having lost power.
3692 * When the hub driver is resumed it will take notice and carry out
3693 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3694 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003695 */
3696void usb_root_hub_lost_power(struct usb_device *rhdev)
3697{
3698 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3699 rhdev->reset_resume = 1;
3700}
3701EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3702
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003703static const char * const usb3_lpm_names[] = {
3704 "U0",
3705 "U1",
3706 "U2",
3707 "U3",
3708};
3709
3710/*
3711 * Send a Set SEL control transfer to the device, prior to enabling
3712 * device-initiated U1 or U2. This lets the device know the exit latencies from
3713 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3714 * packet from the host.
3715 *
3716 * This function will fail if the SEL or PEL values for udev are greater than
3717 * the maximum allowed values for the link state to be enabled.
3718 */
3719static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3720{
3721 struct usb_set_sel_req *sel_values;
3722 unsigned long long u1_sel;
3723 unsigned long long u1_pel;
3724 unsigned long long u2_sel;
3725 unsigned long long u2_pel;
3726 int ret;
3727
Xenia Ragiadakou38d7f682013-09-04 17:24:45 +03003728 if (udev->state != USB_STATE_CONFIGURED)
3729 return 0;
3730
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003731 /* Convert SEL and PEL stored in ns to us */
3732 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3733 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3734 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3735 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3736
3737 /*
3738 * Make sure that the calculated SEL and PEL values for the link
3739 * state we're enabling aren't bigger than the max SEL/PEL
3740 * value that will fit in the SET SEL control transfer.
3741 * Otherwise the device would get an incorrect idea of the exit
3742 * latency for the link state, and could start a device-initiated
3743 * U1/U2 when the exit latencies are too high.
3744 */
3745 if ((state == USB3_LPM_U1 &&
3746 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3747 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3748 (state == USB3_LPM_U2 &&
3749 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3750 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003751 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 -07003752 usb3_lpm_names[state], u1_sel, u1_pel);
3753 return -EINVAL;
3754 }
3755
3756 /*
3757 * If we're enabling device-initiated LPM for one link state,
3758 * but the other link state has a too high SEL or PEL value,
3759 * just set those values to the max in the Set SEL request.
3760 */
3761 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3762 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3763
3764 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3765 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3766
3767 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3768 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3769
3770 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3771 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3772
3773 /*
3774 * usb_enable_lpm() can be called as part of a failed device reset,
3775 * which may be initiated by an error path of a mass storage driver.
3776 * Therefore, use GFP_NOIO.
3777 */
3778 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3779 if (!sel_values)
3780 return -ENOMEM;
3781
3782 sel_values->u1_sel = u1_sel;
3783 sel_values->u1_pel = u1_pel;
3784 sel_values->u2_sel = cpu_to_le16(u2_sel);
3785 sel_values->u2_pel = cpu_to_le16(u2_pel);
3786
3787 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3788 USB_REQ_SET_SEL,
3789 USB_RECIP_DEVICE,
3790 0, 0,
3791 sel_values, sizeof *(sel_values),
3792 USB_CTRL_SET_TIMEOUT);
3793 kfree(sel_values);
3794 return ret;
3795}
3796
3797/*
3798 * Enable or disable device-initiated U1 or U2 transitions.
3799 */
3800static int usb_set_device_initiated_lpm(struct usb_device *udev,
3801 enum usb3_link_state state, bool enable)
3802{
3803 int ret;
3804 int feature;
3805
3806 switch (state) {
3807 case USB3_LPM_U1:
3808 feature = USB_DEVICE_U1_ENABLE;
3809 break;
3810 case USB3_LPM_U2:
3811 feature = USB_DEVICE_U2_ENABLE;
3812 break;
3813 default:
3814 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3815 __func__, enable ? "enable" : "disable");
3816 return -EINVAL;
3817 }
3818
3819 if (udev->state != USB_STATE_CONFIGURED) {
3820 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3821 "for unconfigured device.\n",
3822 __func__, enable ? "enable" : "disable",
3823 usb3_lpm_names[state]);
3824 return 0;
3825 }
3826
3827 if (enable) {
3828 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003829 * Now send the control transfer to enable device-initiated LPM
3830 * for either U1 or U2.
3831 */
3832 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3833 USB_REQ_SET_FEATURE,
3834 USB_RECIP_DEVICE,
3835 feature,
3836 0, NULL, 0,
3837 USB_CTRL_SET_TIMEOUT);
3838 } else {
3839 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3840 USB_REQ_CLEAR_FEATURE,
3841 USB_RECIP_DEVICE,
3842 feature,
3843 0, NULL, 0,
3844 USB_CTRL_SET_TIMEOUT);
3845 }
3846 if (ret < 0) {
3847 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3848 enable ? "Enable" : "Disable",
3849 usb3_lpm_names[state]);
3850 return -EBUSY;
3851 }
3852 return 0;
3853}
3854
3855static int usb_set_lpm_timeout(struct usb_device *udev,
3856 enum usb3_link_state state, int timeout)
3857{
3858 int ret;
3859 int feature;
3860
3861 switch (state) {
3862 case USB3_LPM_U1:
3863 feature = USB_PORT_FEAT_U1_TIMEOUT;
3864 break;
3865 case USB3_LPM_U2:
3866 feature = USB_PORT_FEAT_U2_TIMEOUT;
3867 break;
3868 default:
3869 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3870 __func__);
3871 return -EINVAL;
3872 }
3873
3874 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3875 timeout != USB3_LPM_DEVICE_INITIATED) {
3876 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3877 "which is a reserved value.\n",
3878 usb3_lpm_names[state], timeout);
3879 return -EINVAL;
3880 }
3881
3882 ret = set_port_feature(udev->parent,
3883 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3884 feature);
3885 if (ret < 0) {
3886 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3887 "error code %i\n", usb3_lpm_names[state],
3888 timeout, ret);
3889 return -EBUSY;
3890 }
3891 if (state == USB3_LPM_U1)
3892 udev->u1_params.timeout = timeout;
3893 else
3894 udev->u2_params.timeout = timeout;
3895 return 0;
3896}
3897
3898/*
3899 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3900 * U1/U2 entry.
3901 *
3902 * We will attempt to enable U1 or U2, but there are no guarantees that the
3903 * control transfers to set the hub timeout or enable device-initiated U1/U2
3904 * will be successful.
3905 *
3906 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3907 * driver know about it. If that call fails, it should be harmless, and just
3908 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3909 */
3910static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3911 enum usb3_link_state state)
3912{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003913 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003914 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3915 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3916
3917 /* If the device says it doesn't have *any* exit latency to come out of
3918 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3919 * state.
3920 */
3921 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3922 (state == USB3_LPM_U2 && u2_mel == 0))
3923 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003924
Sarah Sharp65a95b72012-10-05 10:32:07 -07003925 /*
3926 * First, let the device know about the exit latencies
3927 * associated with the link state we're about to enable.
3928 */
3929 ret = usb_req_set_sel(udev, state);
3930 if (ret < 0) {
3931 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3932 usb3_lpm_names[state]);
3933 return;
3934 }
3935
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003936 /* We allow the host controller to set the U1/U2 timeout internally
3937 * first, so that it can change its schedule to account for the
3938 * additional latency to send data to a device in a lower power
3939 * link state.
3940 */
3941 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3942
3943 /* xHCI host controller doesn't want to enable this LPM state. */
3944 if (timeout == 0)
3945 return;
3946
3947 if (timeout < 0) {
3948 dev_warn(&udev->dev, "Could not enable %s link state, "
3949 "xHCI error %i.\n", usb3_lpm_names[state],
3950 timeout);
3951 return;
3952 }
3953
Lu Baolubf5ce5b2015-11-14 16:26:32 +08003954 if (usb_set_lpm_timeout(udev, state, timeout)) {
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003955 /* If we can't set the parent hub U1/U2 timeout,
3956 * device-initiated LPM won't be allowed either, so let the xHCI
3957 * host know that this link state won't be enabled.
3958 */
3959 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
Lu Baolubf5ce5b2015-11-14 16:26:32 +08003960 } else {
3961 /* Only a configured device will accept the Set Feature
3962 * U1/U2_ENABLE
3963 */
3964 if (udev->actconfig)
3965 usb_set_device_initiated_lpm(udev, state, true);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003966
Lu Baolubf5ce5b2015-11-14 16:26:32 +08003967 /* As soon as usb_set_lpm_timeout(timeout) returns 0, the
3968 * hub-initiated LPM is enabled. Thus, LPM is enabled no
3969 * matter the result of usb_set_device_initiated_lpm().
3970 * The only difference is whether device is able to initiate
3971 * LPM.
3972 */
3973 if (state == USB3_LPM_U1)
3974 udev->usb3_lpm_u1_enabled = 1;
3975 else if (state == USB3_LPM_U2)
3976 udev->usb3_lpm_u2_enabled = 1;
3977 }
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
4148
Alan Sternd388dab2006-07-01 22:14:24 -04004149#else /* CONFIG_PM */
4150
Alan Sternf07600c2007-05-30 15:38:16 -04004151#define hub_suspend NULL
4152#define hub_resume NULL
4153#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004154
4155int usb_disable_lpm(struct usb_device *udev)
4156{
4157 return 0;
4158}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004159EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004160
4161void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004162EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004163
4164int usb_unlocked_disable_lpm(struct usb_device *udev)
4165{
4166 return 0;
4167}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004168EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07004169
4170void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07004171EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07004172
4173int usb_disable_ltm(struct usb_device *udev)
4174{
4175 return 0;
4176}
4177EXPORT_SYMBOL_GPL(usb_disable_ltm);
4178
4179void usb_enable_ltm(struct usb_device *udev) { }
4180EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternc4b51a42013-07-11 14:58:23 -04004181
Stephen Rothwell4a95b1f2014-05-29 18:55:06 +10004182static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
4183 u16 portstatus, u16 portchange)
4184{
4185 return 0;
4186}
4187
Alan Sternc4b51a42013-07-11 14:58:23 -04004188#endif /* CONFIG_PM */
Alan Sternd388dab2006-07-01 22:14:24 -04004189
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
4191/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
4192 *
4193 * Between connect detection and reset signaling there must be a delay
4194 * of 100ms at least for debounce and power-settling. The corresponding
4195 * timer shall restart whenever the downstream port detects a disconnect.
Matthias Beyer469271f2013-10-10 23:41:27 +02004196 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 * Apparently there are some bluetooth and irda-dongles and a number of
4198 * low-speed devices for which this debounce period may last over a second.
4199 * Not covered by the spec - but easy to deal with.
4200 *
4201 * This implementation uses a 1500ms total debounce timeout; if the
4202 * connection isn't stable by then it returns -ETIMEDOUT. It checks
4203 * every 25ms for transient disconnects. When the port status has been
4204 * unchanged for 100ms it returns the port status.
4205 */
Lan Tianyuad493e52013-01-23 04:26:30 +08004206int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207{
4208 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 u16 portchange, portstatus;
4210 unsigned connection = 0xffff;
Dan Williamsd99f6b42014-05-20 18:08:17 -07004211 int total_time, stable_time = 0;
4212 struct usb_port *port_dev = hub->ports[port1 - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
4214 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
4215 ret = hub_port_status(hub, port1, &portstatus, &portchange);
4216 if (ret < 0)
4217 return ret;
4218
4219 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
4220 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004221 if (!must_be_connected ||
4222 (connection == USB_PORT_STAT_CONNECTION))
4223 stable_time += HUB_DEBOUNCE_STEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 if (stable_time >= HUB_DEBOUNCE_STABLE)
4225 break;
4226 } else {
4227 stable_time = 0;
4228 connection = portstatus & USB_PORT_STAT_CONNECTION;
4229 }
4230
4231 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004232 usb_clear_port_feature(hub->hdev, port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 USB_PORT_FEAT_C_CONNECTION);
4234 }
4235
4236 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
4237 break;
4238 msleep(HUB_DEBOUNCE_STEP);
4239 }
4240
Dan Williamsd99f6b42014-05-20 18:08:17 -07004241 dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n",
4242 total_time, stable_time, portstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
4244 if (stable_time < HUB_DEBOUNCE_STABLE)
4245 return -ETIMEDOUT;
4246 return portstatus;
4247}
4248
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004249void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250{
Alan Sternddeac4e2009-01-15 17:03:33 -05004251 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
4252 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05004253 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004255EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
4257#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
4258#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
4259
Alan Stern4326ed02007-07-30 17:08:43 -04004260static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261{
4262 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07004263 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
Sarah Sharpc6515272009-04-27 19:57:26 -07004265 /*
4266 * The host controller will choose the device address,
4267 * instead of the core having chosen it earlier
4268 */
4269 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 return -EINVAL;
4271 if (udev->state == USB_STATE_ADDRESS)
4272 return 0;
4273 if (udev->state != USB_STATE_DEFAULT)
4274 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07004275 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07004276 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004277 else
Sarah Sharpc6515272009-04-27 19:57:26 -07004278 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
4279 USB_REQ_SET_ADDRESS, 0, devnum, 0,
4280 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05004282 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07004283 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004285 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 }
4287 return retval;
4288}
4289
Mathias Nyman890dae82013-09-30 17:26:31 +03004290/*
4291 * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM
4292 * when they're plugged into a USB 2.0 port, but they don't work when LPM is
4293 * enabled.
4294 *
4295 * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the
4296 * device says it supports the new USB 2.0 Link PM errata by setting the BESL
4297 * support bit in the BOS descriptor.
4298 */
4299static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev)
4300{
Dan Williamsd99f6b42014-05-20 18:08:17 -07004301 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
4302 int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
Mathias Nyman890dae82013-09-30 17:26:31 +03004303
4304 if (!udev->usb2_hw_lpm_capable)
4305 return;
4306
Dan Williamsd99f6b42014-05-20 18:08:17 -07004307 if (hub)
4308 connect_type = hub->ports[udev->portnum - 1]->connect_type;
Mathias Nyman890dae82013-09-30 17:26:31 +03004309
Bjørn Mork23c05822014-02-27 14:23:55 +01004310 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) ||
Mathias Nyman890dae82013-09-30 17:26:31 +03004311 connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
4312 udev->usb2_hw_lpm_allowed = 1;
4313 usb_set_usb2_hardware_lpm(udev, 1);
4314 }
4315}
4316
Dan Williams48fc7db2013-12-05 17:07:27 -08004317static int hub_enable_device(struct usb_device *udev)
4318{
4319 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4320
4321 if (!hcd->driver->enable_device)
4322 return 0;
4323 if (udev->state == USB_STATE_ADDRESS)
4324 return 0;
4325 if (udev->state != USB_STATE_DEFAULT)
4326 return -EINVAL;
4327
4328 return hcd->driver->enable_device(hcd, udev);
4329}
4330
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331/* Reset device, (re)assign address, get device descriptor.
4332 * Device connection must be stable, no more debouncing needed.
4333 * Returns device in USB_STATE_ADDRESS, except on error.
4334 *
4335 * If this is called for an already-existing device (as part of
Dan Williams5c79a1e2014-05-20 18:09:26 -07004336 * usb_reset_and_verify_device), the caller must own the device lock and
4337 * the port lock. For a newly detected device that is not accessible
4338 * through any global pointers, it's not necessary to lock the device,
4339 * but it is still necessary to lock the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 */
4341static int
Chase Metzger039b3302015-08-18 20:36:58 -07004342hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 int retry_counter)
4344{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07004346 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 int i, j, retval;
4348 unsigned delay = HUB_SHORT_RESET_TIME;
4349 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004350 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04004351 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
4353 /* root hub ports have a slightly longer reset period
4354 * (from USB 2.0 spec, section 7.1.7.5)
4355 */
4356 if (!hdev->parent) {
4357 delay = HUB_ROOT_RESET_TIME;
4358 if (port1 == hdev->bus->otg_port)
4359 hdev->bus->b_hnp_enable = 0;
4360 }
4361
4362 /* Some low speed devices have problems with the quick delay, so */
4363 /* be a bit pessimistic with those devices. RHbug #23670 */
4364 if (oldspeed == USB_SPEED_LOW)
4365 delay = HUB_LONG_RESET_TIME;
4366
Todd E Brandt6fecd4f2014-05-19 10:55:32 -07004367 mutex_lock(&hdev->bus->usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
Luben Tuikov07194ab2011-02-11 11:33:10 -08004369 /* Reset the device; full speed may morph to high speed */
4370 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07004371 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08004372 if (retval < 0) /* error or disconnect */
4373 goto fail;
4374 /* success, speed is known */
4375
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 retval = -ENODEV;
4377
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004378 /* Don't allow speed changes at reset, except usb 3.0 to faster */
4379 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed &&
4380 !(oldspeed == USB_SPEED_SUPER && udev->speed > oldspeed)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 dev_dbg(&udev->dev, "device reset changed speed!\n");
4382 goto fail;
4383 }
4384 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04004385
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
4387 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004388 * For Wireless USB devices, ep0 max packet is always 512 (tho
4389 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 */
4391 switch (udev->speed) {
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004392 case USB_SPEED_SUPER_PLUS:
Sarah Sharp6b403b02009-04-27 19:54:10 -07004393 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08004394 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004395 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004398 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 break;
4400 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
4401 /* to determine the ep0 maxpacket size, try to read
4402 * the device descriptor to get bMaxPacketSize0 and
4403 * then correct our initial guess.
4404 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004405 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 break;
4407 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004408 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 break;
4410 default:
4411 goto fail;
4412 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004413
4414 if (udev->speed == USB_SPEED_WIRELESS)
4415 speed = "variable speed Wireless";
4416 else
4417 speed = usb_speed_string(udev->speed);
4418
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004419 if (udev->speed < USB_SPEED_SUPER)
Sarah Sharpc6515272009-04-27 19:57:26 -07004420 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004421 "%s %s USB device number %d using %s\n",
4422 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05004423 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
4425 /* Set up TT records, if needed */
4426 if (hdev->tt) {
4427 udev->tt = hdev->tt;
4428 udev->ttport = hdev->ttport;
4429 } else if (udev->speed != USB_SPEED_HIGH
4430 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05004431 if (!hub->tt.hub) {
4432 dev_err(&udev->dev, "parent hub has no TT\n");
4433 retval = -EINVAL;
4434 goto fail;
4435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 udev->tt = &hub->tt;
4437 udev->ttport = port1;
4438 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004439
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
4441 * Because device hardware and firmware is sometimes buggy in
4442 * this area, and this is how Linux has done it for ages.
4443 * Change it cautiously.
4444 *
Dan Williams48fc7db2013-12-05 17:07:27 -08004445 * NOTE: If use_new_scheme() is true we will start by issuing
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
4447 * so it may help with some non-standards-compliant devices.
4448 * Otherwise we start with SET_ADDRESS and then try to read the
4449 * first 8 bytes of the device descriptor to get the ep0 maxpacket
4450 * value.
4451 */
4452 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Dan Williams48fc7db2013-12-05 17:07:27 -08004453 bool did_new_scheme = false;
4454
4455 if (use_new_scheme(udev, retry_counter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 struct usb_device_descriptor *buf;
4457 int r = 0;
4458
Dan Williams48fc7db2013-12-05 17:07:27 -08004459 did_new_scheme = true;
4460 retval = hub_enable_device(udev);
Oliver Neukum938569e2014-02-27 10:57:10 +01004461 if (retval < 0) {
4462 dev_err(&udev->dev,
4463 "hub failed to enable device, error %d\n",
4464 retval);
Dan Williams48fc7db2013-12-05 17:07:27 -08004465 goto fail;
Oliver Neukum938569e2014-02-27 10:57:10 +01004466 }
Dan Williams48fc7db2013-12-05 17:07:27 -08004467
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468#define GET_DESCRIPTOR_BUFSIZE 64
4469 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
4470 if (!buf) {
4471 retval = -ENOMEM;
4472 continue;
4473 }
4474
Alan Sternb89ee192007-05-11 10:19:04 -04004475 /* Retry on all errors; some devices are flakey.
4476 * 255 is for WUSB devices, we actually need to use
4477 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 */
4479 for (j = 0; j < 3; ++j) {
4480 buf->bMaxPacketSize0 = 0;
4481 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
4482 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
4483 USB_DT_DEVICE << 8, 0,
4484 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02004485 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004487 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 if (buf->bDescriptorType ==
4489 USB_DT_DEVICE) {
4490 r = 0;
4491 break;
4492 }
4493 /* FALL THROUGH */
4494 default:
4495 if (r == 0)
4496 r = -EPROTO;
4497 break;
4498 }
4499 if (r == 0)
4500 break;
4501 }
4502 udev->descriptor.bMaxPacketSize0 =
4503 buf->bMaxPacketSize0;
4504 kfree(buf);
4505
Andiry Xu75d7cf72011-09-13 16:41:11 -07004506 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 if (retval < 0) /* error or disconnect */
4508 goto fail;
4509 if (oldspeed != udev->speed) {
4510 dev_dbg(&udev->dev,
4511 "device reset changed speed!\n");
4512 retval = -ENODEV;
4513 goto fail;
4514 }
4515 if (r) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004516 if (r != -ENODEV)
4517 dev_err(&udev->dev, "device descriptor read/64, error %d\n",
4518 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 retval = -EMSGSIZE;
4520 continue;
4521 }
4522#undef GET_DESCRIPTOR_BUFSIZE
4523 }
4524
Matthias Beyer469271f2013-10-10 23:41:27 +02004525 /*
4526 * If device is WUSB, we already assigned an
4527 * unauthorized address in the Connect Ack sequence;
4528 * authorization will assign the final address.
4529 */
Sarah Sharpc6515272009-04-27 19:57:26 -07004530 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004531 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
4532 retval = hub_set_address(udev, devnum);
4533 if (retval >= 0)
4534 break;
4535 msleep(200);
4536 }
4537 if (retval < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004538 if (retval != -ENODEV)
4539 dev_err(&udev->dev, "device not accepting address %d, error %d\n",
4540 devnum, retval);
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004541 goto fail;
4542 }
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004543 if (udev->speed >= USB_SPEED_SUPER) {
Sarah Sharpc6515272009-04-27 19:57:26 -07004544 devnum = udev->devnum;
4545 dev_info(&udev->dev,
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004546 "%s SuperSpeed%s USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07004547 (udev->config) ? "reset" : "new",
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004548 (udev->speed == USB_SPEED_SUPER_PLUS) ? "Plus" : "",
Alan Stern3b29b682011-02-22 09:53:41 -05004549 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07004550 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004551
4552 /* cope with hardware quirkiness:
4553 * - let SET_ADDRESS settle, some device hardware wants it
4554 * - read ep0 maxpacket even for high and low speed,
4555 */
4556 msleep(10);
Dan Williams48fc7db2013-12-05 17:07:27 -08004557 /* use_new_scheme() checks the speed which may have
4558 * changed since the initial look so we cache the result
4559 * in did_new_scheme
4560 */
4561 if (did_new_scheme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 break;
Matthias Beyer469271f2013-10-10 23:41:27 +02004563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
4565 retval = usb_get_device_descriptor(udev, 8);
4566 if (retval < 8) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004567 if (retval != -ENODEV)
4568 dev_err(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004569 "device descriptor read/8, error %d\n",
4570 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 if (retval >= 0)
4572 retval = -EMSGSIZE;
4573 } else {
4574 retval = 0;
4575 break;
4576 }
4577 }
4578 if (retval)
4579 goto fail;
4580
Elric Fud8aec3d2012-03-26 21:16:02 +08004581 /*
4582 * Some superspeed devices have finished the link training process
4583 * and attached to a superspeed hub port, but the device descriptor
4584 * got from those devices show they aren't superspeed devices. Warm
4585 * reset the port attached by the devices can fix them.
4586 */
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004587 if ((udev->speed >= USB_SPEED_SUPER) &&
Elric Fud8aec3d2012-03-26 21:16:02 +08004588 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4589 dev_err(&udev->dev, "got a wrong device descriptor, "
4590 "warm reset device\n");
4591 hub_port_reset(hub, port1, udev,
4592 HUB_BH_RESET_TIME, true);
4593 retval = -EINVAL;
4594 goto fail;
4595 }
4596
Sarah Sharp6b403b02009-04-27 19:54:10 -07004597 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004598 udev->speed >= USB_SPEED_SUPER)
Sarah Sharp6b403b02009-04-27 19:54:10 -07004599 i = 512;
4600 else
4601 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07004602 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04004603 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04004605 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 retval = -EMSGSIZE;
4607 goto fail;
4608 }
Alan Stern56626a72010-10-14 15:25:21 -04004609 if (udev->speed == USB_SPEED_FULL)
4610 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4611 else
4612 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004614 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004616
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4618 if (retval < (signed)sizeof(udev->descriptor)) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004619 if (retval != -ENODEV)
4620 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4621 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 if (retval >= 0)
4623 retval = -ENOMSG;
4624 goto fail;
4625 }
4626
Alan Sternad87e032015-12-10 15:27:21 -05004627 usb_detect_quirks(udev);
4628
Andiry Xu1ff4df52011-09-23 14:19:48 -07004629 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4630 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004631 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004632 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004633 usb_set_lpm_parameters(udev);
4634 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004635 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004636
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004638 /* notify HCD that we have a device connected and addressed */
4639 if (hcd->driver->update_device)
4640 hcd->driver->update_device(hcd, udev);
Mathias Nyman890dae82013-09-30 17:26:31 +03004641 hub_set_initial_usb2_lpm_policy(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004643 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004645 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004646 }
Todd E Brandt6fecd4f2014-05-19 10:55:32 -07004647 mutex_unlock(&hdev->bus->usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 return retval;
4649}
4650
4651static void
Chase Metzger039b3302015-08-18 20:36:58 -07004652check_highspeed(struct usb_hub *hub, struct usb_device *udev, int port1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653{
4654 struct usb_qualifier_descriptor *qual;
4655 int status;
4656
Johan Hovold2a159382014-08-25 17:51:26 +02004657 if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER)
4658 return;
4659
Chase Metzger039b3302015-08-18 20:36:58 -07004660 qual = kmalloc(sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 if (qual == NULL)
4662 return;
4663
Chase Metzger039b3302015-08-18 20:36:58 -07004664 status = usb_get_descriptor(udev, USB_DT_DEVICE_QUALIFIER, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 qual, sizeof *qual);
4666 if (status == sizeof *qual) {
4667 dev_info(&udev->dev, "not running at top speed; "
4668 "connect to a high speed hub\n");
4669 /* hub LEDs are probably harder to miss than syslog */
4670 if (hub->has_indicators) {
4671 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08004672 queue_delayed_work(system_power_efficient_wq,
4673 &hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 }
4675 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004676 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677}
4678
4679static unsigned
Chase Metzger039b3302015-08-18 20:36:58 -07004680hub_power_remaining(struct usb_hub *hub)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681{
4682 struct usb_device *hdev = hub->hdev;
4683 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004684 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
Alan Stern55c52712005-11-23 12:03:12 -05004686 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 return 0;
4688
Alan Stern55c52712005-11-23 12:03:12 -05004689 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4690 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004691 struct usb_port *port_dev = hub->ports[port1 - 1];
4692 struct usb_device *udev = port_dev->child;
4693 unsigned unit_load;
4694 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
4696 if (!udev)
4697 continue;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004698 if (hub_is_superspeed(udev))
4699 unit_load = 150;
4700 else
4701 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004703 /*
4704 * Unconfigured devices may not use more than one unit load,
4705 * or 8mA for OTG ports
4706 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 if (udev->actconfig)
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +01004708 delta = usb_get_max_power(udev, udev->actconfig);
Alan Stern55c52712005-11-23 12:03:12 -05004709 else if (port1 != udev->bus->otg_port || hdev->parent)
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004710 delta = unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 else
Alan Stern55c52712005-11-23 12:03:12 -05004712 delta = 8;
4713 if (delta > hub->mA_per_port)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004714 dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n",
4715 delta, hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 remaining -= delta;
4717 }
4718 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004719 dev_warn(hub->intfdev, "%dmA over power budget!\n",
Matthias Beyer469271f2013-10-10 23:41:27 +02004720 -remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 remaining = 0;
4722 }
4723 return remaining;
4724}
4725
Dan Williamsaf376a42014-05-20 18:09:15 -07004726static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
4727 u16 portchange)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 int status, i;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004730 unsigned unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 struct usb_device *hdev = hub->hdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004733 struct usb_port *port_dev = hub->ports[port1 - 1];
Dan Williamsaf376a42014-05-20 18:09:15 -07004734 struct usb_device *udev = port_dev->child;
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004735 static int unreliable_port = -1;
Alan Stern8808f002008-04-28 11:06:55 -04004736
Alan Stern24618b02008-04-28 11:06:28 -04004737 /* Disconnect any existing devices under this port */
Peter Chenb76baa82012-11-09 09:44:43 +08004738 if (udev) {
Peter Chenb2108f12014-11-04 11:14:31 +08004739 if (hcd->usb_phy && !hdev->parent)
Antoine Tenart3d46e732014-09-24 23:05:50 +04004740 usb_phy_notify_disconnect(hcd->usb_phy, udev->speed);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004741 usb_disconnect(&port_dev->child);
Peter Chenb76baa82012-11-09 09:44:43 +08004742 }
Alan Stern24618b02008-04-28 11:06:28 -04004743
Alan Stern253e0572009-10-27 15:20:13 -04004744 /* We can forget about a "removed" device when there's a physical
4745 * disconnect or the connect status changes.
4746 */
4747 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4748 (portchange & USB_PORT_STAT_C_CONNECTION))
4749 clear_bit(port1, hub->removed_bits);
4750
Alan Stern5257d972008-09-22 14:43:08 -04004751 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4752 USB_PORT_STAT_C_ENABLE)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004753 status = hub_port_debounce_be_stable(hub, port1);
Alan Stern5257d972008-09-22 14:43:08 -04004754 if (status < 0) {
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004755 if (status != -ENODEV &&
4756 port1 != unreliable_port &&
4757 printk_ratelimit())
Takashi Iwaidd5f5002014-08-19 17:37:55 +02004758 dev_err(&port_dev->dev, "connect-debounce failed\n");
Alan Stern5257d972008-09-22 14:43:08 -04004759 portstatus &= ~USB_PORT_STAT_CONNECTION;
Oliver Neukum5ee0f802014-07-14 15:39:49 +02004760 unreliable_port = port1;
Alan Stern5257d972008-09-22 14:43:08 -04004761 } else {
4762 portstatus = status;
4763 }
4764 }
4765
Alan Stern253e0572009-10-27 15:20:13 -04004766 /* Return now if debouncing failed or nothing is connected or
4767 * the device was "removed".
4768 */
4769 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4770 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
Deepak Dasfbaecff2015-01-21 23:39:58 +05304772 /*
4773 * maybe switch power back on (e.g. root hub was reset)
4774 * but only if the port isn't owned by someone else.
4775 */
Dan Williams9262c192014-05-20 18:08:12 -07004776 if (hub_is_port_power_switchable(hub)
Deepak Dasfbaecff2015-01-21 23:39:58 +05304777 && !port_is_power_on(hub, portstatus)
4778 && !port_dev->port_owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004780
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 if (portstatus & USB_PORT_STAT_ENABLE)
Matthias Beyer469271f2013-10-10 23:41:27 +02004782 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 return;
4784 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004785 if (hub_is_superspeed(hub->hdev))
4786 unit_load = 150;
4787 else
4788 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
Alan Sterne9e88fb2013-03-27 16:14:01 -04004790 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
4793 /* reallocate for each attempt, since references
4794 * to the previous one can escape in various ways
4795 */
4796 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4797 if (!udev) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004798 dev_err(&port_dev->dev,
4799 "couldn't allocate usb_device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 goto done;
4801 }
4802
4803 usb_set_device_state(udev, USB_STATE_POWERED);
Matthias Beyer469271f2013-10-10 23:41:27 +02004804 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004805 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004806 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004807
Mathias Nyman8a1b2722015-12-10 09:59:25 +02004808 /* Devices connected to SuperSpeed hubs are USB 3.0 or later */
Sarah Sharp131dec32010-12-06 21:00:19 -08004809 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004810 udev->speed = USB_SPEED_SUPER;
4811 else
4812 udev->speed = USB_SPEED_UNKNOWN;
4813
Alan Stern3b29b682011-02-22 09:53:41 -05004814 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004815 if (udev->devnum <= 0) {
4816 status = -ENOTCONN; /* Don't retry */
4817 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004818 }
4819
Sarah Sharpe7b77172009-04-27 19:54:26 -07004820 /* reset (non-USB 3.0 devices) and get descriptor */
Dan Williams5c79a1e2014-05-20 18:09:26 -07004821 usb_lock_port(port_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 status = hub_port_init(hub, udev, port1, i);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004823 usb_unlock_port(port_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 if (status < 0)
4825 goto loop;
4826
Phil Dibowitz93362a82010-07-22 00:05:01 +02004827 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4828 msleep(1000);
4829
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 /* consecutive bus-powered hubs aren't reliable; they can
4831 * violate the voltage drop budget. if the new child has
4832 * a "powered" LED, users should notice we didn't enable it
4833 * (without reading syslog), even without per-port LEDs
4834 * on the parent.
4835 */
4836 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004837 && udev->bus_mA <= unit_load) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 u16 devstat;
4839
4840 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4841 &devstat);
Alan Stern15b73362013-07-30 15:35:40 -04004842 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 dev_dbg(&udev->dev, "get status %d ?\n", status);
4844 goto loop_disable;
4845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4847 dev_err(&udev->dev,
4848 "can't connect bus-powered hub "
4849 "to this port\n");
4850 if (hub->has_indicators) {
4851 hub->indicator[port1-1] =
4852 INDICATOR_AMBER_BLINK;
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08004853 queue_delayed_work(
4854 system_power_efficient_wq,
4855 &hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 }
4857 status = -ENOTCONN; /* Don't retry */
4858 goto loop_disable;
4859 }
4860 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004861
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 /* check for devices running slower than they could */
4863 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4864 && udev->speed == USB_SPEED_FULL
4865 && highspeed_hubs != 0)
Chase Metzger039b3302015-08-18 20:36:58 -07004866 check_highspeed(hub, udev, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004868 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 * udev becomes globally accessible, although presumably
4870 * no one will look at it until hdev is unlocked.
4871 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 status = 0;
4873
Dan Williamsd8521af2014-05-20 18:08:28 -07004874 mutex_lock(&usb_port_peer_mutex);
4875
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 /* We mustn't add new devices if the parent hub has
4877 * been disconnected; we would race with the
4878 * recursively_mark_NOTATTACHED() routine.
4879 */
4880 spin_lock_irq(&device_state_lock);
4881 if (hdev->state == USB_STATE_NOTATTACHED)
4882 status = -ENOTCONN;
4883 else
Dan Williamsd99f6b42014-05-20 18:08:17 -07004884 port_dev->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 spin_unlock_irq(&device_state_lock);
Dan Williamsd8521af2014-05-20 18:08:28 -07004886 mutex_unlock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887
4888 /* Run it through the hoops (find a driver, etc) */
4889 if (!status) {
4890 status = usb_new_device(udev);
4891 if (status) {
Dan Williamsd8521af2014-05-20 18:08:28 -07004892 mutex_lock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 spin_lock_irq(&device_state_lock);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004894 port_dev->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 spin_unlock_irq(&device_state_lock);
Dan Williamsd8521af2014-05-20 18:08:28 -07004896 mutex_unlock(&usb_port_peer_mutex);
Tony Zheng01ed67d2014-10-17 19:43:02 +08004897 } else {
4898 if (hcd->usb_phy && !hdev->parent)
4899 usb_phy_notify_connect(hcd->usb_phy,
4900 udev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 }
4902 }
4903
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 if (status)
4905 goto loop_disable;
4906
4907 status = hub_power_remaining(hub);
4908 if (status)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004909 dev_dbg(hub->intfdev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
4911 return;
4912
4913loop_disable:
4914 hub_port_disable(hub, port1, 1);
4915loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004916 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004917 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004918 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004920 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 break;
4922 }
Alan Stern3a311552008-05-20 16:58:29 -04004923 if (hub->hdev->parent ||
4924 !hcd->driver->port_handed_over ||
Alan Sterne9e88fb2013-03-27 16:14:01 -04004925 !(hcd->driver->port_handed_over)(hcd, port1)) {
4926 if (status != -ENOTCONN && status != -ENODEV)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004927 dev_err(&port_dev->dev,
4928 "unable to enumerate USB device\n");
Alan Sterne9e88fb2013-03-27 16:14:01 -04004929 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004930
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931done:
4932 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304933 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4934 hcd->driver->relinquish_port(hcd, port1);
Dan Williamsaf376a42014-05-20 18:09:15 -07004935
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936}
4937
Dan Williamsaf376a42014-05-20 18:09:15 -07004938/* Handle physical or logical connection change events.
4939 * This routine is called when:
4940 * a port connection-change occurs;
4941 * a port enable-change occurs (often caused by EMI);
4942 * usb_reset_and_verify_device() encounters changed descriptors (as from
4943 * a firmware download)
4944 * caller already locked the hub
4945 */
4946static void hub_port_connect_change(struct usb_hub *hub, int port1,
4947 u16 portstatus, u16 portchange)
Dan Williams5c79a1e2014-05-20 18:09:26 -07004948 __must_hold(&port_dev->status_lock)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004949{
Dan Williamsaf376a42014-05-20 18:09:15 -07004950 struct usb_port *port_dev = hub->ports[port1 - 1];
4951 struct usb_device *udev = port_dev->child;
4952 int status = -ENODEV;
Sarah Sharp714b07b2012-01-24 13:53:18 -08004953
Dan Williamsaf376a42014-05-20 18:09:15 -07004954 dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus,
4955 portchange, portspeed(hub, portstatus));
4956
4957 if (hub->has_indicators) {
4958 set_port_led(hub, port1, HUB_LED_AUTO);
4959 hub->indicator[port1-1] = INDICATOR_AUTO;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004960 }
4961
Dan Williamsaf376a42014-05-20 18:09:15 -07004962#ifdef CONFIG_USB_OTG
4963 /* during HNP, don't repeat the debounce */
4964 if (hub->hdev->bus->is_b_host)
4965 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4966 USB_PORT_STAT_C_ENABLE);
4967#endif
Sarah Sharp714b07b2012-01-24 13:53:18 -08004968
Dan Williamsaf376a42014-05-20 18:09:15 -07004969 /* Try to resuscitate an existing device */
4970 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4971 udev->state != USB_STATE_NOTATTACHED) {
4972 if (portstatus & USB_PORT_STAT_ENABLE) {
4973 status = 0; /* Nothing to do */
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01004974#ifdef CONFIG_PM
Dan Williamsaf376a42014-05-20 18:09:15 -07004975 } else if (udev->state == USB_STATE_SUSPENDED &&
4976 udev->persist_enabled) {
4977 /* For a suspended device, treat this as a
4978 * remote wakeup event.
4979 */
Dan Williams5c79a1e2014-05-20 18:09:26 -07004980 usb_unlock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004981 status = usb_remote_wakeup(udev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004982 usb_lock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004983#endif
4984 } else {
4985 /* Don't resuscitate */;
4986 }
Sarah Sharp714b07b2012-01-24 13:53:18 -08004987 }
Dan Williamsaf376a42014-05-20 18:09:15 -07004988 clear_bit(port1, hub->change_bits);
4989
Dan Williams5c79a1e2014-05-20 18:09:26 -07004990 /* successfully revalidated the connection */
Dan Williamsaf376a42014-05-20 18:09:15 -07004991 if (status == 0)
4992 return;
4993
Dan Williams5c79a1e2014-05-20 18:09:26 -07004994 usb_unlock_port(port_dev);
Dan Williamsaf376a42014-05-20 18:09:15 -07004995 hub_port_connect(hub, port1, portstatus, portchange);
Dan Williams5c79a1e2014-05-20 18:09:26 -07004996 usb_lock_port(port_dev);
Sarah Sharp714b07b2012-01-24 13:53:18 -08004997}
4998
Dan Williamsaf376a42014-05-20 18:09:15 -07004999static void port_event(struct usb_hub *hub, int port1)
Dan Williams5c79a1e2014-05-20 18:09:26 -07005000 __must_hold(&port_dev->status_lock)
Dan Williamsaf376a42014-05-20 18:09:15 -07005001{
Zhuang Jin Can7671bd12015-01-26 23:30:39 +08005002 int connect_change;
Dan Williamsaf376a42014-05-20 18:09:15 -07005003 struct usb_port *port_dev = hub->ports[port1 - 1];
5004 struct usb_device *udev = port_dev->child;
5005 struct usb_device *hdev = hub->hdev;
5006 u16 portstatus, portchange;
5007
5008 connect_change = test_bit(port1, hub->change_bits);
5009 clear_bit(port1, hub->event_bits);
5010 clear_bit(port1, hub->wakeup_bits);
5011
5012 if (hub_port_status(hub, port1, &portstatus, &portchange) < 0)
5013 return;
5014
5015 if (portchange & USB_PORT_STAT_C_CONNECTION) {
5016 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
5017 connect_change = 1;
5018 }
5019
5020 if (portchange & USB_PORT_STAT_C_ENABLE) {
5021 if (!connect_change)
5022 dev_dbg(&port_dev->dev, "enable change, status %08x\n",
5023 portstatus);
5024 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
5025
5026 /*
5027 * EM interference sometimes causes badly shielded USB devices
5028 * to be shutdown by the hub, this hack enables them again.
5029 * Works at least with mouse driver.
5030 */
5031 if (!(portstatus & USB_PORT_STAT_ENABLE)
5032 && !connect_change && udev) {
5033 dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n");
5034 connect_change = 1;
5035 }
5036 }
5037
5038 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
5039 u16 status = 0, unused;
5040
5041 dev_dbg(&port_dev->dev, "over-current change\n");
5042 usb_clear_port_feature(hdev, port1,
5043 USB_PORT_FEAT_C_OVER_CURRENT);
5044 msleep(100); /* Cool down */
5045 hub_power_on(hub, true);
5046 hub_port_status(hub, port1, &status, &unused);
5047 if (status & USB_PORT_STAT_OVERCURRENT)
5048 dev_err(&port_dev->dev, "over-current condition\n");
5049 }
5050
5051 if (portchange & USB_PORT_STAT_C_RESET) {
5052 dev_dbg(&port_dev->dev, "reset change\n");
5053 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET);
5054 }
5055 if ((portchange & USB_PORT_STAT_C_BH_RESET)
5056 && hub_is_superspeed(hdev)) {
5057 dev_dbg(&port_dev->dev, "warm reset change\n");
5058 usb_clear_port_feature(hdev, port1,
5059 USB_PORT_FEAT_C_BH_PORT_RESET);
5060 }
5061 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
5062 dev_dbg(&port_dev->dev, "link state change\n");
5063 usb_clear_port_feature(hdev, port1,
5064 USB_PORT_FEAT_C_PORT_LINK_STATE);
5065 }
5066 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
5067 dev_warn(&port_dev->dev, "config error\n");
5068 usb_clear_port_feature(hdev, port1,
5069 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
5070 }
5071
Dan Williams097a1552014-05-20 18:09:20 -07005072 /* skip port actions that require the port to be powered on */
5073 if (!pm_runtime_active(&port_dev->dev))
5074 return;
5075
Dan Williamsaf376a42014-05-20 18:09:15 -07005076 if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange))
5077 connect_change = 1;
5078
5079 /*
5080 * Warm reset a USB3 protocol port if it's in
5081 * SS.Inactive state.
5082 */
Dan Williams3cd12f92014-05-29 12:58:46 -07005083 if (hub_port_warm_reset_required(hub, port1, portstatus)) {
Dan Williamsaf376a42014-05-20 18:09:15 -07005084 dev_dbg(&port_dev->dev, "do warm reset\n");
5085 if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION)
5086 || udev->state == USB_STATE_NOTATTACHED) {
5087 if (hub_port_reset(hub, port1, NULL,
5088 HUB_BH_RESET_TIME, true) < 0)
5089 hub_port_disable(hub, port1, 1);
Zhuang Jin Can7671bd12015-01-26 23:30:39 +08005090 } else {
5091 usb_unlock_port(port_dev);
5092 usb_lock_device(udev);
5093 usb_reset_device(udev);
5094 usb_unlock_device(udev);
5095 usb_lock_port(port_dev);
5096 connect_change = 0;
5097 }
Dan Williamsaf376a42014-05-20 18:09:15 -07005098 }
5099
5100 if (connect_change)
5101 hub_port_connect_change(hub, port1, portstatus, portchange);
5102}
5103
Petr Mladek32a69582014-09-19 17:32:21 +02005104static void hub_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 struct usb_device *hdev;
5107 struct usb_interface *intf;
5108 struct usb_hub *hub;
5109 struct device *hub_dev;
5110 u16 hubstatus;
5111 u16 hubchange;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
Petr Mladek32a69582014-09-19 17:32:21 +02005114 hub = container_of(work, struct usb_hub, events);
Petr Mladekeb6e2922014-09-19 17:32:20 +02005115 hdev = hub->hdev;
5116 hub_dev = hub->intfdev;
5117 intf = to_usb_interface(hub_dev);
Petr Mladek32a69582014-09-19 17:32:21 +02005118
Petr Mladekeb6e2922014-09-19 17:32:20 +02005119 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
5120 hdev->state, hdev->maxchild,
5121 /* NOTE: expects max 15 ports... */
5122 (u16) hub->change_bits[0],
5123 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124
Petr Mladekeb6e2922014-09-19 17:32:20 +02005125 /* Lock the device, then check to see if we were
5126 * disconnected while waiting for the lock to succeed. */
5127 usb_lock_device(hdev);
5128 if (unlikely(hub->disconnected))
Petr Mladek32a69582014-09-19 17:32:21 +02005129 goto out_hdev_lock;
Petr Mladekeb6e2922014-09-19 17:32:20 +02005130
5131 /* If the hub has died, clean up after it */
5132 if (hdev->state == USB_STATE_NOTATTACHED) {
5133 hub->error = -ENODEV;
5134 hub_quiesce(hub, HUB_DISCONNECT);
Petr Mladek32a69582014-09-19 17:32:21 +02005135 goto out_hdev_lock;
Petr Mladekeb6e2922014-09-19 17:32:20 +02005136 }
5137
5138 /* Autoresume */
5139 ret = usb_autopm_get_interface(intf);
5140 if (ret) {
5141 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Petr Mladek32a69582014-09-19 17:32:21 +02005142 goto out_hdev_lock;
Petr Mladekeb6e2922014-09-19 17:32:20 +02005143 }
5144
5145 /* If this is an inactive hub, do nothing */
5146 if (hub->quiescing)
5147 goto out_autopm;
5148
5149 if (hub->error) {
5150 dev_dbg(hub_dev, "resetting for error %d\n", hub->error);
5151
5152 ret = usb_reset_device(hdev);
Alan Stern40f122f2006-11-09 14:44:33 -05005153 if (ret) {
Petr Mladekeb6e2922014-09-19 17:32:20 +02005154 dev_dbg(hub_dev, "error resetting hub: %d\n", ret);
5155 goto out_autopm;
Alan Stern40f122f2006-11-09 14:44:33 -05005156 }
5157
Petr Mladekeb6e2922014-09-19 17:32:20 +02005158 hub->nerrors = 0;
5159 hub->error = 0;
5160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
Petr Mladekeb6e2922014-09-19 17:32:20 +02005162 /* deal with port status changes */
5163 for (i = 1; i <= hdev->maxchild; i++) {
5164 struct usb_port *port_dev = hub->ports[i - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
Petr Mladekeb6e2922014-09-19 17:32:20 +02005166 if (test_bit(i, hub->event_bits)
5167 || test_bit(i, hub->change_bits)
5168 || test_bit(i, hub->wakeup_bits)) {
5169 /*
5170 * The get_noresume and barrier ensure that if
5171 * the port was in the process of resuming, we
5172 * flush that work and keep the port active for
5173 * the duration of the port_event(). However,
5174 * if the port is runtime pm suspended
5175 * (powered-off), we leave it in that state, run
5176 * an abbreviated port_event(), and move on.
5177 */
5178 pm_runtime_get_noresume(&port_dev->dev);
5179 pm_runtime_barrier(&port_dev->dev);
5180 usb_lock_port(port_dev);
5181 port_event(hub, i);
5182 usb_unlock_port(port_dev);
5183 pm_runtime_put_sync(&port_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 }
Petr Mladekeb6e2922014-09-19 17:32:20 +02005185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
Petr Mladekeb6e2922014-09-19 17:32:20 +02005187 /* deal with hub status changes */
5188 if (test_and_clear_bit(0, hub->event_bits) == 0)
5189 ; /* do nothing */
5190 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
5191 dev_err(hub_dev, "get_hub_status failed\n");
5192 else {
5193 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
5194 dev_dbg(hub_dev, "power change\n");
5195 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
5196 if (hubstatus & HUB_STATUS_LOCAL_POWER)
5197 /* FIXME: Is this always true? */
5198 hub->limited_power = 1;
5199 else
5200 hub->limited_power = 0;
Dan Williamsaf376a42014-05-20 18:09:15 -07005201 }
Petr Mladekeb6e2922014-09-19 17:32:20 +02005202 if (hubchange & HUB_CHANGE_OVERCURRENT) {
5203 u16 status = 0;
5204 u16 unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205
Petr Mladekeb6e2922014-09-19 17:32:20 +02005206 dev_dbg(hub_dev, "over-current change\n");
5207 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
5208 msleep(500); /* Cool down */
5209 hub_power_on(hub, true);
5210 hub_hub_status(hub, &status, &unused);
5211 if (status & HUB_STATUS_OVERCURRENT)
5212 dev_err(hub_dev, "over-current condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 }
Petr Mladekeb6e2922014-09-19 17:32:20 +02005214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215
Petr Mladekeb6e2922014-09-19 17:32:20 +02005216out_autopm:
5217 /* Balance the usb_autopm_get_interface() above */
5218 usb_autopm_put_interface_no_suspend(intf);
Petr Mladek32a69582014-09-19 17:32:21 +02005219out_hdev_lock:
Petr Mladekeb6e2922014-09-19 17:32:20 +02005220 usb_unlock_device(hdev);
Petr Mladek32a69582014-09-19 17:32:21 +02005221
5222 /* Balance the stuff in kick_hub_wq() and allow autosuspend */
5223 usb_autopm_put_interface(intf);
Petr Mladekeb6e2922014-09-19 17:32:20 +02005224 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225}
5226
Németh Márton1e927d92010-01-10 15:34:53 +01005227static const struct usb_device_id hub_id_table[] = {
Ming Leie6f30de2012-10-24 11:59:24 +08005228 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
Matthias Beyer469271f2013-10-10 23:41:27 +02005229 | USB_DEVICE_ID_MATCH_INT_CLASS,
Ming Leie6f30de2012-10-24 11:59:24 +08005230 .idVendor = USB_VENDOR_GENESYS_LOGIC,
5231 .bInterfaceClass = USB_CLASS_HUB,
5232 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
5234 .bDeviceClass = USB_CLASS_HUB},
5235 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
5236 .bInterfaceClass = USB_CLASS_HUB},
5237 { } /* Terminating entry */
5238};
5239
Chase Metzger039b3302015-08-18 20:36:58 -07005240MODULE_DEVICE_TABLE(usb, hub_id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241
5242static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 .name = "hub",
5244 .probe = hub_probe,
5245 .disconnect = hub_disconnect,
5246 .suspend = hub_suspend,
5247 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04005248 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04005249 .pre_reset = hub_pre_reset,
5250 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02005251 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05005253 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254};
5255
5256int usb_hub_init(void)
5257{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 if (usb_register(&hub_driver) < 0) {
5259 printk(KERN_ERR "%s: can't register hub driver\n",
5260 usbcore_name);
5261 return -1;
5262 }
5263
Petr Mladek32a69582014-09-19 17:32:21 +02005264 /*
5265 * The workqueue needs to be freezable to avoid interfering with
5266 * USB-PERSIST port handover. Otherwise it might see that a full-speed
5267 * device was gone before the EHCI controller had handed its port
5268 * over to the companion full-speed controller.
Petr Mladek32a69582014-09-19 17:32:21 +02005269 */
Petr Mladek638139e2014-09-19 17:32:24 +02005270 hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0);
Petr Mladek32a69582014-09-19 17:32:21 +02005271 if (hub_wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
5274 /* Fall through if kernel_thread failed */
5275 usb_deregister(&hub_driver);
Petr Mladek32a69582014-09-19 17:32:21 +02005276 pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277
5278 return -1;
5279}
5280
5281void usb_hub_cleanup(void)
5282{
Petr Mladek32a69582014-09-19 17:32:21 +02005283 destroy_workqueue(hub_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284
5285 /*
5286 * Hub resources are freed for us by usb_deregister. It calls
5287 * usb_driver_purge on every device which in turn calls that
5288 * devices disconnect function if it is using this driver.
5289 * The hub_disconnect function takes care of releasing the
5290 * individual hub resources. -greg
5291 */
5292 usb_deregister(&hub_driver);
5293} /* usb_hub_cleanup() */
5294
Alan Sterneb764c42008-03-03 15:16:04 -05005295static int descriptors_changed(struct usb_device *udev,
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005296 struct usb_device_descriptor *old_device_descriptor,
5297 struct usb_host_bos *old_bos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298{
Alan Sterneb764c42008-03-03 15:16:04 -05005299 int changed = 0;
5300 unsigned index;
5301 unsigned serial_len = 0;
5302 unsigned len;
5303 unsigned old_length;
5304 int length;
5305 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306
Alan Sterneb764c42008-03-03 15:16:04 -05005307 if (memcmp(&udev->descriptor, old_device_descriptor,
5308 sizeof(*old_device_descriptor)) != 0)
5309 return 1;
5310
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005311 if ((old_bos && !udev->bos) || (!old_bos && udev->bos))
5312 return 1;
5313 if (udev->bos) {
Xenia Ragiadakoub9a10482013-08-31 04:40:49 +03005314 len = le16_to_cpu(udev->bos->desc->wTotalLength);
5315 if (len != le16_to_cpu(old_bos->desc->wTotalLength))
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005316 return 1;
Xenia Ragiadakoub9a10482013-08-31 04:40:49 +03005317 if (memcmp(udev->bos->desc, old_bos->desc, len))
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005318 return 1;
5319 }
5320
Alan Sterneb764c42008-03-03 15:16:04 -05005321 /* Since the idVendor, idProduct, and bcdDevice values in the
5322 * device descriptor haven't changed, we will assume the
5323 * Manufacturer and Product strings haven't changed either.
5324 * But the SerialNumber string could be different (e.g., a
5325 * different flash card of the same brand).
5326 */
5327 if (udev->serial)
5328 serial_len = strlen(udev->serial) + 1;
5329
5330 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05005332 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
5333 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 }
Alan Sterneb764c42008-03-03 15:16:04 -05005335
Oliver Neukum0cc1a512008-01-10 10:31:48 +01005336 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 if (buf == NULL) {
5338 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
5339 /* assume the worst */
5340 return 1;
5341 }
5342 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05005343 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
5345 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05005346 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 dev_dbg(&udev->dev, "config index %d, error %d\n",
5348 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05005349 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 break;
5351 }
Chase Metzger039b3302015-08-18 20:36:58 -07005352 if (memcmp(buf, udev->rawdescriptors[index], old_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 != 0) {
5354 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05005355 index,
5356 ((struct usb_config_descriptor *) buf)->
5357 bConfigurationValue);
5358 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 break;
5360 }
5361 }
Alan Sterneb764c42008-03-03 15:16:04 -05005362
5363 if (!changed && serial_len) {
5364 length = usb_string(udev, udev->descriptor.iSerialNumber,
5365 buf, serial_len);
5366 if (length + 1 != serial_len) {
5367 dev_dbg(&udev->dev, "serial string error %d\n",
5368 length);
5369 changed = 1;
5370 } else if (memcmp(buf, udev->serial, length) != 0) {
5371 dev_dbg(&udev->dev, "serial string changed\n");
5372 changed = 1;
5373 }
5374 }
5375
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05005377 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378}
5379
5380/**
Ming Lei742120c2008-06-18 22:00:29 +08005381 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5383 *
Alan Stern79efa092006-06-01 13:33:42 -04005384 * WARNING - don't use this routine to reset a composite device
5385 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08005386 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 *
5388 * Do a port reset, reassign the device's address, and establish its
5389 * former operating configuration. If the reset fails, or the device's
5390 * descriptors change from their values before the reset, or the original
5391 * configuration and altsettings cannot be restored, a flag will be set
Petr Mladek37ebb542014-09-19 17:32:23 +02005392 * telling hub_wq to pretend the device has been disconnected and then
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 * re-connected. All drivers will be unbound, and the device will be
5394 * re-enumerated and probed all over again.
5395 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005396 * Return: 0 if the reset succeeded, -ENODEV if the device has been
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 * flagged for logical disconnection, or some other negative error code
5398 * if the reset wasn't even attempted.
5399 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005400 * Note:
Dan Williams5c79a1e2014-05-20 18:09:26 -07005401 * The caller must own the device lock and the port lock, the latter is
5402 * taken by usb_reset_device(). For example, it's safe to use
5403 * usb_reset_device() from a driver probe() routine after downloading
5404 * new firmware. For calls that might not occur during probe(), drivers
5405 * should lock the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04005406 *
5407 * Locking exception: This routine may also be called from within an
5408 * autoresume handler. Such usage won't conflict with other tasks
5409 * holding the device lock because these tasks should always call
Dan Williams5c79a1e2014-05-20 18:09:26 -07005410 * usb_autopm_resume_device(), thereby preventing any unwanted
5411 * autoresume. The autoresume handler is expected to have already
5412 * acquired the port lock before calling this routine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 */
Ming Lei742120c2008-06-18 22:00:29 +08005414static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415{
5416 struct usb_device *parent_hdev = udev->parent;
5417 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005418 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 struct usb_device_descriptor descriptor = udev->descriptor;
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005420 struct usb_host_bos *bos;
Hans de Goede7a7b5622013-11-08 16:37:26 +01005421 int i, j, ret = 0;
Alan Stern12c3da32005-11-23 12:09:52 -05005422 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423
5424 if (udev->state == USB_STATE_NOTATTACHED ||
5425 udev->state == USB_STATE_SUSPENDED) {
5426 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5427 udev->state);
5428 return -EINVAL;
5429 }
5430
Dan Williams5c79a1e2014-05-20 18:09:26 -07005431 if (!parent_hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 return -EISDIR;
Dan Williams5c79a1e2014-05-20 18:09:26 -07005433
Lan Tianyuad493e52013-01-23 04:26:30 +08005434 parent_hub = usb_hub_to_struct_hub(parent_hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435
Sarah Sharpdcc01c02013-09-30 17:26:29 +03005436 /* Disable USB2 hardware LPM.
5437 * It will be re-enabled by the enumeration process.
5438 */
5439 if (udev->usb2_hw_lpm_enabled == 1)
5440 usb_set_usb2_hardware_lpm(udev, 0);
5441
Sarah Sharpf74631e2012-06-25 12:08:08 -07005442 /* Disable LPM and LTM while we reset the device and reinstall the alt
5443 * settings. Device-initiated LPM settings, and system exit latency
5444 * settings are cleared when the device is reset, so we have to set
5445 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005446 */
5447 ret = usb_unlocked_disable_lpm(udev);
5448 if (ret) {
5449 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
Hans Yang464ad8c2015-12-01 16:54:59 +08005450 goto re_enumerate_no_bos;
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005451 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07005452 ret = usb_disable_ltm(udev);
5453 if (ret) {
5454 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
5455 __func__);
Hans Yang464ad8c2015-12-01 16:54:59 +08005456 goto re_enumerate_no_bos;
Sarah Sharpf74631e2012-06-25 12:08:08 -07005457 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005458
Hans Yang464ad8c2015-12-01 16:54:59 +08005459 bos = udev->bos;
5460 udev->bos = NULL;
5461
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
5463
5464 /* ep0 maxpacket size may change; let the HCD know about it.
5465 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07005466 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04005468 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 break;
5470 }
Alan Sternd5cbad42006-08-11 16:52:39 -04005471
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 if (ret < 0)
5473 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005474
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 /* Device might have changed firmware (DFU or similar) */
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005476 if (descriptors_changed(udev, &descriptor, bos)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 dev_info(&udev->dev, "device firmware changed\n");
5478 udev->descriptor = descriptor; /* for disconnect() calls */
5479 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005480 }
Alan Stern4fe03872009-02-26 10:21:02 -05005481
5482 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 if (!udev->actconfig)
5484 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005485
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005486 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005487 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
5488 if (ret < 0) {
5489 dev_warn(&udev->dev,
5490 "Busted HC? Not enough HCD resources for "
5491 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005492 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005493 goto re_enumerate;
5494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
5496 USB_REQ_SET_CONFIGURATION, 0,
5497 udev->actconfig->desc.bConfigurationValue, 0,
5498 NULL, 0, USB_CTRL_SET_TIMEOUT);
5499 if (ret < 0) {
5500 dev_err(&udev->dev,
5501 "can't restore configuration #%d (error=%d)\n",
5502 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005503 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005505 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005506 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 usb_set_device_state(udev, USB_STATE_CONFIGURED);
5508
Alan Stern4fe03872009-02-26 10:21:02 -05005509 /* Put interfaces back into the same altsettings as before.
5510 * Don't bother to send the Set-Interface request for interfaces
5511 * that were already in altsetting 0; besides being unnecessary,
5512 * many devices can't handle it. Instead just reset the host-side
5513 * endpoint state.
5514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005516 struct usb_host_config *config = udev->actconfig;
5517 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 struct usb_interface_descriptor *desc;
5519
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05005521 if (desc->bAlternateSetting == 0) {
5522 usb_disable_interface(udev, intf, true);
5523 usb_enable_interface(udev, intf, true);
5524 ret = 0;
5525 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08005526 /* Let the bandwidth allocation function know that this
5527 * device has been reset, and it will have to use
5528 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005529 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08005530 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05005531 ret = usb_set_interface(udev, desc->bInterfaceNumber,
5532 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08005533 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05005534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 if (ret < 0) {
5536 dev_err(&udev->dev, "failed to restore interface %d "
5537 "altsetting %d (error=%d)\n",
5538 desc->bInterfaceNumber,
5539 desc->bAlternateSetting,
5540 ret);
5541 goto re_enumerate;
5542 }
Hans de Goede7a7b5622013-11-08 16:37:26 +01005543 /* Resetting also frees any allocated streams */
5544 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++)
5545 intf->cur_altsetting->endpoint[j].streams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 }
5547
5548done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07005549 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Sarah Sharpde68bab2013-09-30 17:26:28 +03005550 usb_set_usb2_hardware_lpm(udev, 1);
Alan Stern78d9a482008-06-23 16:00:40 -04005551 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07005552 usb_enable_ltm(udev);
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005553 usb_release_bos_descriptor(udev);
5554 udev->bos = bos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 return 0;
Matthias Beyer469271f2013-10-10 23:41:27 +02005556
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557re_enumerate:
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005558 usb_release_bos_descriptor(udev);
5559 udev->bos = bos;
Hans Yang464ad8c2015-12-01 16:54:59 +08005560re_enumerate_no_bos:
5561 /* LPM state doesn't matter when we're about to destroy the device. */
5562 hub_port_logical_disconnect(parent_hub, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564}
5565
5566/**
5567 * usb_reset_device - warn interface drivers and perform a USB port reset
5568 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5569 *
Alan Stern79efa092006-06-01 13:33:42 -04005570 * Warns all drivers bound to registered interfaces (using their pre_reset
5571 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08005572 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04005573 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005574 * Return: The same as for usb_reset_and_verify_device().
Alan Stern79efa092006-06-01 13:33:42 -04005575 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005576 * Note:
Alan Stern79efa092006-06-01 13:33:42 -04005577 * The caller must own the device lock. For example, it's safe to use
5578 * this from a driver probe() routine after downloading new firmware.
5579 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08005580 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04005581 *
5582 * If an interface is currently being probed or disconnected, we assume
5583 * its driver knows how to handle resets. For all other interfaces,
5584 * if the driver doesn't have pre_reset and post_reset methods then
5585 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04005586 */
Ming Lei742120c2008-06-18 22:00:29 +08005587int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04005588{
5589 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05005590 int i;
Ming Lei4d769de2013-02-22 16:34:22 -08005591 unsigned int noio_flag;
Dan Williams5c79a1e2014-05-20 18:09:26 -07005592 struct usb_port *port_dev;
Alan Stern79efa092006-06-01 13:33:42 -04005593 struct usb_host_config *config = udev->actconfig;
Dan Williams5c79a1e2014-05-20 18:09:26 -07005594 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
Alan Stern79efa092006-06-01 13:33:42 -04005595
5596 if (udev->state == USB_STATE_NOTATTACHED ||
5597 udev->state == USB_STATE_SUSPENDED) {
5598 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5599 udev->state);
5600 return -EINVAL;
5601 }
5602
Dan Williams5c79a1e2014-05-20 18:09:26 -07005603 if (!udev->parent) {
5604 /* this requires hcd-specific logic; see ohci_restart() */
5605 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
5606 return -EISDIR;
5607 }
5608
5609 port_dev = hub->ports[udev->portnum - 1];
5610
Ming Lei4d769de2013-02-22 16:34:22 -08005611 /*
5612 * Don't allocate memory with GFP_KERNEL in current
5613 * context to avoid possible deadlock if usb mass
5614 * storage interface or usbnet interface(iSCSI case)
5615 * is included in current configuration. The easist
5616 * approach is to do it for every device reset,
5617 * because the device 'memalloc_noio' flag may have
5618 * not been set before reseting the usb device.
5619 */
5620 noio_flag = memalloc_noio_save();
5621
Alan Stern645daaa2006-08-30 15:47:02 -04005622 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05005623 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04005624
Alan Stern79efa092006-06-01 13:33:42 -04005625 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005626 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005627 struct usb_interface *cintf = config->interface[i];
5628 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005629 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05005630
5631 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005632 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04005633 if (drv->pre_reset && drv->post_reset)
5634 unbind = (drv->pre_reset)(cintf);
5635 else if (cintf->condition ==
5636 USB_INTERFACE_BOUND)
5637 unbind = 1;
5638 if (unbind)
5639 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005640 }
5641 }
5642 }
5643
Dan Williams5c79a1e2014-05-20 18:09:26 -07005644 usb_lock_port(port_dev);
Ming Lei742120c2008-06-18 22:00:29 +08005645 ret = usb_reset_and_verify_device(udev);
Dan Williams5c79a1e2014-05-20 18:09:26 -07005646 usb_unlock_port(port_dev);
Alan Stern79efa092006-06-01 13:33:42 -04005647
5648 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005649 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005650 struct usb_interface *cintf = config->interface[i];
5651 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005652 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05005653
Alan Stern78d9a482008-06-23 16:00:40 -04005654 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005655 drv = to_usb_driver(cintf->dev.driver);
5656 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04005657 rebind = (drv->post_reset)(cintf);
5658 else if (cintf->condition ==
5659 USB_INTERFACE_BOUND)
5660 rebind = 1;
Alan Stern6aec0442014-03-12 11:30:38 -04005661 if (rebind)
5662 cintf->needs_binding = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005663 }
Alan Stern79efa092006-06-01 13:33:42 -04005664 }
Alan Stern6aec0442014-03-12 11:30:38 -04005665 usb_unbind_and_rebind_marked_interfaces(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005666 }
5667
Alan Stern94fcda12006-11-20 11:38:46 -05005668 usb_autosuspend_device(udev);
Ming Lei4d769de2013-02-22 16:34:22 -08005669 memalloc_noio_restore(noio_flag);
Alan Stern79efa092006-06-01 13:33:42 -04005670 return ret;
5671}
Ming Lei742120c2008-06-18 22:00:29 +08005672EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005673
5674
5675/**
5676 * usb_queue_reset_device - Reset a USB device from an atomic context
5677 * @iface: USB interface belonging to the device to reset
5678 *
5679 * This function can be used to reset a USB device from an atomic
5680 * context, where usb_reset_device() won't work (as it blocks).
5681 *
5682 * Doing a reset via this method is functionally equivalent to calling
5683 * usb_reset_device(), except for the fact that it is delayed to a
5684 * workqueue. This means that any drivers bound to other interfaces
5685 * might be unbound, as well as users from usbfs in user space.
5686 *
5687 * Corner cases:
5688 *
5689 * - Scheduling two resets at the same time from two different drivers
5690 * attached to two different interfaces of the same device is
5691 * possible; depending on how the driver attached to each interface
5692 * handles ->pre_reset(), the second reset might happen or not.
5693 *
Alan Stern524134d2015-01-21 14:02:43 -05005694 * - If the reset is delayed so long that the interface is unbound from
5695 * its driver, the reset will be skipped.
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005696 *
Alan Stern524134d2015-01-21 14:02:43 -05005697 * - This function can be called during .probe(). It can also be called
5698 * during .disconnect(), but doing so is pointless because the reset
5699 * will not occur. If you really want to reset the device during
5700 * .disconnect(), call usb_reset_device() directly -- but watch out
5701 * for nested unbinding issues!
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005702 */
5703void usb_queue_reset_device(struct usb_interface *iface)
5704{
Alan Stern524134d2015-01-21 14:02:43 -05005705 if (schedule_work(&iface->reset_ws))
5706 usb_get_intf(iface);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005707}
5708EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c792012-09-05 13:44:32 +08005709
5710/**
5711 * usb_hub_find_child - Get the pointer of child device
5712 * attached to the port which is specified by @port1.
5713 * @hdev: USB device belonging to the usb hub
5714 * @port1: port num to indicate which port the child device
5715 * is attached to.
5716 *
5717 * USB drivers call this function to get hub's child device
5718 * pointer.
5719 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005720 * Return: %NULL if input param is invalid and
Lan Tianyuff823c792012-09-05 13:44:32 +08005721 * child's usb_device pointer if non-NULL.
5722 */
5723struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5724 int port1)
5725{
Lan Tianyuad493e52013-01-23 04:26:30 +08005726 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyuff823c792012-09-05 13:44:32 +08005727
5728 if (port1 < 1 || port1 > hdev->maxchild)
5729 return NULL;
5730 return hub->ports[port1 - 1]->child;
5731}
5732EXPORT_SYMBOL_GPL(usb_hub_find_child);
Lan Tianyud5575422012-09-05 13:44:33 +08005733
Lan Tianyud2123fd2013-01-21 22:18:00 +08005734void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
5735 struct usb_hub_descriptor *desc)
5736{
Dan Williamsd99f6b42014-05-20 18:08:17 -07005737 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyud2123fd2013-01-21 22:18:00 +08005738 enum usb_port_connect_type connect_type;
5739 int i;
5740
Dan Williamsd99f6b42014-05-20 18:08:17 -07005741 if (!hub)
5742 return;
5743
Lan Tianyud2123fd2013-01-21 22:18:00 +08005744 if (!hub_is_superspeed(hdev)) {
5745 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005746 struct usb_port *port_dev = hub->ports[i - 1];
Lan Tianyud2123fd2013-01-21 22:18:00 +08005747
Dan Williamsd99f6b42014-05-20 18:08:17 -07005748 connect_type = port_dev->connect_type;
Lan Tianyud2123fd2013-01-21 22:18:00 +08005749 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5750 u8 mask = 1 << (i%8);
5751
5752 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005753 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
Lan Tianyud2123fd2013-01-21 22:18:00 +08005754 desc->u.hs.DeviceRemovable[i/8] |= mask;
5755 }
5756 }
5757 }
5758 } else {
5759 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable);
5760
5761 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005762 struct usb_port *port_dev = hub->ports[i - 1];
Lan Tianyud2123fd2013-01-21 22:18:00 +08005763
Dan Williamsd99f6b42014-05-20 18:08:17 -07005764 connect_type = port_dev->connect_type;
Lan Tianyud2123fd2013-01-21 22:18:00 +08005765 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5766 u16 mask = 1 << i;
5767
5768 if (!(port_removable & mask)) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005769 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
Lan Tianyud2123fd2013-01-21 22:18:00 +08005770 port_removable |= mask;
5771 }
5772 }
5773 }
5774
5775 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
5776 }
5777}
5778
Lan Tianyud5575422012-09-05 13:44:33 +08005779#ifdef CONFIG_ACPI
5780/**
5781 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5782 * @hdev: USB device belonging to the usb hub
5783 * @port1: port num of the port
5784 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005785 * Return: Port's acpi handle if successful, %NULL if params are
5786 * invalid.
Lan Tianyud5575422012-09-05 13:44:33 +08005787 */
5788acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5789 int port1)
5790{
Lan Tianyuad493e52013-01-23 04:26:30 +08005791 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyud5575422012-09-05 13:44:33 +08005792
Mathias Nyman413412612013-06-18 17:28:48 +03005793 if (!hub)
5794 return NULL;
5795
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +01005796 return ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
Lan Tianyud5575422012-09-05 13:44:33 +08005797}
5798#endif