blob: cfdd4eecc5a91827eef449c9ff62d3e4006e5d41 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
31#include <asm/byteorder.h>
32
Lan Tianyu6e30d7c2013-01-11 20:10:38 +080033#include "hub.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -080035/* if we are in debug mode, always announce new devices */
36#ifdef DEBUG
37#ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
38#define CONFIG_USB_ANNOUNCE_NEW_DEVICES
39#endif
40#endif
41
Ming Leie6f30de2012-10-24 11:59:24 +080042#define USB_VENDOR_GENESYS_LOGIC 0x05e3
43#define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01
44
John Youndbe79bb2001-09-17 00:00:00 -070045static inline int hub_is_superspeed(struct usb_device *hdev)
46{
Aman Deep7bf01182011-12-08 12:05:22 +053047 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
John Youndbe79bb2001-09-17 00:00:00 -070048}
Alan Stern1bb5f662006-11-06 11:56:13 -050049
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -070050/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -050051 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
53static DEFINE_SPINLOCK(device_state_lock);
54
55/* khubd's worklist and its lock */
56static DEFINE_SPINLOCK(hub_event_lock);
57static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
58
59/* Wakes up khubd */
60static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
61
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070062static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/* cycle leds on hubs that aren't blinking for attention */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103065static bool blinkenlights = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066module_param (blinkenlights, bool, S_IRUGO);
67MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
68
69/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020070 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
71 * 10 seconds to send reply for the initial 64-byte descriptor request.
72 */
73/* define initial 64-byte descriptor request timeout in milliseconds */
74static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
75module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +080076MODULE_PARM_DESC(initial_descriptor_timeout,
77 "initial 64-byte descriptor request timeout in milliseconds "
78 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020079
80/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * As of 2.6.10 we introduce a new USB device initialization scheme which
82 * closely resembles the way Windows works. Hopefully it will be compatible
83 * with a wider range of devices than the old scheme. However some previously
84 * working devices may start giving rise to "device not accepting address"
85 * errors; if that happens the user can try the old scheme by adjusting the
86 * following module parameters.
87 *
88 * For maximum flexibility there are two boolean parameters to control the
89 * hub driver's behavior. On the first initialization attempt, if the
90 * "old_scheme_first" parameter is set then the old scheme will be used,
91 * otherwise the new scheme is used. If that fails and "use_both_schemes"
92 * is set, then the driver will make another attempt, using the other scheme.
93 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103094static bool old_scheme_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
96MODULE_PARM_DESC(old_scheme_first,
97 "start with the old device initialization scheme");
98
Rusty Russell90ab5ee2012-01-13 09:32:20 +103099static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
101MODULE_PARM_DESC(use_both_schemes,
102 "try the other device initialization scheme if the "
103 "first one fails");
104
Alan Stern32fe0192007-10-10 16:27:07 -0400105/* Mutual exclusion for EHCI CF initialization. This interferes with
106 * port reset on some companion controllers.
107 */
108DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
109EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
110
Alan Stern948fea32008-04-28 11:07:07 -0400111#define HUB_DEBOUNCE_TIMEOUT 1500
112#define HUB_DEBOUNCE_STEP 25
113#define HUB_DEBOUNCE_STABLE 100
114
Ming Lei742120c2008-06-18 22:00:29 +0800115static int usb_reset_and_verify_device(struct usb_device *udev);
116
Sarah Sharp131dec32010-12-06 21:00:19 -0800117static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Sarah Sharp131dec32010-12-06 21:00:19 -0800119 if (hub_is_superspeed(hub->hdev))
120 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500121 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500123 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return "1.5 Mb/s";
125 else
126 return "12 Mb/s";
127}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/* Note that hdev or one of its children must be locked! */
Alan Stern251180842009-07-22 14:41:18 -0400130static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Lan Tianyuff823c792012-09-05 13:44:32 +0800132 if (!hdev || !hdev->actconfig || !hdev->maxchild)
Alan Stern251180842009-07-22 14:41:18 -0400133 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return usb_get_intfdata(hdev->actconfig->interface[0]);
135}
136
Sarah Sharpd9b20992012-02-20 08:31:26 -0800137static int usb_device_supports_lpm(struct usb_device *udev)
138{
139 /* USB 2.1 (and greater) devices indicate LPM support through
140 * their USB 2.0 Extended Capabilities BOS descriptor.
141 */
142 if (udev->speed == USB_SPEED_HIGH) {
143 if (udev->bos->ext_cap &&
144 (USB_LPM_SUPPORT &
145 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
146 return 1;
147 return 0;
148 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800149
150 /* All USB 3.0 must support LPM, but we need their max exit latency
151 * information from the SuperSpeed Extended Capabilities BOS descriptor.
152 */
153 if (!udev->bos->ss_cap) {
154 dev_warn(&udev->dev, "No LPM exit latency info found. "
155 "Power management will be impacted.\n");
156 return 0;
157 }
158 if (udev->parent->lpm_capable)
159 return 1;
160
161 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. "
162 "Power management will be impacted.\n");
Sarah Sharpd9b20992012-02-20 08:31:26 -0800163 return 0;
164}
165
Sarah Sharp51e0a012012-02-20 12:02:19 -0800166/*
167 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
168 * either U1 or U2.
169 */
170static void usb_set_lpm_mel(struct usb_device *udev,
171 struct usb3_lpm_parameters *udev_lpm_params,
172 unsigned int udev_exit_latency,
173 struct usb_hub *hub,
174 struct usb3_lpm_parameters *hub_lpm_params,
175 unsigned int hub_exit_latency)
176{
177 unsigned int total_mel;
178 unsigned int device_mel;
179 unsigned int hub_mel;
180
181 /*
182 * Calculate the time it takes to transition all links from the roothub
183 * to the parent hub into U0. The parent hub must then decode the
184 * packet (hub header decode latency) to figure out which port it was
185 * bound for.
186 *
187 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
188 * means 0.1us). Multiply that by 100 to get nanoseconds.
189 */
190 total_mel = hub_lpm_params->mel +
191 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
192
193 /*
194 * How long will it take to transition the downstream hub's port into
195 * U0? The greater of either the hub exit latency or the device exit
196 * latency.
197 *
198 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
199 * Multiply that by 1000 to get nanoseconds.
200 */
201 device_mel = udev_exit_latency * 1000;
202 hub_mel = hub_exit_latency * 1000;
203 if (device_mel > hub_mel)
204 total_mel += device_mel;
205 else
206 total_mel += hub_mel;
207
208 udev_lpm_params->mel = total_mel;
209}
210
211/*
212 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
213 * a transition from either U1 or U2.
214 */
215static void usb_set_lpm_pel(struct usb_device *udev,
216 struct usb3_lpm_parameters *udev_lpm_params,
217 unsigned int udev_exit_latency,
218 struct usb_hub *hub,
219 struct usb3_lpm_parameters *hub_lpm_params,
220 unsigned int hub_exit_latency,
221 unsigned int port_to_port_exit_latency)
222{
223 unsigned int first_link_pel;
224 unsigned int hub_pel;
225
226 /*
227 * First, the device sends an LFPS to transition the link between the
228 * device and the parent hub into U0. The exit latency is the bigger of
229 * the device exit latency or the hub exit latency.
230 */
231 if (udev_exit_latency > hub_exit_latency)
232 first_link_pel = udev_exit_latency * 1000;
233 else
234 first_link_pel = hub_exit_latency * 1000;
235
236 /*
237 * When the hub starts to receive the LFPS, there is a slight delay for
238 * it to figure out that one of the ports is sending an LFPS. Then it
239 * will forward the LFPS to its upstream link. The exit latency is the
240 * delay, plus the PEL that we calculated for this hub.
241 */
242 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
243
244 /*
245 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
246 * is the greater of the two exit latencies.
247 */
248 if (first_link_pel > hub_pel)
249 udev_lpm_params->pel = first_link_pel;
250 else
251 udev_lpm_params->pel = hub_pel;
252}
253
254/*
255 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
256 * when a device initiates a transition to U0, until when it will receive the
257 * first packet from the host controller.
258 *
259 * Section C.1.5.1 describes the four components to this:
260 * - t1: device PEL
261 * - t2: time for the ERDY to make it from the device to the host.
262 * - t3: a host-specific delay to process the ERDY.
263 * - t4: time for the packet to make it from the host to the device.
264 *
265 * t3 is specific to both the xHCI host and the platform the host is integrated
266 * into. The Intel HW folks have said it's negligible, FIXME if a different
267 * vendor says otherwise.
268 */
269static void usb_set_lpm_sel(struct usb_device *udev,
270 struct usb3_lpm_parameters *udev_lpm_params)
271{
272 struct usb_device *parent;
273 unsigned int num_hubs;
274 unsigned int total_sel;
275
276 /* t1 = device PEL */
277 total_sel = udev_lpm_params->pel;
278 /* How many external hubs are in between the device & the root port. */
279 for (parent = udev->parent, num_hubs = 0; parent->parent;
280 parent = parent->parent)
281 num_hubs++;
282 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
283 if (num_hubs > 0)
284 total_sel += 2100 + 250 * (num_hubs - 1);
285
286 /* t4 = 250ns * num_hubs */
287 total_sel += 250 * num_hubs;
288
289 udev_lpm_params->sel = total_sel;
290}
291
292static void usb_set_lpm_parameters(struct usb_device *udev)
293{
294 struct usb_hub *hub;
295 unsigned int port_to_port_delay;
296 unsigned int udev_u1_del;
297 unsigned int udev_u2_del;
298 unsigned int hub_u1_del;
299 unsigned int hub_u2_del;
300
301 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
302 return;
303
304 hub = hdev_to_hub(udev->parent);
305 /* It doesn't take time to transition the roothub into U0, since it
306 * doesn't have an upstream link.
307 */
308 if (!hub)
309 return;
310
311 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
312 udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
313 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
314 hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
315
316 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
317 hub, &udev->parent->u1_params, hub_u1_del);
318
319 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
320 hub, &udev->parent->u2_params, hub_u2_del);
321
322 /*
323 * Appendix C, section C.2.2.2, says that there is a slight delay from
324 * when the parent hub notices the downstream port is trying to
325 * transition to U0 to when the hub initiates a U0 transition on its
326 * upstream port. The section says the delays are tPort2PortU1EL and
327 * tPort2PortU2EL, but it doesn't define what they are.
328 *
329 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
330 * about the same delays. Use the maximum delay calculations from those
331 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
332 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
333 * assume the device exit latencies they are talking about are the hub
334 * exit latencies.
335 *
336 * What do we do if the U2 exit latency is less than the U1 exit
337 * latency? It's possible, although not likely...
338 */
339 port_to_port_delay = 1;
340
341 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
342 hub, &udev->parent->u1_params, hub_u1_del,
343 port_to_port_delay);
344
345 if (hub_u2_del > hub_u1_del)
346 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
347 else
348 port_to_port_delay = 1 + hub_u1_del;
349
350 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
351 hub, &udev->parent->u2_params, hub_u2_del,
352 port_to_port_delay);
353
354 /* Now that we've got PEL, calculate SEL. */
355 usb_set_lpm_sel(udev, &udev->u1_params);
356 usb_set_lpm_sel(udev, &udev->u2_params);
357}
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359/* USB 2.0 spec Section 11.24.4.5 */
John Youndbe79bb2001-09-17 00:00:00 -0700360static int get_hub_descriptor(struct usb_device *hdev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
John Youndbe79bb2001-09-17 00:00:00 -0700362 int i, ret, size;
363 unsigned dtype;
364
365 if (hub_is_superspeed(hdev)) {
366 dtype = USB_DT_SS_HUB;
367 size = USB_DT_SS_HUB_SIZE;
368 } else {
369 dtype = USB_DT_HUB;
370 size = sizeof(struct usb_hub_descriptor);
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 for (i = 0; i < 3; i++) {
374 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
375 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
John Youndbe79bb2001-09-17 00:00:00 -0700376 dtype << 8, 0, data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 USB_CTRL_GET_TIMEOUT);
378 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
379 return ret;
380 }
381 return -EINVAL;
382}
383
384/*
385 * USB 2.0 spec Section 11.24.2.1
386 */
387static int clear_hub_feature(struct usb_device *hdev, int feature)
388{
389 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
390 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
391}
392
393/*
394 * USB 2.0 spec Section 11.24.2.2
395 */
396static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
397{
398 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
399 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
400 NULL, 0, 1000);
401}
402
403/*
404 * USB 2.0 spec Section 11.24.2.13
405 */
406static int set_port_feature(struct usb_device *hdev, int port1, int feature)
407{
408 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
409 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
410 NULL, 0, 1000);
411}
412
413/*
414 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
415 * for info about using port indicators
416 */
417static void set_port_led(
418 struct usb_hub *hub,
419 int port1,
420 int selector
421)
422{
423 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
424 USB_PORT_FEAT_INDICATOR);
425 if (status < 0)
426 dev_dbg (hub->intfdev,
427 "port %d indicator %s status %d\n",
428 port1,
429 ({ char *s; switch (selector) {
430 case HUB_LED_AMBER: s = "amber"; break;
431 case HUB_LED_GREEN: s = "green"; break;
432 case HUB_LED_OFF: s = "off"; break;
433 case HUB_LED_AUTO: s = "auto"; break;
434 default: s = "??"; break;
435 }; s; }),
436 status);
437}
438
439#define LED_CYCLE_PERIOD ((2*HZ)/3)
440
David Howellsc4028952006-11-22 14:57:56 +0000441static void led_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
David Howellsc4028952006-11-22 14:57:56 +0000443 struct usb_hub *hub =
444 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct usb_device *hdev = hub->hdev;
446 unsigned i;
447 unsigned changed = 0;
448 int cursor = -1;
449
450 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
451 return;
452
453 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
454 unsigned selector, mode;
455
456 /* 30%-50% duty cycle */
457
458 switch (hub->indicator[i]) {
459 /* cycle marker */
460 case INDICATOR_CYCLE:
461 cursor = i;
462 selector = HUB_LED_AUTO;
463 mode = INDICATOR_AUTO;
464 break;
465 /* blinking green = sw attention */
466 case INDICATOR_GREEN_BLINK:
467 selector = HUB_LED_GREEN;
468 mode = INDICATOR_GREEN_BLINK_OFF;
469 break;
470 case INDICATOR_GREEN_BLINK_OFF:
471 selector = HUB_LED_OFF;
472 mode = INDICATOR_GREEN_BLINK;
473 break;
474 /* blinking amber = hw attention */
475 case INDICATOR_AMBER_BLINK:
476 selector = HUB_LED_AMBER;
477 mode = INDICATOR_AMBER_BLINK_OFF;
478 break;
479 case INDICATOR_AMBER_BLINK_OFF:
480 selector = HUB_LED_OFF;
481 mode = INDICATOR_AMBER_BLINK;
482 break;
483 /* blink green/amber = reserved */
484 case INDICATOR_ALT_BLINK:
485 selector = HUB_LED_GREEN;
486 mode = INDICATOR_ALT_BLINK_OFF;
487 break;
488 case INDICATOR_ALT_BLINK_OFF:
489 selector = HUB_LED_AMBER;
490 mode = INDICATOR_ALT_BLINK;
491 break;
492 default:
493 continue;
494 }
495 if (selector != HUB_LED_AUTO)
496 changed = 1;
497 set_port_led(hub, i + 1, selector);
498 hub->indicator[i] = mode;
499 }
500 if (!changed && blinkenlights) {
501 cursor++;
502 cursor %= hub->descriptor->bNbrPorts;
503 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
504 hub->indicator[cursor] = INDICATOR_CYCLE;
505 changed++;
506 }
507 if (changed)
508 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
509}
510
511/* use a short timeout for hub/port status fetches */
512#define USB_STS_TIMEOUT 1000
513#define USB_STS_RETRIES 5
514
515/*
516 * USB 2.0 spec Section 11.24.2.6
517 */
518static int get_hub_status(struct usb_device *hdev,
519 struct usb_hub_status *data)
520{
521 int i, status = -ETIMEDOUT;
522
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200523 for (i = 0; i < USB_STS_RETRIES &&
524 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
526 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
527 data, sizeof(*data), USB_STS_TIMEOUT);
528 }
529 return status;
530}
531
532/*
533 * USB 2.0 spec Section 11.24.2.7
534 */
535static int get_port_status(struct usb_device *hdev, int port1,
536 struct usb_port_status *data)
537{
538 int i, status = -ETIMEDOUT;
539
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200540 for (i = 0; i < USB_STS_RETRIES &&
541 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
543 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
544 data, sizeof(*data), USB_STS_TIMEOUT);
545 }
546 return status;
547}
548
Alan Stern3eb14912008-03-03 15:15:43 -0500549static int hub_port_status(struct usb_hub *hub, int port1,
550 u16 *status, u16 *change)
551{
552 int ret;
553
554 mutex_lock(&hub->status_mutex);
555 ret = get_port_status(hub->hdev, port1, &hub->status->port);
556 if (ret < 4) {
557 dev_err(hub->intfdev,
558 "%s failed (err = %d)\n", __func__, ret);
559 if (ret >= 0)
560 ret = -EIO;
561 } else {
562 *status = le16_to_cpu(hub->status->port.wPortStatus);
563 *change = le16_to_cpu(hub->status->port.wPortChange);
John Youndbe79bb2001-09-17 00:00:00 -0700564
Alan Stern3eb14912008-03-03 15:15:43 -0500565 ret = 0;
566 }
567 mutex_unlock(&hub->status_mutex);
568 return ret;
569}
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571static void kick_khubd(struct usb_hub *hub)
572{
573 unsigned long flags;
574
575 spin_lock_irqsave(&hub_event_lock, flags);
Roel Kluin2e2c5ee2007-10-26 23:54:35 +0200576 if (!hub->disconnected && list_empty(&hub->event_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 list_add_tail(&hub->event_list, &hub_event_list);
Alan Stern8e4ceb32009-12-07 13:01:37 -0500578
579 /* Suppress autosuspend until khubd runs */
580 usb_autopm_get_interface_no_resume(
581 to_usb_interface(hub->intfdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 wake_up(&khubd_wait);
583 }
584 spin_unlock_irqrestore(&hub_event_lock, flags);
585}
586
587void usb_kick_khubd(struct usb_device *hdev)
588{
Alan Stern251180842009-07-22 14:41:18 -0400589 struct usb_hub *hub = hdev_to_hub(hdev);
590
591 if (hub)
592 kick_khubd(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800595/*
596 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
597 * Notification, which indicates it had initiated remote wakeup.
598 *
599 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
600 * device initiates resume, so the USB core will not receive notice of the
601 * resume through the normal hub interrupt URB.
602 */
603void usb_wakeup_notification(struct usb_device *hdev,
604 unsigned int portnum)
605{
606 struct usb_hub *hub;
607
608 if (!hdev)
609 return;
610
611 hub = hdev_to_hub(hdev);
612 if (hub) {
613 set_bit(portnum, hub->wakeup_bits);
614 kick_khubd(hub);
615 }
616}
617EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100620static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200622 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400623 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100624 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 unsigned long bits;
626
Alan Sterne0152682007-08-24 15:42:52 -0400627 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 case -ENOENT: /* synchronous unlink */
629 case -ECONNRESET: /* async unlink */
630 case -ESHUTDOWN: /* hardware going away */
631 return;
632
633 default: /* presumably an error */
634 /* Cause a hub reset after 10 consecutive errors */
Alan Sterne0152682007-08-24 15:42:52 -0400635 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if ((++hub->nerrors < 10) || hub->error)
637 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400638 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /* let khubd handle things */
642 case 0: /* we got data: port status changed */
643 bits = 0;
644 for (i = 0; i < urb->actual_length; ++i)
645 bits |= ((unsigned long) ((*hub->buffer)[i]))
646 << (i*8);
647 hub->event_bits[0] = bits;
648 break;
649 }
650
651 hub->nerrors = 0;
652
653 /* Something happened, let khubd figure it out */
654 kick_khubd(hub);
655
656resubmit:
657 if (hub->quiescing)
658 return;
659
660 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
661 && status != -ENODEV && status != -EPERM)
662 dev_err (hub->intfdev, "resubmit --> %d\n", status);
663}
664
665/* USB 2.0 spec Section 11.24.2.3 */
666static inline int
667hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
668{
Alan Sternc2f65952009-11-18 11:37:15 -0500669 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
671 tt, NULL, 0, 1000);
672}
673
674/*
675 * enumeration blocks khubd for a long time. we use keventd instead, since
676 * long blocking there is the exception, not the rule. accordingly, HCDs
677 * talking to TTs must queue control transfers (not just bulk and iso), so
678 * both can talk to the same hub concurrently.
679 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400680static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
David Howellsc4028952006-11-22 14:57:56 +0000682 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400683 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 unsigned long flags;
685
686 spin_lock_irqsave (&hub->tt.lock, flags);
Octavian Purdila3b6054d2012-10-01 22:21:12 +0300687 while (!list_empty(&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400688 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct usb_tt_clear *clear;
690 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400691 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 int status;
693
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400694 next = hub->tt.clear_list.next;
695 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 list_del (&clear->clear_list);
697
698 /* drop lock so HCD can concurrently report other TT errors */
699 spin_unlock_irqrestore (&hub->tt.lock, flags);
700 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (status)
702 dev_err (&hdev->dev,
703 "clear tt %d (%04x) error %d\n",
704 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400705
706 /* Tell the HCD, even if the operation failed */
707 drv = clear->hcd->driver;
708 if (drv->clear_tt_buffer_complete)
709 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
710
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700711 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400712 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714 spin_unlock_irqrestore (&hub->tt.lock, flags);
715}
716
717/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400718 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
719 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 *
721 * High speed HCDs use this to tell the hub driver that some split control or
722 * bulk transaction failed in a way that requires clearing internal state of
723 * a transaction translator. This is normally detected (and reported) from
724 * interrupt context.
725 *
726 * It may not be possible for that hub to handle additional full (or low)
727 * speed transactions until that state is fully cleared out.
728 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400729int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400731 struct usb_device *udev = urb->dev;
732 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct usb_tt *tt = udev->tt;
734 unsigned long flags;
735 struct usb_tt_clear *clear;
736
737 /* we've got to cope with an arbitrary number of pending TT clears,
738 * since each TT has "at least two" buffers that can need it (and
739 * there can be many TTs per hub). even if they're uncommon.
740 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800741 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
743 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400744 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
747 /* info that CLEAR_TT_BUFFER needs */
748 clear->tt = tt->multi ? udev->ttport : 1;
749 clear->devinfo = usb_pipeendpoint (pipe);
750 clear->devinfo |= udev->devnum << 4;
751 clear->devinfo |= usb_pipecontrol (pipe)
752 ? (USB_ENDPOINT_XFER_CONTROL << 11)
753 : (USB_ENDPOINT_XFER_BULK << 11);
754 if (usb_pipein (pipe))
755 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400756
757 /* info for completion callback */
758 clear->hcd = bus_to_hcd(udev->bus);
759 clear->ep = urb->ep;
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* tell keventd to clear state for this TT */
762 spin_lock_irqsave (&tt->lock, flags);
763 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400764 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400768EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Alan Stern8520f382008-09-22 14:44:26 -0400770/* If do_delay is false, return the number of milliseconds the caller
771 * needs to delay.
772 */
773static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700776 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern8520f382008-09-22 14:44:26 -0400777 unsigned delay;
Alan Stern4489a572006-04-27 15:54:22 -0400778 u16 wHubCharacteristics =
779 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Alan Stern4489a572006-04-27 15:54:22 -0400781 /* Enable power on each port. Some hubs have reserved values
782 * of LPSM (> 2) in their descriptors, even though they are
783 * USB 2.0 hubs. Some hubs do not implement port-power switching
784 * but only emulate it. In all cases, the ports won't work
785 * unless we send these messages to the hub.
786 */
787 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400789 else
790 dev_dbg(hub->intfdev, "trying to enable port power on "
791 "non-switchable hub\n");
792 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
Greg Kroah-Hartmana0693bd2012-09-24 13:04:02 -0700793 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
David Brownellb7896962005-08-31 10:41:44 -0700795 /* Wait at least 100 msec for power to become stable */
Alan Stern8520f382008-09-22 14:44:26 -0400796 delay = max(pgood_delay, (unsigned) 100);
797 if (do_delay)
798 msleep(delay);
799 return delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802static int hub_hub_status(struct usb_hub *hub,
803 u16 *status, u16 *change)
804{
805 int ret;
806
Alan Sterndb90e7a2007-02-05 09:56:15 -0500807 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 ret = get_hub_status(hub->hdev, &hub->status->hub);
809 if (ret < 0)
810 dev_err (hub->intfdev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800811 "%s failed (err = %d)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 else {
813 *status = le16_to_cpu(hub->status->hub.wHubStatus);
814 *change = le16_to_cpu(hub->status->hub.wHubChange);
815 ret = 0;
816 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500817 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return ret;
819}
820
Sarah Sharp41e7e052012-11-14 16:42:32 -0800821static int hub_set_port_link_state(struct usb_hub *hub, int port1,
822 unsigned int link_status)
823{
824 return set_port_feature(hub->hdev,
825 port1 | (link_status << 3),
826 USB_PORT_FEAT_LINK_STATE);
827}
828
829/*
830 * If USB 3.0 ports are placed into the Disabled state, they will no longer
831 * detect any device connects or disconnects. This is generally not what the
832 * USB core wants, since it expects a disabled port to produce a port status
833 * change event when a new device connects.
834 *
835 * Instead, set the link state to Disabled, wait for the link to settle into
836 * that state, clear any change bits, and then put the port into the RxDetect
837 * state.
838 */
839static int hub_usb3_port_disable(struct usb_hub *hub, int port1)
840{
841 int ret;
842 int total_time;
843 u16 portchange, portstatus;
844
845 if (!hub_is_superspeed(hub->hdev))
846 return -EINVAL;
847
848 ret = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_SS_DISABLED);
849 if (ret) {
850 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
851 port1, ret);
852 return ret;
853 }
854
855 /* Wait for the link to enter the disabled state. */
856 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
857 ret = hub_port_status(hub, port1, &portstatus, &portchange);
858 if (ret < 0)
859 return ret;
860
861 if ((portstatus & USB_PORT_STAT_LINK_STATE) ==
862 USB_SS_PORT_LS_SS_DISABLED)
863 break;
864 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
865 break;
866 msleep(HUB_DEBOUNCE_STEP);
867 }
868 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
869 dev_warn(hub->intfdev, "Could not disable port %d after %d ms\n",
870 port1, total_time);
871
872 return hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_RX_DETECT);
873}
874
Alan Stern8b28c752005-08-10 17:04:13 -0400875static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
876{
877 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400878 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400879
Lan Tianyuff823c792012-09-05 13:44:32 +0800880 if (hub->ports[port1 - 1]->child && set_state)
881 usb_set_device_state(hub->ports[port1 - 1]->child,
Alan Stern8b28c752005-08-10 17:04:13 -0400882 USB_STATE_NOTATTACHED);
Sarah Sharp41e7e052012-11-14 16:42:32 -0800883 if (!hub->error) {
884 if (hub_is_superspeed(hub->hdev))
885 ret = hub_usb3_port_disable(hub, port1);
886 else
887 ret = clear_port_feature(hdev, port1,
888 USB_PORT_FEAT_ENABLE);
889 }
Alan Stern8b28c752005-08-10 17:04:13 -0400890 if (ret)
891 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
Alan Stern0458d5b2007-05-04 11:52:20 -0400892 port1, ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400893 return ret;
894}
895
Alan Stern0458d5b2007-05-04 11:52:20 -0400896/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800897 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400898 * time later khubd will disconnect() any existing usb_device on the port
899 * and will re-enumerate if there actually is a device attached.
900 */
901static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
902{
903 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
904 hub_port_disable(hub, port1, 1);
905
906 /* FIXME let caller ask to power down the port:
907 * - some devices won't enumerate without a VBUS power cycle
908 * - SRP saves power that way
909 * - ... new call, TBD ...
910 * That's easy if this hub can switch power per-port, and
911 * khubd reactivates the port later (timer, SRP, etc).
912 * Powerdown must be optional, because of reset/DFU.
913 */
914
915 set_bit(port1, hub->change_bits);
916 kick_khubd(hub);
917}
918
Alan Stern253e0572009-10-27 15:20:13 -0400919/**
920 * usb_remove_device - disable a device's port on its parent hub
921 * @udev: device to be disabled and removed
922 * Context: @udev locked, must be able to sleep.
923 *
924 * After @udev's port has been disabled, khubd is notified and it will
925 * see that the device has been disconnected. When the device is
926 * physically unplugged and something is plugged in, the events will
927 * be received and processed normally.
928 */
929int usb_remove_device(struct usb_device *udev)
930{
931 struct usb_hub *hub;
932 struct usb_interface *intf;
933
934 if (!udev->parent) /* Can't remove a root hub */
935 return -EINVAL;
936 hub = hdev_to_hub(udev->parent);
937 intf = to_usb_interface(hub->intfdev);
938
939 usb_autopm_get_interface(intf);
940 set_bit(udev->portnum, hub->removed_bits);
941 hub_port_logical_disconnect(hub, udev->portnum);
942 usb_autopm_put_interface(intf);
943 return 0;
944}
945
Alan Stern6ee0b272008-04-28 11:06:42 -0400946enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -0500947 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -0400948 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -0400949};
Alan Stern5e6effa2008-03-03 15:15:51 -0500950
Alan Stern8520f382008-09-22 14:44:26 -0400951static void hub_init_func2(struct work_struct *ws);
952static void hub_init_func3(struct work_struct *ws);
953
Alan Sternf2835212008-04-28 11:07:17 -0400954static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500955{
956 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -0800957 struct usb_hcd *hcd;
958 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -0500959 int port1;
Alan Sternf2835212008-04-28 11:07:17 -0400960 int status;
Alan Stern948fea32008-04-28 11:07:07 -0400961 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -0400962 unsigned delay;
963
964 /* Continue a partial initialization */
965 if (type == HUB_INIT2)
966 goto init2;
967 if (type == HUB_INIT3)
968 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -0500969
Elric Fua45aa3b2012-02-18 13:32:27 +0800970 /* The superspeed hub except for root hub has to use Hub Depth
971 * value as an offset into the route string to locate the bits
972 * it uses to determine the downstream port number. So hub driver
973 * should send a set hub depth request to superspeed hub after
974 * the superspeed hub is set configuration in initialization or
975 * reset procedure.
976 *
977 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -0400978 * For any other type of activation, turn it on.
979 */
Alan Stern8520f382008-09-22 14:44:26 -0400980 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +0800981 if (hdev->parent && hub_is_superspeed(hdev)) {
982 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
983 HUB_SET_DEPTH, USB_RT_HUB,
984 hdev->level - 1, 0, NULL, 0,
985 USB_CTRL_SET_TIMEOUT);
986 if (ret < 0)
987 dev_err(hub->intfdev,
988 "set hub depth failed\n");
989 }
Alan Stern8520f382008-09-22 14:44:26 -0400990
991 /* Speed up system boot by using a delayed_work for the
992 * hub's initial power-up delays. This is pretty awkward
993 * and the implementation looks like a home-brewed sort of
994 * setjmp/longjmp, but it saves at least 100 ms for each
995 * root hub (assuming usbcore is compiled into the kernel
996 * rather than as a module). It adds up.
997 *
998 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
999 * because for those activation types the ports have to be
1000 * operational when we return. In theory this could be done
1001 * for HUB_POST_RESET, but it's easier not to.
1002 */
1003 if (type == HUB_INIT) {
1004 delay = hub_power_on(hub, false);
1005 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
1006 schedule_delayed_work(&hub->init_work,
1007 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -04001008
1009 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -05001010 usb_autopm_get_interface_no_resume(
1011 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -04001012 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001013 } else if (type == HUB_RESET_RESUME) {
1014 /* The internal host controller state for the hub device
1015 * may be gone after a host power loss on system resume.
1016 * Update the device's info so the HW knows it's a hub.
1017 */
1018 hcd = bus_to_hcd(hdev->bus);
1019 if (hcd->driver->update_hub_device) {
1020 ret = hcd->driver->update_hub_device(hcd, hdev,
1021 &hub->tt, GFP_NOIO);
1022 if (ret < 0) {
1023 dev_err(hub->intfdev, "Host not "
1024 "accepting hub info "
1025 "update.\n");
1026 dev_err(hub->intfdev, "LS/FS devices "
1027 "and hubs may not work "
1028 "under this hub\n.");
1029 }
1030 }
1031 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001032 } else {
1033 hub_power_on(hub, true);
1034 }
1035 }
1036 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001037
Alan Stern6ee0b272008-04-28 11:06:42 -04001038 /* Check each port and set hub->change_bits to let khubd know
1039 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001040 */
1041 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001042 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern5e6effa2008-03-03 15:15:51 -05001043 u16 portstatus, portchange;
1044
Alan Stern6ee0b272008-04-28 11:06:42 -04001045 portstatus = portchange = 0;
1046 status = hub_port_status(hub, port1, &portstatus, &portchange);
1047 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1048 dev_dbg(hub->intfdev,
1049 "port %d: status %04x change %04x\n",
1050 port1, portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001051
Alan Stern6ee0b272008-04-28 11:06:42 -04001052 /* After anything other than HUB_RESUME (i.e., initialization
1053 * or any sort of reset), every port should be disabled.
1054 * Unconnected ports should likewise be disabled (paranoia),
1055 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001056 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001057 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1058 type != HUB_RESUME ||
1059 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1060 !udev ||
1061 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001062 /*
1063 * USB3 protocol ports will automatically transition
1064 * to Enabled state when detect an USB3.0 device attach.
1065 * Do not disable USB3 protocol ports.
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001066 */
Sarah Sharp131dec32010-12-06 21:00:19 -08001067 if (!hub_is_superspeed(hdev)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001068 clear_port_feature(hdev, port1,
1069 USB_PORT_FEAT_ENABLE);
1070 portstatus &= ~USB_PORT_STAT_ENABLE;
Sarah Sharp85f0ff42010-10-14 07:22:54 -07001071 } else {
1072 /* Pretend that power was lost for USB3 devs */
1073 portstatus &= ~USB_PORT_STAT_ENABLE;
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001074 }
Alan Stern6ee0b272008-04-28 11:06:42 -04001075 }
1076
Alan Stern948fea32008-04-28 11:07:07 -04001077 /* Clear status-change flags; we'll debounce later */
1078 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1079 need_debounce_delay = true;
1080 clear_port_feature(hub->hdev, port1,
1081 USB_PORT_FEAT_C_CONNECTION);
1082 }
1083 if (portchange & USB_PORT_STAT_C_ENABLE) {
1084 need_debounce_delay = true;
1085 clear_port_feature(hub->hdev, port1,
1086 USB_PORT_FEAT_C_ENABLE);
1087 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001088 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1089 hub_is_superspeed(hub->hdev)) {
1090 need_debounce_delay = true;
1091 clear_port_feature(hub->hdev, port1,
1092 USB_PORT_FEAT_C_BH_PORT_RESET);
1093 }
Alan Stern253e0572009-10-27 15:20:13 -04001094 /* We can forget about a "removed" device when there's a
1095 * physical disconnect or the connect status changes.
1096 */
1097 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1098 (portchange & USB_PORT_STAT_C_CONNECTION))
1099 clear_bit(port1, hub->removed_bits);
1100
Alan Stern6ee0b272008-04-28 11:06:42 -04001101 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1102 /* Tell khubd to disconnect the device or
1103 * check for a new connection
1104 */
1105 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1106 set_bit(port1, hub->change_bits);
1107
1108 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001109 bool port_resumed = (portstatus &
1110 USB_PORT_STAT_LINK_STATE) ==
1111 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001112 /* The power session apparently survived the resume.
1113 * If there was an overcurrent or suspend change
1114 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -08001115 * take care of it. Look at the port link state
1116 * for USB 3.0 hubs, since they don't have a suspend
1117 * change bit, and they don't set the port link change
1118 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001119 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001120 if (portchange || (hub_is_superspeed(hub->hdev) &&
1121 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001122 set_bit(port1, hub->change_bits);
1123
1124 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001125#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001126 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001127#endif
Alan Stern8808f002008-04-28 11:06:55 -04001128 set_bit(port1, hub->change_bits);
1129
Alan Stern6ee0b272008-04-28 11:06:42 -04001130 } else {
1131 /* The power session is gone; tell khubd */
1132 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1133 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001134 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001135 }
1136
Alan Stern948fea32008-04-28 11:07:07 -04001137 /* If no port-status-change flags were set, we don't need any
1138 * debouncing. If flags were set we can try to debounce the
1139 * ports all at once right now, instead of letting khubd do them
1140 * one at a time later on.
1141 *
1142 * If any port-status changes do occur during this delay, khubd
1143 * will see them later and handle them normally.
1144 */
Alan Stern8520f382008-09-22 14:44:26 -04001145 if (need_debounce_delay) {
1146 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001147
Alan Stern8520f382008-09-22 14:44:26 -04001148 /* Don't do a long sleep inside a workqueue routine */
1149 if (type == HUB_INIT2) {
1150 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1151 schedule_delayed_work(&hub->init_work,
1152 msecs_to_jiffies(delay));
1153 return; /* Continues at init3: below */
1154 } else {
1155 msleep(delay);
1156 }
1157 }
1158 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001159 hub->quiescing = 0;
1160
1161 status = usb_submit_urb(hub->urb, GFP_NOIO);
1162 if (status < 0)
1163 dev_err(hub->intfdev, "activate --> %d\n", status);
1164 if (hub->has_indicators && blinkenlights)
1165 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1166
1167 /* Scan all ports that need attention */
1168 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001169
1170 /* Allow autosuspend if it was suppressed */
1171 if (type <= HUB_INIT3)
1172 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -05001173}
1174
Alan Stern8520f382008-09-22 14:44:26 -04001175/* Implement the continuations for the delays above */
1176static void hub_init_func2(struct work_struct *ws)
1177{
1178 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1179
1180 hub_activate(hub, HUB_INIT2);
1181}
1182
1183static void hub_init_func3(struct work_struct *ws)
1184{
1185 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1186
1187 hub_activate(hub, HUB_INIT3);
1188}
1189
Alan Stern43303542008-04-28 11:07:31 -04001190enum hub_quiescing_type {
1191 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1192};
1193
1194static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1195{
1196 struct usb_device *hdev = hub->hdev;
1197 int i;
1198
Alan Stern8520f382008-09-22 14:44:26 -04001199 cancel_delayed_work_sync(&hub->init_work);
1200
Alan Stern43303542008-04-28 11:07:31 -04001201 /* khubd and related activity won't re-trigger */
1202 hub->quiescing = 1;
1203
1204 if (type != HUB_SUSPEND) {
1205 /* Disconnect all the children */
1206 for (i = 0; i < hdev->maxchild; ++i) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001207 if (hub->ports[i]->child)
1208 usb_disconnect(&hub->ports[i]->child);
Alan Stern43303542008-04-28 11:07:31 -04001209 }
1210 }
1211
1212 /* Stop khubd and related activity */
1213 usb_kill_urb(hub->urb);
1214 if (hub->has_indicators)
1215 cancel_delayed_work_sync(&hub->leds);
1216 if (hub->tt.hub)
Octavian Purdila036546b2012-10-23 11:33:12 +03001217 flush_work(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001218}
1219
Alan Stern3eb14912008-03-03 15:15:43 -05001220/* caller has locked the hub device */
1221static int hub_pre_reset(struct usb_interface *intf)
1222{
1223 struct usb_hub *hub = usb_get_intfdata(intf);
1224
Alan Stern43303542008-04-28 11:07:31 -04001225 hub_quiesce(hub, HUB_PRE_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001226 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001227}
1228
1229/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001230static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001231{
Alan Stern7de18d82006-06-01 13:37:24 -04001232 struct usb_hub *hub = usb_get_intfdata(intf);
1233
Alan Sternf2835212008-04-28 11:07:17 -04001234 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001235 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001236}
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238static int hub_configure(struct usb_hub *hub,
1239 struct usb_endpoint_descriptor *endpoint)
1240{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001241 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 struct usb_device *hdev = hub->hdev;
1243 struct device *hub_dev = hub->intfdev;
1244 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001245 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 unsigned int pipe;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001247 int maxp, ret, i;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001248 char *message = "out of memory";
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001249 unsigned unit_load;
1250 unsigned full_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Alan Sternd697cdd2009-10-27 15:18:46 -04001252 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 ret = -ENOMEM;
1255 goto fail;
1256 }
1257
1258 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1259 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 ret = -ENOMEM;
1261 goto fail;
1262 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001263 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1266 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 ret = -ENOMEM;
1268 goto fail;
1269 }
1270
1271 /* Request the entire hub descriptor.
1272 * hub->descriptor can handle USB_MAXCHILDREN ports,
1273 * but the hub can/will return fewer bytes here.
1274 */
John Youndbe79bb2001-09-17 00:00:00 -07001275 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (ret < 0) {
1277 message = "can't read hub descriptor";
1278 goto fail;
1279 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1280 message = "hub has too many ports!";
1281 ret = -ENODEV;
1282 goto fail;
1283 }
1284
1285 hdev->maxchild = hub->descriptor->bNbrPorts;
1286 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1287 (hdev->maxchild == 1) ? "" : "s");
1288
Lan Tianyufa2a9562012-09-05 13:44:31 +08001289 hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1290 GFP_KERNEL);
Lan Tianyuff823c792012-09-05 13:44:32 +08001291 if (!hub->ports) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001292 ret = -ENOMEM;
1293 goto fail;
1294 }
1295
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001296 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001297 if (hub_is_superspeed(hdev)) {
1298 unit_load = 150;
1299 full_load = 900;
1300 } else {
1301 unit_load = 100;
1302 full_load = 500;
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
John Youndbe79bb2001-09-17 00:00:00 -07001305 /* FIXME for USB 3.0, skip for now */
1306 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1307 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 int i;
1309 char portstr [USB_MAXCHILDREN + 1];
1310
1311 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001312 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1314 ? 'F' : 'R';
1315 portstr[hdev->maxchild] = 0;
1316 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1317 } else
1318 dev_dbg(hub_dev, "standalone hub\n");
1319
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001320 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301321 case HUB_CHAR_COMMON_LPSM:
1322 dev_dbg(hub_dev, "ganged power switching\n");
1323 break;
1324 case HUB_CHAR_INDV_PORT_LPSM:
1325 dev_dbg(hub_dev, "individual port power switching\n");
1326 break;
1327 case HUB_CHAR_NO_LPSM:
1328 case HUB_CHAR_LPSM:
1329 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1330 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001333 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301334 case HUB_CHAR_COMMON_OCPM:
1335 dev_dbg(hub_dev, "global over-current protection\n");
1336 break;
1337 case HUB_CHAR_INDV_PORT_OCPM:
1338 dev_dbg(hub_dev, "individual port over-current protection\n");
1339 break;
1340 case HUB_CHAR_NO_OCPM:
1341 case HUB_CHAR_OCPM:
1342 dev_dbg(hub_dev, "no over-current protection\n");
1343 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345
1346 spin_lock_init (&hub->tt.lock);
1347 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001348 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301350 case USB_HUB_PR_FS:
1351 break;
1352 case USB_HUB_PR_HS_SINGLE_TT:
1353 dev_dbg(hub_dev, "Single TT\n");
1354 hub->tt.hub = hdev;
1355 break;
1356 case USB_HUB_PR_HS_MULTI_TT:
1357 ret = usb_set_interface(hdev, 0, 1);
1358 if (ret == 0) {
1359 dev_dbg(hub_dev, "TT per port\n");
1360 hub->tt.multi = 1;
1361 } else
1362 dev_err(hub_dev, "Using single TT (err %d)\n",
1363 ret);
1364 hub->tt.hub = hdev;
1365 break;
1366 case USB_HUB_PR_SS:
1367 /* USB 3.0 hubs don't have a TT */
1368 break;
1369 default:
1370 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1371 hdev->descriptor.bDeviceProtocol);
1372 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001375 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001376 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001377 case HUB_TTTT_8_BITS:
1378 if (hdev->descriptor.bDeviceProtocol != 0) {
1379 hub->tt.think_time = 666;
1380 dev_dbg(hub_dev, "TT requires at most %d "
1381 "FS bit times (%d ns)\n",
1382 8, hub->tt.think_time);
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001385 case HUB_TTTT_16_BITS:
1386 hub->tt.think_time = 666 * 2;
1387 dev_dbg(hub_dev, "TT requires at most %d "
1388 "FS bit times (%d ns)\n",
1389 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001391 case HUB_TTTT_24_BITS:
1392 hub->tt.think_time = 666 * 3;
1393 dev_dbg(hub_dev, "TT requires at most %d "
1394 "FS bit times (%d ns)\n",
1395 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001397 case HUB_TTTT_32_BITS:
1398 hub->tt.think_time = 666 * 4;
1399 dev_dbg(hub_dev, "TT requires at most %d "
1400 "FS bit times (%d ns)\n",
1401 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 break;
1403 }
1404
1405 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001406 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 hub->has_indicators = 1;
1408 dev_dbg(hub_dev, "Port indicators are supported\n");
1409 }
1410
1411 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1412 hub->descriptor->bPwrOn2PwrGood * 2);
1413
1414 /* power budgeting mostly matters with bus-powered hubs,
1415 * and battery-powered root hubs (may provide just 8 mA).
1416 */
1417 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001418 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 message = "can't get hub status";
1420 goto fail;
1421 }
Alan Stern7d35b922005-04-25 11:18:32 -04001422 le16_to_cpus(&hubstatus);
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001423 hcd = bus_to_hcd(hdev->bus);
Alan Stern7d35b922005-04-25 11:18:32 -04001424 if (hdev == hdev->bus->root_hub) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001425 if (hcd->power_budget > 0)
1426 hdev->bus_mA = hcd->power_budget;
1427 else
1428 hdev->bus_mA = full_load * hdev->maxchild;
1429 if (hdev->bus_mA >= full_load)
1430 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001431 else {
1432 hub->mA_per_port = hdev->bus_mA;
1433 hub->limited_power = 1;
1434 }
Alan Stern7d35b922005-04-25 11:18:32 -04001435 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001436 int remaining = hdev->bus_mA -
1437 hub->descriptor->bHubContrCurrent;
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1440 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001441 hub->limited_power = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001443 if (remaining < hdev->maxchild * unit_load)
1444 dev_warn(hub_dev,
Alan Stern55c52712005-11-23 12:03:12 -05001445 "insufficient power available "
1446 "to use all downstream ports\n");
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001447 hub->mA_per_port = unit_load; /* 7.2.1 */
1448
Alan Stern55c52712005-11-23 12:03:12 -05001449 } else { /* Self-powered external hub */
1450 /* FIXME: What about battery-powered external hubs that
1451 * provide less current per port? */
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001452 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001453 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001454 if (hub->mA_per_port < full_load)
Alan Stern55c52712005-11-23 12:03:12 -05001455 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1456 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001458 /* Update the HCD's internal representation of this hub before khubd
1459 * starts getting port status changes for devices under the hub.
1460 */
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001461 if (hcd->driver->update_hub_device) {
1462 ret = hcd->driver->update_hub_device(hcd, hdev,
1463 &hub->tt, GFP_KERNEL);
1464 if (ret < 0) {
1465 message = "can't update HCD hub info";
1466 goto fail;
1467 }
1468 }
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1471 if (ret < 0) {
1472 message = "can't get hub status";
1473 goto fail;
1474 }
1475
1476 /* local power status reports aren't always correct */
1477 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1478 dev_dbg(hub_dev, "local power source is %s\n",
1479 (hubstatus & HUB_STATUS_LOCAL_POWER)
1480 ? "lost (inactive)" : "good");
1481
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001482 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 dev_dbg(hub_dev, "%sover-current condition exists\n",
1484 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1485
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001486 /* set up the interrupt endpoint
1487 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1488 * bytes as USB2.0[11.12.3] says because some hubs are known
1489 * to send more data (and thus cause overflow). For root hubs,
1490 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1491 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1493 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1494
1495 if (maxp > sizeof(*hub->buffer))
1496 maxp = sizeof(*hub->buffer);
1497
1498 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1499 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 ret = -ENOMEM;
1501 goto fail;
1502 }
1503
1504 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1505 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 /* maybe cycle the hub leds */
1508 if (hub->has_indicators && blinkenlights)
1509 hub->indicator [0] = INDICATOR_CYCLE;
1510
Lan Tianyufa2a9562012-09-05 13:44:31 +08001511 for (i = 0; i < hdev->maxchild; i++)
1512 if (usb_hub_create_port_device(hub, i + 1) < 0)
1513 dev_err(hub->intfdev,
1514 "couldn't create port%d device.\n", i + 1);
1515
Alan Sternf2835212008-04-28 11:07:17 -04001516 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return 0;
1518
1519fail:
1520 dev_err (hub_dev, "config failed, %s (err %d)\n",
1521 message, ret);
1522 /* hub_disconnect() frees urb and descriptor */
1523 return ret;
1524}
1525
Alan Sterne8054852007-05-04 11:55:11 -04001526static void hub_release(struct kref *kref)
1527{
1528 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1529
1530 usb_put_intf(to_usb_interface(hub->intfdev));
1531 kfree(hub);
1532}
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534static unsigned highspeed_hubs;
1535
1536static void hub_disconnect(struct usb_interface *intf)
1537{
Huajun Li88162302012-03-12 21:00:19 +08001538 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001539 struct usb_device *hdev = interface_to_usbdev(intf);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001540 int i;
1541
Alan Sterne8054852007-05-04 11:55:11 -04001542 /* Take the hub off the event list and don't let it be added again */
1543 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001544 if (!list_empty(&hub->event_list)) {
1545 list_del_init(&hub->event_list);
1546 usb_autopm_put_interface_no_suspend(intf);
1547 }
Alan Sterne8054852007-05-04 11:55:11 -04001548 hub->disconnected = 1;
1549 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Alan Stern7de18d82006-06-01 13:37:24 -04001551 /* Disconnect all children and quiesce the hub */
1552 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001553 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001554
Alan Stern8b28c752005-08-10 17:04:13 -04001555 usb_set_intfdata (intf, NULL);
Alexander Shishkin1f2235b2012-09-12 14:48:31 +03001556
1557 for (i = 0; i < hdev->maxchild; i++)
1558 usb_hub_remove_port_device(hub, i + 1);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001559 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Alan Sterne8054852007-05-04 11:55:11 -04001561 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 highspeed_hubs--;
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 usb_free_urb(hub->urb);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001565 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001566 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001567 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001568 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Alan Sterne8054852007-05-04 11:55:11 -04001570 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
1573static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1574{
1575 struct usb_host_interface *desc;
1576 struct usb_endpoint_descriptor *endpoint;
1577 struct usb_device *hdev;
1578 struct usb_hub *hub;
1579
1580 desc = intf->cur_altsetting;
1581 hdev = interface_to_usbdev(intf);
1582
Ming Lei596d7892012-10-24 11:59:25 +08001583 /*
1584 * Set default autosuspend delay as 0 to speedup bus suspend,
1585 * based on the below considerations:
1586 *
1587 * - Unlike other drivers, the hub driver does not rely on the
1588 * autosuspend delay to provide enough time to handle a wakeup
1589 * event, and the submitted status URB is just to check future
1590 * change on hub downstream ports, so it is safe to do it.
1591 *
1592 * - The patch might cause one or more auto supend/resume for
1593 * below very rare devices when they are plugged into hub
1594 * first time:
1595 *
1596 * devices having trouble initializing, and disconnect
1597 * themselves from the bus and then reconnect a second
1598 * or so later
1599 *
1600 * devices just for downloading firmware, and disconnects
1601 * themselves after completing it
1602 *
1603 * For these quite rare devices, their drivers may change the
1604 * autosuspend delay of their parent hub in the probe() to one
1605 * appropriate value to avoid the subtle problem if someone
1606 * does care it.
1607 *
1608 * - The patch may cause one or more auto suspend/resume on
1609 * hub during running 'lsusb', but it is probably too
1610 * infrequent to worry about.
1611 *
1612 * - Change autosuspend delay of hub can avoid unnecessary auto
1613 * suspend timer for hub, also may decrease power consumption
1614 * of USB bus.
1615 */
1616 pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
1617
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001618 /* Hubs have proper suspend/resume support. */
1619 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001620
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001621 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001622 dev_err(&intf->dev,
1623 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001624 return -E2BIG;
1625 }
1626
David Brownell89ccbdc2006-04-02 10:18:09 -08001627#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1628 if (hdev->parent) {
1629 dev_warn(&intf->dev, "ignoring external hub\n");
1630 return -ENODEV;
1631 }
1632#endif
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 /* Some hubs have a subclass of 1, which AFAICT according to the */
1635 /* specs is not defined, but it works */
1636 if ((desc->desc.bInterfaceSubClass != 0) &&
1637 (desc->desc.bInterfaceSubClass != 1)) {
1638descriptor_error:
1639 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1640 return -EIO;
1641 }
1642
1643 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1644 if (desc->desc.bNumEndpoints != 1)
1645 goto descriptor_error;
1646
1647 endpoint = &desc->endpoint[0].desc;
1648
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001649 /* If it's not an interrupt in endpoint, we'd better punt! */
1650 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 goto descriptor_error;
1652
1653 /* We found a hub */
1654 dev_info (&intf->dev, "USB hub found\n");
1655
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001656 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (!hub) {
1658 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1659 return -ENOMEM;
1660 }
1661
Alan Sterne8054852007-05-04 11:55:11 -04001662 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 INIT_LIST_HEAD(&hub->event_list);
1664 hub->intfdev = &intf->dev;
1665 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001666 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001667 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001668 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001671 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 if (hdev->speed == USB_SPEED_HIGH)
1674 highspeed_hubs++;
1675
Ming Leie6f30de2012-10-24 11:59:24 +08001676 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1677 hub->quirk_check_port_auto_suspend = 1;
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (hub_configure(hub, endpoint) >= 0)
1680 return 0;
1681
1682 hub_disconnect (intf);
1683 return -ENODEV;
1684}
1685
1686static int
1687hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1688{
1689 struct usb_device *hdev = interface_to_usbdev (intf);
Lan Tianyuff823c792012-09-05 13:44:32 +08001690 struct usb_hub *hub = hdev_to_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 /* assert ifno == 0 (part of hub spec) */
1693 switch (code) {
1694 case USBDEVFS_HUB_PORTINFO: {
1695 struct usbdevfs_hub_portinfo *info = user_data;
1696 int i;
1697
1698 spin_lock_irq(&device_state_lock);
1699 if (hdev->devnum <= 0)
1700 info->nports = 0;
1701 else {
1702 info->nports = hdev->maxchild;
1703 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001704 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 info->port[i] = 0;
1706 else
1707 info->port[i] =
Lan Tianyuff823c792012-09-05 13:44:32 +08001708 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710 }
1711 spin_unlock_irq(&device_state_lock);
1712
1713 return info->nports + 1;
1714 }
1715
1716 default:
1717 return -ENOSYS;
1718 }
1719}
1720
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001721/*
1722 * Allow user programs to claim ports on a hub. When a device is attached
1723 * to one of these "claimed" ports, the program will "own" the device.
1724 */
1725static int find_port_owner(struct usb_device *hdev, unsigned port1,
Lan Tianyu336c5c32012-07-06 14:13:52 +08001726 struct dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001727{
1728 if (hdev->state == USB_STATE_NOTATTACHED)
1729 return -ENODEV;
1730 if (port1 == 0 || port1 > hdev->maxchild)
1731 return -EINVAL;
1732
1733 /* This assumes that devices not managed by the hub driver
1734 * will always have maxchild equal to 0.
1735 */
Lan Tianyufa2a9562012-09-05 13:44:31 +08001736 *ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001737 return 0;
1738}
1739
1740/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001741int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1742 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001743{
1744 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001745 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001746
1747 rc = find_port_owner(hdev, port1, &powner);
1748 if (rc)
1749 return rc;
1750 if (*powner)
1751 return -EBUSY;
1752 *powner = owner;
1753 return rc;
1754}
1755
Lan Tianyu336c5c32012-07-06 14:13:52 +08001756int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1757 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001758{
1759 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001760 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001761
1762 rc = find_port_owner(hdev, port1, &powner);
1763 if (rc)
1764 return rc;
1765 if (*powner != owner)
1766 return -ENOENT;
1767 *powner = NULL;
1768 return rc;
1769}
1770
Lan Tianyu336c5c32012-07-06 14:13:52 +08001771void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001772{
Lan Tianyufa2a9562012-09-05 13:44:31 +08001773 struct usb_hub *hub = hdev_to_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001774 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001775
Lan Tianyufa2a9562012-09-05 13:44:31 +08001776 for (n = 0; n < hdev->maxchild; n++) {
1777 if (hub->ports[n]->port_owner == owner)
1778 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001779 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001780
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001781}
1782
1783/* The caller must hold udev's lock */
1784bool usb_device_is_owned(struct usb_device *udev)
1785{
1786 struct usb_hub *hub;
1787
1788 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1789 return false;
1790 hub = hdev_to_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001791 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001792}
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1795{
Lan Tianyuff823c792012-09-05 13:44:32 +08001796 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 int i;
1798
1799 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001800 if (hub->ports[i]->child)
1801 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001803 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001804 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 udev->state = USB_STATE_NOTATTACHED;
1806}
1807
1808/**
1809 * usb_set_device_state - change a device's current state (usbcore, hcds)
1810 * @udev: pointer to device whose state should be changed
1811 * @new_state: new state value to be stored
1812 *
1813 * udev->state is _not_ fully protected by the device lock. Although
1814 * most transitions are made only while holding the lock, the state can
1815 * can change to USB_STATE_NOTATTACHED at almost any time. This
1816 * is so that devices can be marked as disconnected as soon as possible,
1817 * without having to wait for any semaphores to be released. As a result,
1818 * all changes to any device's state must be protected by the
1819 * device_state_lock spinlock.
1820 *
1821 * Once a device has been added to the device tree, all changes to its state
1822 * should be made using this routine. The state should _not_ be set directly.
1823 *
1824 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1825 * Otherwise udev->state is set to new_state, and if new_state is
1826 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1827 * to USB_STATE_NOTATTACHED.
1828 */
1829void usb_set_device_state(struct usb_device *udev,
1830 enum usb_device_state new_state)
1831{
1832 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001833 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 spin_lock_irqsave(&device_state_lock, flags);
1836 if (udev->state == USB_STATE_NOTATTACHED)
1837 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001838 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001839
1840 /* root hub wakeup capabilities are managed out-of-band
1841 * and may involve silicon errata ... ignore them here.
1842 */
1843 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001844 if (udev->state == USB_STATE_SUSPENDED
1845 || new_state == USB_STATE_SUSPENDED)
1846 ; /* No change to wakeup settings */
1847 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001848 wakeup = udev->actconfig->desc.bmAttributes
1849 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001850 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001851 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001852 }
Sarah Sharp15123002007-12-21 16:54:15 -08001853 if (udev->state == USB_STATE_SUSPENDED &&
1854 new_state != USB_STATE_SUSPENDED)
1855 udev->active_duration -= jiffies;
1856 else if (new_state == USB_STATE_SUSPENDED &&
1857 udev->state != USB_STATE_SUSPENDED)
1858 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001859 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001860 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 recursively_mark_NOTATTACHED(udev);
1862 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001863 if (wakeup >= 0)
1864 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
David Vrabel6da9c992009-02-18 14:43:47 +00001866EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001868/*
Alan Stern3b29b682011-02-22 09:53:41 -05001869 * Choose a device number.
1870 *
1871 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1872 * USB-2.0 buses they are also used as device addresses, however on
1873 * USB-3.0 buses the address is assigned by the controller hardware
1874 * and it usually is not the same as the device number.
1875 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001876 * WUSB devices are simple: they have no hubs behind, so the mapping
1877 * device <-> virtual port number becomes 1:1. Why? to simplify the
1878 * life of the device connection logic in
1879 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1880 * handshake we need to assign a temporary address in the unauthorized
1881 * space. For simplicity we use the first virtual port number found to
1882 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1883 * and that becomes it's address [X < 128] or its unauthorized address
1884 * [X | 0x80].
1885 *
1886 * We add 1 as an offset to the one-based USB-stack port number
1887 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1888 * 0 is reserved by USB for default address; (b) Linux's USB stack
1889 * uses always #1 for the root hub of the controller. So USB stack's
1890 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001891 *
1892 * Devices connected under xHCI are not as simple. The host controller
1893 * supports virtualization, so the hardware assigns device addresses and
1894 * the HCD must setup data structures before issuing a set address
1895 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001896 */
Alan Stern3b29b682011-02-22 09:53:41 -05001897static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
1899 int devnum;
1900 struct usb_bus *bus = udev->bus;
1901
1902 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001903 if (udev->wusb) {
1904 devnum = udev->portnum + 1;
1905 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1906 } else {
1907 /* Try to allocate the next devnum beginning at
1908 * bus->devnum_next. */
1909 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1910 bus->devnum_next);
1911 if (devnum >= 128)
1912 devnum = find_next_zero_bit(bus->devmap.devicemap,
1913 128, 1);
1914 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (devnum < 128) {
1917 set_bit(devnum, bus->devmap.devicemap);
1918 udev->devnum = devnum;
1919 }
1920}
1921
Alan Stern3b29b682011-02-22 09:53:41 -05001922static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 if (udev->devnum > 0) {
1925 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1926 udev->devnum = -1;
1927 }
1928}
1929
Alan Stern3b29b682011-02-22 09:53:41 -05001930static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001931{
1932 /* The address for a WUSB device is managed by wusbcore. */
1933 if (!udev->wusb)
1934 udev->devnum = devnum;
1935}
1936
Herbert Xuf7410ce2010-01-10 20:15:03 +11001937static void hub_free_dev(struct usb_device *udev)
1938{
1939 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1940
1941 /* Root hubs aren't real devices, so don't free HCD resources */
1942 if (hcd->driver->free_dev && udev->parent)
1943 hcd->driver->free_dev(hcd, udev);
1944}
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946/**
1947 * usb_disconnect - disconnect a device (usbcore-internal)
1948 * @pdev: pointer to device being disconnected
1949 * Context: !in_interrupt ()
1950 *
1951 * Something got disconnected. Get rid of it and all of its children.
1952 *
1953 * If *pdev is a normal device then the parent hub must already be locked.
1954 * If *pdev is a root hub then this routine will acquire the
1955 * usb_bus_list_lock on behalf of the caller.
1956 *
1957 * Only hub drivers (including virtual root hub drivers for host
1958 * controllers) should ever call this.
1959 *
1960 * This call is synchronous, and may not be used in an interrupt context.
1961 */
1962void usb_disconnect(struct usb_device **pdev)
1963{
1964 struct usb_device *udev = *pdev;
Lan Tianyuff823c792012-09-05 13:44:32 +08001965 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 int i;
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 /* mark the device as inactive, so any further urb submissions for
1969 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001970 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 */
1972 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05001973 dev_info(&udev->dev, "USB disconnect, device number %d\n",
1974 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Alan Stern9ad3d6c2005-11-17 17:10:32 -05001976 usb_lock_device(udev);
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08001979 for (i = 0; i < udev->maxchild; i++) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001980 if (hub->ports[i]->child)
1981 usb_disconnect(&hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983
1984 /* deallocate hcd/hardware state ... nuking all pending urbs and
1985 * cleaning up all state associated with the current configuration
1986 * so that the hardware is now fully quiesced.
1987 */
Alan Stern782da722006-07-01 22:09:35 -04001988 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04001990 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Lan Tianyufde26382013-01-20 01:53:33 +08001992 if (udev->parent) {
1993 struct usb_port *port_dev =
1994 hdev_to_hub(udev->parent)->ports[udev->portnum - 1];
1995
1996 sysfs_remove_link(&udev->dev.kobj, "port");
1997 sysfs_remove_link(&port_dev->dev.kobj, "device");
1998 }
1999
Alan Stern3b23dd62008-12-05 14:10:34 -05002000 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04002001 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07002002
Alan Stern782da722006-07-01 22:09:35 -04002003 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002004 * for de-configuring the device and invoking the remove-device
2005 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002006 */
2007 device_del(&udev->dev);
2008
2009 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 * (or root_hub) pointer.
2011 */
Alan Stern3b29b682011-02-22 09:53:41 -05002012 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 /* Avoid races with recursively_mark_NOTATTACHED() */
2015 spin_lock_irq(&device_state_lock);
2016 *pdev = NULL;
2017 spin_unlock_irq(&device_state_lock);
2018
Herbert Xuf7410ce2010-01-10 20:15:03 +11002019 hub_free_dev(udev);
2020
Alan Stern782da722006-07-01 22:09:35 -04002021 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002024#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025static void show_string(struct usb_device *udev, char *id, char *string)
2026{
2027 if (!string)
2028 return;
Joe Perchesf2ec5222012-10-28 01:05:51 -07002029 dev_info(&udev->dev, "%s: %s\n", id, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002032static void announce_device(struct usb_device *udev)
2033{
2034 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2035 le16_to_cpu(udev->descriptor.idVendor),
2036 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002037 dev_info(&udev->dev,
2038 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002039 udev->descriptor.iManufacturer,
2040 udev->descriptor.iProduct,
2041 udev->descriptor.iSerialNumber);
2042 show_string(udev, "Product", udev->product);
2043 show_string(udev, "Manufacturer", udev->manufacturer);
2044 show_string(udev, "SerialNumber", udev->serial);
2045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002047static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048#endif
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050#ifdef CONFIG_USB_OTG
2051#include "otg_whitelist.h"
2052#endif
2053
Oliver Neukum3ede7602007-01-11 14:35:50 +01002054/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002055 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002056 * @udev: newly addressed device (in ADDRESS state)
2057 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002058 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01002059 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002060static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002062 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064#ifdef CONFIG_USB_OTG
2065 /*
2066 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2067 * to wake us after we've powered off VBUS; and HNP, switching roles
2068 * "host" to "peripheral". The OTG descriptor helps figure this out.
2069 */
2070 if (!udev->bus->is_b_host
2071 && udev->config
2072 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002073 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 struct usb_bus *bus = udev->bus;
2075
2076 /* descriptor may appear anywhere in config */
2077 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2078 le16_to_cpu(udev->config[0].desc.wTotalLength),
2079 USB_DT_OTG, (void **) &desc) == 0) {
2080 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002081 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 dev_info(&udev->dev,
2084 "Dual-Role OTG device on %sHNP port\n",
2085 (port1 == bus->otg_port)
2086 ? "" : "non-");
2087
2088 /* enable HNP before suspend, it's simpler */
2089 if (port1 == bus->otg_port)
2090 bus->b_hnp_enable = 1;
2091 err = usb_control_msg(udev,
2092 usb_sndctrlpipe(udev, 0),
2093 USB_REQ_SET_FEATURE, 0,
2094 bus->b_hnp_enable
2095 ? USB_DEVICE_B_HNP_ENABLE
2096 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2097 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2098 if (err < 0) {
2099 /* OTG MESSAGE: report errors here,
2100 * customize to match your product.
2101 */
2102 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002103 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 err);
2105 bus->b_hnp_enable = 0;
2106 }
2107 }
2108 }
2109 }
2110
2111 if (!is_targeted(udev)) {
2112
2113 /* Maybe it can talk to us, though we can't talk to it.
2114 * (Includes HNP test device.)
2115 */
2116 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002117 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (err < 0)
2119 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2120 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002121 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 goto fail;
2123 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002124fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002126 return err;
2127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002129
2130/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002131 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002132 * @udev: newly addressed device (in ADDRESS state)
2133 *
2134 * This is only called by usb_new_device() and usb_authorize_device()
2135 * and FIXME -- all comments that apply to them apply here wrt to
2136 * environment.
2137 *
2138 * If the device is WUSB and not authorized, we don't attempt to read
2139 * the string descriptors, as they will be errored out by the device
2140 * until it has been authorized.
2141 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002142static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002143{
2144 int err;
2145
2146 if (udev->config == NULL) {
2147 err = usb_get_configuration(udev);
2148 if (err < 0) {
2149 dev_err(&udev->dev, "can't read configurations, error %d\n",
2150 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002151 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002152 }
2153 }
2154 if (udev->wusb == 1 && udev->authorized == 0) {
2155 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2156 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2157 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2158 }
2159 else {
2160 /* read the standard strings and cache them if present */
2161 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2162 udev->manufacturer = usb_cache_string(udev,
2163 udev->descriptor.iManufacturer);
2164 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2165 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002166 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002167 if (err < 0)
2168 return err;
2169
2170 usb_detect_interface_quirks(udev);
2171
2172 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002173}
2174
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002175static void set_usb_port_removable(struct usb_device *udev)
2176{
2177 struct usb_device *hdev = udev->parent;
2178 struct usb_hub *hub;
2179 u8 port = udev->portnum;
2180 u16 wHubCharacteristics;
2181 bool removable = true;
2182
2183 if (!hdev)
2184 return;
2185
2186 hub = hdev_to_hub(udev->parent);
2187
2188 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2189
2190 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2191 return;
2192
2193 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002194 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2195 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002196 removable = false;
2197 } else {
2198 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2199 removable = false;
2200 }
2201
2202 if (removable)
2203 udev->removable = USB_DEVICE_REMOVABLE;
2204 else
2205 udev->removable = USB_DEVICE_FIXED;
2206}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002207
2208/**
2209 * usb_new_device - perform initial device setup (usbcore-internal)
2210 * @udev: newly addressed device (in ADDRESS state)
2211 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002212 * This is called with devices which have been detected but not fully
2213 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002214 * for any device configuration. The caller must have locked either
2215 * the parent hub (if udev is a normal device) or else the
2216 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2217 * udev has already been installed, but udev is not yet visible through
2218 * sysfs or other filesystem code.
2219 *
2220 * It will return if the device is configured properly or not. Zero if
2221 * the interface was registered with the driver core; else a negative
2222 * errno value.
2223 *
2224 * This call is synchronous, and may not be used in an interrupt context.
2225 *
2226 * Only the hub driver or root-hub registrar should ever call this.
2227 */
2228int usb_new_device(struct usb_device *udev)
2229{
2230 int err;
2231
Dan Streetman16985402010-01-06 09:56:53 -05002232 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002233 /* Initialize non-root-hub device wakeup to disabled;
2234 * device (un)configuration controls wakeup capable
2235 * sysfs power/wakeup controls wakeup enabled/disabled
2236 */
2237 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002238 }
2239
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002240 /* Tell the runtime-PM framework the device is active */
2241 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002242 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002243 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002244 pm_runtime_enable(&udev->dev);
2245
Alan Sternc08512c2010-11-15 15:57:58 -05002246 /* By default, forbid autosuspend for all devices. It will be
2247 * allowed for hubs during binding.
2248 */
2249 usb_disable_autosuspend(udev);
2250
Alan Stern8d8558d2009-12-08 15:50:41 -05002251 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002252 if (err < 0)
2253 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002254 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2255 udev->devnum, udev->bus->busnum,
2256 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002257 /* export the usbdev device-node for libusb */
2258 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2259 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2260
Alan Stern6cd13202008-11-13 15:08:30 -05002261 /* Tell the world! */
2262 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002263
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002264 if (udev->serial)
2265 add_device_randomness(udev->serial, strlen(udev->serial));
2266 if (udev->product)
2267 add_device_randomness(udev->product, strlen(udev->product));
2268 if (udev->manufacturer)
2269 add_device_randomness(udev->manufacturer,
2270 strlen(udev->manufacturer));
2271
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002272 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002273
2274 /*
2275 * check whether the hub marks this port as non-removable. Do it
2276 * now so that platform-specific data can override it in
2277 * device_add()
2278 */
2279 if (udev->parent)
2280 set_usb_port_removable(udev);
2281
Alan Stern782da722006-07-01 22:09:35 -04002282 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002283 * for configuring the device and invoking the add-device
2284 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002285 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002286 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 if (err) {
2288 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2289 goto fail;
2290 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002291
Lan Tianyufde26382013-01-20 01:53:33 +08002292 /* Create link files between child device and usb port device. */
2293 if (udev->parent) {
2294 struct usb_port *port_dev =
2295 hdev_to_hub(udev->parent)->ports[udev->portnum - 1];
2296
2297 err = sysfs_create_link(&udev->dev.kobj,
2298 &port_dev->dev.kobj, "port");
2299 if (err)
2300 goto fail;
2301
2302 err = sysfs_create_link(&port_dev->dev.kobj,
2303 &udev->dev.kobj, "device");
2304 if (err) {
2305 sysfs_remove_link(&udev->dev.kobj, "port");
2306 goto fail;
2307 }
2308 }
2309
Alan Stern3b23dd62008-12-05 14:10:34 -05002310 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002311 usb_mark_last_busy(udev);
2312 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002313 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315fail:
2316 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002317 pm_runtime_disable(&udev->dev);
2318 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002319 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002322
2323/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002324 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2325 * @usb_dev: USB device
2326 *
2327 * Move the USB device to a very basic state where interfaces are disabled
2328 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002329 *
2330 * We share a lock (that we have) with device_del(), so we need to
2331 * defer its call.
2332 */
2333int usb_deauthorize_device(struct usb_device *usb_dev)
2334{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002335 usb_lock_device(usb_dev);
2336 if (usb_dev->authorized == 0)
2337 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002338
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002339 usb_dev->authorized = 0;
2340 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002341
2342 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002343 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002344 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002345 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002346 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002347 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002348
2349 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002350 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002351
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002352out_unauthorized:
2353 usb_unlock_device(usb_dev);
2354 return 0;
2355}
2356
2357
2358int usb_authorize_device(struct usb_device *usb_dev)
2359{
2360 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002361
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002362 usb_lock_device(usb_dev);
2363 if (usb_dev->authorized == 1)
2364 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002365
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002366 result = usb_autoresume_device(usb_dev);
2367 if (result < 0) {
2368 dev_err(&usb_dev->dev,
2369 "can't autoresume for authorization: %d\n", result);
2370 goto error_autoresume;
2371 }
2372 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2373 if (result < 0) {
2374 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2375 "authorization: %d\n", result);
2376 goto error_device_descriptor;
2377 }
Alan Sternda307122009-12-08 15:54:44 -05002378
2379 kfree(usb_dev->product);
2380 usb_dev->product = NULL;
2381 kfree(usb_dev->manufacturer);
2382 usb_dev->manufacturer = NULL;
2383 kfree(usb_dev->serial);
2384 usb_dev->serial = NULL;
2385
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002386 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002387 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002388 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002389 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002390 /* Choose and set the configuration. This registers the interfaces
2391 * with the driver core and lets interface drivers bind to them.
2392 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002393 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002394 if (c >= 0) {
2395 result = usb_set_configuration(usb_dev, c);
2396 if (result) {
2397 dev_err(&usb_dev->dev,
2398 "can't set config #%d, error %d\n", c, result);
2399 /* This need not be fatal. The user can try to
2400 * set other configurations. */
2401 }
2402 }
2403 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002404
Alan Stern8d8558d2009-12-08 15:50:41 -05002405error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002406error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002407 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002408error_autoresume:
2409out_authorized:
2410 usb_unlock_device(usb_dev); // complements locktree
2411 return result;
2412}
2413
2414
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002415/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2416static unsigned hub_is_wusb(struct usb_hub *hub)
2417{
2418 struct usb_hcd *hcd;
2419 if (hub->hdev->parent != NULL) /* not a root hub? */
2420 return 0;
2421 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2422 return hcd->wireless;
2423}
2424
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426#define PORT_RESET_TRIES 5
2427#define SET_ADDRESS_TRIES 2
2428#define GET_DESCRIPTOR_TRIES 2
2429#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302430#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2433#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002434#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435#define HUB_LONG_RESET_TIME 200
Sarah Sharp77c7f072012-11-14 17:16:52 -08002436#define HUB_RESET_TIMEOUT 800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Sarah Sharp10d674a2011-09-14 14:24:52 -07002438static int hub_port_reset(struct usb_hub *hub, int port1,
2439 struct usb_device *udev, unsigned int delay, bool warm);
2440
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002441/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2442 * Port worm reset is required to recover
2443 */
2444static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002445{
2446 return hub_is_superspeed(hub->hdev) &&
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002447 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2448 USB_SS_PORT_LS_SS_INACTIVE) ||
2449 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2450 USB_SS_PORT_LS_COMP_MOD)) ;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002451}
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002454 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
2456 int delay_time, ret;
2457 u16 portstatus;
2458 u16 portchange;
2459
2460 for (delay_time = 0;
2461 delay_time < HUB_RESET_TIMEOUT;
2462 delay_time += delay) {
2463 /* wait to give the device a chance to reset */
2464 msleep(delay);
2465
2466 /* read and decode port status */
2467 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2468 if (ret < 0)
2469 return ret;
2470
Sarah Sharp4f434472012-11-15 14:58:04 -08002471 /* The port state is unknown until the reset completes. */
Sarah Sharp470f0be2012-12-11 10:14:03 -08002472 if (!(portstatus & USB_PORT_STAT_RESET))
2473 break;
Sarah Sharp4f434472012-11-15 14:58:04 -08002474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 /* switch to the long delay after two short delay failures */
2476 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2477 delay = HUB_LONG_RESET_TIME;
2478
2479 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002480 "port %d not %sreset yet, waiting %dms\n",
2481 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
2483
Sarah Sharp470f0be2012-12-11 10:14:03 -08002484 if ((portstatus & USB_PORT_STAT_RESET))
2485 return -EBUSY;
2486
2487 if (hub_port_warm_reset_required(hub, portstatus))
2488 return -ENOTCONN;
2489
2490 /* Device went away? */
2491 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2492 return -ENOTCONN;
2493
2494 /* bomb out completely if the connection bounced. A USB 3.0
2495 * connection may bounce if multiple warm resets were issued,
2496 * but the device may have successfully re-connected. Ignore it.
2497 */
2498 if (!hub_is_superspeed(hub->hdev) &&
2499 (portchange & USB_PORT_STAT_C_CONNECTION))
2500 return -ENOTCONN;
2501
2502 if (!(portstatus & USB_PORT_STAT_ENABLE))
2503 return -EBUSY;
2504
2505 if (!udev)
2506 return 0;
2507
2508 if (hub_is_wusb(hub))
2509 udev->speed = USB_SPEED_WIRELESS;
2510 else if (hub_is_superspeed(hub->hdev))
2511 udev->speed = USB_SPEED_SUPER;
2512 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2513 udev->speed = USB_SPEED_HIGH;
2514 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2515 udev->speed = USB_SPEED_LOW;
2516 else
2517 udev->speed = USB_SPEED_FULL;
2518 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
2520
Andiry Xu75d7cf72011-09-13 16:41:11 -07002521static void hub_port_finish_reset(struct usb_hub *hub, int port1,
Sarah Sharpa24a6072012-11-01 11:20:44 -07002522 struct usb_device *udev, int *status)
Andiry Xu75d7cf72011-09-13 16:41:11 -07002523{
2524 switch (*status) {
2525 case 0:
Sarah Sharpa24a6072012-11-01 11:20:44 -07002526 /* TRSTRCY = 10 ms; plus some extra */
2527 msleep(10 + 40);
2528 if (udev) {
2529 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2530
2531 update_devnum(udev, 0);
2532 /* The xHC may think the device is already reset,
2533 * so ignore the status.
2534 */
2535 if (hcd->driver->reset_device)
2536 hcd->driver->reset_device(hcd, udev);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002537 }
2538 /* FALL THROUGH */
2539 case -ENOTCONN:
2540 case -ENODEV:
2541 clear_port_feature(hub->hdev,
2542 port1, USB_PORT_FEAT_C_RESET);
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002543 if (hub_is_superspeed(hub->hdev)) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002544 clear_port_feature(hub->hdev, port1,
2545 USB_PORT_FEAT_C_BH_PORT_RESET);
2546 clear_port_feature(hub->hdev, port1,
2547 USB_PORT_FEAT_C_PORT_LINK_STATE);
Sarah Sharpa24a6072012-11-01 11:20:44 -07002548 clear_port_feature(hub->hdev, port1,
2549 USB_PORT_FEAT_C_CONNECTION);
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002550 }
Sarah Sharpa24a6072012-11-01 11:20:44 -07002551 if (udev)
Andiry Xu75d7cf72011-09-13 16:41:11 -07002552 usb_set_device_state(udev, *status
2553 ? USB_STATE_NOTATTACHED
2554 : USB_STATE_DEFAULT);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002555 break;
2556 }
2557}
2558
2559/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002561 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562{
2563 int i, status;
Sarah Sharpa24a6072012-11-01 11:20:44 -07002564 u16 portchange, portstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002566 if (!hub_is_superspeed(hub->hdev)) {
2567 if (warm) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002568 dev_err(hub->intfdev, "only USB3 hub support "
2569 "warm reset\n");
2570 return -EINVAL;
2571 }
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002572 /* Block EHCI CF initialization during the port reset.
2573 * Some companion controllers don't like it when they mix.
2574 */
2575 down_read(&ehci_cf_port_reset_rwsem);
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07002576 } else if (!warm) {
2577 /*
2578 * If the caller hasn't explicitly requested a warm reset,
2579 * double check and see if one is needed.
2580 */
2581 status = hub_port_status(hub, port1,
2582 &portstatus, &portchange);
2583 if (status < 0)
2584 goto done;
2585
2586 if (hub_port_warm_reset_required(hub, portstatus))
2587 warm = true;
Andiry Xu75d7cf72011-09-13 16:41:11 -07002588 }
Alan Stern32fe0192007-10-10 16:27:07 -04002589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 /* Reset the port */
2591 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002592 status = set_port_feature(hub->hdev, port1, (warm ?
2593 USB_PORT_FEAT_BH_PORT_RESET :
2594 USB_PORT_FEAT_RESET));
2595 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002597 "cannot %sreset port %d (err = %d)\n",
2598 warm ? "warm " : "", port1, status);
2599 } else {
2600 status = hub_port_wait_reset(hub, port1, udev, delay,
2601 warm);
David Brownelldd165252005-08-31 10:45:25 -07002602 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 dev_dbg(hub->intfdev,
2604 "port_wait_reset: err = %d\n",
2605 status);
2606 }
2607
Sarah Sharpa24a6072012-11-01 11:20:44 -07002608 /* Check for disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002609 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
Sarah Sharpa24a6072012-11-01 11:20:44 -07002610 hub_port_finish_reset(hub, port1, udev, &status);
2611
2612 if (!hub_is_superspeed(hub->hdev))
2613 goto done;
2614
2615 /*
2616 * If a USB 3.0 device migrates from reset to an error
2617 * state, re-issue the warm reset.
2618 */
2619 if (hub_port_status(hub, port1,
2620 &portstatus, &portchange) < 0)
2621 goto done;
2622
2623 if (!hub_port_warm_reset_required(hub, portstatus))
2624 goto done;
2625
2626 /*
2627 * If the port is in SS.Inactive or Compliance Mode, the
2628 * hot or warm reset failed. Try another warm reset.
2629 */
2630 if (!warm) {
2631 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2632 port1);
2633 warm = true;
2634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 }
2636
2637 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002638 "port %d not enabled, trying %sreset again...\n",
2639 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 delay = HUB_LONG_RESET_TIME;
2641 }
2642
2643 dev_err (hub->intfdev,
2644 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2645 port1);
2646
Andiry Xu75d7cf72011-09-13 16:41:11 -07002647done:
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002648 if (!hub_is_superspeed(hub->hdev))
Andiry Xu75d7cf72011-09-13 16:41:11 -07002649 up_read(&ehci_cf_port_reset_rwsem);
2650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return status;
2652}
2653
Andiry Xu0ed9a572011-04-27 18:07:43 +08002654/* Check if a port is power on */
2655static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2656{
2657 int ret = 0;
2658
2659 if (hub_is_superspeed(hub->hdev)) {
2660 if (portstatus & USB_SS_PORT_STAT_POWER)
2661 ret = 1;
2662 } else {
2663 if (portstatus & USB_PORT_STAT_POWER)
2664 ret = 1;
2665 }
2666
2667 return ret;
2668}
2669
Alan Sternd388dab2006-07-01 22:14:24 -04002670#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Andiry Xu0ed9a572011-04-27 18:07:43 +08002672/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2673static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2674{
2675 int ret = 0;
2676
2677 if (hub_is_superspeed(hub->hdev)) {
2678 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2679 == USB_SS_PORT_LS_U3)
2680 ret = 1;
2681 } else {
2682 if (portstatus & USB_PORT_STAT_SUSPEND)
2683 ret = 1;
2684 }
2685
2686 return ret;
2687}
Alan Sternb01b03f2008-04-28 11:06:11 -04002688
2689/* Determine whether the device on a port is ready for a normal resume,
2690 * is ready for a reset-resume, or should be disconnected.
2691 */
2692static int check_port_resume_type(struct usb_device *udev,
2693 struct usb_hub *hub, int port1,
2694 int status, unsigned portchange, unsigned portstatus)
2695{
2696 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002697 if (status || port_is_suspended(hub, portstatus) ||
2698 !port_is_power_on(hub, portstatus) ||
2699 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002700 if (status >= 0)
2701 status = -ENODEV;
2702 }
2703
Alan Stern86c57ed2008-06-30 11:14:43 -04002704 /* Can't do a normal resume if the port isn't enabled,
2705 * so try a reset-resume instead.
2706 */
2707 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2708 if (udev->persist_enabled)
2709 udev->reset_resume = 1;
2710 else
2711 status = -ENODEV;
2712 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002713
2714 if (status) {
2715 dev_dbg(hub->intfdev,
2716 "port %d status %04x.%04x after resume, %d\n",
2717 port1, portchange, portstatus, status);
2718 } else if (udev->reset_resume) {
2719
2720 /* Late port handoff can set status-change bits */
2721 if (portchange & USB_PORT_STAT_C_CONNECTION)
2722 clear_port_feature(hub->hdev, port1,
2723 USB_PORT_FEAT_C_CONNECTION);
2724 if (portchange & USB_PORT_STAT_C_ENABLE)
2725 clear_port_feature(hub->hdev, port1,
2726 USB_PORT_FEAT_C_ENABLE);
2727 }
2728
2729 return status;
2730}
2731
Sarah Sharpf74631e2012-06-25 12:08:08 -07002732int usb_disable_ltm(struct usb_device *udev)
2733{
2734 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2735
2736 /* Check if the roothub and device supports LTM. */
2737 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2738 !usb_device_supports_ltm(udev))
2739 return 0;
2740
2741 /* Clear Feature LTM Enable can only be sent if the device is
2742 * configured.
2743 */
2744 if (!udev->actconfig)
2745 return 0;
2746
2747 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2748 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2749 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2750 USB_CTRL_SET_TIMEOUT);
2751}
2752EXPORT_SYMBOL_GPL(usb_disable_ltm);
2753
2754void usb_enable_ltm(struct usb_device *udev)
2755{
2756 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2757
2758 /* Check if the roothub and device supports LTM. */
2759 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2760 !usb_device_supports_ltm(udev))
2761 return;
2762
2763 /* Set Feature LTM Enable can only be sent if the device is
2764 * configured.
2765 */
2766 if (!udev->actconfig)
2767 return;
2768
2769 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2770 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2771 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2772 USB_CTRL_SET_TIMEOUT);
2773}
2774EXPORT_SYMBOL_GPL(usb_enable_ltm);
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776#ifdef CONFIG_USB_SUSPEND
2777
2778/*
Alan Stern140d8f62006-07-01 22:07:21 -04002779 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002780 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002781 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 *
2783 * Suspends a USB device that isn't in active use, conserving power.
2784 * Devices may wake out of a suspend, if anything important happens,
2785 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002786 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 * to disconnect devices while they are suspended.
2788 *
David Brownell390a8c32005-09-13 19:57:27 -07002789 * This only affects the USB hardware for a device; its interfaces
2790 * (and, for hubs, child devices) must already have been suspended.
2791 *
Alan Stern624d6c02007-05-30 15:35:16 -04002792 * Selective port suspend reduces power; most suspended devices draw
2793 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2794 * All devices below the suspended port are also suspended.
2795 *
2796 * Devices leave suspend state when the host wakes them up. Some devices
2797 * also support "remote wakeup", where the device can activate the USB
2798 * tree above them to deliver data, such as a keypress or packet. In
2799 * some cases, this wakes the USB host.
2800 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 * Suspending OTG devices may trigger HNP, if that's been enabled
2802 * between a pair of dual-role devices. That will change roles, such
2803 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2804 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002805 * Devices on USB hub ports have only one "suspend" state, corresponding
2806 * to ACPI D2, "may cause the device to lose some context".
2807 * State transitions include:
2808 *
2809 * - suspend, resume ... when the VBUS power link stays live
2810 * - suspend, disconnect ... VBUS lost
2811 *
2812 * Once VBUS drop breaks the circuit, the port it's using has to go through
2813 * normal re-enumeration procedures, starting with enabling VBUS power.
2814 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2815 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2816 * timer, no SRP, no requests through sysfs.
2817 *
2818 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2819 * the root hub for their bus goes into global suspend ... so we don't
2820 * (falsely) update the device power state to say it suspended.
2821 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 * Returns 0 on success, else negative errno.
2823 */
Alan Stern65bfd292008-11-25 16:39:18 -05002824int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
Alan Stern624d6c02007-05-30 15:35:16 -04002826 struct usb_hub *hub = hdev_to_hub(udev->parent);
2827 int port1 = udev->portnum;
2828 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002829
Alan Stern624d6c02007-05-30 15:35:16 -04002830 /* enable remote wakeup when appropriate; this lets the device
2831 * wake up the upstream hub (including maybe the root hub).
2832 *
2833 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2834 * we don't explicitly enable it here.
2835 */
2836 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002837 if (!hub_is_superspeed(hub->hdev)) {
2838 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2839 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2840 USB_DEVICE_REMOTE_WAKEUP, 0,
2841 NULL, 0,
2842 USB_CTRL_SET_TIMEOUT);
2843 } else {
2844 /* Assume there's only one function on the USB 3.0
2845 * device and enable remote wake for the first
2846 * interface. FIXME if the interface association
2847 * descriptor shows there's more than one function.
2848 */
2849 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2850 USB_REQ_SET_FEATURE,
2851 USB_RECIP_INTERFACE,
2852 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002853 USB_INTRF_FUNC_SUSPEND_RW |
2854 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002855 NULL, 0,
2856 USB_CTRL_SET_TIMEOUT);
2857 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002858 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002859 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2860 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002861 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002862 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002863 return status;
2864 }
Alan Stern624d6c02007-05-30 15:35:16 -04002865 }
2866
Andiry Xu65580b432011-09-23 14:19:52 -07002867 /* disable USB2 hardware LPM */
2868 if (udev->usb2_hw_lpm_enabled == 1)
2869 usb_set_usb2_hardware_lpm(udev, 0);
2870
Sarah Sharpf74631e2012-06-25 12:08:08 -07002871 if (usb_disable_ltm(udev)) {
2872 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2873 __func__);
2874 return -ENOMEM;
2875 }
Sarah Sharp83060952012-05-02 14:25:52 -07002876 if (usb_unlocked_disable_lpm(udev)) {
2877 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2878 __func__);
2879 return -ENOMEM;
2880 }
2881
Alan Stern624d6c02007-05-30 15:35:16 -04002882 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002883 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08002884 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
Andiry Xua8f08d82011-03-31 14:56:50 +08002885 else
2886 status = set_port_feature(hub->hdev, port1,
2887 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002888 if (status) {
2889 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2890 port1, status);
2891 /* paranoia: "should not happen" */
Oliver Neukum0c487202009-10-19 13:19:41 +02002892 if (udev->do_remote_wakeup)
2893 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Alan Stern624d6c02007-05-30 15:35:16 -04002894 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2895 USB_DEVICE_REMOTE_WAKEUP, 0,
2896 NULL, 0,
2897 USB_CTRL_SET_TIMEOUT);
Alan Sterncbb33002011-06-15 16:29:16 -04002898
Andiry Xuc3e751e2012-05-05 00:50:10 +08002899 /* Try to enable USB2 hardware LPM again */
2900 if (udev->usb2_hw_lpm_capable == 1)
2901 usb_set_usb2_hardware_lpm(udev, 1);
2902
Sarah Sharpf74631e2012-06-25 12:08:08 -07002903 /* Try to enable USB3 LTM and LPM again */
2904 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07002905 usb_unlocked_enable_lpm(udev);
2906
Alan Sterncbb33002011-06-15 16:29:16 -04002907 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002908 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002909 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002910 } else {
2911 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002912 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2913 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2914 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002915 usb_set_device_state(udev, USB_STATE_SUSPENDED);
Alan Sternbfd1e912012-10-19 11:03:39 -04002916 udev->port_is_suspended = 1;
Alan Stern624d6c02007-05-30 15:35:16 -04002917 msleep(10);
2918 }
Alan Sternc08512c2010-11-15 15:57:58 -05002919 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04002920 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921}
David Brownellf3f32532005-09-22 22:37:29 -07002922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923/*
David Brownell390a8c32005-09-13 19:57:27 -07002924 * If the USB "suspend" state is in use (rather than "global suspend"),
2925 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04002926 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 * hardware resume signaling is finished, either because of selective
2928 * resume (by host) or remote wakeup (by device) ... now see what changed
2929 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04002930 *
2931 * If @udev->reset_resume is set then the device is reset before the
2932 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 */
Alan Stern140d8f62006-07-01 22:07:21 -04002934static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Alan Stern54515fe2007-05-30 15:38:58 -04002936 int status = 0;
Oliver Neukum07e72b92012-11-29 15:05:57 +01002937 u16 devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002940 dev_dbg(&udev->dev, "%s\n",
2941 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
2943 /* usb ch9 identifies four variants of SUSPENDED, based on what
2944 * state the device resumes to. Linux currently won't see the
2945 * first two on the host side; they'd be inside hub_port_init()
2946 * during many timeouts, but khubd can't suspend until later.
2947 */
2948 usb_set_device_state(udev, udev->actconfig
2949 ? USB_STATE_CONFIGURED
2950 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Alan Stern54515fe2007-05-30 15:38:58 -04002952 /* 10.5.4.5 says not to reset a suspended port if the attached
2953 * device is enabled for remote wakeup. Hence the reset
2954 * operation is carried out here, after the port has been
2955 * resumed.
2956 */
2957 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04002958 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08002959 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 /* 10.5.4.5 says be sure devices in the tree are still there.
2962 * For now let's assume the device didn't go crazy on resume,
2963 * and device drivers will know about any resume quirks.
2964 */
Alan Stern54515fe2007-05-30 15:38:58 -04002965 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04002966 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04002967 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2968 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04002969 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04002970
2971 /* If a normal resume failed, try doing a reset-resume */
2972 if (status && !udev->reset_resume && udev->persist_enabled) {
2973 dev_dbg(&udev->dev, "retry with reset-resume\n");
2974 udev->reset_resume = 1;
2975 goto retry_reset_resume;
2976 }
Alan Stern54515fe2007-05-30 15:38:58 -04002977 }
Alan Sternb40b7a92006-06-19 15:12:38 -04002978
Alan Stern624d6c02007-05-30 15:35:16 -04002979 if (status) {
2980 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2981 status);
Oliver Neukum07e72b92012-11-29 15:05:57 +01002982 /*
2983 * There are a few quirky devices which violate the standard
2984 * by claiming to have remote wakeup enabled after a reset,
2985 * which crash if the feature is cleared, hence check for
2986 * udev->reset_resume
2987 */
2988 } else if (udev->actconfig && !udev->reset_resume) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04002990 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 status = usb_control_msg(udev,
2992 usb_sndctrlpipe(udev, 0),
2993 USB_REQ_CLEAR_FEATURE,
2994 USB_RECIP_DEVICE,
2995 USB_DEVICE_REMOTE_WAKEUP, 0,
2996 NULL, 0,
2997 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04002998 if (status)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002999 dev_dbg(&udev->dev,
3000 "disable remote wakeup, status %d\n",
3001 status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05003002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 }
3005 return status;
3006}
3007
Alan Stern624d6c02007-05-30 15:35:16 -04003008/*
3009 * usb_port_resume - re-activate a suspended usb device's upstream port
3010 * @udev: device to re-activate, not a root hub
3011 * Context: must be able to sleep; device not locked; pm locks held
3012 *
3013 * This will re-activate the suspended device, increasing power usage
3014 * while letting drivers communicate again with its endpoints.
3015 * USB resume explicitly guarantees that the power session between
3016 * the host and the device is the same as it was when the device
3017 * suspended.
3018 *
Alan Sternfeccc302008-03-03 15:15:59 -05003019 * If @udev->reset_resume is set then this routine won't check that the
3020 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04003021 * reset @udev. The end result is that a broken power session can be
3022 * recovered and @udev will appear to persist across a loss of VBUS power.
3023 *
3024 * For example, if a host controller doesn't maintain VBUS suspend current
3025 * during a system sleep or is reset when the system wakes up, all the USB
3026 * power sessions below it will be broken. This is especially troublesome
3027 * for mass-storage devices containing mounted filesystems, since the
3028 * device will appear to have disconnected and all the memory mappings
3029 * to it will be lost. Using the USB_PERSIST facility, the device can be
3030 * made to appear as if it had not disconnected.
3031 *
Ming Lei742120c2008-06-18 22:00:29 +08003032 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05003033 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04003034 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3035 * quite possible for a device to remain unaltered but its media to be
3036 * changed. If the user replaces a flash memory card while the system is
3037 * asleep, he will have only himself to blame when the filesystem on the
3038 * new card is corrupted and the system crashes.
3039 *
Alan Stern624d6c02007-05-30 15:35:16 -04003040 * Returns 0 on success, else negative errno.
3041 */
Alan Stern65bfd292008-11-25 16:39:18 -05003042int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
Alan Stern624d6c02007-05-30 15:35:16 -04003044 struct usb_hub *hub = hdev_to_hub(udev->parent);
3045 int port1 = udev->portnum;
3046 int status;
3047 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003048
3049 /* Skip the initial Clear-Suspend step for a remote wakeup */
3050 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003051 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003052 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
3054 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3055
Alan Sternd5cbad42006-08-11 16:52:39 -04003056 set_bit(port1, hub->busy_bits);
3057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003059 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003060 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
Andiry Xua7114232011-04-27 18:07:50 +08003061 else
3062 status = clear_port_feature(hub->hdev,
3063 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003065 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
3066 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04003069 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003070 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 msleep(25);
3072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3074 * stop resume signaling. Then finish the resume
3075 * sequence.
3076 */
Alan Sternd25450c2006-11-20 11:14:30 -05003077 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003078
Alan Sternb01b03f2008-04-28 11:06:11 -04003079 /* TRSMRCY = 10 msec */
3080 msleep(10);
3081 }
Alan Stern54515fe2007-05-30 15:38:58 -04003082
Alan Sternb01b03f2008-04-28 11:06:11 -04003083 SuspendCleared:
3084 if (status == 0) {
Alan Sternbfd1e912012-10-19 11:03:39 -04003085 udev->port_is_suspended = 0;
Andiry Xua7114232011-04-27 18:07:50 +08003086 if (hub_is_superspeed(hub->hdev)) {
3087 if (portchange & USB_PORT_STAT_C_LINK_STATE)
3088 clear_port_feature(hub->hdev, port1,
3089 USB_PORT_FEAT_C_PORT_LINK_STATE);
3090 } else {
3091 if (portchange & USB_PORT_STAT_C_SUSPEND)
3092 clear_port_feature(hub->hdev, port1,
3093 USB_PORT_FEAT_C_SUSPEND);
3094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Alan Sternd5cbad42006-08-11 16:52:39 -04003097 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04003098
Alan Sternb01b03f2008-04-28 11:06:11 -04003099 status = check_port_resume_type(udev,
3100 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003101 if (status == 0)
3102 status = finish_port_resume(udev);
3103 if (status < 0) {
3104 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3105 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003106 } else {
3107 /* Try to enable USB2 hardware LPM */
3108 if (udev->usb2_hw_lpm_capable == 1)
3109 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003110
Sarah Sharpf74631e2012-06-25 12:08:08 -07003111 /* Try to enable USB3 LTM and LPM */
3112 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003113 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003114 }
Andiry Xu65580b432011-09-23 14:19:52 -07003115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 return status;
3117}
3118
Alan Stern8808f002008-04-28 11:06:55 -04003119/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05003120int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121{
3122 int status = 0;
3123
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003125 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003126 status = usb_autoresume_device(udev);
3127 if (status == 0) {
3128 /* Let the drivers do their thing, then... */
3129 usb_autosuspend_device(udev);
3130 }
Alan Sternd25450c2006-11-20 11:14:30 -05003131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 return status;
3133}
3134
Alan Sternd388dab2006-07-01 22:14:24 -04003135#else /* CONFIG_USB_SUSPEND */
3136
3137/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3138
Alan Stern65bfd292008-11-25 16:39:18 -05003139int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003140{
3141 return 0;
3142}
3143
Alan Sternb01b03f2008-04-28 11:06:11 -04003144/* However we may need to do a reset-resume */
3145
Alan Stern65bfd292008-11-25 16:39:18 -05003146int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003147{
Alan Sternb01b03f2008-04-28 11:06:11 -04003148 struct usb_hub *hub = hdev_to_hub(udev->parent);
3149 int port1 = udev->portnum;
3150 int status;
3151 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04003152
Alan Sternb01b03f2008-04-28 11:06:11 -04003153 status = hub_port_status(hub, port1, &portstatus, &portchange);
3154 status = check_port_resume_type(udev,
3155 hub, port1, status, portchange, portstatus);
3156
3157 if (status) {
3158 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3159 hub_port_logical_disconnect(hub, port1);
3160 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04003161 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08003162 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003163 }
3164 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04003165}
3166
Alan Sternd388dab2006-07-01 22:14:24 -04003167#endif
3168
Ming Leie6f30de2012-10-24 11:59:24 +08003169static int check_ports_changed(struct usb_hub *hub)
3170{
3171 int port1;
3172
3173 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3174 u16 portstatus, portchange;
3175 int status;
3176
3177 status = hub_port_status(hub, port1, &portstatus, &portchange);
3178 if (!status && portchange)
3179 return 1;
3180 }
3181 return 0;
3182}
3183
David Brownelldb690872005-09-13 19:56:33 -07003184static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
3186 struct usb_hub *hub = usb_get_intfdata (intf);
3187 struct usb_device *hdev = hub->hdev;
3188 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003189 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Alan Sterncbb33002011-06-15 16:29:16 -04003191 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3193 struct usb_device *udev;
3194
Lan Tianyuff823c792012-09-05 13:44:32 +08003195 udev = hub->ports[port1 - 1]->child;
Alan Stern6840d252007-09-10 11:34:26 -04003196 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04003197 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003198 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003199 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 }
Ming Leie6f30de2012-10-24 11:59:24 +08003202
3203 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3204 /* check if there are changes pending on hub ports */
3205 if (check_ports_changed(hub)) {
3206 if (PMSG_IS_AUTO(msg))
3207 return -EBUSY;
3208 pm_wakeup_event(&hdev->dev, 2000);
3209 }
3210 }
3211
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003212 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3213 /* Enable hub to send remote wakeup for all ports. */
3214 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3215 status = set_port_feature(hdev,
3216 port1 |
3217 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3218 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3219 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3220 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3221 }
3222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
Harvey Harrison441b62c2008-03-03 16:08:34 -08003224 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003225
David Brownellc9f89fa2005-09-13 19:57:04 -07003226 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003227 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
3230
3231static int hub_resume(struct usb_interface *intf)
3232{
Alan Stern5e6effa2008-03-03 15:15:51 -05003233 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Alan Stern5e6effa2008-03-03 15:15:51 -05003235 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003236 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return 0;
3238}
3239
Alan Sternb41a60e2007-05-30 15:39:33 -04003240static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003241{
Alan Sternb41a60e2007-05-30 15:39:33 -04003242 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003243
Alan Stern5e6effa2008-03-03 15:15:51 -05003244 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003245 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003246 return 0;
3247}
3248
Alan Stern54515fe2007-05-30 15:38:58 -04003249/**
3250 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3251 * @rhdev: struct usb_device for the root hub
3252 *
3253 * The USB host controller driver calls this function when its root hub
3254 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003255 * has been reset. The routine marks @rhdev as having lost power.
3256 * When the hub driver is resumed it will take notice and carry out
3257 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3258 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003259 */
3260void usb_root_hub_lost_power(struct usb_device *rhdev)
3261{
3262 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3263 rhdev->reset_resume = 1;
3264}
3265EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3266
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003267static const char * const usb3_lpm_names[] = {
3268 "U0",
3269 "U1",
3270 "U2",
3271 "U3",
3272};
3273
3274/*
3275 * Send a Set SEL control transfer to the device, prior to enabling
3276 * device-initiated U1 or U2. This lets the device know the exit latencies from
3277 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3278 * packet from the host.
3279 *
3280 * This function will fail if the SEL or PEL values for udev are greater than
3281 * the maximum allowed values for the link state to be enabled.
3282 */
3283static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3284{
3285 struct usb_set_sel_req *sel_values;
3286 unsigned long long u1_sel;
3287 unsigned long long u1_pel;
3288 unsigned long long u2_sel;
3289 unsigned long long u2_pel;
3290 int ret;
3291
3292 /* Convert SEL and PEL stored in ns to us */
3293 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3294 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3295 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3296 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3297
3298 /*
3299 * Make sure that the calculated SEL and PEL values for the link
3300 * state we're enabling aren't bigger than the max SEL/PEL
3301 * value that will fit in the SET SEL control transfer.
3302 * Otherwise the device would get an incorrect idea of the exit
3303 * latency for the link state, and could start a device-initiated
3304 * U1/U2 when the exit latencies are too high.
3305 */
3306 if ((state == USB3_LPM_U1 &&
3307 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3308 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3309 (state == USB3_LPM_U2 &&
3310 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3311 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003312 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 -07003313 usb3_lpm_names[state], u1_sel, u1_pel);
3314 return -EINVAL;
3315 }
3316
3317 /*
3318 * If we're enabling device-initiated LPM for one link state,
3319 * but the other link state has a too high SEL or PEL value,
3320 * just set those values to the max in the Set SEL request.
3321 */
3322 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3323 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3324
3325 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3326 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3327
3328 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3329 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3330
3331 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3332 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3333
3334 /*
3335 * usb_enable_lpm() can be called as part of a failed device reset,
3336 * which may be initiated by an error path of a mass storage driver.
3337 * Therefore, use GFP_NOIO.
3338 */
3339 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3340 if (!sel_values)
3341 return -ENOMEM;
3342
3343 sel_values->u1_sel = u1_sel;
3344 sel_values->u1_pel = u1_pel;
3345 sel_values->u2_sel = cpu_to_le16(u2_sel);
3346 sel_values->u2_pel = cpu_to_le16(u2_pel);
3347
3348 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3349 USB_REQ_SET_SEL,
3350 USB_RECIP_DEVICE,
3351 0, 0,
3352 sel_values, sizeof *(sel_values),
3353 USB_CTRL_SET_TIMEOUT);
3354 kfree(sel_values);
3355 return ret;
3356}
3357
3358/*
3359 * Enable or disable device-initiated U1 or U2 transitions.
3360 */
3361static int usb_set_device_initiated_lpm(struct usb_device *udev,
3362 enum usb3_link_state state, bool enable)
3363{
3364 int ret;
3365 int feature;
3366
3367 switch (state) {
3368 case USB3_LPM_U1:
3369 feature = USB_DEVICE_U1_ENABLE;
3370 break;
3371 case USB3_LPM_U2:
3372 feature = USB_DEVICE_U2_ENABLE;
3373 break;
3374 default:
3375 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3376 __func__, enable ? "enable" : "disable");
3377 return -EINVAL;
3378 }
3379
3380 if (udev->state != USB_STATE_CONFIGURED) {
3381 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3382 "for unconfigured device.\n",
3383 __func__, enable ? "enable" : "disable",
3384 usb3_lpm_names[state]);
3385 return 0;
3386 }
3387
3388 if (enable) {
3389 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003390 * Now send the control transfer to enable device-initiated LPM
3391 * for either U1 or U2.
3392 */
3393 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3394 USB_REQ_SET_FEATURE,
3395 USB_RECIP_DEVICE,
3396 feature,
3397 0, NULL, 0,
3398 USB_CTRL_SET_TIMEOUT);
3399 } else {
3400 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3401 USB_REQ_CLEAR_FEATURE,
3402 USB_RECIP_DEVICE,
3403 feature,
3404 0, NULL, 0,
3405 USB_CTRL_SET_TIMEOUT);
3406 }
3407 if (ret < 0) {
3408 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3409 enable ? "Enable" : "Disable",
3410 usb3_lpm_names[state]);
3411 return -EBUSY;
3412 }
3413 return 0;
3414}
3415
3416static int usb_set_lpm_timeout(struct usb_device *udev,
3417 enum usb3_link_state state, int timeout)
3418{
3419 int ret;
3420 int feature;
3421
3422 switch (state) {
3423 case USB3_LPM_U1:
3424 feature = USB_PORT_FEAT_U1_TIMEOUT;
3425 break;
3426 case USB3_LPM_U2:
3427 feature = USB_PORT_FEAT_U2_TIMEOUT;
3428 break;
3429 default:
3430 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3431 __func__);
3432 return -EINVAL;
3433 }
3434
3435 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3436 timeout != USB3_LPM_DEVICE_INITIATED) {
3437 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3438 "which is a reserved value.\n",
3439 usb3_lpm_names[state], timeout);
3440 return -EINVAL;
3441 }
3442
3443 ret = set_port_feature(udev->parent,
3444 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3445 feature);
3446 if (ret < 0) {
3447 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3448 "error code %i\n", usb3_lpm_names[state],
3449 timeout, ret);
3450 return -EBUSY;
3451 }
3452 if (state == USB3_LPM_U1)
3453 udev->u1_params.timeout = timeout;
3454 else
3455 udev->u2_params.timeout = timeout;
3456 return 0;
3457}
3458
3459/*
3460 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3461 * U1/U2 entry.
3462 *
3463 * We will attempt to enable U1 or U2, but there are no guarantees that the
3464 * control transfers to set the hub timeout or enable device-initiated U1/U2
3465 * will be successful.
3466 *
3467 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3468 * driver know about it. If that call fails, it should be harmless, and just
3469 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3470 */
3471static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3472 enum usb3_link_state state)
3473{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003474 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003475 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3476 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3477
3478 /* If the device says it doesn't have *any* exit latency to come out of
3479 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3480 * state.
3481 */
3482 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3483 (state == USB3_LPM_U2 && u2_mel == 0))
3484 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003485
Sarah Sharp65a95b72012-10-05 10:32:07 -07003486 /*
3487 * First, let the device know about the exit latencies
3488 * associated with the link state we're about to enable.
3489 */
3490 ret = usb_req_set_sel(udev, state);
3491 if (ret < 0) {
3492 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3493 usb3_lpm_names[state]);
3494 return;
3495 }
3496
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003497 /* We allow the host controller to set the U1/U2 timeout internally
3498 * first, so that it can change its schedule to account for the
3499 * additional latency to send data to a device in a lower power
3500 * link state.
3501 */
3502 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3503
3504 /* xHCI host controller doesn't want to enable this LPM state. */
3505 if (timeout == 0)
3506 return;
3507
3508 if (timeout < 0) {
3509 dev_warn(&udev->dev, "Could not enable %s link state, "
3510 "xHCI error %i.\n", usb3_lpm_names[state],
3511 timeout);
3512 return;
3513 }
3514
3515 if (usb_set_lpm_timeout(udev, state, timeout))
3516 /* If we can't set the parent hub U1/U2 timeout,
3517 * device-initiated LPM won't be allowed either, so let the xHCI
3518 * host know that this link state won't be enabled.
3519 */
3520 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3521
3522 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3523 else if (udev->actconfig)
3524 usb_set_device_initiated_lpm(udev, state, true);
3525
3526}
3527
3528/*
3529 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3530 * U1/U2 entry.
3531 *
3532 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3533 * If zero is returned, the parent will not allow the link to go into U1/U2.
3534 *
3535 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3536 * it won't have an effect on the bus link state because the parent hub will
3537 * still disallow device-initiated U1/U2 entry.
3538 *
3539 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3540 * possible. The result will be slightly more bus bandwidth will be taken up
3541 * (to account for U1/U2 exit latency), but it should be harmless.
3542 */
3543static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3544 enum usb3_link_state state)
3545{
3546 int feature;
3547
3548 switch (state) {
3549 case USB3_LPM_U1:
3550 feature = USB_PORT_FEAT_U1_TIMEOUT;
3551 break;
3552 case USB3_LPM_U2:
3553 feature = USB_PORT_FEAT_U2_TIMEOUT;
3554 break;
3555 default:
3556 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3557 __func__);
3558 return -EINVAL;
3559 }
3560
3561 if (usb_set_lpm_timeout(udev, state, 0))
3562 return -EBUSY;
3563
3564 usb_set_device_initiated_lpm(udev, state, false);
3565
3566 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3567 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3568 "bus schedule bandwidth may be impacted.\n",
3569 usb3_lpm_names[state]);
3570 return 0;
3571}
3572
3573/*
3574 * Disable hub-initiated and device-initiated U1 and U2 entry.
3575 * Caller must own the bandwidth_mutex.
3576 *
3577 * This will call usb_enable_lpm() on failure, which will decrement
3578 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3579 */
3580int usb_disable_lpm(struct usb_device *udev)
3581{
3582 struct usb_hcd *hcd;
3583
3584 if (!udev || !udev->parent ||
3585 udev->speed != USB_SPEED_SUPER ||
3586 !udev->lpm_capable)
3587 return 0;
3588
3589 hcd = bus_to_hcd(udev->bus);
3590 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3591 return 0;
3592
3593 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003594 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003595 return 0;
3596
3597 /* If LPM is enabled, attempt to disable it. */
3598 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3599 goto enable_lpm;
3600 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3601 goto enable_lpm;
3602
3603 return 0;
3604
3605enable_lpm:
3606 usb_enable_lpm(udev);
3607 return -EBUSY;
3608}
3609EXPORT_SYMBOL_GPL(usb_disable_lpm);
3610
3611/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3612int usb_unlocked_disable_lpm(struct usb_device *udev)
3613{
3614 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3615 int ret;
3616
3617 if (!hcd)
3618 return -EINVAL;
3619
3620 mutex_lock(hcd->bandwidth_mutex);
3621 ret = usb_disable_lpm(udev);
3622 mutex_unlock(hcd->bandwidth_mutex);
3623
3624 return ret;
3625}
3626EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3627
3628/*
3629 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3630 * xHCI host policy may prevent U1 or U2 from being enabled.
3631 *
3632 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3633 * until the lpm_disable_count drops to zero. Caller must own the
3634 * bandwidth_mutex.
3635 */
3636void usb_enable_lpm(struct usb_device *udev)
3637{
3638 struct usb_hcd *hcd;
3639
3640 if (!udev || !udev->parent ||
3641 udev->speed != USB_SPEED_SUPER ||
3642 !udev->lpm_capable)
3643 return;
3644
3645 udev->lpm_disable_count--;
3646 hcd = bus_to_hcd(udev->bus);
3647 /* Double check that we can both enable and disable LPM.
3648 * Device must be configured to accept set feature U1/U2 timeout.
3649 */
3650 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3651 !hcd->driver->disable_usb3_lpm_timeout)
3652 return;
3653
3654 if (udev->lpm_disable_count > 0)
3655 return;
3656
3657 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3658 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3659}
3660EXPORT_SYMBOL_GPL(usb_enable_lpm);
3661
3662/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3663void usb_unlocked_enable_lpm(struct usb_device *udev)
3664{
3665 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3666
3667 if (!hcd)
3668 return;
3669
3670 mutex_lock(hcd->bandwidth_mutex);
3671 usb_enable_lpm(udev);
3672 mutex_unlock(hcd->bandwidth_mutex);
3673}
3674EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3675
3676
Alan Sternd388dab2006-07-01 22:14:24 -04003677#else /* CONFIG_PM */
3678
Alan Sternf07600c2007-05-30 15:38:16 -04003679#define hub_suspend NULL
3680#define hub_resume NULL
3681#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003682
3683int usb_disable_lpm(struct usb_device *udev)
3684{
3685 return 0;
3686}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003687EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003688
3689void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003690EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003691
3692int usb_unlocked_disable_lpm(struct usb_device *udev)
3693{
3694 return 0;
3695}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003696EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003697
3698void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003699EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07003700
3701int usb_disable_ltm(struct usb_device *udev)
3702{
3703 return 0;
3704}
3705EXPORT_SYMBOL_GPL(usb_disable_ltm);
3706
3707void usb_enable_ltm(struct usb_device *udev) { }
3708EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternd388dab2006-07-01 22:14:24 -04003709#endif
3710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
3712/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3713 *
3714 * Between connect detection and reset signaling there must be a delay
3715 * of 100ms at least for debounce and power-settling. The corresponding
3716 * timer shall restart whenever the downstream port detects a disconnect.
3717 *
3718 * Apparently there are some bluetooth and irda-dongles and a number of
3719 * low-speed devices for which this debounce period may last over a second.
3720 * Not covered by the spec - but easy to deal with.
3721 *
3722 * This implementation uses a 1500ms total debounce timeout; if the
3723 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3724 * every 25ms for transient disconnects. When the port status has been
3725 * unchanged for 100ms it returns the port status.
3726 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727static int hub_port_debounce(struct usb_hub *hub, int port1)
3728{
3729 int ret;
3730 int total_time, stable_time = 0;
3731 u16 portchange, portstatus;
3732 unsigned connection = 0xffff;
3733
3734 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3735 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3736 if (ret < 0)
3737 return ret;
3738
3739 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3740 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3741 stable_time += HUB_DEBOUNCE_STEP;
3742 if (stable_time >= HUB_DEBOUNCE_STABLE)
3743 break;
3744 } else {
3745 stable_time = 0;
3746 connection = portstatus & USB_PORT_STAT_CONNECTION;
3747 }
3748
3749 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3750 clear_port_feature(hub->hdev, port1,
3751 USB_PORT_FEAT_C_CONNECTION);
3752 }
3753
3754 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3755 break;
3756 msleep(HUB_DEBOUNCE_STEP);
3757 }
3758
3759 dev_dbg (hub->intfdev,
3760 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3761 port1, total_time, stable_time, portstatus);
3762
3763 if (stable_time < HUB_DEBOUNCE_STABLE)
3764 return -ETIMEDOUT;
3765 return portstatus;
3766}
3767
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003768void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
Alan Sternddeac4e2009-01-15 17:03:33 -05003770 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3771 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003772 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003774EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
3776#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3777#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3778
Alan Stern4326ed02007-07-30 17:08:43 -04003779static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780{
3781 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003782 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
Sarah Sharpc6515272009-04-27 19:57:26 -07003784 /*
3785 * The host controller will choose the device address,
3786 * instead of the core having chosen it earlier
3787 */
3788 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 return -EINVAL;
3790 if (udev->state == USB_STATE_ADDRESS)
3791 return 0;
3792 if (udev->state != USB_STATE_DEFAULT)
3793 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003794 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003795 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003796 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003797 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3798 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3799 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003801 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003802 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003804 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 }
3806 return retval;
3807}
3808
3809/* Reset device, (re)assign address, get device descriptor.
3810 * Device connection must be stable, no more debouncing needed.
3811 * Returns device in USB_STATE_ADDRESS, except on error.
3812 *
3813 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003814 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 * newly detected device that is not accessible through any global
3816 * pointers, it's not necessary to lock the device.
3817 */
3818static int
3819hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3820 int retry_counter)
3821{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003822 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
3824 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003825 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 int i, j, retval;
3827 unsigned delay = HUB_SHORT_RESET_TIME;
3828 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003829 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003830 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
3832 /* root hub ports have a slightly longer reset period
3833 * (from USB 2.0 spec, section 7.1.7.5)
3834 */
3835 if (!hdev->parent) {
3836 delay = HUB_ROOT_RESET_TIME;
3837 if (port1 == hdev->bus->otg_port)
3838 hdev->bus->b_hnp_enable = 0;
3839 }
3840
3841 /* Some low speed devices have problems with the quick delay, so */
3842 /* be a bit pessimistic with those devices. RHbug #23670 */
3843 if (oldspeed == USB_SPEED_LOW)
3844 delay = HUB_LONG_RESET_TIME;
3845
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003846 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
Luben Tuikov07194ab2011-02-11 11:33:10 -08003848 /* Reset the device; full speed may morph to high speed */
3849 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003850 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003851 if (retval < 0) /* error or disconnect */
3852 goto fail;
3853 /* success, speed is known */
3854
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 retval = -ENODEV;
3856
3857 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3858 dev_dbg(&udev->dev, "device reset changed speed!\n");
3859 goto fail;
3860 }
3861 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3864 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003865 * For Wireless USB devices, ep0 max packet is always 512 (tho
3866 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 */
3868 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003869 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003870 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003871 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003872 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003874 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 break;
3876 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3877 /* to determine the ep0 maxpacket size, try to read
3878 * the device descriptor to get bMaxPacketSize0 and
3879 * then correct our initial guess.
3880 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003881 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 break;
3883 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003884 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 break;
3886 default:
3887 goto fail;
3888 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003889
3890 if (udev->speed == USB_SPEED_WIRELESS)
3891 speed = "variable speed Wireless";
3892 else
3893 speed = usb_speed_string(udev->speed);
3894
Sarah Sharpc6515272009-04-27 19:57:26 -07003895 if (udev->speed != USB_SPEED_SUPER)
3896 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003897 "%s %s USB device number %d using %s\n",
3898 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003899 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
3901 /* Set up TT records, if needed */
3902 if (hdev->tt) {
3903 udev->tt = hdev->tt;
3904 udev->ttport = hdev->ttport;
3905 } else if (udev->speed != USB_SPEED_HIGH
3906 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003907 if (!hub->tt.hub) {
3908 dev_err(&udev->dev, "parent hub has no TT\n");
3909 retval = -EINVAL;
3910 goto fail;
3911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 udev->tt = &hub->tt;
3913 udev->ttport = port1;
3914 }
3915
3916 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3917 * Because device hardware and firmware is sometimes buggy in
3918 * this area, and this is how Linux has done it for ages.
3919 * Change it cautiously.
3920 *
3921 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3922 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3923 * so it may help with some non-standards-compliant devices.
3924 * Otherwise we start with SET_ADDRESS and then try to read the
3925 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3926 * value.
3927 */
3928 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07003929 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 struct usb_device_descriptor *buf;
3931 int r = 0;
3932
3933#define GET_DESCRIPTOR_BUFSIZE 64
3934 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3935 if (!buf) {
3936 retval = -ENOMEM;
3937 continue;
3938 }
3939
Alan Sternb89ee192007-05-11 10:19:04 -04003940 /* Retry on all errors; some devices are flakey.
3941 * 255 is for WUSB devices, we actually need to use
3942 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 */
3944 for (j = 0; j < 3; ++j) {
3945 buf->bMaxPacketSize0 = 0;
3946 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3947 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3948 USB_DT_DEVICE << 8, 0,
3949 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02003950 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003952 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 if (buf->bDescriptorType ==
3954 USB_DT_DEVICE) {
3955 r = 0;
3956 break;
3957 }
3958 /* FALL THROUGH */
3959 default:
3960 if (r == 0)
3961 r = -EPROTO;
3962 break;
3963 }
3964 if (r == 0)
3965 break;
3966 }
3967 udev->descriptor.bMaxPacketSize0 =
3968 buf->bMaxPacketSize0;
3969 kfree(buf);
3970
Andiry Xu75d7cf72011-09-13 16:41:11 -07003971 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 if (retval < 0) /* error or disconnect */
3973 goto fail;
3974 if (oldspeed != udev->speed) {
3975 dev_dbg(&udev->dev,
3976 "device reset changed speed!\n");
3977 retval = -ENODEV;
3978 goto fail;
3979 }
3980 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003981 dev_err(&udev->dev,
3982 "device descriptor read/64, error %d\n",
3983 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 retval = -EMSGSIZE;
3985 continue;
3986 }
3987#undef GET_DESCRIPTOR_BUFSIZE
3988 }
3989
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003990 /*
3991 * If device is WUSB, we already assigned an
3992 * unauthorized address in the Connect Ack sequence;
3993 * authorization will assign the final address.
3994 */
Sarah Sharpc6515272009-04-27 19:57:26 -07003995 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003996 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3997 retval = hub_set_address(udev, devnum);
3998 if (retval >= 0)
3999 break;
4000 msleep(200);
4001 }
4002 if (retval < 0) {
4003 dev_err(&udev->dev,
4004 "device not accepting address %d, error %d\n",
4005 devnum, retval);
4006 goto fail;
4007 }
Sarah Sharpc6515272009-04-27 19:57:26 -07004008 if (udev->speed == USB_SPEED_SUPER) {
4009 devnum = udev->devnum;
4010 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05004011 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07004012 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05004013 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07004014 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004015
4016 /* cope with hardware quirkiness:
4017 * - let SET_ADDRESS settle, some device hardware wants it
4018 * - read ep0 maxpacket even for high and low speed,
4019 */
4020 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07004021 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
4025 retval = usb_get_device_descriptor(udev, 8);
4026 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004027 dev_err(&udev->dev,
4028 "device descriptor read/8, error %d\n",
4029 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 if (retval >= 0)
4031 retval = -EMSGSIZE;
4032 } else {
4033 retval = 0;
4034 break;
4035 }
4036 }
4037 if (retval)
4038 goto fail;
4039
Peter Chenb76baa82012-11-09 09:44:43 +08004040 if (hcd->phy && !hdev->parent)
Peter Chenac965112012-11-09 09:44:44 +08004041 usb_phy_notify_connect(hcd->phy, udev->speed);
Peter Chenb76baa82012-11-09 09:44:43 +08004042
Elric Fud8aec3d2012-03-26 21:16:02 +08004043 /*
4044 * Some superspeed devices have finished the link training process
4045 * and attached to a superspeed hub port, but the device descriptor
4046 * got from those devices show they aren't superspeed devices. Warm
4047 * reset the port attached by the devices can fix them.
4048 */
4049 if ((udev->speed == USB_SPEED_SUPER) &&
4050 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4051 dev_err(&udev->dev, "got a wrong device descriptor, "
4052 "warm reset device\n");
4053 hub_port_reset(hub, port1, udev,
4054 HUB_BH_RESET_TIME, true);
4055 retval = -EINVAL;
4056 goto fail;
4057 }
4058
Sarah Sharp6b403b02009-04-27 19:54:10 -07004059 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
4060 udev->speed == USB_SPEED_SUPER)
4061 i = 512;
4062 else
4063 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07004064 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04004065 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04004067 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 retval = -EMSGSIZE;
4069 goto fail;
4070 }
Alan Stern56626a72010-10-14 15:25:21 -04004071 if (udev->speed == USB_SPEED_FULL)
4072 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4073 else
4074 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004076 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 }
4078
4079 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4080 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004081 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4082 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 if (retval >= 0)
4084 retval = -ENOMSG;
4085 goto fail;
4086 }
4087
Andiry Xu1ff4df52011-09-23 14:19:48 -07004088 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4089 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004090 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004091 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004092 usb_set_lpm_parameters(udev);
4093 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004094 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004095
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004097 /* notify HCD that we have a device connected and addressed */
4098 if (hcd->driver->update_device)
4099 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004101 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004103 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004104 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01004105 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 return retval;
4107}
4108
4109static void
4110check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4111{
4112 struct usb_qualifier_descriptor *qual;
4113 int status;
4114
Christoph Lametere94b1762006-12-06 20:33:17 -08004115 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 if (qual == NULL)
4117 return;
4118
4119 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4120 qual, sizeof *qual);
4121 if (status == sizeof *qual) {
4122 dev_info(&udev->dev, "not running at top speed; "
4123 "connect to a high speed hub\n");
4124 /* hub LEDs are probably harder to miss than syslog */
4125 if (hub->has_indicators) {
4126 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004127 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 }
4129 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004130 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131}
4132
4133static unsigned
4134hub_power_remaining (struct usb_hub *hub)
4135{
4136 struct usb_device *hdev = hub->hdev;
4137 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004138 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
Alan Stern55c52712005-11-23 12:03:12 -05004140 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 return 0;
4142
Alan Stern55c52712005-11-23 12:03:12 -05004143 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4144 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c792012-09-05 13:44:32 +08004145 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern55c52712005-11-23 12:03:12 -05004146 int delta;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004147 unsigned unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
4149 if (!udev)
4150 continue;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004151 if (hub_is_superspeed(udev))
4152 unit_load = 150;
4153 else
4154 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004156 /*
4157 * Unconfigured devices may not use more than one unit load,
4158 * or 8mA for OTG ports
4159 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 if (udev->actconfig)
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +01004161 delta = usb_get_max_power(udev, udev->actconfig);
Alan Stern55c52712005-11-23 12:03:12 -05004162 else if (port1 != udev->bus->otg_port || hdev->parent)
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004163 delta = unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 else
Alan Stern55c52712005-11-23 12:03:12 -05004165 delta = 8;
4166 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004167 dev_warn(&udev->dev,
4168 "%dmA is over %umA budget for port %d!\n",
4169 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 remaining -= delta;
4171 }
4172 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004173 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4174 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 remaining = 0;
4176 }
4177 return remaining;
4178}
4179
4180/* Handle physical or logical connection change events.
4181 * This routine is called when:
4182 * a port connection-change occurs;
4183 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08004184 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 * a firmware download)
4186 * caller already locked the hub
4187 */
4188static void hub_port_connect_change(struct usb_hub *hub, int port1,
4189 u16 portstatus, u16 portchange)
4190{
4191 struct usb_device *hdev = hub->hdev;
4192 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05304193 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04004194 unsigned wHubCharacteristics =
4195 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04004196 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 int status, i;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004198 unsigned unit_load;
Alan Stern24618b02008-04-28 11:06:28 -04004199
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 dev_dbg (hub_dev,
4201 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08004202 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
4204 if (hub->has_indicators) {
4205 set_port_led(hub, port1, HUB_LED_AUTO);
4206 hub->indicator[port1-1] = INDICATOR_AUTO;
4207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
4209#ifdef CONFIG_USB_OTG
4210 /* during HNP, don't repeat the debounce */
4211 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04004212 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4213 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214#endif
4215
Alan Stern8808f002008-04-28 11:06:55 -04004216 /* Try to resuscitate an existing device */
Lan Tianyuff823c792012-09-05 13:44:32 +08004217 udev = hub->ports[port1 - 1]->child;
Alan Stern8808f002008-04-28 11:06:55 -04004218 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4219 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04004220 usb_lock_device(udev);
4221 if (portstatus & USB_PORT_STAT_ENABLE) {
4222 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004223
4224#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04004225 } else if (udev->state == USB_STATE_SUSPENDED &&
4226 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004227 /* For a suspended device, treat this as a
4228 * remote wakeup event.
4229 */
Alan Stern0534d462010-01-08 12:56:30 -05004230 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004231#endif
4232
4233 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004234 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004235 }
4236 usb_unlock_device(udev);
4237
4238 if (status == 0) {
4239 clear_bit(port1, hub->change_bits);
4240 return;
4241 }
4242 }
4243
Alan Stern24618b02008-04-28 11:06:28 -04004244 /* Disconnect any existing devices under this port */
Peter Chenb76baa82012-11-09 09:44:43 +08004245 if (udev) {
4246 if (hcd->phy && !hdev->parent &&
4247 !(portstatus & USB_PORT_STAT_CONNECTION))
Peter Chenac965112012-11-09 09:44:44 +08004248 usb_phy_notify_disconnect(hcd->phy, udev->speed);
Lan Tianyuff823c792012-09-05 13:44:32 +08004249 usb_disconnect(&hub->ports[port1 - 1]->child);
Peter Chenb76baa82012-11-09 09:44:43 +08004250 }
Alan Stern24618b02008-04-28 11:06:28 -04004251 clear_bit(port1, hub->change_bits);
4252
Alan Stern253e0572009-10-27 15:20:13 -04004253 /* We can forget about a "removed" device when there's a physical
4254 * disconnect or the connect status changes.
4255 */
4256 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4257 (portchange & USB_PORT_STAT_C_CONNECTION))
4258 clear_bit(port1, hub->removed_bits);
4259
Alan Stern5257d972008-09-22 14:43:08 -04004260 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4261 USB_PORT_STAT_C_ENABLE)) {
4262 status = hub_port_debounce(hub, port1);
4263 if (status < 0) {
4264 if (printk_ratelimit())
4265 dev_err(hub_dev, "connect-debounce failed, "
4266 "port %d disabled\n", port1);
4267 portstatus &= ~USB_PORT_STAT_CONNECTION;
4268 } else {
4269 portstatus = status;
4270 }
4271 }
4272
Alan Stern253e0572009-10-27 15:20:13 -04004273 /* Return now if debouncing failed or nothing is connected or
4274 * the device was "removed".
4275 */
4276 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4277 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
4279 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07004280 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08004281 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004283
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 if (portstatus & USB_PORT_STAT_ENABLE)
4285 goto done;
4286 return;
4287 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004288 if (hub_is_superspeed(hub->hdev))
4289 unit_load = 150;
4290 else
4291 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
4295 /* reallocate for each attempt, since references
4296 * to the previous one can escape in various ways
4297 */
4298 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4299 if (!udev) {
4300 dev_err (hub_dev,
4301 "couldn't allocate port %d usb_device\n",
4302 port1);
4303 goto done;
4304 }
4305
4306 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05004307 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004308 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004309 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004310
Sarah Sharp131dec32010-12-06 21:00:19 -08004311 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4312 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004313 udev->speed = USB_SPEED_SUPER;
4314 else
4315 udev->speed = USB_SPEED_UNKNOWN;
4316
Alan Stern3b29b682011-02-22 09:53:41 -05004317 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004318 if (udev->devnum <= 0) {
4319 status = -ENOTCONN; /* Don't retry */
4320 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004321 }
4322
Sarah Sharpe7b77172009-04-27 19:54:26 -07004323 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 status = hub_port_init(hub, udev, port1, i);
4325 if (status < 0)
4326 goto loop;
4327
Phil Dibowitz93362a82010-07-22 00:05:01 +02004328 usb_detect_quirks(udev);
4329 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4330 msleep(1000);
4331
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 /* consecutive bus-powered hubs aren't reliable; they can
4333 * violate the voltage drop budget. if the new child has
4334 * a "powered" LED, users should notice we didn't enable it
4335 * (without reading syslog), even without per-port LEDs
4336 * on the parent.
4337 */
4338 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004339 && udev->bus_mA <= unit_load) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 u16 devstat;
4341
4342 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4343 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05004344 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 dev_dbg(&udev->dev, "get status %d ?\n", status);
4346 goto loop_disable;
4347 }
Alan Stern55c52712005-11-23 12:03:12 -05004348 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4350 dev_err(&udev->dev,
4351 "can't connect bus-powered hub "
4352 "to this port\n");
4353 if (hub->has_indicators) {
4354 hub->indicator[port1-1] =
4355 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004356 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 }
4358 status = -ENOTCONN; /* Don't retry */
4359 goto loop_disable;
4360 }
4361 }
4362
4363 /* check for devices running slower than they could */
4364 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4365 && udev->speed == USB_SPEED_FULL
4366 && highspeed_hubs != 0)
4367 check_highspeed (hub, udev, port1);
4368
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004369 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 * udev becomes globally accessible, although presumably
4371 * no one will look at it until hdev is unlocked.
4372 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 status = 0;
4374
4375 /* We mustn't add new devices if the parent hub has
4376 * been disconnected; we would race with the
4377 * recursively_mark_NOTATTACHED() routine.
4378 */
4379 spin_lock_irq(&device_state_lock);
4380 if (hdev->state == USB_STATE_NOTATTACHED)
4381 status = -ENOTCONN;
4382 else
Lan Tianyuff823c792012-09-05 13:44:32 +08004383 hub->ports[port1 - 1]->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 spin_unlock_irq(&device_state_lock);
4385
4386 /* Run it through the hoops (find a driver, etc) */
4387 if (!status) {
4388 status = usb_new_device(udev);
4389 if (status) {
4390 spin_lock_irq(&device_state_lock);
Lan Tianyuff823c792012-09-05 13:44:32 +08004391 hub->ports[port1 - 1]->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 spin_unlock_irq(&device_state_lock);
4393 }
4394 }
4395
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 if (status)
4397 goto loop_disable;
4398
4399 status = hub_power_remaining(hub);
4400 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05004401 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402
4403 return;
4404
4405loop_disable:
4406 hub_port_disable(hub, port1, 1);
4407loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004408 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004409 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004410 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004412 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 break;
4414 }
Alan Stern3a311552008-05-20 16:58:29 -04004415 if (hub->hdev->parent ||
4416 !hcd->driver->port_handed_over ||
4417 !(hcd->driver->port_handed_over)(hcd, port1))
4418 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4419 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
4421done:
4422 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304423 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4424 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425}
4426
Sarah Sharp714b07b2012-01-24 13:53:18 -08004427/* Returns 1 if there was a remote wakeup and a connect status change. */
4428static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004429 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004430{
4431 struct usb_device *hdev;
4432 struct usb_device *udev;
4433 int connect_change = 0;
4434 int ret;
4435
4436 hdev = hub->hdev;
Lan Tianyuff823c792012-09-05 13:44:32 +08004437 udev = hub->ports[port - 1]->child;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004438 if (!hub_is_superspeed(hdev)) {
4439 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4440 return 0;
4441 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4442 } else {
4443 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004444 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4445 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004446 return 0;
4447 }
4448
Sarah Sharp714b07b2012-01-24 13:53:18 -08004449 if (udev) {
4450 /* TRSMRCY = 10 msec */
4451 msleep(10);
4452
4453 usb_lock_device(udev);
4454 ret = usb_remote_wakeup(udev);
4455 usb_unlock_device(udev);
4456 if (ret < 0)
4457 connect_change = 1;
4458 } else {
4459 ret = -ENODEV;
4460 hub_port_disable(hub, port, 1);
4461 }
4462 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4463 port, ret);
4464 return connect_change;
4465}
4466
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467static void hub_events(void)
4468{
4469 struct list_head *tmp;
4470 struct usb_device *hdev;
4471 struct usb_interface *intf;
4472 struct usb_hub *hub;
4473 struct device *hub_dev;
4474 u16 hubstatus;
4475 u16 hubchange;
4476 u16 portstatus;
4477 u16 portchange;
4478 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004479 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
4481 /*
4482 * We restart the list every time to avoid a deadlock with
4483 * deleting hubs downstream from this one. This should be
4484 * safe since we delete the hub from the event list.
4485 * Not the most efficient, but avoids deadlocks.
4486 */
4487 while (1) {
4488
4489 /* Grab the first entry at the beginning of the list */
4490 spin_lock_irq(&hub_event_lock);
4491 if (list_empty(&hub_event_list)) {
4492 spin_unlock_irq(&hub_event_lock);
4493 break;
4494 }
4495
4496 tmp = hub_event_list.next;
4497 list_del_init(tmp);
4498
4499 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004500 kref_get(&hub->kref);
4501 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Alan Sterne8054852007-05-04 11:55:11 -04004503 hdev = hub->hdev;
4504 hub_dev = hub->intfdev;
4505 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004506 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 hdev->state, hub->descriptor
4508 ? hub->descriptor->bNbrPorts
4509 : 0,
4510 /* NOTE: expects max 15 ports... */
4511 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004512 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 /* Lock the device, then check to see if we were
4515 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004516 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004517 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004518 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
4520 /* If the hub has died, clean up after it */
4521 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004522 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004523 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 goto loop;
4525 }
4526
Alan Stern40f122f2006-11-09 14:44:33 -05004527 /* Autoresume */
4528 ret = usb_autopm_get_interface(intf);
4529 if (ret) {
4530 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004532 }
4533
4534 /* If this is an inactive hub, do nothing */
4535 if (hub->quiescing)
4536 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
4538 if (hub->error) {
4539 dev_dbg (hub_dev, "resetting for error %d\n",
4540 hub->error);
4541
Ming Lei742120c2008-06-18 22:00:29 +08004542 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 if (ret) {
4544 dev_dbg (hub_dev,
4545 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004546 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 }
4548
4549 hub->nerrors = 0;
4550 hub->error = 0;
4551 }
4552
4553 /* deal with port status changes */
4554 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4555 if (test_bit(i, hub->busy_bits))
4556 continue;
4557 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004558 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004560 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 continue;
4562
4563 ret = hub_port_status(hub, i,
4564 &portstatus, &portchange);
4565 if (ret < 0)
4566 continue;
4567
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4569 clear_port_feature(hdev, i,
4570 USB_PORT_FEAT_C_CONNECTION);
4571 connect_change = 1;
4572 }
4573
4574 if (portchange & USB_PORT_STAT_C_ENABLE) {
4575 if (!connect_change)
4576 dev_dbg (hub_dev,
4577 "port %d enable change, "
4578 "status %08x\n",
4579 i, portstatus);
4580 clear_port_feature(hdev, i,
4581 USB_PORT_FEAT_C_ENABLE);
4582
4583 /*
4584 * EM interference sometimes causes badly
4585 * shielded USB devices to be shutdown by
4586 * the hub, this hack enables them again.
4587 * Works at least with mouse driver.
4588 */
4589 if (!(portstatus & USB_PORT_STAT_ENABLE)
4590 && !connect_change
Lan Tianyuff823c792012-09-05 13:44:32 +08004591 && hub->ports[i - 1]->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 dev_err (hub_dev,
4593 "port %i "
4594 "disabled by hub (EMI?), "
4595 "re-enabling...\n",
4596 i);
4597 connect_change = 1;
4598 }
4599 }
4600
Sarah Sharp72937e12012-01-24 11:46:50 -08004601 if (hub_handle_remote_wakeup(hub, i,
4602 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004603 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004604
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004606 u16 status = 0;
4607 u16 unused;
4608
4609 dev_dbg(hub_dev, "over-current change on port "
4610 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 clear_port_feature(hdev, i,
4612 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004613 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004614 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004615 hub_port_status(hub, i, &status, &unused);
4616 if (status & USB_PORT_STAT_OVERCURRENT)
4617 dev_err(hub_dev, "over-current "
4618 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 }
4620
4621 if (portchange & USB_PORT_STAT_C_RESET) {
4622 dev_dbg (hub_dev,
4623 "reset change on port %d\n",
4624 i);
4625 clear_port_feature(hdev, i,
4626 USB_PORT_FEAT_C_RESET);
4627 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004628 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4629 hub_is_superspeed(hub->hdev)) {
4630 dev_dbg(hub_dev,
4631 "warm reset change on port %d\n",
4632 i);
4633 clear_port_feature(hdev, i,
4634 USB_PORT_FEAT_C_BH_PORT_RESET);
4635 }
John Youndbe79bb2001-09-17 00:00:00 -07004636 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4637 clear_port_feature(hub->hdev, i,
4638 USB_PORT_FEAT_C_PORT_LINK_STATE);
4639 }
4640 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4641 dev_warn(hub_dev,
4642 "config error on port %d\n",
4643 i);
4644 clear_port_feature(hub->hdev, i,
4645 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
Andiry Xu5e467f62011-04-27 18:07:54 +08004648 /* Warm reset a USB3 protocol port if it's in
4649 * SS.Inactive state.
4650 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02004651 if (hub_port_warm_reset_required(hub, portstatus)) {
Sarah Sharp65bdac52012-11-14 17:58:04 -08004652 int status;
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07004653 struct usb_device *udev =
4654 hub->ports[i - 1]->child;
Sarah Sharp65bdac52012-11-14 17:58:04 -08004655
Andiry Xu5e467f62011-04-27 18:07:54 +08004656 dev_dbg(hub_dev, "warm reset port %d\n", i);
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07004657 if (!udev) {
4658 status = hub_port_reset(hub, i,
4659 NULL, HUB_BH_RESET_TIME,
4660 true);
4661 if (status < 0)
4662 hub_port_disable(hub, i, 1);
4663 } else {
4664 usb_lock_device(udev);
4665 status = usb_reset_device(udev);
4666 usb_unlock_device(udev);
4667 }
Sarah Sharp65bdac52012-11-14 17:58:04 -08004668 connect_change = 0;
Andiry Xu5e467f62011-04-27 18:07:54 +08004669 }
4670
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 if (connect_change)
4672 hub_port_connect_change(hub, i,
4673 portstatus, portchange);
4674 } /* end for i */
4675
4676 /* deal with hub status changes */
4677 if (test_and_clear_bit(0, hub->event_bits) == 0)
4678 ; /* do nothing */
4679 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4680 dev_err (hub_dev, "get_hub_status failed\n");
4681 else {
4682 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4683 dev_dbg (hub_dev, "power change\n");
4684 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004685 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4686 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004687 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004688 else
4689 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 }
4691 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004692 u16 status = 0;
4693 u16 unused;
4694
4695 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004697 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004698 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004699 hub_hub_status(hub, &status, &unused);
4700 if (status & HUB_STATUS_OVERCURRENT)
4701 dev_err(hub_dev, "over-current "
4702 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 }
4704 }
4705
Alan Stern8e4ceb32009-12-07 13:01:37 -05004706 loop_autopm:
4707 /* Balance the usb_autopm_get_interface() above */
4708 usb_autopm_put_interface_no_suspend(intf);
4709 loop:
4710 /* Balance the usb_autopm_get_interface_no_resume() in
4711 * kick_khubd() and allow autosuspend.
4712 */
4713 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004714 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004716 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
4718 } /* end while (1) */
4719}
4720
4721static int hub_thread(void *__unused)
4722{
Alan Stern3bb1af52008-03-03 15:15:36 -05004723 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4724 * port handover. Otherwise it might see that a full-speed device
4725 * was gone before the EHCI controller had handed its port over to
4726 * the companion full-speed controller.
4727 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004728 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004729
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 do {
4731 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004732 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004733 !list_empty(&hub_event_list) ||
4734 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004735 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004737 pr_debug("%s: khubd exiting\n", usbcore_name);
4738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739}
4740
Németh Márton1e927d92010-01-10 15:34:53 +01004741static const struct usb_device_id hub_id_table[] = {
Ming Leie6f30de2012-10-24 11:59:24 +08004742 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
4743 | USB_DEVICE_ID_MATCH_INT_CLASS,
4744 .idVendor = USB_VENDOR_GENESYS_LOGIC,
4745 .bInterfaceClass = USB_CLASS_HUB,
4746 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4748 .bDeviceClass = USB_CLASS_HUB},
4749 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4750 .bInterfaceClass = USB_CLASS_HUB},
4751 { } /* Terminating entry */
4752};
4753
4754MODULE_DEVICE_TABLE (usb, hub_id_table);
4755
4756static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 .name = "hub",
4758 .probe = hub_probe,
4759 .disconnect = hub_disconnect,
4760 .suspend = hub_suspend,
4761 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004762 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004763 .pre_reset = hub_pre_reset,
4764 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004765 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004767 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768};
4769
4770int usb_hub_init(void)
4771{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 if (usb_register(&hub_driver) < 0) {
4773 printk(KERN_ERR "%s: can't register hub driver\n",
4774 usbcore_name);
4775 return -1;
4776 }
4777
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004778 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4779 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
4782 /* Fall through if kernel_thread failed */
4783 usb_deregister(&hub_driver);
4784 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4785
4786 return -1;
4787}
4788
4789void usb_hub_cleanup(void)
4790{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004791 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
4793 /*
4794 * Hub resources are freed for us by usb_deregister. It calls
4795 * usb_driver_purge on every device which in turn calls that
4796 * devices disconnect function if it is using this driver.
4797 * The hub_disconnect function takes care of releasing the
4798 * individual hub resources. -greg
4799 */
4800 usb_deregister(&hub_driver);
4801} /* usb_hub_cleanup() */
4802
Alan Sterneb764c42008-03-03 15:16:04 -05004803static int descriptors_changed(struct usb_device *udev,
4804 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805{
Alan Sterneb764c42008-03-03 15:16:04 -05004806 int changed = 0;
4807 unsigned index;
4808 unsigned serial_len = 0;
4809 unsigned len;
4810 unsigned old_length;
4811 int length;
4812 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
Alan Sterneb764c42008-03-03 15:16:04 -05004814 if (memcmp(&udev->descriptor, old_device_descriptor,
4815 sizeof(*old_device_descriptor)) != 0)
4816 return 1;
4817
4818 /* Since the idVendor, idProduct, and bcdDevice values in the
4819 * device descriptor haven't changed, we will assume the
4820 * Manufacturer and Product strings haven't changed either.
4821 * But the SerialNumber string could be different (e.g., a
4822 * different flash card of the same brand).
4823 */
4824 if (udev->serial)
4825 serial_len = strlen(udev->serial) + 1;
4826
4827 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004829 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4830 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 }
Alan Sterneb764c42008-03-03 15:16:04 -05004832
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004833 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 if (buf == NULL) {
4835 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4836 /* assume the worst */
4837 return 1;
4838 }
4839 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004840 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4842 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004843 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 dev_dbg(&udev->dev, "config index %d, error %d\n",
4845 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004846 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 break;
4848 }
4849 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4850 != 0) {
4851 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004852 index,
4853 ((struct usb_config_descriptor *) buf)->
4854 bConfigurationValue);
4855 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 break;
4857 }
4858 }
Alan Sterneb764c42008-03-03 15:16:04 -05004859
4860 if (!changed && serial_len) {
4861 length = usb_string(udev, udev->descriptor.iSerialNumber,
4862 buf, serial_len);
4863 if (length + 1 != serial_len) {
4864 dev_dbg(&udev->dev, "serial string error %d\n",
4865 length);
4866 changed = 1;
4867 } else if (memcmp(buf, udev->serial, length) != 0) {
4868 dev_dbg(&udev->dev, "serial string changed\n");
4869 changed = 1;
4870 }
4871 }
4872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004874 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875}
4876
4877/**
Ming Lei742120c2008-06-18 22:00:29 +08004878 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4880 *
Alan Stern79efa092006-06-01 13:33:42 -04004881 * WARNING - don't use this routine to reset a composite device
4882 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004883 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 *
4885 * Do a port reset, reassign the device's address, and establish its
4886 * former operating configuration. If the reset fails, or the device's
4887 * descriptors change from their values before the reset, or the original
4888 * configuration and altsettings cannot be restored, a flag will be set
4889 * telling khubd to pretend the device has been disconnected and then
4890 * re-connected. All drivers will be unbound, and the device will be
4891 * re-enumerated and probed all over again.
4892 *
4893 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4894 * flagged for logical disconnection, or some other negative error code
4895 * if the reset wasn't even attempted.
4896 *
4897 * The caller must own the device lock. For example, it's safe to use
4898 * this from a driver probe() routine after downloading new firmware.
4899 * For calls that might not occur during probe(), drivers should lock
4900 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004901 *
4902 * Locking exception: This routine may also be called from within an
4903 * autoresume handler. Such usage won't conflict with other tasks
4904 * holding the device lock because these tasks should always call
4905 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 */
Ming Lei742120c2008-06-18 22:00:29 +08004907static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908{
4909 struct usb_device *parent_hdev = udev->parent;
4910 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004911 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004913 int i, ret = 0;
4914 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
4916 if (udev->state == USB_STATE_NOTATTACHED ||
4917 udev->state == USB_STATE_SUSPENDED) {
4918 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4919 udev->state);
4920 return -EINVAL;
4921 }
4922
4923 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004924 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004925 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 return -EISDIR;
4927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 parent_hub = hdev_to_hub(parent_hdev);
4929
Sarah Sharpf74631e2012-06-25 12:08:08 -07004930 /* Disable LPM and LTM while we reset the device and reinstall the alt
4931 * settings. Device-initiated LPM settings, and system exit latency
4932 * settings are cleared when the device is reset, so we have to set
4933 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004934 */
4935 ret = usb_unlocked_disable_lpm(udev);
4936 if (ret) {
4937 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4938 goto re_enumerate;
4939 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07004940 ret = usb_disable_ltm(udev);
4941 if (ret) {
4942 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4943 __func__);
4944 goto re_enumerate;
4945 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004946
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 set_bit(port1, parent_hub->busy_bits);
4948 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4949
4950 /* ep0 maxpacket size may change; let the HCD know about it.
4951 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004952 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004954 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 break;
4956 }
4957 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 if (ret < 0)
4960 goto re_enumerate;
4961
4962 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004963 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 dev_info(&udev->dev, "device firmware changed\n");
4965 udev->descriptor = descriptor; /* for disconnect() calls */
4966 goto re_enumerate;
4967 }
Alan Stern4fe03872009-02-26 10:21:02 -05004968
4969 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 if (!udev->actconfig)
4971 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004972
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004973 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004974 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4975 if (ret < 0) {
4976 dev_warn(&udev->dev,
4977 "Busted HC? Not enough HCD resources for "
4978 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004979 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004980 goto re_enumerate;
4981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4983 USB_REQ_SET_CONFIGURATION, 0,
4984 udev->actconfig->desc.bConfigurationValue, 0,
4985 NULL, 0, USB_CTRL_SET_TIMEOUT);
4986 if (ret < 0) {
4987 dev_err(&udev->dev,
4988 "can't restore configuration #%d (error=%d)\n",
4989 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004990 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 goto re_enumerate;
4992 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004993 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4995
Alan Stern4fe03872009-02-26 10:21:02 -05004996 /* Put interfaces back into the same altsettings as before.
4997 * Don't bother to send the Set-Interface request for interfaces
4998 * that were already in altsetting 0; besides being unnecessary,
4999 * many devices can't handle it. Instead just reset the host-side
5000 * endpoint state.
5001 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005003 struct usb_host_config *config = udev->actconfig;
5004 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 struct usb_interface_descriptor *desc;
5006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05005008 if (desc->bAlternateSetting == 0) {
5009 usb_disable_interface(udev, intf, true);
5010 usb_enable_interface(udev, intf, true);
5011 ret = 0;
5012 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08005013 /* Let the bandwidth allocation function know that this
5014 * device has been reset, and it will have to use
5015 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005016 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08005017 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05005018 ret = usb_set_interface(udev, desc->bInterfaceNumber,
5019 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08005020 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05005021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 if (ret < 0) {
5023 dev_err(&udev->dev, "failed to restore interface %d "
5024 "altsetting %d (error=%d)\n",
5025 desc->bInterfaceNumber,
5026 desc->bAlternateSetting,
5027 ret);
5028 goto re_enumerate;
5029 }
5030 }
5031
5032done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07005033 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Alan Stern78d9a482008-06-23 16:00:40 -04005034 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07005035 usb_enable_ltm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 return 0;
5037
5038re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005039 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 hub_port_logical_disconnect(parent_hub, port1);
5041 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042}
5043
5044/**
5045 * usb_reset_device - warn interface drivers and perform a USB port reset
5046 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5047 *
Alan Stern79efa092006-06-01 13:33:42 -04005048 * Warns all drivers bound to registered interfaces (using their pre_reset
5049 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08005050 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04005051 *
Alan Stern79efa092006-06-01 13:33:42 -04005052 * Return value is the same as for usb_reset_and_verify_device().
5053 *
5054 * The caller must own the device lock. For example, it's safe to use
5055 * this from a driver probe() routine after downloading new firmware.
5056 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08005057 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04005058 *
5059 * If an interface is currently being probed or disconnected, we assume
5060 * its driver knows how to handle resets. For all other interfaces,
5061 * if the driver doesn't have pre_reset and post_reset methods then
5062 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04005063 */
Ming Lei742120c2008-06-18 22:00:29 +08005064int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04005065{
5066 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05005067 int i;
Alan Stern79efa092006-06-01 13:33:42 -04005068 struct usb_host_config *config = udev->actconfig;
5069
5070 if (udev->state == USB_STATE_NOTATTACHED ||
5071 udev->state == USB_STATE_SUSPENDED) {
5072 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5073 udev->state);
5074 return -EINVAL;
5075 }
5076
Alan Stern645daaa2006-08-30 15:47:02 -04005077 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05005078 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04005079
Alan Stern79efa092006-06-01 13:33:42 -04005080 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005081 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005082 struct usb_interface *cintf = config->interface[i];
5083 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005084 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05005085
5086 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005087 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04005088 if (drv->pre_reset && drv->post_reset)
5089 unbind = (drv->pre_reset)(cintf);
5090 else if (cintf->condition ==
5091 USB_INTERFACE_BOUND)
5092 unbind = 1;
5093 if (unbind)
5094 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005095 }
5096 }
5097 }
5098
Ming Lei742120c2008-06-18 22:00:29 +08005099 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005100
5101 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005102 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005103 struct usb_interface *cintf = config->interface[i];
5104 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005105 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05005106
Alan Stern78d9a482008-06-23 16:00:40 -04005107 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005108 drv = to_usb_driver(cintf->dev.driver);
5109 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04005110 rebind = (drv->post_reset)(cintf);
5111 else if (cintf->condition ==
5112 USB_INTERFACE_BOUND)
5113 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005114 }
Alan Stern6c640942008-10-21 15:40:03 -04005115 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04005116 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005117 }
5118 }
5119
Alan Stern94fcda12006-11-20 11:38:46 -05005120 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005121 return ret;
5122}
Ming Lei742120c2008-06-18 22:00:29 +08005123EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005124
5125
5126/**
5127 * usb_queue_reset_device - Reset a USB device from an atomic context
5128 * @iface: USB interface belonging to the device to reset
5129 *
5130 * This function can be used to reset a USB device from an atomic
5131 * context, where usb_reset_device() won't work (as it blocks).
5132 *
5133 * Doing a reset via this method is functionally equivalent to calling
5134 * usb_reset_device(), except for the fact that it is delayed to a
5135 * workqueue. This means that any drivers bound to other interfaces
5136 * might be unbound, as well as users from usbfs in user space.
5137 *
5138 * Corner cases:
5139 *
5140 * - Scheduling two resets at the same time from two different drivers
5141 * attached to two different interfaces of the same device is
5142 * possible; depending on how the driver attached to each interface
5143 * handles ->pre_reset(), the second reset might happen or not.
5144 *
5145 * - If a driver is unbound and it had a pending reset, the reset will
5146 * be cancelled.
5147 *
5148 * - This function can be called during .probe() or .disconnect()
5149 * times. On return from .disconnect(), any pending resets will be
5150 * cancelled.
5151 *
5152 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5153 * does its own.
5154 *
5155 * NOTE: We don't do any reference count tracking because it is not
5156 * needed. The lifecycle of the work_struct is tied to the
5157 * usb_interface. Before destroying the interface we cancel the
5158 * work_struct, so the fact that work_struct is queued and or
5159 * running means the interface (and thus, the device) exist and
5160 * are referenced.
5161 */
5162void usb_queue_reset_device(struct usb_interface *iface)
5163{
5164 schedule_work(&iface->reset_ws);
5165}
5166EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c792012-09-05 13:44:32 +08005167
5168/**
5169 * usb_hub_find_child - Get the pointer of child device
5170 * attached to the port which is specified by @port1.
5171 * @hdev: USB device belonging to the usb hub
5172 * @port1: port num to indicate which port the child device
5173 * is attached to.
5174 *
5175 * USB drivers call this function to get hub's child device
5176 * pointer.
5177 *
5178 * Return NULL if input param is invalid and
5179 * child's usb_device pointer if non-NULL.
5180 */
5181struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5182 int port1)
5183{
5184 struct usb_hub *hub = hdev_to_hub(hdev);
5185
5186 if (port1 < 1 || port1 > hdev->maxchild)
5187 return NULL;
5188 return hub->ports[port1 - 1]->child;
5189}
5190EXPORT_SYMBOL_GPL(usb_hub_find_child);
Lan Tianyud5575422012-09-05 13:44:33 +08005191
Lan Tianyu05f91682012-09-05 13:44:34 +08005192/**
5193 * usb_set_hub_port_connect_type - set hub port connect type.
5194 * @hdev: USB device belonging to the usb hub
5195 * @port1: port num of the port
5196 * @type: connect type of the port
5197 */
5198void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
5199 enum usb_port_connect_type type)
5200{
5201 struct usb_hub *hub = hdev_to_hub(hdev);
5202
5203 hub->ports[port1 - 1]->connect_type = type;
5204}
5205
5206/**
5207 * usb_get_hub_port_connect_type - Get the port's connect type
5208 * @hdev: USB device belonging to the usb hub
5209 * @port1: port num of the port
5210 *
5211 * Return connect type of the port and if input params are
5212 * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
5213 */
5214enum usb_port_connect_type
5215usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
5216{
5217 struct usb_hub *hub = hdev_to_hub(hdev);
5218
5219 return hub->ports[port1 - 1]->connect_type;
5220}
5221
Lan Tianyud5575422012-09-05 13:44:33 +08005222#ifdef CONFIG_ACPI
5223/**
5224 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5225 * @hdev: USB device belonging to the usb hub
5226 * @port1: port num of the port
5227 *
5228 * Return port's acpi handle if successful, NULL if params are
5229 * invaild.
5230 */
5231acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5232 int port1)
5233{
5234 struct usb_hub *hub = hdev_to_hub(hdev);
5235
5236 return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
5237}
5238#endif