]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/usb/core/hub.c
USB: add devpath sysfs attribute
[mirror_ubuntu-bionic-kernel.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>
9c8d6178 22#include <linux/kthread.h>
4186ecf8 23#include <linux/mutex.h>
7dfb7103 24#include <linux/freezer.h>
1da177e4 25
1da177e4
LT
26#include <asm/uaccess.h>
27#include <asm/byteorder.h>
28
29#include "usb.h"
30#include "hcd.h"
31#include "hub.h"
32
f2a383e4
GKH
33/* if we are in debug mode, always announce new devices */
34#ifdef DEBUG
35#ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
36#define CONFIG_USB_ANNOUNCE_NEW_DEVICES
37#endif
38#endif
39
1bb5f66b
AS
40struct usb_hub {
41 struct device *intfdev; /* the "interface" device */
42 struct usb_device *hdev;
e8054854 43 struct kref kref;
1bb5f66b
AS
44 struct urb *urb; /* for interrupt polling pipe */
45
46 /* buffer for urb ... with extra space in case of babble */
47 char (*buffer)[8];
1bb5f66b
AS
48 union {
49 struct usb_hub_status hub;
50 struct usb_port_status port;
51 } *status; /* buffer for status reports */
db90e7a1 52 struct mutex status_mutex; /* for the status buffer */
1bb5f66b
AS
53
54 int error; /* last reported error */
55 int nerrors; /* track consecutive errors */
56
57 struct list_head event_list; /* hubs w/data or errs ready */
58 unsigned long event_bits[1]; /* status change bitmask */
59 unsigned long change_bits[1]; /* ports with logical connect
60 status change */
61 unsigned long busy_bits[1]; /* ports being reset or
62 resumed */
253e0572
AS
63 unsigned long removed_bits[1]; /* ports with a "removed"
64 device present */
1bb5f66b
AS
65#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
66#error event_bits[] is too short!
67#endif
68
69 struct usb_hub_descriptor *descriptor; /* class descriptor */
70 struct usb_tt tt; /* Transaction Translator */
71
72 unsigned mA_per_port; /* current for each child */
73
74 unsigned limited_power:1;
75 unsigned quiescing:1;
e8054854 76 unsigned disconnected:1;
1bb5f66b
AS
77
78 unsigned has_indicators:1;
79 u8 indicator[USB_MAXCHILDREN];
6d5aefb8 80 struct delayed_work leds;
8520f380 81 struct delayed_work init_work;
7cbe5dca 82 void **port_owners;
1bb5f66b
AS
83};
84
85
1da177e4 86/* Protect struct usb_device->state and ->children members
9ad3d6cc 87 * Note: Both are also protected by ->dev.sem, except that ->state can
1da177e4
LT
88 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
89static DEFINE_SPINLOCK(device_state_lock);
90
91/* khubd's worklist and its lock */
92static DEFINE_SPINLOCK(hub_event_lock);
93static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
94
95/* Wakes up khubd */
96static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
97
9c8d6178 98static struct task_struct *khubd_task;
1da177e4
LT
99
100/* cycle leds on hubs that aren't blinking for attention */
101static int blinkenlights = 0;
102module_param (blinkenlights, bool, S_IRUGO);
103MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
104
fd7c519d
JK
105/*
106 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
107 * 10 seconds to send reply for the initial 64-byte descriptor request.
108 */
109/* define initial 64-byte descriptor request timeout in milliseconds */
110static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
111module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
b9cef6c3
WF
112MODULE_PARM_DESC(initial_descriptor_timeout,
113 "initial 64-byte descriptor request timeout in milliseconds "
114 "(default 5000 - 5.0 seconds)");
fd7c519d 115
1da177e4
LT
116/*
117 * As of 2.6.10 we introduce a new USB device initialization scheme which
118 * closely resembles the way Windows works. Hopefully it will be compatible
119 * with a wider range of devices than the old scheme. However some previously
120 * working devices may start giving rise to "device not accepting address"
121 * errors; if that happens the user can try the old scheme by adjusting the
122 * following module parameters.
123 *
124 * For maximum flexibility there are two boolean parameters to control the
125 * hub driver's behavior. On the first initialization attempt, if the
126 * "old_scheme_first" parameter is set then the old scheme will be used,
127 * otherwise the new scheme is used. If that fails and "use_both_schemes"
128 * is set, then the driver will make another attempt, using the other scheme.
129 */
130static int old_scheme_first = 0;
131module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
132MODULE_PARM_DESC(old_scheme_first,
133 "start with the old device initialization scheme");
134
135static int use_both_schemes = 1;
136module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
137MODULE_PARM_DESC(use_both_schemes,
138 "try the other device initialization scheme if the "
139 "first one fails");
140
32fe0198
AS
141/* Mutual exclusion for EHCI CF initialization. This interferes with
142 * port reset on some companion controllers.
143 */
144DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
145EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
146
948fea37
AS
147#define HUB_DEBOUNCE_TIMEOUT 1500
148#define HUB_DEBOUNCE_STEP 25
149#define HUB_DEBOUNCE_STABLE 100
150
1da177e4 151
742120c6
ML
152static int usb_reset_and_verify_device(struct usb_device *udev);
153
404d5b18 154static inline char *portspeed(int portstatus)
1da177e4
LT
155{
156 if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
157 return "480 Mb/s";
158 else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
159 return "1.5 Mb/s";
d2e9b4d6
SS
160 else if (portstatus & (1 << USB_PORT_FEAT_SUPERSPEED))
161 return "5.0 Gb/s";
1da177e4
LT
162 else
163 return "12 Mb/s";
164}
1da177e4
LT
165
166/* Note that hdev or one of its children must be locked! */
25118084 167static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
1da177e4 168{
25118084
AS
169 if (!hdev || !hdev->actconfig)
170 return NULL;
1da177e4
LT
171 return usb_get_intfdata(hdev->actconfig->interface[0]);
172}
173
174/* USB 2.0 spec Section 11.24.4.5 */
175static int get_hub_descriptor(struct usb_device *hdev, void *data, int size)
176{
177 int i, ret;
178
179 for (i = 0; i < 3; i++) {
180 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
181 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
182 USB_DT_HUB << 8, 0, data, size,
183 USB_CTRL_GET_TIMEOUT);
184 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
185 return ret;
186 }
187 return -EINVAL;
188}
189
190/*
191 * USB 2.0 spec Section 11.24.2.1
192 */
193static int clear_hub_feature(struct usb_device *hdev, int feature)
194{
195 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
196 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
197}
198
199/*
200 * USB 2.0 spec Section 11.24.2.2
201 */
202static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
203{
204 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
205 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
206 NULL, 0, 1000);
207}
208
209/*
210 * USB 2.0 spec Section 11.24.2.13
211 */
212static int set_port_feature(struct usb_device *hdev, int port1, int feature)
213{
214 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
215 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
216 NULL, 0, 1000);
217}
218
219/*
220 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
221 * for info about using port indicators
222 */
223static void set_port_led(
224 struct usb_hub *hub,
225 int port1,
226 int selector
227)
228{
229 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
230 USB_PORT_FEAT_INDICATOR);
231 if (status < 0)
232 dev_dbg (hub->intfdev,
233 "port %d indicator %s status %d\n",
234 port1,
235 ({ char *s; switch (selector) {
236 case HUB_LED_AMBER: s = "amber"; break;
237 case HUB_LED_GREEN: s = "green"; break;
238 case HUB_LED_OFF: s = "off"; break;
239 case HUB_LED_AUTO: s = "auto"; break;
240 default: s = "??"; break;
241 }; s; }),
242 status);
243}
244
245#define LED_CYCLE_PERIOD ((2*HZ)/3)
246
c4028958 247static void led_work (struct work_struct *work)
1da177e4 248{
c4028958
DH
249 struct usb_hub *hub =
250 container_of(work, struct usb_hub, leds.work);
1da177e4
LT
251 struct usb_device *hdev = hub->hdev;
252 unsigned i;
253 unsigned changed = 0;
254 int cursor = -1;
255
256 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
257 return;
258
259 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
260 unsigned selector, mode;
261
262 /* 30%-50% duty cycle */
263
264 switch (hub->indicator[i]) {
265 /* cycle marker */
266 case INDICATOR_CYCLE:
267 cursor = i;
268 selector = HUB_LED_AUTO;
269 mode = INDICATOR_AUTO;
270 break;
271 /* blinking green = sw attention */
272 case INDICATOR_GREEN_BLINK:
273 selector = HUB_LED_GREEN;
274 mode = INDICATOR_GREEN_BLINK_OFF;
275 break;
276 case INDICATOR_GREEN_BLINK_OFF:
277 selector = HUB_LED_OFF;
278 mode = INDICATOR_GREEN_BLINK;
279 break;
280 /* blinking amber = hw attention */
281 case INDICATOR_AMBER_BLINK:
282 selector = HUB_LED_AMBER;
283 mode = INDICATOR_AMBER_BLINK_OFF;
284 break;
285 case INDICATOR_AMBER_BLINK_OFF:
286 selector = HUB_LED_OFF;
287 mode = INDICATOR_AMBER_BLINK;
288 break;
289 /* blink green/amber = reserved */
290 case INDICATOR_ALT_BLINK:
291 selector = HUB_LED_GREEN;
292 mode = INDICATOR_ALT_BLINK_OFF;
293 break;
294 case INDICATOR_ALT_BLINK_OFF:
295 selector = HUB_LED_AMBER;
296 mode = INDICATOR_ALT_BLINK;
297 break;
298 default:
299 continue;
300 }
301 if (selector != HUB_LED_AUTO)
302 changed = 1;
303 set_port_led(hub, i + 1, selector);
304 hub->indicator[i] = mode;
305 }
306 if (!changed && blinkenlights) {
307 cursor++;
308 cursor %= hub->descriptor->bNbrPorts;
309 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
310 hub->indicator[cursor] = INDICATOR_CYCLE;
311 changed++;
312 }
313 if (changed)
314 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
315}
316
317/* use a short timeout for hub/port status fetches */
318#define USB_STS_TIMEOUT 1000
319#define USB_STS_RETRIES 5
320
321/*
322 * USB 2.0 spec Section 11.24.2.6
323 */
324static int get_hub_status(struct usb_device *hdev,
325 struct usb_hub_status *data)
326{
327 int i, status = -ETIMEDOUT;
328
329 for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
330 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
331 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
332 data, sizeof(*data), USB_STS_TIMEOUT);
333 }
334 return status;
335}
336
337/*
338 * USB 2.0 spec Section 11.24.2.7
339 */
340static int get_port_status(struct usb_device *hdev, int port1,
341 struct usb_port_status *data)
342{
343 int i, status = -ETIMEDOUT;
344
345 for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
346 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
347 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
348 data, sizeof(*data), USB_STS_TIMEOUT);
349 }
350 return status;
351}
352
3eb14915
AS
353static int hub_port_status(struct usb_hub *hub, int port1,
354 u16 *status, u16 *change)
355{
356 int ret;
357
358 mutex_lock(&hub->status_mutex);
359 ret = get_port_status(hub->hdev, port1, &hub->status->port);
360 if (ret < 4) {
361 dev_err(hub->intfdev,
362 "%s failed (err = %d)\n", __func__, ret);
363 if (ret >= 0)
364 ret = -EIO;
365 } else {
366 *status = le16_to_cpu(hub->status->port.wPortStatus);
367 *change = le16_to_cpu(hub->status->port.wPortChange);
368 ret = 0;
369 }
370 mutex_unlock(&hub->status_mutex);
371 return ret;
372}
373
1da177e4
LT
374static void kick_khubd(struct usb_hub *hub)
375{
376 unsigned long flags;
377
40f122f3 378 /* Suppress autosuspend until khubd runs */
ccf5b801 379 atomic_set(&to_usb_interface(hub->intfdev)->pm_usage_cnt, 1);
40f122f3 380
1da177e4 381 spin_lock_irqsave(&hub_event_lock, flags);
2e2c5eea 382 if (!hub->disconnected && list_empty(&hub->event_list)) {
1da177e4
LT
383 list_add_tail(&hub->event_list, &hub_event_list);
384 wake_up(&khubd_wait);
385 }
386 spin_unlock_irqrestore(&hub_event_lock, flags);
387}
388
389void usb_kick_khubd(struct usb_device *hdev)
390{
25118084
AS
391 struct usb_hub *hub = hdev_to_hub(hdev);
392
393 if (hub)
394 kick_khubd(hub);
1da177e4
LT
395}
396
397
398/* completion function, fires on port status changes and various faults */
7d12e780 399static void hub_irq(struct urb *urb)
1da177e4 400{
ec17cf1c 401 struct usb_hub *hub = urb->context;
e015268d 402 int status = urb->status;
71d2718f 403 unsigned i;
1da177e4
LT
404 unsigned long bits;
405
e015268d 406 switch (status) {
1da177e4
LT
407 case -ENOENT: /* synchronous unlink */
408 case -ECONNRESET: /* async unlink */
409 case -ESHUTDOWN: /* hardware going away */
410 return;
411
412 default: /* presumably an error */
413 /* Cause a hub reset after 10 consecutive errors */
e015268d 414 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
1da177e4
LT
415 if ((++hub->nerrors < 10) || hub->error)
416 goto resubmit;
e015268d 417 hub->error = status;
1da177e4 418 /* FALL THROUGH */
ec17cf1c 419
1da177e4
LT
420 /* let khubd handle things */
421 case 0: /* we got data: port status changed */
422 bits = 0;
423 for (i = 0; i < urb->actual_length; ++i)
424 bits |= ((unsigned long) ((*hub->buffer)[i]))
425 << (i*8);
426 hub->event_bits[0] = bits;
427 break;
428 }
429
430 hub->nerrors = 0;
431
432 /* Something happened, let khubd figure it out */
433 kick_khubd(hub);
434
435resubmit:
436 if (hub->quiescing)
437 return;
438
439 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
440 && status != -ENODEV && status != -EPERM)
441 dev_err (hub->intfdev, "resubmit --> %d\n", status);
442}
443
444/* USB 2.0 spec Section 11.24.2.3 */
445static inline int
446hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
447{
c2f6595f 448 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1da177e4
LT
449 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
450 tt, NULL, 0, 1000);
451}
452
453/*
454 * enumeration blocks khubd for a long time. we use keventd instead, since
455 * long blocking there is the exception, not the rule. accordingly, HCDs
456 * talking to TTs must queue control transfers (not just bulk and iso), so
457 * both can talk to the same hub concurrently.
458 */
cb88a1b8 459static void hub_tt_work(struct work_struct *work)
1da177e4 460{
c4028958 461 struct usb_hub *hub =
cb88a1b8 462 container_of(work, struct usb_hub, tt.clear_work);
1da177e4 463 unsigned long flags;
55e5fdfa 464 int limit = 100;
1da177e4
LT
465
466 spin_lock_irqsave (&hub->tt.lock, flags);
55e5fdfa 467 while (--limit && !list_empty (&hub->tt.clear_list)) {
d0f830d3 468 struct list_head *next;
1da177e4
LT
469 struct usb_tt_clear *clear;
470 struct usb_device *hdev = hub->hdev;
cb88a1b8 471 const struct hc_driver *drv;
1da177e4
LT
472 int status;
473
d0f830d3
HS
474 next = hub->tt.clear_list.next;
475 clear = list_entry (next, struct usb_tt_clear, clear_list);
1da177e4
LT
476 list_del (&clear->clear_list);
477
478 /* drop lock so HCD can concurrently report other TT errors */
479 spin_unlock_irqrestore (&hub->tt.lock, flags);
480 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
1da177e4
LT
481 if (status)
482 dev_err (&hdev->dev,
483 "clear tt %d (%04x) error %d\n",
484 clear->tt, clear->devinfo, status);
cb88a1b8
AS
485
486 /* Tell the HCD, even if the operation failed */
487 drv = clear->hcd->driver;
488 if (drv->clear_tt_buffer_complete)
489 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
490
1bc3c9e1 491 kfree(clear);
cb88a1b8 492 spin_lock_irqsave(&hub->tt.lock, flags);
1da177e4
LT
493 }
494 spin_unlock_irqrestore (&hub->tt.lock, flags);
495}
496
497/**
cb88a1b8
AS
498 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
499 * @urb: an URB associated with the failed or incomplete split transaction
1da177e4
LT
500 *
501 * High speed HCDs use this to tell the hub driver that some split control or
502 * bulk transaction failed in a way that requires clearing internal state of
503 * a transaction translator. This is normally detected (and reported) from
504 * interrupt context.
505 *
506 * It may not be possible for that hub to handle additional full (or low)
507 * speed transactions until that state is fully cleared out.
508 */
cb88a1b8 509int usb_hub_clear_tt_buffer(struct urb *urb)
1da177e4 510{
cb88a1b8
AS
511 struct usb_device *udev = urb->dev;
512 int pipe = urb->pipe;
1da177e4
LT
513 struct usb_tt *tt = udev->tt;
514 unsigned long flags;
515 struct usb_tt_clear *clear;
516
517 /* we've got to cope with an arbitrary number of pending TT clears,
518 * since each TT has "at least two" buffers that can need it (and
519 * there can be many TTs per hub). even if they're uncommon.
520 */
54e6ecb2 521 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
1da177e4
LT
522 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
523 /* FIXME recover somehow ... RESET_TT? */
cb88a1b8 524 return -ENOMEM;
1da177e4
LT
525 }
526
527 /* info that CLEAR_TT_BUFFER needs */
528 clear->tt = tt->multi ? udev->ttport : 1;
529 clear->devinfo = usb_pipeendpoint (pipe);
530 clear->devinfo |= udev->devnum << 4;
531 clear->devinfo |= usb_pipecontrol (pipe)
532 ? (USB_ENDPOINT_XFER_CONTROL << 11)
533 : (USB_ENDPOINT_XFER_BULK << 11);
534 if (usb_pipein (pipe))
535 clear->devinfo |= 1 << 15;
cb88a1b8
AS
536
537 /* info for completion callback */
538 clear->hcd = bus_to_hcd(udev->bus);
539 clear->ep = urb->ep;
540
1da177e4
LT
541 /* tell keventd to clear state for this TT */
542 spin_lock_irqsave (&tt->lock, flags);
543 list_add_tail (&clear->clear_list, &tt->clear_list);
cb88a1b8 544 schedule_work(&tt->clear_work);
1da177e4 545 spin_unlock_irqrestore (&tt->lock, flags);
cb88a1b8 546 return 0;
1da177e4 547}
cb88a1b8 548EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
1da177e4 549
8520f380
AS
550/* If do_delay is false, return the number of milliseconds the caller
551 * needs to delay.
552 */
553static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
1da177e4
LT
554{
555 int port1;
b789696a 556 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
8520f380 557 unsigned delay;
4489a571
AS
558 u16 wHubCharacteristics =
559 le16_to_cpu(hub->descriptor->wHubCharacteristics);
560
561 /* Enable power on each port. Some hubs have reserved values
562 * of LPSM (> 2) in their descriptors, even though they are
563 * USB 2.0 hubs. Some hubs do not implement port-power switching
564 * but only emulate it. In all cases, the ports won't work
565 * unless we send these messages to the hub.
566 */
567 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
1da177e4 568 dev_dbg(hub->intfdev, "enabling power on all ports\n");
4489a571
AS
569 else
570 dev_dbg(hub->intfdev, "trying to enable port power on "
571 "non-switchable hub\n");
572 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
573 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
1da177e4 574
b789696a 575 /* Wait at least 100 msec for power to become stable */
8520f380
AS
576 delay = max(pgood_delay, (unsigned) 100);
577 if (do_delay)
578 msleep(delay);
579 return delay;
1da177e4
LT
580}
581
1da177e4
LT
582static int hub_hub_status(struct usb_hub *hub,
583 u16 *status, u16 *change)
584{
585 int ret;
586
db90e7a1 587 mutex_lock(&hub->status_mutex);
1da177e4
LT
588 ret = get_hub_status(hub->hdev, &hub->status->hub);
589 if (ret < 0)
590 dev_err (hub->intfdev,
441b62c1 591 "%s failed (err = %d)\n", __func__, ret);
1da177e4
LT
592 else {
593 *status = le16_to_cpu(hub->status->hub.wHubStatus);
594 *change = le16_to_cpu(hub->status->hub.wHubChange);
595 ret = 0;
596 }
db90e7a1 597 mutex_unlock(&hub->status_mutex);
1da177e4
LT
598 return ret;
599}
600
8b28c752
AS
601static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
602{
603 struct usb_device *hdev = hub->hdev;
0458d5b4 604 int ret = 0;
8b28c752 605
0458d5b4 606 if (hdev->children[port1-1] && set_state)
8b28c752
AS
607 usb_set_device_state(hdev->children[port1-1],
608 USB_STATE_NOTATTACHED);
0458d5b4
AS
609 if (!hub->error)
610 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
8b28c752
AS
611 if (ret)
612 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
0458d5b4 613 port1, ret);
8b28c752
AS
614 return ret;
615}
616
0458d5b4
AS
617/*
618 * Disable a port and mark a logical connnect-change event, so that some
619 * time later khubd will disconnect() any existing usb_device on the port
620 * and will re-enumerate if there actually is a device attached.
621 */
622static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
623{
624 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
625 hub_port_disable(hub, port1, 1);
7d069b7d 626
0458d5b4
AS
627 /* FIXME let caller ask to power down the port:
628 * - some devices won't enumerate without a VBUS power cycle
629 * - SRP saves power that way
630 * - ... new call, TBD ...
631 * That's easy if this hub can switch power per-port, and
632 * khubd reactivates the port later (timer, SRP, etc).
633 * Powerdown must be optional, because of reset/DFU.
634 */
635
636 set_bit(port1, hub->change_bits);
637 kick_khubd(hub);
638}
639
253e0572
AS
640/**
641 * usb_remove_device - disable a device's port on its parent hub
642 * @udev: device to be disabled and removed
643 * Context: @udev locked, must be able to sleep.
644 *
645 * After @udev's port has been disabled, khubd is notified and it will
646 * see that the device has been disconnected. When the device is
647 * physically unplugged and something is plugged in, the events will
648 * be received and processed normally.
649 */
650int usb_remove_device(struct usb_device *udev)
651{
652 struct usb_hub *hub;
653 struct usb_interface *intf;
654
655 if (!udev->parent) /* Can't remove a root hub */
656 return -EINVAL;
657 hub = hdev_to_hub(udev->parent);
658 intf = to_usb_interface(hub->intfdev);
659
660 usb_autopm_get_interface(intf);
661 set_bit(udev->portnum, hub->removed_bits);
662 hub_port_logical_disconnect(hub, udev->portnum);
663 usb_autopm_put_interface(intf);
664 return 0;
665}
666
6ee0b270 667enum hub_activation_type {
8520f380
AS
668 HUB_INIT, HUB_INIT2, HUB_INIT3,
669 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
6ee0b270 670};
5e6effae 671
8520f380
AS
672static void hub_init_func2(struct work_struct *ws);
673static void hub_init_func3(struct work_struct *ws);
674
f2835219 675static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
5e6effae
AS
676{
677 struct usb_device *hdev = hub->hdev;
678 int port1;
f2835219 679 int status;
948fea37 680 bool need_debounce_delay = false;
8520f380
AS
681 unsigned delay;
682
683 /* Continue a partial initialization */
684 if (type == HUB_INIT2)
685 goto init2;
686 if (type == HUB_INIT3)
687 goto init3;
5e6effae 688
f2835219
AS
689 /* After a resume, port power should still be on.
690 * For any other type of activation, turn it on.
691 */
8520f380
AS
692 if (type != HUB_RESUME) {
693
694 /* Speed up system boot by using a delayed_work for the
695 * hub's initial power-up delays. This is pretty awkward
696 * and the implementation looks like a home-brewed sort of
697 * setjmp/longjmp, but it saves at least 100 ms for each
698 * root hub (assuming usbcore is compiled into the kernel
699 * rather than as a module). It adds up.
700 *
701 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
702 * because for those activation types the ports have to be
703 * operational when we return. In theory this could be done
704 * for HUB_POST_RESET, but it's easier not to.
705 */
706 if (type == HUB_INIT) {
707 delay = hub_power_on(hub, false);
708 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
709 schedule_delayed_work(&hub->init_work,
710 msecs_to_jiffies(delay));
61fbeba1
AS
711
712 /* Suppress autosuspend until init is done */
ccf5b801
AS
713 atomic_set(&to_usb_interface(hub->intfdev)->
714 pm_usage_cnt, 1);
8520f380
AS
715 return; /* Continues at init2: below */
716 } else {
717 hub_power_on(hub, true);
718 }
719 }
720 init2:
f2835219 721
6ee0b270
AS
722 /* Check each port and set hub->change_bits to let khubd know
723 * which ports need attention.
5e6effae
AS
724 */
725 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
726 struct usb_device *udev = hdev->children[port1-1];
5e6effae
AS
727 u16 portstatus, portchange;
728
6ee0b270
AS
729 portstatus = portchange = 0;
730 status = hub_port_status(hub, port1, &portstatus, &portchange);
731 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
732 dev_dbg(hub->intfdev,
733 "port %d: status %04x change %04x\n",
734 port1, portstatus, portchange);
735
736 /* After anything other than HUB_RESUME (i.e., initialization
737 * or any sort of reset), every port should be disabled.
738 * Unconnected ports should likewise be disabled (paranoia),
739 * and so should ports for which we have no usb_device.
740 */
741 if ((portstatus & USB_PORT_STAT_ENABLE) && (
742 type != HUB_RESUME ||
743 !(portstatus & USB_PORT_STAT_CONNECTION) ||
744 !udev ||
745 udev->state == USB_STATE_NOTATTACHED)) {
746 clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
747 portstatus &= ~USB_PORT_STAT_ENABLE;
5e6effae
AS
748 }
749
948fea37
AS
750 /* Clear status-change flags; we'll debounce later */
751 if (portchange & USB_PORT_STAT_C_CONNECTION) {
752 need_debounce_delay = true;
753 clear_port_feature(hub->hdev, port1,
754 USB_PORT_FEAT_C_CONNECTION);
755 }
756 if (portchange & USB_PORT_STAT_C_ENABLE) {
757 need_debounce_delay = true;
758 clear_port_feature(hub->hdev, port1,
759 USB_PORT_FEAT_C_ENABLE);
760 }
761
253e0572
AS
762 /* We can forget about a "removed" device when there's a
763 * physical disconnect or the connect status changes.
764 */
765 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
766 (portchange & USB_PORT_STAT_C_CONNECTION))
767 clear_bit(port1, hub->removed_bits);
768
6ee0b270
AS
769 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
770 /* Tell khubd to disconnect the device or
771 * check for a new connection
772 */
773 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
774 set_bit(port1, hub->change_bits);
775
776 } else if (portstatus & USB_PORT_STAT_ENABLE) {
777 /* The power session apparently survived the resume.
778 * If there was an overcurrent or suspend change
779 * (i.e., remote wakeup request), have khubd
780 * take care of it.
781 */
782 if (portchange)
783 set_bit(port1, hub->change_bits);
5e6effae 784
6ee0b270 785 } else if (udev->persist_enabled) {
6ee0b270 786#ifdef CONFIG_PM
5e6effae 787 udev->reset_resume = 1;
6ee0b270 788#endif
8808f00c
AS
789 set_bit(port1, hub->change_bits);
790
6ee0b270
AS
791 } else {
792 /* The power session is gone; tell khubd */
793 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
794 set_bit(port1, hub->change_bits);
5e6effae 795 }
5e6effae
AS
796 }
797
948fea37
AS
798 /* If no port-status-change flags were set, we don't need any
799 * debouncing. If flags were set we can try to debounce the
800 * ports all at once right now, instead of letting khubd do them
801 * one at a time later on.
802 *
803 * If any port-status changes do occur during this delay, khubd
804 * will see them later and handle them normally.
805 */
8520f380
AS
806 if (need_debounce_delay) {
807 delay = HUB_DEBOUNCE_STABLE;
808
809 /* Don't do a long sleep inside a workqueue routine */
810 if (type == HUB_INIT2) {
811 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
812 schedule_delayed_work(&hub->init_work,
813 msecs_to_jiffies(delay));
814 return; /* Continues at init3: below */
815 } else {
816 msleep(delay);
817 }
818 }
819 init3:
f2835219
AS
820 hub->quiescing = 0;
821
822 status = usb_submit_urb(hub->urb, GFP_NOIO);
823 if (status < 0)
824 dev_err(hub->intfdev, "activate --> %d\n", status);
825 if (hub->has_indicators && blinkenlights)
826 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
827
828 /* Scan all ports that need attention */
829 kick_khubd(hub);
5e6effae
AS
830}
831
8520f380
AS
832/* Implement the continuations for the delays above */
833static void hub_init_func2(struct work_struct *ws)
834{
835 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
836
837 hub_activate(hub, HUB_INIT2);
838}
839
840static void hub_init_func3(struct work_struct *ws)
841{
842 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
843
844 hub_activate(hub, HUB_INIT3);
845}
846
4330354f
AS
847enum hub_quiescing_type {
848 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
849};
850
851static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
852{
853 struct usb_device *hdev = hub->hdev;
854 int i;
855
8520f380
AS
856 cancel_delayed_work_sync(&hub->init_work);
857
4330354f
AS
858 /* khubd and related activity won't re-trigger */
859 hub->quiescing = 1;
860
861 if (type != HUB_SUSPEND) {
862 /* Disconnect all the children */
863 for (i = 0; i < hdev->maxchild; ++i) {
864 if (hdev->children[i])
865 usb_disconnect(&hdev->children[i]);
866 }
867 }
868
869 /* Stop khubd and related activity */
870 usb_kill_urb(hub->urb);
871 if (hub->has_indicators)
872 cancel_delayed_work_sync(&hub->leds);
873 if (hub->tt.hub)
cb88a1b8 874 cancel_work_sync(&hub->tt.clear_work);
4330354f
AS
875}
876
3eb14915
AS
877/* caller has locked the hub device */
878static int hub_pre_reset(struct usb_interface *intf)
879{
880 struct usb_hub *hub = usb_get_intfdata(intf);
881
4330354f 882 hub_quiesce(hub, HUB_PRE_RESET);
f07600cf 883 return 0;
7d069b7d
AS
884}
885
886/* caller has locked the hub device */
f07600cf 887static int hub_post_reset(struct usb_interface *intf)
7d069b7d 888{
7de18d8b
AS
889 struct usb_hub *hub = usb_get_intfdata(intf);
890
f2835219 891 hub_activate(hub, HUB_POST_RESET);
f07600cf 892 return 0;
7d069b7d
AS
893}
894
1da177e4
LT
895static int hub_configure(struct usb_hub *hub,
896 struct usb_endpoint_descriptor *endpoint)
897{
b356b7c7 898 struct usb_hcd *hcd;
1da177e4
LT
899 struct usb_device *hdev = hub->hdev;
900 struct device *hub_dev = hub->intfdev;
901 u16 hubstatus, hubchange;
74ad9bd2 902 u16 wHubCharacteristics;
1da177e4
LT
903 unsigned int pipe;
904 int maxp, ret;
7cbe5dca 905 char *message = "out of memory";
1da177e4 906
d697cdda 907 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
1da177e4 908 if (!hub->buffer) {
1da177e4
LT
909 ret = -ENOMEM;
910 goto fail;
911 }
912
913 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
914 if (!hub->status) {
1da177e4
LT
915 ret = -ENOMEM;
916 goto fail;
917 }
db90e7a1 918 mutex_init(&hub->status_mutex);
1da177e4
LT
919
920 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
921 if (!hub->descriptor) {
1da177e4
LT
922 ret = -ENOMEM;
923 goto fail;
924 }
925
926 /* Request the entire hub descriptor.
927 * hub->descriptor can handle USB_MAXCHILDREN ports,
928 * but the hub can/will return fewer bytes here.
929 */
930 ret = get_hub_descriptor(hdev, hub->descriptor,
931 sizeof(*hub->descriptor));
932 if (ret < 0) {
933 message = "can't read hub descriptor";
934 goto fail;
935 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
936 message = "hub has too many ports!";
937 ret = -ENODEV;
938 goto fail;
939 }
940
941 hdev->maxchild = hub->descriptor->bNbrPorts;
942 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
943 (hdev->maxchild == 1) ? "" : "s");
944
7cbe5dca
AS
945 hub->port_owners = kzalloc(hdev->maxchild * sizeof(void *), GFP_KERNEL);
946 if (!hub->port_owners) {
947 ret = -ENOMEM;
948 goto fail;
949 }
950
74ad9bd2 951 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
1da177e4 952
74ad9bd2 953 if (wHubCharacteristics & HUB_CHAR_COMPOUND) {
1da177e4
LT
954 int i;
955 char portstr [USB_MAXCHILDREN + 1];
956
957 for (i = 0; i < hdev->maxchild; i++)
958 portstr[i] = hub->descriptor->DeviceRemovable
959 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
960 ? 'F' : 'R';
961 portstr[hdev->maxchild] = 0;
962 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
963 } else
964 dev_dbg(hub_dev, "standalone hub\n");
965
74ad9bd2 966 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
1da177e4
LT
967 case 0x00:
968 dev_dbg(hub_dev, "ganged power switching\n");
969 break;
970 case 0x01:
971 dev_dbg(hub_dev, "individual port power switching\n");
972 break;
973 case 0x02:
974 case 0x03:
975 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
976 break;
977 }
978
74ad9bd2 979 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
1da177e4
LT
980 case 0x00:
981 dev_dbg(hub_dev, "global over-current protection\n");
982 break;
983 case 0x08:
984 dev_dbg(hub_dev, "individual port over-current protection\n");
985 break;
986 case 0x10:
987 case 0x18:
988 dev_dbg(hub_dev, "no over-current protection\n");
989 break;
990 }
991
992 spin_lock_init (&hub->tt.lock);
993 INIT_LIST_HEAD (&hub->tt.clear_list);
cb88a1b8 994 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
1da177e4
LT
995 switch (hdev->descriptor.bDeviceProtocol) {
996 case 0:
997 break;
998 case 1:
999 dev_dbg(hub_dev, "Single TT\n");
1000 hub->tt.hub = hdev;
1001 break;
1002 case 2:
1003 ret = usb_set_interface(hdev, 0, 1);
1004 if (ret == 0) {
1005 dev_dbg(hub_dev, "TT per port\n");
1006 hub->tt.multi = 1;
1007 } else
1008 dev_err(hub_dev, "Using single TT (err %d)\n",
1009 ret);
1010 hub->tt.hub = hdev;
1011 break;
d2e9b4d6
SS
1012 case 3:
1013 /* USB 3.0 hubs don't have a TT */
1014 break;
1da177e4
LT
1015 default:
1016 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1017 hdev->descriptor.bDeviceProtocol);
1018 break;
1019 }
1020
e09711ae 1021 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
74ad9bd2 1022 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
e09711ae
DB
1023 case HUB_TTTT_8_BITS:
1024 if (hdev->descriptor.bDeviceProtocol != 0) {
1025 hub->tt.think_time = 666;
1026 dev_dbg(hub_dev, "TT requires at most %d "
1027 "FS bit times (%d ns)\n",
1028 8, hub->tt.think_time);
1029 }
1da177e4 1030 break;
e09711ae
DB
1031 case HUB_TTTT_16_BITS:
1032 hub->tt.think_time = 666 * 2;
1033 dev_dbg(hub_dev, "TT requires at most %d "
1034 "FS bit times (%d ns)\n",
1035 16, hub->tt.think_time);
1da177e4 1036 break;
e09711ae
DB
1037 case HUB_TTTT_24_BITS:
1038 hub->tt.think_time = 666 * 3;
1039 dev_dbg(hub_dev, "TT requires at most %d "
1040 "FS bit times (%d ns)\n",
1041 24, hub->tt.think_time);
1da177e4 1042 break;
e09711ae
DB
1043 case HUB_TTTT_32_BITS:
1044 hub->tt.think_time = 666 * 4;
1045 dev_dbg(hub_dev, "TT requires at most %d "
1046 "FS bit times (%d ns)\n",
1047 32, hub->tt.think_time);
1da177e4
LT
1048 break;
1049 }
1050
1051 /* probe() zeroes hub->indicator[] */
74ad9bd2 1052 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
1da177e4
LT
1053 hub->has_indicators = 1;
1054 dev_dbg(hub_dev, "Port indicators are supported\n");
1055 }
1056
1057 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1058 hub->descriptor->bPwrOn2PwrGood * 2);
1059
1060 /* power budgeting mostly matters with bus-powered hubs,
1061 * and battery-powered root hubs (may provide just 8 mA).
1062 */
1063 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
55c52718 1064 if (ret < 2) {
1da177e4
LT
1065 message = "can't get hub status";
1066 goto fail;
1067 }
7d35b929
AS
1068 le16_to_cpus(&hubstatus);
1069 if (hdev == hdev->bus->root_hub) {
55c52718
AS
1070 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1071 hub->mA_per_port = 500;
1072 else {
1073 hub->mA_per_port = hdev->bus_mA;
1074 hub->limited_power = 1;
1075 }
7d35b929 1076 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
1da177e4
LT
1077 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1078 hub->descriptor->bHubContrCurrent);
55c52718
AS
1079 hub->limited_power = 1;
1080 if (hdev->maxchild > 0) {
1081 int remaining = hdev->bus_mA -
1082 hub->descriptor->bHubContrCurrent;
1083
1084 if (remaining < hdev->maxchild * 100)
1085 dev_warn(hub_dev,
1086 "insufficient power available "
1087 "to use all downstream ports\n");
1088 hub->mA_per_port = 100; /* 7.2.1.1 */
1089 }
1090 } else { /* Self-powered external hub */
1091 /* FIXME: What about battery-powered external hubs that
1092 * provide less current per port? */
1093 hub->mA_per_port = 500;
7d35b929 1094 }
55c52718
AS
1095 if (hub->mA_per_port < 500)
1096 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1097 hub->mA_per_port);
1da177e4 1098
b356b7c7
SS
1099 /* Update the HCD's internal representation of this hub before khubd
1100 * starts getting port status changes for devices under the hub.
1101 */
1102 hcd = bus_to_hcd(hdev->bus);
1103 if (hcd->driver->update_hub_device) {
1104 ret = hcd->driver->update_hub_device(hcd, hdev,
1105 &hub->tt, GFP_KERNEL);
1106 if (ret < 0) {
1107 message = "can't update HCD hub info";
1108 goto fail;
1109 }
1110 }
1111
1da177e4
LT
1112 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1113 if (ret < 0) {
1114 message = "can't get hub status";
1115 goto fail;
1116 }
1117
1118 /* local power status reports aren't always correct */
1119 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1120 dev_dbg(hub_dev, "local power source is %s\n",
1121 (hubstatus & HUB_STATUS_LOCAL_POWER)
1122 ? "lost (inactive)" : "good");
1123
74ad9bd2 1124 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
1da177e4
LT
1125 dev_dbg(hub_dev, "%sover-current condition exists\n",
1126 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1127
88fafff9 1128 /* set up the interrupt endpoint
1129 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1130 * bytes as USB2.0[11.12.3] says because some hubs are known
1131 * to send more data (and thus cause overflow). For root hubs,
1132 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1133 * to be big enough for at least USB_MAXCHILDREN ports. */
1da177e4
LT
1134 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1135 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1136
1137 if (maxp > sizeof(*hub->buffer))
1138 maxp = sizeof(*hub->buffer);
1139
1140 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1141 if (!hub->urb) {
1da177e4
LT
1142 ret = -ENOMEM;
1143 goto fail;
1144 }
1145
1146 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1147 hub, endpoint->bInterval);
1da177e4
LT
1148
1149 /* maybe cycle the hub leds */
1150 if (hub->has_indicators && blinkenlights)
1151 hub->indicator [0] = INDICATOR_CYCLE;
1152
f2835219 1153 hub_activate(hub, HUB_INIT);
1da177e4
LT
1154 return 0;
1155
1156fail:
1157 dev_err (hub_dev, "config failed, %s (err %d)\n",
1158 message, ret);
1159 /* hub_disconnect() frees urb and descriptor */
1160 return ret;
1161}
1162
e8054854
AS
1163static void hub_release(struct kref *kref)
1164{
1165 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1166
1167 usb_put_intf(to_usb_interface(hub->intfdev));
1168 kfree(hub);
1169}
1170
1da177e4
LT
1171static unsigned highspeed_hubs;
1172
1173static void hub_disconnect(struct usb_interface *intf)
1174{
1175 struct usb_hub *hub = usb_get_intfdata (intf);
e8054854
AS
1176
1177 /* Take the hub off the event list and don't let it be added again */
1178 spin_lock_irq(&hub_event_lock);
1179 list_del_init(&hub->event_list);
1180 hub->disconnected = 1;
1181 spin_unlock_irq(&hub_event_lock);
1da177e4 1182
7de18d8b
AS
1183 /* Disconnect all children and quiesce the hub */
1184 hub->error = 0;
4330354f 1185 hub_quiesce(hub, HUB_DISCONNECT);
7de18d8b 1186
8b28c752 1187 usb_set_intfdata (intf, NULL);
7cbe5dca 1188 hub->hdev->maxchild = 0;
1da177e4 1189
e8054854 1190 if (hub->hdev->speed == USB_SPEED_HIGH)
1da177e4
LT
1191 highspeed_hubs--;
1192
1da177e4 1193 usb_free_urb(hub->urb);
7cbe5dca 1194 kfree(hub->port_owners);
1bc3c9e1 1195 kfree(hub->descriptor);
1bc3c9e1 1196 kfree(hub->status);
d697cdda 1197 kfree(hub->buffer);
1da177e4 1198
e8054854 1199 kref_put(&hub->kref, hub_release);
1da177e4
LT
1200}
1201
1202static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1203{
1204 struct usb_host_interface *desc;
1205 struct usb_endpoint_descriptor *endpoint;
1206 struct usb_device *hdev;
1207 struct usb_hub *hub;
1208
1209 desc = intf->cur_altsetting;
1210 hdev = interface_to_usbdev(intf);
1211
38f3ad5e 1212 if (hdev->level == MAX_TOPO_LEVEL) {
b9cef6c3
WF
1213 dev_err(&intf->dev,
1214 "Unsupported bus topology: hub nested too deep\n");
38f3ad5e
FB
1215 return -E2BIG;
1216 }
1217
89ccbdc9
DB
1218#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1219 if (hdev->parent) {
1220 dev_warn(&intf->dev, "ignoring external hub\n");
1221 return -ENODEV;
1222 }
1223#endif
1224
1da177e4
LT
1225 /* Some hubs have a subclass of 1, which AFAICT according to the */
1226 /* specs is not defined, but it works */
1227 if ((desc->desc.bInterfaceSubClass != 0) &&
1228 (desc->desc.bInterfaceSubClass != 1)) {
1229descriptor_error:
1230 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1231 return -EIO;
1232 }
1233
1234 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1235 if (desc->desc.bNumEndpoints != 1)
1236 goto descriptor_error;
1237
1238 endpoint = &desc->endpoint[0].desc;
1239
fbf81c29
LFC
1240 /* If it's not an interrupt in endpoint, we'd better punt! */
1241 if (!usb_endpoint_is_int_in(endpoint))
1da177e4
LT
1242 goto descriptor_error;
1243
1244 /* We found a hub */
1245 dev_info (&intf->dev, "USB hub found\n");
1246
0a1ef3b5 1247 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
1da177e4
LT
1248 if (!hub) {
1249 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1250 return -ENOMEM;
1251 }
1252
e8054854 1253 kref_init(&hub->kref);
1da177e4
LT
1254 INIT_LIST_HEAD(&hub->event_list);
1255 hub->intfdev = &intf->dev;
1256 hub->hdev = hdev;
c4028958 1257 INIT_DELAYED_WORK(&hub->leds, led_work);
8520f380 1258 INIT_DELAYED_WORK(&hub->init_work, NULL);
e8054854 1259 usb_get_intf(intf);
1da177e4
LT
1260
1261 usb_set_intfdata (intf, hub);
40f122f3 1262 intf->needs_remote_wakeup = 1;
1da177e4
LT
1263
1264 if (hdev->speed == USB_SPEED_HIGH)
1265 highspeed_hubs++;
1266
1267 if (hub_configure(hub, endpoint) >= 0)
1268 return 0;
1269
1270 hub_disconnect (intf);
1271 return -ENODEV;
1272}
1273
1274static int
1275hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1276{
1277 struct usb_device *hdev = interface_to_usbdev (intf);
1278
1279 /* assert ifno == 0 (part of hub spec) */
1280 switch (code) {
1281 case USBDEVFS_HUB_PORTINFO: {
1282 struct usbdevfs_hub_portinfo *info = user_data;
1283 int i;
1284
1285 spin_lock_irq(&device_state_lock);
1286 if (hdev->devnum <= 0)
1287 info->nports = 0;
1288 else {
1289 info->nports = hdev->maxchild;
1290 for (i = 0; i < info->nports; i++) {
1291 if (hdev->children[i] == NULL)
1292 info->port[i] = 0;
1293 else
1294 info->port[i] =
1295 hdev->children[i]->devnum;
1296 }
1297 }
1298 spin_unlock_irq(&device_state_lock);
1299
1300 return info->nports + 1;
1301 }
1302
1303 default:
1304 return -ENOSYS;
1305 }
1306}
1307
7cbe5dca
AS
1308/*
1309 * Allow user programs to claim ports on a hub. When a device is attached
1310 * to one of these "claimed" ports, the program will "own" the device.
1311 */
1312static int find_port_owner(struct usb_device *hdev, unsigned port1,
1313 void ***ppowner)
1314{
1315 if (hdev->state == USB_STATE_NOTATTACHED)
1316 return -ENODEV;
1317 if (port1 == 0 || port1 > hdev->maxchild)
1318 return -EINVAL;
1319
1320 /* This assumes that devices not managed by the hub driver
1321 * will always have maxchild equal to 0.
1322 */
1323 *ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]);
1324 return 0;
1325}
1326
1327/* In the following three functions, the caller must hold hdev's lock */
1328int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, void *owner)
1329{
1330 int rc;
1331 void **powner;
1332
1333 rc = find_port_owner(hdev, port1, &powner);
1334 if (rc)
1335 return rc;
1336 if (*powner)
1337 return -EBUSY;
1338 *powner = owner;
1339 return rc;
1340}
1341
1342int usb_hub_release_port(struct usb_device *hdev, unsigned port1, void *owner)
1343{
1344 int rc;
1345 void **powner;
1346
1347 rc = find_port_owner(hdev, port1, &powner);
1348 if (rc)
1349 return rc;
1350 if (*powner != owner)
1351 return -ENOENT;
1352 *powner = NULL;
1353 return rc;
1354}
1355
1356void usb_hub_release_all_ports(struct usb_device *hdev, void *owner)
1357{
1358 int n;
1359 void **powner;
1360
1361 n = find_port_owner(hdev, 1, &powner);
1362 if (n == 0) {
1363 for (; n < hdev->maxchild; (++n, ++powner)) {
1364 if (*powner == owner)
1365 *powner = NULL;
1366 }
1367 }
1368}
1369
1370/* The caller must hold udev's lock */
1371bool usb_device_is_owned(struct usb_device *udev)
1372{
1373 struct usb_hub *hub;
1374
1375 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1376 return false;
1377 hub = hdev_to_hub(udev->parent);
1378 return !!hub->port_owners[udev->portnum - 1];
1379}
1380
1da177e4 1381
1da177e4
LT
1382static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1383{
1384 int i;
1385
1386 for (i = 0; i < udev->maxchild; ++i) {
1387 if (udev->children[i])
1388 recursively_mark_NOTATTACHED(udev->children[i]);
1389 }
15123006 1390 if (udev->state == USB_STATE_SUSPENDED) {
ee49fb5d 1391 udev->discon_suspended = 1;
15123006
SS
1392 udev->active_duration -= jiffies;
1393 }
1da177e4
LT
1394 udev->state = USB_STATE_NOTATTACHED;
1395}
1396
1397/**
1398 * usb_set_device_state - change a device's current state (usbcore, hcds)
1399 * @udev: pointer to device whose state should be changed
1400 * @new_state: new state value to be stored
1401 *
1402 * udev->state is _not_ fully protected by the device lock. Although
1403 * most transitions are made only while holding the lock, the state can
1404 * can change to USB_STATE_NOTATTACHED at almost any time. This
1405 * is so that devices can be marked as disconnected as soon as possible,
1406 * without having to wait for any semaphores to be released. As a result,
1407 * all changes to any device's state must be protected by the
1408 * device_state_lock spinlock.
1409 *
1410 * Once a device has been added to the device tree, all changes to its state
1411 * should be made using this routine. The state should _not_ be set directly.
1412 *
1413 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1414 * Otherwise udev->state is set to new_state, and if new_state is
1415 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1416 * to USB_STATE_NOTATTACHED.
1417 */
1418void usb_set_device_state(struct usb_device *udev,
1419 enum usb_device_state new_state)
1420{
1421 unsigned long flags;
1422
1423 spin_lock_irqsave(&device_state_lock, flags);
1424 if (udev->state == USB_STATE_NOTATTACHED)
1425 ; /* do nothing */
b94dc6b5 1426 else if (new_state != USB_STATE_NOTATTACHED) {
fb669cc0
DB
1427
1428 /* root hub wakeup capabilities are managed out-of-band
1429 * and may involve silicon errata ... ignore them here.
1430 */
1431 if (udev->parent) {
645daaab
AS
1432 if (udev->state == USB_STATE_SUSPENDED
1433 || new_state == USB_STATE_SUSPENDED)
1434 ; /* No change to wakeup settings */
1435 else if (new_state == USB_STATE_CONFIGURED)
fb669cc0
DB
1436 device_init_wakeup(&udev->dev,
1437 (udev->actconfig->desc.bmAttributes
1438 & USB_CONFIG_ATT_WAKEUP));
645daaab 1439 else
fb669cc0
DB
1440 device_init_wakeup(&udev->dev, 0);
1441 }
15123006
SS
1442 if (udev->state == USB_STATE_SUSPENDED &&
1443 new_state != USB_STATE_SUSPENDED)
1444 udev->active_duration -= jiffies;
1445 else if (new_state == USB_STATE_SUSPENDED &&
1446 udev->state != USB_STATE_SUSPENDED)
1447 udev->active_duration += jiffies;
645daaab 1448 udev->state = new_state;
b94dc6b5 1449 } else
1da177e4
LT
1450 recursively_mark_NOTATTACHED(udev);
1451 spin_unlock_irqrestore(&device_state_lock, flags);
1452}
6da9c990 1453EXPORT_SYMBOL_GPL(usb_set_device_state);
1da177e4 1454
8af548dc
IPG
1455/*
1456 * WUSB devices are simple: they have no hubs behind, so the mapping
1457 * device <-> virtual port number becomes 1:1. Why? to simplify the
1458 * life of the device connection logic in
1459 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1460 * handshake we need to assign a temporary address in the unauthorized
1461 * space. For simplicity we use the first virtual port number found to
1462 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1463 * and that becomes it's address [X < 128] or its unauthorized address
1464 * [X | 0x80].
1465 *
1466 * We add 1 as an offset to the one-based USB-stack port number
1467 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1468 * 0 is reserved by USB for default address; (b) Linux's USB stack
1469 * uses always #1 for the root hub of the controller. So USB stack's
1470 * port #1, which is wusb virtual-port #0 has address #2.
c6515272
SS
1471 *
1472 * Devices connected under xHCI are not as simple. The host controller
1473 * supports virtualization, so the hardware assigns device addresses and
1474 * the HCD must setup data structures before issuing a set address
1475 * command to the hardware.
8af548dc 1476 */
1da177e4
LT
1477static void choose_address(struct usb_device *udev)
1478{
1479 int devnum;
1480 struct usb_bus *bus = udev->bus;
1481
1482 /* If khubd ever becomes multithreaded, this will need a lock */
8af548dc
IPG
1483 if (udev->wusb) {
1484 devnum = udev->portnum + 1;
1485 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1486 } else {
1487 /* Try to allocate the next devnum beginning at
1488 * bus->devnum_next. */
1489 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1490 bus->devnum_next);
1491 if (devnum >= 128)
1492 devnum = find_next_zero_bit(bus->devmap.devicemap,
1493 128, 1);
1494 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1495 }
1da177e4
LT
1496 if (devnum < 128) {
1497 set_bit(devnum, bus->devmap.devicemap);
1498 udev->devnum = devnum;
1499 }
1500}
1501
1502static void release_address(struct usb_device *udev)
1503{
1504 if (udev->devnum > 0) {
1505 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1506 udev->devnum = -1;
1507 }
1508}
1509
4953d141
DV
1510static void update_address(struct usb_device *udev, int devnum)
1511{
1512 /* The address for a WUSB device is managed by wusbcore. */
1513 if (!udev->wusb)
1514 udev->devnum = devnum;
1515}
1516
d5d4db70
AS
1517#ifdef CONFIG_USB_SUSPEND
1518
1519static void usb_stop_pm(struct usb_device *udev)
1520{
1521 /* Synchronize with the ksuspend thread to prevent any more
1522 * autosuspend requests from being submitted, and decrement
1523 * the parent's count of unsuspended children.
1524 */
1525 usb_pm_lock(udev);
1526 if (udev->parent && !udev->discon_suspended)
1527 usb_autosuspend_device(udev->parent);
1528 usb_pm_unlock(udev);
1529
9ac39f28
AS
1530 /* Stop any autosuspend or autoresume requests already submitted */
1531 cancel_delayed_work_sync(&udev->autosuspend);
1532 cancel_work_sync(&udev->autoresume);
d5d4db70
AS
1533}
1534
1535#else
1536
1537static inline void usb_stop_pm(struct usb_device *udev)
1538{ }
1539
1540#endif
1541
1da177e4
LT
1542/**
1543 * usb_disconnect - disconnect a device (usbcore-internal)
1544 * @pdev: pointer to device being disconnected
1545 * Context: !in_interrupt ()
1546 *
1547 * Something got disconnected. Get rid of it and all of its children.
1548 *
1549 * If *pdev is a normal device then the parent hub must already be locked.
1550 * If *pdev is a root hub then this routine will acquire the
1551 * usb_bus_list_lock on behalf of the caller.
1552 *
1553 * Only hub drivers (including virtual root hub drivers for host
1554 * controllers) should ever call this.
1555 *
1556 * This call is synchronous, and may not be used in an interrupt context.
1557 */
1558void usb_disconnect(struct usb_device **pdev)
1559{
1560 struct usb_device *udev = *pdev;
1561 int i;
1562
1563 if (!udev) {
441b62c1 1564 pr_debug ("%s nodev\n", __func__);
1da177e4
LT
1565 return;
1566 }
1567
1568 /* mark the device as inactive, so any further urb submissions for
1569 * this device (and any of its children) will fail immediately.
1570 * this quiesces everyting except pending urbs.
1571 */
1572 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1da177e4
LT
1573 dev_info (&udev->dev, "USB disconnect, address %d\n", udev->devnum);
1574
9ad3d6cc
AS
1575 usb_lock_device(udev);
1576
1da177e4
LT
1577 /* Free up all the children before we remove this device */
1578 for (i = 0; i < USB_MAXCHILDREN; i++) {
1579 if (udev->children[i])
1580 usb_disconnect(&udev->children[i]);
1581 }
1582
1583 /* deallocate hcd/hardware state ... nuking all pending urbs and
1584 * cleaning up all state associated with the current configuration
1585 * so that the hardware is now fully quiesced.
1586 */
782da727 1587 dev_dbg (&udev->dev, "unregistering device\n");
1da177e4 1588 usb_disable_device(udev, 0);
cde217a5 1589 usb_hcd_synchronize_unlinks(udev);
1da177e4 1590
3b23dd6f 1591 usb_remove_ep_devs(&udev->ep0);
782da727
AS
1592 usb_unlock_device(udev);
1593
1594 /* Unregister the device. The device driver is responsible
3b23dd6f
AS
1595 * for de-configuring the device and invoking the remove-device
1596 * notifier chain (used by usbfs and possibly others).
782da727
AS
1597 */
1598 device_del(&udev->dev);
3099e75a 1599
782da727 1600 /* Free the device number and delete the parent's children[]
1da177e4
LT
1601 * (or root_hub) pointer.
1602 */
1da177e4 1603 release_address(udev);
1da177e4
LT
1604
1605 /* Avoid races with recursively_mark_NOTATTACHED() */
1606 spin_lock_irq(&device_state_lock);
1607 *pdev = NULL;
1608 spin_unlock_irq(&device_state_lock);
1609
d5d4db70 1610 usb_stop_pm(udev);
ee49fb5d 1611
782da727 1612 put_device(&udev->dev);
1da177e4
LT
1613}
1614
f2a383e4 1615#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
1da177e4
LT
1616static void show_string(struct usb_device *udev, char *id, char *string)
1617{
1618 if (!string)
1619 return;
1620 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
1621}
1622
f2a383e4
GKH
1623static void announce_device(struct usb_device *udev)
1624{
1625 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
1626 le16_to_cpu(udev->descriptor.idVendor),
1627 le16_to_cpu(udev->descriptor.idProduct));
b9cef6c3
WF
1628 dev_info(&udev->dev,
1629 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
f2a383e4
GKH
1630 udev->descriptor.iManufacturer,
1631 udev->descriptor.iProduct,
1632 udev->descriptor.iSerialNumber);
1633 show_string(udev, "Product", udev->product);
1634 show_string(udev, "Manufacturer", udev->manufacturer);
1635 show_string(udev, "SerialNumber", udev->serial);
1636}
1da177e4 1637#else
f2a383e4 1638static inline void announce_device(struct usb_device *udev) { }
1da177e4
LT
1639#endif
1640
1da177e4
LT
1641#ifdef CONFIG_USB_OTG
1642#include "otg_whitelist.h"
1643#endif
1644
3ede760f 1645/**
d9d16e8a 1646 * usb_configure_device_otg - FIXME (usbcore-internal)
3ede760f
ON
1647 * @udev: newly addressed device (in ADDRESS state)
1648 *
d9d16e8a 1649 * Do configuration for On-The-Go devices
3ede760f 1650 */
d9d16e8a 1651static int usb_configure_device_otg(struct usb_device *udev)
1da177e4 1652{
d9d16e8a 1653 int err = 0;
1da177e4
LT
1654
1655#ifdef CONFIG_USB_OTG
1656 /*
1657 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
1658 * to wake us after we've powered off VBUS; and HNP, switching roles
1659 * "host" to "peripheral". The OTG descriptor helps figure this out.
1660 */
1661 if (!udev->bus->is_b_host
1662 && udev->config
1663 && udev->parent == udev->bus->root_hub) {
1664 struct usb_otg_descriptor *desc = 0;
1665 struct usb_bus *bus = udev->bus;
1666
1667 /* descriptor may appear anywhere in config */
1668 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
1669 le16_to_cpu(udev->config[0].desc.wTotalLength),
1670 USB_DT_OTG, (void **) &desc) == 0) {
1671 if (desc->bmAttributes & USB_OTG_HNP) {
12c3da34 1672 unsigned port1 = udev->portnum;
fc849b85 1673
1da177e4
LT
1674 dev_info(&udev->dev,
1675 "Dual-Role OTG device on %sHNP port\n",
1676 (port1 == bus->otg_port)
1677 ? "" : "non-");
1678
1679 /* enable HNP before suspend, it's simpler */
1680 if (port1 == bus->otg_port)
1681 bus->b_hnp_enable = 1;
1682 err = usb_control_msg(udev,
1683 usb_sndctrlpipe(udev, 0),
1684 USB_REQ_SET_FEATURE, 0,
1685 bus->b_hnp_enable
1686 ? USB_DEVICE_B_HNP_ENABLE
1687 : USB_DEVICE_A_ALT_HNP_SUPPORT,
1688 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
1689 if (err < 0) {
1690 /* OTG MESSAGE: report errors here,
1691 * customize to match your product.
1692 */
1693 dev_info(&udev->dev,
b9cef6c3 1694 "can't set HNP mode: %d\n",
1da177e4
LT
1695 err);
1696 bus->b_hnp_enable = 0;
1697 }
1698 }
1699 }
1700 }
1701
1702 if (!is_targeted(udev)) {
1703
1704 /* Maybe it can talk to us, though we can't talk to it.
1705 * (Includes HNP test device.)
1706 */
1707 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
634a84f8 1708 err = usb_port_suspend(udev, PMSG_SUSPEND);
1da177e4
LT
1709 if (err < 0)
1710 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
1711 }
ffcdc18d 1712 err = -ENOTSUPP;
1da177e4
LT
1713 goto fail;
1714 }
d9d16e8a 1715fail:
1da177e4 1716#endif
d9d16e8a
IPG
1717 return err;
1718}
1719
1720
1721/**
1722 * usb_configure_device - Detect and probe device intfs/otg (usbcore-internal)
1723 * @udev: newly addressed device (in ADDRESS state)
1724 *
1725 * This is only called by usb_new_device() and usb_authorize_device()
1726 * and FIXME -- all comments that apply to them apply here wrt to
1727 * environment.
1728 *
1729 * If the device is WUSB and not authorized, we don't attempt to read
1730 * the string descriptors, as they will be errored out by the device
1731 * until it has been authorized.
1732 */
1733static int usb_configure_device(struct usb_device *udev)
1734{
1735 int err;
1da177e4 1736
d9d16e8a
IPG
1737 if (udev->config == NULL) {
1738 err = usb_get_configuration(udev);
1739 if (err < 0) {
1740 dev_err(&udev->dev, "can't read configurations, error %d\n",
1741 err);
1742 goto fail;
1743 }
1744 }
1745 if (udev->wusb == 1 && udev->authorized == 0) {
1746 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1747 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1748 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1749 }
1750 else {
1751 /* read the standard strings and cache them if present */
1752 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
1753 udev->manufacturer = usb_cache_string(udev,
1754 udev->descriptor.iManufacturer);
1755 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
1756 }
1757 err = usb_configure_device_otg(udev);
1758fail:
1759 return err;
1760}
1761
1762
1763/**
1764 * usb_new_device - perform initial device setup (usbcore-internal)
1765 * @udev: newly addressed device (in ADDRESS state)
1766 *
1767 * This is called with devices which have been enumerated, but not yet
1768 * configured. The device descriptor is available, but not descriptors
1769 * for any device configuration. The caller must have locked either
1770 * the parent hub (if udev is a normal device) or else the
1771 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
1772 * udev has already been installed, but udev is not yet visible through
1773 * sysfs or other filesystem code.
1774 *
1775 * It will return if the device is configured properly or not. Zero if
1776 * the interface was registered with the driver core; else a negative
1777 * errno value.
1778 *
1779 * This call is synchronous, and may not be used in an interrupt context.
1780 *
1781 * Only the hub driver or root-hub registrar should ever call this.
1782 */
1783int usb_new_device(struct usb_device *udev)
1784{
1785 int err;
1786
6cd13201
AS
1787 /* Increment the parent's count of unsuspended children */
1788 if (udev->parent)
1789 usb_autoresume_device(udev->parent);
1790
d9d16e8a
IPG
1791 usb_detect_quirks(udev); /* Determine quirks */
1792 err = usb_configure_device(udev); /* detect & probe dev/intfs */
1793 if (err < 0)
1794 goto fail;
c6515272
SS
1795 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
1796 udev->devnum, udev->bus->busnum,
1797 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
9f8b17e6
KS
1798 /* export the usbdev device-node for libusb */
1799 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
1800 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
1801
6cd13201
AS
1802 /* Tell the world! */
1803 announce_device(udev);
195af2cc 1804
782da727 1805 /* Register the device. The device driver is responsible
3b23dd6f
AS
1806 * for configuring the device and invoking the add-device
1807 * notifier chain (used by usbfs and possibly others).
782da727 1808 */
9f8b17e6 1809 err = device_add(&udev->dev);
1da177e4
LT
1810 if (err) {
1811 dev_err(&udev->dev, "can't device_add, error %d\n", err);
1812 goto fail;
1813 }
9ad3d6cc 1814
3b23dd6f 1815 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
c066475e 1816 return err;
1da177e4
LT
1817
1818fail:
1819 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
6cd13201 1820 usb_stop_pm(udev);
d9d16e8a 1821 return err;
1da177e4
LT
1822}
1823
93993a0a
IPG
1824
1825/**
fd39c86b
RD
1826 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
1827 * @usb_dev: USB device
1828 *
1829 * Move the USB device to a very basic state where interfaces are disabled
1830 * and the device is in fact unconfigured and unusable.
93993a0a
IPG
1831 *
1832 * We share a lock (that we have) with device_del(), so we need to
1833 * defer its call.
1834 */
1835int usb_deauthorize_device(struct usb_device *usb_dev)
1836{
1837 unsigned cnt;
1838 usb_lock_device(usb_dev);
1839 if (usb_dev->authorized == 0)
1840 goto out_unauthorized;
1841 usb_dev->authorized = 0;
1842 usb_set_configuration(usb_dev, -1);
1843 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1844 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1845 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1846 kfree(usb_dev->config);
1847 usb_dev->config = NULL;
1848 for (cnt = 0; cnt < usb_dev->descriptor.bNumConfigurations; cnt++)
1849 kfree(usb_dev->rawdescriptors[cnt]);
1850 usb_dev->descriptor.bNumConfigurations = 0;
1851 kfree(usb_dev->rawdescriptors);
1852out_unauthorized:
1853 usb_unlock_device(usb_dev);
1854 return 0;
1855}
1856
1857
1858int usb_authorize_device(struct usb_device *usb_dev)
1859{
1860 int result = 0, c;
1861 usb_lock_device(usb_dev);
1862 if (usb_dev->authorized == 1)
1863 goto out_authorized;
1864 kfree(usb_dev->product);
1865 usb_dev->product = NULL;
1866 kfree(usb_dev->manufacturer);
1867 usb_dev->manufacturer = NULL;
1868 kfree(usb_dev->serial);
1869 usb_dev->serial = NULL;
1870 result = usb_autoresume_device(usb_dev);
1871 if (result < 0) {
1872 dev_err(&usb_dev->dev,
1873 "can't autoresume for authorization: %d\n", result);
1874 goto error_autoresume;
1875 }
1876 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
1877 if (result < 0) {
1878 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
1879 "authorization: %d\n", result);
1880 goto error_device_descriptor;
1881 }
1882 usb_dev->authorized = 1;
1883 result = usb_configure_device(usb_dev);
1884 if (result < 0)
1885 goto error_configure;
1886 /* Choose and set the configuration. This registers the interfaces
1887 * with the driver core and lets interface drivers bind to them.
1888 */
b5ea060f 1889 c = usb_choose_configuration(usb_dev);
93993a0a
IPG
1890 if (c >= 0) {
1891 result = usb_set_configuration(usb_dev, c);
1892 if (result) {
1893 dev_err(&usb_dev->dev,
1894 "can't set config #%d, error %d\n", c, result);
1895 /* This need not be fatal. The user can try to
1896 * set other configurations. */
1897 }
1898 }
1899 dev_info(&usb_dev->dev, "authorized to connect\n");
1900error_configure:
1901error_device_descriptor:
1902error_autoresume:
1903out_authorized:
1904 usb_unlock_device(usb_dev); // complements locktree
1905 return result;
1906}
1907
1908
0165de09
IPG
1909/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
1910static unsigned hub_is_wusb(struct usb_hub *hub)
1911{
1912 struct usb_hcd *hcd;
1913 if (hub->hdev->parent != NULL) /* not a root hub? */
1914 return 0;
1915 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
1916 return hcd->wireless;
1917}
1918
1919
1da177e4
LT
1920#define PORT_RESET_TRIES 5
1921#define SET_ADDRESS_TRIES 2
1922#define GET_DESCRIPTOR_TRIES 2
1923#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
1924#define USE_NEW_SCHEME(i) ((i) / 2 == old_scheme_first)
1925
1926#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
1927#define HUB_SHORT_RESET_TIME 10
1928#define HUB_LONG_RESET_TIME 200
1929#define HUB_RESET_TIMEOUT 500
1930
1931static int hub_port_wait_reset(struct usb_hub *hub, int port1,
1932 struct usb_device *udev, unsigned int delay)
1933{
1934 int delay_time, ret;
1935 u16 portstatus;
1936 u16 portchange;
1937
1938 for (delay_time = 0;
1939 delay_time < HUB_RESET_TIMEOUT;
1940 delay_time += delay) {
1941 /* wait to give the device a chance to reset */
1942 msleep(delay);
1943
1944 /* read and decode port status */
1945 ret = hub_port_status(hub, port1, &portstatus, &portchange);
1946 if (ret < 0)
1947 return ret;
1948
1949 /* Device went away? */
1950 if (!(portstatus & USB_PORT_STAT_CONNECTION))
1951 return -ENOTCONN;
1952
dd4dd19e 1953 /* bomb out completely if the connection bounced */
1da177e4 1954 if ((portchange & USB_PORT_STAT_C_CONNECTION))
dd4dd19e 1955 return -ENOTCONN;
1da177e4
LT
1956
1957 /* if we`ve finished resetting, then break out of the loop */
1958 if (!(portstatus & USB_PORT_STAT_RESET) &&
1959 (portstatus & USB_PORT_STAT_ENABLE)) {
0165de09
IPG
1960 if (hub_is_wusb(hub))
1961 udev->speed = USB_SPEED_VARIABLE;
1962 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
1da177e4
LT
1963 udev->speed = USB_SPEED_HIGH;
1964 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
1965 udev->speed = USB_SPEED_LOW;
1966 else
1967 udev->speed = USB_SPEED_FULL;
1968 return 0;
1969 }
1970
1971 /* switch to the long delay after two short delay failures */
1972 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
1973 delay = HUB_LONG_RESET_TIME;
1974
1975 dev_dbg (hub->intfdev,
1976 "port %d not reset yet, waiting %dms\n",
1977 port1, delay);
1978 }
1979
1980 return -EBUSY;
1981}
1982
1983static int hub_port_reset(struct usb_hub *hub, int port1,
1984 struct usb_device *udev, unsigned int delay)
1985{
1986 int i, status;
1987
32fe0198
AS
1988 /* Block EHCI CF initialization during the port reset.
1989 * Some companion controllers don't like it when they mix.
1990 */
1991 down_read(&ehci_cf_port_reset_rwsem);
1992
1da177e4
LT
1993 /* Reset the port */
1994 for (i = 0; i < PORT_RESET_TRIES; i++) {
1995 status = set_port_feature(hub->hdev,
1996 port1, USB_PORT_FEAT_RESET);
1997 if (status)
1998 dev_err(hub->intfdev,
1999 "cannot reset port %d (err = %d)\n",
2000 port1, status);
2001 else {
2002 status = hub_port_wait_reset(hub, port1, udev, delay);
dd16525b 2003 if (status && status != -ENOTCONN)
1da177e4
LT
2004 dev_dbg(hub->intfdev,
2005 "port_wait_reset: err = %d\n",
2006 status);
2007 }
2008
2009 /* return on disconnect or reset */
2010 switch (status) {
2011 case 0:
b789696a
DB
2012 /* TRSTRCY = 10 ms; plus some extra */
2013 msleep(10 + 40);
4953d141 2014 update_address(udev, 0);
1da177e4
LT
2015 /* FALL THROUGH */
2016 case -ENOTCONN:
2017 case -ENODEV:
2018 clear_port_feature(hub->hdev,
2019 port1, USB_PORT_FEAT_C_RESET);
2020 /* FIXME need disconnect() for NOTATTACHED device */
2021 usb_set_device_state(udev, status
2022 ? USB_STATE_NOTATTACHED
2023 : USB_STATE_DEFAULT);
32fe0198 2024 goto done;
1da177e4
LT
2025 }
2026
2027 dev_dbg (hub->intfdev,
2028 "port %d not enabled, trying reset again...\n",
2029 port1);
2030 delay = HUB_LONG_RESET_TIME;
2031 }
2032
2033 dev_err (hub->intfdev,
2034 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2035 port1);
2036
32fe0198
AS
2037 done:
2038 up_read(&ehci_cf_port_reset_rwsem);
1da177e4
LT
2039 return status;
2040}
2041
d388dab7 2042#ifdef CONFIG_PM
1da177e4 2043
b01b03f3
AS
2044#define MASK_BITS (USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION | \
2045 USB_PORT_STAT_SUSPEND)
2046#define WANT_BITS (USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION)
2047
2048/* Determine whether the device on a port is ready for a normal resume,
2049 * is ready for a reset-resume, or should be disconnected.
2050 */
2051static int check_port_resume_type(struct usb_device *udev,
2052 struct usb_hub *hub, int port1,
2053 int status, unsigned portchange, unsigned portstatus)
2054{
2055 /* Is the device still present? */
2056 if (status || (portstatus & MASK_BITS) != WANT_BITS) {
2057 if (status >= 0)
2058 status = -ENODEV;
2059 }
2060
86c57edf
AS
2061 /* Can't do a normal resume if the port isn't enabled,
2062 * so try a reset-resume instead.
2063 */
2064 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2065 if (udev->persist_enabled)
2066 udev->reset_resume = 1;
2067 else
2068 status = -ENODEV;
2069 }
b01b03f3
AS
2070
2071 if (status) {
2072 dev_dbg(hub->intfdev,
2073 "port %d status %04x.%04x after resume, %d\n",
2074 port1, portchange, portstatus, status);
2075 } else if (udev->reset_resume) {
2076
2077 /* Late port handoff can set status-change bits */
2078 if (portchange & USB_PORT_STAT_C_CONNECTION)
2079 clear_port_feature(hub->hdev, port1,
2080 USB_PORT_FEAT_C_CONNECTION);
2081 if (portchange & USB_PORT_STAT_C_ENABLE)
2082 clear_port_feature(hub->hdev, port1,
2083 USB_PORT_FEAT_C_ENABLE);
2084 }
2085
2086 return status;
2087}
2088
1da177e4
LT
2089#ifdef CONFIG_USB_SUSPEND
2090
2091/*
624d6c07
AS
2092 * usb_port_suspend - suspend a usb device's upstream port
2093 * @udev: device that's no longer in active use, not a root hub
2094 * Context: must be able to sleep; device not locked; pm locks held
2095 *
2096 * Suspends a USB device that isn't in active use, conserving power.
2097 * Devices may wake out of a suspend, if anything important happens,
2098 * using the remote wakeup mechanism. They may also be taken out of
2099 * suspend by the host, using usb_port_resume(). It's also routine
2100 * to disconnect devices while they are suspended.
2101 *
2102 * This only affects the USB hardware for a device; its interfaces
2103 * (and, for hubs, child devices) must already have been suspended.
2104 *
1da177e4
LT
2105 * Selective port suspend reduces power; most suspended devices draw
2106 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2107 * All devices below the suspended port are also suspended.
2108 *
2109 * Devices leave suspend state when the host wakes them up. Some devices
2110 * also support "remote wakeup", where the device can activate the USB
2111 * tree above them to deliver data, such as a keypress or packet. In
2112 * some cases, this wakes the USB host.
624d6c07
AS
2113 *
2114 * Suspending OTG devices may trigger HNP, if that's been enabled
2115 * between a pair of dual-role devices. That will change roles, such
2116 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2117 *
2118 * Devices on USB hub ports have only one "suspend" state, corresponding
2119 * to ACPI D2, "may cause the device to lose some context".
2120 * State transitions include:
2121 *
2122 * - suspend, resume ... when the VBUS power link stays live
2123 * - suspend, disconnect ... VBUS lost
2124 *
2125 * Once VBUS drop breaks the circuit, the port it's using has to go through
2126 * normal re-enumeration procedures, starting with enabling VBUS power.
2127 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2128 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2129 * timer, no SRP, no requests through sysfs.
2130 *
2131 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2132 * the root hub for their bus goes into global suspend ... so we don't
2133 * (falsely) update the device power state to say it suspended.
2134 *
2135 * Returns 0 on success, else negative errno.
1da177e4 2136 */
65bfd296 2137int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
1da177e4 2138{
624d6c07
AS
2139 struct usb_hub *hub = hdev_to_hub(udev->parent);
2140 int port1 = udev->portnum;
2141 int status;
1da177e4
LT
2142
2143 // dev_dbg(hub->intfdev, "suspend port %d\n", port1);
2144
2145 /* enable remote wakeup when appropriate; this lets the device
2146 * wake up the upstream hub (including maybe the root hub).
2147 *
2148 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2149 * we don't explicitly enable it here.
2150 */
645daaab 2151 if (udev->do_remote_wakeup) {
1da177e4
LT
2152 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2153 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2154 USB_DEVICE_REMOTE_WAKEUP, 0,
2155 NULL, 0,
2156 USB_CTRL_SET_TIMEOUT);
0c487206 2157 if (status) {
624d6c07
AS
2158 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2159 status);
0c487206
ON
2160 /* bail if autosuspend is requested */
2161 if (msg.event & PM_EVENT_AUTO)
2162 return status;
2163 }
1da177e4
LT
2164 }
2165
2166 /* see 7.1.7.6 */
2167 status = set_port_feature(hub->hdev, port1, USB_PORT_FEAT_SUSPEND);
2168 if (status) {
624d6c07
AS
2169 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2170 port1, status);
1da177e4 2171 /* paranoia: "should not happen" */
0c487206
ON
2172 if (udev->do_remote_wakeup)
2173 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1da177e4
LT
2174 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2175 USB_DEVICE_REMOTE_WAKEUP, 0,
2176 NULL, 0,
2177 USB_CTRL_SET_TIMEOUT);
2178 } else {
2179 /* device has up to 10 msec to fully suspend */
645daaab 2180 dev_dbg(&udev->dev, "usb %ssuspend\n",
65bfd296 2181 (msg.event & PM_EVENT_AUTO ? "auto-" : ""));
1da177e4
LT
2182 usb_set_device_state(udev, USB_STATE_SUSPENDED);
2183 msleep(10);
2184 }
2185 return status;
2186}
2187
1da177e4 2188/*
390a8c34
DB
2189 * If the USB "suspend" state is in use (rather than "global suspend"),
2190 * many devices will be individually taken out of suspend state using
54515fe5 2191 * special "resume" signaling. This routine kicks in shortly after
1da177e4
LT
2192 * hardware resume signaling is finished, either because of selective
2193 * resume (by host) or remote wakeup (by device) ... now see what changed
2194 * in the tree that's rooted at this device.
54515fe5
AS
2195 *
2196 * If @udev->reset_resume is set then the device is reset before the
2197 * status check is done.
1da177e4 2198 */
140d8f68 2199static int finish_port_resume(struct usb_device *udev)
1da177e4 2200{
54515fe5 2201 int status = 0;
1da177e4
LT
2202 u16 devstatus;
2203
2204 /* caller owns the udev device lock */
b9cef6c3
WF
2205 dev_dbg(&udev->dev, "%s\n",
2206 udev->reset_resume ? "finish reset-resume" : "finish resume");
1da177e4
LT
2207
2208 /* usb ch9 identifies four variants of SUSPENDED, based on what
2209 * state the device resumes to. Linux currently won't see the
2210 * first two on the host side; they'd be inside hub_port_init()
2211 * during many timeouts, but khubd can't suspend until later.
2212 */
2213 usb_set_device_state(udev, udev->actconfig
2214 ? USB_STATE_CONFIGURED
2215 : USB_STATE_ADDRESS);
1da177e4 2216
54515fe5
AS
2217 /* 10.5.4.5 says not to reset a suspended port if the attached
2218 * device is enabled for remote wakeup. Hence the reset
2219 * operation is carried out here, after the port has been
2220 * resumed.
2221 */
2222 if (udev->reset_resume)
86c57edf 2223 retry_reset_resume:
742120c6 2224 status = usb_reset_and_verify_device(udev);
54515fe5 2225
1da177e4
LT
2226 /* 10.5.4.5 says be sure devices in the tree are still there.
2227 * For now let's assume the device didn't go crazy on resume,
2228 * and device drivers will know about any resume quirks.
2229 */
54515fe5 2230 if (status == 0) {
46dede46 2231 devstatus = 0;
54515fe5
AS
2232 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2233 if (status >= 0)
46dede46 2234 status = (status > 0 ? 0 : -ENODEV);
86c57edf
AS
2235
2236 /* If a normal resume failed, try doing a reset-resume */
2237 if (status && !udev->reset_resume && udev->persist_enabled) {
2238 dev_dbg(&udev->dev, "retry with reset-resume\n");
2239 udev->reset_resume = 1;
2240 goto retry_reset_resume;
2241 }
54515fe5 2242 }
b40b7a90 2243
624d6c07
AS
2244 if (status) {
2245 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2246 status);
2247 } else if (udev->actconfig) {
1da177e4 2248 le16_to_cpus(&devstatus);
686314cf 2249 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
1da177e4
LT
2250 status = usb_control_msg(udev,
2251 usb_sndctrlpipe(udev, 0),
2252 USB_REQ_CLEAR_FEATURE,
2253 USB_RECIP_DEVICE,
2254 USB_DEVICE_REMOTE_WAKEUP, 0,
2255 NULL, 0,
2256 USB_CTRL_SET_TIMEOUT);
a8e7c565 2257 if (status)
b9cef6c3
WF
2258 dev_dbg(&udev->dev,
2259 "disable remote wakeup, status %d\n",
2260 status);
4bf0ba86 2261 }
1da177e4 2262 status = 0;
1da177e4
LT
2263 }
2264 return status;
2265}
2266
624d6c07
AS
2267/*
2268 * usb_port_resume - re-activate a suspended usb device's upstream port
2269 * @udev: device to re-activate, not a root hub
2270 * Context: must be able to sleep; device not locked; pm locks held
2271 *
2272 * This will re-activate the suspended device, increasing power usage
2273 * while letting drivers communicate again with its endpoints.
2274 * USB resume explicitly guarantees that the power session between
2275 * the host and the device is the same as it was when the device
2276 * suspended.
2277 *
feccc30d
AS
2278 * If @udev->reset_resume is set then this routine won't check that the
2279 * port is still enabled. Furthermore, finish_port_resume() above will
54515fe5
AS
2280 * reset @udev. The end result is that a broken power session can be
2281 * recovered and @udev will appear to persist across a loss of VBUS power.
2282 *
2283 * For example, if a host controller doesn't maintain VBUS suspend current
2284 * during a system sleep or is reset when the system wakes up, all the USB
2285 * power sessions below it will be broken. This is especially troublesome
2286 * for mass-storage devices containing mounted filesystems, since the
2287 * device will appear to have disconnected and all the memory mappings
2288 * to it will be lost. Using the USB_PERSIST facility, the device can be
2289 * made to appear as if it had not disconnected.
2290 *
742120c6 2291 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
feccc30d 2292 * every effort to insure that the same device is present after the
54515fe5
AS
2293 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2294 * quite possible for a device to remain unaltered but its media to be
2295 * changed. If the user replaces a flash memory card while the system is
2296 * asleep, he will have only himself to blame when the filesystem on the
2297 * new card is corrupted and the system crashes.
2298 *
624d6c07
AS
2299 * Returns 0 on success, else negative errno.
2300 */
65bfd296 2301int usb_port_resume(struct usb_device *udev, pm_message_t msg)
1da177e4 2302{
624d6c07
AS
2303 struct usb_hub *hub = hdev_to_hub(udev->parent);
2304 int port1 = udev->portnum;
2305 int status;
2306 u16 portchange, portstatus;
d25450c6
AS
2307
2308 /* Skip the initial Clear-Suspend step for a remote wakeup */
2309 status = hub_port_status(hub, port1, &portstatus, &portchange);
2310 if (status == 0 && !(portstatus & USB_PORT_STAT_SUSPEND))
2311 goto SuspendCleared;
1da177e4
LT
2312
2313 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
2314
d5cbad4b
AS
2315 set_bit(port1, hub->busy_bits);
2316
1da177e4
LT
2317 /* see 7.1.7.7; affects power usage, but not budgeting */
2318 status = clear_port_feature(hub->hdev,
2319 port1, USB_PORT_FEAT_SUSPEND);
2320 if (status) {
624d6c07
AS
2321 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
2322 port1, status);
1da177e4 2323 } else {
1da177e4 2324 /* drive resume for at least 20 msec */
686314cf 2325 dev_dbg(&udev->dev, "usb %sresume\n",
65bfd296 2326 (msg.event & PM_EVENT_AUTO ? "auto-" : ""));
1da177e4
LT
2327 msleep(25);
2328
1da177e4
LT
2329 /* Virtual root hubs can trigger on GET_PORT_STATUS to
2330 * stop resume signaling. Then finish the resume
2331 * sequence.
2332 */
d25450c6 2333 status = hub_port_status(hub, port1, &portstatus, &portchange);
54515fe5 2334
b01b03f3
AS
2335 /* TRSMRCY = 10 msec */
2336 msleep(10);
2337 }
2338
54515fe5 2339 SuspendCleared:
b01b03f3
AS
2340 if (status == 0) {
2341 if (portchange & USB_PORT_STAT_C_SUSPEND)
2342 clear_port_feature(hub->hdev, port1,
2343 USB_PORT_FEAT_C_SUSPEND);
1da177e4 2344 }
1da177e4 2345
d5cbad4b 2346 clear_bit(port1, hub->busy_bits);
d5cbad4b 2347
b01b03f3
AS
2348 status = check_port_resume_type(udev,
2349 hub, port1, status, portchange, portstatus);
54515fe5
AS
2350 if (status == 0)
2351 status = finish_port_resume(udev);
2352 if (status < 0) {
2353 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2354 hub_port_logical_disconnect(hub, port1);
2355 }
1da177e4
LT
2356 return status;
2357}
2358
8808f00c 2359/* caller has locked udev */
1da177e4
LT
2360static int remote_wakeup(struct usb_device *udev)
2361{
2362 int status = 0;
2363
1da177e4 2364 if (udev->state == USB_STATE_SUSPENDED) {
645daaab 2365 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
1941044a 2366 usb_mark_last_busy(udev);
65bfd296 2367 status = usb_external_resume_device(udev, PMSG_REMOTE_RESUME);
1da177e4 2368 }
1da177e4
LT
2369 return status;
2370}
2371
d388dab7
AS
2372#else /* CONFIG_USB_SUSPEND */
2373
2374/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
2375
65bfd296 2376int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
d388dab7
AS
2377{
2378 return 0;
2379}
2380
b01b03f3
AS
2381/* However we may need to do a reset-resume */
2382
65bfd296 2383int usb_port_resume(struct usb_device *udev, pm_message_t msg)
d388dab7 2384{
b01b03f3
AS
2385 struct usb_hub *hub = hdev_to_hub(udev->parent);
2386 int port1 = udev->portnum;
2387 int status;
2388 u16 portchange, portstatus;
2389
2390 status = hub_port_status(hub, port1, &portstatus, &portchange);
2391 status = check_port_resume_type(udev,
2392 hub, port1, status, portchange, portstatus);
54515fe5 2393
b01b03f3
AS
2394 if (status) {
2395 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2396 hub_port_logical_disconnect(hub, port1);
2397 } else if (udev->reset_resume) {
54515fe5 2398 dev_dbg(&udev->dev, "reset-resume\n");
742120c6 2399 status = usb_reset_and_verify_device(udev);
54515fe5
AS
2400 }
2401 return status;
d388dab7
AS
2402}
2403
2404static inline int remote_wakeup(struct usb_device *udev)
2405{
2406 return 0;
2407}
2408
2409#endif
2410
db690874 2411static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
1da177e4
LT
2412{
2413 struct usb_hub *hub = usb_get_intfdata (intf);
2414 struct usb_device *hdev = hub->hdev;
2415 unsigned port1;
1da177e4 2416
c9f89fa4 2417 /* fail if children aren't already suspended */
1da177e4
LT
2418 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
2419 struct usb_device *udev;
2420
2421 udev = hdev->children [port1-1];
6840d255 2422 if (udev && udev->can_submit) {
65bfd296 2423 if (!(msg.event & PM_EVENT_AUTO))
645daaab
AS
2424 dev_dbg(&intf->dev, "port %d nyet suspended\n",
2425 port1);
c9f89fa4
DB
2426 return -EBUSY;
2427 }
1da177e4
LT
2428 }
2429
441b62c1 2430 dev_dbg(&intf->dev, "%s\n", __func__);
40f122f3 2431
12f1ff83 2432 /* stop khubd and related activity */
4330354f 2433 hub_quiesce(hub, HUB_SUSPEND);
b6f6436d 2434 return 0;
1da177e4
LT
2435}
2436
2437static int hub_resume(struct usb_interface *intf)
2438{
5e6effae 2439 struct usb_hub *hub = usb_get_intfdata(intf);
40f122f3 2440
5e6effae 2441 dev_dbg(&intf->dev, "%s\n", __func__);
f2835219 2442 hub_activate(hub, HUB_RESUME);
1da177e4
LT
2443 return 0;
2444}
2445
b41a60ec 2446static int hub_reset_resume(struct usb_interface *intf)
f07600cf 2447{
b41a60ec 2448 struct usb_hub *hub = usb_get_intfdata(intf);
f07600cf 2449
5e6effae 2450 dev_dbg(&intf->dev, "%s\n", __func__);
f2835219 2451 hub_activate(hub, HUB_RESET_RESUME);
f07600cf
AS
2452 return 0;
2453}
2454
54515fe5
AS
2455/**
2456 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
2457 * @rhdev: struct usb_device for the root hub
2458 *
2459 * The USB host controller driver calls this function when its root hub
2460 * is resumed and Vbus power has been interrupted or the controller
feccc30d
AS
2461 * has been reset. The routine marks @rhdev as having lost power.
2462 * When the hub driver is resumed it will take notice and carry out
2463 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
2464 * the others will be disconnected.
54515fe5
AS
2465 */
2466void usb_root_hub_lost_power(struct usb_device *rhdev)
2467{
2468 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
2469 rhdev->reset_resume = 1;
2470}
2471EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
2472
d388dab7
AS
2473#else /* CONFIG_PM */
2474
2475static inline int remote_wakeup(struct usb_device *udev)
2476{
2477 return 0;
2478}
2479
f07600cf
AS
2480#define hub_suspend NULL
2481#define hub_resume NULL
2482#define hub_reset_resume NULL
d388dab7
AS
2483#endif
2484
1da177e4
LT
2485
2486/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
2487 *
2488 * Between connect detection and reset signaling there must be a delay
2489 * of 100ms at least for debounce and power-settling. The corresponding
2490 * timer shall restart whenever the downstream port detects a disconnect.
2491 *
2492 * Apparently there are some bluetooth and irda-dongles and a number of
2493 * low-speed devices for which this debounce period may last over a second.
2494 * Not covered by the spec - but easy to deal with.
2495 *
2496 * This implementation uses a 1500ms total debounce timeout; if the
2497 * connection isn't stable by then it returns -ETIMEDOUT. It checks
2498 * every 25ms for transient disconnects. When the port status has been
2499 * unchanged for 100ms it returns the port status.
2500 */
1da177e4
LT
2501static int hub_port_debounce(struct usb_hub *hub, int port1)
2502{
2503 int ret;
2504 int total_time, stable_time = 0;
2505 u16 portchange, portstatus;
2506 unsigned connection = 0xffff;
2507
2508 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
2509 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2510 if (ret < 0)
2511 return ret;
2512
2513 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
2514 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
2515 stable_time += HUB_DEBOUNCE_STEP;
2516 if (stable_time >= HUB_DEBOUNCE_STABLE)
2517 break;
2518 } else {
2519 stable_time = 0;
2520 connection = portstatus & USB_PORT_STAT_CONNECTION;
2521 }
2522
2523 if (portchange & USB_PORT_STAT_C_CONNECTION) {
2524 clear_port_feature(hub->hdev, port1,
2525 USB_PORT_FEAT_C_CONNECTION);
2526 }
2527
2528 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
2529 break;
2530 msleep(HUB_DEBOUNCE_STEP);
2531 }
2532
2533 dev_dbg (hub->intfdev,
2534 "debounce: port %d: total %dms stable %dms status 0x%x\n",
2535 port1, total_time, stable_time, portstatus);
2536
2537 if (stable_time < HUB_DEBOUNCE_STABLE)
2538 return -ETIMEDOUT;
2539 return portstatus;
2540}
2541
fc721f51 2542void usb_ep0_reinit(struct usb_device *udev)
1da177e4 2543{
ddeac4e7
AS
2544 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
2545 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
2caf7fcd 2546 usb_enable_endpoint(udev, &udev->ep0, true);
1da177e4 2547}
fc721f51 2548EXPORT_SYMBOL_GPL(usb_ep0_reinit);
1da177e4
LT
2549
2550#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
2551#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
2552
4326ed0b 2553static int hub_set_address(struct usb_device *udev, int devnum)
1da177e4
LT
2554{
2555 int retval;
c6515272 2556 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1da177e4 2557
c6515272
SS
2558 /*
2559 * The host controller will choose the device address,
2560 * instead of the core having chosen it earlier
2561 */
2562 if (!hcd->driver->address_device && devnum <= 1)
1da177e4
LT
2563 return -EINVAL;
2564 if (udev->state == USB_STATE_ADDRESS)
2565 return 0;
2566 if (udev->state != USB_STATE_DEFAULT)
2567 return -EINVAL;
c6515272
SS
2568 if (hcd->driver->address_device) {
2569 retval = hcd->driver->address_device(hcd, udev);
2570 } else {
2571 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
2572 USB_REQ_SET_ADDRESS, 0, devnum, 0,
2573 NULL, 0, USB_CTRL_SET_TIMEOUT);
2574 if (retval == 0)
2575 update_address(udev, devnum);
2576 }
1da177e4 2577 if (retval == 0) {
4953d141 2578 /* Device now using proper address. */
1da177e4 2579 usb_set_device_state(udev, USB_STATE_ADDRESS);
fc721f51 2580 usb_ep0_reinit(udev);
1da177e4
LT
2581 }
2582 return retval;
2583}
2584
2585/* Reset device, (re)assign address, get device descriptor.
2586 * Device connection must be stable, no more debouncing needed.
2587 * Returns device in USB_STATE_ADDRESS, except on error.
2588 *
2589 * If this is called for an already-existing device (as part of
742120c6 2590 * usb_reset_and_verify_device), the caller must own the device lock. For a
1da177e4
LT
2591 * newly detected device that is not accessible through any global
2592 * pointers, it's not necessary to lock the device.
2593 */
2594static int
2595hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2596 int retry_counter)
2597{
4186ecf8 2598 static DEFINE_MUTEX(usb_address0_mutex);
1da177e4
LT
2599
2600 struct usb_device *hdev = hub->hdev;
e7b77172 2601 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
1da177e4
LT
2602 int i, j, retval;
2603 unsigned delay = HUB_SHORT_RESET_TIME;
2604 enum usb_device_speed oldspeed = udev->speed;
83a07196 2605 char *speed, *type;
4326ed0b 2606 int devnum = udev->devnum;
1da177e4
LT
2607
2608 /* root hub ports have a slightly longer reset period
2609 * (from USB 2.0 spec, section 7.1.7.5)
2610 */
2611 if (!hdev->parent) {
2612 delay = HUB_ROOT_RESET_TIME;
2613 if (port1 == hdev->bus->otg_port)
2614 hdev->bus->b_hnp_enable = 0;
2615 }
2616
2617 /* Some low speed devices have problems with the quick delay, so */
2618 /* be a bit pessimistic with those devices. RHbug #23670 */
2619 if (oldspeed == USB_SPEED_LOW)
2620 delay = HUB_LONG_RESET_TIME;
2621
4186ecf8 2622 mutex_lock(&usb_address0_mutex);
1da177e4 2623
e7b77172
SS
2624 if ((hcd->driver->flags & HCD_USB3) && udev->config) {
2625 /* FIXME this will need special handling by the xHCI driver. */
2626 dev_dbg(&udev->dev,
2627 "xHCI reset of configured device "
2628 "not supported yet.\n");
2629 retval = -EINVAL;
1da177e4 2630 goto fail;
e7b77172
SS
2631 } else if (!udev->config && oldspeed == USB_SPEED_SUPER) {
2632 /* Don't reset USB 3.0 devices during an initial setup */
2633 usb_set_device_state(udev, USB_STATE_DEFAULT);
2634 } else {
2635 /* Reset the device; full speed may morph to high speed */
2636 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
2637 retval = hub_port_reset(hub, port1, udev, delay);
2638 if (retval < 0) /* error or disconnect */
2639 goto fail;
2640 /* success, speed is known */
2641 }
1da177e4
LT
2642 retval = -ENODEV;
2643
2644 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
2645 dev_dbg(&udev->dev, "device reset changed speed!\n");
2646 goto fail;
2647 }
2648 oldspeed = udev->speed;
4326ed0b 2649
1da177e4
LT
2650 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
2651 * it's fixed size except for full speed devices.
5bb6e0ae
IPG
2652 * For Wireless USB devices, ep0 max packet is always 512 (tho
2653 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
1da177e4
LT
2654 */
2655 switch (udev->speed) {
6b403b02 2656 case USB_SPEED_SUPER:
5bb6e0ae 2657 case USB_SPEED_VARIABLE: /* fixed at 512 */
551509d2 2658 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
5bb6e0ae 2659 break;
1da177e4 2660 case USB_SPEED_HIGH: /* fixed at 64 */
551509d2 2661 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
1da177e4
LT
2662 break;
2663 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
2664 /* to determine the ep0 maxpacket size, try to read
2665 * the device descriptor to get bMaxPacketSize0 and
2666 * then correct our initial guess.
2667 */
551509d2 2668 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
1da177e4
LT
2669 break;
2670 case USB_SPEED_LOW: /* fixed at 8 */
551509d2 2671 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
1da177e4
LT
2672 break;
2673 default:
2674 goto fail;
2675 }
2676
83a07196
IPG
2677 type = "";
2678 switch (udev->speed) {
2679 case USB_SPEED_LOW: speed = "low"; break;
2680 case USB_SPEED_FULL: speed = "full"; break;
2681 case USB_SPEED_HIGH: speed = "high"; break;
6b403b02
SS
2682 case USB_SPEED_SUPER:
2683 speed = "super";
2684 break;
83a07196
IPG
2685 case USB_SPEED_VARIABLE:
2686 speed = "variable";
2687 type = "Wireless ";
2688 break;
2689 default: speed = "?"; break;
2690 }
c6515272
SS
2691 if (udev->speed != USB_SPEED_SUPER)
2692 dev_info(&udev->dev,
2693 "%s %s speed %sUSB device using %s and address %d\n",
2694 (udev->config) ? "reset" : "new", speed, type,
2695 udev->bus->controller->driver->name, devnum);
1da177e4
LT
2696
2697 /* Set up TT records, if needed */
2698 if (hdev->tt) {
2699 udev->tt = hdev->tt;
2700 udev->ttport = hdev->ttport;
2701 } else if (udev->speed != USB_SPEED_HIGH
2702 && hdev->speed == USB_SPEED_HIGH) {
2703 udev->tt = &hub->tt;
2704 udev->ttport = port1;
2705 }
2706
2707 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
2708 * Because device hardware and firmware is sometimes buggy in
2709 * this area, and this is how Linux has done it for ages.
2710 * Change it cautiously.
2711 *
2712 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
2713 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
2714 * so it may help with some non-standards-compliant devices.
2715 * Otherwise we start with SET_ADDRESS and then try to read the
2716 * first 8 bytes of the device descriptor to get the ep0 maxpacket
2717 * value.
2718 */
2719 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
c6515272
SS
2720 /*
2721 * An xHCI controller cannot send any packets to a device until
2722 * a set address command successfully completes.
2723 */
2724 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
1da177e4
LT
2725 struct usb_device_descriptor *buf;
2726 int r = 0;
2727
2728#define GET_DESCRIPTOR_BUFSIZE 64
2729 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
2730 if (!buf) {
2731 retval = -ENOMEM;
2732 continue;
2733 }
2734
b89ee19a
AS
2735 /* Retry on all errors; some devices are flakey.
2736 * 255 is for WUSB devices, we actually need to use
2737 * 512 (WUSB1.0[4.8.1]).
1da177e4
LT
2738 */
2739 for (j = 0; j < 3; ++j) {
2740 buf->bMaxPacketSize0 = 0;
2741 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
2742 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
2743 USB_DT_DEVICE << 8, 0,
2744 buf, GET_DESCRIPTOR_BUFSIZE,
fd7c519d 2745 initial_descriptor_timeout);
1da177e4 2746 switch (buf->bMaxPacketSize0) {
5bb6e0ae 2747 case 8: case 16: case 32: case 64: case 255:
1da177e4
LT
2748 if (buf->bDescriptorType ==
2749 USB_DT_DEVICE) {
2750 r = 0;
2751 break;
2752 }
2753 /* FALL THROUGH */
2754 default:
2755 if (r == 0)
2756 r = -EPROTO;
2757 break;
2758 }
2759 if (r == 0)
2760 break;
2761 }
2762 udev->descriptor.bMaxPacketSize0 =
2763 buf->bMaxPacketSize0;
2764 kfree(buf);
2765
2766 retval = hub_port_reset(hub, port1, udev, delay);
2767 if (retval < 0) /* error or disconnect */
2768 goto fail;
2769 if (oldspeed != udev->speed) {
2770 dev_dbg(&udev->dev,
2771 "device reset changed speed!\n");
2772 retval = -ENODEV;
2773 goto fail;
2774 }
2775 if (r) {
b9cef6c3
WF
2776 dev_err(&udev->dev,
2777 "device descriptor read/64, error %d\n",
2778 r);
1da177e4
LT
2779 retval = -EMSGSIZE;
2780 continue;
2781 }
2782#undef GET_DESCRIPTOR_BUFSIZE
2783 }
2784
6c529cdc
IPG
2785 /*
2786 * If device is WUSB, we already assigned an
2787 * unauthorized address in the Connect Ack sequence;
2788 * authorization will assign the final address.
2789 */
c6515272 2790 if (udev->wusb == 0) {
6c529cdc
IPG
2791 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
2792 retval = hub_set_address(udev, devnum);
2793 if (retval >= 0)
2794 break;
2795 msleep(200);
2796 }
2797 if (retval < 0) {
2798 dev_err(&udev->dev,
2799 "device not accepting address %d, error %d\n",
2800 devnum, retval);
2801 goto fail;
2802 }
c6515272
SS
2803 if (udev->speed == USB_SPEED_SUPER) {
2804 devnum = udev->devnum;
2805 dev_info(&udev->dev,
2806 "%s SuperSpeed USB device using %s and address %d\n",
2807 (udev->config) ? "reset" : "new",
2808 udev->bus->controller->driver->name, devnum);
2809 }
6c529cdc
IPG
2810
2811 /* cope with hardware quirkiness:
2812 * - let SET_ADDRESS settle, some device hardware wants it
2813 * - read ep0 maxpacket even for high and low speed,
2814 */
2815 msleep(10);
c6515272 2816 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
1da177e4 2817 break;
6c529cdc 2818 }
1da177e4
LT
2819
2820 retval = usb_get_device_descriptor(udev, 8);
2821 if (retval < 8) {
b9cef6c3
WF
2822 dev_err(&udev->dev,
2823 "device descriptor read/8, error %d\n",
2824 retval);
1da177e4
LT
2825 if (retval >= 0)
2826 retval = -EMSGSIZE;
2827 } else {
2828 retval = 0;
2829 break;
2830 }
2831 }
2832 if (retval)
2833 goto fail;
2834
6b403b02
SS
2835 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
2836 udev->speed == USB_SPEED_SUPER)
2837 i = 512;
2838 else
2839 i = udev->descriptor.bMaxPacketSize0;
1da177e4
LT
2840 if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) {
2841 if (udev->speed != USB_SPEED_FULL ||
2842 !(i == 8 || i == 16 || i == 32 || i == 64)) {
2843 dev_err(&udev->dev, "ep0 maxpacket = %d\n", i);
2844 retval = -EMSGSIZE;
2845 goto fail;
2846 }
2847 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
2848 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
fc721f51 2849 usb_ep0_reinit(udev);
1da177e4
LT
2850 }
2851
2852 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
2853 if (retval < (signed)sizeof(udev->descriptor)) {
b9cef6c3
WF
2854 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
2855 retval);
1da177e4
LT
2856 if (retval >= 0)
2857 retval = -ENOMSG;
2858 goto fail;
2859 }
2860
2861 retval = 0;
2862
2863fail:
4326ed0b 2864 if (retval) {
1da177e4 2865 hub_port_disable(hub, port1, 0);
4953d141 2866 update_address(udev, devnum); /* for disconnect processing */
4326ed0b 2867 }
4186ecf8 2868 mutex_unlock(&usb_address0_mutex);
1da177e4
LT
2869 return retval;
2870}
2871
2872static void
2873check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
2874{
2875 struct usb_qualifier_descriptor *qual;
2876 int status;
2877
e94b1766 2878 qual = kmalloc (sizeof *qual, GFP_KERNEL);
1da177e4
LT
2879 if (qual == NULL)
2880 return;
2881
2882 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
2883 qual, sizeof *qual);
2884 if (status == sizeof *qual) {
2885 dev_info(&udev->dev, "not running at top speed; "
2886 "connect to a high speed hub\n");
2887 /* hub LEDs are probably harder to miss than syslog */
2888 if (hub->has_indicators) {
2889 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
c4028958 2890 schedule_delayed_work (&hub->leds, 0);
1da177e4
LT
2891 }
2892 }
1bc3c9e1 2893 kfree(qual);
1da177e4
LT
2894}
2895
2896static unsigned
2897hub_power_remaining (struct usb_hub *hub)
2898{
2899 struct usb_device *hdev = hub->hdev;
2900 int remaining;
55c52718 2901 int port1;
1da177e4 2902
55c52718 2903 if (!hub->limited_power)
1da177e4
LT
2904 return 0;
2905
55c52718
AS
2906 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
2907 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
2908 struct usb_device *udev = hdev->children[port1 - 1];
2909 int delta;
1da177e4
LT
2910
2911 if (!udev)
2912 continue;
2913
55c52718
AS
2914 /* Unconfigured devices may not use more than 100mA,
2915 * or 8mA for OTG ports */
1da177e4 2916 if (udev->actconfig)
55c52718
AS
2917 delta = udev->actconfig->desc.bMaxPower * 2;
2918 else if (port1 != udev->bus->otg_port || hdev->parent)
2919 delta = 100;
1da177e4 2920 else
55c52718
AS
2921 delta = 8;
2922 if (delta > hub->mA_per_port)
b9cef6c3
WF
2923 dev_warn(&udev->dev,
2924 "%dmA is over %umA budget for port %d!\n",
2925 delta, hub->mA_per_port, port1);
1da177e4
LT
2926 remaining -= delta;
2927 }
2928 if (remaining < 0) {
55c52718
AS
2929 dev_warn(hub->intfdev, "%dmA over power budget!\n",
2930 - remaining);
1da177e4
LT
2931 remaining = 0;
2932 }
2933 return remaining;
2934}
2935
2936/* Handle physical or logical connection change events.
2937 * This routine is called when:
2938 * a port connection-change occurs;
2939 * a port enable-change occurs (often caused by EMI);
742120c6 2940 * usb_reset_and_verify_device() encounters changed descriptors (as from
1da177e4
LT
2941 * a firmware download)
2942 * caller already locked the hub
2943 */
2944static void hub_port_connect_change(struct usb_hub *hub, int port1,
2945 u16 portstatus, u16 portchange)
2946{
2947 struct usb_device *hdev = hub->hdev;
2948 struct device *hub_dev = hub->intfdev;
90da096e 2949 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
24618b0c
AS
2950 unsigned wHubCharacteristics =
2951 le16_to_cpu(hub->descriptor->wHubCharacteristics);
8808f00c 2952 struct usb_device *udev;
1da177e4 2953 int status, i;
24618b0c 2954
1da177e4
LT
2955 dev_dbg (hub_dev,
2956 "port %d, status %04x, change %04x, %s\n",
2957 port1, portstatus, portchange, portspeed (portstatus));
2958
2959 if (hub->has_indicators) {
2960 set_port_led(hub, port1, HUB_LED_AUTO);
2961 hub->indicator[port1-1] = INDICATOR_AUTO;
2962 }
1da177e4
LT
2963
2964#ifdef CONFIG_USB_OTG
2965 /* during HNP, don't repeat the debounce */
2966 if (hdev->bus->is_b_host)
24618b0c
AS
2967 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
2968 USB_PORT_STAT_C_ENABLE);
1da177e4
LT
2969#endif
2970
8808f00c
AS
2971 /* Try to resuscitate an existing device */
2972 udev = hdev->children[port1-1];
2973 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
2974 udev->state != USB_STATE_NOTATTACHED) {
8808f00c
AS
2975 usb_lock_device(udev);
2976 if (portstatus & USB_PORT_STAT_ENABLE) {
2977 status = 0; /* Nothing to do */
8808f00c
AS
2978
2979#ifdef CONFIG_USB_SUSPEND
5257d97a
AS
2980 } else if (udev->state == USB_STATE_SUSPENDED &&
2981 udev->persist_enabled) {
8808f00c
AS
2982 /* For a suspended device, treat this as a
2983 * remote wakeup event.
2984 */
527101ce 2985 status = remote_wakeup(udev);
8808f00c
AS
2986#endif
2987
2988 } else {
5257d97a 2989 status = -ENODEV; /* Don't resuscitate */
8808f00c
AS
2990 }
2991 usb_unlock_device(udev);
2992
2993 if (status == 0) {
2994 clear_bit(port1, hub->change_bits);
2995 return;
2996 }
2997 }
2998
24618b0c 2999 /* Disconnect any existing devices under this port */
8808f00c 3000 if (udev)
24618b0c
AS
3001 usb_disconnect(&hdev->children[port1-1]);
3002 clear_bit(port1, hub->change_bits);
3003
253e0572
AS
3004 /* We can forget about a "removed" device when there's a physical
3005 * disconnect or the connect status changes.
3006 */
3007 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3008 (portchange & USB_PORT_STAT_C_CONNECTION))
3009 clear_bit(port1, hub->removed_bits);
3010
5257d97a
AS
3011 if (portchange & (USB_PORT_STAT_C_CONNECTION |
3012 USB_PORT_STAT_C_ENABLE)) {
3013 status = hub_port_debounce(hub, port1);
3014 if (status < 0) {
3015 if (printk_ratelimit())
3016 dev_err(hub_dev, "connect-debounce failed, "
3017 "port %d disabled\n", port1);
3018 portstatus &= ~USB_PORT_STAT_CONNECTION;
3019 } else {
3020 portstatus = status;
3021 }
3022 }
3023
253e0572
AS
3024 /* Return now if debouncing failed or nothing is connected or
3025 * the device was "removed".
3026 */
3027 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3028 test_bit(port1, hub->removed_bits)) {
1da177e4
LT
3029
3030 /* maybe switch power back on (e.g. root hub was reset) */
74ad9bd2 3031 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
1da177e4
LT
3032 && !(portstatus & (1 << USB_PORT_FEAT_POWER)))
3033 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
5257d97a 3034
1da177e4
LT
3035 if (portstatus & USB_PORT_STAT_ENABLE)
3036 goto done;
3037 return;
3038 }
3039
1da177e4 3040 for (i = 0; i < SET_CONFIG_TRIES; i++) {
1da177e4
LT
3041
3042 /* reallocate for each attempt, since references
3043 * to the previous one can escape in various ways
3044 */
3045 udev = usb_alloc_dev(hdev, hdev->bus, port1);
3046 if (!udev) {
3047 dev_err (hub_dev,
3048 "couldn't allocate port %d usb_device\n",
3049 port1);
3050 goto done;
3051 }
3052
3053 usb_set_device_state(udev, USB_STATE_POWERED);
55c52718 3054 udev->bus_mA = hub->mA_per_port;
b6956ffa 3055 udev->level = hdev->level + 1;
8af548dc 3056 udev->wusb = hub_is_wusb(hub);
55c52718 3057
e7b77172
SS
3058 /*
3059 * USB 3.0 devices are reset automatically before the connect
3060 * port status change appears, and the root hub port status
3061 * shows the correct speed. We also get port change
3062 * notifications for USB 3.0 devices from the USB 3.0 portion of
3063 * an external USB 3.0 hub, but this isn't handled correctly yet
3064 * FIXME.
3065 */
3066
3067 if (!(hcd->driver->flags & HCD_USB3))
3068 udev->speed = USB_SPEED_UNKNOWN;
3069 else if ((hdev->parent == NULL) &&
3070 (portstatus & (1 << USB_PORT_FEAT_SUPERSPEED)))
3071 udev->speed = USB_SPEED_SUPER;
3072 else
3073 udev->speed = USB_SPEED_UNKNOWN;
3074
c6515272
SS
3075 /*
3076 * xHCI needs to issue an address device command later
3077 * in the hub_port_init sequence for SS/HS/FS/LS devices.
3078 */
3079 if (!(hcd->driver->flags & HCD_USB3)) {
3080 /* set the address */
3081 choose_address(udev);
3082 if (udev->devnum <= 0) {
3083 status = -ENOTCONN; /* Don't retry */
3084 goto loop;
3085 }
3086 }
3087
e7b77172 3088 /* reset (non-USB 3.0 devices) and get descriptor */
1da177e4
LT
3089 status = hub_port_init(hub, udev, port1, i);
3090 if (status < 0)
3091 goto loop;
3092
3093 /* consecutive bus-powered hubs aren't reliable; they can
3094 * violate the voltage drop budget. if the new child has
3095 * a "powered" LED, users should notice we didn't enable it
3096 * (without reading syslog), even without per-port LEDs
3097 * on the parent.
3098 */
3099 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
55c52718 3100 && udev->bus_mA <= 100) {
1da177e4
LT
3101 u16 devstat;
3102
3103 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
3104 &devstat);
55c52718 3105 if (status < 2) {
1da177e4
LT
3106 dev_dbg(&udev->dev, "get status %d ?\n", status);
3107 goto loop_disable;
3108 }
55c52718 3109 le16_to_cpus(&devstat);
1da177e4
LT
3110 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
3111 dev_err(&udev->dev,
3112 "can't connect bus-powered hub "
3113 "to this port\n");
3114 if (hub->has_indicators) {
3115 hub->indicator[port1-1] =
3116 INDICATOR_AMBER_BLINK;
c4028958 3117 schedule_delayed_work (&hub->leds, 0);
1da177e4
LT
3118 }
3119 status = -ENOTCONN; /* Don't retry */
3120 goto loop_disable;
3121 }
3122 }
3123
3124 /* check for devices running slower than they could */
3125 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
3126 && udev->speed == USB_SPEED_FULL
3127 && highspeed_hubs != 0)
3128 check_highspeed (hub, udev, port1);
3129
3130 /* Store the parent's children[] pointer. At this point
3131 * udev becomes globally accessible, although presumably
3132 * no one will look at it until hdev is unlocked.
3133 */
1da177e4
LT
3134 status = 0;
3135
3136 /* We mustn't add new devices if the parent hub has
3137 * been disconnected; we would race with the
3138 * recursively_mark_NOTATTACHED() routine.
3139 */
3140 spin_lock_irq(&device_state_lock);
3141 if (hdev->state == USB_STATE_NOTATTACHED)
3142 status = -ENOTCONN;
3143 else
3144 hdev->children[port1-1] = udev;
3145 spin_unlock_irq(&device_state_lock);
3146
3147 /* Run it through the hoops (find a driver, etc) */
3148 if (!status) {
3149 status = usb_new_device(udev);
3150 if (status) {
3151 spin_lock_irq(&device_state_lock);
3152 hdev->children[port1-1] = NULL;
3153 spin_unlock_irq(&device_state_lock);
3154 }
3155 }
3156
1da177e4
LT
3157 if (status)
3158 goto loop_disable;
3159
3160 status = hub_power_remaining(hub);
3161 if (status)
55c52718 3162 dev_dbg(hub_dev, "%dmA power budget left\n", status);
1da177e4
LT
3163
3164 return;
3165
3166loop_disable:
3167 hub_port_disable(hub, port1, 1);
3168loop:
fc721f51 3169 usb_ep0_reinit(udev);
1da177e4
LT
3170 release_address(udev);
3171 usb_put_dev(udev);
ffcdc18d 3172 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
1da177e4
LT
3173 break;
3174 }
3a31155c
AS
3175 if (hub->hdev->parent ||
3176 !hcd->driver->port_handed_over ||
3177 !(hcd->driver->port_handed_over)(hcd, port1))
3178 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
3179 port1);
1da177e4
LT
3180
3181done:
3182 hub_port_disable(hub, port1, 1);
90da096e
BR
3183 if (hcd->driver->relinquish_port && !hub->hdev->parent)
3184 hcd->driver->relinquish_port(hcd, port1);
1da177e4
LT
3185}
3186
3187static void hub_events(void)
3188{
3189 struct list_head *tmp;
3190 struct usb_device *hdev;
3191 struct usb_interface *intf;
3192 struct usb_hub *hub;
3193 struct device *hub_dev;
3194 u16 hubstatus;
3195 u16 hubchange;
3196 u16 portstatus;
3197 u16 portchange;
3198 int i, ret;
3199 int connect_change;
3200
3201 /*
3202 * We restart the list every time to avoid a deadlock with
3203 * deleting hubs downstream from this one. This should be
3204 * safe since we delete the hub from the event list.
3205 * Not the most efficient, but avoids deadlocks.
3206 */
3207 while (1) {
3208
3209 /* Grab the first entry at the beginning of the list */
3210 spin_lock_irq(&hub_event_lock);
3211 if (list_empty(&hub_event_list)) {
3212 spin_unlock_irq(&hub_event_lock);
3213 break;
3214 }
3215
3216 tmp = hub_event_list.next;
3217 list_del_init(tmp);
3218
3219 hub = list_entry(tmp, struct usb_hub, event_list);
e8054854
AS
3220 kref_get(&hub->kref);
3221 spin_unlock_irq(&hub_event_lock);
1da177e4 3222
e8054854
AS
3223 hdev = hub->hdev;
3224 hub_dev = hub->intfdev;
3225 intf = to_usb_interface(hub_dev);
40f122f3 3226 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
1da177e4
LT
3227 hdev->state, hub->descriptor
3228 ? hub->descriptor->bNbrPorts
3229 : 0,
3230 /* NOTE: expects max 15 ports... */
3231 (u16) hub->change_bits[0],
40f122f3 3232 (u16) hub->event_bits[0]);
1da177e4 3233
1da177e4
LT
3234 /* Lock the device, then check to see if we were
3235 * disconnected while waiting for the lock to succeed. */
06b84e8a 3236 usb_lock_device(hdev);
e8054854 3237 if (unlikely(hub->disconnected))
1da177e4
LT
3238 goto loop;
3239
3240 /* If the hub has died, clean up after it */
3241 if (hdev->state == USB_STATE_NOTATTACHED) {
7de18d8b 3242 hub->error = -ENODEV;
4330354f 3243 hub_quiesce(hub, HUB_DISCONNECT);
1da177e4
LT
3244 goto loop;
3245 }
3246
40f122f3
AS
3247 /* Autoresume */
3248 ret = usb_autopm_get_interface(intf);
3249 if (ret) {
3250 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
3251 goto loop;
3252 }
a8e7c565 3253
40f122f3 3254 /* If this is an inactive hub, do nothing */
1da177e4 3255 if (hub->quiescing)
40f122f3 3256 goto loop_autopm;
1da177e4
LT
3257
3258 if (hub->error) {
3259 dev_dbg (hub_dev, "resetting for error %d\n",
3260 hub->error);
3261
742120c6 3262 ret = usb_reset_device(hdev);
1da177e4
LT
3263 if (ret) {
3264 dev_dbg (hub_dev,
3265 "error resetting hub: %d\n", ret);
40f122f3 3266 goto loop_autopm;
1da177e4
LT
3267 }
3268
3269 hub->nerrors = 0;
3270 hub->error = 0;
3271 }
3272
3273 /* deal with port status changes */
3274 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
3275 if (test_bit(i, hub->busy_bits))
3276 continue;
3277 connect_change = test_bit(i, hub->change_bits);
3278 if (!test_and_clear_bit(i, hub->event_bits) &&
6ee0b270 3279 !connect_change)
1da177e4
LT
3280 continue;
3281
3282 ret = hub_port_status(hub, i,
3283 &portstatus, &portchange);
3284 if (ret < 0)
3285 continue;
3286
1da177e4
LT
3287 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3288 clear_port_feature(hdev, i,
3289 USB_PORT_FEAT_C_CONNECTION);
3290 connect_change = 1;
3291 }
3292
3293 if (portchange & USB_PORT_STAT_C_ENABLE) {
3294 if (!connect_change)
3295 dev_dbg (hub_dev,
3296 "port %d enable change, "
3297 "status %08x\n",
3298 i, portstatus);
3299 clear_port_feature(hdev, i,
3300 USB_PORT_FEAT_C_ENABLE);
3301
3302 /*
3303 * EM interference sometimes causes badly
3304 * shielded USB devices to be shutdown by
3305 * the hub, this hack enables them again.
3306 * Works at least with mouse driver.
3307 */
3308 if (!(portstatus & USB_PORT_STAT_ENABLE)
3309 && !connect_change
3310 && hdev->children[i-1]) {
3311 dev_err (hub_dev,
3312 "port %i "
3313 "disabled by hub (EMI?), "
3314 "re-enabling...\n",
3315 i);
3316 connect_change = 1;
3317 }
3318 }
3319
3320 if (portchange & USB_PORT_STAT_C_SUSPEND) {
8808f00c
AS
3321 struct usb_device *udev;
3322
1da177e4
LT
3323 clear_port_feature(hdev, i,
3324 USB_PORT_FEAT_C_SUSPEND);
8808f00c
AS
3325 udev = hdev->children[i-1];
3326 if (udev) {
3327 usb_lock_device(udev);
1da177e4
LT
3328 ret = remote_wakeup(hdev->
3329 children[i-1]);
8808f00c 3330 usb_unlock_device(udev);
1da177e4
LT
3331 if (ret < 0)
3332 connect_change = 1;
3333 } else {
3334 ret = -ENODEV;
3335 hub_port_disable(hub, i, 1);
3336 }
3337 dev_dbg (hub_dev,
3338 "resume on port %d, status %d\n",
3339 i, ret);
3340 }
3341
3342 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
3343 dev_err (hub_dev,
3344 "over-current change on port %d\n",
3345 i);
3346 clear_port_feature(hdev, i,
3347 USB_PORT_FEAT_C_OVER_CURRENT);
8520f380 3348 hub_power_on(hub, true);
1da177e4
LT
3349 }
3350
3351 if (portchange & USB_PORT_STAT_C_RESET) {
3352 dev_dbg (hub_dev,
3353 "reset change on port %d\n",
3354 i);
3355 clear_port_feature(hdev, i,
3356 USB_PORT_FEAT_C_RESET);
3357 }
3358
3359 if (connect_change)
3360 hub_port_connect_change(hub, i,
3361 portstatus, portchange);
3362 } /* end for i */
3363
3364 /* deal with hub status changes */
3365 if (test_and_clear_bit(0, hub->event_bits) == 0)
3366 ; /* do nothing */
3367 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
3368 dev_err (hub_dev, "get_hub_status failed\n");
3369 else {
3370 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
3371 dev_dbg (hub_dev, "power change\n");
3372 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
55c52718
AS
3373 if (hubstatus & HUB_STATUS_LOCAL_POWER)
3374 /* FIXME: Is this always true? */
55c52718 3375 hub->limited_power = 1;
403fae78 3376 else
3377 hub->limited_power = 0;
1da177e4
LT
3378 }
3379 if (hubchange & HUB_CHANGE_OVERCURRENT) {
3380 dev_dbg (hub_dev, "overcurrent change\n");
3381 msleep(500); /* Cool down */
3382 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
8520f380 3383 hub_power_on(hub, true);
1da177e4
LT
3384 }
3385 }
3386
40f122f3
AS
3387loop_autopm:
3388 /* Allow autosuspend if we're not going to run again */
3389 if (list_empty(&hub->event_list))
3390 usb_autopm_enable(intf);
1da177e4
LT
3391loop:
3392 usb_unlock_device(hdev);
e8054854 3393 kref_put(&hub->kref, hub_release);
1da177e4
LT
3394
3395 } /* end while (1) */
3396}
3397
3398static int hub_thread(void *__unused)
3399{
3bb1af52
AS
3400 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
3401 * port handover. Otherwise it might see that a full-speed device
3402 * was gone before the EHCI controller had handed its port over to
3403 * the companion full-speed controller.
3404 */
83144186 3405 set_freezable();
3bb1af52 3406
1da177e4
LT
3407 do {
3408 hub_events();
e42837bc 3409 wait_event_freezable(khubd_wait,
9c8d6178 3410 !list_empty(&hub_event_list) ||
3411 kthread_should_stop());
9c8d6178 3412 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
1da177e4 3413
9c8d6178 3414 pr_debug("%s: khubd exiting\n", usbcore_name);
3415 return 0;
1da177e4
LT
3416}
3417
3418static struct usb_device_id hub_id_table [] = {
3419 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
3420 .bDeviceClass = USB_CLASS_HUB},
3421 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
3422 .bInterfaceClass = USB_CLASS_HUB},
3423 { } /* Terminating entry */
3424};
3425
3426MODULE_DEVICE_TABLE (usb, hub_id_table);
3427
3428static struct usb_driver hub_driver = {
1da177e4
LT
3429 .name = "hub",
3430 .probe = hub_probe,
3431 .disconnect = hub_disconnect,
3432 .suspend = hub_suspend,
3433 .resume = hub_resume,
f07600cf 3434 .reset_resume = hub_reset_resume,
7de18d8b
AS
3435 .pre_reset = hub_pre_reset,
3436 .post_reset = hub_post_reset,
1da177e4
LT
3437 .ioctl = hub_ioctl,
3438 .id_table = hub_id_table,
40f122f3 3439 .supports_autosuspend = 1,
1da177e4
LT
3440};
3441
3442int usb_hub_init(void)
3443{
1da177e4
LT
3444 if (usb_register(&hub_driver) < 0) {
3445 printk(KERN_ERR "%s: can't register hub driver\n",
3446 usbcore_name);
3447 return -1;
3448 }
3449
9c8d6178 3450 khubd_task = kthread_run(hub_thread, NULL, "khubd");
3451 if (!IS_ERR(khubd_task))
1da177e4 3452 return 0;
1da177e4
LT
3453
3454 /* Fall through if kernel_thread failed */
3455 usb_deregister(&hub_driver);
3456 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
3457
3458 return -1;
3459}
3460
3461void usb_hub_cleanup(void)
3462{
9c8d6178 3463 kthread_stop(khubd_task);
1da177e4
LT
3464
3465 /*
3466 * Hub resources are freed for us by usb_deregister. It calls
3467 * usb_driver_purge on every device which in turn calls that
3468 * devices disconnect function if it is using this driver.
3469 * The hub_disconnect function takes care of releasing the
3470 * individual hub resources. -greg
3471 */
3472 usb_deregister(&hub_driver);
3473} /* usb_hub_cleanup() */
3474
eb764c4b
AS
3475static int descriptors_changed(struct usb_device *udev,
3476 struct usb_device_descriptor *old_device_descriptor)
3477{
3478 int changed = 0;
3479 unsigned index;
3480 unsigned serial_len = 0;
3481 unsigned len;
3482 unsigned old_length;
3483 int length;
3484 char *buf;
3485
3486 if (memcmp(&udev->descriptor, old_device_descriptor,
3487 sizeof(*old_device_descriptor)) != 0)
3488 return 1;
3489
3490 /* Since the idVendor, idProduct, and bcdDevice values in the
3491 * device descriptor haven't changed, we will assume the
3492 * Manufacturer and Product strings haven't changed either.
3493 * But the SerialNumber string could be different (e.g., a
3494 * different flash card of the same brand).
3495 */
3496 if (udev->serial)
3497 serial_len = strlen(udev->serial) + 1;
1da177e4 3498
eb764c4b 3499 len = serial_len;
1da177e4 3500 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
eb764c4b
AS
3501 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
3502 len = max(len, old_length);
1da177e4 3503 }
eb764c4b 3504
0cc1a51f 3505 buf = kmalloc(len, GFP_NOIO);
1da177e4
LT
3506 if (buf == NULL) {
3507 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
3508 /* assume the worst */
3509 return 1;
3510 }
3511 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
eb764c4b 3512 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
1da177e4
LT
3513 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
3514 old_length);
eb764c4b 3515 if (length != old_length) {
1da177e4
LT
3516 dev_dbg(&udev->dev, "config index %d, error %d\n",
3517 index, length);
eb764c4b 3518 changed = 1;
1da177e4
LT
3519 break;
3520 }
3521 if (memcmp (buf, udev->rawdescriptors[index], old_length)
3522 != 0) {
3523 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
eb764c4b
AS
3524 index,
3525 ((struct usb_config_descriptor *) buf)->
3526 bConfigurationValue);
3527 changed = 1;
1da177e4
LT
3528 break;
3529 }
3530 }
eb764c4b
AS
3531
3532 if (!changed && serial_len) {
3533 length = usb_string(udev, udev->descriptor.iSerialNumber,
3534 buf, serial_len);
3535 if (length + 1 != serial_len) {
3536 dev_dbg(&udev->dev, "serial string error %d\n",
3537 length);
3538 changed = 1;
3539 } else if (memcmp(buf, udev->serial, length) != 0) {
3540 dev_dbg(&udev->dev, "serial string changed\n");
3541 changed = 1;
3542 }
3543 }
3544
1da177e4 3545 kfree(buf);
eb764c4b 3546 return changed;
1da177e4
LT
3547}
3548
3549/**
742120c6 3550 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
1da177e4
LT
3551 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
3552 *
79efa097
AS
3553 * WARNING - don't use this routine to reset a composite device
3554 * (one with multiple interfaces owned by separate drivers)!
742120c6 3555 * Use usb_reset_device() instead.
1da177e4
LT
3556 *
3557 * Do a port reset, reassign the device's address, and establish its
3558 * former operating configuration. If the reset fails, or the device's
3559 * descriptors change from their values before the reset, or the original
3560 * configuration and altsettings cannot be restored, a flag will be set
3561 * telling khubd to pretend the device has been disconnected and then
3562 * re-connected. All drivers will be unbound, and the device will be
3563 * re-enumerated and probed all over again.
3564 *
3565 * Returns 0 if the reset succeeded, -ENODEV if the device has been
3566 * flagged for logical disconnection, or some other negative error code
3567 * if the reset wasn't even attempted.
3568 *
3569 * The caller must own the device lock. For example, it's safe to use
3570 * this from a driver probe() routine after downloading new firmware.
3571 * For calls that might not occur during probe(), drivers should lock
3572 * the device using usb_lock_device_for_reset().
6bc6cff5
AS
3573 *
3574 * Locking exception: This routine may also be called from within an
3575 * autoresume handler. Such usage won't conflict with other tasks
3576 * holding the device lock because these tasks should always call
3577 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
1da177e4 3578 */
742120c6 3579static int usb_reset_and_verify_device(struct usb_device *udev)
1da177e4
LT
3580{
3581 struct usb_device *parent_hdev = udev->parent;
3582 struct usb_hub *parent_hub;
3583 struct usb_device_descriptor descriptor = udev->descriptor;
12c3da34
AS
3584 int i, ret = 0;
3585 int port1 = udev->portnum;
1da177e4
LT
3586
3587 if (udev->state == USB_STATE_NOTATTACHED ||
3588 udev->state == USB_STATE_SUSPENDED) {
3589 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
3590 udev->state);
3591 return -EINVAL;
3592 }
3593
3594 if (!parent_hdev) {
3595 /* this requires hcd-specific logic; see OHCI hc_restart() */
441b62c1 3596 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
1da177e4
LT
3597 return -EISDIR;
3598 }
1da177e4
LT
3599 parent_hub = hdev_to_hub(parent_hdev);
3600
1da177e4
LT
3601 set_bit(port1, parent_hub->busy_bits);
3602 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
3603
3604 /* ep0 maxpacket size may change; let the HCD know about it.
3605 * Other endpoints will be handled by re-enumeration. */
fc721f51 3606 usb_ep0_reinit(udev);
1da177e4 3607 ret = hub_port_init(parent_hub, udev, port1, i);
dd4dd19e 3608 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
1da177e4
LT
3609 break;
3610 }
3611 clear_bit(port1, parent_hub->busy_bits);
d5cbad4b 3612
1da177e4
LT
3613 if (ret < 0)
3614 goto re_enumerate;
3615
3616 /* Device might have changed firmware (DFU or similar) */
eb764c4b 3617 if (descriptors_changed(udev, &descriptor)) {
1da177e4
LT
3618 dev_info(&udev->dev, "device firmware changed\n");
3619 udev->descriptor = descriptor; /* for disconnect() calls */
3620 goto re_enumerate;
3621 }
4fe0387a
AS
3622
3623 /* Restore the device's previous configuration */
1da177e4
LT
3624 if (!udev->actconfig)
3625 goto done;
1da177e4
LT
3626 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3627 USB_REQ_SET_CONFIGURATION, 0,
3628 udev->actconfig->desc.bConfigurationValue, 0,
3629 NULL, 0, USB_CTRL_SET_TIMEOUT);
3630 if (ret < 0) {
3631 dev_err(&udev->dev,
3632 "can't restore configuration #%d (error=%d)\n",
3633 udev->actconfig->desc.bConfigurationValue, ret);
3634 goto re_enumerate;
3635 }
3636 usb_set_device_state(udev, USB_STATE_CONFIGURED);
3637
4fe0387a
AS
3638 /* Put interfaces back into the same altsettings as before.
3639 * Don't bother to send the Set-Interface request for interfaces
3640 * that were already in altsetting 0; besides being unnecessary,
3641 * many devices can't handle it. Instead just reset the host-side
3642 * endpoint state.
3643 */
1da177e4
LT
3644 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
3645 struct usb_interface *intf = udev->actconfig->interface[i];
3646 struct usb_interface_descriptor *desc;
3647
1da177e4 3648 desc = &intf->cur_altsetting->desc;
4fe0387a
AS
3649 if (desc->bAlternateSetting == 0) {
3650 usb_disable_interface(udev, intf, true);
3651 usb_enable_interface(udev, intf, true);
3652 ret = 0;
3653 } else {
3654 ret = usb_set_interface(udev, desc->bInterfaceNumber,
3655 desc->bAlternateSetting);
3656 }
1da177e4
LT
3657 if (ret < 0) {
3658 dev_err(&udev->dev, "failed to restore interface %d "
3659 "altsetting %d (error=%d)\n",
3660 desc->bInterfaceNumber,
3661 desc->bAlternateSetting,
3662 ret);
3663 goto re_enumerate;
3664 }
3665 }
3666
3667done:
1da177e4
LT
3668 return 0;
3669
3670re_enumerate:
3671 hub_port_logical_disconnect(parent_hub, port1);
3672 return -ENODEV;
3673}
79efa097
AS
3674
3675/**
742120c6 3676 * usb_reset_device - warn interface drivers and perform a USB port reset
79efa097 3677 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
79efa097
AS
3678 *
3679 * Warns all drivers bound to registered interfaces (using their pre_reset
3680 * method), performs the port reset, and then lets the drivers know that
3681 * the reset is over (using their post_reset method).
3682 *
742120c6 3683 * Return value is the same as for usb_reset_and_verify_device().
79efa097
AS
3684 *
3685 * The caller must own the device lock. For example, it's safe to use
3686 * this from a driver probe() routine after downloading new firmware.
3687 * For calls that might not occur during probe(), drivers should lock
3688 * the device using usb_lock_device_for_reset().
78d9a487
AS
3689 *
3690 * If an interface is currently being probed or disconnected, we assume
3691 * its driver knows how to handle resets. For all other interfaces,
3692 * if the driver doesn't have pre_reset and post_reset methods then
3693 * we attempt to unbind it and rebind afterward.
79efa097 3694 */
742120c6 3695int usb_reset_device(struct usb_device *udev)
79efa097
AS
3696{
3697 int ret;
852c4b43 3698 int i;
79efa097
AS
3699 struct usb_host_config *config = udev->actconfig;
3700
3701 if (udev->state == USB_STATE_NOTATTACHED ||
3702 udev->state == USB_STATE_SUSPENDED) {
3703 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
3704 udev->state);
3705 return -EINVAL;
3706 }
3707
645daaab 3708 /* Prevent autosuspend during the reset */
94fcda1f 3709 usb_autoresume_device(udev);
645daaab 3710
79efa097 3711 if (config) {
79efa097 3712 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
852c4b43
AS
3713 struct usb_interface *cintf = config->interface[i];
3714 struct usb_driver *drv;
78d9a487 3715 int unbind = 0;
852c4b43
AS
3716
3717 if (cintf->dev.driver) {
79efa097 3718 drv = to_usb_driver(cintf->dev.driver);
78d9a487
AS
3719 if (drv->pre_reset && drv->post_reset)
3720 unbind = (drv->pre_reset)(cintf);
3721 else if (cintf->condition ==
3722 USB_INTERFACE_BOUND)
3723 unbind = 1;
3724 if (unbind)
3725 usb_forced_unbind_intf(cintf);
79efa097
AS
3726 }
3727 }
3728 }
3729
742120c6 3730 ret = usb_reset_and_verify_device(udev);
79efa097
AS
3731
3732 if (config) {
79efa097 3733 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
852c4b43
AS
3734 struct usb_interface *cintf = config->interface[i];
3735 struct usb_driver *drv;
78d9a487 3736 int rebind = cintf->needs_binding;
852c4b43 3737
78d9a487 3738 if (!rebind && cintf->dev.driver) {
79efa097
AS
3739 drv = to_usb_driver(cintf->dev.driver);
3740 if (drv->post_reset)
78d9a487
AS
3741 rebind = (drv->post_reset)(cintf);
3742 else if (cintf->condition ==
3743 USB_INTERFACE_BOUND)
3744 rebind = 1;
79efa097 3745 }
6c640945 3746 if (ret == 0 && rebind)
78d9a487 3747 usb_rebind_intf(cintf);
79efa097
AS
3748 }
3749 }
3750
94fcda1f 3751 usb_autosuspend_device(udev);
79efa097
AS
3752 return ret;
3753}
742120c6 3754EXPORT_SYMBOL_GPL(usb_reset_device);
dc023dce
IPG
3755
3756
3757/**
3758 * usb_queue_reset_device - Reset a USB device from an atomic context
3759 * @iface: USB interface belonging to the device to reset
3760 *
3761 * This function can be used to reset a USB device from an atomic
3762 * context, where usb_reset_device() won't work (as it blocks).
3763 *
3764 * Doing a reset via this method is functionally equivalent to calling
3765 * usb_reset_device(), except for the fact that it is delayed to a
3766 * workqueue. This means that any drivers bound to other interfaces
3767 * might be unbound, as well as users from usbfs in user space.
3768 *
3769 * Corner cases:
3770 *
3771 * - Scheduling two resets at the same time from two different drivers
3772 * attached to two different interfaces of the same device is
3773 * possible; depending on how the driver attached to each interface
3774 * handles ->pre_reset(), the second reset might happen or not.
3775 *
3776 * - If a driver is unbound and it had a pending reset, the reset will
3777 * be cancelled.
3778 *
3779 * - This function can be called during .probe() or .disconnect()
3780 * times. On return from .disconnect(), any pending resets will be
3781 * cancelled.
3782 *
3783 * There is no no need to lock/unlock the @reset_ws as schedule_work()
3784 * does its own.
3785 *
3786 * NOTE: We don't do any reference count tracking because it is not
3787 * needed. The lifecycle of the work_struct is tied to the
3788 * usb_interface. Before destroying the interface we cancel the
3789 * work_struct, so the fact that work_struct is queued and or
3790 * running means the interface (and thus, the device) exist and
3791 * are referenced.
3792 */
3793void usb_queue_reset_device(struct usb_interface *iface)
3794{
3795 schedule_work(&iface->reset_ws);
3796}
3797EXPORT_SYMBOL_GPL(usb_queue_reset_device);