blob: 781546269d269e4b8d60ee8117d1db4d173517d3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB hub driver.
3 *
4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999 Johannes Erdfelt
6 * (C) Copyright 1999 Gregory P. Smith
7 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
8 *
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/completion.h>
16#include <linux/sched.h>
17#include <linux/list.h>
18#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/ioctl.h>
20#include <linux/usb.h>
21#include <linux/usbdevice_fs.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020022#include <linux/usb/hcd.h>
Richard Zhao925aa462012-07-11 11:09:28 +080023#include <linux/usb/otg.h>
Phil Dibowitz93362a82010-07-22 00:05:01 +020024#include <linux/usb/quirks.h>
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070025#include <linux/kthread.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010026#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080027#include <linux/freezer.h>
Theodore Ts'ob04b3152012-07-04 11:22:20 -040028#include <linux/random.h>
Lan Tianyuad493e52013-01-23 04:26:30 +080029#include <linux/pm_qos.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/uaccess.h>
32#include <asm/byteorder.h>
33
Lan Tianyu6e30d7c2013-01-11 20:10:38 +080034#include "hub.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -080036/* if we are in debug mode, always announce new devices */
37#ifdef DEBUG
38#ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
39#define CONFIG_USB_ANNOUNCE_NEW_DEVICES
40#endif
41#endif
42
Ming Leie6f30de2012-10-24 11:59:24 +080043#define USB_VENDOR_GENESYS_LOGIC 0x05e3
44#define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01
45
John Youndbe79bb2001-09-17 00:00:00 -070046static inline int hub_is_superspeed(struct usb_device *hdev)
47{
Aman Deep7bf01182011-12-08 12:05:22 +053048 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
John Youndbe79bb2001-09-17 00:00:00 -070049}
Alan Stern1bb5f662006-11-06 11:56:13 -050050
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -070051/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -050052 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
54static DEFINE_SPINLOCK(device_state_lock);
55
56/* khubd's worklist and its lock */
57static DEFINE_SPINLOCK(hub_event_lock);
58static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
59
60/* Wakes up khubd */
61static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
62
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070063static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* cycle leds on hubs that aren't blinking for attention */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103066static bool blinkenlights = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067module_param (blinkenlights, bool, S_IRUGO);
68MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
69
70/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020071 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
72 * 10 seconds to send reply for the initial 64-byte descriptor request.
73 */
74/* define initial 64-byte descriptor request timeout in milliseconds */
75static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
76module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +080077MODULE_PARM_DESC(initial_descriptor_timeout,
78 "initial 64-byte descriptor request timeout in milliseconds "
79 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020080
81/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * As of 2.6.10 we introduce a new USB device initialization scheme which
83 * closely resembles the way Windows works. Hopefully it will be compatible
84 * with a wider range of devices than the old scheme. However some previously
85 * working devices may start giving rise to "device not accepting address"
86 * errors; if that happens the user can try the old scheme by adjusting the
87 * following module parameters.
88 *
89 * For maximum flexibility there are two boolean parameters to control the
90 * hub driver's behavior. On the first initialization attempt, if the
91 * "old_scheme_first" parameter is set then the old scheme will be used,
92 * otherwise the new scheme is used. If that fails and "use_both_schemes"
93 * is set, then the driver will make another attempt, using the other scheme.
94 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103095static bool old_scheme_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
97MODULE_PARM_DESC(old_scheme_first,
98 "start with the old device initialization scheme");
99
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030100static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
102MODULE_PARM_DESC(use_both_schemes,
103 "try the other device initialization scheme if the "
104 "first one fails");
105
Alan Stern32fe0192007-10-10 16:27:07 -0400106/* Mutual exclusion for EHCI CF initialization. This interferes with
107 * port reset on some companion controllers.
108 */
109DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
110EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
111
Lan Tianyuad493e52013-01-23 04:26:30 +0800112#define HUB_DEBOUNCE_TIMEOUT 2000
Alan Stern948fea32008-04-28 11:07:07 -0400113#define HUB_DEBOUNCE_STEP 25
114#define HUB_DEBOUNCE_STABLE 100
115
Ming Lei742120c2008-06-18 22:00:29 +0800116static int usb_reset_and_verify_device(struct usb_device *udev);
117
Sarah Sharp131dec32010-12-06 21:00:19 -0800118static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Sarah Sharp131dec32010-12-06 21:00:19 -0800120 if (hub_is_superspeed(hub->hdev))
121 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500122 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500124 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return "1.5 Mb/s";
126 else
127 return "12 Mb/s";
128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/* Note that hdev or one of its children must be locked! */
Lan Tianyuad493e52013-01-23 04:26:30 +0800131struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Lan Tianyuff823c72012-09-05 13:44:32 +0800133 if (!hdev || !hdev->actconfig || !hdev->maxchild)
Alan Stern251180842009-07-22 14:41:18 -0400134 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return usb_get_intfdata(hdev->actconfig->interface[0]);
136}
137
Sarah Sharpd9b20992012-02-20 08:31:26 -0800138static int usb_device_supports_lpm(struct usb_device *udev)
139{
140 /* USB 2.1 (and greater) devices indicate LPM support through
141 * their USB 2.0 Extended Capabilities BOS descriptor.
142 */
143 if (udev->speed == USB_SPEED_HIGH) {
144 if (udev->bos->ext_cap &&
145 (USB_LPM_SUPPORT &
146 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
147 return 1;
148 return 0;
149 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800150
151 /* All USB 3.0 must support LPM, but we need their max exit latency
152 * information from the SuperSpeed Extended Capabilities BOS descriptor.
153 */
154 if (!udev->bos->ss_cap) {
155 dev_warn(&udev->dev, "No LPM exit latency info found. "
156 "Power management will be impacted.\n");
157 return 0;
158 }
159 if (udev->parent->lpm_capable)
160 return 1;
161
162 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. "
163 "Power management will be impacted.\n");
Sarah Sharpd9b20992012-02-20 08:31:26 -0800164 return 0;
165}
166
Sarah Sharp51e0a012012-02-20 12:02:19 -0800167/*
168 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
169 * either U1 or U2.
170 */
171static void usb_set_lpm_mel(struct usb_device *udev,
172 struct usb3_lpm_parameters *udev_lpm_params,
173 unsigned int udev_exit_latency,
174 struct usb_hub *hub,
175 struct usb3_lpm_parameters *hub_lpm_params,
176 unsigned int hub_exit_latency)
177{
178 unsigned int total_mel;
179 unsigned int device_mel;
180 unsigned int hub_mel;
181
182 /*
183 * Calculate the time it takes to transition all links from the roothub
184 * to the parent hub into U0. The parent hub must then decode the
185 * packet (hub header decode latency) to figure out which port it was
186 * bound for.
187 *
188 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
189 * means 0.1us). Multiply that by 100 to get nanoseconds.
190 */
191 total_mel = hub_lpm_params->mel +
192 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
193
194 /*
195 * How long will it take to transition the downstream hub's port into
196 * U0? The greater of either the hub exit latency or the device exit
197 * latency.
198 *
199 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
200 * Multiply that by 1000 to get nanoseconds.
201 */
202 device_mel = udev_exit_latency * 1000;
203 hub_mel = hub_exit_latency * 1000;
204 if (device_mel > hub_mel)
205 total_mel += device_mel;
206 else
207 total_mel += hub_mel;
208
209 udev_lpm_params->mel = total_mel;
210}
211
212/*
213 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
214 * a transition from either U1 or U2.
215 */
216static void usb_set_lpm_pel(struct usb_device *udev,
217 struct usb3_lpm_parameters *udev_lpm_params,
218 unsigned int udev_exit_latency,
219 struct usb_hub *hub,
220 struct usb3_lpm_parameters *hub_lpm_params,
221 unsigned int hub_exit_latency,
222 unsigned int port_to_port_exit_latency)
223{
224 unsigned int first_link_pel;
225 unsigned int hub_pel;
226
227 /*
228 * First, the device sends an LFPS to transition the link between the
229 * device and the parent hub into U0. The exit latency is the bigger of
230 * the device exit latency or the hub exit latency.
231 */
232 if (udev_exit_latency > hub_exit_latency)
233 first_link_pel = udev_exit_latency * 1000;
234 else
235 first_link_pel = hub_exit_latency * 1000;
236
237 /*
238 * When the hub starts to receive the LFPS, there is a slight delay for
239 * it to figure out that one of the ports is sending an LFPS. Then it
240 * will forward the LFPS to its upstream link. The exit latency is the
241 * delay, plus the PEL that we calculated for this hub.
242 */
243 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
244
245 /*
246 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
247 * is the greater of the two exit latencies.
248 */
249 if (first_link_pel > hub_pel)
250 udev_lpm_params->pel = first_link_pel;
251 else
252 udev_lpm_params->pel = hub_pel;
253}
254
255/*
256 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
257 * when a device initiates a transition to U0, until when it will receive the
258 * first packet from the host controller.
259 *
260 * Section C.1.5.1 describes the four components to this:
261 * - t1: device PEL
262 * - t2: time for the ERDY to make it from the device to the host.
263 * - t3: a host-specific delay to process the ERDY.
264 * - t4: time for the packet to make it from the host to the device.
265 *
266 * t3 is specific to both the xHCI host and the platform the host is integrated
267 * into. The Intel HW folks have said it's negligible, FIXME if a different
268 * vendor says otherwise.
269 */
270static void usb_set_lpm_sel(struct usb_device *udev,
271 struct usb3_lpm_parameters *udev_lpm_params)
272{
273 struct usb_device *parent;
274 unsigned int num_hubs;
275 unsigned int total_sel;
276
277 /* t1 = device PEL */
278 total_sel = udev_lpm_params->pel;
279 /* How many external hubs are in between the device & the root port. */
280 for (parent = udev->parent, num_hubs = 0; parent->parent;
281 parent = parent->parent)
282 num_hubs++;
283 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
284 if (num_hubs > 0)
285 total_sel += 2100 + 250 * (num_hubs - 1);
286
287 /* t4 = 250ns * num_hubs */
288 total_sel += 250 * num_hubs;
289
290 udev_lpm_params->sel = total_sel;
291}
292
293static void usb_set_lpm_parameters(struct usb_device *udev)
294{
295 struct usb_hub *hub;
296 unsigned int port_to_port_delay;
297 unsigned int udev_u1_del;
298 unsigned int udev_u2_del;
299 unsigned int hub_u1_del;
300 unsigned int hub_u2_del;
301
302 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
303 return;
304
Lan Tianyuad493e52013-01-23 04:26:30 +0800305 hub = usb_hub_to_struct_hub(udev->parent);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800306 /* It doesn't take time to transition the roothub into U0, since it
307 * doesn't have an upstream link.
308 */
309 if (!hub)
310 return;
311
312 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
313 udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
314 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
315 hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
316
317 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
318 hub, &udev->parent->u1_params, hub_u1_del);
319
320 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
321 hub, &udev->parent->u2_params, hub_u2_del);
322
323 /*
324 * Appendix C, section C.2.2.2, says that there is a slight delay from
325 * when the parent hub notices the downstream port is trying to
326 * transition to U0 to when the hub initiates a U0 transition on its
327 * upstream port. The section says the delays are tPort2PortU1EL and
328 * tPort2PortU2EL, but it doesn't define what they are.
329 *
330 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
331 * about the same delays. Use the maximum delay calculations from those
332 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
333 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
334 * assume the device exit latencies they are talking about are the hub
335 * exit latencies.
336 *
337 * What do we do if the U2 exit latency is less than the U1 exit
338 * latency? It's possible, although not likely...
339 */
340 port_to_port_delay = 1;
341
342 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
343 hub, &udev->parent->u1_params, hub_u1_del,
344 port_to_port_delay);
345
346 if (hub_u2_del > hub_u1_del)
347 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
348 else
349 port_to_port_delay = 1 + hub_u1_del;
350
351 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
352 hub, &udev->parent->u2_params, hub_u2_del,
353 port_to_port_delay);
354
355 /* Now that we've got PEL, calculate SEL. */
356 usb_set_lpm_sel(udev, &udev->u1_params);
357 usb_set_lpm_sel(udev, &udev->u2_params);
358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/* USB 2.0 spec Section 11.24.4.5 */
John Youndbe79bb2001-09-17 00:00:00 -0700361static int get_hub_descriptor(struct usb_device *hdev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
John Youndbe79bb2001-09-17 00:00:00 -0700363 int i, ret, size;
364 unsigned dtype;
365
366 if (hub_is_superspeed(hdev)) {
367 dtype = USB_DT_SS_HUB;
368 size = USB_DT_SS_HUB_SIZE;
369 } else {
370 dtype = USB_DT_HUB;
371 size = sizeof(struct usb_hub_descriptor);
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 for (i = 0; i < 3; i++) {
375 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
376 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
John Youndbe79bb2001-09-17 00:00:00 -0700377 dtype << 8, 0, data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 USB_CTRL_GET_TIMEOUT);
379 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
380 return ret;
381 }
382 return -EINVAL;
383}
384
385/*
386 * USB 2.0 spec Section 11.24.2.1
387 */
388static int clear_hub_feature(struct usb_device *hdev, int feature)
389{
390 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
391 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
392}
393
394/*
395 * USB 2.0 spec Section 11.24.2.2
396 */
Lan Tianyuad493e52013-01-23 04:26:30 +0800397int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
400 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
401 NULL, 0, 1000);
402}
403
404/*
405 * USB 2.0 spec Section 11.24.2.13
406 */
407static int set_port_feature(struct usb_device *hdev, int port1, int feature)
408{
409 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
410 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
411 NULL, 0, 1000);
412}
413
414/*
415 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
416 * for info about using port indicators
417 */
418static void set_port_led(
419 struct usb_hub *hub,
420 int port1,
421 int selector
422)
423{
424 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
425 USB_PORT_FEAT_INDICATOR);
426 if (status < 0)
427 dev_dbg (hub->intfdev,
428 "port %d indicator %s status %d\n",
429 port1,
430 ({ char *s; switch (selector) {
431 case HUB_LED_AMBER: s = "amber"; break;
432 case HUB_LED_GREEN: s = "green"; break;
433 case HUB_LED_OFF: s = "off"; break;
434 case HUB_LED_AUTO: s = "auto"; break;
435 default: s = "??"; break;
436 }; s; }),
437 status);
438}
439
440#define LED_CYCLE_PERIOD ((2*HZ)/3)
441
David Howellsc4028952006-11-22 14:57:56 +0000442static void led_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
David Howellsc4028952006-11-22 14:57:56 +0000444 struct usb_hub *hub =
445 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct usb_device *hdev = hub->hdev;
447 unsigned i;
448 unsigned changed = 0;
449 int cursor = -1;
450
451 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
452 return;
453
454 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
455 unsigned selector, mode;
456
457 /* 30%-50% duty cycle */
458
459 switch (hub->indicator[i]) {
460 /* cycle marker */
461 case INDICATOR_CYCLE:
462 cursor = i;
463 selector = HUB_LED_AUTO;
464 mode = INDICATOR_AUTO;
465 break;
466 /* blinking green = sw attention */
467 case INDICATOR_GREEN_BLINK:
468 selector = HUB_LED_GREEN;
469 mode = INDICATOR_GREEN_BLINK_OFF;
470 break;
471 case INDICATOR_GREEN_BLINK_OFF:
472 selector = HUB_LED_OFF;
473 mode = INDICATOR_GREEN_BLINK;
474 break;
475 /* blinking amber = hw attention */
476 case INDICATOR_AMBER_BLINK:
477 selector = HUB_LED_AMBER;
478 mode = INDICATOR_AMBER_BLINK_OFF;
479 break;
480 case INDICATOR_AMBER_BLINK_OFF:
481 selector = HUB_LED_OFF;
482 mode = INDICATOR_AMBER_BLINK;
483 break;
484 /* blink green/amber = reserved */
485 case INDICATOR_ALT_BLINK:
486 selector = HUB_LED_GREEN;
487 mode = INDICATOR_ALT_BLINK_OFF;
488 break;
489 case INDICATOR_ALT_BLINK_OFF:
490 selector = HUB_LED_AMBER;
491 mode = INDICATOR_ALT_BLINK;
492 break;
493 default:
494 continue;
495 }
496 if (selector != HUB_LED_AUTO)
497 changed = 1;
498 set_port_led(hub, i + 1, selector);
499 hub->indicator[i] = mode;
500 }
501 if (!changed && blinkenlights) {
502 cursor++;
503 cursor %= hub->descriptor->bNbrPorts;
504 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
505 hub->indicator[cursor] = INDICATOR_CYCLE;
506 changed++;
507 }
508 if (changed)
509 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
510}
511
512/* use a short timeout for hub/port status fetches */
513#define USB_STS_TIMEOUT 1000
514#define USB_STS_RETRIES 5
515
516/*
517 * USB 2.0 spec Section 11.24.2.6
518 */
519static int get_hub_status(struct usb_device *hdev,
520 struct usb_hub_status *data)
521{
522 int i, status = -ETIMEDOUT;
523
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200524 for (i = 0; i < USB_STS_RETRIES &&
525 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
527 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
528 data, sizeof(*data), USB_STS_TIMEOUT);
529 }
530 return status;
531}
532
533/*
534 * USB 2.0 spec Section 11.24.2.7
535 */
536static int get_port_status(struct usb_device *hdev, int port1,
537 struct usb_port_status *data)
538{
539 int i, status = -ETIMEDOUT;
540
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200541 for (i = 0; i < USB_STS_RETRIES &&
542 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
544 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
545 data, sizeof(*data), USB_STS_TIMEOUT);
546 }
547 return status;
548}
549
Alan Stern3eb14912008-03-03 15:15:43 -0500550static int hub_port_status(struct usb_hub *hub, int port1,
551 u16 *status, u16 *change)
552{
553 int ret;
554
555 mutex_lock(&hub->status_mutex);
556 ret = get_port_status(hub->hdev, port1, &hub->status->port);
557 if (ret < 4) {
558 dev_err(hub->intfdev,
559 "%s failed (err = %d)\n", __func__, ret);
560 if (ret >= 0)
561 ret = -EIO;
562 } else {
563 *status = le16_to_cpu(hub->status->port.wPortStatus);
564 *change = le16_to_cpu(hub->status->port.wPortChange);
John Youndbe79bb2001-09-17 00:00:00 -0700565
Alan Stern3eb14912008-03-03 15:15:43 -0500566 ret = 0;
567 }
568 mutex_unlock(&hub->status_mutex);
569 return ret;
570}
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572static void kick_khubd(struct usb_hub *hub)
573{
574 unsigned long flags;
575
576 spin_lock_irqsave(&hub_event_lock, flags);
Roel Kluin2e2c5ee2007-10-26 23:54:35 +0200577 if (!hub->disconnected && list_empty(&hub->event_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 list_add_tail(&hub->event_list, &hub_event_list);
Alan Stern8e4ceb32009-12-07 13:01:37 -0500579
580 /* Suppress autosuspend until khubd runs */
581 usb_autopm_get_interface_no_resume(
582 to_usb_interface(hub->intfdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 wake_up(&khubd_wait);
584 }
585 spin_unlock_irqrestore(&hub_event_lock, flags);
586}
587
588void usb_kick_khubd(struct usb_device *hdev)
589{
Lan Tianyuad493e52013-01-23 04:26:30 +0800590 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Alan Stern251180842009-07-22 14:41:18 -0400591
592 if (hub)
593 kick_khubd(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800596/*
597 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
598 * Notification, which indicates it had initiated remote wakeup.
599 *
600 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
601 * device initiates resume, so the USB core will not receive notice of the
602 * resume through the normal hub interrupt URB.
603 */
604void usb_wakeup_notification(struct usb_device *hdev,
605 unsigned int portnum)
606{
607 struct usb_hub *hub;
608
609 if (!hdev)
610 return;
611
Lan Tianyuad493e52013-01-23 04:26:30 +0800612 hub = usb_hub_to_struct_hub(hdev);
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800613 if (hub) {
614 set_bit(portnum, hub->wakeup_bits);
615 kick_khubd(hub);
616 }
617}
618EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100621static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200623 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400624 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100625 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 unsigned long bits;
627
Alan Sterne0152682007-08-24 15:42:52 -0400628 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 case -ENOENT: /* synchronous unlink */
630 case -ECONNRESET: /* async unlink */
631 case -ESHUTDOWN: /* hardware going away */
632 return;
633
634 default: /* presumably an error */
635 /* Cause a hub reset after 10 consecutive errors */
Alan Sterne0152682007-08-24 15:42:52 -0400636 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if ((++hub->nerrors < 10) || hub->error)
638 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400639 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /* let khubd handle things */
643 case 0: /* we got data: port status changed */
644 bits = 0;
645 for (i = 0; i < urb->actual_length; ++i)
646 bits |= ((unsigned long) ((*hub->buffer)[i]))
647 << (i*8);
648 hub->event_bits[0] = bits;
649 break;
650 }
651
652 hub->nerrors = 0;
653
654 /* Something happened, let khubd figure it out */
655 kick_khubd(hub);
656
657resubmit:
658 if (hub->quiescing)
659 return;
660
661 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
662 && status != -ENODEV && status != -EPERM)
663 dev_err (hub->intfdev, "resubmit --> %d\n", status);
664}
665
666/* USB 2.0 spec Section 11.24.2.3 */
667static inline int
668hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
669{
Alan Sternc2f65952009-11-18 11:37:15 -0500670 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
672 tt, NULL, 0, 1000);
673}
674
675/*
676 * enumeration blocks khubd for a long time. we use keventd instead, since
677 * long blocking there is the exception, not the rule. accordingly, HCDs
678 * talking to TTs must queue control transfers (not just bulk and iso), so
679 * both can talk to the same hub concurrently.
680 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400681static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
David Howellsc4028952006-11-22 14:57:56 +0000683 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400684 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 unsigned long flags;
686
687 spin_lock_irqsave (&hub->tt.lock, flags);
Octavian Purdila3b6054d2012-10-01 22:21:12 +0300688 while (!list_empty(&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400689 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct usb_tt_clear *clear;
691 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400692 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int status;
694
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400695 next = hub->tt.clear_list.next;
696 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 list_del (&clear->clear_list);
698
699 /* drop lock so HCD can concurrently report other TT errors */
700 spin_unlock_irqrestore (&hub->tt.lock, flags);
701 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (status)
703 dev_err (&hdev->dev,
704 "clear tt %d (%04x) error %d\n",
705 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400706
707 /* Tell the HCD, even if the operation failed */
708 drv = clear->hcd->driver;
709 if (drv->clear_tt_buffer_complete)
710 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
711
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700712 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400713 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 spin_unlock_irqrestore (&hub->tt.lock, flags);
716}
717
718/**
Lan Tianyu971fcd42013-01-23 04:26:29 +0800719 * usb_hub_set_port_power - control hub port's power state
720 * @hdev: target hub
721 * @port1: port index
722 * @set: expected status
723 *
724 * call this function to control port's power via setting or
725 * clearing the port's PORT_POWER feature.
726 */
727int usb_hub_set_port_power(struct usb_device *hdev, int port1,
728 bool set)
729{
730 int ret;
Lan Tianyuad493e52013-01-23 04:26:30 +0800731 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
732 struct usb_port *port_dev = hub->ports[port1 - 1];
Lan Tianyu971fcd42013-01-23 04:26:29 +0800733
734 if (set)
735 ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
736 else
Lan Tianyuad493e52013-01-23 04:26:30 +0800737 ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
738
739 if (!ret)
740 port_dev->power_is_on = set;
Lan Tianyu971fcd42013-01-23 04:26:29 +0800741 return ret;
742}
743
744/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400745 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
746 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 *
748 * High speed HCDs use this to tell the hub driver that some split control or
749 * bulk transaction failed in a way that requires clearing internal state of
750 * a transaction translator. This is normally detected (and reported) from
751 * interrupt context.
752 *
753 * It may not be possible for that hub to handle additional full (or low)
754 * speed transactions until that state is fully cleared out.
755 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400756int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400758 struct usb_device *udev = urb->dev;
759 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 struct usb_tt *tt = udev->tt;
761 unsigned long flags;
762 struct usb_tt_clear *clear;
763
764 /* we've got to cope with an arbitrary number of pending TT clears,
765 * since each TT has "at least two" buffers that can need it (and
766 * there can be many TTs per hub). even if they're uncommon.
767 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800768 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
770 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400771 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773
774 /* info that CLEAR_TT_BUFFER needs */
775 clear->tt = tt->multi ? udev->ttport : 1;
776 clear->devinfo = usb_pipeendpoint (pipe);
777 clear->devinfo |= udev->devnum << 4;
778 clear->devinfo |= usb_pipecontrol (pipe)
779 ? (USB_ENDPOINT_XFER_CONTROL << 11)
780 : (USB_ENDPOINT_XFER_BULK << 11);
781 if (usb_pipein (pipe))
782 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400783
784 /* info for completion callback */
785 clear->hcd = bus_to_hcd(udev->bus);
786 clear->ep = urb->ep;
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 /* tell keventd to clear state for this TT */
789 spin_lock_irqsave (&tt->lock, flags);
790 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400791 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400793 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400795EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Alan Stern8520f382008-09-22 14:44:26 -0400797/* If do_delay is false, return the number of milliseconds the caller
798 * needs to delay.
799 */
800static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700803 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern8520f382008-09-22 14:44:26 -0400804 unsigned delay;
Alan Stern4489a572006-04-27 15:54:22 -0400805 u16 wHubCharacteristics =
806 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Alan Stern4489a572006-04-27 15:54:22 -0400808 /* Enable power on each port. Some hubs have reserved values
809 * of LPSM (> 2) in their descriptors, even though they are
810 * USB 2.0 hubs. Some hubs do not implement port-power switching
811 * but only emulate it. In all cases, the ports won't work
812 * unless we send these messages to the hub.
813 */
814 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400816 else
817 dev_dbg(hub->intfdev, "trying to enable port power on "
818 "non-switchable hub\n");
819 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
Lan Tianyuad493e52013-01-23 04:26:30 +0800820 if (hub->ports[port1 - 1]->power_is_on)
821 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
822 else
823 usb_clear_port_feature(hub->hdev, port1,
824 USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
David Brownellb7896962005-08-31 10:41:44 -0700826 /* Wait at least 100 msec for power to become stable */
Alan Stern8520f382008-09-22 14:44:26 -0400827 delay = max(pgood_delay, (unsigned) 100);
828 if (do_delay)
829 msleep(delay);
830 return delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833static int hub_hub_status(struct usb_hub *hub,
834 u16 *status, u16 *change)
835{
836 int ret;
837
Alan Sterndb90e7a2007-02-05 09:56:15 -0500838 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 ret = get_hub_status(hub->hdev, &hub->status->hub);
840 if (ret < 0)
841 dev_err (hub->intfdev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800842 "%s failed (err = %d)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 else {
844 *status = le16_to_cpu(hub->status->hub.wHubStatus);
845 *change = le16_to_cpu(hub->status->hub.wHubChange);
846 ret = 0;
847 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500848 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return ret;
850}
851
Sarah Sharp41e7e052012-11-14 16:42:32 -0800852static int hub_set_port_link_state(struct usb_hub *hub, int port1,
853 unsigned int link_status)
854{
855 return set_port_feature(hub->hdev,
856 port1 | (link_status << 3),
857 USB_PORT_FEAT_LINK_STATE);
858}
859
860/*
861 * If USB 3.0 ports are placed into the Disabled state, they will no longer
862 * detect any device connects or disconnects. This is generally not what the
863 * USB core wants, since it expects a disabled port to produce a port status
864 * change event when a new device connects.
865 *
866 * Instead, set the link state to Disabled, wait for the link to settle into
867 * that state, clear any change bits, and then put the port into the RxDetect
868 * state.
869 */
870static int hub_usb3_port_disable(struct usb_hub *hub, int port1)
871{
872 int ret;
873 int total_time;
874 u16 portchange, portstatus;
875
876 if (!hub_is_superspeed(hub->hdev))
877 return -EINVAL;
878
879 ret = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_SS_DISABLED);
880 if (ret) {
881 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
882 port1, ret);
883 return ret;
884 }
885
886 /* Wait for the link to enter the disabled state. */
887 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
888 ret = hub_port_status(hub, port1, &portstatus, &portchange);
889 if (ret < 0)
890 return ret;
891
892 if ((portstatus & USB_PORT_STAT_LINK_STATE) ==
893 USB_SS_PORT_LS_SS_DISABLED)
894 break;
895 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
896 break;
897 msleep(HUB_DEBOUNCE_STEP);
898 }
899 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
900 dev_warn(hub->intfdev, "Could not disable port %d after %d ms\n",
901 port1, total_time);
902
903 return hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_RX_DETECT);
904}
905
Alan Stern8b28c752005-08-10 17:04:13 -0400906static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
907{
908 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400909 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400910
Lan Tianyuff823c72012-09-05 13:44:32 +0800911 if (hub->ports[port1 - 1]->child && set_state)
912 usb_set_device_state(hub->ports[port1 - 1]->child,
Alan Stern8b28c752005-08-10 17:04:13 -0400913 USB_STATE_NOTATTACHED);
Sarah Sharp41e7e052012-11-14 16:42:32 -0800914 if (!hub->error) {
915 if (hub_is_superspeed(hub->hdev))
916 ret = hub_usb3_port_disable(hub, port1);
917 else
Lan Tianyuad493e52013-01-23 04:26:30 +0800918 ret = usb_clear_port_feature(hdev, port1,
Sarah Sharp41e7e052012-11-14 16:42:32 -0800919 USB_PORT_FEAT_ENABLE);
920 }
Alan Stern8b28c752005-08-10 17:04:13 -0400921 if (ret)
922 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
Alan Stern0458d5b2007-05-04 11:52:20 -0400923 port1, ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400924 return ret;
925}
926
Alan Stern0458d5b2007-05-04 11:52:20 -0400927/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800928 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400929 * time later khubd will disconnect() any existing usb_device on the port
930 * and will re-enumerate if there actually is a device attached.
931 */
932static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
933{
934 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
935 hub_port_disable(hub, port1, 1);
936
937 /* FIXME let caller ask to power down the port:
938 * - some devices won't enumerate without a VBUS power cycle
939 * - SRP saves power that way
940 * - ... new call, TBD ...
941 * That's easy if this hub can switch power per-port, and
942 * khubd reactivates the port later (timer, SRP, etc).
943 * Powerdown must be optional, because of reset/DFU.
944 */
945
946 set_bit(port1, hub->change_bits);
947 kick_khubd(hub);
948}
949
Alan Stern253e0572009-10-27 15:20:13 -0400950/**
951 * usb_remove_device - disable a device's port on its parent hub
952 * @udev: device to be disabled and removed
953 * Context: @udev locked, must be able to sleep.
954 *
955 * After @udev's port has been disabled, khubd is notified and it will
956 * see that the device has been disconnected. When the device is
957 * physically unplugged and something is plugged in, the events will
958 * be received and processed normally.
959 */
960int usb_remove_device(struct usb_device *udev)
961{
962 struct usb_hub *hub;
963 struct usb_interface *intf;
964
965 if (!udev->parent) /* Can't remove a root hub */
966 return -EINVAL;
Lan Tianyuad493e52013-01-23 04:26:30 +0800967 hub = usb_hub_to_struct_hub(udev->parent);
Alan Stern253e0572009-10-27 15:20:13 -0400968 intf = to_usb_interface(hub->intfdev);
969
970 usb_autopm_get_interface(intf);
971 set_bit(udev->portnum, hub->removed_bits);
972 hub_port_logical_disconnect(hub, udev->portnum);
973 usb_autopm_put_interface(intf);
974 return 0;
975}
976
Alan Stern6ee0b272008-04-28 11:06:42 -0400977enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -0500978 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -0400979 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -0400980};
Alan Stern5e6effa2008-03-03 15:15:51 -0500981
Alan Stern8520f382008-09-22 14:44:26 -0400982static void hub_init_func2(struct work_struct *ws);
983static void hub_init_func3(struct work_struct *ws);
984
Alan Sternf2835212008-04-28 11:07:17 -0400985static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500986{
987 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -0800988 struct usb_hcd *hcd;
989 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -0500990 int port1;
Alan Sternf2835212008-04-28 11:07:17 -0400991 int status;
Alan Stern948fea32008-04-28 11:07:07 -0400992 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -0400993 unsigned delay;
994
995 /* Continue a partial initialization */
996 if (type == HUB_INIT2)
997 goto init2;
998 if (type == HUB_INIT3)
999 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -05001000
Elric Fua45aa3b2012-02-18 13:32:27 +08001001 /* The superspeed hub except for root hub has to use Hub Depth
1002 * value as an offset into the route string to locate the bits
1003 * it uses to determine the downstream port number. So hub driver
1004 * should send a set hub depth request to superspeed hub after
1005 * the superspeed hub is set configuration in initialization or
1006 * reset procedure.
1007 *
1008 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -04001009 * For any other type of activation, turn it on.
1010 */
Alan Stern8520f382008-09-22 14:44:26 -04001011 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +08001012 if (hdev->parent && hub_is_superspeed(hdev)) {
1013 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1014 HUB_SET_DEPTH, USB_RT_HUB,
1015 hdev->level - 1, 0, NULL, 0,
1016 USB_CTRL_SET_TIMEOUT);
1017 if (ret < 0)
1018 dev_err(hub->intfdev,
1019 "set hub depth failed\n");
1020 }
Alan Stern8520f382008-09-22 14:44:26 -04001021
1022 /* Speed up system boot by using a delayed_work for the
1023 * hub's initial power-up delays. This is pretty awkward
1024 * and the implementation looks like a home-brewed sort of
1025 * setjmp/longjmp, but it saves at least 100 ms for each
1026 * root hub (assuming usbcore is compiled into the kernel
1027 * rather than as a module). It adds up.
1028 *
1029 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
1030 * because for those activation types the ports have to be
1031 * operational when we return. In theory this could be done
1032 * for HUB_POST_RESET, but it's easier not to.
1033 */
1034 if (type == HUB_INIT) {
1035 delay = hub_power_on(hub, false);
1036 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
1037 schedule_delayed_work(&hub->init_work,
1038 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -04001039
1040 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -05001041 usb_autopm_get_interface_no_resume(
1042 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -04001043 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001044 } else if (type == HUB_RESET_RESUME) {
1045 /* The internal host controller state for the hub device
1046 * may be gone after a host power loss on system resume.
1047 * Update the device's info so the HW knows it's a hub.
1048 */
1049 hcd = bus_to_hcd(hdev->bus);
1050 if (hcd->driver->update_hub_device) {
1051 ret = hcd->driver->update_hub_device(hcd, hdev,
1052 &hub->tt, GFP_NOIO);
1053 if (ret < 0) {
1054 dev_err(hub->intfdev, "Host not "
1055 "accepting hub info "
1056 "update.\n");
1057 dev_err(hub->intfdev, "LS/FS devices "
1058 "and hubs may not work "
1059 "under this hub\n.");
1060 }
1061 }
1062 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001063 } else {
1064 hub_power_on(hub, true);
1065 }
1066 }
1067 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001068
Alan Stern6ee0b272008-04-28 11:06:42 -04001069 /* Check each port and set hub->change_bits to let khubd know
1070 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001071 */
1072 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001073 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern5e6effa2008-03-03 15:15:51 -05001074 u16 portstatus, portchange;
1075
Alan Stern6ee0b272008-04-28 11:06:42 -04001076 portstatus = portchange = 0;
1077 status = hub_port_status(hub, port1, &portstatus, &portchange);
1078 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1079 dev_dbg(hub->intfdev,
1080 "port %d: status %04x change %04x\n",
1081 port1, portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001082
Alan Stern6ee0b272008-04-28 11:06:42 -04001083 /* After anything other than HUB_RESUME (i.e., initialization
1084 * or any sort of reset), every port should be disabled.
1085 * Unconnected ports should likewise be disabled (paranoia),
1086 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001087 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001088 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1089 type != HUB_RESUME ||
1090 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1091 !udev ||
1092 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001093 /*
1094 * USB3 protocol ports will automatically transition
1095 * to Enabled state when detect an USB3.0 device attach.
1096 * Do not disable USB3 protocol ports.
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001097 */
Sarah Sharp131dec32010-12-06 21:00:19 -08001098 if (!hub_is_superspeed(hdev)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08001099 usb_clear_port_feature(hdev, port1,
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001100 USB_PORT_FEAT_ENABLE);
1101 portstatus &= ~USB_PORT_STAT_ENABLE;
Sarah Sharp85f0ff42010-10-14 07:22:54 -07001102 } else {
1103 /* Pretend that power was lost for USB3 devs */
1104 portstatus &= ~USB_PORT_STAT_ENABLE;
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001105 }
Alan Stern6ee0b272008-04-28 11:06:42 -04001106 }
1107
Alan Stern948fea32008-04-28 11:07:07 -04001108 /* Clear status-change flags; we'll debounce later */
1109 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1110 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001111 usb_clear_port_feature(hub->hdev, port1,
Alan Stern948fea32008-04-28 11:07:07 -04001112 USB_PORT_FEAT_C_CONNECTION);
1113 }
1114 if (portchange & USB_PORT_STAT_C_ENABLE) {
1115 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001116 usb_clear_port_feature(hub->hdev, port1,
Alan Stern948fea32008-04-28 11:07:07 -04001117 USB_PORT_FEAT_C_ENABLE);
1118 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001119 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1120 hub_is_superspeed(hub->hdev)) {
1121 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001122 usb_clear_port_feature(hub->hdev, port1,
Don Zickus79c3dd82011-11-03 09:07:18 -04001123 USB_PORT_FEAT_C_BH_PORT_RESET);
1124 }
Alan Stern253e0572009-10-27 15:20:13 -04001125 /* We can forget about a "removed" device when there's a
1126 * physical disconnect or the connect status changes.
1127 */
1128 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1129 (portchange & USB_PORT_STAT_C_CONNECTION))
1130 clear_bit(port1, hub->removed_bits);
1131
Alan Stern6ee0b272008-04-28 11:06:42 -04001132 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1133 /* Tell khubd to disconnect the device or
1134 * check for a new connection
1135 */
1136 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1137 set_bit(port1, hub->change_bits);
1138
1139 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001140 bool port_resumed = (portstatus &
1141 USB_PORT_STAT_LINK_STATE) ==
1142 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001143 /* The power session apparently survived the resume.
1144 * If there was an overcurrent or suspend change
1145 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -08001146 * take care of it. Look at the port link state
1147 * for USB 3.0 hubs, since they don't have a suspend
1148 * change bit, and they don't set the port link change
1149 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001150 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001151 if (portchange || (hub_is_superspeed(hub->hdev) &&
1152 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001153 set_bit(port1, hub->change_bits);
1154
1155 } else if (udev->persist_enabled) {
Lan Tianyuad493e52013-01-23 04:26:30 +08001156 struct usb_port *port_dev = hub->ports[port1 - 1];
1157
Alan Stern6ee0b272008-04-28 11:06:42 -04001158#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001159 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001160#endif
Lan Tianyuad493e52013-01-23 04:26:30 +08001161 /* Don't set the change_bits when the device
1162 * was powered off.
1163 */
1164 if (port_dev->power_is_on)
1165 set_bit(port1, hub->change_bits);
Alan Stern8808f002008-04-28 11:06:55 -04001166
Alan Stern6ee0b272008-04-28 11:06:42 -04001167 } else {
1168 /* The power session is gone; tell khubd */
1169 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1170 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001171 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001172 }
1173
Alan Stern948fea32008-04-28 11:07:07 -04001174 /* If no port-status-change flags were set, we don't need any
1175 * debouncing. If flags were set we can try to debounce the
1176 * ports all at once right now, instead of letting khubd do them
1177 * one at a time later on.
1178 *
1179 * If any port-status changes do occur during this delay, khubd
1180 * will see them later and handle them normally.
1181 */
Alan Stern8520f382008-09-22 14:44:26 -04001182 if (need_debounce_delay) {
1183 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001184
Alan Stern8520f382008-09-22 14:44:26 -04001185 /* Don't do a long sleep inside a workqueue routine */
1186 if (type == HUB_INIT2) {
1187 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1188 schedule_delayed_work(&hub->init_work,
1189 msecs_to_jiffies(delay));
1190 return; /* Continues at init3: below */
1191 } else {
1192 msleep(delay);
1193 }
1194 }
1195 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001196 hub->quiescing = 0;
1197
1198 status = usb_submit_urb(hub->urb, GFP_NOIO);
1199 if (status < 0)
1200 dev_err(hub->intfdev, "activate --> %d\n", status);
1201 if (hub->has_indicators && blinkenlights)
1202 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1203
1204 /* Scan all ports that need attention */
1205 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001206
1207 /* Allow autosuspend if it was suppressed */
1208 if (type <= HUB_INIT3)
1209 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -05001210}
1211
Alan Stern8520f382008-09-22 14:44:26 -04001212/* Implement the continuations for the delays above */
1213static void hub_init_func2(struct work_struct *ws)
1214{
1215 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1216
1217 hub_activate(hub, HUB_INIT2);
1218}
1219
1220static void hub_init_func3(struct work_struct *ws)
1221{
1222 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1223
1224 hub_activate(hub, HUB_INIT3);
1225}
1226
Alan Stern43303542008-04-28 11:07:31 -04001227enum hub_quiescing_type {
1228 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1229};
1230
1231static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1232{
1233 struct usb_device *hdev = hub->hdev;
1234 int i;
1235
Alan Stern8520f382008-09-22 14:44:26 -04001236 cancel_delayed_work_sync(&hub->init_work);
1237
Alan Stern43303542008-04-28 11:07:31 -04001238 /* khubd and related activity won't re-trigger */
1239 hub->quiescing = 1;
1240
1241 if (type != HUB_SUSPEND) {
1242 /* Disconnect all the children */
1243 for (i = 0; i < hdev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001244 if (hub->ports[i]->child)
1245 usb_disconnect(&hub->ports[i]->child);
Alan Stern43303542008-04-28 11:07:31 -04001246 }
1247 }
1248
1249 /* Stop khubd and related activity */
1250 usb_kill_urb(hub->urb);
1251 if (hub->has_indicators)
1252 cancel_delayed_work_sync(&hub->leds);
1253 if (hub->tt.hub)
Octavian Purdila036546b2012-10-23 11:33:12 +03001254 flush_work(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001255}
1256
Alan Stern3eb14912008-03-03 15:15:43 -05001257/* caller has locked the hub device */
1258static int hub_pre_reset(struct usb_interface *intf)
1259{
1260 struct usb_hub *hub = usb_get_intfdata(intf);
1261
Alan Stern43303542008-04-28 11:07:31 -04001262 hub_quiesce(hub, HUB_PRE_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001263 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001264}
1265
1266/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001267static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001268{
Alan Stern7de18d82006-06-01 13:37:24 -04001269 struct usb_hub *hub = usb_get_intfdata(intf);
1270
Alan Sternf2835212008-04-28 11:07:17 -04001271 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001272 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001273}
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275static int hub_configure(struct usb_hub *hub,
1276 struct usb_endpoint_descriptor *endpoint)
1277{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001278 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 struct usb_device *hdev = hub->hdev;
1280 struct device *hub_dev = hub->intfdev;
1281 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001282 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 unsigned int pipe;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001284 int maxp, ret, i;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001285 char *message = "out of memory";
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001286 unsigned unit_load;
1287 unsigned full_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Alan Sternd697cdd2009-10-27 15:18:46 -04001289 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 ret = -ENOMEM;
1292 goto fail;
1293 }
1294
1295 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1296 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 ret = -ENOMEM;
1298 goto fail;
1299 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001300 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1303 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 ret = -ENOMEM;
1305 goto fail;
1306 }
1307
1308 /* Request the entire hub descriptor.
1309 * hub->descriptor can handle USB_MAXCHILDREN ports,
1310 * but the hub can/will return fewer bytes here.
1311 */
John Youndbe79bb2001-09-17 00:00:00 -07001312 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (ret < 0) {
1314 message = "can't read hub descriptor";
1315 goto fail;
1316 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1317 message = "hub has too many ports!";
1318 ret = -ENODEV;
1319 goto fail;
David Linares769d73682013-03-25 10:50:27 +00001320 } else if (hub->descriptor->bNbrPorts == 0) {
1321 message = "hub doesn't have any ports!";
1322 ret = -ENODEV;
1323 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325
1326 hdev->maxchild = hub->descriptor->bNbrPorts;
1327 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1328 (hdev->maxchild == 1) ? "" : "s");
1329
Lan Tianyufa2a9562012-09-05 13:44:31 +08001330 hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1331 GFP_KERNEL);
Lan Tianyuff823c72012-09-05 13:44:32 +08001332 if (!hub->ports) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001333 ret = -ENOMEM;
1334 goto fail;
1335 }
1336
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001337 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001338 if (hub_is_superspeed(hdev)) {
1339 unit_load = 150;
1340 full_load = 900;
1341 } else {
1342 unit_load = 100;
1343 full_load = 500;
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
John Youndbe79bb2001-09-17 00:00:00 -07001346 /* FIXME for USB 3.0, skip for now */
1347 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1348 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int i;
1350 char portstr [USB_MAXCHILDREN + 1];
1351
1352 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001353 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1355 ? 'F' : 'R';
1356 portstr[hdev->maxchild] = 0;
1357 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1358 } else
1359 dev_dbg(hub_dev, "standalone hub\n");
1360
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001361 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301362 case HUB_CHAR_COMMON_LPSM:
1363 dev_dbg(hub_dev, "ganged power switching\n");
1364 break;
1365 case HUB_CHAR_INDV_PORT_LPSM:
1366 dev_dbg(hub_dev, "individual port power switching\n");
1367 break;
1368 case HUB_CHAR_NO_LPSM:
1369 case HUB_CHAR_LPSM:
1370 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1371 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001374 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301375 case HUB_CHAR_COMMON_OCPM:
1376 dev_dbg(hub_dev, "global over-current protection\n");
1377 break;
1378 case HUB_CHAR_INDV_PORT_OCPM:
1379 dev_dbg(hub_dev, "individual port over-current protection\n");
1380 break;
1381 case HUB_CHAR_NO_OCPM:
1382 case HUB_CHAR_OCPM:
1383 dev_dbg(hub_dev, "no over-current protection\n");
1384 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386
1387 spin_lock_init (&hub->tt.lock);
1388 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001389 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301391 case USB_HUB_PR_FS:
1392 break;
1393 case USB_HUB_PR_HS_SINGLE_TT:
1394 dev_dbg(hub_dev, "Single TT\n");
1395 hub->tt.hub = hdev;
1396 break;
1397 case USB_HUB_PR_HS_MULTI_TT:
1398 ret = usb_set_interface(hdev, 0, 1);
1399 if (ret == 0) {
1400 dev_dbg(hub_dev, "TT per port\n");
1401 hub->tt.multi = 1;
1402 } else
1403 dev_err(hub_dev, "Using single TT (err %d)\n",
1404 ret);
1405 hub->tt.hub = hdev;
1406 break;
1407 case USB_HUB_PR_SS:
1408 /* USB 3.0 hubs don't have a TT */
1409 break;
1410 default:
1411 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1412 hdev->descriptor.bDeviceProtocol);
1413 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001416 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001417 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001418 case HUB_TTTT_8_BITS:
1419 if (hdev->descriptor.bDeviceProtocol != 0) {
1420 hub->tt.think_time = 666;
1421 dev_dbg(hub_dev, "TT requires at most %d "
1422 "FS bit times (%d ns)\n",
1423 8, hub->tt.think_time);
1424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001426 case HUB_TTTT_16_BITS:
1427 hub->tt.think_time = 666 * 2;
1428 dev_dbg(hub_dev, "TT requires at most %d "
1429 "FS bit times (%d ns)\n",
1430 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001432 case HUB_TTTT_24_BITS:
1433 hub->tt.think_time = 666 * 3;
1434 dev_dbg(hub_dev, "TT requires at most %d "
1435 "FS bit times (%d ns)\n",
1436 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001438 case HUB_TTTT_32_BITS:
1439 hub->tt.think_time = 666 * 4;
1440 dev_dbg(hub_dev, "TT requires at most %d "
1441 "FS bit times (%d ns)\n",
1442 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 break;
1444 }
1445
1446 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001447 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 hub->has_indicators = 1;
1449 dev_dbg(hub_dev, "Port indicators are supported\n");
1450 }
1451
1452 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1453 hub->descriptor->bPwrOn2PwrGood * 2);
1454
1455 /* power budgeting mostly matters with bus-powered hubs,
1456 * and battery-powered root hubs (may provide just 8 mA).
1457 */
1458 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001459 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 message = "can't get hub status";
1461 goto fail;
1462 }
Alan Stern7d35b922005-04-25 11:18:32 -04001463 le16_to_cpus(&hubstatus);
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001464 hcd = bus_to_hcd(hdev->bus);
Alan Stern7d35b922005-04-25 11:18:32 -04001465 if (hdev == hdev->bus->root_hub) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001466 if (hcd->power_budget > 0)
1467 hdev->bus_mA = hcd->power_budget;
1468 else
1469 hdev->bus_mA = full_load * hdev->maxchild;
1470 if (hdev->bus_mA >= full_load)
1471 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001472 else {
1473 hub->mA_per_port = hdev->bus_mA;
1474 hub->limited_power = 1;
1475 }
Alan Stern7d35b922005-04-25 11:18:32 -04001476 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001477 int remaining = hdev->bus_mA -
1478 hub->descriptor->bHubContrCurrent;
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1481 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001482 hub->limited_power = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001484 if (remaining < hdev->maxchild * unit_load)
1485 dev_warn(hub_dev,
Alan Stern55c52712005-11-23 12:03:12 -05001486 "insufficient power available "
1487 "to use all downstream ports\n");
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001488 hub->mA_per_port = unit_load; /* 7.2.1 */
1489
Alan Stern55c52712005-11-23 12:03:12 -05001490 } else { /* Self-powered external hub */
1491 /* FIXME: What about battery-powered external hubs that
1492 * provide less current per port? */
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001493 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001494 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001495 if (hub->mA_per_port < full_load)
Alan Stern55c52712005-11-23 12:03:12 -05001496 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1497 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001499 /* Update the HCD's internal representation of this hub before khubd
1500 * starts getting port status changes for devices under the hub.
1501 */
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001502 if (hcd->driver->update_hub_device) {
1503 ret = hcd->driver->update_hub_device(hcd, hdev,
1504 &hub->tt, GFP_KERNEL);
1505 if (ret < 0) {
1506 message = "can't update HCD hub info";
1507 goto fail;
1508 }
1509 }
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1512 if (ret < 0) {
1513 message = "can't get hub status";
1514 goto fail;
1515 }
1516
1517 /* local power status reports aren't always correct */
1518 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1519 dev_dbg(hub_dev, "local power source is %s\n",
1520 (hubstatus & HUB_STATUS_LOCAL_POWER)
1521 ? "lost (inactive)" : "good");
1522
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001523 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 dev_dbg(hub_dev, "%sover-current condition exists\n",
1525 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1526
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001527 /* set up the interrupt endpoint
1528 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1529 * bytes as USB2.0[11.12.3] says because some hubs are known
1530 * to send more data (and thus cause overflow). For root hubs,
1531 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1532 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1534 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1535
1536 if (maxp > sizeof(*hub->buffer))
1537 maxp = sizeof(*hub->buffer);
1538
1539 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1540 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 ret = -ENOMEM;
1542 goto fail;
1543 }
1544
1545 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1546 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 /* maybe cycle the hub leds */
1549 if (hub->has_indicators && blinkenlights)
1550 hub->indicator [0] = INDICATOR_CYCLE;
1551
Lan Tianyufa2a9562012-09-05 13:44:31 +08001552 for (i = 0; i < hdev->maxchild; i++)
1553 if (usb_hub_create_port_device(hub, i + 1) < 0)
1554 dev_err(hub->intfdev,
1555 "couldn't create port%d device.\n", i + 1);
1556
Lan Tianyud2123fd2013-01-21 22:18:00 +08001557 usb_hub_adjust_deviceremovable(hdev, hub->descriptor);
1558
Alan Sternf2835212008-04-28 11:07:17 -04001559 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return 0;
1561
1562fail:
1563 dev_err (hub_dev, "config failed, %s (err %d)\n",
1564 message, ret);
1565 /* hub_disconnect() frees urb and descriptor */
1566 return ret;
1567}
1568
Alan Sterne8054852007-05-04 11:55:11 -04001569static void hub_release(struct kref *kref)
1570{
1571 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1572
1573 usb_put_intf(to_usb_interface(hub->intfdev));
1574 kfree(hub);
1575}
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577static unsigned highspeed_hubs;
1578
1579static void hub_disconnect(struct usb_interface *intf)
1580{
Huajun Li88162302012-03-12 21:00:19 +08001581 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001582 struct usb_device *hdev = interface_to_usbdev(intf);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001583 int i;
1584
Alan Sterne8054852007-05-04 11:55:11 -04001585 /* Take the hub off the event list and don't let it be added again */
1586 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001587 if (!list_empty(&hub->event_list)) {
1588 list_del_init(&hub->event_list);
1589 usb_autopm_put_interface_no_suspend(intf);
1590 }
Alan Sterne8054852007-05-04 11:55:11 -04001591 hub->disconnected = 1;
1592 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Alan Stern7de18d82006-06-01 13:37:24 -04001594 /* Disconnect all children and quiesce the hub */
1595 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001596 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001597
Alan Stern8b28c752005-08-10 17:04:13 -04001598 usb_set_intfdata (intf, NULL);
Alexander Shishkin1f2235b2012-09-12 14:48:31 +03001599
1600 for (i = 0; i < hdev->maxchild; i++)
1601 usb_hub_remove_port_device(hub, i + 1);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001602 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Alan Sterne8054852007-05-04 11:55:11 -04001604 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 highspeed_hubs--;
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 usb_free_urb(hub->urb);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001608 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001609 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001610 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001611 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Lan Tianyu971fcd42013-01-23 04:26:29 +08001613 pm_suspend_ignore_children(&intf->dev, false);
Alan Sterne8054852007-05-04 11:55:11 -04001614 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
1617static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1618{
1619 struct usb_host_interface *desc;
1620 struct usb_endpoint_descriptor *endpoint;
1621 struct usb_device *hdev;
1622 struct usb_hub *hub;
1623
1624 desc = intf->cur_altsetting;
1625 hdev = interface_to_usbdev(intf);
1626
Ming Lei596d7892012-10-24 11:59:25 +08001627 /*
1628 * Set default autosuspend delay as 0 to speedup bus suspend,
1629 * based on the below considerations:
1630 *
1631 * - Unlike other drivers, the hub driver does not rely on the
1632 * autosuspend delay to provide enough time to handle a wakeup
1633 * event, and the submitted status URB is just to check future
1634 * change on hub downstream ports, so it is safe to do it.
1635 *
1636 * - The patch might cause one or more auto supend/resume for
1637 * below very rare devices when they are plugged into hub
1638 * first time:
1639 *
1640 * devices having trouble initializing, and disconnect
1641 * themselves from the bus and then reconnect a second
1642 * or so later
1643 *
1644 * devices just for downloading firmware, and disconnects
1645 * themselves after completing it
1646 *
1647 * For these quite rare devices, their drivers may change the
1648 * autosuspend delay of their parent hub in the probe() to one
1649 * appropriate value to avoid the subtle problem if someone
1650 * does care it.
1651 *
1652 * - The patch may cause one or more auto suspend/resume on
1653 * hub during running 'lsusb', but it is probably too
1654 * infrequent to worry about.
1655 *
1656 * - Change autosuspend delay of hub can avoid unnecessary auto
1657 * suspend timer for hub, also may decrease power consumption
1658 * of USB bus.
1659 */
1660 pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
1661
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001662 /* Hubs have proper suspend/resume support. */
1663 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001664
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001665 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001666 dev_err(&intf->dev,
1667 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001668 return -E2BIG;
1669 }
1670
David Brownell89ccbdc2006-04-02 10:18:09 -08001671#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1672 if (hdev->parent) {
1673 dev_warn(&intf->dev, "ignoring external hub\n");
1674 return -ENODEV;
1675 }
1676#endif
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 /* Some hubs have a subclass of 1, which AFAICT according to the */
1679 /* specs is not defined, but it works */
1680 if ((desc->desc.bInterfaceSubClass != 0) &&
1681 (desc->desc.bInterfaceSubClass != 1)) {
1682descriptor_error:
1683 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1684 return -EIO;
1685 }
1686
1687 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1688 if (desc->desc.bNumEndpoints != 1)
1689 goto descriptor_error;
1690
1691 endpoint = &desc->endpoint[0].desc;
1692
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001693 /* If it's not an interrupt in endpoint, we'd better punt! */
1694 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 goto descriptor_error;
1696
1697 /* We found a hub */
1698 dev_info (&intf->dev, "USB hub found\n");
1699
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001700 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (!hub) {
1702 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1703 return -ENOMEM;
1704 }
1705
Alan Sterne8054852007-05-04 11:55:11 -04001706 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 INIT_LIST_HEAD(&hub->event_list);
1708 hub->intfdev = &intf->dev;
1709 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001710 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001711 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001712 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001715 intf->needs_remote_wakeup = 1;
Lan Tianyu971fcd42013-01-23 04:26:29 +08001716 pm_suspend_ignore_children(&intf->dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 if (hdev->speed == USB_SPEED_HIGH)
1719 highspeed_hubs++;
1720
Ming Leie6f30de2012-10-24 11:59:24 +08001721 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1722 hub->quirk_check_port_auto_suspend = 1;
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (hub_configure(hub, endpoint) >= 0)
1725 return 0;
1726
1727 hub_disconnect (intf);
1728 return -ENODEV;
1729}
1730
1731static int
1732hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1733{
1734 struct usb_device *hdev = interface_to_usbdev (intf);
Lan Tianyuad493e52013-01-23 04:26:30 +08001735 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 /* assert ifno == 0 (part of hub spec) */
1738 switch (code) {
1739 case USBDEVFS_HUB_PORTINFO: {
1740 struct usbdevfs_hub_portinfo *info = user_data;
1741 int i;
1742
1743 spin_lock_irq(&device_state_lock);
1744 if (hdev->devnum <= 0)
1745 info->nports = 0;
1746 else {
1747 info->nports = hdev->maxchild;
1748 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001749 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 info->port[i] = 0;
1751 else
1752 info->port[i] =
Lan Tianyuff823c72012-09-05 13:44:32 +08001753 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755 }
1756 spin_unlock_irq(&device_state_lock);
1757
1758 return info->nports + 1;
1759 }
1760
1761 default:
1762 return -ENOSYS;
1763 }
1764}
1765
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001766/*
1767 * Allow user programs to claim ports on a hub. When a device is attached
1768 * to one of these "claimed" ports, the program will "own" the device.
1769 */
1770static int find_port_owner(struct usb_device *hdev, unsigned port1,
Lan Tianyu336c5c32012-07-06 14:13:52 +08001771 struct dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001772{
1773 if (hdev->state == USB_STATE_NOTATTACHED)
1774 return -ENODEV;
1775 if (port1 == 0 || port1 > hdev->maxchild)
1776 return -EINVAL;
1777
1778 /* This assumes that devices not managed by the hub driver
1779 * will always have maxchild equal to 0.
1780 */
Lan Tianyuad493e52013-01-23 04:26:30 +08001781 *ppowner = &(usb_hub_to_struct_hub(hdev)->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001782 return 0;
1783}
1784
1785/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001786int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1787 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001788{
1789 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001790 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001791
1792 rc = find_port_owner(hdev, port1, &powner);
1793 if (rc)
1794 return rc;
1795 if (*powner)
1796 return -EBUSY;
1797 *powner = owner;
1798 return rc;
1799}
1800
Lan Tianyu336c5c32012-07-06 14:13:52 +08001801int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1802 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001803{
1804 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001805 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001806
1807 rc = find_port_owner(hdev, port1, &powner);
1808 if (rc)
1809 return rc;
1810 if (*powner != owner)
1811 return -ENOENT;
1812 *powner = NULL;
1813 return rc;
1814}
1815
Lan Tianyu336c5c32012-07-06 14:13:52 +08001816void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001817{
Lan Tianyuad493e52013-01-23 04:26:30 +08001818 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001819 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001820
Lan Tianyufa2a9562012-09-05 13:44:31 +08001821 for (n = 0; n < hdev->maxchild; n++) {
1822 if (hub->ports[n]->port_owner == owner)
1823 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001824 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001825
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001826}
1827
1828/* The caller must hold udev's lock */
1829bool usb_device_is_owned(struct usb_device *udev)
1830{
1831 struct usb_hub *hub;
1832
1833 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1834 return false;
Lan Tianyuad493e52013-01-23 04:26:30 +08001835 hub = usb_hub_to_struct_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001836 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001837}
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1840{
Lan Tianyuad493e52013-01-23 04:26:30 +08001841 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 int i;
1843
1844 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001845 if (hub->ports[i]->child)
1846 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001848 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001849 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 udev->state = USB_STATE_NOTATTACHED;
1851}
1852
1853/**
1854 * usb_set_device_state - change a device's current state (usbcore, hcds)
1855 * @udev: pointer to device whose state should be changed
1856 * @new_state: new state value to be stored
1857 *
1858 * udev->state is _not_ fully protected by the device lock. Although
1859 * most transitions are made only while holding the lock, the state can
1860 * can change to USB_STATE_NOTATTACHED at almost any time. This
1861 * is so that devices can be marked as disconnected as soon as possible,
1862 * without having to wait for any semaphores to be released. As a result,
1863 * all changes to any device's state must be protected by the
1864 * device_state_lock spinlock.
1865 *
1866 * Once a device has been added to the device tree, all changes to its state
1867 * should be made using this routine. The state should _not_ be set directly.
1868 *
1869 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1870 * Otherwise udev->state is set to new_state, and if new_state is
1871 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1872 * to USB_STATE_NOTATTACHED.
1873 */
1874void usb_set_device_state(struct usb_device *udev,
1875 enum usb_device_state new_state)
1876{
1877 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001878 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 spin_lock_irqsave(&device_state_lock, flags);
1881 if (udev->state == USB_STATE_NOTATTACHED)
1882 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001883 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001884
1885 /* root hub wakeup capabilities are managed out-of-band
1886 * and may involve silicon errata ... ignore them here.
1887 */
1888 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001889 if (udev->state == USB_STATE_SUSPENDED
1890 || new_state == USB_STATE_SUSPENDED)
1891 ; /* No change to wakeup settings */
1892 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001893 wakeup = udev->actconfig->desc.bmAttributes
1894 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001895 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001896 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001897 }
Sarah Sharp15123002007-12-21 16:54:15 -08001898 if (udev->state == USB_STATE_SUSPENDED &&
1899 new_state != USB_STATE_SUSPENDED)
1900 udev->active_duration -= jiffies;
1901 else if (new_state == USB_STATE_SUSPENDED &&
1902 udev->state != USB_STATE_SUSPENDED)
1903 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001904 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001905 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 recursively_mark_NOTATTACHED(udev);
1907 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001908 if (wakeup >= 0)
1909 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
David Vrabel6da9c992009-02-18 14:43:47 +00001911EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001913/*
Alan Stern3b29b682011-02-22 09:53:41 -05001914 * Choose a device number.
1915 *
1916 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1917 * USB-2.0 buses they are also used as device addresses, however on
1918 * USB-3.0 buses the address is assigned by the controller hardware
1919 * and it usually is not the same as the device number.
1920 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001921 * WUSB devices are simple: they have no hubs behind, so the mapping
1922 * device <-> virtual port number becomes 1:1. Why? to simplify the
1923 * life of the device connection logic in
1924 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1925 * handshake we need to assign a temporary address in the unauthorized
1926 * space. For simplicity we use the first virtual port number found to
1927 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1928 * and that becomes it's address [X < 128] or its unauthorized address
1929 * [X | 0x80].
1930 *
1931 * We add 1 as an offset to the one-based USB-stack port number
1932 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1933 * 0 is reserved by USB for default address; (b) Linux's USB stack
1934 * uses always #1 for the root hub of the controller. So USB stack's
1935 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001936 *
1937 * Devices connected under xHCI are not as simple. The host controller
1938 * supports virtualization, so the hardware assigns device addresses and
1939 * the HCD must setup data structures before issuing a set address
1940 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001941 */
Alan Stern3b29b682011-02-22 09:53:41 -05001942static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944 int devnum;
1945 struct usb_bus *bus = udev->bus;
1946
1947 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001948 if (udev->wusb) {
1949 devnum = udev->portnum + 1;
1950 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1951 } else {
1952 /* Try to allocate the next devnum beginning at
1953 * bus->devnum_next. */
1954 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1955 bus->devnum_next);
1956 if (devnum >= 128)
1957 devnum = find_next_zero_bit(bus->devmap.devicemap,
1958 128, 1);
1959 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (devnum < 128) {
1962 set_bit(devnum, bus->devmap.devicemap);
1963 udev->devnum = devnum;
1964 }
1965}
1966
Alan Stern3b29b682011-02-22 09:53:41 -05001967static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 if (udev->devnum > 0) {
1970 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1971 udev->devnum = -1;
1972 }
1973}
1974
Alan Stern3b29b682011-02-22 09:53:41 -05001975static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001976{
1977 /* The address for a WUSB device is managed by wusbcore. */
1978 if (!udev->wusb)
1979 udev->devnum = devnum;
1980}
1981
Herbert Xuf7410ce2010-01-10 20:15:03 +11001982static void hub_free_dev(struct usb_device *udev)
1983{
1984 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1985
1986 /* Root hubs aren't real devices, so don't free HCD resources */
1987 if (hcd->driver->free_dev && udev->parent)
1988 hcd->driver->free_dev(hcd, udev);
1989}
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991/**
1992 * usb_disconnect - disconnect a device (usbcore-internal)
1993 * @pdev: pointer to device being disconnected
1994 * Context: !in_interrupt ()
1995 *
1996 * Something got disconnected. Get rid of it and all of its children.
1997 *
1998 * If *pdev is a normal device then the parent hub must already be locked.
1999 * If *pdev is a root hub then this routine will acquire the
2000 * usb_bus_list_lock on behalf of the caller.
2001 *
2002 * Only hub drivers (including virtual root hub drivers for host
2003 * controllers) should ever call this.
2004 *
2005 * This call is synchronous, and may not be used in an interrupt context.
2006 */
2007void usb_disconnect(struct usb_device **pdev)
2008{
2009 struct usb_device *udev = *pdev;
Lan Tianyuad493e52013-01-23 04:26:30 +08002010 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 int i;
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 /* mark the device as inactive, so any further urb submissions for
2014 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002015 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 */
2017 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05002018 dev_info(&udev->dev, "USB disconnect, device number %d\n",
2019 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002021 usb_lock_device(udev);
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08002024 for (i = 0; i < udev->maxchild; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08002025 if (hub->ports[i]->child)
2026 usb_disconnect(&hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028
2029 /* deallocate hcd/hardware state ... nuking all pending urbs and
2030 * cleaning up all state associated with the current configuration
2031 * so that the hardware is now fully quiesced.
2032 */
Alan Stern782da722006-07-01 22:09:35 -04002033 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04002035 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Lan Tianyufde26382013-01-20 01:53:33 +08002037 if (udev->parent) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002038 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
2039 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002040
2041 sysfs_remove_link(&udev->dev.kobj, "port");
2042 sysfs_remove_link(&port_dev->dev.kobj, "device");
Lan Tianyu971fcd42013-01-23 04:26:29 +08002043
Lan Tianyuad493e52013-01-23 04:26:30 +08002044 if (!port_dev->did_runtime_put)
2045 pm_runtime_put(&port_dev->dev);
2046 else
2047 port_dev->did_runtime_put = false;
Lan Tianyufde26382013-01-20 01:53:33 +08002048 }
2049
Alan Stern3b23dd62008-12-05 14:10:34 -05002050 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04002051 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07002052
Alan Stern782da722006-07-01 22:09:35 -04002053 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002054 * for de-configuring the device and invoking the remove-device
2055 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002056 */
2057 device_del(&udev->dev);
2058
2059 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 * (or root_hub) pointer.
2061 */
Alan Stern3b29b682011-02-22 09:53:41 -05002062 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 /* Avoid races with recursively_mark_NOTATTACHED() */
2065 spin_lock_irq(&device_state_lock);
2066 *pdev = NULL;
2067 spin_unlock_irq(&device_state_lock);
2068
Herbert Xuf7410ce2010-01-10 20:15:03 +11002069 hub_free_dev(udev);
2070
Alan Stern782da722006-07-01 22:09:35 -04002071 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002074#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075static void show_string(struct usb_device *udev, char *id, char *string)
2076{
2077 if (!string)
2078 return;
Joe Perchesf2ec5222012-10-28 01:05:51 -07002079 dev_info(&udev->dev, "%s: %s\n", id, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
2081
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002082static void announce_device(struct usb_device *udev)
2083{
2084 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2085 le16_to_cpu(udev->descriptor.idVendor),
2086 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002087 dev_info(&udev->dev,
2088 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002089 udev->descriptor.iManufacturer,
2090 udev->descriptor.iProduct,
2091 udev->descriptor.iSerialNumber);
2092 show_string(udev, "Product", udev->product);
2093 show_string(udev, "Manufacturer", udev->manufacturer);
2094 show_string(udev, "SerialNumber", udev->serial);
2095}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002097static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098#endif
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100#ifdef CONFIG_USB_OTG
2101#include "otg_whitelist.h"
2102#endif
2103
Oliver Neukum3ede7602007-01-11 14:35:50 +01002104/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002105 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002106 * @udev: newly addressed device (in ADDRESS state)
2107 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002108 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01002109 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002110static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002112 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114#ifdef CONFIG_USB_OTG
2115 /*
2116 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2117 * to wake us after we've powered off VBUS; and HNP, switching roles
2118 * "host" to "peripheral". The OTG descriptor helps figure this out.
2119 */
2120 if (!udev->bus->is_b_host
2121 && udev->config
2122 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002123 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 struct usb_bus *bus = udev->bus;
2125
2126 /* descriptor may appear anywhere in config */
2127 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2128 le16_to_cpu(udev->config[0].desc.wTotalLength),
2129 USB_DT_OTG, (void **) &desc) == 0) {
2130 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002131 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 dev_info(&udev->dev,
2134 "Dual-Role OTG device on %sHNP port\n",
2135 (port1 == bus->otg_port)
2136 ? "" : "non-");
2137
2138 /* enable HNP before suspend, it's simpler */
2139 if (port1 == bus->otg_port)
2140 bus->b_hnp_enable = 1;
2141 err = usb_control_msg(udev,
2142 usb_sndctrlpipe(udev, 0),
2143 USB_REQ_SET_FEATURE, 0,
2144 bus->b_hnp_enable
2145 ? USB_DEVICE_B_HNP_ENABLE
2146 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2147 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2148 if (err < 0) {
2149 /* OTG MESSAGE: report errors here,
2150 * customize to match your product.
2151 */
2152 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002153 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 err);
2155 bus->b_hnp_enable = 0;
2156 }
2157 }
2158 }
2159 }
2160
2161 if (!is_targeted(udev)) {
2162
2163 /* Maybe it can talk to us, though we can't talk to it.
2164 * (Includes HNP test device.)
2165 */
2166 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002167 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 if (err < 0)
2169 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2170 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002171 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 goto fail;
2173 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002174fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002176 return err;
2177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002179
2180/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002181 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002182 * @udev: newly addressed device (in ADDRESS state)
2183 *
2184 * This is only called by usb_new_device() and usb_authorize_device()
2185 * and FIXME -- all comments that apply to them apply here wrt to
2186 * environment.
2187 *
2188 * If the device is WUSB and not authorized, we don't attempt to read
2189 * the string descriptors, as they will be errored out by the device
2190 * until it has been authorized.
2191 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002192static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002193{
2194 int err;
2195
2196 if (udev->config == NULL) {
2197 err = usb_get_configuration(udev);
2198 if (err < 0) {
2199 dev_err(&udev->dev, "can't read configurations, error %d\n",
2200 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002201 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002202 }
2203 }
2204 if (udev->wusb == 1 && udev->authorized == 0) {
2205 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2206 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2207 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2208 }
2209 else {
2210 /* read the standard strings and cache them if present */
2211 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2212 udev->manufacturer = usb_cache_string(udev,
2213 udev->descriptor.iManufacturer);
2214 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2215 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002216 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002217 if (err < 0)
2218 return err;
2219
2220 usb_detect_interface_quirks(udev);
2221
2222 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002223}
2224
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002225static void set_usb_port_removable(struct usb_device *udev)
2226{
2227 struct usb_device *hdev = udev->parent;
2228 struct usb_hub *hub;
2229 u8 port = udev->portnum;
2230 u16 wHubCharacteristics;
2231 bool removable = true;
2232
2233 if (!hdev)
2234 return;
2235
Lan Tianyuad493e52013-01-23 04:26:30 +08002236 hub = usb_hub_to_struct_hub(udev->parent);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002237
2238 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2239
2240 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2241 return;
2242
2243 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002244 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2245 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002246 removable = false;
2247 } else {
2248 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2249 removable = false;
2250 }
2251
2252 if (removable)
2253 udev->removable = USB_DEVICE_REMOVABLE;
2254 else
2255 udev->removable = USB_DEVICE_FIXED;
2256}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002257
2258/**
2259 * usb_new_device - perform initial device setup (usbcore-internal)
2260 * @udev: newly addressed device (in ADDRESS state)
2261 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002262 * This is called with devices which have been detected but not fully
2263 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002264 * for any device configuration. The caller must have locked either
2265 * the parent hub (if udev is a normal device) or else the
2266 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2267 * udev has already been installed, but udev is not yet visible through
2268 * sysfs or other filesystem code.
2269 *
2270 * It will return if the device is configured properly or not. Zero if
2271 * the interface was registered with the driver core; else a negative
2272 * errno value.
2273 *
2274 * This call is synchronous, and may not be used in an interrupt context.
2275 *
2276 * Only the hub driver or root-hub registrar should ever call this.
2277 */
2278int usb_new_device(struct usb_device *udev)
2279{
2280 int err;
2281
Dan Streetman16985402010-01-06 09:56:53 -05002282 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002283 /* Initialize non-root-hub device wakeup to disabled;
2284 * device (un)configuration controls wakeup capable
2285 * sysfs power/wakeup controls wakeup enabled/disabled
2286 */
2287 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002288 }
2289
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002290 /* Tell the runtime-PM framework the device is active */
2291 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002292 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002293 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002294 pm_runtime_enable(&udev->dev);
2295
Alan Sternc08512c2010-11-15 15:57:58 -05002296 /* By default, forbid autosuspend for all devices. It will be
2297 * allowed for hubs during binding.
2298 */
2299 usb_disable_autosuspend(udev);
2300
Alan Stern8d8558d2009-12-08 15:50:41 -05002301 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002302 if (err < 0)
2303 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002304 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2305 udev->devnum, udev->bus->busnum,
2306 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002307 /* export the usbdev device-node for libusb */
2308 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2309 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2310
Alan Stern6cd13202008-11-13 15:08:30 -05002311 /* Tell the world! */
2312 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002313
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002314 if (udev->serial)
2315 add_device_randomness(udev->serial, strlen(udev->serial));
2316 if (udev->product)
2317 add_device_randomness(udev->product, strlen(udev->product));
2318 if (udev->manufacturer)
2319 add_device_randomness(udev->manufacturer,
2320 strlen(udev->manufacturer));
2321
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002322 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002323
2324 /*
2325 * check whether the hub marks this port as non-removable. Do it
2326 * now so that platform-specific data can override it in
2327 * device_add()
2328 */
2329 if (udev->parent)
2330 set_usb_port_removable(udev);
2331
Alan Stern782da722006-07-01 22:09:35 -04002332 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002333 * for configuring the device and invoking the add-device
2334 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002335 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002336 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (err) {
2338 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2339 goto fail;
2340 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002341
Lan Tianyufde26382013-01-20 01:53:33 +08002342 /* Create link files between child device and usb port device. */
2343 if (udev->parent) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002344 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
2345 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002346
2347 err = sysfs_create_link(&udev->dev.kobj,
2348 &port_dev->dev.kobj, "port");
2349 if (err)
2350 goto fail;
2351
2352 err = sysfs_create_link(&port_dev->dev.kobj,
2353 &udev->dev.kobj, "device");
2354 if (err) {
2355 sysfs_remove_link(&udev->dev.kobj, "port");
2356 goto fail;
2357 }
Lan Tianyu971fcd42013-01-23 04:26:29 +08002358
2359 pm_runtime_get_sync(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002360 }
2361
Alan Stern3b23dd62008-12-05 14:10:34 -05002362 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002363 usb_mark_last_busy(udev);
2364 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002365 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367fail:
2368 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002369 pm_runtime_disable(&udev->dev);
2370 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002371 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002374
2375/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002376 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2377 * @usb_dev: USB device
2378 *
2379 * Move the USB device to a very basic state where interfaces are disabled
2380 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002381 *
2382 * We share a lock (that we have) with device_del(), so we need to
2383 * defer its call.
2384 */
2385int usb_deauthorize_device(struct usb_device *usb_dev)
2386{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002387 usb_lock_device(usb_dev);
2388 if (usb_dev->authorized == 0)
2389 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002390
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002391 usb_dev->authorized = 0;
2392 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002393
2394 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002395 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002396 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002397 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002398 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002399 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002400
2401 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002402 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002403
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002404out_unauthorized:
2405 usb_unlock_device(usb_dev);
2406 return 0;
2407}
2408
2409
2410int usb_authorize_device(struct usb_device *usb_dev)
2411{
2412 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002413
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002414 usb_lock_device(usb_dev);
2415 if (usb_dev->authorized == 1)
2416 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002417
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002418 result = usb_autoresume_device(usb_dev);
2419 if (result < 0) {
2420 dev_err(&usb_dev->dev,
2421 "can't autoresume for authorization: %d\n", result);
2422 goto error_autoresume;
2423 }
2424 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2425 if (result < 0) {
2426 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2427 "authorization: %d\n", result);
2428 goto error_device_descriptor;
2429 }
Alan Sternda307122009-12-08 15:54:44 -05002430
2431 kfree(usb_dev->product);
2432 usb_dev->product = NULL;
2433 kfree(usb_dev->manufacturer);
2434 usb_dev->manufacturer = NULL;
2435 kfree(usb_dev->serial);
2436 usb_dev->serial = NULL;
2437
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002438 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002439 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002440 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002441 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002442 /* Choose and set the configuration. This registers the interfaces
2443 * with the driver core and lets interface drivers bind to them.
2444 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002445 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002446 if (c >= 0) {
2447 result = usb_set_configuration(usb_dev, c);
2448 if (result) {
2449 dev_err(&usb_dev->dev,
2450 "can't set config #%d, error %d\n", c, result);
2451 /* This need not be fatal. The user can try to
2452 * set other configurations. */
2453 }
2454 }
2455 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002456
Alan Stern8d8558d2009-12-08 15:50:41 -05002457error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002458error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002459 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002460error_autoresume:
2461out_authorized:
2462 usb_unlock_device(usb_dev); // complements locktree
2463 return result;
2464}
2465
2466
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002467/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2468static unsigned hub_is_wusb(struct usb_hub *hub)
2469{
2470 struct usb_hcd *hcd;
2471 if (hub->hdev->parent != NULL) /* not a root hub? */
2472 return 0;
2473 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2474 return hcd->wireless;
2475}
2476
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478#define PORT_RESET_TRIES 5
2479#define SET_ADDRESS_TRIES 2
2480#define GET_DESCRIPTOR_TRIES 2
2481#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302482#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2485#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002486#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487#define HUB_LONG_RESET_TIME 200
Sarah Sharp77c7f072012-11-14 17:16:52 -08002488#define HUB_RESET_TIMEOUT 800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Sarah Sharp10d674a2011-09-14 14:24:52 -07002490static int hub_port_reset(struct usb_hub *hub, int port1,
2491 struct usb_device *udev, unsigned int delay, bool warm);
2492
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002493/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2494 * Port worm reset is required to recover
2495 */
2496static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002497{
2498 return hub_is_superspeed(hub->hdev) &&
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002499 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2500 USB_SS_PORT_LS_SS_INACTIVE) ||
2501 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2502 USB_SS_PORT_LS_COMP_MOD)) ;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002503}
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002506 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
2508 int delay_time, ret;
2509 u16 portstatus;
2510 u16 portchange;
2511
2512 for (delay_time = 0;
2513 delay_time < HUB_RESET_TIMEOUT;
2514 delay_time += delay) {
2515 /* wait to give the device a chance to reset */
2516 msleep(delay);
2517
2518 /* read and decode port status */
2519 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2520 if (ret < 0)
2521 return ret;
2522
Sarah Sharp4f434472012-11-15 14:58:04 -08002523 /* The port state is unknown until the reset completes. */
Sarah Sharp470f0be2012-12-11 10:14:03 -08002524 if (!(portstatus & USB_PORT_STAT_RESET))
2525 break;
Sarah Sharp4f434472012-11-15 14:58:04 -08002526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 /* switch to the long delay after two short delay failures */
2528 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2529 delay = HUB_LONG_RESET_TIME;
2530
2531 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002532 "port %d not %sreset yet, waiting %dms\n",
2533 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
2535
Sarah Sharp470f0be2012-12-11 10:14:03 -08002536 if ((portstatus & USB_PORT_STAT_RESET))
2537 return -EBUSY;
2538
2539 if (hub_port_warm_reset_required(hub, portstatus))
2540 return -ENOTCONN;
2541
2542 /* Device went away? */
2543 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2544 return -ENOTCONN;
2545
2546 /* bomb out completely if the connection bounced. A USB 3.0
2547 * connection may bounce if multiple warm resets were issued,
2548 * but the device may have successfully re-connected. Ignore it.
2549 */
2550 if (!hub_is_superspeed(hub->hdev) &&
2551 (portchange & USB_PORT_STAT_C_CONNECTION))
2552 return -ENOTCONN;
2553
2554 if (!(portstatus & USB_PORT_STAT_ENABLE))
2555 return -EBUSY;
2556
2557 if (!udev)
2558 return 0;
2559
2560 if (hub_is_wusb(hub))
2561 udev->speed = USB_SPEED_WIRELESS;
2562 else if (hub_is_superspeed(hub->hdev))
2563 udev->speed = USB_SPEED_SUPER;
2564 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2565 udev->speed = USB_SPEED_HIGH;
2566 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2567 udev->speed = USB_SPEED_LOW;
2568 else
2569 udev->speed = USB_SPEED_FULL;
2570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
Andiry Xu75d7cf72011-09-13 16:41:11 -07002573static void hub_port_finish_reset(struct usb_hub *hub, int port1,
Sarah Sharpa24a6072012-11-01 11:20:44 -07002574 struct usb_device *udev, int *status)
Andiry Xu75d7cf72011-09-13 16:41:11 -07002575{
2576 switch (*status) {
2577 case 0:
Sarah Sharpa24a6072012-11-01 11:20:44 -07002578 /* TRSTRCY = 10 ms; plus some extra */
2579 msleep(10 + 40);
2580 if (udev) {
2581 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2582
2583 update_devnum(udev, 0);
2584 /* The xHC may think the device is already reset,
2585 * so ignore the status.
2586 */
2587 if (hcd->driver->reset_device)
2588 hcd->driver->reset_device(hcd, udev);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002589 }
2590 /* FALL THROUGH */
2591 case -ENOTCONN:
2592 case -ENODEV:
Lan Tianyuad493e52013-01-23 04:26:30 +08002593 usb_clear_port_feature(hub->hdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002594 port1, USB_PORT_FEAT_C_RESET);
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002595 if (hub_is_superspeed(hub->hdev)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002596 usb_clear_port_feature(hub->hdev, port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002597 USB_PORT_FEAT_C_BH_PORT_RESET);
Lan Tianyuad493e52013-01-23 04:26:30 +08002598 usb_clear_port_feature(hub->hdev, port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002599 USB_PORT_FEAT_C_PORT_LINK_STATE);
Lan Tianyuad493e52013-01-23 04:26:30 +08002600 usb_clear_port_feature(hub->hdev, port1,
Sarah Sharpa24a6072012-11-01 11:20:44 -07002601 USB_PORT_FEAT_C_CONNECTION);
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002602 }
Sarah Sharpa24a6072012-11-01 11:20:44 -07002603 if (udev)
Andiry Xu75d7cf72011-09-13 16:41:11 -07002604 usb_set_device_state(udev, *status
2605 ? USB_STATE_NOTATTACHED
2606 : USB_STATE_DEFAULT);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002607 break;
2608 }
2609}
2610
2611/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002613 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
2615 int i, status;
Sarah Sharpa24a6072012-11-01 11:20:44 -07002616 u16 portchange, portstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002618 if (!hub_is_superspeed(hub->hdev)) {
2619 if (warm) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002620 dev_err(hub->intfdev, "only USB3 hub support "
2621 "warm reset\n");
2622 return -EINVAL;
2623 }
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002624 /* Block EHCI CF initialization during the port reset.
2625 * Some companion controllers don't like it when they mix.
2626 */
2627 down_read(&ehci_cf_port_reset_rwsem);
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07002628 } else if (!warm) {
2629 /*
2630 * If the caller hasn't explicitly requested a warm reset,
2631 * double check and see if one is needed.
2632 */
2633 status = hub_port_status(hub, port1,
2634 &portstatus, &portchange);
2635 if (status < 0)
2636 goto done;
2637
2638 if (hub_port_warm_reset_required(hub, portstatus))
2639 warm = true;
Andiry Xu75d7cf72011-09-13 16:41:11 -07002640 }
Alan Stern32fe0192007-10-10 16:27:07 -04002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 /* Reset the port */
2643 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002644 status = set_port_feature(hub->hdev, port1, (warm ?
2645 USB_PORT_FEAT_BH_PORT_RESET :
2646 USB_PORT_FEAT_RESET));
2647 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002649 "cannot %sreset port %d (err = %d)\n",
2650 warm ? "warm " : "", port1, status);
2651 } else {
2652 status = hub_port_wait_reset(hub, port1, udev, delay,
2653 warm);
David Brownelldd165252005-08-31 10:45:25 -07002654 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 dev_dbg(hub->intfdev,
2656 "port_wait_reset: err = %d\n",
2657 status);
2658 }
2659
Sarah Sharpa24a6072012-11-01 11:20:44 -07002660 /* Check for disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002661 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
Sarah Sharpa24a6072012-11-01 11:20:44 -07002662 hub_port_finish_reset(hub, port1, udev, &status);
2663
2664 if (!hub_is_superspeed(hub->hdev))
2665 goto done;
2666
2667 /*
2668 * If a USB 3.0 device migrates from reset to an error
2669 * state, re-issue the warm reset.
2670 */
2671 if (hub_port_status(hub, port1,
2672 &portstatus, &portchange) < 0)
2673 goto done;
2674
2675 if (!hub_port_warm_reset_required(hub, portstatus))
2676 goto done;
2677
2678 /*
2679 * If the port is in SS.Inactive or Compliance Mode, the
2680 * hot or warm reset failed. Try another warm reset.
2681 */
2682 if (!warm) {
2683 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2684 port1);
2685 warm = true;
2686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
2688
2689 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002690 "port %d not enabled, trying %sreset again...\n",
2691 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 delay = HUB_LONG_RESET_TIME;
2693 }
2694
2695 dev_err (hub->intfdev,
2696 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2697 port1);
2698
Andiry Xu75d7cf72011-09-13 16:41:11 -07002699done:
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002700 if (!hub_is_superspeed(hub->hdev))
Andiry Xu75d7cf72011-09-13 16:41:11 -07002701 up_read(&ehci_cf_port_reset_rwsem);
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 return status;
2704}
2705
Andiry Xu0ed9a572011-04-27 18:07:43 +08002706/* Check if a port is power on */
2707static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2708{
2709 int ret = 0;
2710
2711 if (hub_is_superspeed(hub->hdev)) {
2712 if (portstatus & USB_SS_PORT_STAT_POWER)
2713 ret = 1;
2714 } else {
2715 if (portstatus & USB_PORT_STAT_POWER)
2716 ret = 1;
2717 }
2718
2719 return ret;
2720}
2721
Alan Sternd388dab2006-07-01 22:14:24 -04002722#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Andiry Xu0ed9a572011-04-27 18:07:43 +08002724/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2725static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2726{
2727 int ret = 0;
2728
2729 if (hub_is_superspeed(hub->hdev)) {
2730 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2731 == USB_SS_PORT_LS_U3)
2732 ret = 1;
2733 } else {
2734 if (portstatus & USB_PORT_STAT_SUSPEND)
2735 ret = 1;
2736 }
2737
2738 return ret;
2739}
Alan Sternb01b03f2008-04-28 11:06:11 -04002740
2741/* Determine whether the device on a port is ready for a normal resume,
2742 * is ready for a reset-resume, or should be disconnected.
2743 */
2744static int check_port_resume_type(struct usb_device *udev,
2745 struct usb_hub *hub, int port1,
2746 int status, unsigned portchange, unsigned portstatus)
2747{
2748 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002749 if (status || port_is_suspended(hub, portstatus) ||
2750 !port_is_power_on(hub, portstatus) ||
2751 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002752 if (status >= 0)
2753 status = -ENODEV;
2754 }
2755
Alan Stern86c57ed2008-06-30 11:14:43 -04002756 /* Can't do a normal resume if the port isn't enabled,
2757 * so try a reset-resume instead.
2758 */
2759 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2760 if (udev->persist_enabled)
2761 udev->reset_resume = 1;
2762 else
2763 status = -ENODEV;
2764 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002765
2766 if (status) {
2767 dev_dbg(hub->intfdev,
2768 "port %d status %04x.%04x after resume, %d\n",
2769 port1, portchange, portstatus, status);
2770 } else if (udev->reset_resume) {
2771
2772 /* Late port handoff can set status-change bits */
2773 if (portchange & USB_PORT_STAT_C_CONNECTION)
Lan Tianyuad493e52013-01-23 04:26:30 +08002774 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04002775 USB_PORT_FEAT_C_CONNECTION);
2776 if (portchange & USB_PORT_STAT_C_ENABLE)
Lan Tianyuad493e52013-01-23 04:26:30 +08002777 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04002778 USB_PORT_FEAT_C_ENABLE);
2779 }
2780
2781 return status;
2782}
2783
Sarah Sharpf74631e2012-06-25 12:08:08 -07002784int usb_disable_ltm(struct usb_device *udev)
2785{
2786 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2787
2788 /* Check if the roothub and device supports LTM. */
2789 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2790 !usb_device_supports_ltm(udev))
2791 return 0;
2792
2793 /* Clear Feature LTM Enable can only be sent if the device is
2794 * configured.
2795 */
2796 if (!udev->actconfig)
2797 return 0;
2798
2799 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2800 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2801 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2802 USB_CTRL_SET_TIMEOUT);
2803}
2804EXPORT_SYMBOL_GPL(usb_disable_ltm);
2805
2806void usb_enable_ltm(struct usb_device *udev)
2807{
2808 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2809
2810 /* Check if the roothub and device supports LTM. */
2811 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2812 !usb_device_supports_ltm(udev))
2813 return;
2814
2815 /* Set Feature LTM Enable can only be sent if the device is
2816 * configured.
2817 */
2818 if (!udev->actconfig)
2819 return;
2820
2821 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2822 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2823 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2824 USB_CTRL_SET_TIMEOUT);
2825}
2826EXPORT_SYMBOL_GPL(usb_enable_ltm);
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828#ifdef CONFIG_USB_SUSPEND
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002829/*
2830 * usb_disable_function_remotewakeup - disable usb3.0
2831 * device's function remote wakeup
2832 * @udev: target device
2833 *
2834 * Assume there's only one function on the USB 3.0
2835 * device and disable remote wake for the first
2836 * interface. FIXME if the interface association
2837 * descriptor shows there's more than one function.
2838 */
2839static int usb_disable_function_remotewakeup(struct usb_device *udev)
2840{
2841 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2842 USB_REQ_CLEAR_FEATURE, USB_RECIP_INTERFACE,
2843 USB_INTRF_FUNC_SUSPEND, 0, NULL, 0,
2844 USB_CTRL_SET_TIMEOUT);
2845}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
2847/*
Alan Stern140d8f62006-07-01 22:07:21 -04002848 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002849 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002850 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 *
2852 * Suspends a USB device that isn't in active use, conserving power.
2853 * Devices may wake out of a suspend, if anything important happens,
2854 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002855 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 * to disconnect devices while they are suspended.
2857 *
David Brownell390a8c32005-09-13 19:57:27 -07002858 * This only affects the USB hardware for a device; its interfaces
2859 * (and, for hubs, child devices) must already have been suspended.
2860 *
Alan Stern624d6c02007-05-30 15:35:16 -04002861 * Selective port suspend reduces power; most suspended devices draw
2862 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2863 * All devices below the suspended port are also suspended.
2864 *
2865 * Devices leave suspend state when the host wakes them up. Some devices
2866 * also support "remote wakeup", where the device can activate the USB
2867 * tree above them to deliver data, such as a keypress or packet. In
2868 * some cases, this wakes the USB host.
2869 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 * Suspending OTG devices may trigger HNP, if that's been enabled
2871 * between a pair of dual-role devices. That will change roles, such
2872 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2873 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002874 * Devices on USB hub ports have only one "suspend" state, corresponding
2875 * to ACPI D2, "may cause the device to lose some context".
2876 * State transitions include:
2877 *
2878 * - suspend, resume ... when the VBUS power link stays live
2879 * - suspend, disconnect ... VBUS lost
2880 *
2881 * Once VBUS drop breaks the circuit, the port it's using has to go through
2882 * normal re-enumeration procedures, starting with enabling VBUS power.
2883 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2884 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2885 * timer, no SRP, no requests through sysfs.
2886 *
2887 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2888 * the root hub for their bus goes into global suspend ... so we don't
2889 * (falsely) update the device power state to say it suspended.
2890 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 * Returns 0 on success, else negative errno.
2892 */
Alan Stern65bfd292008-11-25 16:39:18 -05002893int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
Lan Tianyuad493e52013-01-23 04:26:30 +08002895 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
2896 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
2897 enum pm_qos_flags_status pm_qos_stat;
Alan Stern624d6c02007-05-30 15:35:16 -04002898 int port1 = udev->portnum;
2899 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002900
Alan Stern624d6c02007-05-30 15:35:16 -04002901 /* enable remote wakeup when appropriate; this lets the device
2902 * wake up the upstream hub (including maybe the root hub).
2903 *
2904 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2905 * we don't explicitly enable it here.
2906 */
2907 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002908 if (!hub_is_superspeed(hub->hdev)) {
2909 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2910 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2911 USB_DEVICE_REMOTE_WAKEUP, 0,
2912 NULL, 0,
2913 USB_CTRL_SET_TIMEOUT);
2914 } else {
2915 /* Assume there's only one function on the USB 3.0
2916 * device and enable remote wake for the first
2917 * interface. FIXME if the interface association
2918 * descriptor shows there's more than one function.
2919 */
2920 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2921 USB_REQ_SET_FEATURE,
2922 USB_RECIP_INTERFACE,
2923 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002924 USB_INTRF_FUNC_SUSPEND_RW |
2925 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002926 NULL, 0,
2927 USB_CTRL_SET_TIMEOUT);
2928 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002929 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002930 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2931 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002932 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002933 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002934 return status;
2935 }
Alan Stern624d6c02007-05-30 15:35:16 -04002936 }
2937
Andiry Xu65580b432011-09-23 14:19:52 -07002938 /* disable USB2 hardware LPM */
2939 if (udev->usb2_hw_lpm_enabled == 1)
2940 usb_set_usb2_hardware_lpm(udev, 0);
2941
Sarah Sharpf74631e2012-06-25 12:08:08 -07002942 if (usb_disable_ltm(udev)) {
2943 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2944 __func__);
2945 return -ENOMEM;
2946 }
Sarah Sharp83060952012-05-02 14:25:52 -07002947 if (usb_unlocked_disable_lpm(udev)) {
2948 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2949 __func__);
2950 return -ENOMEM;
2951 }
2952
Alan Stern624d6c02007-05-30 15:35:16 -04002953 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002954 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08002955 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
Andiry Xua8f08d82011-03-31 14:56:50 +08002956 else
2957 status = set_port_feature(hub->hdev, port1,
2958 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002959 if (status) {
2960 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2961 port1, status);
2962 /* paranoia: "should not happen" */
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002963 if (udev->do_remote_wakeup) {
2964 if (!hub_is_superspeed(hub->hdev)) {
2965 (void) usb_control_msg(udev,
2966 usb_sndctrlpipe(udev, 0),
2967 USB_REQ_CLEAR_FEATURE,
2968 USB_RECIP_DEVICE,
2969 USB_DEVICE_REMOTE_WAKEUP, 0,
2970 NULL, 0,
2971 USB_CTRL_SET_TIMEOUT);
2972 } else
2973 (void) usb_disable_function_remotewakeup(udev);
2974
2975 }
Alan Sterncbb33002011-06-15 16:29:16 -04002976
Andiry Xuc3e751e2012-05-05 00:50:10 +08002977 /* Try to enable USB2 hardware LPM again */
2978 if (udev->usb2_hw_lpm_capable == 1)
2979 usb_set_usb2_hardware_lpm(udev, 1);
2980
Sarah Sharpf74631e2012-06-25 12:08:08 -07002981 /* Try to enable USB3 LTM and LPM again */
2982 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07002983 usb_unlocked_enable_lpm(udev);
2984
Alan Sterncbb33002011-06-15 16:29:16 -04002985 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002986 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002987 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002988 } else {
2989 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002990 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2991 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2992 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002993 usb_set_device_state(udev, USB_STATE_SUSPENDED);
Alan Sternbfd1e912012-10-19 11:03:39 -04002994 udev->port_is_suspended = 1;
Alan Stern624d6c02007-05-30 15:35:16 -04002995 msleep(10);
2996 }
Lan Tianyuad493e52013-01-23 04:26:30 +08002997
2998 /*
2999 * Check whether current status meets the requirement of
3000 * usb port power off mechanism
3001 */
3002 pm_qos_stat = dev_pm_qos_flags(&port_dev->dev,
3003 PM_QOS_FLAG_NO_POWER_OFF);
3004 if (!udev->do_remote_wakeup
3005 && pm_qos_stat != PM_QOS_FLAGS_ALL
3006 && udev->persist_enabled
3007 && !status) {
3008 pm_runtime_put_sync(&port_dev->dev);
3009 port_dev->did_runtime_put = true;
3010 }
3011
Alan Sternc08512c2010-11-15 15:57:58 -05003012 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04003013 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014}
David Brownellf3f32532005-09-22 22:37:29 -07003015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016/*
David Brownell390a8c32005-09-13 19:57:27 -07003017 * If the USB "suspend" state is in use (rather than "global suspend"),
3018 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04003019 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 * hardware resume signaling is finished, either because of selective
3021 * resume (by host) or remote wakeup (by device) ... now see what changed
3022 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04003023 *
3024 * If @udev->reset_resume is set then the device is reset before the
3025 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 */
Alan Stern140d8f62006-07-01 22:07:21 -04003027static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
Alan Stern54515fe2007-05-30 15:38:58 -04003029 int status = 0;
Oliver Neukum07e72b92012-11-29 15:05:57 +01003030 u16 devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003033 dev_dbg(&udev->dev, "%s\n",
3034 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
3036 /* usb ch9 identifies four variants of SUSPENDED, based on what
3037 * state the device resumes to. Linux currently won't see the
3038 * first two on the host side; they'd be inside hub_port_init()
3039 * during many timeouts, but khubd can't suspend until later.
3040 */
3041 usb_set_device_state(udev, udev->actconfig
3042 ? USB_STATE_CONFIGURED
3043 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Alan Stern54515fe2007-05-30 15:38:58 -04003045 /* 10.5.4.5 says not to reset a suspended port if the attached
3046 * device is enabled for remote wakeup. Hence the reset
3047 * operation is carried out here, after the port has been
3048 * resumed.
3049 */
3050 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04003051 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08003052 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003053
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 /* 10.5.4.5 says be sure devices in the tree are still there.
3055 * For now let's assume the device didn't go crazy on resume,
3056 * and device drivers will know about any resume quirks.
3057 */
Alan Stern54515fe2007-05-30 15:38:58 -04003058 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04003059 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04003060 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
3061 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04003062 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04003063
3064 /* If a normal resume failed, try doing a reset-resume */
3065 if (status && !udev->reset_resume && udev->persist_enabled) {
3066 dev_dbg(&udev->dev, "retry with reset-resume\n");
3067 udev->reset_resume = 1;
3068 goto retry_reset_resume;
3069 }
Alan Stern54515fe2007-05-30 15:38:58 -04003070 }
Alan Sternb40b7a92006-06-19 15:12:38 -04003071
Alan Stern624d6c02007-05-30 15:35:16 -04003072 if (status) {
3073 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
3074 status);
Oliver Neukum07e72b92012-11-29 15:05:57 +01003075 /*
3076 * There are a few quirky devices which violate the standard
3077 * by claiming to have remote wakeup enabled after a reset,
3078 * which crash if the feature is cleared, hence check for
3079 * udev->reset_resume
3080 */
3081 } else if (udev->actconfig && !udev->reset_resume) {
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003082 if (!hub_is_superspeed(udev->parent)) {
3083 le16_to_cpus(&devstatus);
3084 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
3085 status = usb_control_msg(udev,
3086 usb_sndctrlpipe(udev, 0),
3087 USB_REQ_CLEAR_FEATURE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 USB_RECIP_DEVICE,
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003089 USB_DEVICE_REMOTE_WAKEUP, 0,
3090 NULL, 0,
3091 USB_CTRL_SET_TIMEOUT);
3092 } else {
3093 status = usb_get_status(udev, USB_RECIP_INTERFACE, 0,
3094 &devstatus);
3095 le16_to_cpus(&devstatus);
3096 if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP
3097 | USB_INTRF_STAT_FUNC_RW))
3098 status =
3099 usb_disable_function_remotewakeup(udev);
Alan Stern4bf0ba82005-11-21 11:58:07 -05003100 }
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003101
3102 if (status)
3103 dev_dbg(&udev->dev,
3104 "disable remote wakeup, status %d\n",
3105 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 }
3108 return status;
3109}
3110
Alan Stern624d6c02007-05-30 15:35:16 -04003111/*
3112 * usb_port_resume - re-activate a suspended usb device's upstream port
3113 * @udev: device to re-activate, not a root hub
3114 * Context: must be able to sleep; device not locked; pm locks held
3115 *
3116 * This will re-activate the suspended device, increasing power usage
3117 * while letting drivers communicate again with its endpoints.
3118 * USB resume explicitly guarantees that the power session between
3119 * the host and the device is the same as it was when the device
3120 * suspended.
3121 *
Alan Sternfeccc302008-03-03 15:15:59 -05003122 * If @udev->reset_resume is set then this routine won't check that the
3123 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04003124 * reset @udev. The end result is that a broken power session can be
3125 * recovered and @udev will appear to persist across a loss of VBUS power.
3126 *
3127 * For example, if a host controller doesn't maintain VBUS suspend current
3128 * during a system sleep or is reset when the system wakes up, all the USB
3129 * power sessions below it will be broken. This is especially troublesome
3130 * for mass-storage devices containing mounted filesystems, since the
3131 * device will appear to have disconnected and all the memory mappings
3132 * to it will be lost. Using the USB_PERSIST facility, the device can be
3133 * made to appear as if it had not disconnected.
3134 *
Ming Lei742120c2008-06-18 22:00:29 +08003135 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05003136 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04003137 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3138 * quite possible for a device to remain unaltered but its media to be
3139 * changed. If the user replaces a flash memory card while the system is
3140 * asleep, he will have only himself to blame when the filesystem on the
3141 * new card is corrupted and the system crashes.
3142 *
Alan Stern624d6c02007-05-30 15:35:16 -04003143 * Returns 0 on success, else negative errno.
3144 */
Alan Stern65bfd292008-11-25 16:39:18 -05003145int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
Lan Tianyuad493e52013-01-23 04:26:30 +08003147 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3148 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003149 int port1 = udev->portnum;
3150 int status;
3151 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003152
Lan Tianyuad493e52013-01-23 04:26:30 +08003153 if (port_dev->did_runtime_put) {
3154 status = pm_runtime_get_sync(&port_dev->dev);
3155 port_dev->did_runtime_put = false;
3156 if (status < 0) {
3157 dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
3158 status);
3159 return status;
3160 }
3161 }
3162
Alan Sternd25450c2006-11-20 11:14:30 -05003163 /* Skip the initial Clear-Suspend step for a remote wakeup */
3164 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003165 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003166 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
3168 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3169
Alan Sternd5cbad42006-08-11 16:52:39 -04003170 set_bit(port1, hub->busy_bits);
3171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003173 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003174 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
Andiry Xua7114232011-04-27 18:07:50 +08003175 else
Lan Tianyuad493e52013-01-23 04:26:30 +08003176 status = usb_clear_port_feature(hub->hdev,
Andiry Xua7114232011-04-27 18:07:50 +08003177 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003179 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
3180 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04003183 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003184 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 msleep(25);
3186
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3188 * stop resume signaling. Then finish the resume
3189 * sequence.
3190 */
Alan Sternd25450c2006-11-20 11:14:30 -05003191 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003192
Alan Sternb01b03f2008-04-28 11:06:11 -04003193 /* TRSMRCY = 10 msec */
3194 msleep(10);
3195 }
Alan Stern54515fe2007-05-30 15:38:58 -04003196
Alan Sternb01b03f2008-04-28 11:06:11 -04003197 SuspendCleared:
3198 if (status == 0) {
Alan Sternbfd1e912012-10-19 11:03:39 -04003199 udev->port_is_suspended = 0;
Andiry Xua7114232011-04-27 18:07:50 +08003200 if (hub_is_superspeed(hub->hdev)) {
3201 if (portchange & USB_PORT_STAT_C_LINK_STATE)
Lan Tianyuad493e52013-01-23 04:26:30 +08003202 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003203 USB_PORT_FEAT_C_PORT_LINK_STATE);
3204 } else {
3205 if (portchange & USB_PORT_STAT_C_SUSPEND)
Lan Tianyuad493e52013-01-23 04:26:30 +08003206 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003207 USB_PORT_FEAT_C_SUSPEND);
3208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Alan Sternd5cbad42006-08-11 16:52:39 -04003211 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04003212
Alan Sternb01b03f2008-04-28 11:06:11 -04003213 status = check_port_resume_type(udev,
3214 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003215 if (status == 0)
3216 status = finish_port_resume(udev);
3217 if (status < 0) {
3218 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3219 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003220 } else {
3221 /* Try to enable USB2 hardware LPM */
3222 if (udev->usb2_hw_lpm_capable == 1)
3223 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003224
Sarah Sharpf74631e2012-06-25 12:08:08 -07003225 /* Try to enable USB3 LTM and LPM */
3226 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003227 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003228 }
Andiry Xu65580b432011-09-23 14:19:52 -07003229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 return status;
3231}
3232
Alan Stern8808f002008-04-28 11:06:55 -04003233/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05003234int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
3236 int status = 0;
3237
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003239 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003240 status = usb_autoresume_device(udev);
3241 if (status == 0) {
3242 /* Let the drivers do their thing, then... */
3243 usb_autosuspend_device(udev);
3244 }
Alan Sternd25450c2006-11-20 11:14:30 -05003245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return status;
3247}
3248
Alan Sternd388dab2006-07-01 22:14:24 -04003249#else /* CONFIG_USB_SUSPEND */
3250
3251/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3252
Alan Stern65bfd292008-11-25 16:39:18 -05003253int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003254{
3255 return 0;
3256}
3257
Alan Sternb01b03f2008-04-28 11:06:11 -04003258/* However we may need to do a reset-resume */
3259
Alan Stern65bfd292008-11-25 16:39:18 -05003260int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003261{
Lan Tianyuad493e52013-01-23 04:26:30 +08003262 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
Alan Sternb01b03f2008-04-28 11:06:11 -04003263 int port1 = udev->portnum;
3264 int status;
3265 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04003266
Alan Sternb01b03f2008-04-28 11:06:11 -04003267 status = hub_port_status(hub, port1, &portstatus, &portchange);
3268 status = check_port_resume_type(udev,
3269 hub, port1, status, portchange, portstatus);
3270
3271 if (status) {
3272 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3273 hub_port_logical_disconnect(hub, port1);
3274 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04003275 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08003276 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003277 }
3278 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04003279}
3280
Alan Sternd388dab2006-07-01 22:14:24 -04003281#endif
3282
Ming Leie6f30de2012-10-24 11:59:24 +08003283static int check_ports_changed(struct usb_hub *hub)
3284{
3285 int port1;
3286
3287 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3288 u16 portstatus, portchange;
3289 int status;
3290
3291 status = hub_port_status(hub, port1, &portstatus, &portchange);
3292 if (!status && portchange)
3293 return 1;
3294 }
3295 return 0;
3296}
3297
David Brownelldb690872005-09-13 19:56:33 -07003298static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
3300 struct usb_hub *hub = usb_get_intfdata (intf);
3301 struct usb_device *hdev = hub->hdev;
3302 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003303 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Alan Sterncbb33002011-06-15 16:29:16 -04003305 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3307 struct usb_device *udev;
3308
Lan Tianyuff823c72012-09-05 13:44:32 +08003309 udev = hub->ports[port1 - 1]->child;
Alan Stern6840d252007-09-10 11:34:26 -04003310 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04003311 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003312 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003313 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 }
Ming Leie6f30de2012-10-24 11:59:24 +08003316
3317 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3318 /* check if there are changes pending on hub ports */
3319 if (check_ports_changed(hub)) {
3320 if (PMSG_IS_AUTO(msg))
3321 return -EBUSY;
3322 pm_wakeup_event(&hdev->dev, 2000);
3323 }
3324 }
3325
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003326 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3327 /* Enable hub to send remote wakeup for all ports. */
3328 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3329 status = set_port_feature(hdev,
3330 port1 |
3331 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3332 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3333 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3334 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3335 }
3336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Harvey Harrison441b62c2008-03-03 16:08:34 -08003338 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003339
David Brownellc9f89fa2005-09-13 19:57:04 -07003340 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003341 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343}
3344
3345static int hub_resume(struct usb_interface *intf)
3346{
Alan Stern5e6effa2008-03-03 15:15:51 -05003347 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Alan Stern5e6effa2008-03-03 15:15:51 -05003349 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003350 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 return 0;
3352}
3353
Alan Sternb41a60e2007-05-30 15:39:33 -04003354static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003355{
Alan Sternb41a60e2007-05-30 15:39:33 -04003356 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003357
Alan Stern5e6effa2008-03-03 15:15:51 -05003358 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003359 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003360 return 0;
3361}
3362
Alan Stern54515fe2007-05-30 15:38:58 -04003363/**
3364 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3365 * @rhdev: struct usb_device for the root hub
3366 *
3367 * The USB host controller driver calls this function when its root hub
3368 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003369 * has been reset. The routine marks @rhdev as having lost power.
3370 * When the hub driver is resumed it will take notice and carry out
3371 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3372 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003373 */
3374void usb_root_hub_lost_power(struct usb_device *rhdev)
3375{
3376 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3377 rhdev->reset_resume = 1;
3378}
3379EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3380
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003381static const char * const usb3_lpm_names[] = {
3382 "U0",
3383 "U1",
3384 "U2",
3385 "U3",
3386};
3387
3388/*
3389 * Send a Set SEL control transfer to the device, prior to enabling
3390 * device-initiated U1 or U2. This lets the device know the exit latencies from
3391 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3392 * packet from the host.
3393 *
3394 * This function will fail if the SEL or PEL values for udev are greater than
3395 * the maximum allowed values for the link state to be enabled.
3396 */
3397static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3398{
3399 struct usb_set_sel_req *sel_values;
3400 unsigned long long u1_sel;
3401 unsigned long long u1_pel;
3402 unsigned long long u2_sel;
3403 unsigned long long u2_pel;
3404 int ret;
3405
3406 /* Convert SEL and PEL stored in ns to us */
3407 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3408 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3409 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3410 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3411
3412 /*
3413 * Make sure that the calculated SEL and PEL values for the link
3414 * state we're enabling aren't bigger than the max SEL/PEL
3415 * value that will fit in the SET SEL control transfer.
3416 * Otherwise the device would get an incorrect idea of the exit
3417 * latency for the link state, and could start a device-initiated
3418 * U1/U2 when the exit latencies are too high.
3419 */
3420 if ((state == USB3_LPM_U1 &&
3421 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3422 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3423 (state == USB3_LPM_U2 &&
3424 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3425 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003426 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 -07003427 usb3_lpm_names[state], u1_sel, u1_pel);
3428 return -EINVAL;
3429 }
3430
3431 /*
3432 * If we're enabling device-initiated LPM for one link state,
3433 * but the other link state has a too high SEL or PEL value,
3434 * just set those values to the max in the Set SEL request.
3435 */
3436 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3437 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3438
3439 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3440 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3441
3442 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3443 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3444
3445 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3446 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3447
3448 /*
3449 * usb_enable_lpm() can be called as part of a failed device reset,
3450 * which may be initiated by an error path of a mass storage driver.
3451 * Therefore, use GFP_NOIO.
3452 */
3453 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3454 if (!sel_values)
3455 return -ENOMEM;
3456
3457 sel_values->u1_sel = u1_sel;
3458 sel_values->u1_pel = u1_pel;
3459 sel_values->u2_sel = cpu_to_le16(u2_sel);
3460 sel_values->u2_pel = cpu_to_le16(u2_pel);
3461
3462 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3463 USB_REQ_SET_SEL,
3464 USB_RECIP_DEVICE,
3465 0, 0,
3466 sel_values, sizeof *(sel_values),
3467 USB_CTRL_SET_TIMEOUT);
3468 kfree(sel_values);
3469 return ret;
3470}
3471
3472/*
3473 * Enable or disable device-initiated U1 or U2 transitions.
3474 */
3475static int usb_set_device_initiated_lpm(struct usb_device *udev,
3476 enum usb3_link_state state, bool enable)
3477{
3478 int ret;
3479 int feature;
3480
3481 switch (state) {
3482 case USB3_LPM_U1:
3483 feature = USB_DEVICE_U1_ENABLE;
3484 break;
3485 case USB3_LPM_U2:
3486 feature = USB_DEVICE_U2_ENABLE;
3487 break;
3488 default:
3489 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3490 __func__, enable ? "enable" : "disable");
3491 return -EINVAL;
3492 }
3493
3494 if (udev->state != USB_STATE_CONFIGURED) {
3495 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3496 "for unconfigured device.\n",
3497 __func__, enable ? "enable" : "disable",
3498 usb3_lpm_names[state]);
3499 return 0;
3500 }
3501
3502 if (enable) {
3503 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003504 * Now send the control transfer to enable device-initiated LPM
3505 * for either U1 or U2.
3506 */
3507 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3508 USB_REQ_SET_FEATURE,
3509 USB_RECIP_DEVICE,
3510 feature,
3511 0, NULL, 0,
3512 USB_CTRL_SET_TIMEOUT);
3513 } else {
3514 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3515 USB_REQ_CLEAR_FEATURE,
3516 USB_RECIP_DEVICE,
3517 feature,
3518 0, NULL, 0,
3519 USB_CTRL_SET_TIMEOUT);
3520 }
3521 if (ret < 0) {
3522 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3523 enable ? "Enable" : "Disable",
3524 usb3_lpm_names[state]);
3525 return -EBUSY;
3526 }
3527 return 0;
3528}
3529
3530static int usb_set_lpm_timeout(struct usb_device *udev,
3531 enum usb3_link_state state, int timeout)
3532{
3533 int ret;
3534 int feature;
3535
3536 switch (state) {
3537 case USB3_LPM_U1:
3538 feature = USB_PORT_FEAT_U1_TIMEOUT;
3539 break;
3540 case USB3_LPM_U2:
3541 feature = USB_PORT_FEAT_U2_TIMEOUT;
3542 break;
3543 default:
3544 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3545 __func__);
3546 return -EINVAL;
3547 }
3548
3549 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3550 timeout != USB3_LPM_DEVICE_INITIATED) {
3551 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3552 "which is a reserved value.\n",
3553 usb3_lpm_names[state], timeout);
3554 return -EINVAL;
3555 }
3556
3557 ret = set_port_feature(udev->parent,
3558 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3559 feature);
3560 if (ret < 0) {
3561 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3562 "error code %i\n", usb3_lpm_names[state],
3563 timeout, ret);
3564 return -EBUSY;
3565 }
3566 if (state == USB3_LPM_U1)
3567 udev->u1_params.timeout = timeout;
3568 else
3569 udev->u2_params.timeout = timeout;
3570 return 0;
3571}
3572
3573/*
3574 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3575 * U1/U2 entry.
3576 *
3577 * We will attempt to enable U1 or U2, but there are no guarantees that the
3578 * control transfers to set the hub timeout or enable device-initiated U1/U2
3579 * will be successful.
3580 *
3581 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3582 * driver know about it. If that call fails, it should be harmless, and just
3583 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3584 */
3585static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3586 enum usb3_link_state state)
3587{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003588 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003589 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3590 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3591
3592 /* If the device says it doesn't have *any* exit latency to come out of
3593 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3594 * state.
3595 */
3596 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3597 (state == USB3_LPM_U2 && u2_mel == 0))
3598 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003599
Sarah Sharp65a95b72012-10-05 10:32:07 -07003600 /*
3601 * First, let the device know about the exit latencies
3602 * associated with the link state we're about to enable.
3603 */
3604 ret = usb_req_set_sel(udev, state);
3605 if (ret < 0) {
3606 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3607 usb3_lpm_names[state]);
3608 return;
3609 }
3610
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003611 /* We allow the host controller to set the U1/U2 timeout internally
3612 * first, so that it can change its schedule to account for the
3613 * additional latency to send data to a device in a lower power
3614 * link state.
3615 */
3616 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3617
3618 /* xHCI host controller doesn't want to enable this LPM state. */
3619 if (timeout == 0)
3620 return;
3621
3622 if (timeout < 0) {
3623 dev_warn(&udev->dev, "Could not enable %s link state, "
3624 "xHCI error %i.\n", usb3_lpm_names[state],
3625 timeout);
3626 return;
3627 }
3628
3629 if (usb_set_lpm_timeout(udev, state, timeout))
3630 /* If we can't set the parent hub U1/U2 timeout,
3631 * device-initiated LPM won't be allowed either, so let the xHCI
3632 * host know that this link state won't be enabled.
3633 */
3634 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3635
3636 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3637 else if (udev->actconfig)
3638 usb_set_device_initiated_lpm(udev, state, true);
3639
3640}
3641
3642/*
3643 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3644 * U1/U2 entry.
3645 *
3646 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3647 * If zero is returned, the parent will not allow the link to go into U1/U2.
3648 *
3649 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3650 * it won't have an effect on the bus link state because the parent hub will
3651 * still disallow device-initiated U1/U2 entry.
3652 *
3653 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3654 * possible. The result will be slightly more bus bandwidth will be taken up
3655 * (to account for U1/U2 exit latency), but it should be harmless.
3656 */
3657static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3658 enum usb3_link_state state)
3659{
3660 int feature;
3661
3662 switch (state) {
3663 case USB3_LPM_U1:
3664 feature = USB_PORT_FEAT_U1_TIMEOUT;
3665 break;
3666 case USB3_LPM_U2:
3667 feature = USB_PORT_FEAT_U2_TIMEOUT;
3668 break;
3669 default:
3670 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3671 __func__);
3672 return -EINVAL;
3673 }
3674
3675 if (usb_set_lpm_timeout(udev, state, 0))
3676 return -EBUSY;
3677
3678 usb_set_device_initiated_lpm(udev, state, false);
3679
3680 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3681 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3682 "bus schedule bandwidth may be impacted.\n",
3683 usb3_lpm_names[state]);
3684 return 0;
3685}
3686
3687/*
3688 * Disable hub-initiated and device-initiated U1 and U2 entry.
3689 * Caller must own the bandwidth_mutex.
3690 *
3691 * This will call usb_enable_lpm() on failure, which will decrement
3692 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3693 */
3694int usb_disable_lpm(struct usb_device *udev)
3695{
3696 struct usb_hcd *hcd;
3697
3698 if (!udev || !udev->parent ||
3699 udev->speed != USB_SPEED_SUPER ||
3700 !udev->lpm_capable)
3701 return 0;
3702
3703 hcd = bus_to_hcd(udev->bus);
3704 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3705 return 0;
3706
3707 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003708 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003709 return 0;
3710
3711 /* If LPM is enabled, attempt to disable it. */
3712 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3713 goto enable_lpm;
3714 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3715 goto enable_lpm;
3716
3717 return 0;
3718
3719enable_lpm:
3720 usb_enable_lpm(udev);
3721 return -EBUSY;
3722}
3723EXPORT_SYMBOL_GPL(usb_disable_lpm);
3724
3725/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3726int usb_unlocked_disable_lpm(struct usb_device *udev)
3727{
3728 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3729 int ret;
3730
3731 if (!hcd)
3732 return -EINVAL;
3733
3734 mutex_lock(hcd->bandwidth_mutex);
3735 ret = usb_disable_lpm(udev);
3736 mutex_unlock(hcd->bandwidth_mutex);
3737
3738 return ret;
3739}
3740EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3741
3742/*
3743 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3744 * xHCI host policy may prevent U1 or U2 from being enabled.
3745 *
3746 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3747 * until the lpm_disable_count drops to zero. Caller must own the
3748 * bandwidth_mutex.
3749 */
3750void usb_enable_lpm(struct usb_device *udev)
3751{
3752 struct usb_hcd *hcd;
3753
3754 if (!udev || !udev->parent ||
3755 udev->speed != USB_SPEED_SUPER ||
3756 !udev->lpm_capable)
3757 return;
3758
3759 udev->lpm_disable_count--;
3760 hcd = bus_to_hcd(udev->bus);
3761 /* Double check that we can both enable and disable LPM.
3762 * Device must be configured to accept set feature U1/U2 timeout.
3763 */
3764 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3765 !hcd->driver->disable_usb3_lpm_timeout)
3766 return;
3767
3768 if (udev->lpm_disable_count > 0)
3769 return;
3770
3771 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3772 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3773}
3774EXPORT_SYMBOL_GPL(usb_enable_lpm);
3775
3776/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3777void usb_unlocked_enable_lpm(struct usb_device *udev)
3778{
3779 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3780
3781 if (!hcd)
3782 return;
3783
3784 mutex_lock(hcd->bandwidth_mutex);
3785 usb_enable_lpm(udev);
3786 mutex_unlock(hcd->bandwidth_mutex);
3787}
3788EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3789
3790
Alan Sternd388dab2006-07-01 22:14:24 -04003791#else /* CONFIG_PM */
3792
Alan Sternf07600c2007-05-30 15:38:16 -04003793#define hub_suspend NULL
3794#define hub_resume NULL
3795#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003796
3797int usb_disable_lpm(struct usb_device *udev)
3798{
3799 return 0;
3800}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003801EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003802
3803void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003804EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003805
3806int usb_unlocked_disable_lpm(struct usb_device *udev)
3807{
3808 return 0;
3809}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003810EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003811
3812void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003813EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07003814
3815int usb_disable_ltm(struct usb_device *udev)
3816{
3817 return 0;
3818}
3819EXPORT_SYMBOL_GPL(usb_disable_ltm);
3820
3821void usb_enable_ltm(struct usb_device *udev) { }
3822EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternd388dab2006-07-01 22:14:24 -04003823#endif
3824
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
3826/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3827 *
3828 * Between connect detection and reset signaling there must be a delay
3829 * of 100ms at least for debounce and power-settling. The corresponding
3830 * timer shall restart whenever the downstream port detects a disconnect.
3831 *
3832 * Apparently there are some bluetooth and irda-dongles and a number of
3833 * low-speed devices for which this debounce period may last over a second.
3834 * Not covered by the spec - but easy to deal with.
3835 *
3836 * This implementation uses a 1500ms total debounce timeout; if the
3837 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3838 * every 25ms for transient disconnects. When the port status has been
3839 * unchanged for 100ms it returns the port status.
3840 */
Lan Tianyuad493e52013-01-23 04:26:30 +08003841int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842{
3843 int ret;
3844 int total_time, stable_time = 0;
3845 u16 portchange, portstatus;
3846 unsigned connection = 0xffff;
3847
3848 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3849 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3850 if (ret < 0)
3851 return ret;
3852
3853 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3854 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
Lan Tianyuad493e52013-01-23 04:26:30 +08003855 if (!must_be_connected ||
3856 (connection == USB_PORT_STAT_CONNECTION))
3857 stable_time += HUB_DEBOUNCE_STEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 if (stable_time >= HUB_DEBOUNCE_STABLE)
3859 break;
3860 } else {
3861 stable_time = 0;
3862 connection = portstatus & USB_PORT_STAT_CONNECTION;
3863 }
3864
3865 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Lan Tianyuad493e52013-01-23 04:26:30 +08003866 usb_clear_port_feature(hub->hdev, port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 USB_PORT_FEAT_C_CONNECTION);
3868 }
3869
3870 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3871 break;
3872 msleep(HUB_DEBOUNCE_STEP);
3873 }
3874
3875 dev_dbg (hub->intfdev,
3876 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3877 port1, total_time, stable_time, portstatus);
3878
3879 if (stable_time < HUB_DEBOUNCE_STABLE)
3880 return -ETIMEDOUT;
3881 return portstatus;
3882}
3883
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003884void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885{
Alan Sternddeac4e72009-01-15 17:03:33 -05003886 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3887 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003888 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003890EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
3892#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3893#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3894
Alan Stern4326ed02007-07-30 17:08:43 -04003895static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896{
3897 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003898 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Sarah Sharpc6515272009-04-27 19:57:26 -07003900 /*
3901 * The host controller will choose the device address,
3902 * instead of the core having chosen it earlier
3903 */
3904 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 return -EINVAL;
3906 if (udev->state == USB_STATE_ADDRESS)
3907 return 0;
3908 if (udev->state != USB_STATE_DEFAULT)
3909 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003910 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003911 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003912 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003913 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3914 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3915 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003917 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003918 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003920 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 }
3922 return retval;
3923}
3924
3925/* Reset device, (re)assign address, get device descriptor.
3926 * Device connection must be stable, no more debouncing needed.
3927 * Returns device in USB_STATE_ADDRESS, except on error.
3928 *
3929 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003930 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 * newly detected device that is not accessible through any global
3932 * pointers, it's not necessary to lock the device.
3933 */
3934static int
3935hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3936 int retry_counter)
3937{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003938 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
3940 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003941 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 int i, j, retval;
3943 unsigned delay = HUB_SHORT_RESET_TIME;
3944 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003945 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003946 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
3948 /* root hub ports have a slightly longer reset period
3949 * (from USB 2.0 spec, section 7.1.7.5)
3950 */
3951 if (!hdev->parent) {
3952 delay = HUB_ROOT_RESET_TIME;
3953 if (port1 == hdev->bus->otg_port)
3954 hdev->bus->b_hnp_enable = 0;
3955 }
3956
3957 /* Some low speed devices have problems with the quick delay, so */
3958 /* be a bit pessimistic with those devices. RHbug #23670 */
3959 if (oldspeed == USB_SPEED_LOW)
3960 delay = HUB_LONG_RESET_TIME;
3961
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003962 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
Luben Tuikov07194ab2011-02-11 11:33:10 -08003964 /* Reset the device; full speed may morph to high speed */
3965 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003966 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003967 if (retval < 0) /* error or disconnect */
3968 goto fail;
3969 /* success, speed is known */
3970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 retval = -ENODEV;
3972
3973 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3974 dev_dbg(&udev->dev, "device reset changed speed!\n");
3975 goto fail;
3976 }
3977 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003978
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3980 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003981 * For Wireless USB devices, ep0 max packet is always 512 (tho
3982 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 */
3984 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003985 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003986 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003987 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003990 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 break;
3992 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3993 /* to determine the ep0 maxpacket size, try to read
3994 * the device descriptor to get bMaxPacketSize0 and
3995 * then correct our initial guess.
3996 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003997 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 break;
3999 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004000 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 break;
4002 default:
4003 goto fail;
4004 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004005
4006 if (udev->speed == USB_SPEED_WIRELESS)
4007 speed = "variable speed Wireless";
4008 else
4009 speed = usb_speed_string(udev->speed);
4010
Sarah Sharpc6515272009-04-27 19:57:26 -07004011 if (udev->speed != USB_SPEED_SUPER)
4012 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004013 "%s %s USB device number %d using %s\n",
4014 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05004015 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
4017 /* Set up TT records, if needed */
4018 if (hdev->tt) {
4019 udev->tt = hdev->tt;
4020 udev->ttport = hdev->ttport;
4021 } else if (udev->speed != USB_SPEED_HIGH
4022 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05004023 if (!hub->tt.hub) {
4024 dev_err(&udev->dev, "parent hub has no TT\n");
4025 retval = -EINVAL;
4026 goto fail;
4027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 udev->tt = &hub->tt;
4029 udev->ttport = port1;
4030 }
4031
4032 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
4033 * Because device hardware and firmware is sometimes buggy in
4034 * this area, and this is how Linux has done it for ages.
4035 * Change it cautiously.
4036 *
4037 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
4038 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
4039 * so it may help with some non-standards-compliant devices.
4040 * Otherwise we start with SET_ADDRESS and then try to read the
4041 * first 8 bytes of the device descriptor to get the ep0 maxpacket
4042 * value.
4043 */
4044 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07004045 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 struct usb_device_descriptor *buf;
4047 int r = 0;
4048
4049#define GET_DESCRIPTOR_BUFSIZE 64
4050 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
4051 if (!buf) {
4052 retval = -ENOMEM;
4053 continue;
4054 }
4055
Alan Sternb89ee192007-05-11 10:19:04 -04004056 /* Retry on all errors; some devices are flakey.
4057 * 255 is for WUSB devices, we actually need to use
4058 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 */
4060 for (j = 0; j < 3; ++j) {
4061 buf->bMaxPacketSize0 = 0;
4062 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
4063 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
4064 USB_DT_DEVICE << 8, 0,
4065 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02004066 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004068 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 if (buf->bDescriptorType ==
4070 USB_DT_DEVICE) {
4071 r = 0;
4072 break;
4073 }
4074 /* FALL THROUGH */
4075 default:
4076 if (r == 0)
4077 r = -EPROTO;
4078 break;
4079 }
4080 if (r == 0)
4081 break;
4082 }
4083 udev->descriptor.bMaxPacketSize0 =
4084 buf->bMaxPacketSize0;
4085 kfree(buf);
4086
Andiry Xu75d7cf72011-09-13 16:41:11 -07004087 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 if (retval < 0) /* error or disconnect */
4089 goto fail;
4090 if (oldspeed != udev->speed) {
4091 dev_dbg(&udev->dev,
4092 "device reset changed speed!\n");
4093 retval = -ENODEV;
4094 goto fail;
4095 }
4096 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004097 dev_err(&udev->dev,
4098 "device descriptor read/64, error %d\n",
4099 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 retval = -EMSGSIZE;
4101 continue;
4102 }
4103#undef GET_DESCRIPTOR_BUFSIZE
4104 }
4105
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004106 /*
4107 * If device is WUSB, we already assigned an
4108 * unauthorized address in the Connect Ack sequence;
4109 * authorization will assign the final address.
4110 */
Sarah Sharpc6515272009-04-27 19:57:26 -07004111 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004112 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
4113 retval = hub_set_address(udev, devnum);
4114 if (retval >= 0)
4115 break;
4116 msleep(200);
4117 }
4118 if (retval < 0) {
4119 dev_err(&udev->dev,
4120 "device not accepting address %d, error %d\n",
4121 devnum, retval);
4122 goto fail;
4123 }
Sarah Sharpc6515272009-04-27 19:57:26 -07004124 if (udev->speed == USB_SPEED_SUPER) {
4125 devnum = udev->devnum;
4126 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05004127 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07004128 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05004129 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07004130 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004131
4132 /* cope with hardware quirkiness:
4133 * - let SET_ADDRESS settle, some device hardware wants it
4134 * - read ep0 maxpacket even for high and low speed,
4135 */
4136 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07004137 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
4141 retval = usb_get_device_descriptor(udev, 8);
4142 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004143 dev_err(&udev->dev,
4144 "device descriptor read/8, error %d\n",
4145 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 if (retval >= 0)
4147 retval = -EMSGSIZE;
4148 } else {
4149 retval = 0;
4150 break;
4151 }
4152 }
4153 if (retval)
4154 goto fail;
4155
Peter Chenb76baa82012-11-09 09:44:43 +08004156 if (hcd->phy && !hdev->parent)
Peter Chenac965112012-11-09 09:44:44 +08004157 usb_phy_notify_connect(hcd->phy, udev->speed);
Peter Chenb76baa82012-11-09 09:44:43 +08004158
Elric Fud8aec3d2012-03-26 21:16:02 +08004159 /*
4160 * Some superspeed devices have finished the link training process
4161 * and attached to a superspeed hub port, but the device descriptor
4162 * got from those devices show they aren't superspeed devices. Warm
4163 * reset the port attached by the devices can fix them.
4164 */
4165 if ((udev->speed == USB_SPEED_SUPER) &&
4166 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4167 dev_err(&udev->dev, "got a wrong device descriptor, "
4168 "warm reset device\n");
4169 hub_port_reset(hub, port1, udev,
4170 HUB_BH_RESET_TIME, true);
4171 retval = -EINVAL;
4172 goto fail;
4173 }
4174
Sarah Sharp6b403b02009-04-27 19:54:10 -07004175 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
4176 udev->speed == USB_SPEED_SUPER)
4177 i = 512;
4178 else
4179 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07004180 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04004181 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04004183 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 retval = -EMSGSIZE;
4185 goto fail;
4186 }
Alan Stern56626a72010-10-14 15:25:21 -04004187 if (udev->speed == USB_SPEED_FULL)
4188 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4189 else
4190 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004192 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 }
4194
4195 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4196 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004197 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4198 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 if (retval >= 0)
4200 retval = -ENOMSG;
4201 goto fail;
4202 }
4203
Andiry Xu1ff4df52011-09-23 14:19:48 -07004204 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4205 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004206 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004207 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004208 usb_set_lpm_parameters(udev);
4209 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004210 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004211
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004213 /* notify HCD that we have a device connected and addressed */
4214 if (hcd->driver->update_device)
4215 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004217 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004219 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004220 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01004221 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 return retval;
4223}
4224
4225static void
4226check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4227{
4228 struct usb_qualifier_descriptor *qual;
4229 int status;
4230
Christoph Lametere94b1762006-12-06 20:33:17 -08004231 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 if (qual == NULL)
4233 return;
4234
4235 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4236 qual, sizeof *qual);
4237 if (status == sizeof *qual) {
4238 dev_info(&udev->dev, "not running at top speed; "
4239 "connect to a high speed hub\n");
4240 /* hub LEDs are probably harder to miss than syslog */
4241 if (hub->has_indicators) {
4242 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004243 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 }
4245 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004246 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247}
4248
4249static unsigned
4250hub_power_remaining (struct usb_hub *hub)
4251{
4252 struct usb_device *hdev = hub->hdev;
4253 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004254 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
Alan Stern55c52712005-11-23 12:03:12 -05004256 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 return 0;
4258
Alan Stern55c52712005-11-23 12:03:12 -05004259 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4260 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c72012-09-05 13:44:32 +08004261 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern55c52712005-11-23 12:03:12 -05004262 int delta;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004263 unsigned unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
4265 if (!udev)
4266 continue;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004267 if (hub_is_superspeed(udev))
4268 unit_load = 150;
4269 else
4270 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004272 /*
4273 * Unconfigured devices may not use more than one unit load,
4274 * or 8mA for OTG ports
4275 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 if (udev->actconfig)
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +01004277 delta = usb_get_max_power(udev, udev->actconfig);
Alan Stern55c52712005-11-23 12:03:12 -05004278 else if (port1 != udev->bus->otg_port || hdev->parent)
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004279 delta = unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 else
Alan Stern55c52712005-11-23 12:03:12 -05004281 delta = 8;
4282 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004283 dev_warn(&udev->dev,
4284 "%dmA is over %umA budget for port %d!\n",
4285 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 remaining -= delta;
4287 }
4288 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004289 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4290 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 remaining = 0;
4292 }
4293 return remaining;
4294}
4295
4296/* Handle physical or logical connection change events.
4297 * This routine is called when:
4298 * a port connection-change occurs;
4299 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08004300 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 * a firmware download)
4302 * caller already locked the hub
4303 */
4304static void hub_port_connect_change(struct usb_hub *hub, int port1,
4305 u16 portstatus, u16 portchange)
4306{
4307 struct usb_device *hdev = hub->hdev;
4308 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05304309 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04004310 unsigned wHubCharacteristics =
4311 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04004312 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 int status, i;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004314 unsigned unit_load;
Alan Stern24618b02008-04-28 11:06:28 -04004315
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 dev_dbg (hub_dev,
4317 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08004318 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319
4320 if (hub->has_indicators) {
4321 set_port_led(hub, port1, HUB_LED_AUTO);
4322 hub->indicator[port1-1] = INDICATOR_AUTO;
4323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
4325#ifdef CONFIG_USB_OTG
4326 /* during HNP, don't repeat the debounce */
4327 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04004328 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4329 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330#endif
4331
Alan Stern8808f002008-04-28 11:06:55 -04004332 /* Try to resuscitate an existing device */
Lan Tianyuff823c72012-09-05 13:44:32 +08004333 udev = hub->ports[port1 - 1]->child;
Alan Stern8808f002008-04-28 11:06:55 -04004334 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4335 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04004336 usb_lock_device(udev);
4337 if (portstatus & USB_PORT_STAT_ENABLE) {
4338 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004339
4340#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04004341 } else if (udev->state == USB_STATE_SUSPENDED &&
4342 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004343 /* For a suspended device, treat this as a
4344 * remote wakeup event.
4345 */
Alan Stern0534d462010-01-08 12:56:30 -05004346 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004347#endif
4348
4349 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004350 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004351 }
4352 usb_unlock_device(udev);
4353
4354 if (status == 0) {
4355 clear_bit(port1, hub->change_bits);
4356 return;
4357 }
4358 }
4359
Alan Stern24618b02008-04-28 11:06:28 -04004360 /* Disconnect any existing devices under this port */
Peter Chenb76baa82012-11-09 09:44:43 +08004361 if (udev) {
4362 if (hcd->phy && !hdev->parent &&
4363 !(portstatus & USB_PORT_STAT_CONNECTION))
Peter Chenac965112012-11-09 09:44:44 +08004364 usb_phy_notify_disconnect(hcd->phy, udev->speed);
Lan Tianyuff823c72012-09-05 13:44:32 +08004365 usb_disconnect(&hub->ports[port1 - 1]->child);
Peter Chenb76baa82012-11-09 09:44:43 +08004366 }
Alan Stern24618b02008-04-28 11:06:28 -04004367 clear_bit(port1, hub->change_bits);
4368
Alan Stern253e0572009-10-27 15:20:13 -04004369 /* We can forget about a "removed" device when there's a physical
4370 * disconnect or the connect status changes.
4371 */
4372 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4373 (portchange & USB_PORT_STAT_C_CONNECTION))
4374 clear_bit(port1, hub->removed_bits);
4375
Alan Stern5257d972008-09-22 14:43:08 -04004376 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4377 USB_PORT_STAT_C_ENABLE)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004378 status = hub_port_debounce_be_stable(hub, port1);
Alan Stern5257d972008-09-22 14:43:08 -04004379 if (status < 0) {
4380 if (printk_ratelimit())
4381 dev_err(hub_dev, "connect-debounce failed, "
4382 "port %d disabled\n", port1);
4383 portstatus &= ~USB_PORT_STAT_CONNECTION;
4384 } else {
4385 portstatus = status;
4386 }
4387 }
4388
Alan Stern253e0572009-10-27 15:20:13 -04004389 /* Return now if debouncing failed or nothing is connected or
4390 * the device was "removed".
4391 */
4392 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4393 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
4395 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07004396 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08004397 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004399
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 if (portstatus & USB_PORT_STAT_ENABLE)
4401 goto done;
4402 return;
4403 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004404 if (hub_is_superspeed(hub->hdev))
4405 unit_load = 150;
4406 else
4407 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
4411 /* reallocate for each attempt, since references
4412 * to the previous one can escape in various ways
4413 */
4414 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4415 if (!udev) {
4416 dev_err (hub_dev,
4417 "couldn't allocate port %d usb_device\n",
4418 port1);
4419 goto done;
4420 }
4421
4422 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05004423 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004424 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004425 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004426
Sarah Sharp131dec32010-12-06 21:00:19 -08004427 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4428 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004429 udev->speed = USB_SPEED_SUPER;
4430 else
4431 udev->speed = USB_SPEED_UNKNOWN;
4432
Alan Stern3b29b682011-02-22 09:53:41 -05004433 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004434 if (udev->devnum <= 0) {
4435 status = -ENOTCONN; /* Don't retry */
4436 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004437 }
4438
Sarah Sharpe7b77172009-04-27 19:54:26 -07004439 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 status = hub_port_init(hub, udev, port1, i);
4441 if (status < 0)
4442 goto loop;
4443
Phil Dibowitz93362a82010-07-22 00:05:01 +02004444 usb_detect_quirks(udev);
4445 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4446 msleep(1000);
4447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 /* consecutive bus-powered hubs aren't reliable; they can
4449 * violate the voltage drop budget. if the new child has
4450 * a "powered" LED, users should notice we didn't enable it
4451 * (without reading syslog), even without per-port LEDs
4452 * on the parent.
4453 */
4454 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004455 && udev->bus_mA <= unit_load) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 u16 devstat;
4457
4458 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4459 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05004460 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 dev_dbg(&udev->dev, "get status %d ?\n", status);
4462 goto loop_disable;
4463 }
Alan Stern55c52712005-11-23 12:03:12 -05004464 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4466 dev_err(&udev->dev,
4467 "can't connect bus-powered hub "
4468 "to this port\n");
4469 if (hub->has_indicators) {
4470 hub->indicator[port1-1] =
4471 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004472 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 }
4474 status = -ENOTCONN; /* Don't retry */
4475 goto loop_disable;
4476 }
4477 }
4478
4479 /* check for devices running slower than they could */
4480 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4481 && udev->speed == USB_SPEED_FULL
4482 && highspeed_hubs != 0)
4483 check_highspeed (hub, udev, port1);
4484
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004485 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 * udev becomes globally accessible, although presumably
4487 * no one will look at it until hdev is unlocked.
4488 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 status = 0;
4490
4491 /* We mustn't add new devices if the parent hub has
4492 * been disconnected; we would race with the
4493 * recursively_mark_NOTATTACHED() routine.
4494 */
4495 spin_lock_irq(&device_state_lock);
4496 if (hdev->state == USB_STATE_NOTATTACHED)
4497 status = -ENOTCONN;
4498 else
Lan Tianyuff823c72012-09-05 13:44:32 +08004499 hub->ports[port1 - 1]->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 spin_unlock_irq(&device_state_lock);
4501
4502 /* Run it through the hoops (find a driver, etc) */
4503 if (!status) {
4504 status = usb_new_device(udev);
4505 if (status) {
4506 spin_lock_irq(&device_state_lock);
Lan Tianyuff823c72012-09-05 13:44:32 +08004507 hub->ports[port1 - 1]->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 spin_unlock_irq(&device_state_lock);
4509 }
4510 }
4511
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 if (status)
4513 goto loop_disable;
4514
4515 status = hub_power_remaining(hub);
4516 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05004517 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
4519 return;
4520
4521loop_disable:
4522 hub_port_disable(hub, port1, 1);
4523loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004524 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004525 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004526 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004528 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 break;
4530 }
Alan Stern3a311552008-05-20 16:58:29 -04004531 if (hub->hdev->parent ||
4532 !hcd->driver->port_handed_over ||
4533 !(hcd->driver->port_handed_over)(hcd, port1))
4534 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4535 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536
4537done:
4538 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304539 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4540 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541}
4542
Sarah Sharp714b07b2012-01-24 13:53:18 -08004543/* Returns 1 if there was a remote wakeup and a connect status change. */
4544static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004545 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004546{
4547 struct usb_device *hdev;
4548 struct usb_device *udev;
4549 int connect_change = 0;
4550 int ret;
4551
4552 hdev = hub->hdev;
Lan Tianyuff823c72012-09-05 13:44:32 +08004553 udev = hub->ports[port - 1]->child;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004554 if (!hub_is_superspeed(hdev)) {
4555 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4556 return 0;
Lan Tianyuad493e52013-01-23 04:26:30 +08004557 usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004558 } else {
4559 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004560 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4561 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004562 return 0;
4563 }
4564
Sarah Sharp714b07b2012-01-24 13:53:18 -08004565 if (udev) {
4566 /* TRSMRCY = 10 msec */
4567 msleep(10);
4568
4569 usb_lock_device(udev);
4570 ret = usb_remote_wakeup(udev);
4571 usb_unlock_device(udev);
4572 if (ret < 0)
4573 connect_change = 1;
4574 } else {
4575 ret = -ENODEV;
4576 hub_port_disable(hub, port, 1);
4577 }
4578 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4579 port, ret);
4580 return connect_change;
4581}
4582
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583static void hub_events(void)
4584{
4585 struct list_head *tmp;
4586 struct usb_device *hdev;
4587 struct usb_interface *intf;
4588 struct usb_hub *hub;
4589 struct device *hub_dev;
4590 u16 hubstatus;
4591 u16 hubchange;
4592 u16 portstatus;
4593 u16 portchange;
4594 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004595 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
4597 /*
4598 * We restart the list every time to avoid a deadlock with
4599 * deleting hubs downstream from this one. This should be
4600 * safe since we delete the hub from the event list.
4601 * Not the most efficient, but avoids deadlocks.
4602 */
4603 while (1) {
4604
4605 /* Grab the first entry at the beginning of the list */
4606 spin_lock_irq(&hub_event_lock);
4607 if (list_empty(&hub_event_list)) {
4608 spin_unlock_irq(&hub_event_lock);
4609 break;
4610 }
4611
4612 tmp = hub_event_list.next;
4613 list_del_init(tmp);
4614
4615 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004616 kref_get(&hub->kref);
4617 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Alan Sterne8054852007-05-04 11:55:11 -04004619 hdev = hub->hdev;
4620 hub_dev = hub->intfdev;
4621 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004622 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 hdev->state, hub->descriptor
4624 ? hub->descriptor->bNbrPorts
4625 : 0,
4626 /* NOTE: expects max 15 ports... */
4627 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004628 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 /* Lock the device, then check to see if we were
4631 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004632 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004633 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004634 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
4636 /* If the hub has died, clean up after it */
4637 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004638 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004639 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 goto loop;
4641 }
4642
Alan Stern40f122f2006-11-09 14:44:33 -05004643 /* Autoresume */
4644 ret = usb_autopm_get_interface(intf);
4645 if (ret) {
4646 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004648 }
4649
4650 /* If this is an inactive hub, do nothing */
4651 if (hub->quiescing)
4652 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
4654 if (hub->error) {
4655 dev_dbg (hub_dev, "resetting for error %d\n",
4656 hub->error);
4657
Ming Lei742120c2008-06-18 22:00:29 +08004658 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 if (ret) {
4660 dev_dbg (hub_dev,
4661 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004662 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 }
4664
4665 hub->nerrors = 0;
4666 hub->error = 0;
4667 }
4668
4669 /* deal with port status changes */
4670 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4671 if (test_bit(i, hub->busy_bits))
4672 continue;
4673 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004674 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004676 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 continue;
4678
4679 ret = hub_port_status(hub, i,
4680 &portstatus, &portchange);
4681 if (ret < 0)
4682 continue;
4683
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004685 usb_clear_port_feature(hdev, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 USB_PORT_FEAT_C_CONNECTION);
4687 connect_change = 1;
4688 }
4689
4690 if (portchange & USB_PORT_STAT_C_ENABLE) {
4691 if (!connect_change)
4692 dev_dbg (hub_dev,
4693 "port %d enable change, "
4694 "status %08x\n",
4695 i, portstatus);
Lan Tianyuad493e52013-01-23 04:26:30 +08004696 usb_clear_port_feature(hdev, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 USB_PORT_FEAT_C_ENABLE);
4698
4699 /*
4700 * EM interference sometimes causes badly
4701 * shielded USB devices to be shutdown by
4702 * the hub, this hack enables them again.
4703 * Works at least with mouse driver.
4704 */
4705 if (!(portstatus & USB_PORT_STAT_ENABLE)
4706 && !connect_change
Lan Tianyuff823c72012-09-05 13:44:32 +08004707 && hub->ports[i - 1]->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 dev_err (hub_dev,
4709 "port %i "
4710 "disabled by hub (EMI?), "
4711 "re-enabling...\n",
4712 i);
4713 connect_change = 1;
4714 }
4715 }
4716
Sarah Sharp72937e12012-01-24 11:46:50 -08004717 if (hub_handle_remote_wakeup(hub, i,
4718 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004719 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004720
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004722 u16 status = 0;
4723 u16 unused;
4724
4725 dev_dbg(hub_dev, "over-current change on port "
4726 "%d\n", i);
Lan Tianyuad493e52013-01-23 04:26:30 +08004727 usb_clear_port_feature(hdev, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004729 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004730 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004731 hub_port_status(hub, i, &status, &unused);
4732 if (status & USB_PORT_STAT_OVERCURRENT)
4733 dev_err(hub_dev, "over-current "
4734 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 }
4736
4737 if (portchange & USB_PORT_STAT_C_RESET) {
4738 dev_dbg (hub_dev,
4739 "reset change on port %d\n",
4740 i);
Lan Tianyuad493e52013-01-23 04:26:30 +08004741 usb_clear_port_feature(hdev, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 USB_PORT_FEAT_C_RESET);
4743 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004744 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4745 hub_is_superspeed(hub->hdev)) {
4746 dev_dbg(hub_dev,
4747 "warm reset change on port %d\n",
4748 i);
Lan Tianyuad493e52013-01-23 04:26:30 +08004749 usb_clear_port_feature(hdev, i,
Sarah Sharpc7061572010-12-06 15:08:20 -08004750 USB_PORT_FEAT_C_BH_PORT_RESET);
4751 }
John Youndbe79bb2001-09-17 00:00:00 -07004752 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004753 usb_clear_port_feature(hub->hdev, i,
John Youndbe79bb2001-09-17 00:00:00 -07004754 USB_PORT_FEAT_C_PORT_LINK_STATE);
4755 }
4756 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4757 dev_warn(hub_dev,
4758 "config error on port %d\n",
4759 i);
Lan Tianyuad493e52013-01-23 04:26:30 +08004760 usb_clear_port_feature(hub->hdev, i,
John Youndbe79bb2001-09-17 00:00:00 -07004761 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
Andiry Xu5e467f62011-04-27 18:07:54 +08004764 /* Warm reset a USB3 protocol port if it's in
4765 * SS.Inactive state.
4766 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02004767 if (hub_port_warm_reset_required(hub, portstatus)) {
Sarah Sharp65bdac52012-11-14 17:58:04 -08004768 int status;
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07004769 struct usb_device *udev =
4770 hub->ports[i - 1]->child;
Sarah Sharp65bdac52012-11-14 17:58:04 -08004771
Andiry Xu5e467f62011-04-27 18:07:54 +08004772 dev_dbg(hub_dev, "warm reset port %d\n", i);
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07004773 if (!udev) {
4774 status = hub_port_reset(hub, i,
4775 NULL, HUB_BH_RESET_TIME,
4776 true);
4777 if (status < 0)
4778 hub_port_disable(hub, i, 1);
4779 } else {
4780 usb_lock_device(udev);
4781 status = usb_reset_device(udev);
4782 usb_unlock_device(udev);
4783 }
Sarah Sharp65bdac52012-11-14 17:58:04 -08004784 connect_change = 0;
Andiry Xu5e467f62011-04-27 18:07:54 +08004785 }
4786
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 if (connect_change)
4788 hub_port_connect_change(hub, i,
4789 portstatus, portchange);
4790 } /* end for i */
4791
4792 /* deal with hub status changes */
4793 if (test_and_clear_bit(0, hub->event_bits) == 0)
4794 ; /* do nothing */
4795 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4796 dev_err (hub_dev, "get_hub_status failed\n");
4797 else {
4798 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4799 dev_dbg (hub_dev, "power change\n");
4800 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004801 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4802 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004803 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004804 else
4805 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 }
4807 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004808 u16 status = 0;
4809 u16 unused;
4810
4811 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004813 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004814 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004815 hub_hub_status(hub, &status, &unused);
4816 if (status & HUB_STATUS_OVERCURRENT)
4817 dev_err(hub_dev, "over-current "
4818 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 }
4820 }
4821
Alan Stern8e4ceb32009-12-07 13:01:37 -05004822 loop_autopm:
4823 /* Balance the usb_autopm_get_interface() above */
4824 usb_autopm_put_interface_no_suspend(intf);
4825 loop:
4826 /* Balance the usb_autopm_get_interface_no_resume() in
4827 * kick_khubd() and allow autosuspend.
4828 */
4829 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004830 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004832 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
4834 } /* end while (1) */
4835}
4836
4837static int hub_thread(void *__unused)
4838{
Alan Stern3bb1af52008-03-03 15:15:36 -05004839 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4840 * port handover. Otherwise it might see that a full-speed device
4841 * was gone before the EHCI controller had handed its port over to
4842 * the companion full-speed controller.
4843 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004844 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004845
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 do {
4847 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004848 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004849 !list_empty(&hub_event_list) ||
4850 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004851 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004853 pr_debug("%s: khubd exiting\n", usbcore_name);
4854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855}
4856
Németh Márton1e927d92010-01-10 15:34:53 +01004857static const struct usb_device_id hub_id_table[] = {
Ming Leie6f30de2012-10-24 11:59:24 +08004858 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
4859 | USB_DEVICE_ID_MATCH_INT_CLASS,
4860 .idVendor = USB_VENDOR_GENESYS_LOGIC,
4861 .bInterfaceClass = USB_CLASS_HUB,
4862 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4864 .bDeviceClass = USB_CLASS_HUB},
4865 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4866 .bInterfaceClass = USB_CLASS_HUB},
4867 { } /* Terminating entry */
4868};
4869
4870MODULE_DEVICE_TABLE (usb, hub_id_table);
4871
4872static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 .name = "hub",
4874 .probe = hub_probe,
4875 .disconnect = hub_disconnect,
4876 .suspend = hub_suspend,
4877 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004878 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004879 .pre_reset = hub_pre_reset,
4880 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004881 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004883 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884};
4885
4886int usb_hub_init(void)
4887{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 if (usb_register(&hub_driver) < 0) {
4889 printk(KERN_ERR "%s: can't register hub driver\n",
4890 usbcore_name);
4891 return -1;
4892 }
4893
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004894 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4895 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
4898 /* Fall through if kernel_thread failed */
4899 usb_deregister(&hub_driver);
4900 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4901
4902 return -1;
4903}
4904
4905void usb_hub_cleanup(void)
4906{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004907 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
4909 /*
4910 * Hub resources are freed for us by usb_deregister. It calls
4911 * usb_driver_purge on every device which in turn calls that
4912 * devices disconnect function if it is using this driver.
4913 * The hub_disconnect function takes care of releasing the
4914 * individual hub resources. -greg
4915 */
4916 usb_deregister(&hub_driver);
4917} /* usb_hub_cleanup() */
4918
Alan Sterneb764c42008-03-03 15:16:04 -05004919static int descriptors_changed(struct usb_device *udev,
4920 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921{
Alan Sterneb764c42008-03-03 15:16:04 -05004922 int changed = 0;
4923 unsigned index;
4924 unsigned serial_len = 0;
4925 unsigned len;
4926 unsigned old_length;
4927 int length;
4928 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
Alan Sterneb764c42008-03-03 15:16:04 -05004930 if (memcmp(&udev->descriptor, old_device_descriptor,
4931 sizeof(*old_device_descriptor)) != 0)
4932 return 1;
4933
4934 /* Since the idVendor, idProduct, and bcdDevice values in the
4935 * device descriptor haven't changed, we will assume the
4936 * Manufacturer and Product strings haven't changed either.
4937 * But the SerialNumber string could be different (e.g., a
4938 * different flash card of the same brand).
4939 */
4940 if (udev->serial)
4941 serial_len = strlen(udev->serial) + 1;
4942
4943 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004945 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4946 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 }
Alan Sterneb764c42008-03-03 15:16:04 -05004948
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004949 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 if (buf == NULL) {
4951 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4952 /* assume the worst */
4953 return 1;
4954 }
4955 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004956 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4958 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004959 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 dev_dbg(&udev->dev, "config index %d, error %d\n",
4961 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004962 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 break;
4964 }
4965 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4966 != 0) {
4967 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004968 index,
4969 ((struct usb_config_descriptor *) buf)->
4970 bConfigurationValue);
4971 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 break;
4973 }
4974 }
Alan Sterneb764c42008-03-03 15:16:04 -05004975
4976 if (!changed && serial_len) {
4977 length = usb_string(udev, udev->descriptor.iSerialNumber,
4978 buf, serial_len);
4979 if (length + 1 != serial_len) {
4980 dev_dbg(&udev->dev, "serial string error %d\n",
4981 length);
4982 changed = 1;
4983 } else if (memcmp(buf, udev->serial, length) != 0) {
4984 dev_dbg(&udev->dev, "serial string changed\n");
4985 changed = 1;
4986 }
4987 }
4988
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004990 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991}
4992
4993/**
Ming Lei742120c2008-06-18 22:00:29 +08004994 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4996 *
Alan Stern79efa092006-06-01 13:33:42 -04004997 * WARNING - don't use this routine to reset a composite device
4998 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004999 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 *
5001 * Do a port reset, reassign the device's address, and establish its
5002 * former operating configuration. If the reset fails, or the device's
5003 * descriptors change from their values before the reset, or the original
5004 * configuration and altsettings cannot be restored, a flag will be set
5005 * telling khubd to pretend the device has been disconnected and then
5006 * re-connected. All drivers will be unbound, and the device will be
5007 * re-enumerated and probed all over again.
5008 *
5009 * Returns 0 if the reset succeeded, -ENODEV if the device has been
5010 * flagged for logical disconnection, or some other negative error code
5011 * if the reset wasn't even attempted.
5012 *
5013 * The caller must own the device lock. For example, it's safe to use
5014 * this from a driver probe() routine after downloading new firmware.
5015 * For calls that might not occur during probe(), drivers should lock
5016 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04005017 *
5018 * Locking exception: This routine may also be called from within an
5019 * autoresume handler. Such usage won't conflict with other tasks
5020 * holding the device lock because these tasks should always call
5021 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 */
Ming Lei742120c2008-06-18 22:00:29 +08005023static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024{
5025 struct usb_device *parent_hdev = udev->parent;
5026 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005027 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05005029 int i, ret = 0;
5030 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
5032 if (udev->state == USB_STATE_NOTATTACHED ||
5033 udev->state == USB_STATE_SUSPENDED) {
5034 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5035 udev->state);
5036 return -EINVAL;
5037 }
5038
5039 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02005040 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08005041 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 return -EISDIR;
5043 }
Lan Tianyuad493e52013-01-23 04:26:30 +08005044 parent_hub = usb_hub_to_struct_hub(parent_hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
Sarah Sharpf74631e2012-06-25 12:08:08 -07005046 /* Disable LPM and LTM while we reset the device and reinstall the alt
5047 * settings. Device-initiated LPM settings, and system exit latency
5048 * settings are cleared when the device is reset, so we have to set
5049 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005050 */
5051 ret = usb_unlocked_disable_lpm(udev);
5052 if (ret) {
5053 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
5054 goto re_enumerate;
5055 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07005056 ret = usb_disable_ltm(udev);
5057 if (ret) {
5058 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
5059 __func__);
5060 goto re_enumerate;
5061 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005062
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 set_bit(port1, parent_hub->busy_bits);
5064 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
5065
5066 /* ep0 maxpacket size may change; let the HCD know about it.
5067 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07005068 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04005070 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 break;
5072 }
5073 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04005074
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 if (ret < 0)
5076 goto re_enumerate;
5077
5078 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05005079 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 dev_info(&udev->dev, "device firmware changed\n");
5081 udev->descriptor = descriptor; /* for disconnect() calls */
5082 goto re_enumerate;
5083 }
Alan Stern4fe03872009-02-26 10:21:02 -05005084
5085 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 if (!udev->actconfig)
5087 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005088
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005089 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005090 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
5091 if (ret < 0) {
5092 dev_warn(&udev->dev,
5093 "Busted HC? Not enough HCD resources for "
5094 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005095 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005096 goto re_enumerate;
5097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
5099 USB_REQ_SET_CONFIGURATION, 0,
5100 udev->actconfig->desc.bConfigurationValue, 0,
5101 NULL, 0, USB_CTRL_SET_TIMEOUT);
5102 if (ret < 0) {
5103 dev_err(&udev->dev,
5104 "can't restore configuration #%d (error=%d)\n",
5105 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005106 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 goto re_enumerate;
5108 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005109 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 usb_set_device_state(udev, USB_STATE_CONFIGURED);
5111
Alan Stern4fe03872009-02-26 10:21:02 -05005112 /* Put interfaces back into the same altsettings as before.
5113 * Don't bother to send the Set-Interface request for interfaces
5114 * that were already in altsetting 0; besides being unnecessary,
5115 * many devices can't handle it. Instead just reset the host-side
5116 * endpoint state.
5117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005119 struct usb_host_config *config = udev->actconfig;
5120 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 struct usb_interface_descriptor *desc;
5122
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05005124 if (desc->bAlternateSetting == 0) {
5125 usb_disable_interface(udev, intf, true);
5126 usb_enable_interface(udev, intf, true);
5127 ret = 0;
5128 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08005129 /* Let the bandwidth allocation function know that this
5130 * device has been reset, and it will have to use
5131 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005132 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08005133 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05005134 ret = usb_set_interface(udev, desc->bInterfaceNumber,
5135 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08005136 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05005137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 if (ret < 0) {
5139 dev_err(&udev->dev, "failed to restore interface %d "
5140 "altsetting %d (error=%d)\n",
5141 desc->bInterfaceNumber,
5142 desc->bAlternateSetting,
5143 ret);
5144 goto re_enumerate;
5145 }
5146 }
5147
5148done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07005149 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Alan Stern78d9a482008-06-23 16:00:40 -04005150 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07005151 usb_enable_ltm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 return 0;
5153
5154re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005155 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 hub_port_logical_disconnect(parent_hub, port1);
5157 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158}
5159
5160/**
5161 * usb_reset_device - warn interface drivers and perform a USB port reset
5162 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5163 *
Alan Stern79efa092006-06-01 13:33:42 -04005164 * Warns all drivers bound to registered interfaces (using their pre_reset
5165 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08005166 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04005167 *
Alan Stern79efa092006-06-01 13:33:42 -04005168 * Return value is the same as for usb_reset_and_verify_device().
5169 *
5170 * The caller must own the device lock. For example, it's safe to use
5171 * this from a driver probe() routine after downloading new firmware.
5172 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08005173 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04005174 *
5175 * If an interface is currently being probed or disconnected, we assume
5176 * its driver knows how to handle resets. For all other interfaces,
5177 * if the driver doesn't have pre_reset and post_reset methods then
5178 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04005179 */
Ming Lei742120c2008-06-18 22:00:29 +08005180int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04005181{
5182 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05005183 int i;
Ming Lei4d769de2013-02-22 16:34:22 -08005184 unsigned int noio_flag;
Alan Stern79efa092006-06-01 13:33:42 -04005185 struct usb_host_config *config = udev->actconfig;
5186
5187 if (udev->state == USB_STATE_NOTATTACHED ||
5188 udev->state == USB_STATE_SUSPENDED) {
5189 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5190 udev->state);
5191 return -EINVAL;
5192 }
5193
Ming Lei4d769de2013-02-22 16:34:22 -08005194 /*
5195 * Don't allocate memory with GFP_KERNEL in current
5196 * context to avoid possible deadlock if usb mass
5197 * storage interface or usbnet interface(iSCSI case)
5198 * is included in current configuration. The easist
5199 * approach is to do it for every device reset,
5200 * because the device 'memalloc_noio' flag may have
5201 * not been set before reseting the usb device.
5202 */
5203 noio_flag = memalloc_noio_save();
5204
Alan Stern645daaa2006-08-30 15:47:02 -04005205 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05005206 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04005207
Alan Stern79efa092006-06-01 13:33:42 -04005208 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005209 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005210 struct usb_interface *cintf = config->interface[i];
5211 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005212 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05005213
5214 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005215 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04005216 if (drv->pre_reset && drv->post_reset)
5217 unbind = (drv->pre_reset)(cintf);
5218 else if (cintf->condition ==
5219 USB_INTERFACE_BOUND)
5220 unbind = 1;
5221 if (unbind)
5222 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005223 }
5224 }
5225 }
5226
Ming Lei742120c2008-06-18 22:00:29 +08005227 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005228
5229 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005230 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005231 struct usb_interface *cintf = config->interface[i];
5232 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005233 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05005234
Alan Stern78d9a482008-06-23 16:00:40 -04005235 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005236 drv = to_usb_driver(cintf->dev.driver);
5237 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04005238 rebind = (drv->post_reset)(cintf);
5239 else if (cintf->condition ==
5240 USB_INTERFACE_BOUND)
5241 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005242 }
Alan Stern6c640942008-10-21 15:40:03 -04005243 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04005244 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005245 }
5246 }
5247
Alan Stern94fcda12006-11-20 11:38:46 -05005248 usb_autosuspend_device(udev);
Ming Lei4d769de2013-02-22 16:34:22 -08005249 memalloc_noio_restore(noio_flag);
Alan Stern79efa092006-06-01 13:33:42 -04005250 return ret;
5251}
Ming Lei742120c2008-06-18 22:00:29 +08005252EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005253
5254
5255/**
5256 * usb_queue_reset_device - Reset a USB device from an atomic context
5257 * @iface: USB interface belonging to the device to reset
5258 *
5259 * This function can be used to reset a USB device from an atomic
5260 * context, where usb_reset_device() won't work (as it blocks).
5261 *
5262 * Doing a reset via this method is functionally equivalent to calling
5263 * usb_reset_device(), except for the fact that it is delayed to a
5264 * workqueue. This means that any drivers bound to other interfaces
5265 * might be unbound, as well as users from usbfs in user space.
5266 *
5267 * Corner cases:
5268 *
5269 * - Scheduling two resets at the same time from two different drivers
5270 * attached to two different interfaces of the same device is
5271 * possible; depending on how the driver attached to each interface
5272 * handles ->pre_reset(), the second reset might happen or not.
5273 *
5274 * - If a driver is unbound and it had a pending reset, the reset will
5275 * be cancelled.
5276 *
5277 * - This function can be called during .probe() or .disconnect()
5278 * times. On return from .disconnect(), any pending resets will be
5279 * cancelled.
5280 *
5281 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5282 * does its own.
5283 *
5284 * NOTE: We don't do any reference count tracking because it is not
5285 * needed. The lifecycle of the work_struct is tied to the
5286 * usb_interface. Before destroying the interface we cancel the
5287 * work_struct, so the fact that work_struct is queued and or
5288 * running means the interface (and thus, the device) exist and
5289 * are referenced.
5290 */
5291void usb_queue_reset_device(struct usb_interface *iface)
5292{
5293 schedule_work(&iface->reset_ws);
5294}
5295EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c72012-09-05 13:44:32 +08005296
5297/**
5298 * usb_hub_find_child - Get the pointer of child device
5299 * attached to the port which is specified by @port1.
5300 * @hdev: USB device belonging to the usb hub
5301 * @port1: port num to indicate which port the child device
5302 * is attached to.
5303 *
5304 * USB drivers call this function to get hub's child device
5305 * pointer.
5306 *
5307 * Return NULL if input param is invalid and
5308 * child's usb_device pointer if non-NULL.
5309 */
5310struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5311 int port1)
5312{
Lan Tianyuad493e52013-01-23 04:26:30 +08005313 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyuff823c72012-09-05 13:44:32 +08005314
5315 if (port1 < 1 || port1 > hdev->maxchild)
5316 return NULL;
5317 return hub->ports[port1 - 1]->child;
5318}
5319EXPORT_SYMBOL_GPL(usb_hub_find_child);
Lan Tianyud5575422012-09-05 13:44:33 +08005320
Lan Tianyu05f91682012-09-05 13:44:34 +08005321/**
5322 * usb_set_hub_port_connect_type - set hub port connect type.
5323 * @hdev: USB device belonging to the usb hub
5324 * @port1: port num of the port
5325 * @type: connect type of the port
5326 */
5327void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
5328 enum usb_port_connect_type type)
5329{
Lan Tianyuad493e52013-01-23 04:26:30 +08005330 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyu05f91682012-09-05 13:44:34 +08005331
5332 hub->ports[port1 - 1]->connect_type = type;
5333}
5334
5335/**
5336 * usb_get_hub_port_connect_type - Get the port's connect type
5337 * @hdev: USB device belonging to the usb hub
5338 * @port1: port num of the port
5339 *
5340 * Return connect type of the port and if input params are
5341 * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
5342 */
5343enum usb_port_connect_type
5344usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
5345{
Lan Tianyuad493e52013-01-23 04:26:30 +08005346 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyu05f91682012-09-05 13:44:34 +08005347
5348 return hub->ports[port1 - 1]->connect_type;
5349}
5350
Lan Tianyud2123fd2013-01-21 22:18:00 +08005351void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
5352 struct usb_hub_descriptor *desc)
5353{
5354 enum usb_port_connect_type connect_type;
5355 int i;
5356
5357 if (!hub_is_superspeed(hdev)) {
5358 for (i = 1; i <= hdev->maxchild; i++) {
5359 connect_type = usb_get_hub_port_connect_type(hdev, i);
5360
5361 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5362 u8 mask = 1 << (i%8);
5363
5364 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) {
5365 dev_dbg(&hdev->dev, "usb port%d's DeviceRemovable is changed to 1 according to platform information.\n",
5366 i);
5367 desc->u.hs.DeviceRemovable[i/8] |= mask;
5368 }
5369 }
5370 }
5371 } else {
5372 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable);
5373
5374 for (i = 1; i <= hdev->maxchild; i++) {
5375 connect_type = usb_get_hub_port_connect_type(hdev, i);
5376
5377 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5378 u16 mask = 1 << i;
5379
5380 if (!(port_removable & mask)) {
5381 dev_dbg(&hdev->dev, "usb port%d's DeviceRemovable is changed to 1 according to platform information.\n",
5382 i);
5383 port_removable |= mask;
5384 }
5385 }
5386 }
5387
5388 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
5389 }
5390}
5391
Lan Tianyud5575422012-09-05 13:44:33 +08005392#ifdef CONFIG_ACPI
5393/**
5394 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5395 * @hdev: USB device belonging to the usb hub
5396 * @port1: port num of the port
5397 *
5398 * Return port's acpi handle if successful, NULL if params are
5399 * invaild.
5400 */
5401acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5402 int port1)
5403{
Lan Tianyuad493e52013-01-23 04:26:30 +08005404 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyud5575422012-09-05 13:44:33 +08005405
5406 return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
5407}
5408#endif