USB: hub_for_each_child should skip unconnected ports
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / core / hub.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
11#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>
1da177e4
LT
19#include <linux/ioctl.h>
20#include <linux/usb.h>
21#include <linux/usbdevice_fs.h>
27729aad 22#include <linux/usb/hcd.h>
925aa46b 23#include <linux/usb/otg.h>
93362a87 24#include <linux/usb/quirks.h>
9c8d6178 25#include <linux/kthread.h>
4186ecf8 26#include <linux/mutex.h>
7dfb7103 27#include <linux/freezer.h>
b04b3156 28#include <linux/random.h>
1da177e4 29
1da177e4
LT
30#include <asm/uaccess.h>
31#include <asm/byteorder.h>
32
33#include "usb.h"
1da177e4 34
f2a383e4
GKH
35/* 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
fa2a9566 42struct usb_port {
ff823c79 43 struct usb_device *child;
fa2a9566
LT
44 struct device dev;
45 struct dev_state *port_owner;
05f91689 46 enum usb_port_connect_type connect_type;
fa2a9566
LT
47};
48
1bb5f66b
AS
49struct usb_hub {
50 struct device *intfdev; /* the "interface" device */
51 struct usb_device *hdev;
e8054854 52 struct kref kref;
1bb5f66b
AS
53 struct urb *urb; /* for interrupt polling pipe */
54
55 /* buffer for urb ... with extra space in case of babble */
56 char (*buffer)[8];
1bb5f66b
AS
57 union {
58 struct usb_hub_status hub;
59 struct usb_port_status port;
60 } *status; /* buffer for status reports */
db90e7a1 61 struct mutex status_mutex; /* for the status buffer */
1bb5f66b
AS
62
63 int error; /* last reported error */
64 int nerrors; /* track consecutive errors */
65
66 struct list_head event_list; /* hubs w/data or errs ready */
67 unsigned long event_bits[1]; /* status change bitmask */
68 unsigned long change_bits[1]; /* ports with logical connect
69 status change */
70 unsigned long busy_bits[1]; /* ports being reset or
71 resumed */
253e0572
AS
72 unsigned long removed_bits[1]; /* ports with a "removed"
73 device present */
4ee823b8
SS
74 unsigned long wakeup_bits[1]; /* ports that have signaled
75 remote wakeup */
1bb5f66b
AS
76#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
77#error event_bits[] is too short!
78#endif
79
80 struct usb_hub_descriptor *descriptor; /* class descriptor */
81 struct usb_tt tt; /* Transaction Translator */
82
83 unsigned mA_per_port; /* current for each child */
84
85 unsigned limited_power:1;
86 unsigned quiescing:1;
e8054854 87 unsigned disconnected:1;
1bb5f66b
AS
88
89 unsigned has_indicators:1;
90 u8 indicator[USB_MAXCHILDREN];
6d5aefb8 91 struct delayed_work leds;
8520f380 92 struct delayed_work init_work;
fa2a9566 93 struct usb_port **ports;
1bb5f66b
AS
94};
95
dbe79bbe
JY
96static inline int hub_is_superspeed(struct usb_device *hdev)
97{
7bf01185 98 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
dbe79bbe 99}
1bb5f66b 100
fa286188 101/* Protect struct usb_device->state and ->children members
9ad3d6cc 102 * Note: Both are also protected by ->dev.sem, except that ->state can
1da177e4
LT
103 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
104static DEFINE_SPINLOCK(device_state_lock);
105
106/* khubd's worklist and its lock */
107static DEFINE_SPINLOCK(hub_event_lock);
108static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
109
110/* Wakes up khubd */
111static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
112
9c8d6178 113static struct task_struct *khubd_task;
1da177e4
LT
114
115/* cycle leds on hubs that aren't blinking for attention */
90ab5ee9 116static bool blinkenlights = 0;
1da177e4
LT
117module_param (blinkenlights, bool, S_IRUGO);
118MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
119
fd7c519d
JK
120/*
121 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
122 * 10 seconds to send reply for the initial 64-byte descriptor request.
123 */
124/* define initial 64-byte descriptor request timeout in milliseconds */
125static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
126module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
b9cef6c3
WF
127MODULE_PARM_DESC(initial_descriptor_timeout,
128 "initial 64-byte descriptor request timeout in milliseconds "
129 "(default 5000 - 5.0 seconds)");
fd7c519d 130
1da177e4
LT
131/*
132 * As of 2.6.10 we introduce a new USB device initialization scheme which
133 * closely resembles the way Windows works. Hopefully it will be compatible
134 * with a wider range of devices than the old scheme. However some previously
135 * working devices may start giving rise to "device not accepting address"
136 * errors; if that happens the user can try the old scheme by adjusting the
137 * following module parameters.
138 *
139 * For maximum flexibility there are two boolean parameters to control the
140 * hub driver's behavior. On the first initialization attempt, if the
141 * "old_scheme_first" parameter is set then the old scheme will be used,
142 * otherwise the new scheme is used. If that fails and "use_both_schemes"
143 * is set, then the driver will make another attempt, using the other scheme.
144 */
90ab5ee9 145static bool old_scheme_first = 0;
1da177e4
LT
146module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
147MODULE_PARM_DESC(old_scheme_first,
148 "start with the old device initialization scheme");
149
90ab5ee9 150static bool use_both_schemes = 1;
1da177e4
LT
151module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
152MODULE_PARM_DESC(use_both_schemes,
153 "try the other device initialization scheme if the "
154 "first one fails");
155
32fe0198
AS
156/* Mutual exclusion for EHCI CF initialization. This interferes with
157 * port reset on some companion controllers.
158 */
159DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
160EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
161
948fea37
AS
162#define HUB_DEBOUNCE_TIMEOUT 1500
163#define HUB_DEBOUNCE_STEP 25
164#define HUB_DEBOUNCE_STABLE 100
165
fa2a9566
LT
166#define to_usb_port(_dev) \
167 container_of(_dev, struct usb_port, dev)
1da177e4 168
742120c6
ML
169static int usb_reset_and_verify_device(struct usb_device *udev);
170
131dec34 171static inline char *portspeed(struct usb_hub *hub, int portstatus)
1da177e4 172{
131dec34
SS
173 if (hub_is_superspeed(hub->hdev))
174 return "5.0 Gb/s";
288ead45 175 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
1da177e4 176 return "480 Mb/s";
288ead45 177 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
1da177e4
LT
178 return "1.5 Mb/s";
179 else
180 return "12 Mb/s";
181}
1da177e4
LT
182
183/* Note that hdev or one of its children must be locked! */
25118084 184static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
1da177e4 185{
ff823c79 186 if (!hdev || !hdev->actconfig || !hdev->maxchild)
25118084 187 return NULL;
1da177e4
LT
188 return usb_get_intfdata(hdev->actconfig->interface[0]);
189}
190
d9b2099c
SS
191static int usb_device_supports_lpm(struct usb_device *udev)
192{
193 /* USB 2.1 (and greater) devices indicate LPM support through
194 * their USB 2.0 Extended Capabilities BOS descriptor.
195 */
196 if (udev->speed == USB_SPEED_HIGH) {
197 if (udev->bos->ext_cap &&
198 (USB_LPM_SUPPORT &
199 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
200 return 1;
201 return 0;
202 }
51e0a012
SS
203
204 /* All USB 3.0 must support LPM, but we need their max exit latency
205 * information from the SuperSpeed Extended Capabilities BOS descriptor.
206 */
207 if (!udev->bos->ss_cap) {
208 dev_warn(&udev->dev, "No LPM exit latency info found. "
209 "Power management will be impacted.\n");
210 return 0;
211 }
212 if (udev->parent->lpm_capable)
213 return 1;
214
215 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. "
216 "Power management will be impacted.\n");
d9b2099c
SS
217 return 0;
218}
219
51e0a012
SS
220/*
221 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
222 * either U1 or U2.
223 */
224static void usb_set_lpm_mel(struct usb_device *udev,
225 struct usb3_lpm_parameters *udev_lpm_params,
226 unsigned int udev_exit_latency,
227 struct usb_hub *hub,
228 struct usb3_lpm_parameters *hub_lpm_params,
229 unsigned int hub_exit_latency)
230{
231 unsigned int total_mel;
232 unsigned int device_mel;
233 unsigned int hub_mel;
234
235 /*
236 * Calculate the time it takes to transition all links from the roothub
237 * to the parent hub into U0. The parent hub must then decode the
238 * packet (hub header decode latency) to figure out which port it was
239 * bound for.
240 *
241 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
242 * means 0.1us). Multiply that by 100 to get nanoseconds.
243 */
244 total_mel = hub_lpm_params->mel +
245 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
246
247 /*
248 * How long will it take to transition the downstream hub's port into
249 * U0? The greater of either the hub exit latency or the device exit
250 * latency.
251 *
252 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
253 * Multiply that by 1000 to get nanoseconds.
254 */
255 device_mel = udev_exit_latency * 1000;
256 hub_mel = hub_exit_latency * 1000;
257 if (device_mel > hub_mel)
258 total_mel += device_mel;
259 else
260 total_mel += hub_mel;
261
262 udev_lpm_params->mel = total_mel;
263}
264
265/*
266 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
267 * a transition from either U1 or U2.
268 */
269static void usb_set_lpm_pel(struct usb_device *udev,
270 struct usb3_lpm_parameters *udev_lpm_params,
271 unsigned int udev_exit_latency,
272 struct usb_hub *hub,
273 struct usb3_lpm_parameters *hub_lpm_params,
274 unsigned int hub_exit_latency,
275 unsigned int port_to_port_exit_latency)
276{
277 unsigned int first_link_pel;
278 unsigned int hub_pel;
279
280 /*
281 * First, the device sends an LFPS to transition the link between the
282 * device and the parent hub into U0. The exit latency is the bigger of
283 * the device exit latency or the hub exit latency.
284 */
285 if (udev_exit_latency > hub_exit_latency)
286 first_link_pel = udev_exit_latency * 1000;
287 else
288 first_link_pel = hub_exit_latency * 1000;
289
290 /*
291 * When the hub starts to receive the LFPS, there is a slight delay for
292 * it to figure out that one of the ports is sending an LFPS. Then it
293 * will forward the LFPS to its upstream link. The exit latency is the
294 * delay, plus the PEL that we calculated for this hub.
295 */
296 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
297
298 /*
299 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
300 * is the greater of the two exit latencies.
301 */
302 if (first_link_pel > hub_pel)
303 udev_lpm_params->pel = first_link_pel;
304 else
305 udev_lpm_params->pel = hub_pel;
306}
307
308/*
309 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
310 * when a device initiates a transition to U0, until when it will receive the
311 * first packet from the host controller.
312 *
313 * Section C.1.5.1 describes the four components to this:
314 * - t1: device PEL
315 * - t2: time for the ERDY to make it from the device to the host.
316 * - t3: a host-specific delay to process the ERDY.
317 * - t4: time for the packet to make it from the host to the device.
318 *
319 * t3 is specific to both the xHCI host and the platform the host is integrated
320 * into. The Intel HW folks have said it's negligible, FIXME if a different
321 * vendor says otherwise.
322 */
323static void usb_set_lpm_sel(struct usb_device *udev,
324 struct usb3_lpm_parameters *udev_lpm_params)
325{
326 struct usb_device *parent;
327 unsigned int num_hubs;
328 unsigned int total_sel;
329
330 /* t1 = device PEL */
331 total_sel = udev_lpm_params->pel;
332 /* How many external hubs are in between the device & the root port. */
333 for (parent = udev->parent, num_hubs = 0; parent->parent;
334 parent = parent->parent)
335 num_hubs++;
336 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
337 if (num_hubs > 0)
338 total_sel += 2100 + 250 * (num_hubs - 1);
339
340 /* t4 = 250ns * num_hubs */
341 total_sel += 250 * num_hubs;
342
343 udev_lpm_params->sel = total_sel;
344}
345
346static void usb_set_lpm_parameters(struct usb_device *udev)
347{
348 struct usb_hub *hub;
349 unsigned int port_to_port_delay;
350 unsigned int udev_u1_del;
351 unsigned int udev_u2_del;
352 unsigned int hub_u1_del;
353 unsigned int hub_u2_del;
354
355 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
356 return;
357
358 hub = hdev_to_hub(udev->parent);
359 /* It doesn't take time to transition the roothub into U0, since it
360 * doesn't have an upstream link.
361 */
362 if (!hub)
363 return;
364
365 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
366 udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
367 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
368 hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
369
370 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
371 hub, &udev->parent->u1_params, hub_u1_del);
372
373 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
374 hub, &udev->parent->u2_params, hub_u2_del);
375
376 /*
377 * Appendix C, section C.2.2.2, says that there is a slight delay from
378 * when the parent hub notices the downstream port is trying to
379 * transition to U0 to when the hub initiates a U0 transition on its
380 * upstream port. The section says the delays are tPort2PortU1EL and
381 * tPort2PortU2EL, but it doesn't define what they are.
382 *
383 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
384 * about the same delays. Use the maximum delay calculations from those
385 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
386 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
387 * assume the device exit latencies they are talking about are the hub
388 * exit latencies.
389 *
390 * What do we do if the U2 exit latency is less than the U1 exit
391 * latency? It's possible, although not likely...
392 */
393 port_to_port_delay = 1;
394
395 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
396 hub, &udev->parent->u1_params, hub_u1_del,
397 port_to_port_delay);
398
399 if (hub_u2_del > hub_u1_del)
400 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
401 else
402 port_to_port_delay = 1 + hub_u1_del;
403
404 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
405 hub, &udev->parent->u2_params, hub_u2_del,
406 port_to_port_delay);
407
408 /* Now that we've got PEL, calculate SEL. */
409 usb_set_lpm_sel(udev, &udev->u1_params);
410 usb_set_lpm_sel(udev, &udev->u2_params);
411}
412
1da177e4 413/* USB 2.0 spec Section 11.24.4.5 */
dbe79bbe 414static int get_hub_descriptor(struct usb_device *hdev, void *data)
1da177e4 415{
dbe79bbe
JY
416 int i, ret, size;
417 unsigned dtype;
418
419 if (hub_is_superspeed(hdev)) {
420 dtype = USB_DT_SS_HUB;
421 size = USB_DT_SS_HUB_SIZE;
422 } else {
423 dtype = USB_DT_HUB;
424 size = sizeof(struct usb_hub_descriptor);
425 }
1da177e4
LT
426
427 for (i = 0; i < 3; i++) {
428 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
429 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
dbe79bbe 430 dtype << 8, 0, data, size,
1da177e4
LT
431 USB_CTRL_GET_TIMEOUT);
432 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
433 return ret;
434 }
435 return -EINVAL;
436}
437
438/*
439 * USB 2.0 spec Section 11.24.2.1
440 */
441static int clear_hub_feature(struct usb_device *hdev, int feature)
442{
443 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
444 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
445}
446
447/*
448 * USB 2.0 spec Section 11.24.2.2
449 */
450static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
451{
452 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
453 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
454 NULL, 0, 1000);
455}
456
457/*
458 * USB 2.0 spec Section 11.24.2.13
459 */
460static int set_port_feature(struct usb_device *hdev, int port1, int feature)
461{
462 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
463 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
464 NULL, 0, 1000);
465}
466
467/*
468 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
469 * for info about using port indicators
470 */
471static void set_port_led(
472 struct usb_hub *hub,
473 int port1,
474 int selector
475)
476{
477 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
478 USB_PORT_FEAT_INDICATOR);
479 if (status < 0)
480 dev_dbg (hub->intfdev,
481 "port %d indicator %s status %d\n",
482 port1,
483 ({ char *s; switch (selector) {
484 case HUB_LED_AMBER: s = "amber"; break;
485 case HUB_LED_GREEN: s = "green"; break;
486 case HUB_LED_OFF: s = "off"; break;
487 case HUB_LED_AUTO: s = "auto"; break;
488 default: s = "??"; break;
489 }; s; }),
490 status);
491}
492
493#define LED_CYCLE_PERIOD ((2*HZ)/3)
494
c4028958 495static void led_work (struct work_struct *work)
1da177e4 496{
c4028958
DH
497 struct usb_hub *hub =
498 container_of(work, struct usb_hub, leds.work);
1da177e4
LT
499 struct usb_device *hdev = hub->hdev;
500 unsigned i;
501 unsigned changed = 0;
502 int cursor = -1;
503
504 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
505 return;
506
507 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
508 unsigned selector, mode;
509
510 /* 30%-50% duty cycle */
511
512 switch (hub->indicator[i]) {
513 /* cycle marker */
514 case INDICATOR_CYCLE:
515 cursor = i;
516 selector = HUB_LED_AUTO;
517 mode = INDICATOR_AUTO;
518 break;
519 /* blinking green = sw attention */
520 case INDICATOR_GREEN_BLINK:
521 selector = HUB_LED_GREEN;
522 mode = INDICATOR_GREEN_BLINK_OFF;
523 break;
524 case INDICATOR_GREEN_BLINK_OFF:
525 selector = HUB_LED_OFF;
526 mode = INDICATOR_GREEN_BLINK;
527 break;
528 /* blinking amber = hw attention */
529 case INDICATOR_AMBER_BLINK:
530 selector = HUB_LED_AMBER;
531 mode = INDICATOR_AMBER_BLINK_OFF;
532 break;
533 case INDICATOR_AMBER_BLINK_OFF:
534 selector = HUB_LED_OFF;
535 mode = INDICATOR_AMBER_BLINK;
536 break;
537 /* blink green/amber = reserved */
538 case INDICATOR_ALT_BLINK:
539 selector = HUB_LED_GREEN;
540 mode = INDICATOR_ALT_BLINK_OFF;
541 break;
542 case INDICATOR_ALT_BLINK_OFF:
543 selector = HUB_LED_AMBER;
544 mode = INDICATOR_ALT_BLINK;
545 break;
546 default:
547 continue;
548 }
549 if (selector != HUB_LED_AUTO)
550 changed = 1;
551 set_port_led(hub, i + 1, selector);
552 hub->indicator[i] = mode;
553 }
554 if (!changed && blinkenlights) {
555 cursor++;
556 cursor %= hub->descriptor->bNbrPorts;
557 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
558 hub->indicator[cursor] = INDICATOR_CYCLE;
559 changed++;
560 }
561 if (changed)
562 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
563}
564
565/* use a short timeout for hub/port status fetches */
566#define USB_STS_TIMEOUT 1000
567#define USB_STS_RETRIES 5
568
569/*
570 * USB 2.0 spec Section 11.24.2.6
571 */
572static int get_hub_status(struct usb_device *hdev,
573 struct usb_hub_status *data)
574{
575 int i, status = -ETIMEDOUT;
576
3824c1dd
LP
577 for (i = 0; i < USB_STS_RETRIES &&
578 (status == -ETIMEDOUT || status == -EPIPE); i++) {
1da177e4
LT
579 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
580 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
581 data, sizeof(*data), USB_STS_TIMEOUT);
582 }
583 return status;
584}
585
586/*
587 * USB 2.0 spec Section 11.24.2.7
588 */
589static int get_port_status(struct usb_device *hdev, int port1,
590 struct usb_port_status *data)
591{
592 int i, status = -ETIMEDOUT;
593
3824c1dd
LP
594 for (i = 0; i < USB_STS_RETRIES &&
595 (status == -ETIMEDOUT || status == -EPIPE); i++) {
1da177e4
LT
596 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
597 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
598 data, sizeof(*data), USB_STS_TIMEOUT);
599 }
600 return status;
601}
602
3eb14915
AS
603static int hub_port_status(struct usb_hub *hub, int port1,
604 u16 *status, u16 *change)
605{
606 int ret;
607
608 mutex_lock(&hub->status_mutex);
609 ret = get_port_status(hub->hdev, port1, &hub->status->port);
610 if (ret < 4) {
611 dev_err(hub->intfdev,
612 "%s failed (err = %d)\n", __func__, ret);
613 if (ret >= 0)
614 ret = -EIO;
615 } else {
616 *status = le16_to_cpu(hub->status->port.wPortStatus);
617 *change = le16_to_cpu(hub->status->port.wPortChange);
dbe79bbe 618
3eb14915
AS
619 ret = 0;
620 }
621 mutex_unlock(&hub->status_mutex);
622 return ret;
623}
624
1da177e4
LT
625static void kick_khubd(struct usb_hub *hub)
626{
627 unsigned long flags;
628
629 spin_lock_irqsave(&hub_event_lock, flags);
2e2c5eea 630 if (!hub->disconnected && list_empty(&hub->event_list)) {
1da177e4 631 list_add_tail(&hub->event_list, &hub_event_list);
8e4ceb38
AS
632
633 /* Suppress autosuspend until khubd runs */
634 usb_autopm_get_interface_no_resume(
635 to_usb_interface(hub->intfdev));
1da177e4
LT
636 wake_up(&khubd_wait);
637 }
638 spin_unlock_irqrestore(&hub_event_lock, flags);
639}
640
641void usb_kick_khubd(struct usb_device *hdev)
642{
25118084
AS
643 struct usb_hub *hub = hdev_to_hub(hdev);
644
645 if (hub)
646 kick_khubd(hub);
1da177e4
LT
647}
648
4ee823b8
SS
649/*
650 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
651 * Notification, which indicates it had initiated remote wakeup.
652 *
653 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
654 * device initiates resume, so the USB core will not receive notice of the
655 * resume through the normal hub interrupt URB.
656 */
657void usb_wakeup_notification(struct usb_device *hdev,
658 unsigned int portnum)
659{
660 struct usb_hub *hub;
661
662 if (!hdev)
663 return;
664
665 hub = hdev_to_hub(hdev);
666 if (hub) {
667 set_bit(portnum, hub->wakeup_bits);
668 kick_khubd(hub);
669 }
670}
671EXPORT_SYMBOL_GPL(usb_wakeup_notification);
1da177e4
LT
672
673/* completion function, fires on port status changes and various faults */
7d12e780 674static void hub_irq(struct urb *urb)
1da177e4 675{
ec17cf1c 676 struct usb_hub *hub = urb->context;
e015268d 677 int status = urb->status;
71d2718f 678 unsigned i;
1da177e4
LT
679 unsigned long bits;
680
e015268d 681 switch (status) {
1da177e4
LT
682 case -ENOENT: /* synchronous unlink */
683 case -ECONNRESET: /* async unlink */
684 case -ESHUTDOWN: /* hardware going away */
685 return;
686
687 default: /* presumably an error */
688 /* Cause a hub reset after 10 consecutive errors */
e015268d 689 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
1da177e4
LT
690 if ((++hub->nerrors < 10) || hub->error)
691 goto resubmit;
e015268d 692 hub->error = status;
1da177e4 693 /* FALL THROUGH */
ec17cf1c 694
1da177e4
LT
695 /* let khubd handle things */
696 case 0: /* we got data: port status changed */
697 bits = 0;
698 for (i = 0; i < urb->actual_length; ++i)
699 bits |= ((unsigned long) ((*hub->buffer)[i]))
700 << (i*8);
701 hub->event_bits[0] = bits;
702 break;
703 }
704
705 hub->nerrors = 0;
706
707 /* Something happened, let khubd figure it out */
708 kick_khubd(hub);
709
710resubmit:
711 if (hub->quiescing)
712 return;
713
714 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
715 && status != -ENODEV && status != -EPERM)
716 dev_err (hub->intfdev, "resubmit --> %d\n", status);
717}
718
719/* USB 2.0 spec Section 11.24.2.3 */
720static inline int
721hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
722{
c2f6595f 723 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1da177e4
LT
724 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
725 tt, NULL, 0, 1000);
726}
727
728/*
729 * enumeration blocks khubd for a long time. we use keventd instead, since
730 * long blocking there is the exception, not the rule. accordingly, HCDs
731 * talking to TTs must queue control transfers (not just bulk and iso), so
732 * both can talk to the same hub concurrently.
733 */
cb88a1b8 734static void hub_tt_work(struct work_struct *work)
1da177e4 735{
c4028958 736 struct usb_hub *hub =
cb88a1b8 737 container_of(work, struct usb_hub, tt.clear_work);
1da177e4 738 unsigned long flags;
55e5fdfa 739 int limit = 100;
1da177e4
LT
740
741 spin_lock_irqsave (&hub->tt.lock, flags);
55e5fdfa 742 while (--limit && !list_empty (&hub->tt.clear_list)) {
d0f830d3 743 struct list_head *next;
1da177e4
LT
744 struct usb_tt_clear *clear;
745 struct usb_device *hdev = hub->hdev;
cb88a1b8 746 const struct hc_driver *drv;
1da177e4
LT
747 int status;
748
d0f830d3
HS
749 next = hub->tt.clear_list.next;
750 clear = list_entry (next, struct usb_tt_clear, clear_list);
1da177e4
LT
751 list_del (&clear->clear_list);
752
753 /* drop lock so HCD can concurrently report other TT errors */
754 spin_unlock_irqrestore (&hub->tt.lock, flags);
755 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
1da177e4
LT
756 if (status)
757 dev_err (&hdev->dev,
758 "clear tt %d (%04x) error %d\n",
759 clear->tt, clear->devinfo, status);
cb88a1b8
AS
760
761 /* Tell the HCD, even if the operation failed */
762 drv = clear->hcd->driver;
763 if (drv->clear_tt_buffer_complete)
764 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
765
1bc3c9e1 766 kfree(clear);
cb88a1b8 767 spin_lock_irqsave(&hub->tt.lock, flags);
1da177e4
LT
768 }
769 spin_unlock_irqrestore (&hub->tt.lock, flags);
770}
771
772/**
cb88a1b8
AS
773 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
774 * @urb: an URB associated with the failed or incomplete split transaction
1da177e4
LT
775 *
776 * High speed HCDs use this to tell the hub driver that some split control or
777 * bulk transaction failed in a way that requires clearing internal state of
778 * a transaction translator. This is normally detected (and reported) from
779 * interrupt context.
780 *
781 * It may not be possible for that hub to handle additional full (or low)
782 * speed transactions until that state is fully cleared out.
783 */
cb88a1b8 784int usb_hub_clear_tt_buffer(struct urb *urb)
1da177e4 785{
cb88a1b8
AS
786 struct usb_device *udev = urb->dev;
787 int pipe = urb->pipe;
1da177e4
LT
788 struct usb_tt *tt = udev->tt;
789 unsigned long flags;
790 struct usb_tt_clear *clear;
791
792 /* we've got to cope with an arbitrary number of pending TT clears,
793 * since each TT has "at least two" buffers that can need it (and
794 * there can be many TTs per hub). even if they're uncommon.
795 */
54e6ecb2 796 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
1da177e4
LT
797 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
798 /* FIXME recover somehow ... RESET_TT? */
cb88a1b8 799 return -ENOMEM;
1da177e4
LT
800 }
801
802 /* info that CLEAR_TT_BUFFER needs */
803 clear->tt = tt->multi ? udev->ttport : 1;
804 clear->devinfo = usb_pipeendpoint (pipe);
805 clear->devinfo |= udev->devnum << 4;
806 clear->devinfo |= usb_pipecontrol (pipe)
807 ? (USB_ENDPOINT_XFER_CONTROL << 11)
808 : (USB_ENDPOINT_XFER_BULK << 11);
809 if (usb_pipein (pipe))
810 clear->devinfo |= 1 << 15;
cb88a1b8
AS
811
812 /* info for completion callback */
813 clear->hcd = bus_to_hcd(udev->bus);
814 clear->ep = urb->ep;
815
1da177e4
LT
816 /* tell keventd to clear state for this TT */
817 spin_lock_irqsave (&tt->lock, flags);
818 list_add_tail (&clear->clear_list, &tt->clear_list);
cb88a1b8 819 schedule_work(&tt->clear_work);
1da177e4 820 spin_unlock_irqrestore (&tt->lock, flags);
cb88a1b8 821 return 0;
1da177e4 822}
cb88a1b8 823EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
1da177e4 824
8520f380
AS
825/* If do_delay is false, return the number of milliseconds the caller
826 * needs to delay.
827 */
828static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
1da177e4
LT
829{
830 int port1;
b789696a 831 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
8520f380 832 unsigned delay;
4489a571
AS
833 u16 wHubCharacteristics =
834 le16_to_cpu(hub->descriptor->wHubCharacteristics);
835
836 /* Enable power on each port. Some hubs have reserved values
837 * of LPSM (> 2) in their descriptors, even though they are
838 * USB 2.0 hubs. Some hubs do not implement port-power switching
839 * but only emulate it. In all cases, the ports won't work
840 * unless we send these messages to the hub.
841 */
842 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
1da177e4 843 dev_dbg(hub->intfdev, "enabling power on all ports\n");
4489a571
AS
844 else
845 dev_dbg(hub->intfdev, "trying to enable port power on "
846 "non-switchable hub\n");
847 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
a0693bd0 848 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
1da177e4 849
b789696a 850 /* Wait at least 100 msec for power to become stable */
8520f380
AS
851 delay = max(pgood_delay, (unsigned) 100);
852 if (do_delay)
853 msleep(delay);
854 return delay;
1da177e4
LT
855}
856
1da177e4
LT
857static int hub_hub_status(struct usb_hub *hub,
858 u16 *status, u16 *change)
859{
860 int ret;
861
db90e7a1 862 mutex_lock(&hub->status_mutex);
1da177e4
LT
863 ret = get_hub_status(hub->hdev, &hub->status->hub);
864 if (ret < 0)
865 dev_err (hub->intfdev,
441b62c1 866 "%s failed (err = %d)\n", __func__, ret);
1da177e4
LT
867 else {
868 *status = le16_to_cpu(hub->status->hub.wHubStatus);
869 *change = le16_to_cpu(hub->status->hub.wHubChange);
870 ret = 0;
871 }
db90e7a1 872 mutex_unlock(&hub->status_mutex);
1da177e4
LT
873 return ret;
874}
875
8b28c752
AS
876static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
877{
878 struct usb_device *hdev = hub->hdev;
0458d5b4 879 int ret = 0;
8b28c752 880
ff823c79
LT
881 if (hub->ports[port1 - 1]->child && set_state)
882 usb_set_device_state(hub->ports[port1 - 1]->child,
8b28c752 883 USB_STATE_NOTATTACHED);
dbe79bbe 884 if (!hub->error && !hub_is_superspeed(hub->hdev))
0458d5b4 885 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
8b28c752
AS
886 if (ret)
887 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
0458d5b4 888 port1, ret);
8b28c752
AS
889 return ret;
890}
891
0458d5b4 892/*
6d42fcdb 893 * Disable a port and mark a logical connect-change event, so that some
0458d5b4
AS
894 * time later khubd will disconnect() any existing usb_device on the port
895 * and will re-enumerate if there actually is a device attached.
896 */
897static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
898{
899 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
900 hub_port_disable(hub, port1, 1);
7d069b7d 901
0458d5b4
AS
902 /* FIXME let caller ask to power down the port:
903 * - some devices won't enumerate without a VBUS power cycle
904 * - SRP saves power that way
905 * - ... new call, TBD ...
906 * That's easy if this hub can switch power per-port, and
907 * khubd reactivates the port later (timer, SRP, etc).
908 * Powerdown must be optional, because of reset/DFU.
909 */
910
911 set_bit(port1, hub->change_bits);
912 kick_khubd(hub);
913}
914
253e0572
AS
915/**
916 * usb_remove_device - disable a device's port on its parent hub
917 * @udev: device to be disabled and removed
918 * Context: @udev locked, must be able to sleep.
919 *
920 * After @udev's port has been disabled, khubd is notified and it will
921 * see that the device has been disconnected. When the device is
922 * physically unplugged and something is plugged in, the events will
923 * be received and processed normally.
924 */
925int usb_remove_device(struct usb_device *udev)
926{
927 struct usb_hub *hub;
928 struct usb_interface *intf;
929
930 if (!udev->parent) /* Can't remove a root hub */
931 return -EINVAL;
932 hub = hdev_to_hub(udev->parent);
933 intf = to_usb_interface(hub->intfdev);
934
935 usb_autopm_get_interface(intf);
936 set_bit(udev->portnum, hub->removed_bits);
937 hub_port_logical_disconnect(hub, udev->portnum);
938 usb_autopm_put_interface(intf);
939 return 0;
940}
941
6ee0b270 942enum hub_activation_type {
8e4ceb38 943 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
8520f380 944 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
6ee0b270 945};
5e6effae 946
8520f380
AS
947static void hub_init_func2(struct work_struct *ws);
948static void hub_init_func3(struct work_struct *ws);
949
f2835219 950static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
5e6effae
AS
951{
952 struct usb_device *hdev = hub->hdev;
653a39d1
SS
953 struct usb_hcd *hcd;
954 int ret;
5e6effae 955 int port1;
f2835219 956 int status;
948fea37 957 bool need_debounce_delay = false;
8520f380
AS
958 unsigned delay;
959
960 /* Continue a partial initialization */
961 if (type == HUB_INIT2)
962 goto init2;
963 if (type == HUB_INIT3)
964 goto init3;
5e6effae 965
a45aa3b3
EF
966 /* The superspeed hub except for root hub has to use Hub Depth
967 * value as an offset into the route string to locate the bits
968 * it uses to determine the downstream port number. So hub driver
969 * should send a set hub depth request to superspeed hub after
970 * the superspeed hub is set configuration in initialization or
971 * reset procedure.
972 *
973 * After a resume, port power should still be on.
f2835219
AS
974 * For any other type of activation, turn it on.
975 */
8520f380 976 if (type != HUB_RESUME) {
a45aa3b3
EF
977 if (hdev->parent && hub_is_superspeed(hdev)) {
978 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
979 HUB_SET_DEPTH, USB_RT_HUB,
980 hdev->level - 1, 0, NULL, 0,
981 USB_CTRL_SET_TIMEOUT);
982 if (ret < 0)
983 dev_err(hub->intfdev,
984 "set hub depth failed\n");
985 }
8520f380
AS
986
987 /* Speed up system boot by using a delayed_work for the
988 * hub's initial power-up delays. This is pretty awkward
989 * and the implementation looks like a home-brewed sort of
990 * setjmp/longjmp, but it saves at least 100 ms for each
991 * root hub (assuming usbcore is compiled into the kernel
992 * rather than as a module). It adds up.
993 *
994 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
995 * because for those activation types the ports have to be
996 * operational when we return. In theory this could be done
997 * for HUB_POST_RESET, but it's easier not to.
998 */
999 if (type == HUB_INIT) {
1000 delay = hub_power_on(hub, false);
1001 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
1002 schedule_delayed_work(&hub->init_work,
1003 msecs_to_jiffies(delay));
61fbeba1
AS
1004
1005 /* Suppress autosuspend until init is done */
8e4ceb38
AS
1006 usb_autopm_get_interface_no_resume(
1007 to_usb_interface(hub->intfdev));
8520f380 1008 return; /* Continues at init2: below */
653a39d1
SS
1009 } else if (type == HUB_RESET_RESUME) {
1010 /* The internal host controller state for the hub device
1011 * may be gone after a host power loss on system resume.
1012 * Update the device's info so the HW knows it's a hub.
1013 */
1014 hcd = bus_to_hcd(hdev->bus);
1015 if (hcd->driver->update_hub_device) {
1016 ret = hcd->driver->update_hub_device(hcd, hdev,
1017 &hub->tt, GFP_NOIO);
1018 if (ret < 0) {
1019 dev_err(hub->intfdev, "Host not "
1020 "accepting hub info "
1021 "update.\n");
1022 dev_err(hub->intfdev, "LS/FS devices "
1023 "and hubs may not work "
1024 "under this hub\n.");
1025 }
1026 }
1027 hub_power_on(hub, true);
8520f380
AS
1028 } else {
1029 hub_power_on(hub, true);
1030 }
1031 }
1032 init2:
f2835219 1033
6ee0b270
AS
1034 /* Check each port and set hub->change_bits to let khubd know
1035 * which ports need attention.
5e6effae
AS
1036 */
1037 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
ff823c79 1038 struct usb_device *udev = hub->ports[port1 - 1]->child;
5e6effae
AS
1039 u16 portstatus, portchange;
1040
6ee0b270
AS
1041 portstatus = portchange = 0;
1042 status = hub_port_status(hub, port1, &portstatus, &portchange);
1043 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1044 dev_dbg(hub->intfdev,
1045 "port %d: status %04x change %04x\n",
1046 port1, portstatus, portchange);
1047
1048 /* After anything other than HUB_RESUME (i.e., initialization
1049 * or any sort of reset), every port should be disabled.
1050 * Unconnected ports should likewise be disabled (paranoia),
1051 * and so should ports for which we have no usb_device.
1052 */
1053 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1054 type != HUB_RESUME ||
1055 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1056 !udev ||
1057 udev->state == USB_STATE_NOTATTACHED)) {
9f0a6cd3
AX
1058 /*
1059 * USB3 protocol ports will automatically transition
1060 * to Enabled state when detect an USB3.0 device attach.
1061 * Do not disable USB3 protocol ports.
9f0a6cd3 1062 */
131dec34 1063 if (!hub_is_superspeed(hdev)) {
9f0a6cd3
AX
1064 clear_port_feature(hdev, port1,
1065 USB_PORT_FEAT_ENABLE);
1066 portstatus &= ~USB_PORT_STAT_ENABLE;
85f0ff46
SS
1067 } else {
1068 /* Pretend that power was lost for USB3 devs */
1069 portstatus &= ~USB_PORT_STAT_ENABLE;
9f0a6cd3 1070 }
5e6effae
AS
1071 }
1072
948fea37
AS
1073 /* Clear status-change flags; we'll debounce later */
1074 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1075 need_debounce_delay = true;
1076 clear_port_feature(hub->hdev, port1,
1077 USB_PORT_FEAT_C_CONNECTION);
1078 }
1079 if (portchange & USB_PORT_STAT_C_ENABLE) {
1080 need_debounce_delay = true;
1081 clear_port_feature(hub->hdev, port1,
1082 USB_PORT_FEAT_C_ENABLE);
1083 }
79c3dd81
DZ
1084 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1085 hub_is_superspeed(hub->hdev)) {
1086 need_debounce_delay = true;
1087 clear_port_feature(hub->hdev, port1,
1088 USB_PORT_FEAT_C_BH_PORT_RESET);
1089 }
253e0572
AS
1090 /* We can forget about a "removed" device when there's a
1091 * physical disconnect or the connect status changes.
1092 */
1093 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1094 (portchange & USB_PORT_STAT_C_CONNECTION))
1095 clear_bit(port1, hub->removed_bits);
1096
6ee0b270
AS
1097 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1098 /* Tell khubd to disconnect the device or
1099 * check for a new connection
1100 */
1101 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1102 set_bit(port1, hub->change_bits);
1103
1104 } else if (portstatus & USB_PORT_STAT_ENABLE) {
72937e1e
SS
1105 bool port_resumed = (portstatus &
1106 USB_PORT_STAT_LINK_STATE) ==
1107 USB_SS_PORT_LS_U0;
6ee0b270
AS
1108 /* The power session apparently survived the resume.
1109 * If there was an overcurrent or suspend change
1110 * (i.e., remote wakeup request), have khubd
72937e1e
SS
1111 * take care of it. Look at the port link state
1112 * for USB 3.0 hubs, since they don't have a suspend
1113 * change bit, and they don't set the port link change
1114 * bit on device-initiated resume.
6ee0b270 1115 */
72937e1e
SS
1116 if (portchange || (hub_is_superspeed(hub->hdev) &&
1117 port_resumed))
6ee0b270 1118 set_bit(port1, hub->change_bits);
5e6effae 1119
6ee0b270 1120 } else if (udev->persist_enabled) {
6ee0b270 1121#ifdef CONFIG_PM
5e6effae 1122 udev->reset_resume = 1;
6ee0b270 1123#endif
8808f00c
AS
1124 set_bit(port1, hub->change_bits);
1125
6ee0b270
AS
1126 } else {
1127 /* The power session is gone; tell khubd */
1128 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1129 set_bit(port1, hub->change_bits);
5e6effae 1130 }
5e6effae
AS
1131 }
1132
948fea37
AS
1133 /* If no port-status-change flags were set, we don't need any
1134 * debouncing. If flags were set we can try to debounce the
1135 * ports all at once right now, instead of letting khubd do them
1136 * one at a time later on.
1137 *
1138 * If any port-status changes do occur during this delay, khubd
1139 * will see them later and handle them normally.
1140 */
8520f380
AS
1141 if (need_debounce_delay) {
1142 delay = HUB_DEBOUNCE_STABLE;
1143
1144 /* Don't do a long sleep inside a workqueue routine */
1145 if (type == HUB_INIT2) {
1146 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1147 schedule_delayed_work(&hub->init_work,
1148 msecs_to_jiffies(delay));
1149 return; /* Continues at init3: below */
1150 } else {
1151 msleep(delay);
1152 }
1153 }
1154 init3:
f2835219
AS
1155 hub->quiescing = 0;
1156
1157 status = usb_submit_urb(hub->urb, GFP_NOIO);
1158 if (status < 0)
1159 dev_err(hub->intfdev, "activate --> %d\n", status);
1160 if (hub->has_indicators && blinkenlights)
1161 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1162
1163 /* Scan all ports that need attention */
1164 kick_khubd(hub);
8e4ceb38
AS
1165
1166 /* Allow autosuspend if it was suppressed */
1167 if (type <= HUB_INIT3)
1168 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
5e6effae
AS
1169}
1170
8520f380
AS
1171/* Implement the continuations for the delays above */
1172static void hub_init_func2(struct work_struct *ws)
1173{
1174 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1175
1176 hub_activate(hub, HUB_INIT2);
1177}
1178
1179static void hub_init_func3(struct work_struct *ws)
1180{
1181 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1182
1183 hub_activate(hub, HUB_INIT3);
1184}
1185
4330354f
AS
1186enum hub_quiescing_type {
1187 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1188};
1189
1190static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1191{
1192 struct usb_device *hdev = hub->hdev;
1193 int i;
1194
8520f380
AS
1195 cancel_delayed_work_sync(&hub->init_work);
1196
4330354f
AS
1197 /* khubd and related activity won't re-trigger */
1198 hub->quiescing = 1;
1199
1200 if (type != HUB_SUSPEND) {
1201 /* Disconnect all the children */
1202 for (i = 0; i < hdev->maxchild; ++i) {
ff823c79
LT
1203 if (hub->ports[i]->child)
1204 usb_disconnect(&hub->ports[i]->child);
4330354f
AS
1205 }
1206 }
1207
1208 /* Stop khubd and related activity */
1209 usb_kill_urb(hub->urb);
1210 if (hub->has_indicators)
1211 cancel_delayed_work_sync(&hub->leds);
1212 if (hub->tt.hub)
cb88a1b8 1213 cancel_work_sync(&hub->tt.clear_work);
4330354f
AS
1214}
1215
3eb14915
AS
1216/* caller has locked the hub device */
1217static int hub_pre_reset(struct usb_interface *intf)
1218{
1219 struct usb_hub *hub = usb_get_intfdata(intf);
1220
4330354f 1221 hub_quiesce(hub, HUB_PRE_RESET);
f07600cf 1222 return 0;
7d069b7d
AS
1223}
1224
1225/* caller has locked the hub device */
f07600cf 1226static int hub_post_reset(struct usb_interface *intf)
7d069b7d 1227{
7de18d8b
AS
1228 struct usb_hub *hub = usb_get_intfdata(intf);
1229
f2835219 1230 hub_activate(hub, HUB_POST_RESET);
f07600cf 1231 return 0;
7d069b7d
AS
1232}
1233
fa2a9566
LT
1234static void usb_port_device_release(struct device *dev)
1235{
1236 struct usb_port *port_dev = to_usb_port(dev);
1237
1238 kfree(port_dev);
1239}
1240
1241static void usb_hub_remove_port_device(struct usb_hub *hub,
1242 int port1)
1243{
1244 device_unregister(&hub->ports[port1 - 1]->dev);
1245}
1246
1247struct device_type usb_port_device_type = {
1248 .name = "usb_port",
1249 .release = usb_port_device_release,
1250};
1251
1252static int usb_hub_create_port_device(struct usb_hub *hub,
1253 int port1)
1254{
1255 struct usb_port *port_dev = NULL;
1256 int retval;
1257
1258 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
1259 if (!port_dev) {
1260 retval = -ENOMEM;
1261 goto exit;
1262 }
1263
1264 hub->ports[port1 - 1] = port_dev;
1265 port_dev->dev.parent = hub->intfdev;
1266 port_dev->dev.type = &usb_port_device_type;
1267 dev_set_name(&port_dev->dev, "port%d", port1);
1268
1269 retval = device_register(&port_dev->dev);
1270 if (retval)
1271 goto error_register;
1272 return 0;
1273
1274error_register:
1275 put_device(&port_dev->dev);
1276exit:
1277 return retval;
1278}
1279
1da177e4
LT
1280static int hub_configure(struct usb_hub *hub,
1281 struct usb_endpoint_descriptor *endpoint)
1282{
b356b7c7 1283 struct usb_hcd *hcd;
1da177e4
LT
1284 struct usb_device *hdev = hub->hdev;
1285 struct device *hub_dev = hub->intfdev;
1286 u16 hubstatus, hubchange;
74ad9bd2 1287 u16 wHubCharacteristics;
1da177e4 1288 unsigned int pipe;
fa2a9566 1289 int maxp, ret, i;
7cbe5dca 1290 char *message = "out of memory";
1da177e4 1291
d697cdda 1292 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
1da177e4 1293 if (!hub->buffer) {
1da177e4
LT
1294 ret = -ENOMEM;
1295 goto fail;
1296 }
1297
1298 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1299 if (!hub->status) {
1da177e4
LT
1300 ret = -ENOMEM;
1301 goto fail;
1302 }
db90e7a1 1303 mutex_init(&hub->status_mutex);
1da177e4
LT
1304
1305 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1306 if (!hub->descriptor) {
1da177e4
LT
1307 ret = -ENOMEM;
1308 goto fail;
1309 }
1310
1311 /* Request the entire hub descriptor.
1312 * hub->descriptor can handle USB_MAXCHILDREN ports,
1313 * but the hub can/will return fewer bytes here.
1314 */
dbe79bbe 1315 ret = get_hub_descriptor(hdev, hub->descriptor);
1da177e4
LT
1316 if (ret < 0) {
1317 message = "can't read hub descriptor";
1318 goto fail;
1319 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1320 message = "hub has too many ports!";
1321 ret = -ENODEV;
1322 goto fail;
1323 }
1324
1325 hdev->maxchild = hub->descriptor->bNbrPorts;
1326 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1327 (hdev->maxchild == 1) ? "" : "s");
1328
fa2a9566
LT
1329 hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1330 GFP_KERNEL);
ff823c79 1331 if (!hub->ports) {
7cbe5dca
AS
1332 ret = -ENOMEM;
1333 goto fail;
1334 }
1335
74ad9bd2 1336 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
1da177e4 1337
dbe79bbe
JY
1338 /* FIXME for USB 3.0, skip for now */
1339 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1340 !(hub_is_superspeed(hdev))) {
1da177e4
LT
1341 int i;
1342 char portstr [USB_MAXCHILDREN + 1];
1343
1344 for (i = 0; i < hdev->maxchild; i++)
dbe79bbe 1345 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
1da177e4
LT
1346 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1347 ? 'F' : 'R';
1348 portstr[hdev->maxchild] = 0;
1349 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1350 } else
1351 dev_dbg(hub_dev, "standalone hub\n");
1352
74ad9bd2 1353 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
7bf01185
AD
1354 case HUB_CHAR_COMMON_LPSM:
1355 dev_dbg(hub_dev, "ganged power switching\n");
1356 break;
1357 case HUB_CHAR_INDV_PORT_LPSM:
1358 dev_dbg(hub_dev, "individual port power switching\n");
1359 break;
1360 case HUB_CHAR_NO_LPSM:
1361 case HUB_CHAR_LPSM:
1362 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1363 break;
1da177e4
LT
1364 }
1365
74ad9bd2 1366 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
7bf01185
AD
1367 case HUB_CHAR_COMMON_OCPM:
1368 dev_dbg(hub_dev, "global over-current protection\n");
1369 break;
1370 case HUB_CHAR_INDV_PORT_OCPM:
1371 dev_dbg(hub_dev, "individual port over-current protection\n");
1372 break;
1373 case HUB_CHAR_NO_OCPM:
1374 case HUB_CHAR_OCPM:
1375 dev_dbg(hub_dev, "no over-current protection\n");
1376 break;
1da177e4
LT
1377 }
1378
1379 spin_lock_init (&hub->tt.lock);
1380 INIT_LIST_HEAD (&hub->tt.clear_list);
cb88a1b8 1381 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
1da177e4 1382 switch (hdev->descriptor.bDeviceProtocol) {
7bf01185
AD
1383 case USB_HUB_PR_FS:
1384 break;
1385 case USB_HUB_PR_HS_SINGLE_TT:
1386 dev_dbg(hub_dev, "Single TT\n");
1387 hub->tt.hub = hdev;
1388 break;
1389 case USB_HUB_PR_HS_MULTI_TT:
1390 ret = usb_set_interface(hdev, 0, 1);
1391 if (ret == 0) {
1392 dev_dbg(hub_dev, "TT per port\n");
1393 hub->tt.multi = 1;
1394 } else
1395 dev_err(hub_dev, "Using single TT (err %d)\n",
1396 ret);
1397 hub->tt.hub = hdev;
1398 break;
1399 case USB_HUB_PR_SS:
1400 /* USB 3.0 hubs don't have a TT */
1401 break;
1402 default:
1403 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1404 hdev->descriptor.bDeviceProtocol);
1405 break;
1da177e4
LT
1406 }
1407
e09711ae 1408 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
74ad9bd2 1409 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
e09711ae
DB
1410 case HUB_TTTT_8_BITS:
1411 if (hdev->descriptor.bDeviceProtocol != 0) {
1412 hub->tt.think_time = 666;
1413 dev_dbg(hub_dev, "TT requires at most %d "
1414 "FS bit times (%d ns)\n",
1415 8, hub->tt.think_time);
1416 }
1da177e4 1417 break;
e09711ae
DB
1418 case HUB_TTTT_16_BITS:
1419 hub->tt.think_time = 666 * 2;
1420 dev_dbg(hub_dev, "TT requires at most %d "
1421 "FS bit times (%d ns)\n",
1422 16, hub->tt.think_time);
1da177e4 1423 break;
e09711ae
DB
1424 case HUB_TTTT_24_BITS:
1425 hub->tt.think_time = 666 * 3;
1426 dev_dbg(hub_dev, "TT requires at most %d "
1427 "FS bit times (%d ns)\n",
1428 24, hub->tt.think_time);
1da177e4 1429 break;
e09711ae
DB
1430 case HUB_TTTT_32_BITS:
1431 hub->tt.think_time = 666 * 4;
1432 dev_dbg(hub_dev, "TT requires at most %d "
1433 "FS bit times (%d ns)\n",
1434 32, hub->tt.think_time);
1da177e4
LT
1435 break;
1436 }
1437
1438 /* probe() zeroes hub->indicator[] */
74ad9bd2 1439 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
1da177e4
LT
1440 hub->has_indicators = 1;
1441 dev_dbg(hub_dev, "Port indicators are supported\n");
1442 }
1443
1444 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1445 hub->descriptor->bPwrOn2PwrGood * 2);
1446
1447 /* power budgeting mostly matters with bus-powered hubs,
1448 * and battery-powered root hubs (may provide just 8 mA).
1449 */
1450 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
55c52718 1451 if (ret < 2) {
1da177e4
LT
1452 message = "can't get hub status";
1453 goto fail;
1454 }
7d35b929
AS
1455 le16_to_cpus(&hubstatus);
1456 if (hdev == hdev->bus->root_hub) {
55c52718
AS
1457 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1458 hub->mA_per_port = 500;
1459 else {
1460 hub->mA_per_port = hdev->bus_mA;
1461 hub->limited_power = 1;
1462 }
7d35b929 1463 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
1da177e4
LT
1464 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1465 hub->descriptor->bHubContrCurrent);
55c52718
AS
1466 hub->limited_power = 1;
1467 if (hdev->maxchild > 0) {
1468 int remaining = hdev->bus_mA -
1469 hub->descriptor->bHubContrCurrent;
1470
1471 if (remaining < hdev->maxchild * 100)
1472 dev_warn(hub_dev,
1473 "insufficient power available "
1474 "to use all downstream ports\n");
1475 hub->mA_per_port = 100; /* 7.2.1.1 */
1476 }
1477 } else { /* Self-powered external hub */
1478 /* FIXME: What about battery-powered external hubs that
1479 * provide less current per port? */
1480 hub->mA_per_port = 500;
7d35b929 1481 }
55c52718
AS
1482 if (hub->mA_per_port < 500)
1483 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1484 hub->mA_per_port);
1da177e4 1485
b356b7c7
SS
1486 /* Update the HCD's internal representation of this hub before khubd
1487 * starts getting port status changes for devices under the hub.
1488 */
1489 hcd = bus_to_hcd(hdev->bus);
1490 if (hcd->driver->update_hub_device) {
1491 ret = hcd->driver->update_hub_device(hcd, hdev,
1492 &hub->tt, GFP_KERNEL);
1493 if (ret < 0) {
1494 message = "can't update HCD hub info";
1495 goto fail;
1496 }
1497 }
1498
1da177e4
LT
1499 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1500 if (ret < 0) {
1501 message = "can't get hub status";
1502 goto fail;
1503 }
1504
1505 /* local power status reports aren't always correct */
1506 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1507 dev_dbg(hub_dev, "local power source is %s\n",
1508 (hubstatus & HUB_STATUS_LOCAL_POWER)
1509 ? "lost (inactive)" : "good");
1510
74ad9bd2 1511 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
1da177e4
LT
1512 dev_dbg(hub_dev, "%sover-current condition exists\n",
1513 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1514
88fafff9 1515 /* set up the interrupt endpoint
1516 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1517 * bytes as USB2.0[11.12.3] says because some hubs are known
1518 * to send more data (and thus cause overflow). For root hubs,
1519 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1520 * to be big enough for at least USB_MAXCHILDREN ports. */
1da177e4
LT
1521 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1522 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1523
1524 if (maxp > sizeof(*hub->buffer))
1525 maxp = sizeof(*hub->buffer);
1526
1527 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1528 if (!hub->urb) {
1da177e4
LT
1529 ret = -ENOMEM;
1530 goto fail;
1531 }
1532
1533 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1534 hub, endpoint->bInterval);
1da177e4
LT
1535
1536 /* maybe cycle the hub leds */
1537 if (hub->has_indicators && blinkenlights)
1538 hub->indicator [0] = INDICATOR_CYCLE;
1539
fa2a9566
LT
1540 for (i = 0; i < hdev->maxchild; i++)
1541 if (usb_hub_create_port_device(hub, i + 1) < 0)
1542 dev_err(hub->intfdev,
1543 "couldn't create port%d device.\n", i + 1);
1544
f2835219 1545 hub_activate(hub, HUB_INIT);
1da177e4
LT
1546 return 0;
1547
1548fail:
1549 dev_err (hub_dev, "config failed, %s (err %d)\n",
1550 message, ret);
1551 /* hub_disconnect() frees urb and descriptor */
1552 return ret;
1553}
1554
e8054854
AS
1555static void hub_release(struct kref *kref)
1556{
1557 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1558
1559 usb_put_intf(to_usb_interface(hub->intfdev));
1560 kfree(hub);
1561}
1562
1da177e4
LT
1563static unsigned highspeed_hubs;
1564
1565static void hub_disconnect(struct usb_interface *intf)
1566{
8816230e 1567 struct usb_hub *hub = usb_get_intfdata(intf);
fa286188 1568 struct usb_device *hdev = interface_to_usbdev(intf);
fa2a9566
LT
1569 int i;
1570
e8054854
AS
1571 /* Take the hub off the event list and don't let it be added again */
1572 spin_lock_irq(&hub_event_lock);
8e4ceb38
AS
1573 if (!list_empty(&hub->event_list)) {
1574 list_del_init(&hub->event_list);
1575 usb_autopm_put_interface_no_suspend(intf);
1576 }
e8054854
AS
1577 hub->disconnected = 1;
1578 spin_unlock_irq(&hub_event_lock);
1da177e4 1579
7de18d8b
AS
1580 /* Disconnect all children and quiesce the hub */
1581 hub->error = 0;
4330354f 1582 hub_quiesce(hub, HUB_DISCONNECT);
7de18d8b 1583
8b28c752 1584 usb_set_intfdata (intf, NULL);
1f2235b8
AS
1585
1586 for (i = 0; i < hdev->maxchild; i++)
1587 usb_hub_remove_port_device(hub, i + 1);
7cbe5dca 1588 hub->hdev->maxchild = 0;
1da177e4 1589
e8054854 1590 if (hub->hdev->speed == USB_SPEED_HIGH)
1da177e4
LT
1591 highspeed_hubs--;
1592
1da177e4 1593 usb_free_urb(hub->urb);
fa2a9566 1594 kfree(hub->ports);
1bc3c9e1 1595 kfree(hub->descriptor);
1bc3c9e1 1596 kfree(hub->status);
d697cdda 1597 kfree(hub->buffer);
1da177e4 1598
e8054854 1599 kref_put(&hub->kref, hub_release);
1da177e4
LT
1600}
1601
1602static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1603{
1604 struct usb_host_interface *desc;
1605 struct usb_endpoint_descriptor *endpoint;
1606 struct usb_device *hdev;
1607 struct usb_hub *hub;
1608
1609 desc = intf->cur_altsetting;
1610 hdev = interface_to_usbdev(intf);
1611
2839f5bc
SS
1612 /* Hubs have proper suspend/resume support. */
1613 usb_enable_autosuspend(hdev);
088f7fec 1614
38f3ad5e 1615 if (hdev->level == MAX_TOPO_LEVEL) {
b9cef6c3
WF
1616 dev_err(&intf->dev,
1617 "Unsupported bus topology: hub nested too deep\n");
38f3ad5e
FB
1618 return -E2BIG;
1619 }
1620
89ccbdc9
DB
1621#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1622 if (hdev->parent) {
1623 dev_warn(&intf->dev, "ignoring external hub\n");
1624 return -ENODEV;
1625 }
1626#endif
1627
1da177e4
LT
1628 /* Some hubs have a subclass of 1, which AFAICT according to the */
1629 /* specs is not defined, but it works */
1630 if ((desc->desc.bInterfaceSubClass != 0) &&
1631 (desc->desc.bInterfaceSubClass != 1)) {
1632descriptor_error:
1633 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1634 return -EIO;
1635 }
1636
1637 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1638 if (desc->desc.bNumEndpoints != 1)
1639 goto descriptor_error;
1640
1641 endpoint = &desc->endpoint[0].desc;
1642
fbf81c29
LFC
1643 /* If it's not an interrupt in endpoint, we'd better punt! */
1644 if (!usb_endpoint_is_int_in(endpoint))
1da177e4
LT
1645 goto descriptor_error;
1646
1647 /* We found a hub */
1648 dev_info (&intf->dev, "USB hub found\n");
1649
0a1ef3b5 1650 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
1da177e4
LT
1651 if (!hub) {
1652 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1653 return -ENOMEM;
1654 }
1655
e8054854 1656 kref_init(&hub->kref);
1da177e4
LT
1657 INIT_LIST_HEAD(&hub->event_list);
1658 hub->intfdev = &intf->dev;
1659 hub->hdev = hdev;
c4028958 1660 INIT_DELAYED_WORK(&hub->leds, led_work);
8520f380 1661 INIT_DELAYED_WORK(&hub->init_work, NULL);
e8054854 1662 usb_get_intf(intf);
1da177e4
LT
1663
1664 usb_set_intfdata (intf, hub);
40f122f3 1665 intf->needs_remote_wakeup = 1;
1da177e4
LT
1666
1667 if (hdev->speed == USB_SPEED_HIGH)
1668 highspeed_hubs++;
1669
1670 if (hub_configure(hub, endpoint) >= 0)
1671 return 0;
1672
1673 hub_disconnect (intf);
1674 return -ENODEV;
1675}
1676
1677static int
1678hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1679{
1680 struct usb_device *hdev = interface_to_usbdev (intf);
ff823c79 1681 struct usb_hub *hub = hdev_to_hub(hdev);
1da177e4
LT
1682
1683 /* assert ifno == 0 (part of hub spec) */
1684 switch (code) {
1685 case USBDEVFS_HUB_PORTINFO: {
1686 struct usbdevfs_hub_portinfo *info = user_data;
1687 int i;
1688
1689 spin_lock_irq(&device_state_lock);
1690 if (hdev->devnum <= 0)
1691 info->nports = 0;
1692 else {
1693 info->nports = hdev->maxchild;
1694 for (i = 0; i < info->nports; i++) {
ff823c79 1695 if (hub->ports[i]->child == NULL)
1da177e4
LT
1696 info->port[i] = 0;
1697 else
1698 info->port[i] =
ff823c79 1699 hub->ports[i]->child->devnum;
1da177e4
LT
1700 }
1701 }
1702 spin_unlock_irq(&device_state_lock);
1703
1704 return info->nports + 1;
1705 }
1706
1707 default:
1708 return -ENOSYS;
1709 }
1710}
1711
7cbe5dca
AS
1712/*
1713 * Allow user programs to claim ports on a hub. When a device is attached
1714 * to one of these "claimed" ports, the program will "own" the device.
1715 */
1716static int find_port_owner(struct usb_device *hdev, unsigned port1,
336c5c31 1717 struct dev_state ***ppowner)
7cbe5dca
AS
1718{
1719 if (hdev->state == USB_STATE_NOTATTACHED)
1720 return -ENODEV;
1721 if (port1 == 0 || port1 > hdev->maxchild)
1722 return -EINVAL;
1723
1724 /* This assumes that devices not managed by the hub driver
1725 * will always have maxchild equal to 0.
1726 */
fa2a9566 1727 *ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
7cbe5dca
AS
1728 return 0;
1729}
1730
1731/* In the following three functions, the caller must hold hdev's lock */
336c5c31
LT
1732int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1733 struct dev_state *owner)
7cbe5dca
AS
1734{
1735 int rc;
336c5c31 1736 struct dev_state **powner;
7cbe5dca
AS
1737
1738 rc = find_port_owner(hdev, port1, &powner);
1739 if (rc)
1740 return rc;
1741 if (*powner)
1742 return -EBUSY;
1743 *powner = owner;
1744 return rc;
1745}
1746
336c5c31
LT
1747int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1748 struct dev_state *owner)
7cbe5dca
AS
1749{
1750 int rc;
336c5c31 1751 struct dev_state **powner;
7cbe5dca
AS
1752
1753 rc = find_port_owner(hdev, port1, &powner);
1754 if (rc)
1755 return rc;
1756 if (*powner != owner)
1757 return -ENOENT;
1758 *powner = NULL;
1759 return rc;
1760}
1761
336c5c31 1762void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
7cbe5dca 1763{
fa2a9566 1764 struct usb_hub *hub = hdev_to_hub(hdev);
7cbe5dca 1765 int n;
7cbe5dca 1766
fa2a9566
LT
1767 for (n = 0; n < hdev->maxchild; n++) {
1768 if (hub->ports[n]->port_owner == owner)
1769 hub->ports[n]->port_owner = NULL;
7cbe5dca 1770 }
fa2a9566 1771
7cbe5dca
AS
1772}
1773
1774/* The caller must hold udev's lock */
1775bool usb_device_is_owned(struct usb_device *udev)
1776{
1777 struct usb_hub *hub;
1778
1779 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1780 return false;
1781 hub = hdev_to_hub(udev->parent);
fa2a9566 1782 return !!hub->ports[udev->portnum - 1]->port_owner;
7cbe5dca
AS
1783}
1784
1da177e4
LT
1785static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1786{
ff823c79 1787 struct usb_hub *hub = hdev_to_hub(udev);
1da177e4
LT
1788 int i;
1789
1790 for (i = 0; i < udev->maxchild; ++i) {
ff823c79
LT
1791 if (hub->ports[i]->child)
1792 recursively_mark_NOTATTACHED(hub->ports[i]->child);
1da177e4 1793 }
9bbdf1e0 1794 if (udev->state == USB_STATE_SUSPENDED)
15123006 1795 udev->active_duration -= jiffies;
1da177e4
LT
1796 udev->state = USB_STATE_NOTATTACHED;
1797}
1798
1799/**
1800 * usb_set_device_state - change a device's current state (usbcore, hcds)
1801 * @udev: pointer to device whose state should be changed
1802 * @new_state: new state value to be stored
1803 *
1804 * udev->state is _not_ fully protected by the device lock. Although
1805 * most transitions are made only while holding the lock, the state can
1806 * can change to USB_STATE_NOTATTACHED at almost any time. This
1807 * is so that devices can be marked as disconnected as soon as possible,
1808 * without having to wait for any semaphores to be released. As a result,
1809 * all changes to any device's state must be protected by the
1810 * device_state_lock spinlock.
1811 *
1812 * Once a device has been added to the device tree, all changes to its state
1813 * should be made using this routine. The state should _not_ be set directly.
1814 *
1815 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1816 * Otherwise udev->state is set to new_state, and if new_state is
1817 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1818 * to USB_STATE_NOTATTACHED.
1819 */
1820void usb_set_device_state(struct usb_device *udev,
1821 enum usb_device_state new_state)
1822{
1823 unsigned long flags;
4681b171 1824 int wakeup = -1;
1da177e4
LT
1825
1826 spin_lock_irqsave(&device_state_lock, flags);
1827 if (udev->state == USB_STATE_NOTATTACHED)
1828 ; /* do nothing */
b94dc6b5 1829 else if (new_state != USB_STATE_NOTATTACHED) {
fb669cc0
DB
1830
1831 /* root hub wakeup capabilities are managed out-of-band
1832 * and may involve silicon errata ... ignore them here.
1833 */
1834 if (udev->parent) {
645daaab
AS
1835 if (udev->state == USB_STATE_SUSPENDED
1836 || new_state == USB_STATE_SUSPENDED)
1837 ; /* No change to wakeup settings */
1838 else if (new_state == USB_STATE_CONFIGURED)
4681b171
RW
1839 wakeup = udev->actconfig->desc.bmAttributes
1840 & USB_CONFIG_ATT_WAKEUP;
645daaab 1841 else
4681b171 1842 wakeup = 0;
fb669cc0 1843 }
15123006
SS
1844 if (udev->state == USB_STATE_SUSPENDED &&
1845 new_state != USB_STATE_SUSPENDED)
1846 udev->active_duration -= jiffies;
1847 else if (new_state == USB_STATE_SUSPENDED &&
1848 udev->state != USB_STATE_SUSPENDED)
1849 udev->active_duration += jiffies;
645daaab 1850 udev->state = new_state;
b94dc6b5 1851 } else
1da177e4
LT
1852 recursively_mark_NOTATTACHED(udev);
1853 spin_unlock_irqrestore(&device_state_lock, flags);
4681b171
RW
1854 if (wakeup >= 0)
1855 device_set_wakeup_capable(&udev->dev, wakeup);
1da177e4 1856}
6da9c990 1857EXPORT_SYMBOL_GPL(usb_set_device_state);
1da177e4 1858
8af548dc 1859/*
3b29b68b
AS
1860 * Choose a device number.
1861 *
1862 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1863 * USB-2.0 buses they are also used as device addresses, however on
1864 * USB-3.0 buses the address is assigned by the controller hardware
1865 * and it usually is not the same as the device number.
1866 *
8af548dc
IPG
1867 * WUSB devices are simple: they have no hubs behind, so the mapping
1868 * device <-> virtual port number becomes 1:1. Why? to simplify the
1869 * life of the device connection logic in
1870 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1871 * handshake we need to assign a temporary address in the unauthorized
1872 * space. For simplicity we use the first virtual port number found to
1873 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1874 * and that becomes it's address [X < 128] or its unauthorized address
1875 * [X | 0x80].
1876 *
1877 * We add 1 as an offset to the one-based USB-stack port number
1878 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1879 * 0 is reserved by USB for default address; (b) Linux's USB stack
1880 * uses always #1 for the root hub of the controller. So USB stack's
1881 * port #1, which is wusb virtual-port #0 has address #2.
c6515272
SS
1882 *
1883 * Devices connected under xHCI are not as simple. The host controller
1884 * supports virtualization, so the hardware assigns device addresses and
1885 * the HCD must setup data structures before issuing a set address
1886 * command to the hardware.
8af548dc 1887 */
3b29b68b 1888static void choose_devnum(struct usb_device *udev)
1da177e4
LT
1889{
1890 int devnum;
1891 struct usb_bus *bus = udev->bus;
1892
1893 /* If khubd ever becomes multithreaded, this will need a lock */
8af548dc
IPG
1894 if (udev->wusb) {
1895 devnum = udev->portnum + 1;
1896 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1897 } else {
1898 /* Try to allocate the next devnum beginning at
1899 * bus->devnum_next. */
1900 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1901 bus->devnum_next);
1902 if (devnum >= 128)
1903 devnum = find_next_zero_bit(bus->devmap.devicemap,
1904 128, 1);
1905 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1906 }
1da177e4
LT
1907 if (devnum < 128) {
1908 set_bit(devnum, bus->devmap.devicemap);
1909 udev->devnum = devnum;
1910 }
1911}
1912
3b29b68b 1913static void release_devnum(struct usb_device *udev)
1da177e4
LT
1914{
1915 if (udev->devnum > 0) {
1916 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1917 udev->devnum = -1;
1918 }
1919}
1920
3b29b68b 1921static void update_devnum(struct usb_device *udev, int devnum)
4953d141
DV
1922{
1923 /* The address for a WUSB device is managed by wusbcore. */
1924 if (!udev->wusb)
1925 udev->devnum = devnum;
1926}
1927
f7410ced
HX
1928static void hub_free_dev(struct usb_device *udev)
1929{
1930 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1931
1932 /* Root hubs aren't real devices, so don't free HCD resources */
1933 if (hcd->driver->free_dev && udev->parent)
1934 hcd->driver->free_dev(hcd, udev);
1935}
1936
1da177e4
LT
1937/**
1938 * usb_disconnect - disconnect a device (usbcore-internal)
1939 * @pdev: pointer to device being disconnected
1940 * Context: !in_interrupt ()
1941 *
1942 * Something got disconnected. Get rid of it and all of its children.
1943 *
1944 * If *pdev is a normal device then the parent hub must already be locked.
1945 * If *pdev is a root hub then this routine will acquire the
1946 * usb_bus_list_lock on behalf of the caller.
1947 *
1948 * Only hub drivers (including virtual root hub drivers for host
1949 * controllers) should ever call this.
1950 *
1951 * This call is synchronous, and may not be used in an interrupt context.
1952 */
1953void usb_disconnect(struct usb_device **pdev)
1954{
1955 struct usb_device *udev = *pdev;
ff823c79 1956 struct usb_hub *hub = hdev_to_hub(udev);
1da177e4
LT
1957 int i;
1958
1da177e4
LT
1959 /* mark the device as inactive, so any further urb submissions for
1960 * this device (and any of its children) will fail immediately.
25985edc 1961 * this quiesces everything except pending urbs.
1da177e4
LT
1962 */
1963 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
3b29b68b
AS
1964 dev_info(&udev->dev, "USB disconnect, device number %d\n",
1965 udev->devnum);
1da177e4 1966
9ad3d6cc
AS
1967 usb_lock_device(udev);
1968
1da177e4 1969 /* Free up all the children before we remove this device */
8816230e 1970 for (i = 0; i < udev->maxchild; i++) {
ff823c79
LT
1971 if (hub->ports[i]->child)
1972 usb_disconnect(&hub->ports[i]->child);
1da177e4
LT
1973 }
1974
1975 /* deallocate hcd/hardware state ... nuking all pending urbs and
1976 * cleaning up all state associated with the current configuration
1977 * so that the hardware is now fully quiesced.
1978 */
782da727 1979 dev_dbg (&udev->dev, "unregistering device\n");
1da177e4 1980 usb_disable_device(udev, 0);
cde217a5 1981 usb_hcd_synchronize_unlinks(udev);
1da177e4 1982
3b23dd6f 1983 usb_remove_ep_devs(&udev->ep0);
782da727
AS
1984 usb_unlock_device(udev);
1985
1986 /* Unregister the device. The device driver is responsible
3b23dd6f
AS
1987 * for de-configuring the device and invoking the remove-device
1988 * notifier chain (used by usbfs and possibly others).
782da727
AS
1989 */
1990 device_del(&udev->dev);
3099e75a 1991
782da727 1992 /* Free the device number and delete the parent's children[]
1da177e4
LT
1993 * (or root_hub) pointer.
1994 */
3b29b68b 1995 release_devnum(udev);
1da177e4
LT
1996
1997 /* Avoid races with recursively_mark_NOTATTACHED() */
1998 spin_lock_irq(&device_state_lock);
1999 *pdev = NULL;
2000 spin_unlock_irq(&device_state_lock);
2001
f7410ced
HX
2002 hub_free_dev(udev);
2003
782da727 2004 put_device(&udev->dev);
1da177e4
LT
2005}
2006
f2a383e4 2007#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
1da177e4
LT
2008static void show_string(struct usb_device *udev, char *id, char *string)
2009{
2010 if (!string)
2011 return;
2012 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
2013}
2014
f2a383e4
GKH
2015static void announce_device(struct usb_device *udev)
2016{
2017 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2018 le16_to_cpu(udev->descriptor.idVendor),
2019 le16_to_cpu(udev->descriptor.idProduct));
b9cef6c3
WF
2020 dev_info(&udev->dev,
2021 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
f2a383e4
GKH
2022 udev->descriptor.iManufacturer,
2023 udev->descriptor.iProduct,
2024 udev->descriptor.iSerialNumber);
2025 show_string(udev, "Product", udev->product);
2026 show_string(udev, "Manufacturer", udev->manufacturer);
2027 show_string(udev, "SerialNumber", udev->serial);
2028}
1da177e4 2029#else
f2a383e4 2030static inline void announce_device(struct usb_device *udev) { }
1da177e4
LT
2031#endif
2032
1da177e4
LT
2033#ifdef CONFIG_USB_OTG
2034#include "otg_whitelist.h"
2035#endif
2036
3ede760f 2037/**
8d8558d1 2038 * usb_enumerate_device_otg - FIXME (usbcore-internal)
3ede760f
ON
2039 * @udev: newly addressed device (in ADDRESS state)
2040 *
8d8558d1 2041 * Finish enumeration for On-The-Go devices
3ede760f 2042 */
8d8558d1 2043static int usb_enumerate_device_otg(struct usb_device *udev)
1da177e4 2044{
d9d16e8a 2045 int err = 0;
1da177e4
LT
2046
2047#ifdef CONFIG_USB_OTG
2048 /*
2049 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2050 * to wake us after we've powered off VBUS; and HNP, switching roles
2051 * "host" to "peripheral". The OTG descriptor helps figure this out.
2052 */
2053 if (!udev->bus->is_b_host
2054 && udev->config
2055 && udev->parent == udev->bus->root_hub) {
2eb5052e 2056 struct usb_otg_descriptor *desc = NULL;
1da177e4
LT
2057 struct usb_bus *bus = udev->bus;
2058
2059 /* descriptor may appear anywhere in config */
2060 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2061 le16_to_cpu(udev->config[0].desc.wTotalLength),
2062 USB_DT_OTG, (void **) &desc) == 0) {
2063 if (desc->bmAttributes & USB_OTG_HNP) {
12c3da34 2064 unsigned port1 = udev->portnum;
fc849b85 2065
1da177e4
LT
2066 dev_info(&udev->dev,
2067 "Dual-Role OTG device on %sHNP port\n",
2068 (port1 == bus->otg_port)
2069 ? "" : "non-");
2070
2071 /* enable HNP before suspend, it's simpler */
2072 if (port1 == bus->otg_port)
2073 bus->b_hnp_enable = 1;
2074 err = usb_control_msg(udev,
2075 usb_sndctrlpipe(udev, 0),
2076 USB_REQ_SET_FEATURE, 0,
2077 bus->b_hnp_enable
2078 ? USB_DEVICE_B_HNP_ENABLE
2079 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2080 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2081 if (err < 0) {
2082 /* OTG MESSAGE: report errors here,
2083 * customize to match your product.
2084 */
2085 dev_info(&udev->dev,
b9cef6c3 2086 "can't set HNP mode: %d\n",
1da177e4
LT
2087 err);
2088 bus->b_hnp_enable = 0;
2089 }
2090 }
2091 }
2092 }
2093
2094 if (!is_targeted(udev)) {
2095
2096 /* Maybe it can talk to us, though we can't talk to it.
2097 * (Includes HNP test device.)
2098 */
2099 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
634a84f8 2100 err = usb_port_suspend(udev, PMSG_SUSPEND);
1da177e4
LT
2101 if (err < 0)
2102 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2103 }
ffcdc18d 2104 err = -ENOTSUPP;
1da177e4
LT
2105 goto fail;
2106 }
d9d16e8a 2107fail:
1da177e4 2108#endif
d9d16e8a
IPG
2109 return err;
2110}
2111
2112
2113/**
8d8558d1 2114 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
d9d16e8a
IPG
2115 * @udev: newly addressed device (in ADDRESS state)
2116 *
2117 * This is only called by usb_new_device() and usb_authorize_device()
2118 * and FIXME -- all comments that apply to them apply here wrt to
2119 * environment.
2120 *
2121 * If the device is WUSB and not authorized, we don't attempt to read
2122 * the string descriptors, as they will be errored out by the device
2123 * until it has been authorized.
2124 */
8d8558d1 2125static int usb_enumerate_device(struct usb_device *udev)
d9d16e8a
IPG
2126{
2127 int err;
1da177e4 2128
d9d16e8a
IPG
2129 if (udev->config == NULL) {
2130 err = usb_get_configuration(udev);
2131 if (err < 0) {
2132 dev_err(&udev->dev, "can't read configurations, error %d\n",
2133 err);
80da2e0d 2134 return err;
d9d16e8a
IPG
2135 }
2136 }
2137 if (udev->wusb == 1 && udev->authorized == 0) {
2138 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2139 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2140 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2141 }
2142 else {
2143 /* read the standard strings and cache them if present */
2144 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2145 udev->manufacturer = usb_cache_string(udev,
2146 udev->descriptor.iManufacturer);
2147 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2148 }
8d8558d1 2149 err = usb_enumerate_device_otg(udev);
80da2e0d
LP
2150 if (err < 0)
2151 return err;
2152
2153 usb_detect_interface_quirks(udev);
2154
2155 return 0;
d9d16e8a
IPG
2156}
2157
d35e70d5
MG
2158static void set_usb_port_removable(struct usb_device *udev)
2159{
2160 struct usb_device *hdev = udev->parent;
2161 struct usb_hub *hub;
2162 u8 port = udev->portnum;
2163 u16 wHubCharacteristics;
2164 bool removable = true;
2165
2166 if (!hdev)
2167 return;
2168
2169 hub = hdev_to_hub(udev->parent);
2170
2171 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2172
2173 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2174 return;
2175
2176 if (hub_is_superspeed(hdev)) {
ca3c1539
LT
2177 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2178 & (1 << port))
d35e70d5
MG
2179 removable = false;
2180 } else {
2181 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2182 removable = false;
2183 }
2184
2185 if (removable)
2186 udev->removable = USB_DEVICE_REMOVABLE;
2187 else
2188 udev->removable = USB_DEVICE_FIXED;
2189}
d9d16e8a
IPG
2190
2191/**
2192 * usb_new_device - perform initial device setup (usbcore-internal)
2193 * @udev: newly addressed device (in ADDRESS state)
2194 *
8d8558d1
AS
2195 * This is called with devices which have been detected but not fully
2196 * enumerated. The device descriptor is available, but not descriptors
d9d16e8a
IPG
2197 * for any device configuration. The caller must have locked either
2198 * the parent hub (if udev is a normal device) or else the
2199 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2200 * udev has already been installed, but udev is not yet visible through
2201 * sysfs or other filesystem code.
2202 *
2203 * It will return if the device is configured properly or not. Zero if
2204 * the interface was registered with the driver core; else a negative
2205 * errno value.
2206 *
2207 * This call is synchronous, and may not be used in an interrupt context.
2208 *
2209 * Only the hub driver or root-hub registrar should ever call this.
2210 */
2211int usb_new_device(struct usb_device *udev)
2212{
2213 int err;
2214
16985408 2215 if (udev->parent) {
16985408
DS
2216 /* Initialize non-root-hub device wakeup to disabled;
2217 * device (un)configuration controls wakeup capable
2218 * sysfs power/wakeup controls wakeup enabled/disabled
2219 */
2220 device_init_wakeup(&udev->dev, 0);
16985408
DS
2221 }
2222
9bbdf1e0
AS
2223 /* Tell the runtime-PM framework the device is active */
2224 pm_runtime_set_active(&udev->dev);
c08512c7 2225 pm_runtime_get_noresume(&udev->dev);
fcc4a01e 2226 pm_runtime_use_autosuspend(&udev->dev);
9bbdf1e0
AS
2227 pm_runtime_enable(&udev->dev);
2228
c08512c7
AS
2229 /* By default, forbid autosuspend for all devices. It will be
2230 * allowed for hubs during binding.
2231 */
2232 usb_disable_autosuspend(udev);
2233
8d8558d1 2234 err = usb_enumerate_device(udev); /* Read descriptors */
d9d16e8a
IPG
2235 if (err < 0)
2236 goto fail;
c6515272
SS
2237 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2238 udev->devnum, udev->bus->busnum,
2239 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
9f8b17e6
KS
2240 /* export the usbdev device-node for libusb */
2241 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2242 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2243
6cd13201
AS
2244 /* Tell the world! */
2245 announce_device(udev);
195af2cc 2246
b04b3156
TT
2247 if (udev->serial)
2248 add_device_randomness(udev->serial, strlen(udev->serial));
2249 if (udev->product)
2250 add_device_randomness(udev->product, strlen(udev->product));
2251 if (udev->manufacturer)
2252 add_device_randomness(udev->manufacturer,
2253 strlen(udev->manufacturer));
2254
927bc916 2255 device_enable_async_suspend(&udev->dev);
d35e70d5
MG
2256
2257 /*
2258 * check whether the hub marks this port as non-removable. Do it
2259 * now so that platform-specific data can override it in
2260 * device_add()
2261 */
2262 if (udev->parent)
2263 set_usb_port_removable(udev);
2264
782da727 2265 /* Register the device. The device driver is responsible
3b23dd6f
AS
2266 * for configuring the device and invoking the add-device
2267 * notifier chain (used by usbfs and possibly others).
782da727 2268 */
9f8b17e6 2269 err = device_add(&udev->dev);
1da177e4
LT
2270 if (err) {
2271 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2272 goto fail;
2273 }
9ad3d6cc 2274
3b23dd6f 2275 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
c08512c7
AS
2276 usb_mark_last_busy(udev);
2277 pm_runtime_put_sync_autosuspend(&udev->dev);
c066475e 2278 return err;
1da177e4
LT
2279
2280fail:
2281 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
9bbdf1e0
AS
2282 pm_runtime_disable(&udev->dev);
2283 pm_runtime_set_suspended(&udev->dev);
d9d16e8a 2284 return err;
1da177e4
LT
2285}
2286
93993a0a
IPG
2287
2288/**
fd39c86b
RD
2289 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2290 * @usb_dev: USB device
2291 *
2292 * Move the USB device to a very basic state where interfaces are disabled
2293 * and the device is in fact unconfigured and unusable.
93993a0a
IPG
2294 *
2295 * We share a lock (that we have) with device_del(), so we need to
2296 * defer its call.
2297 */
2298int usb_deauthorize_device(struct usb_device *usb_dev)
2299{
93993a0a
IPG
2300 usb_lock_device(usb_dev);
2301 if (usb_dev->authorized == 0)
2302 goto out_unauthorized;
da307123 2303
93993a0a
IPG
2304 usb_dev->authorized = 0;
2305 usb_set_configuration(usb_dev, -1);
da307123
AS
2306
2307 kfree(usb_dev->product);
93993a0a 2308 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
da307123 2309 kfree(usb_dev->manufacturer);
93993a0a 2310 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
da307123 2311 kfree(usb_dev->serial);
93993a0a 2312 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
da307123
AS
2313
2314 usb_destroy_configuration(usb_dev);
93993a0a 2315 usb_dev->descriptor.bNumConfigurations = 0;
da307123 2316
93993a0a
IPG
2317out_unauthorized:
2318 usb_unlock_device(usb_dev);
2319 return 0;
2320}
2321
2322
2323int usb_authorize_device(struct usb_device *usb_dev)
2324{
2325 int result = 0, c;
da307123 2326
93993a0a
IPG
2327 usb_lock_device(usb_dev);
2328 if (usb_dev->authorized == 1)
2329 goto out_authorized;
da307123 2330
93993a0a
IPG
2331 result = usb_autoresume_device(usb_dev);
2332 if (result < 0) {
2333 dev_err(&usb_dev->dev,
2334 "can't autoresume for authorization: %d\n", result);
2335 goto error_autoresume;
2336 }
2337 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2338 if (result < 0) {
2339 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2340 "authorization: %d\n", result);
2341 goto error_device_descriptor;
2342 }
da307123
AS
2343
2344 kfree(usb_dev->product);
2345 usb_dev->product = NULL;
2346 kfree(usb_dev->manufacturer);
2347 usb_dev->manufacturer = NULL;
2348 kfree(usb_dev->serial);
2349 usb_dev->serial = NULL;
2350
93993a0a 2351 usb_dev->authorized = 1;
8d8558d1 2352 result = usb_enumerate_device(usb_dev);
93993a0a 2353 if (result < 0)
8d8558d1 2354 goto error_enumerate;
93993a0a
IPG
2355 /* Choose and set the configuration. This registers the interfaces
2356 * with the driver core and lets interface drivers bind to them.
2357 */
b5ea060f 2358 c = usb_choose_configuration(usb_dev);
93993a0a
IPG
2359 if (c >= 0) {
2360 result = usb_set_configuration(usb_dev, c);
2361 if (result) {
2362 dev_err(&usb_dev->dev,
2363 "can't set config #%d, error %d\n", c, result);
2364 /* This need not be fatal. The user can try to
2365 * set other configurations. */
2366 }
2367 }
2368 dev_info(&usb_dev->dev, "authorized to connect\n");
da307123 2369
8d8558d1 2370error_enumerate:
93993a0a 2371error_device_descriptor:
da307123 2372 usb_autosuspend_device(usb_dev);
93993a0a
IPG
2373error_autoresume:
2374out_authorized:
2375 usb_unlock_device(usb_dev); // complements locktree
2376 return result;
2377}
2378
2379
0165de09
IPG
2380/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2381static unsigned hub_is_wusb(struct usb_hub *hub)
2382{
2383 struct usb_hcd *hcd;
2384 if (hub->hdev->parent != NULL) /* not a root hub? */
2385 return 0;
2386 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2387 return hcd->wireless;
2388}
2389
2390
1da177e4
LT
2391#define PORT_RESET_TRIES 5
2392#define SET_ADDRESS_TRIES 2
2393#define GET_DESCRIPTOR_TRIES 2
2394#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
90ab5ee9 2395#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
1da177e4
LT
2396
2397#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2398#define HUB_SHORT_RESET_TIME 10
75d7cf72 2399#define HUB_BH_RESET_TIME 50
1da177e4
LT
2400#define HUB_LONG_RESET_TIME 200
2401#define HUB_RESET_TIMEOUT 500
2402
10d674a8
SS
2403static int hub_port_reset(struct usb_hub *hub, int port1,
2404 struct usb_device *udev, unsigned int delay, bool warm);
2405
8bea2bd3
SL
2406/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2407 * Port worm reset is required to recover
2408 */
2409static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
10d674a8
SS
2410{
2411 return hub_is_superspeed(hub->hdev) &&
8bea2bd3
SL
2412 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2413 USB_SS_PORT_LS_SS_INACTIVE) ||
2414 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2415 USB_SS_PORT_LS_COMP_MOD)) ;
10d674a8
SS
2416}
2417
1da177e4 2418static int hub_port_wait_reset(struct usb_hub *hub, int port1,
75d7cf72 2419 struct usb_device *udev, unsigned int delay, bool warm)
1da177e4
LT
2420{
2421 int delay_time, ret;
2422 u16 portstatus;
2423 u16 portchange;
2424
2425 for (delay_time = 0;
2426 delay_time < HUB_RESET_TIMEOUT;
2427 delay_time += delay) {
2428 /* wait to give the device a chance to reset */
2429 msleep(delay);
2430
2431 /* read and decode port status */
2432 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2433 if (ret < 0)
2434 return ret;
2435
75d7cf72
AX
2436 /*
2437 * Some buggy devices require a warm reset to be issued even
2438 * when the port appears not to be connected.
2439 */
2440 if (!warm) {
10d674a8
SS
2441 /*
2442 * Some buggy devices can cause an NEC host controller
2443 * to transition to the "Error" state after a hot port
2444 * reset. This will show up as the port state in
2445 * "Inactive", and the port may also report a
2446 * disconnect. Forcing a warm port reset seems to make
2447 * the device work.
2448 *
2449 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2450 */
8bea2bd3 2451 if (hub_port_warm_reset_required(hub, portstatus)) {
10d674a8
SS
2452 int ret;
2453
2454 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2455 clear_port_feature(hub->hdev, port1,
2456 USB_PORT_FEAT_C_CONNECTION);
2457 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2458 clear_port_feature(hub->hdev, port1,
2459 USB_PORT_FEAT_C_PORT_LINK_STATE);
2460 if (portchange & USB_PORT_STAT_C_RESET)
2461 clear_port_feature(hub->hdev, port1,
2462 USB_PORT_FEAT_C_RESET);
2463 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2464 port1);
2465 ret = hub_port_reset(hub, port1,
2466 udev, HUB_BH_RESET_TIME,
2467 true);
2468 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2469 clear_port_feature(hub->hdev, port1,
2470 USB_PORT_FEAT_C_CONNECTION);
2471 return ret;
2472 }
75d7cf72
AX
2473 /* Device went away? */
2474 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2475 return -ENOTCONN;
2476
2477 /* bomb out completely if the connection bounced */
2478 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2479 return -ENOTCONN;
2480
2481 /* if we`ve finished resetting, then break out of
2482 * the loop
2483 */
2484 if (!(portstatus & USB_PORT_STAT_RESET) &&
2485 (portstatus & USB_PORT_STAT_ENABLE)) {
2486 if (hub_is_wusb(hub))
2487 udev->speed = USB_SPEED_WIRELESS;
2488 else if (hub_is_superspeed(hub->hdev))
2489 udev->speed = USB_SPEED_SUPER;
2490 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2491 udev->speed = USB_SPEED_HIGH;
2492 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2493 udev->speed = USB_SPEED_LOW;
2494 else
2495 udev->speed = USB_SPEED_FULL;
2496 return 0;
2497 }
2498 } else {
2499 if (portchange & USB_PORT_STAT_C_BH_RESET)
2500 return 0;
1da177e4
LT
2501 }
2502
2503 /* switch to the long delay after two short delay failures */
2504 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2505 delay = HUB_LONG_RESET_TIME;
2506
2507 dev_dbg (hub->intfdev,
75d7cf72
AX
2508 "port %d not %sreset yet, waiting %dms\n",
2509 port1, warm ? "warm " : "", delay);
1da177e4
LT
2510 }
2511
2512 return -EBUSY;
2513}
2514
75d7cf72
AX
2515static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2516 struct usb_device *udev, int *status, bool warm)
2517{
2518 switch (*status) {
2519 case 0:
2520 if (!warm) {
2521 struct usb_hcd *hcd;
2522 /* TRSTRCY = 10 ms; plus some extra */
2523 msleep(10 + 40);
2524 update_devnum(udev, 0);
2525 hcd = bus_to_hcd(udev->bus);
2526 if (hcd->driver->reset_device) {
2527 *status = hcd->driver->reset_device(hcd, udev);
2528 if (*status < 0) {
2529 dev_err(&udev->dev, "Cannot reset "
2530 "HCD device state\n");
2531 break;
2532 }
2533 }
2534 }
2535 /* FALL THROUGH */
2536 case -ENOTCONN:
2537 case -ENODEV:
2538 clear_port_feature(hub->hdev,
2539 port1, USB_PORT_FEAT_C_RESET);
2540 /* FIXME need disconnect() for NOTATTACHED device */
2541 if (warm) {
2542 clear_port_feature(hub->hdev, port1,
2543 USB_PORT_FEAT_C_BH_PORT_RESET);
2544 clear_port_feature(hub->hdev, port1,
2545 USB_PORT_FEAT_C_PORT_LINK_STATE);
2546 } else {
2547 usb_set_device_state(udev, *status
2548 ? USB_STATE_NOTATTACHED
2549 : USB_STATE_DEFAULT);
2550 }
2551 break;
2552 }
2553}
2554
2555/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
1da177e4 2556static int hub_port_reset(struct usb_hub *hub, int port1,
75d7cf72 2557 struct usb_device *udev, unsigned int delay, bool warm)
1da177e4
LT
2558{
2559 int i, status;
2560
75d7cf72
AX
2561 if (!warm) {
2562 /* Block EHCI CF initialization during the port reset.
2563 * Some companion controllers don't like it when they mix.
2564 */
2565 down_read(&ehci_cf_port_reset_rwsem);
2566 } else {
2567 if (!hub_is_superspeed(hub->hdev)) {
2568 dev_err(hub->intfdev, "only USB3 hub support "
2569 "warm reset\n");
2570 return -EINVAL;
2571 }
2572 }
32fe0198 2573
1da177e4
LT
2574 /* Reset the port */
2575 for (i = 0; i < PORT_RESET_TRIES; i++) {
75d7cf72
AX
2576 status = set_port_feature(hub->hdev, port1, (warm ?
2577 USB_PORT_FEAT_BH_PORT_RESET :
2578 USB_PORT_FEAT_RESET));
2579 if (status) {
1da177e4 2580 dev_err(hub->intfdev,
75d7cf72
AX
2581 "cannot %sreset port %d (err = %d)\n",
2582 warm ? "warm " : "", port1, status);
2583 } else {
2584 status = hub_port_wait_reset(hub, port1, udev, delay,
2585 warm);
dd16525b 2586 if (status && status != -ENOTCONN)
1da177e4
LT
2587 dev_dbg(hub->intfdev,
2588 "port_wait_reset: err = %d\n",
2589 status);
2590 }
2591
2592 /* return on disconnect or reset */
75d7cf72
AX
2593 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2594 hub_port_finish_reset(hub, port1, udev, &status, warm);
32fe0198 2595 goto done;
1da177e4
LT
2596 }
2597
2598 dev_dbg (hub->intfdev,
75d7cf72
AX
2599 "port %d not enabled, trying %sreset again...\n",
2600 port1, warm ? "warm " : "");
1da177e4
LT
2601 delay = HUB_LONG_RESET_TIME;
2602 }
2603
2604 dev_err (hub->intfdev,
2605 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2606 port1);
2607
75d7cf72
AX
2608done:
2609 if (!warm)
2610 up_read(&ehci_cf_port_reset_rwsem);
5e467f6e 2611
75d7cf72 2612 return status;
5e467f6e
AX
2613}
2614
0ed9a57e
AX
2615/* Check if a port is power on */
2616static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2617{
2618 int ret = 0;
2619
2620 if (hub_is_superspeed(hub->hdev)) {
2621 if (portstatus & USB_SS_PORT_STAT_POWER)
2622 ret = 1;
2623 } else {
2624 if (portstatus & USB_PORT_STAT_POWER)
2625 ret = 1;
2626 }
2627
2628 return ret;
2629}
2630
d388dab7 2631#ifdef CONFIG_PM
1da177e4 2632
0ed9a57e
AX
2633/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2634static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2635{
2636 int ret = 0;
2637
2638 if (hub_is_superspeed(hub->hdev)) {
2639 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2640 == USB_SS_PORT_LS_U3)
2641 ret = 1;
2642 } else {
2643 if (portstatus & USB_PORT_STAT_SUSPEND)
2644 ret = 1;
2645 }
2646
2647 return ret;
2648}
b01b03f3
AS
2649
2650/* Determine whether the device on a port is ready for a normal resume,
2651 * is ready for a reset-resume, or should be disconnected.
2652 */
2653static int check_port_resume_type(struct usb_device *udev,
2654 struct usb_hub *hub, int port1,
2655 int status, unsigned portchange, unsigned portstatus)
2656{
2657 /* Is the device still present? */
0ed9a57e
AX
2658 if (status || port_is_suspended(hub, portstatus) ||
2659 !port_is_power_on(hub, portstatus) ||
2660 !(portstatus & USB_PORT_STAT_CONNECTION)) {
b01b03f3
AS
2661 if (status >= 0)
2662 status = -ENODEV;
2663 }
2664
86c57edf
AS
2665 /* Can't do a normal resume if the port isn't enabled,
2666 * so try a reset-resume instead.
2667 */
2668 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2669 if (udev->persist_enabled)
2670 udev->reset_resume = 1;
2671 else
2672 status = -ENODEV;
2673 }
b01b03f3
AS
2674
2675 if (status) {
2676 dev_dbg(hub->intfdev,
2677 "port %d status %04x.%04x after resume, %d\n",
2678 port1, portchange, portstatus, status);
2679 } else if (udev->reset_resume) {
2680
2681 /* Late port handoff can set status-change bits */
2682 if (portchange & USB_PORT_STAT_C_CONNECTION)
2683 clear_port_feature(hub->hdev, port1,
2684 USB_PORT_FEAT_C_CONNECTION);
2685 if (portchange & USB_PORT_STAT_C_ENABLE)
2686 clear_port_feature(hub->hdev, port1,
2687 USB_PORT_FEAT_C_ENABLE);
2688 }
2689
2690 return status;
2691}
2692
f74631e3
SS
2693int usb_disable_ltm(struct usb_device *udev)
2694{
2695 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2696
2697 /* Check if the roothub and device supports LTM. */
2698 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2699 !usb_device_supports_ltm(udev))
2700 return 0;
2701
2702 /* Clear Feature LTM Enable can only be sent if the device is
2703 * configured.
2704 */
2705 if (!udev->actconfig)
2706 return 0;
2707
2708 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2709 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2710 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2711 USB_CTRL_SET_TIMEOUT);
2712}
2713EXPORT_SYMBOL_GPL(usb_disable_ltm);
2714
2715void usb_enable_ltm(struct usb_device *udev)
2716{
2717 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2718
2719 /* Check if the roothub and device supports LTM. */
2720 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2721 !usb_device_supports_ltm(udev))
2722 return;
2723
2724 /* Set Feature LTM Enable can only be sent if the device is
2725 * configured.
2726 */
2727 if (!udev->actconfig)
2728 return;
2729
2730 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2731 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2732 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2733 USB_CTRL_SET_TIMEOUT);
2734}
2735EXPORT_SYMBOL_GPL(usb_enable_ltm);
2736
1da177e4
LT
2737#ifdef CONFIG_USB_SUSPEND
2738
2739/*
624d6c07
AS
2740 * usb_port_suspend - suspend a usb device's upstream port
2741 * @udev: device that's no longer in active use, not a root hub
2742 * Context: must be able to sleep; device not locked; pm locks held
2743 *
2744 * Suspends a USB device that isn't in active use, conserving power.
2745 * Devices may wake out of a suspend, if anything important happens,
2746 * using the remote wakeup mechanism. They may also be taken out of
2747 * suspend by the host, using usb_port_resume(). It's also routine
2748 * to disconnect devices while they are suspended.
2749 *
2750 * This only affects the USB hardware for a device; its interfaces
2751 * (and, for hubs, child devices) must already have been suspended.
2752 *
1da177e4
LT
2753 * Selective port suspend reduces power; most suspended devices draw
2754 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2755 * All devices below the suspended port are also suspended.
2756 *
2757 * Devices leave suspend state when the host wakes them up. Some devices
2758 * also support "remote wakeup", where the device can activate the USB
2759 * tree above them to deliver data, such as a keypress or packet. In
2760 * some cases, this wakes the USB host.
624d6c07
AS
2761 *
2762 * Suspending OTG devices may trigger HNP, if that's been enabled
2763 * between a pair of dual-role devices. That will change roles, such
2764 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2765 *
2766 * Devices on USB hub ports have only one "suspend" state, corresponding
2767 * to ACPI D2, "may cause the device to lose some context".
2768 * State transitions include:
2769 *
2770 * - suspend, resume ... when the VBUS power link stays live
2771 * - suspend, disconnect ... VBUS lost
2772 *
2773 * Once VBUS drop breaks the circuit, the port it's using has to go through
2774 * normal re-enumeration procedures, starting with enabling VBUS power.
2775 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2776 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2777 * timer, no SRP, no requests through sysfs.
2778 *
2779 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2780 * the root hub for their bus goes into global suspend ... so we don't
2781 * (falsely) update the device power state to say it suspended.
2782 *
2783 * Returns 0 on success, else negative errno.
1da177e4 2784 */
65bfd296 2785int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
1da177e4 2786{
624d6c07
AS
2787 struct usb_hub *hub = hdev_to_hub(udev->parent);
2788 int port1 = udev->portnum;
2789 int status;
1da177e4 2790
1da177e4
LT
2791 /* enable remote wakeup when appropriate; this lets the device
2792 * wake up the upstream hub (including maybe the root hub).
2793 *
2794 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2795 * we don't explicitly enable it here.
2796 */
645daaab 2797 if (udev->do_remote_wakeup) {
623bef9e
SS
2798 if (!hub_is_superspeed(hub->hdev)) {
2799 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2800 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2801 USB_DEVICE_REMOTE_WAKEUP, 0,
2802 NULL, 0,
2803 USB_CTRL_SET_TIMEOUT);
2804 } else {
2805 /* Assume there's only one function on the USB 3.0
2806 * device and enable remote wake for the first
2807 * interface. FIXME if the interface association
2808 * descriptor shows there's more than one function.
2809 */
2810 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2811 USB_REQ_SET_FEATURE,
2812 USB_RECIP_INTERFACE,
2813 USB_INTRF_FUNC_SUSPEND,
3b9b6acd
SS
2814 USB_INTRF_FUNC_SUSPEND_RW |
2815 USB_INTRF_FUNC_SUSPEND_LP,
623bef9e
SS
2816 NULL, 0,
2817 USB_CTRL_SET_TIMEOUT);
2818 }
0c487206 2819 if (status) {
624d6c07
AS
2820 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2821 status);
0c487206 2822 /* bail if autosuspend is requested */
5b1b0b81 2823 if (PMSG_IS_AUTO(msg))
0c487206
ON
2824 return status;
2825 }
1da177e4
LT
2826 }
2827
65580b43
AX
2828 /* disable USB2 hardware LPM */
2829 if (udev->usb2_hw_lpm_enabled == 1)
2830 usb_set_usb2_hardware_lpm(udev, 0);
2831
f74631e3
SS
2832 if (usb_disable_ltm(udev)) {
2833 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2834 __func__);
2835 return -ENOMEM;
2836 }
8306095f
SS
2837 if (usb_unlocked_disable_lpm(udev)) {
2838 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2839 __func__);
2840 return -ENOMEM;
2841 }
2842
1da177e4 2843 /* see 7.1.7.6 */
a7114230
AX
2844 if (hub_is_superspeed(hub->hdev))
2845 status = set_port_feature(hub->hdev,
2846 port1 | (USB_SS_PORT_LS_U3 << 3),
2847 USB_PORT_FEAT_LINK_STATE);
a8f08d86
AX
2848 else
2849 status = set_port_feature(hub->hdev, port1,
2850 USB_PORT_FEAT_SUSPEND);
1da177e4 2851 if (status) {
624d6c07
AS
2852 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2853 port1, status);
1da177e4 2854 /* paranoia: "should not happen" */
0c487206
ON
2855 if (udev->do_remote_wakeup)
2856 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1da177e4
LT
2857 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2858 USB_DEVICE_REMOTE_WAKEUP, 0,
2859 NULL, 0,
2860 USB_CTRL_SET_TIMEOUT);
cbb33004 2861
c3e751e4
AX
2862 /* Try to enable USB2 hardware LPM again */
2863 if (udev->usb2_hw_lpm_capable == 1)
2864 usb_set_usb2_hardware_lpm(udev, 1);
2865
f74631e3
SS
2866 /* Try to enable USB3 LTM and LPM again */
2867 usb_enable_ltm(udev);
8306095f
SS
2868 usb_unlocked_enable_lpm(udev);
2869
cbb33004 2870 /* System sleep transitions should never fail */
5b1b0b81 2871 if (!PMSG_IS_AUTO(msg))
cbb33004 2872 status = 0;
1da177e4
LT
2873 } else {
2874 /* device has up to 10 msec to fully suspend */
30b1a7a3
AS
2875 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2876 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2877 udev->do_remote_wakeup);
1da177e4
LT
2878 usb_set_device_state(udev, USB_STATE_SUSPENDED);
2879 msleep(10);
2880 }
c08512c7 2881 usb_mark_last_busy(hub->hdev);
1da177e4
LT
2882 return status;
2883}
2884
1da177e4 2885/*
390a8c34
DB
2886 * If the USB "suspend" state is in use (rather than "global suspend"),
2887 * many devices will be individually taken out of suspend state using
54515fe5 2888 * special "resume" signaling. This routine kicks in shortly after
1da177e4
LT
2889 * hardware resume signaling is finished, either because of selective
2890 * resume (by host) or remote wakeup (by device) ... now see what changed
2891 * in the tree that's rooted at this device.
54515fe5
AS
2892 *
2893 * If @udev->reset_resume is set then the device is reset before the
2894 * status check is done.
1da177e4 2895 */
140d8f68 2896static int finish_port_resume(struct usb_device *udev)
1da177e4 2897{
54515fe5 2898 int status = 0;
1da177e4
LT
2899 u16 devstatus;
2900
2901 /* caller owns the udev device lock */
b9cef6c3
WF
2902 dev_dbg(&udev->dev, "%s\n",
2903 udev->reset_resume ? "finish reset-resume" : "finish resume");
1da177e4
LT
2904
2905 /* usb ch9 identifies four variants of SUSPENDED, based on what
2906 * state the device resumes to. Linux currently won't see the
2907 * first two on the host side; they'd be inside hub_port_init()
2908 * during many timeouts, but khubd can't suspend until later.
2909 */
2910 usb_set_device_state(udev, udev->actconfig
2911 ? USB_STATE_CONFIGURED
2912 : USB_STATE_ADDRESS);
1da177e4 2913
54515fe5
AS
2914 /* 10.5.4.5 says not to reset a suspended port if the attached
2915 * device is enabled for remote wakeup. Hence the reset
2916 * operation is carried out here, after the port has been
2917 * resumed.
2918 */
2919 if (udev->reset_resume)
86c57edf 2920 retry_reset_resume:
742120c6 2921 status = usb_reset_and_verify_device(udev);
54515fe5 2922
1da177e4
LT
2923 /* 10.5.4.5 says be sure devices in the tree are still there.
2924 * For now let's assume the device didn't go crazy on resume,
2925 * and device drivers will know about any resume quirks.
2926 */
54515fe5 2927 if (status == 0) {
46dede46 2928 devstatus = 0;
54515fe5
AS
2929 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2930 if (status >= 0)
46dede46 2931 status = (status > 0 ? 0 : -ENODEV);
86c57edf
AS
2932
2933 /* If a normal resume failed, try doing a reset-resume */
2934 if (status && !udev->reset_resume && udev->persist_enabled) {
2935 dev_dbg(&udev->dev, "retry with reset-resume\n");
2936 udev->reset_resume = 1;
2937 goto retry_reset_resume;
2938 }
54515fe5 2939 }
b40b7a90 2940
624d6c07
AS
2941 if (status) {
2942 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2943 status);
2944 } else if (udev->actconfig) {
1da177e4 2945 le16_to_cpus(&devstatus);
686314cf 2946 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
1da177e4
LT
2947 status = usb_control_msg(udev,
2948 usb_sndctrlpipe(udev, 0),
2949 USB_REQ_CLEAR_FEATURE,
2950 USB_RECIP_DEVICE,
2951 USB_DEVICE_REMOTE_WAKEUP, 0,
2952 NULL, 0,
2953 USB_CTRL_SET_TIMEOUT);
a8e7c565 2954 if (status)
b9cef6c3
WF
2955 dev_dbg(&udev->dev,
2956 "disable remote wakeup, status %d\n",
2957 status);
4bf0ba86 2958 }
1da177e4 2959 status = 0;
1da177e4
LT
2960 }
2961 return status;
2962}
2963
624d6c07
AS
2964/*
2965 * usb_port_resume - re-activate a suspended usb device's upstream port
2966 * @udev: device to re-activate, not a root hub
2967 * Context: must be able to sleep; device not locked; pm locks held
2968 *
2969 * This will re-activate the suspended device, increasing power usage
2970 * while letting drivers communicate again with its endpoints.
2971 * USB resume explicitly guarantees that the power session between
2972 * the host and the device is the same as it was when the device
2973 * suspended.
2974 *
feccc30d
AS
2975 * If @udev->reset_resume is set then this routine won't check that the
2976 * port is still enabled. Furthermore, finish_port_resume() above will
54515fe5
AS
2977 * reset @udev. The end result is that a broken power session can be
2978 * recovered and @udev will appear to persist across a loss of VBUS power.
2979 *
2980 * For example, if a host controller doesn't maintain VBUS suspend current
2981 * during a system sleep or is reset when the system wakes up, all the USB
2982 * power sessions below it will be broken. This is especially troublesome
2983 * for mass-storage devices containing mounted filesystems, since the
2984 * device will appear to have disconnected and all the memory mappings
2985 * to it will be lost. Using the USB_PERSIST facility, the device can be
2986 * made to appear as if it had not disconnected.
2987 *
742120c6 2988 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
feccc30d 2989 * every effort to insure that the same device is present after the
54515fe5
AS
2990 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2991 * quite possible for a device to remain unaltered but its media to be
2992 * changed. If the user replaces a flash memory card while the system is
2993 * asleep, he will have only himself to blame when the filesystem on the
2994 * new card is corrupted and the system crashes.
2995 *
624d6c07
AS
2996 * Returns 0 on success, else negative errno.
2997 */
65bfd296 2998int usb_port_resume(struct usb_device *udev, pm_message_t msg)
1da177e4 2999{
624d6c07
AS
3000 struct usb_hub *hub = hdev_to_hub(udev->parent);
3001 int port1 = udev->portnum;
3002 int status;
3003 u16 portchange, portstatus;
d25450c6
AS
3004
3005 /* Skip the initial Clear-Suspend step for a remote wakeup */
3006 status = hub_port_status(hub, port1, &portstatus, &portchange);
0ed9a57e 3007 if (status == 0 && !port_is_suspended(hub, portstatus))
d25450c6 3008 goto SuspendCleared;
1da177e4
LT
3009
3010 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3011
d5cbad4b
AS
3012 set_bit(port1, hub->busy_bits);
3013
1da177e4 3014 /* see 7.1.7.7; affects power usage, but not budgeting */
a7114230
AX
3015 if (hub_is_superspeed(hub->hdev))
3016 status = set_port_feature(hub->hdev,
3017 port1 | (USB_SS_PORT_LS_U0 << 3),
3018 USB_PORT_FEAT_LINK_STATE);
3019 else
3020 status = clear_port_feature(hub->hdev,
3021 port1, USB_PORT_FEAT_SUSPEND);
1da177e4 3022 if (status) {
624d6c07
AS
3023 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
3024 port1, status);
1da177e4 3025 } else {
1da177e4 3026 /* drive resume for at least 20 msec */
686314cf 3027 dev_dbg(&udev->dev, "usb %sresume\n",
5b1b0b81 3028 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
1da177e4
LT
3029 msleep(25);
3030
1da177e4
LT
3031 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3032 * stop resume signaling. Then finish the resume
3033 * sequence.
3034 */
d25450c6 3035 status = hub_port_status(hub, port1, &portstatus, &portchange);
54515fe5 3036
b01b03f3
AS
3037 /* TRSMRCY = 10 msec */
3038 msleep(10);
3039 }
3040
54515fe5 3041 SuspendCleared:
b01b03f3 3042 if (status == 0) {
a7114230
AX
3043 if (hub_is_superspeed(hub->hdev)) {
3044 if (portchange & USB_PORT_STAT_C_LINK_STATE)
3045 clear_port_feature(hub->hdev, port1,
3046 USB_PORT_FEAT_C_PORT_LINK_STATE);
3047 } else {
3048 if (portchange & USB_PORT_STAT_C_SUSPEND)
3049 clear_port_feature(hub->hdev, port1,
3050 USB_PORT_FEAT_C_SUSPEND);
3051 }
1da177e4 3052 }
1da177e4 3053
d5cbad4b 3054 clear_bit(port1, hub->busy_bits);
d5cbad4b 3055
b01b03f3
AS
3056 status = check_port_resume_type(udev,
3057 hub, port1, status, portchange, portstatus);
54515fe5
AS
3058 if (status == 0)
3059 status = finish_port_resume(udev);
3060 if (status < 0) {
3061 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3062 hub_port_logical_disconnect(hub, port1);
65580b43
AX
3063 } else {
3064 /* Try to enable USB2 hardware LPM */
3065 if (udev->usb2_hw_lpm_capable == 1)
3066 usb_set_usb2_hardware_lpm(udev, 1);
8306095f 3067
f74631e3
SS
3068 /* Try to enable USB3 LTM and LPM */
3069 usb_enable_ltm(udev);
8306095f 3070 usb_unlocked_enable_lpm(udev);
54515fe5 3071 }
65580b43 3072
1da177e4
LT
3073 return status;
3074}
3075
8808f00c 3076/* caller has locked udev */
0534d468 3077int usb_remote_wakeup(struct usb_device *udev)
1da177e4
LT
3078{
3079 int status = 0;
3080
1da177e4 3081 if (udev->state == USB_STATE_SUSPENDED) {
645daaab 3082 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
9bbdf1e0
AS
3083 status = usb_autoresume_device(udev);
3084 if (status == 0) {
3085 /* Let the drivers do their thing, then... */
3086 usb_autosuspend_device(udev);
3087 }
1da177e4 3088 }
1da177e4
LT
3089 return status;
3090}
3091
d388dab7
AS
3092#else /* CONFIG_USB_SUSPEND */
3093
3094/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3095
65bfd296 3096int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
d388dab7
AS
3097{
3098 return 0;
3099}
3100
b01b03f3
AS
3101/* However we may need to do a reset-resume */
3102
65bfd296 3103int usb_port_resume(struct usb_device *udev, pm_message_t msg)
d388dab7 3104{
b01b03f3
AS
3105 struct usb_hub *hub = hdev_to_hub(udev->parent);
3106 int port1 = udev->portnum;
3107 int status;
3108 u16 portchange, portstatus;
3109
3110 status = hub_port_status(hub, port1, &portstatus, &portchange);
3111 status = check_port_resume_type(udev,
3112 hub, port1, status, portchange, portstatus);
54515fe5 3113
b01b03f3
AS
3114 if (status) {
3115 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3116 hub_port_logical_disconnect(hub, port1);
3117 } else if (udev->reset_resume) {
54515fe5 3118 dev_dbg(&udev->dev, "reset-resume\n");
742120c6 3119 status = usb_reset_and_verify_device(udev);
54515fe5
AS
3120 }
3121 return status;
d388dab7
AS
3122}
3123
d388dab7
AS
3124#endif
3125
db690874 3126static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
1da177e4
LT
3127{
3128 struct usb_hub *hub = usb_get_intfdata (intf);
3129 struct usb_device *hdev = hub->hdev;
3130 unsigned port1;
4296c70a 3131 int status;
1da177e4 3132
cbb33004 3133 /* Warn if children aren't already suspended */
1da177e4
LT
3134 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3135 struct usb_device *udev;
3136
ff823c79 3137 udev = hub->ports[port1 - 1]->child;
6840d255 3138 if (udev && udev->can_submit) {
cbb33004 3139 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
5b1b0b81 3140 if (PMSG_IS_AUTO(msg))
cbb33004 3141 return -EBUSY;
c9f89fa4 3142 }
1da177e4 3143 }
4296c70a
SS
3144 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3145 /* Enable hub to send remote wakeup for all ports. */
3146 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3147 status = set_port_feature(hdev,
3148 port1 |
3149 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3150 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3151 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3152 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3153 }
3154 }
1da177e4 3155
441b62c1 3156 dev_dbg(&intf->dev, "%s\n", __func__);
40f122f3 3157
12f1ff83 3158 /* stop khubd and related activity */
4330354f 3159 hub_quiesce(hub, HUB_SUSPEND);
b6f6436d 3160 return 0;
1da177e4
LT
3161}
3162
3163static int hub_resume(struct usb_interface *intf)
3164{
5e6effae 3165 struct usb_hub *hub = usb_get_intfdata(intf);
40f122f3 3166
5e6effae 3167 dev_dbg(&intf->dev, "%s\n", __func__);
f2835219 3168 hub_activate(hub, HUB_RESUME);
1da177e4
LT
3169 return 0;
3170}
3171
b41a60ec 3172static int hub_reset_resume(struct usb_interface *intf)
f07600cf 3173{
b41a60ec 3174 struct usb_hub *hub = usb_get_intfdata(intf);
f07600cf 3175
5e6effae 3176 dev_dbg(&intf->dev, "%s\n", __func__);
f2835219 3177 hub_activate(hub, HUB_RESET_RESUME);
f07600cf
AS
3178 return 0;
3179}
3180
54515fe5
AS
3181/**
3182 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3183 * @rhdev: struct usb_device for the root hub
3184 *
3185 * The USB host controller driver calls this function when its root hub
3186 * is resumed and Vbus power has been interrupted or the controller
feccc30d
AS
3187 * has been reset. The routine marks @rhdev as having lost power.
3188 * When the hub driver is resumed it will take notice and carry out
3189 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3190 * the others will be disconnected.
54515fe5
AS
3191 */
3192void usb_root_hub_lost_power(struct usb_device *rhdev)
3193{
3194 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3195 rhdev->reset_resume = 1;
3196}
3197EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3198
1ea7e0e8
SS
3199static const char * const usb3_lpm_names[] = {
3200 "U0",
3201 "U1",
3202 "U2",
3203 "U3",
3204};
3205
3206/*
3207 * Send a Set SEL control transfer to the device, prior to enabling
3208 * device-initiated U1 or U2. This lets the device know the exit latencies from
3209 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3210 * packet from the host.
3211 *
3212 * This function will fail if the SEL or PEL values for udev are greater than
3213 * the maximum allowed values for the link state to be enabled.
3214 */
3215static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3216{
3217 struct usb_set_sel_req *sel_values;
3218 unsigned long long u1_sel;
3219 unsigned long long u1_pel;
3220 unsigned long long u2_sel;
3221 unsigned long long u2_pel;
3222 int ret;
3223
3224 /* Convert SEL and PEL stored in ns to us */
3225 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3226 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3227 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3228 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3229
3230 /*
3231 * Make sure that the calculated SEL and PEL values for the link
3232 * state we're enabling aren't bigger than the max SEL/PEL
3233 * value that will fit in the SET SEL control transfer.
3234 * Otherwise the device would get an incorrect idea of the exit
3235 * latency for the link state, and could start a device-initiated
3236 * U1/U2 when the exit latencies are too high.
3237 */
3238 if ((state == USB3_LPM_U1 &&
3239 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3240 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3241 (state == USB3_LPM_U2 &&
3242 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3243 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
1510a1a2 3244 dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n",
1ea7e0e8
SS
3245 usb3_lpm_names[state], u1_sel, u1_pel);
3246 return -EINVAL;
3247 }
3248
3249 /*
3250 * If we're enabling device-initiated LPM for one link state,
3251 * but the other link state has a too high SEL or PEL value,
3252 * just set those values to the max in the Set SEL request.
3253 */
3254 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3255 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3256
3257 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3258 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3259
3260 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3261 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3262
3263 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3264 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3265
3266 /*
3267 * usb_enable_lpm() can be called as part of a failed device reset,
3268 * which may be initiated by an error path of a mass storage driver.
3269 * Therefore, use GFP_NOIO.
3270 */
3271 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3272 if (!sel_values)
3273 return -ENOMEM;
3274
3275 sel_values->u1_sel = u1_sel;
3276 sel_values->u1_pel = u1_pel;
3277 sel_values->u2_sel = cpu_to_le16(u2_sel);
3278 sel_values->u2_pel = cpu_to_le16(u2_pel);
3279
3280 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3281 USB_REQ_SET_SEL,
3282 USB_RECIP_DEVICE,
3283 0, 0,
3284 sel_values, sizeof *(sel_values),
3285 USB_CTRL_SET_TIMEOUT);
3286 kfree(sel_values);
3287 return ret;
3288}
3289
3290/*
3291 * Enable or disable device-initiated U1 or U2 transitions.
3292 */
3293static int usb_set_device_initiated_lpm(struct usb_device *udev,
3294 enum usb3_link_state state, bool enable)
3295{
3296 int ret;
3297 int feature;
3298
3299 switch (state) {
3300 case USB3_LPM_U1:
3301 feature = USB_DEVICE_U1_ENABLE;
3302 break;
3303 case USB3_LPM_U2:
3304 feature = USB_DEVICE_U2_ENABLE;
3305 break;
3306 default:
3307 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3308 __func__, enable ? "enable" : "disable");
3309 return -EINVAL;
3310 }
3311
3312 if (udev->state != USB_STATE_CONFIGURED) {
3313 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3314 "for unconfigured device.\n",
3315 __func__, enable ? "enable" : "disable",
3316 usb3_lpm_names[state]);
3317 return 0;
3318 }
3319
3320 if (enable) {
1ea7e0e8
SS
3321 /*
3322 * Now send the control transfer to enable device-initiated LPM
3323 * for either U1 or U2.
3324 */
3325 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3326 USB_REQ_SET_FEATURE,
3327 USB_RECIP_DEVICE,
3328 feature,
3329 0, NULL, 0,
3330 USB_CTRL_SET_TIMEOUT);
3331 } else {
3332 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3333 USB_REQ_CLEAR_FEATURE,
3334 USB_RECIP_DEVICE,
3335 feature,
3336 0, NULL, 0,
3337 USB_CTRL_SET_TIMEOUT);
3338 }
3339 if (ret < 0) {
3340 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3341 enable ? "Enable" : "Disable",
3342 usb3_lpm_names[state]);
3343 return -EBUSY;
3344 }
3345 return 0;
3346}
3347
3348static int usb_set_lpm_timeout(struct usb_device *udev,
3349 enum usb3_link_state state, int timeout)
3350{
3351 int ret;
3352 int feature;
3353
3354 switch (state) {
3355 case USB3_LPM_U1:
3356 feature = USB_PORT_FEAT_U1_TIMEOUT;
3357 break;
3358 case USB3_LPM_U2:
3359 feature = USB_PORT_FEAT_U2_TIMEOUT;
3360 break;
3361 default:
3362 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3363 __func__);
3364 return -EINVAL;
3365 }
3366
3367 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3368 timeout != USB3_LPM_DEVICE_INITIATED) {
3369 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3370 "which is a reserved value.\n",
3371 usb3_lpm_names[state], timeout);
3372 return -EINVAL;
3373 }
3374
3375 ret = set_port_feature(udev->parent,
3376 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3377 feature);
3378 if (ret < 0) {
3379 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3380 "error code %i\n", usb3_lpm_names[state],
3381 timeout, ret);
3382 return -EBUSY;
3383 }
3384 if (state == USB3_LPM_U1)
3385 udev->u1_params.timeout = timeout;
3386 else
3387 udev->u2_params.timeout = timeout;
3388 return 0;
3389}
3390
3391/*
3392 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3393 * U1/U2 entry.
3394 *
3395 * We will attempt to enable U1 or U2, but there are no guarantees that the
3396 * control transfers to set the hub timeout or enable device-initiated U1/U2
3397 * will be successful.
3398 *
3399 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3400 * driver know about it. If that call fails, it should be harmless, and just
3401 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3402 */
3403static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3404 enum usb3_link_state state)
3405{
65a95b75 3406 int timeout, ret;
ae8963ad
SS
3407 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3408 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3409
3410 /* If the device says it doesn't have *any* exit latency to come out of
3411 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3412 * state.
3413 */
3414 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3415 (state == USB3_LPM_U2 && u2_mel == 0))
3416 return;
1ea7e0e8 3417
65a95b75
SS
3418 /*
3419 * First, let the device know about the exit latencies
3420 * associated with the link state we're about to enable.
3421 */
3422 ret = usb_req_set_sel(udev, state);
3423 if (ret < 0) {
3424 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3425 usb3_lpm_names[state]);
3426 return;
3427 }
3428
1ea7e0e8
SS
3429 /* We allow the host controller to set the U1/U2 timeout internally
3430 * first, so that it can change its schedule to account for the
3431 * additional latency to send data to a device in a lower power
3432 * link state.
3433 */
3434 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3435
3436 /* xHCI host controller doesn't want to enable this LPM state. */
3437 if (timeout == 0)
3438 return;
3439
3440 if (timeout < 0) {
3441 dev_warn(&udev->dev, "Could not enable %s link state, "
3442 "xHCI error %i.\n", usb3_lpm_names[state],
3443 timeout);
3444 return;
3445 }
3446
3447 if (usb_set_lpm_timeout(udev, state, timeout))
3448 /* If we can't set the parent hub U1/U2 timeout,
3449 * device-initiated LPM won't be allowed either, so let the xHCI
3450 * host know that this link state won't be enabled.
3451 */
3452 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3453
3454 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3455 else if (udev->actconfig)
3456 usb_set_device_initiated_lpm(udev, state, true);
3457
3458}
3459
3460/*
3461 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3462 * U1/U2 entry.
3463 *
3464 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3465 * If zero is returned, the parent will not allow the link to go into U1/U2.
3466 *
3467 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3468 * it won't have an effect on the bus link state because the parent hub will
3469 * still disallow device-initiated U1/U2 entry.
3470 *
3471 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3472 * possible. The result will be slightly more bus bandwidth will be taken up
3473 * (to account for U1/U2 exit latency), but it should be harmless.
3474 */
3475static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3476 enum usb3_link_state state)
3477{
3478 int feature;
3479
3480 switch (state) {
3481 case USB3_LPM_U1:
3482 feature = USB_PORT_FEAT_U1_TIMEOUT;
3483 break;
3484 case USB3_LPM_U2:
3485 feature = USB_PORT_FEAT_U2_TIMEOUT;
3486 break;
3487 default:
3488 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3489 __func__);
3490 return -EINVAL;
3491 }
3492
3493 if (usb_set_lpm_timeout(udev, state, 0))
3494 return -EBUSY;
3495
3496 usb_set_device_initiated_lpm(udev, state, false);
3497
3498 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3499 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3500 "bus schedule bandwidth may be impacted.\n",
3501 usb3_lpm_names[state]);
3502 return 0;
3503}
3504
3505/*
3506 * Disable hub-initiated and device-initiated U1 and U2 entry.
3507 * Caller must own the bandwidth_mutex.
3508 *
3509 * This will call usb_enable_lpm() on failure, which will decrement
3510 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3511 */
3512int usb_disable_lpm(struct usb_device *udev)
3513{
3514 struct usb_hcd *hcd;
3515
3516 if (!udev || !udev->parent ||
3517 udev->speed != USB_SPEED_SUPER ||
3518 !udev->lpm_capable)
3519 return 0;
3520
3521 hcd = bus_to_hcd(udev->bus);
3522 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3523 return 0;
3524
3525 udev->lpm_disable_count++;
55558c33 3526 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
1ea7e0e8
SS
3527 return 0;
3528
3529 /* If LPM is enabled, attempt to disable it. */
3530 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3531 goto enable_lpm;
3532 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3533 goto enable_lpm;
3534
3535 return 0;
3536
3537enable_lpm:
3538 usb_enable_lpm(udev);
3539 return -EBUSY;
3540}
3541EXPORT_SYMBOL_GPL(usb_disable_lpm);
3542
3543/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3544int usb_unlocked_disable_lpm(struct usb_device *udev)
3545{
3546 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3547 int ret;
3548
3549 if (!hcd)
3550 return -EINVAL;
3551
3552 mutex_lock(hcd->bandwidth_mutex);
3553 ret = usb_disable_lpm(udev);
3554 mutex_unlock(hcd->bandwidth_mutex);
3555
3556 return ret;
3557}
3558EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3559
3560/*
3561 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3562 * xHCI host policy may prevent U1 or U2 from being enabled.
3563 *
3564 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3565 * until the lpm_disable_count drops to zero. Caller must own the
3566 * bandwidth_mutex.
3567 */
3568void usb_enable_lpm(struct usb_device *udev)
3569{
3570 struct usb_hcd *hcd;
3571
3572 if (!udev || !udev->parent ||
3573 udev->speed != USB_SPEED_SUPER ||
3574 !udev->lpm_capable)
3575 return;
3576
3577 udev->lpm_disable_count--;
3578 hcd = bus_to_hcd(udev->bus);
3579 /* Double check that we can both enable and disable LPM.
3580 * Device must be configured to accept set feature U1/U2 timeout.
3581 */
3582 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3583 !hcd->driver->disable_usb3_lpm_timeout)
3584 return;
3585
3586 if (udev->lpm_disable_count > 0)
3587 return;
3588
3589 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3590 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3591}
3592EXPORT_SYMBOL_GPL(usb_enable_lpm);
3593
3594/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3595void usb_unlocked_enable_lpm(struct usb_device *udev)
3596{
3597 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3598
3599 if (!hcd)
3600 return;
3601
3602 mutex_lock(hcd->bandwidth_mutex);
3603 usb_enable_lpm(udev);
3604 mutex_unlock(hcd->bandwidth_mutex);
3605}
3606EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3607
3608
d388dab7
AS
3609#else /* CONFIG_PM */
3610
f07600cf
AS
3611#define hub_suspend NULL
3612#define hub_resume NULL
3613#define hub_reset_resume NULL
1ea7e0e8
SS
3614
3615int usb_disable_lpm(struct usb_device *udev)
3616{
3617 return 0;
3618}
e9261fb6 3619EXPORT_SYMBOL_GPL(usb_disable_lpm);
1ea7e0e8
SS
3620
3621void usb_enable_lpm(struct usb_device *udev) { }
e9261fb6 3622EXPORT_SYMBOL_GPL(usb_enable_lpm);
1ea7e0e8
SS
3623
3624int usb_unlocked_disable_lpm(struct usb_device *udev)
3625{
3626 return 0;
3627}
e9261fb6 3628EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
1ea7e0e8
SS
3629
3630void usb_unlocked_enable_lpm(struct usb_device *udev) { }
e9261fb6 3631EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
f74631e3
SS
3632
3633int usb_disable_ltm(struct usb_device *udev)
3634{
3635 return 0;
3636}
3637EXPORT_SYMBOL_GPL(usb_disable_ltm);
3638
3639void usb_enable_ltm(struct usb_device *udev) { }
3640EXPORT_SYMBOL_GPL(usb_enable_ltm);
d388dab7
AS
3641#endif
3642
1da177e4
LT
3643
3644/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3645 *
3646 * Between connect detection and reset signaling there must be a delay
3647 * of 100ms at least for debounce and power-settling. The corresponding
3648 * timer shall restart whenever the downstream port detects a disconnect.
3649 *
3650 * Apparently there are some bluetooth and irda-dongles and a number of
3651 * low-speed devices for which this debounce period may last over a second.
3652 * Not covered by the spec - but easy to deal with.
3653 *
3654 * This implementation uses a 1500ms total debounce timeout; if the
3655 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3656 * every 25ms for transient disconnects. When the port status has been
3657 * unchanged for 100ms it returns the port status.
3658 */
1da177e4
LT
3659static int hub_port_debounce(struct usb_hub *hub, int port1)
3660{
3661 int ret;
3662 int total_time, stable_time = 0;
3663 u16 portchange, portstatus;
3664 unsigned connection = 0xffff;
3665
3666 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3667 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3668 if (ret < 0)
3669 return ret;
3670
3671 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3672 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3673 stable_time += HUB_DEBOUNCE_STEP;
3674 if (stable_time >= HUB_DEBOUNCE_STABLE)
3675 break;
3676 } else {
3677 stable_time = 0;
3678 connection = portstatus & USB_PORT_STAT_CONNECTION;
3679 }
3680
3681 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3682 clear_port_feature(hub->hdev, port1,
3683 USB_PORT_FEAT_C_CONNECTION);
3684 }
3685
3686 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3687 break;
3688 msleep(HUB_DEBOUNCE_STEP);
3689 }
3690
3691 dev_dbg (hub->intfdev,
3692 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3693 port1, total_time, stable_time, portstatus);
3694
3695 if (stable_time < HUB_DEBOUNCE_STABLE)
3696 return -ETIMEDOUT;
3697 return portstatus;
3698}
3699
fc721f51 3700void usb_ep0_reinit(struct usb_device *udev)
1da177e4 3701{
ddeac4e7
AS
3702 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3703 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
2caf7fcd 3704 usb_enable_endpoint(udev, &udev->ep0, true);
1da177e4 3705}
fc721f51 3706EXPORT_SYMBOL_GPL(usb_ep0_reinit);
1da177e4
LT
3707
3708#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3709#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3710
4326ed0b 3711static int hub_set_address(struct usb_device *udev, int devnum)
1da177e4
LT
3712{
3713 int retval;
c6515272 3714 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1da177e4 3715
c6515272
SS
3716 /*
3717 * The host controller will choose the device address,
3718 * instead of the core having chosen it earlier
3719 */
3720 if (!hcd->driver->address_device && devnum <= 1)
1da177e4
LT
3721 return -EINVAL;
3722 if (udev->state == USB_STATE_ADDRESS)
3723 return 0;
3724 if (udev->state != USB_STATE_DEFAULT)
3725 return -EINVAL;
c8d4af8e 3726 if (hcd->driver->address_device)
c6515272 3727 retval = hcd->driver->address_device(hcd, udev);
c8d4af8e 3728 else
c6515272
SS
3729 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3730 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3731 NULL, 0, USB_CTRL_SET_TIMEOUT);
1da177e4 3732 if (retval == 0) {
3b29b68b 3733 update_devnum(udev, devnum);
4953d141 3734 /* Device now using proper address. */
1da177e4 3735 usb_set_device_state(udev, USB_STATE_ADDRESS);
fc721f51 3736 usb_ep0_reinit(udev);
1da177e4
LT
3737 }
3738 return retval;
3739}
3740
3741/* Reset device, (re)assign address, get device descriptor.
3742 * Device connection must be stable, no more debouncing needed.
3743 * Returns device in USB_STATE_ADDRESS, except on error.
3744 *
3745 * If this is called for an already-existing device (as part of
742120c6 3746 * usb_reset_and_verify_device), the caller must own the device lock. For a
1da177e4
LT
3747 * newly detected device that is not accessible through any global
3748 * pointers, it's not necessary to lock the device.
3749 */
3750static int
3751hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3752 int retry_counter)
3753{
4186ecf8 3754 static DEFINE_MUTEX(usb_address0_mutex);
1da177e4
LT
3755
3756 struct usb_device *hdev = hub->hdev;
e7b77172 3757 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
1da177e4
LT
3758 int i, j, retval;
3759 unsigned delay = HUB_SHORT_RESET_TIME;
3760 enum usb_device_speed oldspeed = udev->speed;
e538dfda 3761 const char *speed;
4326ed0b 3762 int devnum = udev->devnum;
1da177e4
LT
3763
3764 /* root hub ports have a slightly longer reset period
3765 * (from USB 2.0 spec, section 7.1.7.5)
3766 */
3767 if (!hdev->parent) {
3768 delay = HUB_ROOT_RESET_TIME;
3769 if (port1 == hdev->bus->otg_port)
3770 hdev->bus->b_hnp_enable = 0;
3771 }
3772
3773 /* Some low speed devices have problems with the quick delay, so */
3774 /* be a bit pessimistic with those devices. RHbug #23670 */
3775 if (oldspeed == USB_SPEED_LOW)
3776 delay = HUB_LONG_RESET_TIME;
3777
4186ecf8 3778 mutex_lock(&usb_address0_mutex);
1da177e4 3779
07194ab7
LT
3780 /* Reset the device; full speed may morph to high speed */
3781 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
75d7cf72 3782 retval = hub_port_reset(hub, port1, udev, delay, false);
07194ab7
LT
3783 if (retval < 0) /* error or disconnect */
3784 goto fail;
3785 /* success, speed is known */
3786
1da177e4
LT
3787 retval = -ENODEV;
3788
3789 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3790 dev_dbg(&udev->dev, "device reset changed speed!\n");
3791 goto fail;
3792 }
3793 oldspeed = udev->speed;
4326ed0b 3794
1da177e4
LT
3795 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3796 * it's fixed size except for full speed devices.
5bb6e0ae
IPG
3797 * For Wireless USB devices, ep0 max packet is always 512 (tho
3798 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
1da177e4
LT
3799 */
3800 switch (udev->speed) {
6b403b02 3801 case USB_SPEED_SUPER:
551cdbbe 3802 case USB_SPEED_WIRELESS: /* fixed at 512 */
551509d2 3803 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
5bb6e0ae 3804 break;
1da177e4 3805 case USB_SPEED_HIGH: /* fixed at 64 */
551509d2 3806 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
1da177e4
LT
3807 break;
3808 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3809 /* to determine the ep0 maxpacket size, try to read
3810 * the device descriptor to get bMaxPacketSize0 and
3811 * then correct our initial guess.
3812 */
551509d2 3813 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
1da177e4
LT
3814 break;
3815 case USB_SPEED_LOW: /* fixed at 8 */
551509d2 3816 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
1da177e4
LT
3817 break;
3818 default:
3819 goto fail;
3820 }
e538dfda
MN
3821
3822 if (udev->speed == USB_SPEED_WIRELESS)
3823 speed = "variable speed Wireless";
3824 else
3825 speed = usb_speed_string(udev->speed);
3826
c6515272
SS
3827 if (udev->speed != USB_SPEED_SUPER)
3828 dev_info(&udev->dev,
e538dfda
MN
3829 "%s %s USB device number %d using %s\n",
3830 (udev->config) ? "reset" : "new", speed,
3b29b68b 3831 devnum, udev->bus->controller->driver->name);
1da177e4
LT
3832
3833 /* Set up TT records, if needed */
3834 if (hdev->tt) {
3835 udev->tt = hdev->tt;
3836 udev->ttport = hdev->ttport;
3837 } else if (udev->speed != USB_SPEED_HIGH
3838 && hdev->speed == USB_SPEED_HIGH) {
d199c96d
AS
3839 if (!hub->tt.hub) {
3840 dev_err(&udev->dev, "parent hub has no TT\n");
3841 retval = -EINVAL;
3842 goto fail;
3843 }
1da177e4
LT
3844 udev->tt = &hub->tt;
3845 udev->ttport = port1;
3846 }
3847
3848 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3849 * Because device hardware and firmware is sometimes buggy in
3850 * this area, and this is how Linux has done it for ages.
3851 * Change it cautiously.
3852 *
3853 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3854 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3855 * so it may help with some non-standards-compliant devices.
3856 * Otherwise we start with SET_ADDRESS and then try to read the
3857 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3858 * value.
3859 */
3860 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
c6515272 3861 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
1da177e4
LT
3862 struct usb_device_descriptor *buf;
3863 int r = 0;
3864
3865#define GET_DESCRIPTOR_BUFSIZE 64
3866 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3867 if (!buf) {
3868 retval = -ENOMEM;
3869 continue;
3870 }
3871
b89ee19a
AS
3872 /* Retry on all errors; some devices are flakey.
3873 * 255 is for WUSB devices, we actually need to use
3874 * 512 (WUSB1.0[4.8.1]).
1da177e4
LT
3875 */
3876 for (j = 0; j < 3; ++j) {
3877 buf->bMaxPacketSize0 = 0;
3878 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3879 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3880 USB_DT_DEVICE << 8, 0,
3881 buf, GET_DESCRIPTOR_BUFSIZE,
fd7c519d 3882 initial_descriptor_timeout);
1da177e4 3883 switch (buf->bMaxPacketSize0) {
5bb6e0ae 3884 case 8: case 16: case 32: case 64: case 255:
1da177e4
LT
3885 if (buf->bDescriptorType ==
3886 USB_DT_DEVICE) {
3887 r = 0;
3888 break;
3889 }
3890 /* FALL THROUGH */
3891 default:
3892 if (r == 0)
3893 r = -EPROTO;
3894 break;
3895 }
3896 if (r == 0)
3897 break;
3898 }
3899 udev->descriptor.bMaxPacketSize0 =
3900 buf->bMaxPacketSize0;
3901 kfree(buf);
3902
75d7cf72 3903 retval = hub_port_reset(hub, port1, udev, delay, false);
1da177e4
LT
3904 if (retval < 0) /* error or disconnect */
3905 goto fail;
3906 if (oldspeed != udev->speed) {
3907 dev_dbg(&udev->dev,
3908 "device reset changed speed!\n");
3909 retval = -ENODEV;
3910 goto fail;
3911 }
3912 if (r) {
b9cef6c3
WF
3913 dev_err(&udev->dev,
3914 "device descriptor read/64, error %d\n",
3915 r);
1da177e4
LT
3916 retval = -EMSGSIZE;
3917 continue;
3918 }
3919#undef GET_DESCRIPTOR_BUFSIZE
3920 }
3921
6c529cdc
IPG
3922 /*
3923 * If device is WUSB, we already assigned an
3924 * unauthorized address in the Connect Ack sequence;
3925 * authorization will assign the final address.
3926 */
c6515272 3927 if (udev->wusb == 0) {
6c529cdc
IPG
3928 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3929 retval = hub_set_address(udev, devnum);
3930 if (retval >= 0)
3931 break;
3932 msleep(200);
3933 }
3934 if (retval < 0) {
3935 dev_err(&udev->dev,
3936 "device not accepting address %d, error %d\n",
3937 devnum, retval);
3938 goto fail;
3939 }
c6515272
SS
3940 if (udev->speed == USB_SPEED_SUPER) {
3941 devnum = udev->devnum;
3942 dev_info(&udev->dev,
3b29b68b 3943 "%s SuperSpeed USB device number %d using %s\n",
c6515272 3944 (udev->config) ? "reset" : "new",
3b29b68b 3945 devnum, udev->bus->controller->driver->name);
c6515272 3946 }
6c529cdc
IPG
3947
3948 /* cope with hardware quirkiness:
3949 * - let SET_ADDRESS settle, some device hardware wants it
3950 * - read ep0 maxpacket even for high and low speed,
3951 */
3952 msleep(10);
c6515272 3953 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
1da177e4 3954 break;
6c529cdc 3955 }
1da177e4
LT
3956
3957 retval = usb_get_device_descriptor(udev, 8);
3958 if (retval < 8) {
b9cef6c3
WF
3959 dev_err(&udev->dev,
3960 "device descriptor read/8, error %d\n",
3961 retval);
1da177e4
LT
3962 if (retval >= 0)
3963 retval = -EMSGSIZE;
3964 } else {
3965 retval = 0;
3966 break;
3967 }
3968 }
3969 if (retval)
3970 goto fail;
3971
d8aec3db
EF
3972 /*
3973 * Some superspeed devices have finished the link training process
3974 * and attached to a superspeed hub port, but the device descriptor
3975 * got from those devices show they aren't superspeed devices. Warm
3976 * reset the port attached by the devices can fix them.
3977 */
3978 if ((udev->speed == USB_SPEED_SUPER) &&
3979 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
3980 dev_err(&udev->dev, "got a wrong device descriptor, "
3981 "warm reset device\n");
3982 hub_port_reset(hub, port1, udev,
3983 HUB_BH_RESET_TIME, true);
3984 retval = -EINVAL;
3985 goto fail;
3986 }
3987
6b403b02
SS
3988 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
3989 udev->speed == USB_SPEED_SUPER)
3990 i = 512;
3991 else
3992 i = udev->descriptor.bMaxPacketSize0;
29cc8897 3993 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
56626a72 3994 if (udev->speed == USB_SPEED_LOW ||
1da177e4 3995 !(i == 8 || i == 16 || i == 32 || i == 64)) {
56626a72 3996 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
1da177e4
LT
3997 retval = -EMSGSIZE;
3998 goto fail;
3999 }
56626a72
AS
4000 if (udev->speed == USB_SPEED_FULL)
4001 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4002 else
4003 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
1da177e4 4004 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
fc721f51 4005 usb_ep0_reinit(udev);
1da177e4
LT
4006 }
4007
4008 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4009 if (retval < (signed)sizeof(udev->descriptor)) {
b9cef6c3
WF
4010 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4011 retval);
1da177e4
LT
4012 if (retval >= 0)
4013 retval = -ENOMSG;
4014 goto fail;
4015 }
4016
1ff4df56
AX
4017 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4018 retval = usb_get_bos_descriptor(udev);
51e0a012 4019 if (!retval) {
d9b2099c 4020 udev->lpm_capable = usb_device_supports_lpm(udev);
51e0a012
SS
4021 usb_set_lpm_parameters(udev);
4022 }
1ff4df56 4023 }
3148bf04 4024
1da177e4 4025 retval = 0;
48f24970
AD
4026 /* notify HCD that we have a device connected and addressed */
4027 if (hcd->driver->update_device)
4028 hcd->driver->update_device(hcd, udev);
1da177e4 4029fail:
4326ed0b 4030 if (retval) {
1da177e4 4031 hub_port_disable(hub, port1, 0);
3b29b68b 4032 update_devnum(udev, devnum); /* for disconnect processing */
4326ed0b 4033 }
4186ecf8 4034 mutex_unlock(&usb_address0_mutex);
1da177e4
LT
4035 return retval;
4036}
4037
4038static void
4039check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4040{
4041 struct usb_qualifier_descriptor *qual;
4042 int status;
4043
e94b1766 4044 qual = kmalloc (sizeof *qual, GFP_KERNEL);
1da177e4
LT
4045 if (qual == NULL)
4046 return;
4047
4048 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4049 qual, sizeof *qual);
4050 if (status == sizeof *qual) {
4051 dev_info(&udev->dev, "not running at top speed; "
4052 "connect to a high speed hub\n");
4053 /* hub LEDs are probably harder to miss than syslog */
4054 if (hub->has_indicators) {
4055 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
c4028958 4056 schedule_delayed_work (&hub->leds, 0);
1da177e4
LT
4057 }
4058 }
1bc3c9e1 4059 kfree(qual);
1da177e4
LT
4060}
4061
4062static unsigned
4063hub_power_remaining (struct usb_hub *hub)
4064{
4065 struct usb_device *hdev = hub->hdev;
4066 int remaining;
55c52718 4067 int port1;
1da177e4 4068
55c52718 4069 if (!hub->limited_power)
1da177e4
LT
4070 return 0;
4071
55c52718
AS
4072 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4073 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
ff823c79 4074 struct usb_device *udev = hub->ports[port1 - 1]->child;
55c52718 4075 int delta;
1da177e4
LT
4076
4077 if (!udev)
4078 continue;
4079
55c52718
AS
4080 /* Unconfigured devices may not use more than 100mA,
4081 * or 8mA for OTG ports */
1da177e4 4082 if (udev->actconfig)
55c52718
AS
4083 delta = udev->actconfig->desc.bMaxPower * 2;
4084 else if (port1 != udev->bus->otg_port || hdev->parent)
4085 delta = 100;
1da177e4 4086 else
55c52718
AS
4087 delta = 8;
4088 if (delta > hub->mA_per_port)
b9cef6c3
WF
4089 dev_warn(&udev->dev,
4090 "%dmA is over %umA budget for port %d!\n",
4091 delta, hub->mA_per_port, port1);
1da177e4
LT
4092 remaining -= delta;
4093 }
4094 if (remaining < 0) {
55c52718
AS
4095 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4096 - remaining);
1da177e4
LT
4097 remaining = 0;
4098 }
4099 return remaining;
4100}
4101
4102/* Handle physical or logical connection change events.
4103 * This routine is called when:
4104 * a port connection-change occurs;
4105 * a port enable-change occurs (often caused by EMI);
742120c6 4106 * usb_reset_and_verify_device() encounters changed descriptors (as from
1da177e4
LT
4107 * a firmware download)
4108 * caller already locked the hub
4109 */
4110static void hub_port_connect_change(struct usb_hub *hub, int port1,
4111 u16 portstatus, u16 portchange)
4112{
4113 struct usb_device *hdev = hub->hdev;
4114 struct device *hub_dev = hub->intfdev;
90da096e 4115 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
24618b0c
AS
4116 unsigned wHubCharacteristics =
4117 le16_to_cpu(hub->descriptor->wHubCharacteristics);
8808f00c 4118 struct usb_device *udev;
1da177e4 4119 int status, i;
24618b0c 4120
1da177e4
LT
4121 dev_dbg (hub_dev,
4122 "port %d, status %04x, change %04x, %s\n",
131dec34 4123 port1, portstatus, portchange, portspeed(hub, portstatus));
1da177e4
LT
4124
4125 if (hub->has_indicators) {
4126 set_port_led(hub, port1, HUB_LED_AUTO);
4127 hub->indicator[port1-1] = INDICATOR_AUTO;
4128 }
1da177e4
LT
4129
4130#ifdef CONFIG_USB_OTG
4131 /* during HNP, don't repeat the debounce */
4132 if (hdev->bus->is_b_host)
24618b0c
AS
4133 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4134 USB_PORT_STAT_C_ENABLE);
1da177e4
LT
4135#endif
4136
8808f00c 4137 /* Try to resuscitate an existing device */
ff823c79 4138 udev = hub->ports[port1 - 1]->child;
8808f00c
AS
4139 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4140 udev->state != USB_STATE_NOTATTACHED) {
8808f00c
AS
4141 usb_lock_device(udev);
4142 if (portstatus & USB_PORT_STAT_ENABLE) {
4143 status = 0; /* Nothing to do */
8808f00c
AS
4144
4145#ifdef CONFIG_USB_SUSPEND
5257d97a
AS
4146 } else if (udev->state == USB_STATE_SUSPENDED &&
4147 udev->persist_enabled) {
8808f00c
AS
4148 /* For a suspended device, treat this as a
4149 * remote wakeup event.
4150 */
0534d468 4151 status = usb_remote_wakeup(udev);
8808f00c
AS
4152#endif
4153
4154 } else {
5257d97a 4155 status = -ENODEV; /* Don't resuscitate */
8808f00c
AS
4156 }
4157 usb_unlock_device(udev);
4158
4159 if (status == 0) {
4160 clear_bit(port1, hub->change_bits);
4161 return;
4162 }
4163 }
4164
24618b0c 4165 /* Disconnect any existing devices under this port */
8808f00c 4166 if (udev)
ff823c79 4167 usb_disconnect(&hub->ports[port1 - 1]->child);
24618b0c
AS
4168 clear_bit(port1, hub->change_bits);
4169
253e0572
AS
4170 /* We can forget about a "removed" device when there's a physical
4171 * disconnect or the connect status changes.
4172 */
4173 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4174 (portchange & USB_PORT_STAT_C_CONNECTION))
4175 clear_bit(port1, hub->removed_bits);
4176
5257d97a
AS
4177 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4178 USB_PORT_STAT_C_ENABLE)) {
4179 status = hub_port_debounce(hub, port1);
4180 if (status < 0) {
4181 if (printk_ratelimit())
4182 dev_err(hub_dev, "connect-debounce failed, "
4183 "port %d disabled\n", port1);
4184 portstatus &= ~USB_PORT_STAT_CONNECTION;
4185 } else {
4186 portstatus = status;
4187 }
4188 }
4189
925aa46b
RZ
4190 if (hcd->phy && !hdev->parent) {
4191 if (portstatus & USB_PORT_STAT_CONNECTION)
4192 usb_phy_notify_connect(hcd->phy, port1);
4193 else
4194 usb_phy_notify_disconnect(hcd->phy, port1);
4195 }
4196
253e0572
AS
4197 /* Return now if debouncing failed or nothing is connected or
4198 * the device was "removed".
4199 */
4200 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4201 test_bit(port1, hub->removed_bits)) {
1da177e4
LT
4202
4203 /* maybe switch power back on (e.g. root hub was reset) */
74ad9bd2 4204 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
0ed9a57e 4205 && !port_is_power_on(hub, portstatus))
1da177e4 4206 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
5257d97a 4207
1da177e4
LT
4208 if (portstatus & USB_PORT_STAT_ENABLE)
4209 goto done;
4210 return;
4211 }
4212
1da177e4 4213 for (i = 0; i < SET_CONFIG_TRIES; i++) {
1da177e4
LT
4214
4215 /* reallocate for each attempt, since references
4216 * to the previous one can escape in various ways
4217 */
4218 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4219 if (!udev) {
4220 dev_err (hub_dev,
4221 "couldn't allocate port %d usb_device\n",
4222 port1);
4223 goto done;
4224 }
4225
4226 usb_set_device_state(udev, USB_STATE_POWERED);
55c52718 4227 udev->bus_mA = hub->mA_per_port;
b6956ffa 4228 udev->level = hdev->level + 1;
8af548dc 4229 udev->wusb = hub_is_wusb(hub);
55c52718 4230
131dec34
SS
4231 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4232 if (hub_is_superspeed(hub->hdev))
e7b77172
SS
4233 udev->speed = USB_SPEED_SUPER;
4234 else
4235 udev->speed = USB_SPEED_UNKNOWN;
4236
3b29b68b 4237 choose_devnum(udev);
c8d4af8e
AX
4238 if (udev->devnum <= 0) {
4239 status = -ENOTCONN; /* Don't retry */
4240 goto loop;
c6515272
SS
4241 }
4242
e7b77172 4243 /* reset (non-USB 3.0 devices) and get descriptor */
1da177e4
LT
4244 status = hub_port_init(hub, udev, port1, i);
4245 if (status < 0)
4246 goto loop;
4247
93362a87
PD
4248 usb_detect_quirks(udev);
4249 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4250 msleep(1000);
4251
1da177e4
LT
4252 /* consecutive bus-powered hubs aren't reliable; they can
4253 * violate the voltage drop budget. if the new child has
4254 * a "powered" LED, users should notice we didn't enable it
4255 * (without reading syslog), even without per-port LEDs
4256 * on the parent.
4257 */
4258 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
55c52718 4259 && udev->bus_mA <= 100) {
1da177e4
LT
4260 u16 devstat;
4261
4262 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4263 &devstat);
55c52718 4264 if (status < 2) {
1da177e4
LT
4265 dev_dbg(&udev->dev, "get status %d ?\n", status);
4266 goto loop_disable;
4267 }
55c52718 4268 le16_to_cpus(&devstat);
1da177e4
LT
4269 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4270 dev_err(&udev->dev,
4271 "can't connect bus-powered hub "
4272 "to this port\n");
4273 if (hub->has_indicators) {
4274 hub->indicator[port1-1] =
4275 INDICATOR_AMBER_BLINK;
c4028958 4276 schedule_delayed_work (&hub->leds, 0);
1da177e4
LT
4277 }
4278 status = -ENOTCONN; /* Don't retry */
4279 goto loop_disable;
4280 }
4281 }
4282
4283 /* check for devices running slower than they could */
4284 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4285 && udev->speed == USB_SPEED_FULL
4286 && highspeed_hubs != 0)
4287 check_highspeed (hub, udev, port1);
4288
fa286188 4289 /* Store the parent's children[] pointer. At this point
1da177e4
LT
4290 * udev becomes globally accessible, although presumably
4291 * no one will look at it until hdev is unlocked.
4292 */
1da177e4
LT
4293 status = 0;
4294
4295 /* We mustn't add new devices if the parent hub has
4296 * been disconnected; we would race with the
4297 * recursively_mark_NOTATTACHED() routine.
4298 */
4299 spin_lock_irq(&device_state_lock);
4300 if (hdev->state == USB_STATE_NOTATTACHED)
4301 status = -ENOTCONN;
4302 else
ff823c79 4303 hub->ports[port1 - 1]->child = udev;
1da177e4
LT
4304 spin_unlock_irq(&device_state_lock);
4305
4306 /* Run it through the hoops (find a driver, etc) */
4307 if (!status) {
4308 status = usb_new_device(udev);
4309 if (status) {
4310 spin_lock_irq(&device_state_lock);
ff823c79 4311 hub->ports[port1 - 1]->child = NULL;
1da177e4
LT
4312 spin_unlock_irq(&device_state_lock);
4313 }
4314 }
4315
1da177e4
LT
4316 if (status)
4317 goto loop_disable;
4318
4319 status = hub_power_remaining(hub);
4320 if (status)
55c52718 4321 dev_dbg(hub_dev, "%dmA power budget left\n", status);
1da177e4
LT
4322
4323 return;
4324
4325loop_disable:
4326 hub_port_disable(hub, port1, 1);
4327loop:
fc721f51 4328 usb_ep0_reinit(udev);
3b29b68b 4329 release_devnum(udev);
f7410ced 4330 hub_free_dev(udev);
1da177e4 4331 usb_put_dev(udev);
ffcdc18d 4332 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
1da177e4
LT
4333 break;
4334 }
3a31155c
AS
4335 if (hub->hdev->parent ||
4336 !hcd->driver->port_handed_over ||
4337 !(hcd->driver->port_handed_over)(hcd, port1))
4338 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4339 port1);
1da177e4
LT
4340
4341done:
4342 hub_port_disable(hub, port1, 1);
90da096e
BR
4343 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4344 hcd->driver->relinquish_port(hcd, port1);
1da177e4
LT
4345}
4346
714b07be
SS
4347/* Returns 1 if there was a remote wakeup and a connect status change. */
4348static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
72937e1e 4349 u16 portstatus, u16 portchange)
714b07be
SS
4350{
4351 struct usb_device *hdev;
4352 struct usb_device *udev;
4353 int connect_change = 0;
4354 int ret;
4355
4356 hdev = hub->hdev;
ff823c79 4357 udev = hub->ports[port - 1]->child;
4ee823b8
SS
4358 if (!hub_is_superspeed(hdev)) {
4359 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4360 return 0;
4361 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4362 } else {
4363 if (!udev || udev->state != USB_STATE_SUSPENDED ||
72937e1e
SS
4364 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4365 USB_SS_PORT_LS_U0)
4ee823b8
SS
4366 return 0;
4367 }
4368
714b07be
SS
4369 if (udev) {
4370 /* TRSMRCY = 10 msec */
4371 msleep(10);
4372
4373 usb_lock_device(udev);
4374 ret = usb_remote_wakeup(udev);
4375 usb_unlock_device(udev);
4376 if (ret < 0)
4377 connect_change = 1;
4378 } else {
4379 ret = -ENODEV;
4380 hub_port_disable(hub, port, 1);
4381 }
4382 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4383 port, ret);
4384 return connect_change;
4385}
4386
1da177e4
LT
4387static void hub_events(void)
4388{
4389 struct list_head *tmp;
4390 struct usb_device *hdev;
4391 struct usb_interface *intf;
4392 struct usb_hub *hub;
4393 struct device *hub_dev;
4394 u16 hubstatus;
4395 u16 hubchange;
4396 u16 portstatus;
4397 u16 portchange;
4398 int i, ret;
4ee823b8 4399 int connect_change, wakeup_change;
1da177e4
LT
4400
4401 /*
4402 * We restart the list every time to avoid a deadlock with
4403 * deleting hubs downstream from this one. This should be
4404 * safe since we delete the hub from the event list.
4405 * Not the most efficient, but avoids deadlocks.
4406 */
4407 while (1) {
4408
4409 /* Grab the first entry at the beginning of the list */
4410 spin_lock_irq(&hub_event_lock);
4411 if (list_empty(&hub_event_list)) {
4412 spin_unlock_irq(&hub_event_lock);
4413 break;
4414 }
4415
4416 tmp = hub_event_list.next;
4417 list_del_init(tmp);
4418
4419 hub = list_entry(tmp, struct usb_hub, event_list);
e8054854
AS
4420 kref_get(&hub->kref);
4421 spin_unlock_irq(&hub_event_lock);
1da177e4 4422
e8054854
AS
4423 hdev = hub->hdev;
4424 hub_dev = hub->intfdev;
4425 intf = to_usb_interface(hub_dev);
40f122f3 4426 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
1da177e4
LT
4427 hdev->state, hub->descriptor
4428 ? hub->descriptor->bNbrPorts
4429 : 0,
4430 /* NOTE: expects max 15 ports... */
4431 (u16) hub->change_bits[0],
40f122f3 4432 (u16) hub->event_bits[0]);
1da177e4 4433
1da177e4
LT
4434 /* Lock the device, then check to see if we were
4435 * disconnected while waiting for the lock to succeed. */
06b84e8a 4436 usb_lock_device(hdev);
e8054854 4437 if (unlikely(hub->disconnected))
9bbdf1e0 4438 goto loop_disconnected;
1da177e4
LT
4439
4440 /* If the hub has died, clean up after it */
4441 if (hdev->state == USB_STATE_NOTATTACHED) {
7de18d8b 4442 hub->error = -ENODEV;
4330354f 4443 hub_quiesce(hub, HUB_DISCONNECT);
1da177e4
LT
4444 goto loop;
4445 }
4446
40f122f3
AS
4447 /* Autoresume */
4448 ret = usb_autopm_get_interface(intf);
4449 if (ret) {
4450 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
4451 goto loop;
4452 }
a8e7c565 4453
40f122f3 4454 /* If this is an inactive hub, do nothing */
1da177e4 4455 if (hub->quiescing)
40f122f3 4456 goto loop_autopm;
1da177e4
LT
4457
4458 if (hub->error) {
4459 dev_dbg (hub_dev, "resetting for error %d\n",
4460 hub->error);
4461
742120c6 4462 ret = usb_reset_device(hdev);
1da177e4
LT
4463 if (ret) {
4464 dev_dbg (hub_dev,
4465 "error resetting hub: %d\n", ret);
40f122f3 4466 goto loop_autopm;
1da177e4
LT
4467 }
4468
4469 hub->nerrors = 0;
4470 hub->error = 0;
4471 }
4472
4473 /* deal with port status changes */
4474 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4475 if (test_bit(i, hub->busy_bits))
4476 continue;
4477 connect_change = test_bit(i, hub->change_bits);
72937e1e 4478 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
1da177e4 4479 if (!test_and_clear_bit(i, hub->event_bits) &&
4ee823b8 4480 !connect_change && !wakeup_change)
1da177e4
LT
4481 continue;
4482
4483 ret = hub_port_status(hub, i,
4484 &portstatus, &portchange);
4485 if (ret < 0)
4486 continue;
4487
1da177e4
LT
4488 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4489 clear_port_feature(hdev, i,
4490 USB_PORT_FEAT_C_CONNECTION);
4491 connect_change = 1;
4492 }
4493
4494 if (portchange & USB_PORT_STAT_C_ENABLE) {
4495 if (!connect_change)
4496 dev_dbg (hub_dev,
4497 "port %d enable change, "
4498 "status %08x\n",
4499 i, portstatus);
4500 clear_port_feature(hdev, i,
4501 USB_PORT_FEAT_C_ENABLE);
4502
4503 /*
4504 * EM interference sometimes causes badly
4505 * shielded USB devices to be shutdown by
4506 * the hub, this hack enables them again.
4507 * Works at least with mouse driver.
4508 */
4509 if (!(portstatus & USB_PORT_STAT_ENABLE)
4510 && !connect_change
ff823c79 4511 && hub->ports[i - 1]->child) {
1da177e4
LT
4512 dev_err (hub_dev,
4513 "port %i "
4514 "disabled by hub (EMI?), "
4515 "re-enabling...\n",
4516 i);
4517 connect_change = 1;
4518 }
4519 }
4520
72937e1e
SS
4521 if (hub_handle_remote_wakeup(hub, i,
4522 portstatus, portchange))
714b07be 4523 connect_change = 1;
8808f00c 4524
1da177e4 4525 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
752d57a8
PB
4526 u16 status = 0;
4527 u16 unused;
4528
4529 dev_dbg(hub_dev, "over-current change on port "
4530 "%d\n", i);
1da177e4
LT
4531 clear_port_feature(hdev, i,
4532 USB_PORT_FEAT_C_OVER_CURRENT);
752d57a8 4533 msleep(100); /* Cool down */
8520f380 4534 hub_power_on(hub, true);
752d57a8
PB
4535 hub_port_status(hub, i, &status, &unused);
4536 if (status & USB_PORT_STAT_OVERCURRENT)
4537 dev_err(hub_dev, "over-current "
4538 "condition on port %d\n", i);
1da177e4
LT
4539 }
4540
4541 if (portchange & USB_PORT_STAT_C_RESET) {
4542 dev_dbg (hub_dev,
4543 "reset change on port %d\n",
4544 i);
4545 clear_port_feature(hdev, i,
4546 USB_PORT_FEAT_C_RESET);
4547 }
c7061574
SS
4548 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4549 hub_is_superspeed(hub->hdev)) {
4550 dev_dbg(hub_dev,
4551 "warm reset change on port %d\n",
4552 i);
4553 clear_port_feature(hdev, i,
4554 USB_PORT_FEAT_C_BH_PORT_RESET);
4555 }
dbe79bbe
JY
4556 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4557 clear_port_feature(hub->hdev, i,
4558 USB_PORT_FEAT_C_PORT_LINK_STATE);
4559 }
4560 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4561 dev_warn(hub_dev,
4562 "config error on port %d\n",
4563 i);
4564 clear_port_feature(hub->hdev, i,
4565 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4566 }
1da177e4 4567
5e467f6e
AX
4568 /* Warm reset a USB3 protocol port if it's in
4569 * SS.Inactive state.
4570 */
8bea2bd3 4571 if (hub_port_warm_reset_required(hub, portstatus)) {
5e467f6e 4572 dev_dbg(hub_dev, "warm reset port %d\n", i);
75d7cf72
AX
4573 hub_port_reset(hub, i, NULL,
4574 HUB_BH_RESET_TIME, true);
5e467f6e
AX
4575 }
4576
1da177e4
LT
4577 if (connect_change)
4578 hub_port_connect_change(hub, i,
4579 portstatus, portchange);
4580 } /* end for i */
4581
4582 /* deal with hub status changes */
4583 if (test_and_clear_bit(0, hub->event_bits) == 0)
4584 ; /* do nothing */
4585 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4586 dev_err (hub_dev, "get_hub_status failed\n");
4587 else {
4588 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4589 dev_dbg (hub_dev, "power change\n");
4590 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
55c52718
AS
4591 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4592 /* FIXME: Is this always true? */
55c52718 4593 hub->limited_power = 1;
403fae78 4594 else
4595 hub->limited_power = 0;
1da177e4
LT
4596 }
4597 if (hubchange & HUB_CHANGE_OVERCURRENT) {
752d57a8
PB
4598 u16 status = 0;
4599 u16 unused;
4600
4601 dev_dbg(hub_dev, "over-current change\n");
1da177e4 4602 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
752d57a8 4603 msleep(500); /* Cool down */
8520f380 4604 hub_power_on(hub, true);
752d57a8
PB
4605 hub_hub_status(hub, &status, &unused);
4606 if (status & HUB_STATUS_OVERCURRENT)
4607 dev_err(hub_dev, "over-current "
4608 "condition\n");
1da177e4
LT
4609 }
4610 }
4611
8e4ceb38
AS
4612 loop_autopm:
4613 /* Balance the usb_autopm_get_interface() above */
4614 usb_autopm_put_interface_no_suspend(intf);
4615 loop:
4616 /* Balance the usb_autopm_get_interface_no_resume() in
4617 * kick_khubd() and allow autosuspend.
4618 */
4619 usb_autopm_put_interface(intf);
9bbdf1e0 4620 loop_disconnected:
1da177e4 4621 usb_unlock_device(hdev);
e8054854 4622 kref_put(&hub->kref, hub_release);
1da177e4
LT
4623
4624 } /* end while (1) */
4625}
4626
4627static int hub_thread(void *__unused)
4628{
3bb1af52
AS
4629 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4630 * port handover. Otherwise it might see that a full-speed device
4631 * was gone before the EHCI controller had handed its port over to
4632 * the companion full-speed controller.
4633 */
83144186 4634 set_freezable();
3bb1af52 4635
1da177e4
LT
4636 do {
4637 hub_events();
e42837bc 4638 wait_event_freezable(khubd_wait,
9c8d6178
AM
4639 !list_empty(&hub_event_list) ||
4640 kthread_should_stop());
9c8d6178 4641 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
1da177e4 4642
9c8d6178
AM
4643 pr_debug("%s: khubd exiting\n", usbcore_name);
4644 return 0;
1da177e4
LT
4645}
4646
1e927d96 4647static const struct usb_device_id hub_id_table[] = {
1da177e4
LT
4648 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4649 .bDeviceClass = USB_CLASS_HUB},
4650 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4651 .bInterfaceClass = USB_CLASS_HUB},
4652 { } /* Terminating entry */
4653};
4654
4655MODULE_DEVICE_TABLE (usb, hub_id_table);
4656
4657static struct usb_driver hub_driver = {
1da177e4
LT
4658 .name = "hub",
4659 .probe = hub_probe,
4660 .disconnect = hub_disconnect,
4661 .suspend = hub_suspend,
4662 .resume = hub_resume,
f07600cf 4663 .reset_resume = hub_reset_resume,
7de18d8b
AS
4664 .pre_reset = hub_pre_reset,
4665 .post_reset = hub_post_reset,
c532b29a 4666 .unlocked_ioctl = hub_ioctl,
1da177e4 4667 .id_table = hub_id_table,
40f122f3 4668 .supports_autosuspend = 1,
1da177e4
LT
4669};
4670
4671int usb_hub_init(void)
4672{
1da177e4
LT
4673 if (usb_register(&hub_driver) < 0) {
4674 printk(KERN_ERR "%s: can't register hub driver\n",
4675 usbcore_name);
4676 return -1;
4677 }
4678
9c8d6178
AM
4679 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4680 if (!IS_ERR(khubd_task))
1da177e4 4681 return 0;
1da177e4
LT
4682
4683 /* Fall through if kernel_thread failed */
4684 usb_deregister(&hub_driver);
4685 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4686
4687 return -1;
4688}
4689
4690void usb_hub_cleanup(void)
4691{
9c8d6178 4692 kthread_stop(khubd_task);
1da177e4
LT
4693
4694 /*
4695 * Hub resources are freed for us by usb_deregister. It calls
4696 * usb_driver_purge on every device which in turn calls that
4697 * devices disconnect function if it is using this driver.
4698 * The hub_disconnect function takes care of releasing the
4699 * individual hub resources. -greg
4700 */
4701 usb_deregister(&hub_driver);
4702} /* usb_hub_cleanup() */
4703
eb764c4b
AS
4704static int descriptors_changed(struct usb_device *udev,
4705 struct usb_device_descriptor *old_device_descriptor)
4706{
4707 int changed = 0;
4708 unsigned index;
4709 unsigned serial_len = 0;
4710 unsigned len;
4711 unsigned old_length;
4712 int length;
4713 char *buf;
4714
4715 if (memcmp(&udev->descriptor, old_device_descriptor,
4716 sizeof(*old_device_descriptor)) != 0)
4717 return 1;
4718
4719 /* Since the idVendor, idProduct, and bcdDevice values in the
4720 * device descriptor haven't changed, we will assume the
4721 * Manufacturer and Product strings haven't changed either.
4722 * But the SerialNumber string could be different (e.g., a
4723 * different flash card of the same brand).
4724 */
4725 if (udev->serial)
4726 serial_len = strlen(udev->serial) + 1;
1da177e4 4727
eb764c4b 4728 len = serial_len;
1da177e4 4729 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
eb764c4b
AS
4730 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4731 len = max(len, old_length);
1da177e4 4732 }
eb764c4b 4733
0cc1a51f 4734 buf = kmalloc(len, GFP_NOIO);
1da177e4
LT
4735 if (buf == NULL) {
4736 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4737 /* assume the worst */
4738 return 1;
4739 }
4740 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
eb764c4b 4741 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
1da177e4
LT
4742 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4743 old_length);
eb764c4b 4744 if (length != old_length) {
1da177e4
LT
4745 dev_dbg(&udev->dev, "config index %d, error %d\n",
4746 index, length);
eb764c4b 4747 changed = 1;
1da177e4
LT
4748 break;
4749 }
4750 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4751 != 0) {
4752 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
eb764c4b
AS
4753 index,
4754 ((struct usb_config_descriptor *) buf)->
4755 bConfigurationValue);
4756 changed = 1;
1da177e4
LT
4757 break;
4758 }
4759 }
eb764c4b
AS
4760
4761 if (!changed && serial_len) {
4762 length = usb_string(udev, udev->descriptor.iSerialNumber,
4763 buf, serial_len);
4764 if (length + 1 != serial_len) {
4765 dev_dbg(&udev->dev, "serial string error %d\n",
4766 length);
4767 changed = 1;
4768 } else if (memcmp(buf, udev->serial, length) != 0) {
4769 dev_dbg(&udev->dev, "serial string changed\n");
4770 changed = 1;
4771 }
4772 }
4773
1da177e4 4774 kfree(buf);
eb764c4b 4775 return changed;
1da177e4
LT
4776}
4777
4778/**
742120c6 4779 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
1da177e4
LT
4780 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4781 *
79efa097
AS
4782 * WARNING - don't use this routine to reset a composite device
4783 * (one with multiple interfaces owned by separate drivers)!
742120c6 4784 * Use usb_reset_device() instead.
1da177e4
LT
4785 *
4786 * Do a port reset, reassign the device's address, and establish its
4787 * former operating configuration. If the reset fails, or the device's
4788 * descriptors change from their values before the reset, or the original
4789 * configuration and altsettings cannot be restored, a flag will be set
4790 * telling khubd to pretend the device has been disconnected and then
4791 * re-connected. All drivers will be unbound, and the device will be
4792 * re-enumerated and probed all over again.
4793 *
4794 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4795 * flagged for logical disconnection, or some other negative error code
4796 * if the reset wasn't even attempted.
4797 *
4798 * The caller must own the device lock. For example, it's safe to use
4799 * this from a driver probe() routine after downloading new firmware.
4800 * For calls that might not occur during probe(), drivers should lock
4801 * the device using usb_lock_device_for_reset().
6bc6cff5
AS
4802 *
4803 * Locking exception: This routine may also be called from within an
4804 * autoresume handler. Such usage won't conflict with other tasks
4805 * holding the device lock because these tasks should always call
4806 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
1da177e4 4807 */
742120c6 4808static int usb_reset_and_verify_device(struct usb_device *udev)
1da177e4
LT
4809{
4810 struct usb_device *parent_hdev = udev->parent;
4811 struct usb_hub *parent_hub;
3f0479e0 4812 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1da177e4 4813 struct usb_device_descriptor descriptor = udev->descriptor;
12c3da34
AS
4814 int i, ret = 0;
4815 int port1 = udev->portnum;
1da177e4
LT
4816
4817 if (udev->state == USB_STATE_NOTATTACHED ||
4818 udev->state == USB_STATE_SUSPENDED) {
4819 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4820 udev->state);
4821 return -EINVAL;
4822 }
4823
4824 if (!parent_hdev) {
4bec9917 4825 /* this requires hcd-specific logic; see ohci_restart() */
441b62c1 4826 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
1da177e4
LT
4827 return -EISDIR;
4828 }
1da177e4
LT
4829 parent_hub = hdev_to_hub(parent_hdev);
4830
f74631e3
SS
4831 /* Disable LPM and LTM while we reset the device and reinstall the alt
4832 * settings. Device-initiated LPM settings, and system exit latency
4833 * settings are cleared when the device is reset, so we have to set
4834 * them up again.
6d1d0513
SS
4835 */
4836 ret = usb_unlocked_disable_lpm(udev);
4837 if (ret) {
4838 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4839 goto re_enumerate;
4840 }
f74631e3
SS
4841 ret = usb_disable_ltm(udev);
4842 if (ret) {
4843 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4844 __func__);
4845 goto re_enumerate;
4846 }
6d1d0513 4847
1da177e4
LT
4848 set_bit(port1, parent_hub->busy_bits);
4849 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4850
4851 /* ep0 maxpacket size may change; let the HCD know about it.
4852 * Other endpoints will be handled by re-enumeration. */
fc721f51 4853 usb_ep0_reinit(udev);
1da177e4 4854 ret = hub_port_init(parent_hub, udev, port1, i);
dd4dd19e 4855 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
1da177e4
LT
4856 break;
4857 }
4858 clear_bit(port1, parent_hub->busy_bits);
d5cbad4b 4859
1da177e4
LT
4860 if (ret < 0)
4861 goto re_enumerate;
4862
4863 /* Device might have changed firmware (DFU or similar) */
eb764c4b 4864 if (descriptors_changed(udev, &descriptor)) {
1da177e4
LT
4865 dev_info(&udev->dev, "device firmware changed\n");
4866 udev->descriptor = descriptor; /* for disconnect() calls */
4867 goto re_enumerate;
4868 }
4fe0387a
AS
4869
4870 /* Restore the device's previous configuration */
1da177e4
LT
4871 if (!udev->actconfig)
4872 goto done;
3f0479e0 4873
d673bfcb 4874 mutex_lock(hcd->bandwidth_mutex);
3f0479e0
SS
4875 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4876 if (ret < 0) {
4877 dev_warn(&udev->dev,
4878 "Busted HC? Not enough HCD resources for "
4879 "old configuration.\n");
d673bfcb 4880 mutex_unlock(hcd->bandwidth_mutex);
3f0479e0
SS
4881 goto re_enumerate;
4882 }
1da177e4
LT
4883 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4884 USB_REQ_SET_CONFIGURATION, 0,
4885 udev->actconfig->desc.bConfigurationValue, 0,
4886 NULL, 0, USB_CTRL_SET_TIMEOUT);
4887 if (ret < 0) {
4888 dev_err(&udev->dev,
4889 "can't restore configuration #%d (error=%d)\n",
4890 udev->actconfig->desc.bConfigurationValue, ret);
d673bfcb 4891 mutex_unlock(hcd->bandwidth_mutex);
1da177e4
LT
4892 goto re_enumerate;
4893 }
d673bfcb 4894 mutex_unlock(hcd->bandwidth_mutex);
1da177e4
LT
4895 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4896
4fe0387a
AS
4897 /* Put interfaces back into the same altsettings as before.
4898 * Don't bother to send the Set-Interface request for interfaces
4899 * that were already in altsetting 0; besides being unnecessary,
4900 * many devices can't handle it. Instead just reset the host-side
4901 * endpoint state.
4902 */
1da177e4 4903 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
3f0479e0
SS
4904 struct usb_host_config *config = udev->actconfig;
4905 struct usb_interface *intf = config->interface[i];
1da177e4
LT
4906 struct usb_interface_descriptor *desc;
4907
1da177e4 4908 desc = &intf->cur_altsetting->desc;
4fe0387a
AS
4909 if (desc->bAlternateSetting == 0) {
4910 usb_disable_interface(udev, intf, true);
4911 usb_enable_interface(udev, intf, true);
4912 ret = 0;
4913 } else {
04a723ea
SS
4914 /* Let the bandwidth allocation function know that this
4915 * device has been reset, and it will have to use
4916 * alternate setting 0 as the current alternate setting.
3f0479e0 4917 */
04a723ea 4918 intf->resetting_device = 1;
4fe0387a
AS
4919 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4920 desc->bAlternateSetting);
04a723ea 4921 intf->resetting_device = 0;
4fe0387a 4922 }
1da177e4
LT
4923 if (ret < 0) {
4924 dev_err(&udev->dev, "failed to restore interface %d "
4925 "altsetting %d (error=%d)\n",
4926 desc->bInterfaceNumber,
4927 desc->bAlternateSetting,
4928 ret);
4929 goto re_enumerate;
4930 }
4931 }
4932
4933done:
f74631e3 4934 /* Now that the alt settings are re-installed, enable LTM and LPM. */
8306095f 4935 usb_unlocked_enable_lpm(udev);
f74631e3 4936 usb_enable_ltm(udev);
1da177e4
LT
4937 return 0;
4938
4939re_enumerate:
6d1d0513 4940 /* LPM state doesn't matter when we're about to destroy the device. */
1da177e4
LT
4941 hub_port_logical_disconnect(parent_hub, port1);
4942 return -ENODEV;
4943}
79efa097
AS
4944
4945/**
742120c6 4946 * usb_reset_device - warn interface drivers and perform a USB port reset
79efa097 4947 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
79efa097
AS
4948 *
4949 * Warns all drivers bound to registered interfaces (using their pre_reset
4950 * method), performs the port reset, and then lets the drivers know that
4951 * the reset is over (using their post_reset method).
4952 *
742120c6 4953 * Return value is the same as for usb_reset_and_verify_device().
79efa097
AS
4954 *
4955 * The caller must own the device lock. For example, it's safe to use
4956 * this from a driver probe() routine after downloading new firmware.
4957 * For calls that might not occur during probe(), drivers should lock
4958 * the device using usb_lock_device_for_reset().
78d9a487
AS
4959 *
4960 * If an interface is currently being probed or disconnected, we assume
4961 * its driver knows how to handle resets. For all other interfaces,
4962 * if the driver doesn't have pre_reset and post_reset methods then
4963 * we attempt to unbind it and rebind afterward.
79efa097 4964 */
742120c6 4965int usb_reset_device(struct usb_device *udev)
79efa097
AS
4966{
4967 int ret;
852c4b43 4968 int i;
79efa097
AS
4969 struct usb_host_config *config = udev->actconfig;
4970
4971 if (udev->state == USB_STATE_NOTATTACHED ||
4972 udev->state == USB_STATE_SUSPENDED) {
4973 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4974 udev->state);
4975 return -EINVAL;
4976 }
4977
645daaab 4978 /* Prevent autosuspend during the reset */
94fcda1f 4979 usb_autoresume_device(udev);
645daaab 4980
79efa097 4981 if (config) {
79efa097 4982 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
852c4b43
AS
4983 struct usb_interface *cintf = config->interface[i];
4984 struct usb_driver *drv;
78d9a487 4985 int unbind = 0;
852c4b43
AS
4986
4987 if (cintf->dev.driver) {
79efa097 4988 drv = to_usb_driver(cintf->dev.driver);
78d9a487
AS
4989 if (drv->pre_reset && drv->post_reset)
4990 unbind = (drv->pre_reset)(cintf);
4991 else if (cintf->condition ==
4992 USB_INTERFACE_BOUND)
4993 unbind = 1;
4994 if (unbind)
4995 usb_forced_unbind_intf(cintf);
79efa097
AS
4996 }
4997 }
4998 }
4999
742120c6 5000 ret = usb_reset_and_verify_device(udev);
79efa097
AS
5001
5002 if (config) {
79efa097 5003 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
852c4b43
AS
5004 struct usb_interface *cintf = config->interface[i];
5005 struct usb_driver *drv;
78d9a487 5006 int rebind = cintf->needs_binding;
852c4b43 5007
78d9a487 5008 if (!rebind && cintf->dev.driver) {
79efa097
AS
5009 drv = to_usb_driver(cintf->dev.driver);
5010 if (drv->post_reset)
78d9a487
AS
5011 rebind = (drv->post_reset)(cintf);
5012 else if (cintf->condition ==
5013 USB_INTERFACE_BOUND)
5014 rebind = 1;
79efa097 5015 }
6c640945 5016 if (ret == 0 && rebind)
78d9a487 5017 usb_rebind_intf(cintf);
79efa097
AS
5018 }
5019 }
5020
94fcda1f 5021 usb_autosuspend_device(udev);
79efa097
AS
5022 return ret;
5023}
742120c6 5024EXPORT_SYMBOL_GPL(usb_reset_device);
dc023dce
IPG
5025
5026
5027/**
5028 * usb_queue_reset_device - Reset a USB device from an atomic context
5029 * @iface: USB interface belonging to the device to reset
5030 *
5031 * This function can be used to reset a USB device from an atomic
5032 * context, where usb_reset_device() won't work (as it blocks).
5033 *
5034 * Doing a reset via this method is functionally equivalent to calling
5035 * usb_reset_device(), except for the fact that it is delayed to a
5036 * workqueue. This means that any drivers bound to other interfaces
5037 * might be unbound, as well as users from usbfs in user space.
5038 *
5039 * Corner cases:
5040 *
5041 * - Scheduling two resets at the same time from two different drivers
5042 * attached to two different interfaces of the same device is
5043 * possible; depending on how the driver attached to each interface
5044 * handles ->pre_reset(), the second reset might happen or not.
5045 *
5046 * - If a driver is unbound and it had a pending reset, the reset will
5047 * be cancelled.
5048 *
5049 * - This function can be called during .probe() or .disconnect()
5050 * times. On return from .disconnect(), any pending resets will be
5051 * cancelled.
5052 *
5053 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5054 * does its own.
5055 *
5056 * NOTE: We don't do any reference count tracking because it is not
5057 * needed. The lifecycle of the work_struct is tied to the
5058 * usb_interface. Before destroying the interface we cancel the
5059 * work_struct, so the fact that work_struct is queued and or
5060 * running means the interface (and thus, the device) exist and
5061 * are referenced.
5062 */
5063void usb_queue_reset_device(struct usb_interface *iface)
5064{
5065 schedule_work(&iface->reset_ws);
5066}
5067EXPORT_SYMBOL_GPL(usb_queue_reset_device);
ff823c79
LT
5068
5069/**
5070 * usb_hub_find_child - Get the pointer of child device
5071 * attached to the port which is specified by @port1.
5072 * @hdev: USB device belonging to the usb hub
5073 * @port1: port num to indicate which port the child device
5074 * is attached to.
5075 *
5076 * USB drivers call this function to get hub's child device
5077 * pointer.
5078 *
5079 * Return NULL if input param is invalid and
5080 * child's usb_device pointer if non-NULL.
5081 */
5082struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5083 int port1)
5084{
5085 struct usb_hub *hub = hdev_to_hub(hdev);
5086
5087 if (port1 < 1 || port1 > hdev->maxchild)
5088 return NULL;
5089 return hub->ports[port1 - 1]->child;
5090}
5091EXPORT_SYMBOL_GPL(usb_hub_find_child);
d5575424 5092
05f91689
LT
5093/**
5094 * usb_set_hub_port_connect_type - set hub port connect type.
5095 * @hdev: USB device belonging to the usb hub
5096 * @port1: port num of the port
5097 * @type: connect type of the port
5098 */
5099void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
5100 enum usb_port_connect_type type)
5101{
5102 struct usb_hub *hub = hdev_to_hub(hdev);
5103
5104 hub->ports[port1 - 1]->connect_type = type;
5105}
5106
5107/**
5108 * usb_get_hub_port_connect_type - Get the port's connect type
5109 * @hdev: USB device belonging to the usb hub
5110 * @port1: port num of the port
5111 *
5112 * Return connect type of the port and if input params are
5113 * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
5114 */
5115enum usb_port_connect_type
5116usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
5117{
5118 struct usb_hub *hub = hdev_to_hub(hdev);
5119
5120 return hub->ports[port1 - 1]->connect_type;
5121}
5122
d5575424
LT
5123#ifdef CONFIG_ACPI
5124/**
5125 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5126 * @hdev: USB device belonging to the usb hub
5127 * @port1: port num of the port
5128 *
5129 * Return port's acpi handle if successful, NULL if params are
5130 * invaild.
5131 */
5132acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5133 int port1)
5134{
5135 struct usb_hub *hub = hdev_to_hub(hdev);
5136
5137 return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
5138}
5139#endif