blob: cdbade14899575e40957300863129df240bc7466 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB hub driver.
3 *
4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999 Johannes Erdfelt
6 * (C) Copyright 1999 Gregory P. Smith
7 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
8 *
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/completion.h>
16#include <linux/sched.h>
17#include <linux/list.h>
18#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/ioctl.h>
20#include <linux/usb.h>
21#include <linux/usbdevice_fs.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020022#include <linux/usb/hcd.h>
Richard Zhao925aa462012-07-11 11:09:28 +080023#include <linux/usb/otg.h>
Phil Dibowitz93362a82010-07-22 00:05:01 +020024#include <linux/usb/quirks.h>
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070025#include <linux/kthread.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010026#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080027#include <linux/freezer.h>
Theodore Ts'ob04b3152012-07-04 11:22:20 -040028#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
31#include <asm/byteorder.h>
32
33#include "usb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -080035/* if we are in debug mode, always announce new devices */
36#ifdef DEBUG
37#ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
38#define CONFIG_USB_ANNOUNCE_NEW_DEVICES
39#endif
40#endif
41
Lan Tianyufa2a9562012-09-05 13:44:31 +080042struct usb_port {
Lan Tianyuff823c72012-09-05 13:44:32 +080043 struct usb_device *child;
Lan Tianyufa2a9562012-09-05 13:44:31 +080044 struct device dev;
45 struct dev_state *port_owner;
46};
47
Alan Stern1bb5f662006-11-06 11:56:13 -050048struct usb_hub {
49 struct device *intfdev; /* the "interface" device */
50 struct usb_device *hdev;
Alan Sterne8054852007-05-04 11:55:11 -040051 struct kref kref;
Alan Stern1bb5f662006-11-06 11:56:13 -050052 struct urb *urb; /* for interrupt polling pipe */
53
54 /* buffer for urb ... with extra space in case of babble */
55 char (*buffer)[8];
Alan Stern1bb5f662006-11-06 11:56:13 -050056 union {
57 struct usb_hub_status hub;
58 struct usb_port_status port;
59 } *status; /* buffer for status reports */
Alan Sterndb90e7a2007-02-05 09:56:15 -050060 struct mutex status_mutex; /* for the status buffer */
Alan Stern1bb5f662006-11-06 11:56:13 -050061
62 int error; /* last reported error */
63 int nerrors; /* track consecutive errors */
64
65 struct list_head event_list; /* hubs w/data or errs ready */
66 unsigned long event_bits[1]; /* status change bitmask */
67 unsigned long change_bits[1]; /* ports with logical connect
68 status change */
69 unsigned long busy_bits[1]; /* ports being reset or
70 resumed */
Alan Stern253e0572009-10-27 15:20:13 -040071 unsigned long removed_bits[1]; /* ports with a "removed"
72 device present */
Sarah Sharp4ee823b2011-11-14 18:00:01 -080073 unsigned long wakeup_bits[1]; /* ports that have signaled
74 remote wakeup */
Alan Stern1bb5f662006-11-06 11:56:13 -050075#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
76#error event_bits[] is too short!
77#endif
78
79 struct usb_hub_descriptor *descriptor; /* class descriptor */
80 struct usb_tt tt; /* Transaction Translator */
81
82 unsigned mA_per_port; /* current for each child */
83
84 unsigned limited_power:1;
85 unsigned quiescing:1;
Alan Sterne8054852007-05-04 11:55:11 -040086 unsigned disconnected:1;
Alan Stern1bb5f662006-11-06 11:56:13 -050087
88 unsigned has_indicators:1;
89 u8 indicator[USB_MAXCHILDREN];
David Howells6d5aefb2006-12-05 19:36:26 +000090 struct delayed_work leds;
Alan Stern8520f382008-09-22 14:44:26 -040091 struct delayed_work init_work;
Lan Tianyufa2a9562012-09-05 13:44:31 +080092 struct usb_port **ports;
Alan Stern1bb5f662006-11-06 11:56:13 -050093};
94
John Youndbe79bb2001-09-17 00:00:00 -070095static inline int hub_is_superspeed(struct usb_device *hdev)
96{
Aman Deep7bf01182011-12-08 12:05:22 +053097 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
John Youndbe79bb2001-09-17 00:00:00 -070098}
Alan Stern1bb5f662006-11-06 11:56:13 -050099
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -0700100/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -0500101 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
103static DEFINE_SPINLOCK(device_state_lock);
104
105/* khubd's worklist and its lock */
106static DEFINE_SPINLOCK(hub_event_lock);
107static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
108
109/* Wakes up khubd */
110static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
111
akpm@osdl.org9c8d6172005-06-20 14:29:58 -0700112static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/* cycle leds on hubs that aren't blinking for attention */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030115static bool blinkenlights = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116module_param (blinkenlights, bool, S_IRUGO);
117MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
118
119/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200120 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
121 * 10 seconds to send reply for the initial 64-byte descriptor request.
122 */
123/* define initial 64-byte descriptor request timeout in milliseconds */
124static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
125module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +0800126MODULE_PARM_DESC(initial_descriptor_timeout,
127 "initial 64-byte descriptor request timeout in milliseconds "
128 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200129
130/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * As of 2.6.10 we introduce a new USB device initialization scheme which
132 * closely resembles the way Windows works. Hopefully it will be compatible
133 * with a wider range of devices than the old scheme. However some previously
134 * working devices may start giving rise to "device not accepting address"
135 * errors; if that happens the user can try the old scheme by adjusting the
136 * following module parameters.
137 *
138 * For maximum flexibility there are two boolean parameters to control the
139 * hub driver's behavior. On the first initialization attempt, if the
140 * "old_scheme_first" parameter is set then the old scheme will be used,
141 * otherwise the new scheme is used. If that fails and "use_both_schemes"
142 * is set, then the driver will make another attempt, using the other scheme.
143 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030144static bool old_scheme_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
146MODULE_PARM_DESC(old_scheme_first,
147 "start with the old device initialization scheme");
148
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030149static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
151MODULE_PARM_DESC(use_both_schemes,
152 "try the other device initialization scheme if the "
153 "first one fails");
154
Alan Stern32fe0192007-10-10 16:27:07 -0400155/* Mutual exclusion for EHCI CF initialization. This interferes with
156 * port reset on some companion controllers.
157 */
158DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
159EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
160
Alan Stern948fea32008-04-28 11:07:07 -0400161#define HUB_DEBOUNCE_TIMEOUT 1500
162#define HUB_DEBOUNCE_STEP 25
163#define HUB_DEBOUNCE_STABLE 100
164
Lan Tianyufa2a9562012-09-05 13:44:31 +0800165#define to_usb_port(_dev) \
166 container_of(_dev, struct usb_port, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Ming Lei742120c2008-06-18 22:00:29 +0800168static int usb_reset_and_verify_device(struct usb_device *udev);
169
Sarah Sharp131dec32010-12-06 21:00:19 -0800170static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Sarah Sharp131dec32010-12-06 21:00:19 -0800172 if (hub_is_superspeed(hub->hdev))
173 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500174 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500176 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return "1.5 Mb/s";
178 else
179 return "12 Mb/s";
180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/* Note that hdev or one of its children must be locked! */
Alan Stern251180842009-07-22 14:41:18 -0400183static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Lan Tianyuff823c72012-09-05 13:44:32 +0800185 if (!hdev || !hdev->actconfig || !hdev->maxchild)
Alan Stern251180842009-07-22 14:41:18 -0400186 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return usb_get_intfdata(hdev->actconfig->interface[0]);
188}
189
Sarah Sharpd9b20992012-02-20 08:31:26 -0800190static int usb_device_supports_lpm(struct usb_device *udev)
191{
192 /* USB 2.1 (and greater) devices indicate LPM support through
193 * their USB 2.0 Extended Capabilities BOS descriptor.
194 */
195 if (udev->speed == USB_SPEED_HIGH) {
196 if (udev->bos->ext_cap &&
197 (USB_LPM_SUPPORT &
198 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
199 return 1;
200 return 0;
201 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800202
203 /* All USB 3.0 must support LPM, but we need their max exit latency
204 * information from the SuperSpeed Extended Capabilities BOS descriptor.
205 */
206 if (!udev->bos->ss_cap) {
207 dev_warn(&udev->dev, "No LPM exit latency info found. "
208 "Power management will be impacted.\n");
209 return 0;
210 }
211 if (udev->parent->lpm_capable)
212 return 1;
213
214 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. "
215 "Power management will be impacted.\n");
Sarah Sharpd9b20992012-02-20 08:31:26 -0800216 return 0;
217}
218
Sarah Sharp51e0a012012-02-20 12:02:19 -0800219/*
220 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
221 * either U1 or U2.
222 */
223static void usb_set_lpm_mel(struct usb_device *udev,
224 struct usb3_lpm_parameters *udev_lpm_params,
225 unsigned int udev_exit_latency,
226 struct usb_hub *hub,
227 struct usb3_lpm_parameters *hub_lpm_params,
228 unsigned int hub_exit_latency)
229{
230 unsigned int total_mel;
231 unsigned int device_mel;
232 unsigned int hub_mel;
233
234 /*
235 * Calculate the time it takes to transition all links from the roothub
236 * to the parent hub into U0. The parent hub must then decode the
237 * packet (hub header decode latency) to figure out which port it was
238 * bound for.
239 *
240 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
241 * means 0.1us). Multiply that by 100 to get nanoseconds.
242 */
243 total_mel = hub_lpm_params->mel +
244 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
245
246 /*
247 * How long will it take to transition the downstream hub's port into
248 * U0? The greater of either the hub exit latency or the device exit
249 * latency.
250 *
251 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
252 * Multiply that by 1000 to get nanoseconds.
253 */
254 device_mel = udev_exit_latency * 1000;
255 hub_mel = hub_exit_latency * 1000;
256 if (device_mel > hub_mel)
257 total_mel += device_mel;
258 else
259 total_mel += hub_mel;
260
261 udev_lpm_params->mel = total_mel;
262}
263
264/*
265 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
266 * a transition from either U1 or U2.
267 */
268static void usb_set_lpm_pel(struct usb_device *udev,
269 struct usb3_lpm_parameters *udev_lpm_params,
270 unsigned int udev_exit_latency,
271 struct usb_hub *hub,
272 struct usb3_lpm_parameters *hub_lpm_params,
273 unsigned int hub_exit_latency,
274 unsigned int port_to_port_exit_latency)
275{
276 unsigned int first_link_pel;
277 unsigned int hub_pel;
278
279 /*
280 * First, the device sends an LFPS to transition the link between the
281 * device and the parent hub into U0. The exit latency is the bigger of
282 * the device exit latency or the hub exit latency.
283 */
284 if (udev_exit_latency > hub_exit_latency)
285 first_link_pel = udev_exit_latency * 1000;
286 else
287 first_link_pel = hub_exit_latency * 1000;
288
289 /*
290 * When the hub starts to receive the LFPS, there is a slight delay for
291 * it to figure out that one of the ports is sending an LFPS. Then it
292 * will forward the LFPS to its upstream link. The exit latency is the
293 * delay, plus the PEL that we calculated for this hub.
294 */
295 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
296
297 /*
298 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
299 * is the greater of the two exit latencies.
300 */
301 if (first_link_pel > hub_pel)
302 udev_lpm_params->pel = first_link_pel;
303 else
304 udev_lpm_params->pel = hub_pel;
305}
306
307/*
308 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
309 * when a device initiates a transition to U0, until when it will receive the
310 * first packet from the host controller.
311 *
312 * Section C.1.5.1 describes the four components to this:
313 * - t1: device PEL
314 * - t2: time for the ERDY to make it from the device to the host.
315 * - t3: a host-specific delay to process the ERDY.
316 * - t4: time for the packet to make it from the host to the device.
317 *
318 * t3 is specific to both the xHCI host and the platform the host is integrated
319 * into. The Intel HW folks have said it's negligible, FIXME if a different
320 * vendor says otherwise.
321 */
322static void usb_set_lpm_sel(struct usb_device *udev,
323 struct usb3_lpm_parameters *udev_lpm_params)
324{
325 struct usb_device *parent;
326 unsigned int num_hubs;
327 unsigned int total_sel;
328
329 /* t1 = device PEL */
330 total_sel = udev_lpm_params->pel;
331 /* How many external hubs are in between the device & the root port. */
332 for (parent = udev->parent, num_hubs = 0; parent->parent;
333 parent = parent->parent)
334 num_hubs++;
335 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
336 if (num_hubs > 0)
337 total_sel += 2100 + 250 * (num_hubs - 1);
338
339 /* t4 = 250ns * num_hubs */
340 total_sel += 250 * num_hubs;
341
342 udev_lpm_params->sel = total_sel;
343}
344
345static void usb_set_lpm_parameters(struct usb_device *udev)
346{
347 struct usb_hub *hub;
348 unsigned int port_to_port_delay;
349 unsigned int udev_u1_del;
350 unsigned int udev_u2_del;
351 unsigned int hub_u1_del;
352 unsigned int hub_u2_del;
353
354 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
355 return;
356
357 hub = hdev_to_hub(udev->parent);
358 /* It doesn't take time to transition the roothub into U0, since it
359 * doesn't have an upstream link.
360 */
361 if (!hub)
362 return;
363
364 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
365 udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
366 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
367 hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
368
369 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
370 hub, &udev->parent->u1_params, hub_u1_del);
371
372 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
373 hub, &udev->parent->u2_params, hub_u2_del);
374
375 /*
376 * Appendix C, section C.2.2.2, says that there is a slight delay from
377 * when the parent hub notices the downstream port is trying to
378 * transition to U0 to when the hub initiates a U0 transition on its
379 * upstream port. The section says the delays are tPort2PortU1EL and
380 * tPort2PortU2EL, but it doesn't define what they are.
381 *
382 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
383 * about the same delays. Use the maximum delay calculations from those
384 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
385 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
386 * assume the device exit latencies they are talking about are the hub
387 * exit latencies.
388 *
389 * What do we do if the U2 exit latency is less than the U1 exit
390 * latency? It's possible, although not likely...
391 */
392 port_to_port_delay = 1;
393
394 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
395 hub, &udev->parent->u1_params, hub_u1_del,
396 port_to_port_delay);
397
398 if (hub_u2_del > hub_u1_del)
399 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
400 else
401 port_to_port_delay = 1 + hub_u1_del;
402
403 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
404 hub, &udev->parent->u2_params, hub_u2_del,
405 port_to_port_delay);
406
407 /* Now that we've got PEL, calculate SEL. */
408 usb_set_lpm_sel(udev, &udev->u1_params);
409 usb_set_lpm_sel(udev, &udev->u2_params);
410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/* USB 2.0 spec Section 11.24.4.5 */
John Youndbe79bb2001-09-17 00:00:00 -0700413static int get_hub_descriptor(struct usb_device *hdev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
John Youndbe79bb2001-09-17 00:00:00 -0700415 int i, ret, size;
416 unsigned dtype;
417
418 if (hub_is_superspeed(hdev)) {
419 dtype = USB_DT_SS_HUB;
420 size = USB_DT_SS_HUB_SIZE;
421 } else {
422 dtype = USB_DT_HUB;
423 size = sizeof(struct usb_hub_descriptor);
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 for (i = 0; i < 3; i++) {
427 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
428 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
John Youndbe79bb2001-09-17 00:00:00 -0700429 dtype << 8, 0, data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 USB_CTRL_GET_TIMEOUT);
431 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
432 return ret;
433 }
434 return -EINVAL;
435}
436
437/*
438 * USB 2.0 spec Section 11.24.2.1
439 */
440static int clear_hub_feature(struct usb_device *hdev, int feature)
441{
442 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
443 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
444}
445
446/*
447 * USB 2.0 spec Section 11.24.2.2
448 */
449static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
450{
451 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
452 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
453 NULL, 0, 1000);
454}
455
456/*
457 * USB 2.0 spec Section 11.24.2.13
458 */
459static int set_port_feature(struct usb_device *hdev, int port1, int feature)
460{
461 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
462 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
463 NULL, 0, 1000);
464}
465
466/*
467 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
468 * for info about using port indicators
469 */
470static void set_port_led(
471 struct usb_hub *hub,
472 int port1,
473 int selector
474)
475{
476 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
477 USB_PORT_FEAT_INDICATOR);
478 if (status < 0)
479 dev_dbg (hub->intfdev,
480 "port %d indicator %s status %d\n",
481 port1,
482 ({ char *s; switch (selector) {
483 case HUB_LED_AMBER: s = "amber"; break;
484 case HUB_LED_GREEN: s = "green"; break;
485 case HUB_LED_OFF: s = "off"; break;
486 case HUB_LED_AUTO: s = "auto"; break;
487 default: s = "??"; break;
488 }; s; }),
489 status);
490}
491
492#define LED_CYCLE_PERIOD ((2*HZ)/3)
493
David Howellsc4028952006-11-22 14:57:56 +0000494static void led_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
David Howellsc4028952006-11-22 14:57:56 +0000496 struct usb_hub *hub =
497 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct usb_device *hdev = hub->hdev;
499 unsigned i;
500 unsigned changed = 0;
501 int cursor = -1;
502
503 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
504 return;
505
506 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
507 unsigned selector, mode;
508
509 /* 30%-50% duty cycle */
510
511 switch (hub->indicator[i]) {
512 /* cycle marker */
513 case INDICATOR_CYCLE:
514 cursor = i;
515 selector = HUB_LED_AUTO;
516 mode = INDICATOR_AUTO;
517 break;
518 /* blinking green = sw attention */
519 case INDICATOR_GREEN_BLINK:
520 selector = HUB_LED_GREEN;
521 mode = INDICATOR_GREEN_BLINK_OFF;
522 break;
523 case INDICATOR_GREEN_BLINK_OFF:
524 selector = HUB_LED_OFF;
525 mode = INDICATOR_GREEN_BLINK;
526 break;
527 /* blinking amber = hw attention */
528 case INDICATOR_AMBER_BLINK:
529 selector = HUB_LED_AMBER;
530 mode = INDICATOR_AMBER_BLINK_OFF;
531 break;
532 case INDICATOR_AMBER_BLINK_OFF:
533 selector = HUB_LED_OFF;
534 mode = INDICATOR_AMBER_BLINK;
535 break;
536 /* blink green/amber = reserved */
537 case INDICATOR_ALT_BLINK:
538 selector = HUB_LED_GREEN;
539 mode = INDICATOR_ALT_BLINK_OFF;
540 break;
541 case INDICATOR_ALT_BLINK_OFF:
542 selector = HUB_LED_AMBER;
543 mode = INDICATOR_ALT_BLINK;
544 break;
545 default:
546 continue;
547 }
548 if (selector != HUB_LED_AUTO)
549 changed = 1;
550 set_port_led(hub, i + 1, selector);
551 hub->indicator[i] = mode;
552 }
553 if (!changed && blinkenlights) {
554 cursor++;
555 cursor %= hub->descriptor->bNbrPorts;
556 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
557 hub->indicator[cursor] = INDICATOR_CYCLE;
558 changed++;
559 }
560 if (changed)
561 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
562}
563
564/* use a short timeout for hub/port status fetches */
565#define USB_STS_TIMEOUT 1000
566#define USB_STS_RETRIES 5
567
568/*
569 * USB 2.0 spec Section 11.24.2.6
570 */
571static int get_hub_status(struct usb_device *hdev,
572 struct usb_hub_status *data)
573{
574 int i, status = -ETIMEDOUT;
575
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200576 for (i = 0; i < USB_STS_RETRIES &&
577 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
579 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
580 data, sizeof(*data), USB_STS_TIMEOUT);
581 }
582 return status;
583}
584
585/*
586 * USB 2.0 spec Section 11.24.2.7
587 */
588static int get_port_status(struct usb_device *hdev, int port1,
589 struct usb_port_status *data)
590{
591 int i, status = -ETIMEDOUT;
592
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200593 for (i = 0; i < USB_STS_RETRIES &&
594 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
596 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
597 data, sizeof(*data), USB_STS_TIMEOUT);
598 }
599 return status;
600}
601
Alan Stern3eb14912008-03-03 15:15:43 -0500602static int hub_port_status(struct usb_hub *hub, int port1,
603 u16 *status, u16 *change)
604{
605 int ret;
606
607 mutex_lock(&hub->status_mutex);
608 ret = get_port_status(hub->hdev, port1, &hub->status->port);
609 if (ret < 4) {
610 dev_err(hub->intfdev,
611 "%s failed (err = %d)\n", __func__, ret);
612 if (ret >= 0)
613 ret = -EIO;
614 } else {
615 *status = le16_to_cpu(hub->status->port.wPortStatus);
616 *change = le16_to_cpu(hub->status->port.wPortChange);
John Youndbe79bb2001-09-17 00:00:00 -0700617
Alan Stern3eb14912008-03-03 15:15:43 -0500618 ret = 0;
619 }
620 mutex_unlock(&hub->status_mutex);
621 return ret;
622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624static void kick_khubd(struct usb_hub *hub)
625{
626 unsigned long flags;
627
628 spin_lock_irqsave(&hub_event_lock, flags);
Roel Kluin2e2c5ee2007-10-26 23:54:35 +0200629 if (!hub->disconnected && list_empty(&hub->event_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 list_add_tail(&hub->event_list, &hub_event_list);
Alan Stern8e4ceb32009-12-07 13:01:37 -0500631
632 /* Suppress autosuspend until khubd runs */
633 usb_autopm_get_interface_no_resume(
634 to_usb_interface(hub->intfdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 wake_up(&khubd_wait);
636 }
637 spin_unlock_irqrestore(&hub_event_lock, flags);
638}
639
640void usb_kick_khubd(struct usb_device *hdev)
641{
Alan Stern251180842009-07-22 14:41:18 -0400642 struct usb_hub *hub = hdev_to_hub(hdev);
643
644 if (hub)
645 kick_khubd(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800648/*
649 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
650 * Notification, which indicates it had initiated remote wakeup.
651 *
652 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
653 * device initiates resume, so the USB core will not receive notice of the
654 * resume through the normal hub interrupt URB.
655 */
656void usb_wakeup_notification(struct usb_device *hdev,
657 unsigned int portnum)
658{
659 struct usb_hub *hub;
660
661 if (!hdev)
662 return;
663
664 hub = hdev_to_hub(hdev);
665 if (hub) {
666 set_bit(portnum, hub->wakeup_bits);
667 kick_khubd(hub);
668 }
669}
670EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100673static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200675 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400676 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100677 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 unsigned long bits;
679
Alan Sterne0152682007-08-24 15:42:52 -0400680 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 case -ENOENT: /* synchronous unlink */
682 case -ECONNRESET: /* async unlink */
683 case -ESHUTDOWN: /* hardware going away */
684 return;
685
686 default: /* presumably an error */
687 /* Cause a hub reset after 10 consecutive errors */
Alan Sterne0152682007-08-24 15:42:52 -0400688 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if ((++hub->nerrors < 10) || hub->error)
690 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400691 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /* let khubd handle things */
695 case 0: /* we got data: port status changed */
696 bits = 0;
697 for (i = 0; i < urb->actual_length; ++i)
698 bits |= ((unsigned long) ((*hub->buffer)[i]))
699 << (i*8);
700 hub->event_bits[0] = bits;
701 break;
702 }
703
704 hub->nerrors = 0;
705
706 /* Something happened, let khubd figure it out */
707 kick_khubd(hub);
708
709resubmit:
710 if (hub->quiescing)
711 return;
712
713 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
714 && status != -ENODEV && status != -EPERM)
715 dev_err (hub->intfdev, "resubmit --> %d\n", status);
716}
717
718/* USB 2.0 spec Section 11.24.2.3 */
719static inline int
720hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
721{
Alan Sternc2f65952009-11-18 11:37:15 -0500722 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
724 tt, NULL, 0, 1000);
725}
726
727/*
728 * enumeration blocks khubd for a long time. we use keventd instead, since
729 * long blocking there is the exception, not the rule. accordingly, HCDs
730 * talking to TTs must queue control transfers (not just bulk and iso), so
731 * both can talk to the same hub concurrently.
732 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400733static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
David Howellsc4028952006-11-22 14:57:56 +0000735 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400736 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 unsigned long flags;
Mark Lord55e5fdf2007-05-14 19:48:02 -0400738 int limit = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 spin_lock_irqsave (&hub->tt.lock, flags);
Mark Lord55e5fdf2007-05-14 19:48:02 -0400741 while (--limit && !list_empty (&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400742 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct usb_tt_clear *clear;
744 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400745 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int status;
747
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400748 next = hub->tt.clear_list.next;
749 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 list_del (&clear->clear_list);
751
752 /* drop lock so HCD can concurrently report other TT errors */
753 spin_unlock_irqrestore (&hub->tt.lock, flags);
754 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (status)
756 dev_err (&hdev->dev,
757 "clear tt %d (%04x) error %d\n",
758 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400759
760 /* Tell the HCD, even if the operation failed */
761 drv = clear->hcd->driver;
762 if (drv->clear_tt_buffer_complete)
763 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
764
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700765 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400766 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768 spin_unlock_irqrestore (&hub->tt.lock, flags);
769}
770
771/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400772 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
773 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *
775 * High speed HCDs use this to tell the hub driver that some split control or
776 * bulk transaction failed in a way that requires clearing internal state of
777 * a transaction translator. This is normally detected (and reported) from
778 * interrupt context.
779 *
780 * It may not be possible for that hub to handle additional full (or low)
781 * speed transactions until that state is fully cleared out.
782 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400783int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400785 struct usb_device *udev = urb->dev;
786 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct usb_tt *tt = udev->tt;
788 unsigned long flags;
789 struct usb_tt_clear *clear;
790
791 /* we've got to cope with an arbitrary number of pending TT clears,
792 * since each TT has "at least two" buffers that can need it (and
793 * there can be many TTs per hub). even if they're uncommon.
794 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800795 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
797 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400798 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800
801 /* info that CLEAR_TT_BUFFER needs */
802 clear->tt = tt->multi ? udev->ttport : 1;
803 clear->devinfo = usb_pipeendpoint (pipe);
804 clear->devinfo |= udev->devnum << 4;
805 clear->devinfo |= usb_pipecontrol (pipe)
806 ? (USB_ENDPOINT_XFER_CONTROL << 11)
807 : (USB_ENDPOINT_XFER_BULK << 11);
808 if (usb_pipein (pipe))
809 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400810
811 /* info for completion callback */
812 clear->hcd = bus_to_hcd(udev->bus);
813 clear->ep = urb->ep;
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /* tell keventd to clear state for this TT */
816 spin_lock_irqsave (&tt->lock, flags);
817 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400818 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400822EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Alan Stern8520f382008-09-22 14:44:26 -0400824/* If do_delay is false, return the number of milliseconds the caller
825 * needs to delay.
826 */
827static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700830 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern8520f382008-09-22 14:44:26 -0400831 unsigned delay;
Alan Stern4489a572006-04-27 15:54:22 -0400832 u16 wHubCharacteristics =
833 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Alan Stern4489a572006-04-27 15:54:22 -0400835 /* Enable power on each port. Some hubs have reserved values
836 * of LPSM (> 2) in their descriptors, even though they are
837 * USB 2.0 hubs. Some hubs do not implement port-power switching
838 * but only emulate it. In all cases, the ports won't work
839 * unless we send these messages to the hub.
840 */
841 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400843 else
844 dev_dbg(hub->intfdev, "trying to enable port power on "
845 "non-switchable hub\n");
846 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
847 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
David Brownellb7896962005-08-31 10:41:44 -0700849 /* Wait at least 100 msec for power to become stable */
Alan Stern8520f382008-09-22 14:44:26 -0400850 delay = max(pgood_delay, (unsigned) 100);
851 if (do_delay)
852 msleep(delay);
853 return delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856static int hub_hub_status(struct usb_hub *hub,
857 u16 *status, u16 *change)
858{
859 int ret;
860
Alan Sterndb90e7a2007-02-05 09:56:15 -0500861 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 ret = get_hub_status(hub->hdev, &hub->status->hub);
863 if (ret < 0)
864 dev_err (hub->intfdev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800865 "%s failed (err = %d)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 else {
867 *status = le16_to_cpu(hub->status->hub.wHubStatus);
868 *change = le16_to_cpu(hub->status->hub.wHubChange);
869 ret = 0;
870 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500871 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return ret;
873}
874
Alan Stern8b28c752005-08-10 17:04:13 -0400875static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
876{
877 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400878 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400879
Lan Tianyuff823c72012-09-05 13:44:32 +0800880 if (hub->ports[port1 - 1]->child && set_state)
881 usb_set_device_state(hub->ports[port1 - 1]->child,
Alan Stern8b28c752005-08-10 17:04:13 -0400882 USB_STATE_NOTATTACHED);
John Youndbe79bb2001-09-17 00:00:00 -0700883 if (!hub->error && !hub_is_superspeed(hub->hdev))
Alan Stern0458d5b2007-05-04 11:52:20 -0400884 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
Alan Stern8b28c752005-08-10 17:04:13 -0400885 if (ret)
886 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
Alan Stern0458d5b2007-05-04 11:52:20 -0400887 port1, ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400888 return ret;
889}
890
Alan Stern0458d5b2007-05-04 11:52:20 -0400891/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800892 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400893 * time later khubd will disconnect() any existing usb_device on the port
894 * and will re-enumerate if there actually is a device attached.
895 */
896static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
897{
898 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
899 hub_port_disable(hub, port1, 1);
900
901 /* FIXME let caller ask to power down the port:
902 * - some devices won't enumerate without a VBUS power cycle
903 * - SRP saves power that way
904 * - ... new call, TBD ...
905 * That's easy if this hub can switch power per-port, and
906 * khubd reactivates the port later (timer, SRP, etc).
907 * Powerdown must be optional, because of reset/DFU.
908 */
909
910 set_bit(port1, hub->change_bits);
911 kick_khubd(hub);
912}
913
Alan Stern253e0572009-10-27 15:20:13 -0400914/**
915 * usb_remove_device - disable a device's port on its parent hub
916 * @udev: device to be disabled and removed
917 * Context: @udev locked, must be able to sleep.
918 *
919 * After @udev's port has been disabled, khubd is notified and it will
920 * see that the device has been disconnected. When the device is
921 * physically unplugged and something is plugged in, the events will
922 * be received and processed normally.
923 */
924int usb_remove_device(struct usb_device *udev)
925{
926 struct usb_hub *hub;
927 struct usb_interface *intf;
928
929 if (!udev->parent) /* Can't remove a root hub */
930 return -EINVAL;
931 hub = hdev_to_hub(udev->parent);
932 intf = to_usb_interface(hub->intfdev);
933
934 usb_autopm_get_interface(intf);
935 set_bit(udev->portnum, hub->removed_bits);
936 hub_port_logical_disconnect(hub, udev->portnum);
937 usb_autopm_put_interface(intf);
938 return 0;
939}
940
Alan Stern6ee0b272008-04-28 11:06:42 -0400941enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -0500942 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -0400943 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -0400944};
Alan Stern5e6effa2008-03-03 15:15:51 -0500945
Alan Stern8520f382008-09-22 14:44:26 -0400946static void hub_init_func2(struct work_struct *ws);
947static void hub_init_func3(struct work_struct *ws);
948
Alan Sternf2835212008-04-28 11:07:17 -0400949static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500950{
951 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -0800952 struct usb_hcd *hcd;
953 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -0500954 int port1;
Alan Sternf2835212008-04-28 11:07:17 -0400955 int status;
Alan Stern948fea32008-04-28 11:07:07 -0400956 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -0400957 unsigned delay;
958
959 /* Continue a partial initialization */
960 if (type == HUB_INIT2)
961 goto init2;
962 if (type == HUB_INIT3)
963 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -0500964
Elric Fua45aa3b2012-02-18 13:32:27 +0800965 /* The superspeed hub except for root hub has to use Hub Depth
966 * value as an offset into the route string to locate the bits
967 * it uses to determine the downstream port number. So hub driver
968 * should send a set hub depth request to superspeed hub after
969 * the superspeed hub is set configuration in initialization or
970 * reset procedure.
971 *
972 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -0400973 * For any other type of activation, turn it on.
974 */
Alan Stern8520f382008-09-22 14:44:26 -0400975 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +0800976 if (hdev->parent && hub_is_superspeed(hdev)) {
977 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
978 HUB_SET_DEPTH, USB_RT_HUB,
979 hdev->level - 1, 0, NULL, 0,
980 USB_CTRL_SET_TIMEOUT);
981 if (ret < 0)
982 dev_err(hub->intfdev,
983 "set hub depth failed\n");
984 }
Alan Stern8520f382008-09-22 14:44:26 -0400985
986 /* Speed up system boot by using a delayed_work for the
987 * hub's initial power-up delays. This is pretty awkward
988 * and the implementation looks like a home-brewed sort of
989 * setjmp/longjmp, but it saves at least 100 ms for each
990 * root hub (assuming usbcore is compiled into the kernel
991 * rather than as a module). It adds up.
992 *
993 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
994 * because for those activation types the ports have to be
995 * operational when we return. In theory this could be done
996 * for HUB_POST_RESET, but it's easier not to.
997 */
998 if (type == HUB_INIT) {
999 delay = hub_power_on(hub, false);
1000 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
1001 schedule_delayed_work(&hub->init_work,
1002 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -04001003
1004 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -05001005 usb_autopm_get_interface_no_resume(
1006 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -04001007 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001008 } else if (type == HUB_RESET_RESUME) {
1009 /* The internal host controller state for the hub device
1010 * may be gone after a host power loss on system resume.
1011 * Update the device's info so the HW knows it's a hub.
1012 */
1013 hcd = bus_to_hcd(hdev->bus);
1014 if (hcd->driver->update_hub_device) {
1015 ret = hcd->driver->update_hub_device(hcd, hdev,
1016 &hub->tt, GFP_NOIO);
1017 if (ret < 0) {
1018 dev_err(hub->intfdev, "Host not "
1019 "accepting hub info "
1020 "update.\n");
1021 dev_err(hub->intfdev, "LS/FS devices "
1022 "and hubs may not work "
1023 "under this hub\n.");
1024 }
1025 }
1026 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001027 } else {
1028 hub_power_on(hub, true);
1029 }
1030 }
1031 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001032
Alan Stern6ee0b272008-04-28 11:06:42 -04001033 /* Check each port and set hub->change_bits to let khubd know
1034 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001035 */
1036 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001037 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern5e6effa2008-03-03 15:15:51 -05001038 u16 portstatus, portchange;
1039
Alan Stern6ee0b272008-04-28 11:06:42 -04001040 portstatus = portchange = 0;
1041 status = hub_port_status(hub, port1, &portstatus, &portchange);
1042 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1043 dev_dbg(hub->intfdev,
1044 "port %d: status %04x change %04x\n",
1045 port1, portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001046
Alan Stern6ee0b272008-04-28 11:06:42 -04001047 /* After anything other than HUB_RESUME (i.e., initialization
1048 * or any sort of reset), every port should be disabled.
1049 * Unconnected ports should likewise be disabled (paranoia),
1050 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001051 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001052 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1053 type != HUB_RESUME ||
1054 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1055 !udev ||
1056 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001057 /*
1058 * USB3 protocol ports will automatically transition
1059 * to Enabled state when detect an USB3.0 device attach.
1060 * Do not disable USB3 protocol ports.
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001061 */
Sarah Sharp131dec32010-12-06 21:00:19 -08001062 if (!hub_is_superspeed(hdev)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001063 clear_port_feature(hdev, port1,
1064 USB_PORT_FEAT_ENABLE);
1065 portstatus &= ~USB_PORT_STAT_ENABLE;
Sarah Sharp85f0ff42010-10-14 07:22:54 -07001066 } else {
1067 /* Pretend that power was lost for USB3 devs */
1068 portstatus &= ~USB_PORT_STAT_ENABLE;
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001069 }
Alan Stern6ee0b272008-04-28 11:06:42 -04001070 }
1071
Alan Stern948fea32008-04-28 11:07:07 -04001072 /* Clear status-change flags; we'll debounce later */
1073 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1074 need_debounce_delay = true;
1075 clear_port_feature(hub->hdev, port1,
1076 USB_PORT_FEAT_C_CONNECTION);
1077 }
1078 if (portchange & USB_PORT_STAT_C_ENABLE) {
1079 need_debounce_delay = true;
1080 clear_port_feature(hub->hdev, port1,
1081 USB_PORT_FEAT_C_ENABLE);
1082 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001083 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1084 hub_is_superspeed(hub->hdev)) {
1085 need_debounce_delay = true;
1086 clear_port_feature(hub->hdev, port1,
1087 USB_PORT_FEAT_C_BH_PORT_RESET);
1088 }
Alan Stern253e0572009-10-27 15:20:13 -04001089 /* We can forget about a "removed" device when there's a
1090 * physical disconnect or the connect status changes.
1091 */
1092 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1093 (portchange & USB_PORT_STAT_C_CONNECTION))
1094 clear_bit(port1, hub->removed_bits);
1095
Alan Stern6ee0b272008-04-28 11:06:42 -04001096 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1097 /* Tell khubd to disconnect the device or
1098 * check for a new connection
1099 */
1100 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1101 set_bit(port1, hub->change_bits);
1102
1103 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001104 bool port_resumed = (portstatus &
1105 USB_PORT_STAT_LINK_STATE) ==
1106 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001107 /* The power session apparently survived the resume.
1108 * If there was an overcurrent or suspend change
1109 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -08001110 * take care of it. Look at the port link state
1111 * for USB 3.0 hubs, since they don't have a suspend
1112 * change bit, and they don't set the port link change
1113 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001114 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001115 if (portchange || (hub_is_superspeed(hub->hdev) &&
1116 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001117 set_bit(port1, hub->change_bits);
1118
1119 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001120#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001121 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001122#endif
Alan Stern8808f002008-04-28 11:06:55 -04001123 set_bit(port1, hub->change_bits);
1124
Alan Stern6ee0b272008-04-28 11:06:42 -04001125 } else {
1126 /* The power session is gone; tell khubd */
1127 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1128 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001129 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001130 }
1131
Alan Stern948fea32008-04-28 11:07:07 -04001132 /* If no port-status-change flags were set, we don't need any
1133 * debouncing. If flags were set we can try to debounce the
1134 * ports all at once right now, instead of letting khubd do them
1135 * one at a time later on.
1136 *
1137 * If any port-status changes do occur during this delay, khubd
1138 * will see them later and handle them normally.
1139 */
Alan Stern8520f382008-09-22 14:44:26 -04001140 if (need_debounce_delay) {
1141 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001142
Alan Stern8520f382008-09-22 14:44:26 -04001143 /* Don't do a long sleep inside a workqueue routine */
1144 if (type == HUB_INIT2) {
1145 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1146 schedule_delayed_work(&hub->init_work,
1147 msecs_to_jiffies(delay));
1148 return; /* Continues at init3: below */
1149 } else {
1150 msleep(delay);
1151 }
1152 }
1153 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001154 hub->quiescing = 0;
1155
1156 status = usb_submit_urb(hub->urb, GFP_NOIO);
1157 if (status < 0)
1158 dev_err(hub->intfdev, "activate --> %d\n", status);
1159 if (hub->has_indicators && blinkenlights)
1160 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1161
1162 /* Scan all ports that need attention */
1163 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001164
1165 /* Allow autosuspend if it was suppressed */
1166 if (type <= HUB_INIT3)
1167 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -05001168}
1169
Alan Stern8520f382008-09-22 14:44:26 -04001170/* Implement the continuations for the delays above */
1171static void hub_init_func2(struct work_struct *ws)
1172{
1173 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1174
1175 hub_activate(hub, HUB_INIT2);
1176}
1177
1178static void hub_init_func3(struct work_struct *ws)
1179{
1180 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1181
1182 hub_activate(hub, HUB_INIT3);
1183}
1184
Alan Stern43303542008-04-28 11:07:31 -04001185enum hub_quiescing_type {
1186 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1187};
1188
1189static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1190{
1191 struct usb_device *hdev = hub->hdev;
1192 int i;
1193
Alan Stern8520f382008-09-22 14:44:26 -04001194 cancel_delayed_work_sync(&hub->init_work);
1195
Alan Stern43303542008-04-28 11:07:31 -04001196 /* khubd and related activity won't re-trigger */
1197 hub->quiescing = 1;
1198
1199 if (type != HUB_SUSPEND) {
1200 /* Disconnect all the children */
1201 for (i = 0; i < hdev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001202 if (hub->ports[i]->child)
1203 usb_disconnect(&hub->ports[i]->child);
Alan Stern43303542008-04-28 11:07:31 -04001204 }
1205 }
1206
1207 /* Stop khubd and related activity */
1208 usb_kill_urb(hub->urb);
1209 if (hub->has_indicators)
1210 cancel_delayed_work_sync(&hub->leds);
1211 if (hub->tt.hub)
Alan Sterncb88a1b2009-06-29 10:43:32 -04001212 cancel_work_sync(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001213}
1214
Alan Stern3eb14912008-03-03 15:15:43 -05001215/* caller has locked the hub device */
1216static int hub_pre_reset(struct usb_interface *intf)
1217{
1218 struct usb_hub *hub = usb_get_intfdata(intf);
1219
Alan Stern43303542008-04-28 11:07:31 -04001220 hub_quiesce(hub, HUB_PRE_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001221 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001222}
1223
1224/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001225static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001226{
Alan Stern7de18d82006-06-01 13:37:24 -04001227 struct usb_hub *hub = usb_get_intfdata(intf);
1228
Alan Sternf2835212008-04-28 11:07:17 -04001229 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001230 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001231}
1232
Lan Tianyufa2a9562012-09-05 13:44:31 +08001233static void usb_port_device_release(struct device *dev)
1234{
1235 struct usb_port *port_dev = to_usb_port(dev);
1236
1237 kfree(port_dev);
1238}
1239
1240static void usb_hub_remove_port_device(struct usb_hub *hub,
1241 int port1)
1242{
1243 device_unregister(&hub->ports[port1 - 1]->dev);
1244}
1245
1246struct device_type usb_port_device_type = {
1247 .name = "usb_port",
1248 .release = usb_port_device_release,
1249};
1250
1251static int usb_hub_create_port_device(struct usb_hub *hub,
1252 int port1)
1253{
1254 struct usb_port *port_dev = NULL;
1255 int retval;
1256
1257 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
1258 if (!port_dev) {
1259 retval = -ENOMEM;
1260 goto exit;
1261 }
1262
1263 hub->ports[port1 - 1] = port_dev;
1264 port_dev->dev.parent = hub->intfdev;
1265 port_dev->dev.type = &usb_port_device_type;
1266 dev_set_name(&port_dev->dev, "port%d", port1);
1267
1268 retval = device_register(&port_dev->dev);
1269 if (retval)
1270 goto error_register;
1271 return 0;
1272
1273error_register:
1274 put_device(&port_dev->dev);
1275exit:
1276 return retval;
1277}
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279static int hub_configure(struct usb_hub *hub,
1280 struct usb_endpoint_descriptor *endpoint)
1281{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001282 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 struct usb_device *hdev = hub->hdev;
1284 struct device *hub_dev = hub->intfdev;
1285 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001286 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 unsigned int pipe;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001288 int maxp, ret, i;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001289 char *message = "out of memory";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Alan Sternd697cdd2009-10-27 15:18:46 -04001291 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 ret = -ENOMEM;
1294 goto fail;
1295 }
1296
1297 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1298 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 ret = -ENOMEM;
1300 goto fail;
1301 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001302 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1305 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 ret = -ENOMEM;
1307 goto fail;
1308 }
1309
1310 /* Request the entire hub descriptor.
1311 * hub->descriptor can handle USB_MAXCHILDREN ports,
1312 * but the hub can/will return fewer bytes here.
1313 */
John Youndbe79bb2001-09-17 00:00:00 -07001314 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (ret < 0) {
1316 message = "can't read hub descriptor";
1317 goto fail;
1318 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1319 message = "hub has too many ports!";
1320 ret = -ENODEV;
1321 goto fail;
1322 }
1323
1324 hdev->maxchild = hub->descriptor->bNbrPorts;
1325 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1326 (hdev->maxchild == 1) ? "" : "s");
1327
Lan Tianyufa2a9562012-09-05 13:44:31 +08001328 hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1329 GFP_KERNEL);
Lan Tianyuff823c72012-09-05 13:44:32 +08001330 if (!hub->ports) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001331 ret = -ENOMEM;
1332 goto fail;
1333 }
1334
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001335 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
John Youndbe79bb2001-09-17 00:00:00 -07001337 /* FIXME for USB 3.0, skip for now */
1338 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1339 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 int i;
1341 char portstr [USB_MAXCHILDREN + 1];
1342
1343 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001344 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1346 ? 'F' : 'R';
1347 portstr[hdev->maxchild] = 0;
1348 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1349 } else
1350 dev_dbg(hub_dev, "standalone hub\n");
1351
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001352 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301353 case HUB_CHAR_COMMON_LPSM:
1354 dev_dbg(hub_dev, "ganged power switching\n");
1355 break;
1356 case HUB_CHAR_INDV_PORT_LPSM:
1357 dev_dbg(hub_dev, "individual port power switching\n");
1358 break;
1359 case HUB_CHAR_NO_LPSM:
1360 case HUB_CHAR_LPSM:
1361 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1362 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001365 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301366 case HUB_CHAR_COMMON_OCPM:
1367 dev_dbg(hub_dev, "global over-current protection\n");
1368 break;
1369 case HUB_CHAR_INDV_PORT_OCPM:
1370 dev_dbg(hub_dev, "individual port over-current protection\n");
1371 break;
1372 case HUB_CHAR_NO_OCPM:
1373 case HUB_CHAR_OCPM:
1374 dev_dbg(hub_dev, "no over-current protection\n");
1375 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377
1378 spin_lock_init (&hub->tt.lock);
1379 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001380 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301382 case USB_HUB_PR_FS:
1383 break;
1384 case USB_HUB_PR_HS_SINGLE_TT:
1385 dev_dbg(hub_dev, "Single TT\n");
1386 hub->tt.hub = hdev;
1387 break;
1388 case USB_HUB_PR_HS_MULTI_TT:
1389 ret = usb_set_interface(hdev, 0, 1);
1390 if (ret == 0) {
1391 dev_dbg(hub_dev, "TT per port\n");
1392 hub->tt.multi = 1;
1393 } else
1394 dev_err(hub_dev, "Using single TT (err %d)\n",
1395 ret);
1396 hub->tt.hub = hdev;
1397 break;
1398 case USB_HUB_PR_SS:
1399 /* USB 3.0 hubs don't have a TT */
1400 break;
1401 default:
1402 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1403 hdev->descriptor.bDeviceProtocol);
1404 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001407 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001408 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001409 case HUB_TTTT_8_BITS:
1410 if (hdev->descriptor.bDeviceProtocol != 0) {
1411 hub->tt.think_time = 666;
1412 dev_dbg(hub_dev, "TT requires at most %d "
1413 "FS bit times (%d ns)\n",
1414 8, hub->tt.think_time);
1415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001417 case HUB_TTTT_16_BITS:
1418 hub->tt.think_time = 666 * 2;
1419 dev_dbg(hub_dev, "TT requires at most %d "
1420 "FS bit times (%d ns)\n",
1421 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001423 case HUB_TTTT_24_BITS:
1424 hub->tt.think_time = 666 * 3;
1425 dev_dbg(hub_dev, "TT requires at most %d "
1426 "FS bit times (%d ns)\n",
1427 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001429 case HUB_TTTT_32_BITS:
1430 hub->tt.think_time = 666 * 4;
1431 dev_dbg(hub_dev, "TT requires at most %d "
1432 "FS bit times (%d ns)\n",
1433 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 break;
1435 }
1436
1437 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001438 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 hub->has_indicators = 1;
1440 dev_dbg(hub_dev, "Port indicators are supported\n");
1441 }
1442
1443 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1444 hub->descriptor->bPwrOn2PwrGood * 2);
1445
1446 /* power budgeting mostly matters with bus-powered hubs,
1447 * and battery-powered root hubs (may provide just 8 mA).
1448 */
1449 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001450 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 message = "can't get hub status";
1452 goto fail;
1453 }
Alan Stern7d35b922005-04-25 11:18:32 -04001454 le16_to_cpus(&hubstatus);
1455 if (hdev == hdev->bus->root_hub) {
Alan Stern55c52712005-11-23 12:03:12 -05001456 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1457 hub->mA_per_port = 500;
1458 else {
1459 hub->mA_per_port = hdev->bus_mA;
1460 hub->limited_power = 1;
1461 }
Alan Stern7d35b922005-04-25 11:18:32 -04001462 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1464 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001465 hub->limited_power = 1;
1466 if (hdev->maxchild > 0) {
1467 int remaining = hdev->bus_mA -
1468 hub->descriptor->bHubContrCurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Alan Stern55c52712005-11-23 12:03:12 -05001470 if (remaining < hdev->maxchild * 100)
1471 dev_warn(hub_dev,
1472 "insufficient power available "
1473 "to use all downstream ports\n");
1474 hub->mA_per_port = 100; /* 7.2.1.1 */
1475 }
1476 } else { /* Self-powered external hub */
1477 /* FIXME: What about battery-powered external hubs that
1478 * provide less current per port? */
1479 hub->mA_per_port = 500;
1480 }
1481 if (hub->mA_per_port < 500)
1482 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1483 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001485 /* Update the HCD's internal representation of this hub before khubd
1486 * starts getting port status changes for devices under the hub.
1487 */
1488 hcd = bus_to_hcd(hdev->bus);
1489 if (hcd->driver->update_hub_device) {
1490 ret = hcd->driver->update_hub_device(hcd, hdev,
1491 &hub->tt, GFP_KERNEL);
1492 if (ret < 0) {
1493 message = "can't update HCD hub info";
1494 goto fail;
1495 }
1496 }
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1499 if (ret < 0) {
1500 message = "can't get hub status";
1501 goto fail;
1502 }
1503
1504 /* local power status reports aren't always correct */
1505 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1506 dev_dbg(hub_dev, "local power source is %s\n",
1507 (hubstatus & HUB_STATUS_LOCAL_POWER)
1508 ? "lost (inactive)" : "good");
1509
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001510 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 dev_dbg(hub_dev, "%sover-current condition exists\n",
1512 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1513
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001514 /* set up the interrupt endpoint
1515 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1516 * bytes as USB2.0[11.12.3] says because some hubs are known
1517 * to send more data (and thus cause overflow). For root hubs,
1518 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1519 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1521 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1522
1523 if (maxp > sizeof(*hub->buffer))
1524 maxp = sizeof(*hub->buffer);
1525
1526 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1527 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 ret = -ENOMEM;
1529 goto fail;
1530 }
1531
1532 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1533 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 /* maybe cycle the hub leds */
1536 if (hub->has_indicators && blinkenlights)
1537 hub->indicator [0] = INDICATOR_CYCLE;
1538
Lan Tianyufa2a9562012-09-05 13:44:31 +08001539 for (i = 0; i < hdev->maxchild; i++)
1540 if (usb_hub_create_port_device(hub, i + 1) < 0)
1541 dev_err(hub->intfdev,
1542 "couldn't create port%d device.\n", i + 1);
1543
Alan Sternf2835212008-04-28 11:07:17 -04001544 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return 0;
1546
1547fail:
1548 dev_err (hub_dev, "config failed, %s (err %d)\n",
1549 message, ret);
1550 /* hub_disconnect() frees urb and descriptor */
1551 return ret;
1552}
1553
Alan Sterne8054852007-05-04 11:55:11 -04001554static void hub_release(struct kref *kref)
1555{
1556 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1557
1558 usb_put_intf(to_usb_interface(hub->intfdev));
1559 kfree(hub);
1560}
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562static unsigned highspeed_hubs;
1563
1564static void hub_disconnect(struct usb_interface *intf)
1565{
Huajun Li88162302012-03-12 21:00:19 +08001566 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001567 struct usb_device *hdev = interface_to_usbdev(intf);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001568 int i;
1569
1570 for (i = 0; i < hdev->maxchild; i++)
1571 usb_hub_remove_port_device(hub, i + 1);
Alan Sterne8054852007-05-04 11:55:11 -04001572
1573 /* Take the hub off the event list and don't let it be added again */
1574 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001575 if (!list_empty(&hub->event_list)) {
1576 list_del_init(&hub->event_list);
1577 usb_autopm_put_interface_no_suspend(intf);
1578 }
Alan Sterne8054852007-05-04 11:55:11 -04001579 hub->disconnected = 1;
1580 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Alan Stern7de18d82006-06-01 13:37:24 -04001582 /* Disconnect all children and quiesce the hub */
1583 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001584 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001585
Alan Stern8b28c752005-08-10 17:04:13 -04001586 usb_set_intfdata (intf, NULL);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001587 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Alan Sterne8054852007-05-04 11:55:11 -04001589 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 highspeed_hubs--;
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 usb_free_urb(hub->urb);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001593 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001594 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001595 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001596 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Alan Sterne8054852007-05-04 11:55:11 -04001598 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
1601static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1602{
1603 struct usb_host_interface *desc;
1604 struct usb_endpoint_descriptor *endpoint;
1605 struct usb_device *hdev;
1606 struct usb_hub *hub;
1607
1608 desc = intf->cur_altsetting;
1609 hdev = interface_to_usbdev(intf);
1610
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001611 /* Hubs have proper suspend/resume support. */
1612 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001613
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001614 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001615 dev_err(&intf->dev,
1616 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001617 return -E2BIG;
1618 }
1619
David Brownell89ccbdc2006-04-02 10:18:09 -08001620#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1621 if (hdev->parent) {
1622 dev_warn(&intf->dev, "ignoring external hub\n");
1623 return -ENODEV;
1624 }
1625#endif
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 /* Some hubs have a subclass of 1, which AFAICT according to the */
1628 /* specs is not defined, but it works */
1629 if ((desc->desc.bInterfaceSubClass != 0) &&
1630 (desc->desc.bInterfaceSubClass != 1)) {
1631descriptor_error:
1632 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1633 return -EIO;
1634 }
1635
1636 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1637 if (desc->desc.bNumEndpoints != 1)
1638 goto descriptor_error;
1639
1640 endpoint = &desc->endpoint[0].desc;
1641
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001642 /* If it's not an interrupt in endpoint, we'd better punt! */
1643 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 goto descriptor_error;
1645
1646 /* We found a hub */
1647 dev_info (&intf->dev, "USB hub found\n");
1648
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001649 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (!hub) {
1651 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1652 return -ENOMEM;
1653 }
1654
Alan Sterne8054852007-05-04 11:55:11 -04001655 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 INIT_LIST_HEAD(&hub->event_list);
1657 hub->intfdev = &intf->dev;
1658 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001659 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001660 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001661 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001664 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 if (hdev->speed == USB_SPEED_HIGH)
1667 highspeed_hubs++;
1668
1669 if (hub_configure(hub, endpoint) >= 0)
1670 return 0;
1671
1672 hub_disconnect (intf);
1673 return -ENODEV;
1674}
1675
1676static int
1677hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1678{
1679 struct usb_device *hdev = interface_to_usbdev (intf);
Lan Tianyuff823c72012-09-05 13:44:32 +08001680 struct usb_hub *hub = hdev_to_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 /* assert ifno == 0 (part of hub spec) */
1683 switch (code) {
1684 case USBDEVFS_HUB_PORTINFO: {
1685 struct usbdevfs_hub_portinfo *info = user_data;
1686 int i;
1687
1688 spin_lock_irq(&device_state_lock);
1689 if (hdev->devnum <= 0)
1690 info->nports = 0;
1691 else {
1692 info->nports = hdev->maxchild;
1693 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001694 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 info->port[i] = 0;
1696 else
1697 info->port[i] =
Lan Tianyuff823c72012-09-05 13:44:32 +08001698 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700 }
1701 spin_unlock_irq(&device_state_lock);
1702
1703 return info->nports + 1;
1704 }
1705
1706 default:
1707 return -ENOSYS;
1708 }
1709}
1710
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001711/*
1712 * Allow user programs to claim ports on a hub. When a device is attached
1713 * to one of these "claimed" ports, the program will "own" the device.
1714 */
1715static int find_port_owner(struct usb_device *hdev, unsigned port1,
Lan Tianyu336c5c32012-07-06 14:13:52 +08001716 struct dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001717{
1718 if (hdev->state == USB_STATE_NOTATTACHED)
1719 return -ENODEV;
1720 if (port1 == 0 || port1 > hdev->maxchild)
1721 return -EINVAL;
1722
1723 /* This assumes that devices not managed by the hub driver
1724 * will always have maxchild equal to 0.
1725 */
Lan Tianyufa2a9562012-09-05 13:44:31 +08001726 *ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001727 return 0;
1728}
1729
1730/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001731int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1732 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001733{
1734 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001735 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001736
1737 rc = find_port_owner(hdev, port1, &powner);
1738 if (rc)
1739 return rc;
1740 if (*powner)
1741 return -EBUSY;
1742 *powner = owner;
1743 return rc;
1744}
1745
Lan Tianyu336c5c32012-07-06 14:13:52 +08001746int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1747 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001748{
1749 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001750 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001751
1752 rc = find_port_owner(hdev, port1, &powner);
1753 if (rc)
1754 return rc;
1755 if (*powner != owner)
1756 return -ENOENT;
1757 *powner = NULL;
1758 return rc;
1759}
1760
Lan Tianyu336c5c32012-07-06 14:13:52 +08001761void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001762{
Lan Tianyufa2a9562012-09-05 13:44:31 +08001763 struct usb_hub *hub = hdev_to_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001764 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001765
Lan Tianyufa2a9562012-09-05 13:44:31 +08001766 for (n = 0; n < hdev->maxchild; n++) {
1767 if (hub->ports[n]->port_owner == owner)
1768 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001769 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001770
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001771}
1772
1773/* The caller must hold udev's lock */
1774bool usb_device_is_owned(struct usb_device *udev)
1775{
1776 struct usb_hub *hub;
1777
1778 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1779 return false;
1780 hub = hdev_to_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001781 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001782}
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1785{
Lan Tianyuff823c72012-09-05 13:44:32 +08001786 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 int i;
1788
1789 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001790 if (hub->ports[i]->child)
1791 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001793 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001794 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 udev->state = USB_STATE_NOTATTACHED;
1796}
1797
1798/**
1799 * usb_set_device_state - change a device's current state (usbcore, hcds)
1800 * @udev: pointer to device whose state should be changed
1801 * @new_state: new state value to be stored
1802 *
1803 * udev->state is _not_ fully protected by the device lock. Although
1804 * most transitions are made only while holding the lock, the state can
1805 * can change to USB_STATE_NOTATTACHED at almost any time. This
1806 * is so that devices can be marked as disconnected as soon as possible,
1807 * without having to wait for any semaphores to be released. As a result,
1808 * all changes to any device's state must be protected by the
1809 * device_state_lock spinlock.
1810 *
1811 * Once a device has been added to the device tree, all changes to its state
1812 * should be made using this routine. The state should _not_ be set directly.
1813 *
1814 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1815 * Otherwise udev->state is set to new_state, and if new_state is
1816 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1817 * to USB_STATE_NOTATTACHED.
1818 */
1819void usb_set_device_state(struct usb_device *udev,
1820 enum usb_device_state new_state)
1821{
1822 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001823 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 spin_lock_irqsave(&device_state_lock, flags);
1826 if (udev->state == USB_STATE_NOTATTACHED)
1827 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001828 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001829
1830 /* root hub wakeup capabilities are managed out-of-band
1831 * and may involve silicon errata ... ignore them here.
1832 */
1833 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001834 if (udev->state == USB_STATE_SUSPENDED
1835 || new_state == USB_STATE_SUSPENDED)
1836 ; /* No change to wakeup settings */
1837 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001838 wakeup = udev->actconfig->desc.bmAttributes
1839 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001840 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001841 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001842 }
Sarah Sharp15123002007-12-21 16:54:15 -08001843 if (udev->state == USB_STATE_SUSPENDED &&
1844 new_state != USB_STATE_SUSPENDED)
1845 udev->active_duration -= jiffies;
1846 else if (new_state == USB_STATE_SUSPENDED &&
1847 udev->state != USB_STATE_SUSPENDED)
1848 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001849 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001850 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 recursively_mark_NOTATTACHED(udev);
1852 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001853 if (wakeup >= 0)
1854 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
David Vrabel6da9c992009-02-18 14:43:47 +00001856EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001858/*
Alan Stern3b29b682011-02-22 09:53:41 -05001859 * Choose a device number.
1860 *
1861 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1862 * USB-2.0 buses they are also used as device addresses, however on
1863 * USB-3.0 buses the address is assigned by the controller hardware
1864 * and it usually is not the same as the device number.
1865 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001866 * WUSB devices are simple: they have no hubs behind, so the mapping
1867 * device <-> virtual port number becomes 1:1. Why? to simplify the
1868 * life of the device connection logic in
1869 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1870 * handshake we need to assign a temporary address in the unauthorized
1871 * space. For simplicity we use the first virtual port number found to
1872 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1873 * and that becomes it's address [X < 128] or its unauthorized address
1874 * [X | 0x80].
1875 *
1876 * We add 1 as an offset to the one-based USB-stack port number
1877 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1878 * 0 is reserved by USB for default address; (b) Linux's USB stack
1879 * uses always #1 for the root hub of the controller. So USB stack's
1880 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001881 *
1882 * Devices connected under xHCI are not as simple. The host controller
1883 * supports virtualization, so the hardware assigns device addresses and
1884 * the HCD must setup data structures before issuing a set address
1885 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001886 */
Alan Stern3b29b682011-02-22 09:53:41 -05001887static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
1889 int devnum;
1890 struct usb_bus *bus = udev->bus;
1891
1892 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001893 if (udev->wusb) {
1894 devnum = udev->portnum + 1;
1895 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1896 } else {
1897 /* Try to allocate the next devnum beginning at
1898 * bus->devnum_next. */
1899 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1900 bus->devnum_next);
1901 if (devnum >= 128)
1902 devnum = find_next_zero_bit(bus->devmap.devicemap,
1903 128, 1);
1904 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (devnum < 128) {
1907 set_bit(devnum, bus->devmap.devicemap);
1908 udev->devnum = devnum;
1909 }
1910}
1911
Alan Stern3b29b682011-02-22 09:53:41 -05001912static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 if (udev->devnum > 0) {
1915 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1916 udev->devnum = -1;
1917 }
1918}
1919
Alan Stern3b29b682011-02-22 09:53:41 -05001920static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001921{
1922 /* The address for a WUSB device is managed by wusbcore. */
1923 if (!udev->wusb)
1924 udev->devnum = devnum;
1925}
1926
Herbert Xuf7410ce2010-01-10 20:15:03 +11001927static void hub_free_dev(struct usb_device *udev)
1928{
1929 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1930
1931 /* Root hubs aren't real devices, so don't free HCD resources */
1932 if (hcd->driver->free_dev && udev->parent)
1933 hcd->driver->free_dev(hcd, udev);
1934}
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936/**
1937 * usb_disconnect - disconnect a device (usbcore-internal)
1938 * @pdev: pointer to device being disconnected
1939 * Context: !in_interrupt ()
1940 *
1941 * Something got disconnected. Get rid of it and all of its children.
1942 *
1943 * If *pdev is a normal device then the parent hub must already be locked.
1944 * If *pdev is a root hub then this routine will acquire the
1945 * usb_bus_list_lock on behalf of the caller.
1946 *
1947 * Only hub drivers (including virtual root hub drivers for host
1948 * controllers) should ever call this.
1949 *
1950 * This call is synchronous, and may not be used in an interrupt context.
1951 */
1952void usb_disconnect(struct usb_device **pdev)
1953{
1954 struct usb_device *udev = *pdev;
Lan Tianyuff823c72012-09-05 13:44:32 +08001955 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 int i;
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 /* mark the device as inactive, so any further urb submissions for
1959 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001960 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 */
1962 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05001963 dev_info(&udev->dev, "USB disconnect, device number %d\n",
1964 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Alan Stern9ad3d6c2005-11-17 17:10:32 -05001966 usb_lock_device(udev);
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08001969 for (i = 0; i < udev->maxchild; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001970 if (hub->ports[i]->child)
1971 usb_disconnect(&hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
1973
1974 /* deallocate hcd/hardware state ... nuking all pending urbs and
1975 * cleaning up all state associated with the current configuration
1976 * so that the hardware is now fully quiesced.
1977 */
Alan Stern782da722006-07-01 22:09:35 -04001978 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04001980 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Alan Stern3b23dd62008-12-05 14:10:34 -05001982 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04001983 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07001984
Alan Stern782da722006-07-01 22:09:35 -04001985 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05001986 * for de-configuring the device and invoking the remove-device
1987 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04001988 */
1989 device_del(&udev->dev);
1990
1991 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 * (or root_hub) pointer.
1993 */
Alan Stern3b29b682011-02-22 09:53:41 -05001994 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 /* Avoid races with recursively_mark_NOTATTACHED() */
1997 spin_lock_irq(&device_state_lock);
1998 *pdev = NULL;
1999 spin_unlock_irq(&device_state_lock);
2000
Herbert Xuf7410ce2010-01-10 20:15:03 +11002001 hub_free_dev(udev);
2002
Alan Stern782da722006-07-01 22:09:35 -04002003 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002006#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007static void show_string(struct usb_device *udev, char *id, char *string)
2008{
2009 if (!string)
2010 return;
2011 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
2012}
2013
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002014static void announce_device(struct usb_device *udev)
2015{
2016 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2017 le16_to_cpu(udev->descriptor.idVendor),
2018 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002019 dev_info(&udev->dev,
2020 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002021 udev->descriptor.iManufacturer,
2022 udev->descriptor.iProduct,
2023 udev->descriptor.iSerialNumber);
2024 show_string(udev, "Product", udev->product);
2025 show_string(udev, "Manufacturer", udev->manufacturer);
2026 show_string(udev, "SerialNumber", udev->serial);
2027}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002029static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030#endif
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032#ifdef CONFIG_USB_OTG
2033#include "otg_whitelist.h"
2034#endif
2035
Oliver Neukum3ede7602007-01-11 14:35:50 +01002036/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002037 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002038 * @udev: newly addressed device (in ADDRESS state)
2039 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002040 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01002041 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002042static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002044 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046#ifdef CONFIG_USB_OTG
2047 /*
2048 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2049 * to wake us after we've powered off VBUS; and HNP, switching roles
2050 * "host" to "peripheral". The OTG descriptor helps figure this out.
2051 */
2052 if (!udev->bus->is_b_host
2053 && udev->config
2054 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002055 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 struct usb_bus *bus = udev->bus;
2057
2058 /* descriptor may appear anywhere in config */
2059 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2060 le16_to_cpu(udev->config[0].desc.wTotalLength),
2061 USB_DT_OTG, (void **) &desc) == 0) {
2062 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002063 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 dev_info(&udev->dev,
2066 "Dual-Role OTG device on %sHNP port\n",
2067 (port1 == bus->otg_port)
2068 ? "" : "non-");
2069
2070 /* enable HNP before suspend, it's simpler */
2071 if (port1 == bus->otg_port)
2072 bus->b_hnp_enable = 1;
2073 err = usb_control_msg(udev,
2074 usb_sndctrlpipe(udev, 0),
2075 USB_REQ_SET_FEATURE, 0,
2076 bus->b_hnp_enable
2077 ? USB_DEVICE_B_HNP_ENABLE
2078 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2079 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2080 if (err < 0) {
2081 /* OTG MESSAGE: report errors here,
2082 * customize to match your product.
2083 */
2084 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002085 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 err);
2087 bus->b_hnp_enable = 0;
2088 }
2089 }
2090 }
2091 }
2092
2093 if (!is_targeted(udev)) {
2094
2095 /* Maybe it can talk to us, though we can't talk to it.
2096 * (Includes HNP test device.)
2097 */
2098 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002099 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (err < 0)
2101 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2102 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002103 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 goto fail;
2105 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002106fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002108 return err;
2109}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002111
2112/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002113 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002114 * @udev: newly addressed device (in ADDRESS state)
2115 *
2116 * This is only called by usb_new_device() and usb_authorize_device()
2117 * and FIXME -- all comments that apply to them apply here wrt to
2118 * environment.
2119 *
2120 * If the device is WUSB and not authorized, we don't attempt to read
2121 * the string descriptors, as they will be errored out by the device
2122 * until it has been authorized.
2123 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002124static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002125{
2126 int err;
2127
2128 if (udev->config == NULL) {
2129 err = usb_get_configuration(udev);
2130 if (err < 0) {
2131 dev_err(&udev->dev, "can't read configurations, error %d\n",
2132 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002133 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002134 }
2135 }
2136 if (udev->wusb == 1 && udev->authorized == 0) {
2137 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2138 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2139 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2140 }
2141 else {
2142 /* read the standard strings and cache them if present */
2143 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2144 udev->manufacturer = usb_cache_string(udev,
2145 udev->descriptor.iManufacturer);
2146 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2147 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002148 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002149 if (err < 0)
2150 return err;
2151
2152 usb_detect_interface_quirks(udev);
2153
2154 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002155}
2156
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002157static void set_usb_port_removable(struct usb_device *udev)
2158{
2159 struct usb_device *hdev = udev->parent;
2160 struct usb_hub *hub;
2161 u8 port = udev->portnum;
2162 u16 wHubCharacteristics;
2163 bool removable = true;
2164
2165 if (!hdev)
2166 return;
2167
2168 hub = hdev_to_hub(udev->parent);
2169
2170 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2171
2172 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2173 return;
2174
2175 if (hub_is_superspeed(hdev)) {
2176 if (hub->descriptor->u.ss.DeviceRemovable & (1 << port))
2177 removable = false;
2178 } else {
2179 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2180 removable = false;
2181 }
2182
2183 if (removable)
2184 udev->removable = USB_DEVICE_REMOVABLE;
2185 else
2186 udev->removable = USB_DEVICE_FIXED;
2187}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002188
2189/**
2190 * usb_new_device - perform initial device setup (usbcore-internal)
2191 * @udev: newly addressed device (in ADDRESS state)
2192 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002193 * This is called with devices which have been detected but not fully
2194 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002195 * for any device configuration. The caller must have locked either
2196 * the parent hub (if udev is a normal device) or else the
2197 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2198 * udev has already been installed, but udev is not yet visible through
2199 * sysfs or other filesystem code.
2200 *
2201 * It will return if the device is configured properly or not. Zero if
2202 * the interface was registered with the driver core; else a negative
2203 * errno value.
2204 *
2205 * This call is synchronous, and may not be used in an interrupt context.
2206 *
2207 * Only the hub driver or root-hub registrar should ever call this.
2208 */
2209int usb_new_device(struct usb_device *udev)
2210{
2211 int err;
2212
Dan Streetman16985402010-01-06 09:56:53 -05002213 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002214 /* Initialize non-root-hub device wakeup to disabled;
2215 * device (un)configuration controls wakeup capable
2216 * sysfs power/wakeup controls wakeup enabled/disabled
2217 */
2218 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002219 }
2220
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002221 /* Tell the runtime-PM framework the device is active */
2222 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002223 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002224 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002225 pm_runtime_enable(&udev->dev);
2226
Alan Sternc08512c2010-11-15 15:57:58 -05002227 /* By default, forbid autosuspend for all devices. It will be
2228 * allowed for hubs during binding.
2229 */
2230 usb_disable_autosuspend(udev);
2231
Alan Stern8d8558d2009-12-08 15:50:41 -05002232 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002233 if (err < 0)
2234 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002235 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2236 udev->devnum, udev->bus->busnum,
2237 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002238 /* export the usbdev device-node for libusb */
2239 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2240 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2241
Alan Stern6cd13202008-11-13 15:08:30 -05002242 /* Tell the world! */
2243 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002244
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002245 if (udev->serial)
2246 add_device_randomness(udev->serial, strlen(udev->serial));
2247 if (udev->product)
2248 add_device_randomness(udev->product, strlen(udev->product));
2249 if (udev->manufacturer)
2250 add_device_randomness(udev->manufacturer,
2251 strlen(udev->manufacturer));
2252
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002253 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002254
2255 /*
2256 * check whether the hub marks this port as non-removable. Do it
2257 * now so that platform-specific data can override it in
2258 * device_add()
2259 */
2260 if (udev->parent)
2261 set_usb_port_removable(udev);
2262
Alan Stern782da722006-07-01 22:09:35 -04002263 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002264 * for configuring the device and invoking the add-device
2265 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002266 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002267 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (err) {
2269 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2270 goto fail;
2271 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002272
Alan Stern3b23dd62008-12-05 14:10:34 -05002273 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002274 usb_mark_last_busy(udev);
2275 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002276 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278fail:
2279 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002280 pm_runtime_disable(&udev->dev);
2281 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002282 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002285
2286/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002287 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2288 * @usb_dev: USB device
2289 *
2290 * Move the USB device to a very basic state where interfaces are disabled
2291 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002292 *
2293 * We share a lock (that we have) with device_del(), so we need to
2294 * defer its call.
2295 */
2296int usb_deauthorize_device(struct usb_device *usb_dev)
2297{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002298 usb_lock_device(usb_dev);
2299 if (usb_dev->authorized == 0)
2300 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002301
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002302 usb_dev->authorized = 0;
2303 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002304
2305 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002306 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002307 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002308 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002309 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002310 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002311
2312 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002313 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002314
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002315out_unauthorized:
2316 usb_unlock_device(usb_dev);
2317 return 0;
2318}
2319
2320
2321int usb_authorize_device(struct usb_device *usb_dev)
2322{
2323 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002324
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002325 usb_lock_device(usb_dev);
2326 if (usb_dev->authorized == 1)
2327 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002328
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002329 result = usb_autoresume_device(usb_dev);
2330 if (result < 0) {
2331 dev_err(&usb_dev->dev,
2332 "can't autoresume for authorization: %d\n", result);
2333 goto error_autoresume;
2334 }
2335 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2336 if (result < 0) {
2337 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2338 "authorization: %d\n", result);
2339 goto error_device_descriptor;
2340 }
Alan Sternda307122009-12-08 15:54:44 -05002341
2342 kfree(usb_dev->product);
2343 usb_dev->product = NULL;
2344 kfree(usb_dev->manufacturer);
2345 usb_dev->manufacturer = NULL;
2346 kfree(usb_dev->serial);
2347 usb_dev->serial = NULL;
2348
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002349 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002350 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002351 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002352 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002353 /* Choose and set the configuration. This registers the interfaces
2354 * with the driver core and lets interface drivers bind to them.
2355 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002356 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002357 if (c >= 0) {
2358 result = usb_set_configuration(usb_dev, c);
2359 if (result) {
2360 dev_err(&usb_dev->dev,
2361 "can't set config #%d, error %d\n", c, result);
2362 /* This need not be fatal. The user can try to
2363 * set other configurations. */
2364 }
2365 }
2366 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002367
Alan Stern8d8558d2009-12-08 15:50:41 -05002368error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002369error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002370 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002371error_autoresume:
2372out_authorized:
2373 usb_unlock_device(usb_dev); // complements locktree
2374 return result;
2375}
2376
2377
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002378/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2379static unsigned hub_is_wusb(struct usb_hub *hub)
2380{
2381 struct usb_hcd *hcd;
2382 if (hub->hdev->parent != NULL) /* not a root hub? */
2383 return 0;
2384 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2385 return hcd->wireless;
2386}
2387
2388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389#define PORT_RESET_TRIES 5
2390#define SET_ADDRESS_TRIES 2
2391#define GET_DESCRIPTOR_TRIES 2
2392#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302393#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2396#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002397#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398#define HUB_LONG_RESET_TIME 200
2399#define HUB_RESET_TIMEOUT 500
2400
Sarah Sharp10d674a2011-09-14 14:24:52 -07002401static int hub_port_reset(struct usb_hub *hub, int port1,
2402 struct usb_device *udev, unsigned int delay, bool warm);
2403
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002404/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2405 * Port worm reset is required to recover
2406 */
2407static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002408{
2409 return hub_is_superspeed(hub->hdev) &&
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002410 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2411 USB_SS_PORT_LS_SS_INACTIVE) ||
2412 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2413 USB_SS_PORT_LS_COMP_MOD)) ;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002414}
2415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002417 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
2419 int delay_time, ret;
2420 u16 portstatus;
2421 u16 portchange;
2422
2423 for (delay_time = 0;
2424 delay_time < HUB_RESET_TIMEOUT;
2425 delay_time += delay) {
2426 /* wait to give the device a chance to reset */
2427 msleep(delay);
2428
2429 /* read and decode port status */
2430 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2431 if (ret < 0)
2432 return ret;
2433
Andiry Xu75d7cf72011-09-13 16:41:11 -07002434 /*
2435 * Some buggy devices require a warm reset to be issued even
2436 * when the port appears not to be connected.
2437 */
2438 if (!warm) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002439 /*
2440 * Some buggy devices can cause an NEC host controller
2441 * to transition to the "Error" state after a hot port
2442 * reset. This will show up as the port state in
2443 * "Inactive", and the port may also report a
2444 * disconnect. Forcing a warm port reset seems to make
2445 * the device work.
2446 *
2447 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2448 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002449 if (hub_port_warm_reset_required(hub, portstatus)) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002450 int ret;
2451
2452 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2453 clear_port_feature(hub->hdev, port1,
2454 USB_PORT_FEAT_C_CONNECTION);
2455 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2456 clear_port_feature(hub->hdev, port1,
2457 USB_PORT_FEAT_C_PORT_LINK_STATE);
2458 if (portchange & USB_PORT_STAT_C_RESET)
2459 clear_port_feature(hub->hdev, port1,
2460 USB_PORT_FEAT_C_RESET);
2461 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2462 port1);
2463 ret = hub_port_reset(hub, port1,
2464 udev, HUB_BH_RESET_TIME,
2465 true);
2466 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2467 clear_port_feature(hub->hdev, port1,
2468 USB_PORT_FEAT_C_CONNECTION);
2469 return ret;
2470 }
Andiry Xu75d7cf72011-09-13 16:41:11 -07002471 /* Device went away? */
2472 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2473 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Andiry Xu75d7cf72011-09-13 16:41:11 -07002475 /* bomb out completely if the connection bounced */
2476 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2477 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Andiry Xu75d7cf72011-09-13 16:41:11 -07002479 /* if we`ve finished resetting, then break out of
2480 * the loop
2481 */
2482 if (!(portstatus & USB_PORT_STAT_RESET) &&
2483 (portstatus & USB_PORT_STAT_ENABLE)) {
2484 if (hub_is_wusb(hub))
2485 udev->speed = USB_SPEED_WIRELESS;
2486 else if (hub_is_superspeed(hub->hdev))
2487 udev->speed = USB_SPEED_SUPER;
2488 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2489 udev->speed = USB_SPEED_HIGH;
2490 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2491 udev->speed = USB_SPEED_LOW;
2492 else
2493 udev->speed = USB_SPEED_FULL;
2494 return 0;
2495 }
2496 } else {
2497 if (portchange & USB_PORT_STAT_C_BH_RESET)
2498 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 }
2500
2501 /* switch to the long delay after two short delay failures */
2502 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2503 delay = HUB_LONG_RESET_TIME;
2504
2505 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002506 "port %d not %sreset yet, waiting %dms\n",
2507 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 }
2509
2510 return -EBUSY;
2511}
2512
Andiry Xu75d7cf72011-09-13 16:41:11 -07002513static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2514 struct usb_device *udev, int *status, bool warm)
2515{
2516 switch (*status) {
2517 case 0:
2518 if (!warm) {
2519 struct usb_hcd *hcd;
2520 /* TRSTRCY = 10 ms; plus some extra */
2521 msleep(10 + 40);
2522 update_devnum(udev, 0);
2523 hcd = bus_to_hcd(udev->bus);
2524 if (hcd->driver->reset_device) {
2525 *status = hcd->driver->reset_device(hcd, udev);
2526 if (*status < 0) {
2527 dev_err(&udev->dev, "Cannot reset "
2528 "HCD device state\n");
2529 break;
2530 }
2531 }
2532 }
2533 /* FALL THROUGH */
2534 case -ENOTCONN:
2535 case -ENODEV:
2536 clear_port_feature(hub->hdev,
2537 port1, USB_PORT_FEAT_C_RESET);
2538 /* FIXME need disconnect() for NOTATTACHED device */
2539 if (warm) {
2540 clear_port_feature(hub->hdev, port1,
2541 USB_PORT_FEAT_C_BH_PORT_RESET);
2542 clear_port_feature(hub->hdev, port1,
2543 USB_PORT_FEAT_C_PORT_LINK_STATE);
2544 } else {
2545 usb_set_device_state(udev, *status
2546 ? USB_STATE_NOTATTACHED
2547 : USB_STATE_DEFAULT);
2548 }
2549 break;
2550 }
2551}
2552
2553/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002555 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
2557 int i, status;
2558
Andiry Xu75d7cf72011-09-13 16:41:11 -07002559 if (!warm) {
2560 /* Block EHCI CF initialization during the port reset.
2561 * Some companion controllers don't like it when they mix.
2562 */
2563 down_read(&ehci_cf_port_reset_rwsem);
2564 } else {
2565 if (!hub_is_superspeed(hub->hdev)) {
2566 dev_err(hub->intfdev, "only USB3 hub support "
2567 "warm reset\n");
2568 return -EINVAL;
2569 }
2570 }
Alan Stern32fe0192007-10-10 16:27:07 -04002571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 /* Reset the port */
2573 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002574 status = set_port_feature(hub->hdev, port1, (warm ?
2575 USB_PORT_FEAT_BH_PORT_RESET :
2576 USB_PORT_FEAT_RESET));
2577 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002579 "cannot %sreset port %d (err = %d)\n",
2580 warm ? "warm " : "", port1, status);
2581 } else {
2582 status = hub_port_wait_reset(hub, port1, udev, delay,
2583 warm);
David Brownelldd165252005-08-31 10:45:25 -07002584 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 dev_dbg(hub->intfdev,
2586 "port_wait_reset: err = %d\n",
2587 status);
2588 }
2589
2590 /* return on disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002591 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2592 hub_port_finish_reset(hub, port1, udev, &status, warm);
Alan Stern32fe0192007-10-10 16:27:07 -04002593 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 }
2595
2596 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002597 "port %d not enabled, trying %sreset again...\n",
2598 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 delay = HUB_LONG_RESET_TIME;
2600 }
2601
2602 dev_err (hub->intfdev,
2603 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2604 port1);
2605
Andiry Xu75d7cf72011-09-13 16:41:11 -07002606done:
2607 if (!warm)
2608 up_read(&ehci_cf_port_reset_rwsem);
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return status;
2611}
2612
Andiry Xu0ed9a572011-04-27 18:07:43 +08002613/* Check if a port is power on */
2614static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2615{
2616 int ret = 0;
2617
2618 if (hub_is_superspeed(hub->hdev)) {
2619 if (portstatus & USB_SS_PORT_STAT_POWER)
2620 ret = 1;
2621 } else {
2622 if (portstatus & USB_PORT_STAT_POWER)
2623 ret = 1;
2624 }
2625
2626 return ret;
2627}
2628
Alan Sternd388dab2006-07-01 22:14:24 -04002629#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Andiry Xu0ed9a572011-04-27 18:07:43 +08002631/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2632static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2633{
2634 int ret = 0;
2635
2636 if (hub_is_superspeed(hub->hdev)) {
2637 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2638 == USB_SS_PORT_LS_U3)
2639 ret = 1;
2640 } else {
2641 if (portstatus & USB_PORT_STAT_SUSPEND)
2642 ret = 1;
2643 }
2644
2645 return ret;
2646}
Alan Sternb01b03f2008-04-28 11:06:11 -04002647
2648/* Determine whether the device on a port is ready for a normal resume,
2649 * is ready for a reset-resume, or should be disconnected.
2650 */
2651static int check_port_resume_type(struct usb_device *udev,
2652 struct usb_hub *hub, int port1,
2653 int status, unsigned portchange, unsigned portstatus)
2654{
2655 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002656 if (status || port_is_suspended(hub, portstatus) ||
2657 !port_is_power_on(hub, portstatus) ||
2658 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002659 if (status >= 0)
2660 status = -ENODEV;
2661 }
2662
Alan Stern86c57ed2008-06-30 11:14:43 -04002663 /* Can't do a normal resume if the port isn't enabled,
2664 * so try a reset-resume instead.
2665 */
2666 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2667 if (udev->persist_enabled)
2668 udev->reset_resume = 1;
2669 else
2670 status = -ENODEV;
2671 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002672
2673 if (status) {
2674 dev_dbg(hub->intfdev,
2675 "port %d status %04x.%04x after resume, %d\n",
2676 port1, portchange, portstatus, status);
2677 } else if (udev->reset_resume) {
2678
2679 /* Late port handoff can set status-change bits */
2680 if (portchange & USB_PORT_STAT_C_CONNECTION)
2681 clear_port_feature(hub->hdev, port1,
2682 USB_PORT_FEAT_C_CONNECTION);
2683 if (portchange & USB_PORT_STAT_C_ENABLE)
2684 clear_port_feature(hub->hdev, port1,
2685 USB_PORT_FEAT_C_ENABLE);
2686 }
2687
2688 return status;
2689}
2690
Sarah Sharpf74631e2012-06-25 12:08:08 -07002691int usb_disable_ltm(struct usb_device *udev)
2692{
2693 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2694
2695 /* Check if the roothub and device supports LTM. */
2696 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2697 !usb_device_supports_ltm(udev))
2698 return 0;
2699
2700 /* Clear Feature LTM Enable can only be sent if the device is
2701 * configured.
2702 */
2703 if (!udev->actconfig)
2704 return 0;
2705
2706 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2707 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2708 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2709 USB_CTRL_SET_TIMEOUT);
2710}
2711EXPORT_SYMBOL_GPL(usb_disable_ltm);
2712
2713void usb_enable_ltm(struct usb_device *udev)
2714{
2715 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2716
2717 /* Check if the roothub and device supports LTM. */
2718 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2719 !usb_device_supports_ltm(udev))
2720 return;
2721
2722 /* Set Feature LTM Enable can only be sent if the device is
2723 * configured.
2724 */
2725 if (!udev->actconfig)
2726 return;
2727
2728 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2729 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2730 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2731 USB_CTRL_SET_TIMEOUT);
2732}
2733EXPORT_SYMBOL_GPL(usb_enable_ltm);
2734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735#ifdef CONFIG_USB_SUSPEND
2736
2737/*
Alan Stern140d8f62006-07-01 22:07:21 -04002738 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002739 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002740 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 *
2742 * Suspends a USB device that isn't in active use, conserving power.
2743 * Devices may wake out of a suspend, if anything important happens,
2744 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002745 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 * to disconnect devices while they are suspended.
2747 *
David Brownell390a8c32005-09-13 19:57:27 -07002748 * This only affects the USB hardware for a device; its interfaces
2749 * (and, for hubs, child devices) must already have been suspended.
2750 *
Alan Stern624d6c02007-05-30 15:35:16 -04002751 * Selective port suspend reduces power; most suspended devices draw
2752 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2753 * All devices below the suspended port are also suspended.
2754 *
2755 * Devices leave suspend state when the host wakes them up. Some devices
2756 * also support "remote wakeup", where the device can activate the USB
2757 * tree above them to deliver data, such as a keypress or packet. In
2758 * some cases, this wakes the USB host.
2759 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 * Suspending OTG devices may trigger HNP, if that's been enabled
2761 * between a pair of dual-role devices. That will change roles, such
2762 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2763 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002764 * Devices on USB hub ports have only one "suspend" state, corresponding
2765 * to ACPI D2, "may cause the device to lose some context".
2766 * State transitions include:
2767 *
2768 * - suspend, resume ... when the VBUS power link stays live
2769 * - suspend, disconnect ... VBUS lost
2770 *
2771 * Once VBUS drop breaks the circuit, the port it's using has to go through
2772 * normal re-enumeration procedures, starting with enabling VBUS power.
2773 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2774 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2775 * timer, no SRP, no requests through sysfs.
2776 *
2777 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2778 * the root hub for their bus goes into global suspend ... so we don't
2779 * (falsely) update the device power state to say it suspended.
2780 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 * Returns 0 on success, else negative errno.
2782 */
Alan Stern65bfd292008-11-25 16:39:18 -05002783int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784{
Alan Stern624d6c02007-05-30 15:35:16 -04002785 struct usb_hub *hub = hdev_to_hub(udev->parent);
2786 int port1 = udev->portnum;
2787 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002788
Alan Stern624d6c02007-05-30 15:35:16 -04002789 /* enable remote wakeup when appropriate; this lets the device
2790 * wake up the upstream hub (including maybe the root hub).
2791 *
2792 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2793 * we don't explicitly enable it here.
2794 */
2795 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002796 if (!hub_is_superspeed(hub->hdev)) {
2797 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2798 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2799 USB_DEVICE_REMOTE_WAKEUP, 0,
2800 NULL, 0,
2801 USB_CTRL_SET_TIMEOUT);
2802 } else {
2803 /* Assume there's only one function on the USB 3.0
2804 * device and enable remote wake for the first
2805 * interface. FIXME if the interface association
2806 * descriptor shows there's more than one function.
2807 */
2808 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2809 USB_REQ_SET_FEATURE,
2810 USB_RECIP_INTERFACE,
2811 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002812 USB_INTRF_FUNC_SUSPEND_RW |
2813 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002814 NULL, 0,
2815 USB_CTRL_SET_TIMEOUT);
2816 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002817 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002818 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2819 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002820 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002821 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002822 return status;
2823 }
Alan Stern624d6c02007-05-30 15:35:16 -04002824 }
2825
Andiry Xu65580b432011-09-23 14:19:52 -07002826 /* disable USB2 hardware LPM */
2827 if (udev->usb2_hw_lpm_enabled == 1)
2828 usb_set_usb2_hardware_lpm(udev, 0);
2829
Sarah Sharpf74631e2012-06-25 12:08:08 -07002830 if (usb_disable_ltm(udev)) {
2831 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2832 __func__);
2833 return -ENOMEM;
2834 }
Sarah Sharp83060952012-05-02 14:25:52 -07002835 if (usb_unlocked_disable_lpm(udev)) {
2836 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2837 __func__);
2838 return -ENOMEM;
2839 }
2840
Alan Stern624d6c02007-05-30 15:35:16 -04002841 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002842 if (hub_is_superspeed(hub->hdev))
2843 status = set_port_feature(hub->hdev,
2844 port1 | (USB_SS_PORT_LS_U3 << 3),
2845 USB_PORT_FEAT_LINK_STATE);
Andiry Xua8f08d82011-03-31 14:56:50 +08002846 else
2847 status = set_port_feature(hub->hdev, port1,
2848 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002849 if (status) {
2850 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2851 port1, status);
2852 /* paranoia: "should not happen" */
Oliver Neukum0c487202009-10-19 13:19:41 +02002853 if (udev->do_remote_wakeup)
2854 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Alan Stern624d6c02007-05-30 15:35:16 -04002855 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2856 USB_DEVICE_REMOTE_WAKEUP, 0,
2857 NULL, 0,
2858 USB_CTRL_SET_TIMEOUT);
Alan Sterncbb33002011-06-15 16:29:16 -04002859
Andiry Xuc3e751e2012-05-05 00:50:10 +08002860 /* Try to enable USB2 hardware LPM again */
2861 if (udev->usb2_hw_lpm_capable == 1)
2862 usb_set_usb2_hardware_lpm(udev, 1);
2863
Sarah Sharpf74631e2012-06-25 12:08:08 -07002864 /* Try to enable USB3 LTM and LPM again */
2865 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07002866 usb_unlocked_enable_lpm(udev);
2867
Alan Sterncbb33002011-06-15 16:29:16 -04002868 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002869 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002870 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002871 } else {
2872 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002873 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2874 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2875 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002876 usb_set_device_state(udev, USB_STATE_SUSPENDED);
2877 msleep(10);
2878 }
Alan Sternc08512c2010-11-15 15:57:58 -05002879 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04002880 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881}
David Brownellf3f32532005-09-22 22:37:29 -07002882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883/*
David Brownell390a8c32005-09-13 19:57:27 -07002884 * If the USB "suspend" state is in use (rather than "global suspend"),
2885 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04002886 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 * hardware resume signaling is finished, either because of selective
2888 * resume (by host) or remote wakeup (by device) ... now see what changed
2889 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04002890 *
2891 * If @udev->reset_resume is set then the device is reset before the
2892 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 */
Alan Stern140d8f62006-07-01 22:07:21 -04002894static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Alan Stern54515fe2007-05-30 15:38:58 -04002896 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 u16 devstatus;
2898
2899 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002900 dev_dbg(&udev->dev, "%s\n",
2901 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
2903 /* usb ch9 identifies four variants of SUSPENDED, based on what
2904 * state the device resumes to. Linux currently won't see the
2905 * first two on the host side; they'd be inside hub_port_init()
2906 * during many timeouts, but khubd can't suspend until later.
2907 */
2908 usb_set_device_state(udev, udev->actconfig
2909 ? USB_STATE_CONFIGURED
2910 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Alan Stern54515fe2007-05-30 15:38:58 -04002912 /* 10.5.4.5 says not to reset a suspended port if the attached
2913 * device is enabled for remote wakeup. Hence the reset
2914 * operation is carried out here, after the port has been
2915 * resumed.
2916 */
2917 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04002918 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08002919 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002920
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 /* 10.5.4.5 says be sure devices in the tree are still there.
2922 * For now let's assume the device didn't go crazy on resume,
2923 * and device drivers will know about any resume quirks.
2924 */
Alan Stern54515fe2007-05-30 15:38:58 -04002925 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04002926 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04002927 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2928 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04002929 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04002930
2931 /* If a normal resume failed, try doing a reset-resume */
2932 if (status && !udev->reset_resume && udev->persist_enabled) {
2933 dev_dbg(&udev->dev, "retry with reset-resume\n");
2934 udev->reset_resume = 1;
2935 goto retry_reset_resume;
2936 }
Alan Stern54515fe2007-05-30 15:38:58 -04002937 }
Alan Sternb40b7a92006-06-19 15:12:38 -04002938
Alan Stern624d6c02007-05-30 15:35:16 -04002939 if (status) {
2940 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2941 status);
2942 } else if (udev->actconfig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04002944 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 status = usb_control_msg(udev,
2946 usb_sndctrlpipe(udev, 0),
2947 USB_REQ_CLEAR_FEATURE,
2948 USB_RECIP_DEVICE,
2949 USB_DEVICE_REMOTE_WAKEUP, 0,
2950 NULL, 0,
2951 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04002952 if (status)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002953 dev_dbg(&udev->dev,
2954 "disable remote wakeup, status %d\n",
2955 status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05002956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 }
2959 return status;
2960}
2961
Alan Stern624d6c02007-05-30 15:35:16 -04002962/*
2963 * usb_port_resume - re-activate a suspended usb device's upstream port
2964 * @udev: device to re-activate, not a root hub
2965 * Context: must be able to sleep; device not locked; pm locks held
2966 *
2967 * This will re-activate the suspended device, increasing power usage
2968 * while letting drivers communicate again with its endpoints.
2969 * USB resume explicitly guarantees that the power session between
2970 * the host and the device is the same as it was when the device
2971 * suspended.
2972 *
Alan Sternfeccc302008-03-03 15:15:59 -05002973 * If @udev->reset_resume is set then this routine won't check that the
2974 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04002975 * reset @udev. The end result is that a broken power session can be
2976 * recovered and @udev will appear to persist across a loss of VBUS power.
2977 *
2978 * For example, if a host controller doesn't maintain VBUS suspend current
2979 * during a system sleep or is reset when the system wakes up, all the USB
2980 * power sessions below it will be broken. This is especially troublesome
2981 * for mass-storage devices containing mounted filesystems, since the
2982 * device will appear to have disconnected and all the memory mappings
2983 * to it will be lost. Using the USB_PERSIST facility, the device can be
2984 * made to appear as if it had not disconnected.
2985 *
Ming Lei742120c2008-06-18 22:00:29 +08002986 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05002987 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04002988 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2989 * quite possible for a device to remain unaltered but its media to be
2990 * changed. If the user replaces a flash memory card while the system is
2991 * asleep, he will have only himself to blame when the filesystem on the
2992 * new card is corrupted and the system crashes.
2993 *
Alan Stern624d6c02007-05-30 15:35:16 -04002994 * Returns 0 on success, else negative errno.
2995 */
Alan Stern65bfd292008-11-25 16:39:18 -05002996int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
Alan Stern624d6c02007-05-30 15:35:16 -04002998 struct usb_hub *hub = hdev_to_hub(udev->parent);
2999 int port1 = udev->portnum;
3000 int status;
3001 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003002
3003 /* Skip the initial Clear-Suspend step for a remote wakeup */
3004 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003005 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003006 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
3008 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3009
Alan Sternd5cbad42006-08-11 16:52:39 -04003010 set_bit(port1, hub->busy_bits);
3011
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003013 if (hub_is_superspeed(hub->hdev))
3014 status = set_port_feature(hub->hdev,
3015 port1 | (USB_SS_PORT_LS_U0 << 3),
3016 USB_PORT_FEAT_LINK_STATE);
3017 else
3018 status = clear_port_feature(hub->hdev,
3019 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003021 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
3022 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04003025 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003026 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 msleep(25);
3028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3030 * stop resume signaling. Then finish the resume
3031 * sequence.
3032 */
Alan Sternd25450c2006-11-20 11:14:30 -05003033 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003034
Alan Sternb01b03f2008-04-28 11:06:11 -04003035 /* TRSMRCY = 10 msec */
3036 msleep(10);
3037 }
Alan Stern54515fe2007-05-30 15:38:58 -04003038
Alan Sternb01b03f2008-04-28 11:06:11 -04003039 SuspendCleared:
3040 if (status == 0) {
Andiry Xua7114232011-04-27 18:07:50 +08003041 if (hub_is_superspeed(hub->hdev)) {
3042 if (portchange & USB_PORT_STAT_C_LINK_STATE)
3043 clear_port_feature(hub->hdev, port1,
3044 USB_PORT_FEAT_C_PORT_LINK_STATE);
3045 } else {
3046 if (portchange & USB_PORT_STAT_C_SUSPEND)
3047 clear_port_feature(hub->hdev, port1,
3048 USB_PORT_FEAT_C_SUSPEND);
3049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Alan Sternd5cbad42006-08-11 16:52:39 -04003052 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04003053
Alan Sternb01b03f2008-04-28 11:06:11 -04003054 status = check_port_resume_type(udev,
3055 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003056 if (status == 0)
3057 status = finish_port_resume(udev);
3058 if (status < 0) {
3059 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3060 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003061 } else {
3062 /* Try to enable USB2 hardware LPM */
3063 if (udev->usb2_hw_lpm_capable == 1)
3064 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003065
Sarah Sharpf74631e2012-06-25 12:08:08 -07003066 /* Try to enable USB3 LTM and LPM */
3067 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003068 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003069 }
Andiry Xu65580b432011-09-23 14:19:52 -07003070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 return status;
3072}
3073
Alan Stern8808f002008-04-28 11:06:55 -04003074/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05003075int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
3077 int status = 0;
3078
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003080 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003081 status = usb_autoresume_device(udev);
3082 if (status == 0) {
3083 /* Let the drivers do their thing, then... */
3084 usb_autosuspend_device(udev);
3085 }
Alan Sternd25450c2006-11-20 11:14:30 -05003086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 return status;
3088}
3089
Alan Sternd388dab2006-07-01 22:14:24 -04003090#else /* CONFIG_USB_SUSPEND */
3091
3092/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3093
Alan Stern65bfd292008-11-25 16:39:18 -05003094int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003095{
3096 return 0;
3097}
3098
Alan Sternb01b03f2008-04-28 11:06:11 -04003099/* However we may need to do a reset-resume */
3100
Alan Stern65bfd292008-11-25 16:39:18 -05003101int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003102{
Alan Sternb01b03f2008-04-28 11:06:11 -04003103 struct usb_hub *hub = hdev_to_hub(udev->parent);
3104 int port1 = udev->portnum;
3105 int status;
3106 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04003107
Alan Sternb01b03f2008-04-28 11:06:11 -04003108 status = hub_port_status(hub, port1, &portstatus, &portchange);
3109 status = check_port_resume_type(udev,
3110 hub, port1, status, portchange, portstatus);
3111
3112 if (status) {
3113 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3114 hub_port_logical_disconnect(hub, port1);
3115 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04003116 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08003117 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003118 }
3119 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04003120}
3121
Alan Sternd388dab2006-07-01 22:14:24 -04003122#endif
3123
David Brownelldb690872005-09-13 19:56:33 -07003124static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125{
3126 struct usb_hub *hub = usb_get_intfdata (intf);
3127 struct usb_device *hdev = hub->hdev;
3128 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003129 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Alan Sterncbb33002011-06-15 16:29:16 -04003131 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3133 struct usb_device *udev;
3134
Lan Tianyuff823c72012-09-05 13:44:32 +08003135 udev = hub->ports[port1 - 1]->child;
Alan Stern6840d252007-09-10 11:34:26 -04003136 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04003137 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003138 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003139 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 }
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003142 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3143 /* Enable hub to send remote wakeup for all ports. */
3144 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3145 status = set_port_feature(hdev,
3146 port1 |
3147 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3148 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3149 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3150 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3151 }
3152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Harvey Harrison441b62c2008-03-03 16:08:34 -08003154 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003155
David Brownellc9f89fa2005-09-13 19:57:04 -07003156 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003157 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
3161static int hub_resume(struct usb_interface *intf)
3162{
Alan Stern5e6effa2008-03-03 15:15:51 -05003163 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Alan Stern5e6effa2008-03-03 15:15:51 -05003165 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003166 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 return 0;
3168}
3169
Alan Sternb41a60e2007-05-30 15:39:33 -04003170static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003171{
Alan Sternb41a60e2007-05-30 15:39:33 -04003172 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003173
Alan Stern5e6effa2008-03-03 15:15:51 -05003174 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003175 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003176 return 0;
3177}
3178
Alan Stern54515fe2007-05-30 15:38:58 -04003179/**
3180 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3181 * @rhdev: struct usb_device for the root hub
3182 *
3183 * The USB host controller driver calls this function when its root hub
3184 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003185 * has been reset. The routine marks @rhdev as having lost power.
3186 * When the hub driver is resumed it will take notice and carry out
3187 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3188 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003189 */
3190void usb_root_hub_lost_power(struct usb_device *rhdev)
3191{
3192 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3193 rhdev->reset_resume = 1;
3194}
3195EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3196
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003197static const char * const usb3_lpm_names[] = {
3198 "U0",
3199 "U1",
3200 "U2",
3201 "U3",
3202};
3203
3204/*
3205 * Send a Set SEL control transfer to the device, prior to enabling
3206 * device-initiated U1 or U2. This lets the device know the exit latencies from
3207 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3208 * packet from the host.
3209 *
3210 * This function will fail if the SEL or PEL values for udev are greater than
3211 * the maximum allowed values for the link state to be enabled.
3212 */
3213static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3214{
3215 struct usb_set_sel_req *sel_values;
3216 unsigned long long u1_sel;
3217 unsigned long long u1_pel;
3218 unsigned long long u2_sel;
3219 unsigned long long u2_pel;
3220 int ret;
3221
3222 /* Convert SEL and PEL stored in ns to us */
3223 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3224 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3225 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3226 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3227
3228 /*
3229 * Make sure that the calculated SEL and PEL values for the link
3230 * state we're enabling aren't bigger than the max SEL/PEL
3231 * value that will fit in the SET SEL control transfer.
3232 * Otherwise the device would get an incorrect idea of the exit
3233 * latency for the link state, and could start a device-initiated
3234 * U1/U2 when the exit latencies are too high.
3235 */
3236 if ((state == USB3_LPM_U1 &&
3237 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3238 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3239 (state == USB3_LPM_U2 &&
3240 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3241 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
3242 dev_dbg(&udev->dev, "Device-initiated %s disabled due "
3243 "to long SEL %llu ms or PEL %llu ms\n",
3244 usb3_lpm_names[state], u1_sel, u1_pel);
3245 return -EINVAL;
3246 }
3247
3248 /*
3249 * If we're enabling device-initiated LPM for one link state,
3250 * but the other link state has a too high SEL or PEL value,
3251 * just set those values to the max in the Set SEL request.
3252 */
3253 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3254 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3255
3256 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3257 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3258
3259 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3260 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3261
3262 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3263 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3264
3265 /*
3266 * usb_enable_lpm() can be called as part of a failed device reset,
3267 * which may be initiated by an error path of a mass storage driver.
3268 * Therefore, use GFP_NOIO.
3269 */
3270 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3271 if (!sel_values)
3272 return -ENOMEM;
3273
3274 sel_values->u1_sel = u1_sel;
3275 sel_values->u1_pel = u1_pel;
3276 sel_values->u2_sel = cpu_to_le16(u2_sel);
3277 sel_values->u2_pel = cpu_to_le16(u2_pel);
3278
3279 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3280 USB_REQ_SET_SEL,
3281 USB_RECIP_DEVICE,
3282 0, 0,
3283 sel_values, sizeof *(sel_values),
3284 USB_CTRL_SET_TIMEOUT);
3285 kfree(sel_values);
3286 return ret;
3287}
3288
3289/*
3290 * Enable or disable device-initiated U1 or U2 transitions.
3291 */
3292static int usb_set_device_initiated_lpm(struct usb_device *udev,
3293 enum usb3_link_state state, bool enable)
3294{
3295 int ret;
3296 int feature;
3297
3298 switch (state) {
3299 case USB3_LPM_U1:
3300 feature = USB_DEVICE_U1_ENABLE;
3301 break;
3302 case USB3_LPM_U2:
3303 feature = USB_DEVICE_U2_ENABLE;
3304 break;
3305 default:
3306 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3307 __func__, enable ? "enable" : "disable");
3308 return -EINVAL;
3309 }
3310
3311 if (udev->state != USB_STATE_CONFIGURED) {
3312 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3313 "for unconfigured device.\n",
3314 __func__, enable ? "enable" : "disable",
3315 usb3_lpm_names[state]);
3316 return 0;
3317 }
3318
3319 if (enable) {
3320 /*
3321 * First, let the device know about the exit latencies
3322 * associated with the link state we're about to enable.
3323 */
3324 ret = usb_req_set_sel(udev, state);
3325 if (ret < 0) {
3326 dev_warn(&udev->dev, "Set SEL for device-initiated "
3327 "%s failed.\n", usb3_lpm_names[state]);
3328 return -EBUSY;
3329 }
3330 /*
3331 * Now send the control transfer to enable device-initiated LPM
3332 * for either U1 or U2.
3333 */
3334 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3335 USB_REQ_SET_FEATURE,
3336 USB_RECIP_DEVICE,
3337 feature,
3338 0, NULL, 0,
3339 USB_CTRL_SET_TIMEOUT);
3340 } else {
3341 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3342 USB_REQ_CLEAR_FEATURE,
3343 USB_RECIP_DEVICE,
3344 feature,
3345 0, NULL, 0,
3346 USB_CTRL_SET_TIMEOUT);
3347 }
3348 if (ret < 0) {
3349 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3350 enable ? "Enable" : "Disable",
3351 usb3_lpm_names[state]);
3352 return -EBUSY;
3353 }
3354 return 0;
3355}
3356
3357static int usb_set_lpm_timeout(struct usb_device *udev,
3358 enum usb3_link_state state, int timeout)
3359{
3360 int ret;
3361 int feature;
3362
3363 switch (state) {
3364 case USB3_LPM_U1:
3365 feature = USB_PORT_FEAT_U1_TIMEOUT;
3366 break;
3367 case USB3_LPM_U2:
3368 feature = USB_PORT_FEAT_U2_TIMEOUT;
3369 break;
3370 default:
3371 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3372 __func__);
3373 return -EINVAL;
3374 }
3375
3376 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3377 timeout != USB3_LPM_DEVICE_INITIATED) {
3378 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3379 "which is a reserved value.\n",
3380 usb3_lpm_names[state], timeout);
3381 return -EINVAL;
3382 }
3383
3384 ret = set_port_feature(udev->parent,
3385 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3386 feature);
3387 if (ret < 0) {
3388 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3389 "error code %i\n", usb3_lpm_names[state],
3390 timeout, ret);
3391 return -EBUSY;
3392 }
3393 if (state == USB3_LPM_U1)
3394 udev->u1_params.timeout = timeout;
3395 else
3396 udev->u2_params.timeout = timeout;
3397 return 0;
3398}
3399
3400/*
3401 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3402 * U1/U2 entry.
3403 *
3404 * We will attempt to enable U1 or U2, but there are no guarantees that the
3405 * control transfers to set the hub timeout or enable device-initiated U1/U2
3406 * will be successful.
3407 *
3408 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3409 * driver know about it. If that call fails, it should be harmless, and just
3410 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3411 */
3412static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3413 enum usb3_link_state state)
3414{
3415 int timeout;
3416
3417 /* We allow the host controller to set the U1/U2 timeout internally
3418 * first, so that it can change its schedule to account for the
3419 * additional latency to send data to a device in a lower power
3420 * link state.
3421 */
3422 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3423
3424 /* xHCI host controller doesn't want to enable this LPM state. */
3425 if (timeout == 0)
3426 return;
3427
3428 if (timeout < 0) {
3429 dev_warn(&udev->dev, "Could not enable %s link state, "
3430 "xHCI error %i.\n", usb3_lpm_names[state],
3431 timeout);
3432 return;
3433 }
3434
3435 if (usb_set_lpm_timeout(udev, state, timeout))
3436 /* If we can't set the parent hub U1/U2 timeout,
3437 * device-initiated LPM won't be allowed either, so let the xHCI
3438 * host know that this link state won't be enabled.
3439 */
3440 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3441
3442 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3443 else if (udev->actconfig)
3444 usb_set_device_initiated_lpm(udev, state, true);
3445
3446}
3447
3448/*
3449 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3450 * U1/U2 entry.
3451 *
3452 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3453 * If zero is returned, the parent will not allow the link to go into U1/U2.
3454 *
3455 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3456 * it won't have an effect on the bus link state because the parent hub will
3457 * still disallow device-initiated U1/U2 entry.
3458 *
3459 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3460 * possible. The result will be slightly more bus bandwidth will be taken up
3461 * (to account for U1/U2 exit latency), but it should be harmless.
3462 */
3463static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3464 enum usb3_link_state state)
3465{
3466 int feature;
3467
3468 switch (state) {
3469 case USB3_LPM_U1:
3470 feature = USB_PORT_FEAT_U1_TIMEOUT;
3471 break;
3472 case USB3_LPM_U2:
3473 feature = USB_PORT_FEAT_U2_TIMEOUT;
3474 break;
3475 default:
3476 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3477 __func__);
3478 return -EINVAL;
3479 }
3480
3481 if (usb_set_lpm_timeout(udev, state, 0))
3482 return -EBUSY;
3483
3484 usb_set_device_initiated_lpm(udev, state, false);
3485
3486 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3487 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3488 "bus schedule bandwidth may be impacted.\n",
3489 usb3_lpm_names[state]);
3490 return 0;
3491}
3492
3493/*
3494 * Disable hub-initiated and device-initiated U1 and U2 entry.
3495 * Caller must own the bandwidth_mutex.
3496 *
3497 * This will call usb_enable_lpm() on failure, which will decrement
3498 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3499 */
3500int usb_disable_lpm(struct usb_device *udev)
3501{
3502 struct usb_hcd *hcd;
3503
3504 if (!udev || !udev->parent ||
3505 udev->speed != USB_SPEED_SUPER ||
3506 !udev->lpm_capable)
3507 return 0;
3508
3509 hcd = bus_to_hcd(udev->bus);
3510 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3511 return 0;
3512
3513 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003514 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003515 return 0;
3516
3517 /* If LPM is enabled, attempt to disable it. */
3518 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3519 goto enable_lpm;
3520 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3521 goto enable_lpm;
3522
3523 return 0;
3524
3525enable_lpm:
3526 usb_enable_lpm(udev);
3527 return -EBUSY;
3528}
3529EXPORT_SYMBOL_GPL(usb_disable_lpm);
3530
3531/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3532int usb_unlocked_disable_lpm(struct usb_device *udev)
3533{
3534 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3535 int ret;
3536
3537 if (!hcd)
3538 return -EINVAL;
3539
3540 mutex_lock(hcd->bandwidth_mutex);
3541 ret = usb_disable_lpm(udev);
3542 mutex_unlock(hcd->bandwidth_mutex);
3543
3544 return ret;
3545}
3546EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3547
3548/*
3549 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3550 * xHCI host policy may prevent U1 or U2 from being enabled.
3551 *
3552 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3553 * until the lpm_disable_count drops to zero. Caller must own the
3554 * bandwidth_mutex.
3555 */
3556void usb_enable_lpm(struct usb_device *udev)
3557{
3558 struct usb_hcd *hcd;
3559
3560 if (!udev || !udev->parent ||
3561 udev->speed != USB_SPEED_SUPER ||
3562 !udev->lpm_capable)
3563 return;
3564
3565 udev->lpm_disable_count--;
3566 hcd = bus_to_hcd(udev->bus);
3567 /* Double check that we can both enable and disable LPM.
3568 * Device must be configured to accept set feature U1/U2 timeout.
3569 */
3570 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3571 !hcd->driver->disable_usb3_lpm_timeout)
3572 return;
3573
3574 if (udev->lpm_disable_count > 0)
3575 return;
3576
3577 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3578 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3579}
3580EXPORT_SYMBOL_GPL(usb_enable_lpm);
3581
3582/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3583void usb_unlocked_enable_lpm(struct usb_device *udev)
3584{
3585 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3586
3587 if (!hcd)
3588 return;
3589
3590 mutex_lock(hcd->bandwidth_mutex);
3591 usb_enable_lpm(udev);
3592 mutex_unlock(hcd->bandwidth_mutex);
3593}
3594EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3595
3596
Alan Sternd388dab2006-07-01 22:14:24 -04003597#else /* CONFIG_PM */
3598
Alan Sternf07600c2007-05-30 15:38:16 -04003599#define hub_suspend NULL
3600#define hub_resume NULL
3601#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003602
3603int usb_disable_lpm(struct usb_device *udev)
3604{
3605 return 0;
3606}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003607EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003608
3609void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003610EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003611
3612int usb_unlocked_disable_lpm(struct usb_device *udev)
3613{
3614 return 0;
3615}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003616EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003617
3618void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003619EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07003620
3621int usb_disable_ltm(struct usb_device *udev)
3622{
3623 return 0;
3624}
3625EXPORT_SYMBOL_GPL(usb_disable_ltm);
3626
3627void usb_enable_ltm(struct usb_device *udev) { }
3628EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternd388dab2006-07-01 22:14:24 -04003629#endif
3630
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
3632/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3633 *
3634 * Between connect detection and reset signaling there must be a delay
3635 * of 100ms at least for debounce and power-settling. The corresponding
3636 * timer shall restart whenever the downstream port detects a disconnect.
3637 *
3638 * Apparently there are some bluetooth and irda-dongles and a number of
3639 * low-speed devices for which this debounce period may last over a second.
3640 * Not covered by the spec - but easy to deal with.
3641 *
3642 * This implementation uses a 1500ms total debounce timeout; if the
3643 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3644 * every 25ms for transient disconnects. When the port status has been
3645 * unchanged for 100ms it returns the port status.
3646 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647static int hub_port_debounce(struct usb_hub *hub, int port1)
3648{
3649 int ret;
3650 int total_time, stable_time = 0;
3651 u16 portchange, portstatus;
3652 unsigned connection = 0xffff;
3653
3654 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3655 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3656 if (ret < 0)
3657 return ret;
3658
3659 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3660 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3661 stable_time += HUB_DEBOUNCE_STEP;
3662 if (stable_time >= HUB_DEBOUNCE_STABLE)
3663 break;
3664 } else {
3665 stable_time = 0;
3666 connection = portstatus & USB_PORT_STAT_CONNECTION;
3667 }
3668
3669 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3670 clear_port_feature(hub->hdev, port1,
3671 USB_PORT_FEAT_C_CONNECTION);
3672 }
3673
3674 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3675 break;
3676 msleep(HUB_DEBOUNCE_STEP);
3677 }
3678
3679 dev_dbg (hub->intfdev,
3680 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3681 port1, total_time, stable_time, portstatus);
3682
3683 if (stable_time < HUB_DEBOUNCE_STABLE)
3684 return -ETIMEDOUT;
3685 return portstatus;
3686}
3687
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003688void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689{
Alan Sternddeac4e2009-01-15 17:03:33 -05003690 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3691 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003692 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003694EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
3696#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3697#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3698
Alan Stern4326ed02007-07-30 17:08:43 -04003699static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
3701 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003702 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
Sarah Sharpc6515272009-04-27 19:57:26 -07003704 /*
3705 * The host controller will choose the device address,
3706 * instead of the core having chosen it earlier
3707 */
3708 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 return -EINVAL;
3710 if (udev->state == USB_STATE_ADDRESS)
3711 return 0;
3712 if (udev->state != USB_STATE_DEFAULT)
3713 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003714 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003715 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003716 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003717 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3718 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3719 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003721 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003722 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003724 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 }
3726 return retval;
3727}
3728
3729/* Reset device, (re)assign address, get device descriptor.
3730 * Device connection must be stable, no more debouncing needed.
3731 * Returns device in USB_STATE_ADDRESS, except on error.
3732 *
3733 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003734 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 * newly detected device that is not accessible through any global
3736 * pointers, it's not necessary to lock the device.
3737 */
3738static int
3739hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3740 int retry_counter)
3741{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003742 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
3744 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003745 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 int i, j, retval;
3747 unsigned delay = HUB_SHORT_RESET_TIME;
3748 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003749 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003750 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
3752 /* root hub ports have a slightly longer reset period
3753 * (from USB 2.0 spec, section 7.1.7.5)
3754 */
3755 if (!hdev->parent) {
3756 delay = HUB_ROOT_RESET_TIME;
3757 if (port1 == hdev->bus->otg_port)
3758 hdev->bus->b_hnp_enable = 0;
3759 }
3760
3761 /* Some low speed devices have problems with the quick delay, so */
3762 /* be a bit pessimistic with those devices. RHbug #23670 */
3763 if (oldspeed == USB_SPEED_LOW)
3764 delay = HUB_LONG_RESET_TIME;
3765
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003766 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Luben Tuikov07194ab2011-02-11 11:33:10 -08003768 /* Reset the device; full speed may morph to high speed */
3769 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003770 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003771 if (retval < 0) /* error or disconnect */
3772 goto fail;
3773 /* success, speed is known */
3774
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 retval = -ENODEV;
3776
3777 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3778 dev_dbg(&udev->dev, "device reset changed speed!\n");
3779 goto fail;
3780 }
3781 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003782
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3784 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003785 * For Wireless USB devices, ep0 max packet is always 512 (tho
3786 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 */
3788 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003789 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003790 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003791 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003792 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003794 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 break;
3796 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3797 /* to determine the ep0 maxpacket size, try to read
3798 * the device descriptor to get bMaxPacketSize0 and
3799 * then correct our initial guess.
3800 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003801 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 break;
3803 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003804 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 break;
3806 default:
3807 goto fail;
3808 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003809
3810 if (udev->speed == USB_SPEED_WIRELESS)
3811 speed = "variable speed Wireless";
3812 else
3813 speed = usb_speed_string(udev->speed);
3814
Sarah Sharpc6515272009-04-27 19:57:26 -07003815 if (udev->speed != USB_SPEED_SUPER)
3816 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003817 "%s %s USB device number %d using %s\n",
3818 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003819 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
3821 /* Set up TT records, if needed */
3822 if (hdev->tt) {
3823 udev->tt = hdev->tt;
3824 udev->ttport = hdev->ttport;
3825 } else if (udev->speed != USB_SPEED_HIGH
3826 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003827 if (!hub->tt.hub) {
3828 dev_err(&udev->dev, "parent hub has no TT\n");
3829 retval = -EINVAL;
3830 goto fail;
3831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 udev->tt = &hub->tt;
3833 udev->ttport = port1;
3834 }
3835
3836 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3837 * Because device hardware and firmware is sometimes buggy in
3838 * this area, and this is how Linux has done it for ages.
3839 * Change it cautiously.
3840 *
3841 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3842 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3843 * so it may help with some non-standards-compliant devices.
3844 * Otherwise we start with SET_ADDRESS and then try to read the
3845 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3846 * value.
3847 */
3848 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07003849 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 struct usb_device_descriptor *buf;
3851 int r = 0;
3852
3853#define GET_DESCRIPTOR_BUFSIZE 64
3854 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3855 if (!buf) {
3856 retval = -ENOMEM;
3857 continue;
3858 }
3859
Alan Sternb89ee192007-05-11 10:19:04 -04003860 /* Retry on all errors; some devices are flakey.
3861 * 255 is for WUSB devices, we actually need to use
3862 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 */
3864 for (j = 0; j < 3; ++j) {
3865 buf->bMaxPacketSize0 = 0;
3866 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3867 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3868 USB_DT_DEVICE << 8, 0,
3869 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02003870 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003872 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 if (buf->bDescriptorType ==
3874 USB_DT_DEVICE) {
3875 r = 0;
3876 break;
3877 }
3878 /* FALL THROUGH */
3879 default:
3880 if (r == 0)
3881 r = -EPROTO;
3882 break;
3883 }
3884 if (r == 0)
3885 break;
3886 }
3887 udev->descriptor.bMaxPacketSize0 =
3888 buf->bMaxPacketSize0;
3889 kfree(buf);
3890
Andiry Xu75d7cf72011-09-13 16:41:11 -07003891 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 if (retval < 0) /* error or disconnect */
3893 goto fail;
3894 if (oldspeed != udev->speed) {
3895 dev_dbg(&udev->dev,
3896 "device reset changed speed!\n");
3897 retval = -ENODEV;
3898 goto fail;
3899 }
3900 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003901 dev_err(&udev->dev,
3902 "device descriptor read/64, error %d\n",
3903 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 retval = -EMSGSIZE;
3905 continue;
3906 }
3907#undef GET_DESCRIPTOR_BUFSIZE
3908 }
3909
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003910 /*
3911 * If device is WUSB, we already assigned an
3912 * unauthorized address in the Connect Ack sequence;
3913 * authorization will assign the final address.
3914 */
Sarah Sharpc6515272009-04-27 19:57:26 -07003915 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003916 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3917 retval = hub_set_address(udev, devnum);
3918 if (retval >= 0)
3919 break;
3920 msleep(200);
3921 }
3922 if (retval < 0) {
3923 dev_err(&udev->dev,
3924 "device not accepting address %d, error %d\n",
3925 devnum, retval);
3926 goto fail;
3927 }
Sarah Sharpc6515272009-04-27 19:57:26 -07003928 if (udev->speed == USB_SPEED_SUPER) {
3929 devnum = udev->devnum;
3930 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05003931 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07003932 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05003933 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07003934 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003935
3936 /* cope with hardware quirkiness:
3937 * - let SET_ADDRESS settle, some device hardware wants it
3938 * - read ep0 maxpacket even for high and low speed,
3939 */
3940 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07003941 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
3945 retval = usb_get_device_descriptor(udev, 8);
3946 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003947 dev_err(&udev->dev,
3948 "device descriptor read/8, error %d\n",
3949 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 if (retval >= 0)
3951 retval = -EMSGSIZE;
3952 } else {
3953 retval = 0;
3954 break;
3955 }
3956 }
3957 if (retval)
3958 goto fail;
3959
Elric Fud8aec3d2012-03-26 21:16:02 +08003960 /*
3961 * Some superspeed devices have finished the link training process
3962 * and attached to a superspeed hub port, but the device descriptor
3963 * got from those devices show they aren't superspeed devices. Warm
3964 * reset the port attached by the devices can fix them.
3965 */
3966 if ((udev->speed == USB_SPEED_SUPER) &&
3967 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
3968 dev_err(&udev->dev, "got a wrong device descriptor, "
3969 "warm reset device\n");
3970 hub_port_reset(hub, port1, udev,
3971 HUB_BH_RESET_TIME, true);
3972 retval = -EINVAL;
3973 goto fail;
3974 }
3975
Sarah Sharp6b403b02009-04-27 19:54:10 -07003976 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
3977 udev->speed == USB_SPEED_SUPER)
3978 i = 512;
3979 else
3980 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003981 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04003982 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04003984 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 retval = -EMSGSIZE;
3986 goto fail;
3987 }
Alan Stern56626a72010-10-14 15:25:21 -04003988 if (udev->speed == USB_SPEED_FULL)
3989 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
3990 else
3991 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003993 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 }
3995
3996 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
3997 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003998 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
3999 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 if (retval >= 0)
4001 retval = -ENOMSG;
4002 goto fail;
4003 }
4004
Andiry Xu1ff4df52011-09-23 14:19:48 -07004005 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4006 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004007 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004008 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004009 usb_set_lpm_parameters(udev);
4010 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004011 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004012
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004014 /* notify HCD that we have a device connected and addressed */
4015 if (hcd->driver->update_device)
4016 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004018 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004020 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004021 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01004022 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 return retval;
4024}
4025
4026static void
4027check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4028{
4029 struct usb_qualifier_descriptor *qual;
4030 int status;
4031
Christoph Lametere94b1762006-12-06 20:33:17 -08004032 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 if (qual == NULL)
4034 return;
4035
4036 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4037 qual, sizeof *qual);
4038 if (status == sizeof *qual) {
4039 dev_info(&udev->dev, "not running at top speed; "
4040 "connect to a high speed hub\n");
4041 /* hub LEDs are probably harder to miss than syslog */
4042 if (hub->has_indicators) {
4043 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004044 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 }
4046 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004047 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048}
4049
4050static unsigned
4051hub_power_remaining (struct usb_hub *hub)
4052{
4053 struct usb_device *hdev = hub->hdev;
4054 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004055 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Alan Stern55c52712005-11-23 12:03:12 -05004057 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 return 0;
4059
Alan Stern55c52712005-11-23 12:03:12 -05004060 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4061 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c72012-09-05 13:44:32 +08004062 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern55c52712005-11-23 12:03:12 -05004063 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
4065 if (!udev)
4066 continue;
4067
Alan Stern55c52712005-11-23 12:03:12 -05004068 /* Unconfigured devices may not use more than 100mA,
4069 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05004071 delta = udev->actconfig->desc.bMaxPower * 2;
4072 else if (port1 != udev->bus->otg_port || hdev->parent)
4073 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 else
Alan Stern55c52712005-11-23 12:03:12 -05004075 delta = 8;
4076 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004077 dev_warn(&udev->dev,
4078 "%dmA is over %umA budget for port %d!\n",
4079 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 remaining -= delta;
4081 }
4082 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004083 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4084 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 remaining = 0;
4086 }
4087 return remaining;
4088}
4089
4090/* Handle physical or logical connection change events.
4091 * This routine is called when:
4092 * a port connection-change occurs;
4093 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08004094 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 * a firmware download)
4096 * caller already locked the hub
4097 */
4098static void hub_port_connect_change(struct usb_hub *hub, int port1,
4099 u16 portstatus, u16 portchange)
4100{
4101 struct usb_device *hdev = hub->hdev;
4102 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05304103 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04004104 unsigned wHubCharacteristics =
4105 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04004106 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04004108
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 dev_dbg (hub_dev,
4110 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08004111 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
4113 if (hub->has_indicators) {
4114 set_port_led(hub, port1, HUB_LED_AUTO);
4115 hub->indicator[port1-1] = INDICATOR_AUTO;
4116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
4118#ifdef CONFIG_USB_OTG
4119 /* during HNP, don't repeat the debounce */
4120 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04004121 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4122 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123#endif
4124
Alan Stern8808f002008-04-28 11:06:55 -04004125 /* Try to resuscitate an existing device */
Lan Tianyuff823c72012-09-05 13:44:32 +08004126 udev = hub->ports[port1 - 1]->child;
Alan Stern8808f002008-04-28 11:06:55 -04004127 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4128 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04004129 usb_lock_device(udev);
4130 if (portstatus & USB_PORT_STAT_ENABLE) {
4131 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004132
4133#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04004134 } else if (udev->state == USB_STATE_SUSPENDED &&
4135 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004136 /* For a suspended device, treat this as a
4137 * remote wakeup event.
4138 */
Alan Stern0534d462010-01-08 12:56:30 -05004139 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004140#endif
4141
4142 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004143 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004144 }
4145 usb_unlock_device(udev);
4146
4147 if (status == 0) {
4148 clear_bit(port1, hub->change_bits);
4149 return;
4150 }
4151 }
4152
Alan Stern24618b02008-04-28 11:06:28 -04004153 /* Disconnect any existing devices under this port */
Alan Stern8808f002008-04-28 11:06:55 -04004154 if (udev)
Lan Tianyuff823c72012-09-05 13:44:32 +08004155 usb_disconnect(&hub->ports[port1 - 1]->child);
Alan Stern24618b02008-04-28 11:06:28 -04004156 clear_bit(port1, hub->change_bits);
4157
Alan Stern253e0572009-10-27 15:20:13 -04004158 /* We can forget about a "removed" device when there's a physical
4159 * disconnect or the connect status changes.
4160 */
4161 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4162 (portchange & USB_PORT_STAT_C_CONNECTION))
4163 clear_bit(port1, hub->removed_bits);
4164
Alan Stern5257d972008-09-22 14:43:08 -04004165 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4166 USB_PORT_STAT_C_ENABLE)) {
4167 status = hub_port_debounce(hub, port1);
4168 if (status < 0) {
4169 if (printk_ratelimit())
4170 dev_err(hub_dev, "connect-debounce failed, "
4171 "port %d disabled\n", port1);
4172 portstatus &= ~USB_PORT_STAT_CONNECTION;
4173 } else {
4174 portstatus = status;
4175 }
4176 }
4177
Richard Zhao925aa462012-07-11 11:09:28 +08004178 if (hcd->phy && !hdev->parent) {
4179 if (portstatus & USB_PORT_STAT_CONNECTION)
4180 usb_phy_notify_connect(hcd->phy, port1);
4181 else
4182 usb_phy_notify_disconnect(hcd->phy, port1);
4183 }
4184
Alan Stern253e0572009-10-27 15:20:13 -04004185 /* Return now if debouncing failed or nothing is connected or
4186 * the device was "removed".
4187 */
4188 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4189 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
4191 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07004192 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08004193 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004195
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 if (portstatus & USB_PORT_STAT_ENABLE)
4197 goto done;
4198 return;
4199 }
4200
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
4203 /* reallocate for each attempt, since references
4204 * to the previous one can escape in various ways
4205 */
4206 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4207 if (!udev) {
4208 dev_err (hub_dev,
4209 "couldn't allocate port %d usb_device\n",
4210 port1);
4211 goto done;
4212 }
4213
4214 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05004215 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004216 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004217 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004218
Sarah Sharp131dec32010-12-06 21:00:19 -08004219 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4220 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004221 udev->speed = USB_SPEED_SUPER;
4222 else
4223 udev->speed = USB_SPEED_UNKNOWN;
4224
Alan Stern3b29b682011-02-22 09:53:41 -05004225 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004226 if (udev->devnum <= 0) {
4227 status = -ENOTCONN; /* Don't retry */
4228 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004229 }
4230
Sarah Sharpe7b77172009-04-27 19:54:26 -07004231 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 status = hub_port_init(hub, udev, port1, i);
4233 if (status < 0)
4234 goto loop;
4235
Phil Dibowitz93362a82010-07-22 00:05:01 +02004236 usb_detect_quirks(udev);
4237 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4238 msleep(1000);
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 /* consecutive bus-powered hubs aren't reliable; they can
4241 * violate the voltage drop budget. if the new child has
4242 * a "powered" LED, users should notice we didn't enable it
4243 * (without reading syslog), even without per-port LEDs
4244 * on the parent.
4245 */
4246 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05004247 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 u16 devstat;
4249
4250 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4251 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05004252 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 dev_dbg(&udev->dev, "get status %d ?\n", status);
4254 goto loop_disable;
4255 }
Alan Stern55c52712005-11-23 12:03:12 -05004256 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4258 dev_err(&udev->dev,
4259 "can't connect bus-powered hub "
4260 "to this port\n");
4261 if (hub->has_indicators) {
4262 hub->indicator[port1-1] =
4263 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004264 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 }
4266 status = -ENOTCONN; /* Don't retry */
4267 goto loop_disable;
4268 }
4269 }
4270
4271 /* check for devices running slower than they could */
4272 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4273 && udev->speed == USB_SPEED_FULL
4274 && highspeed_hubs != 0)
4275 check_highspeed (hub, udev, port1);
4276
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004277 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 * udev becomes globally accessible, although presumably
4279 * no one will look at it until hdev is unlocked.
4280 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 status = 0;
4282
4283 /* We mustn't add new devices if the parent hub has
4284 * been disconnected; we would race with the
4285 * recursively_mark_NOTATTACHED() routine.
4286 */
4287 spin_lock_irq(&device_state_lock);
4288 if (hdev->state == USB_STATE_NOTATTACHED)
4289 status = -ENOTCONN;
4290 else
Lan Tianyuff823c72012-09-05 13:44:32 +08004291 hub->ports[port1 - 1]->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 spin_unlock_irq(&device_state_lock);
4293
4294 /* Run it through the hoops (find a driver, etc) */
4295 if (!status) {
4296 status = usb_new_device(udev);
4297 if (status) {
4298 spin_lock_irq(&device_state_lock);
Lan Tianyuff823c72012-09-05 13:44:32 +08004299 hub->ports[port1 - 1]->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 spin_unlock_irq(&device_state_lock);
4301 }
4302 }
4303
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 if (status)
4305 goto loop_disable;
4306
4307 status = hub_power_remaining(hub);
4308 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05004309 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
4311 return;
4312
4313loop_disable:
4314 hub_port_disable(hub, port1, 1);
4315loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004316 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004317 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004318 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004320 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 break;
4322 }
Alan Stern3a311552008-05-20 16:58:29 -04004323 if (hub->hdev->parent ||
4324 !hcd->driver->port_handed_over ||
4325 !(hcd->driver->port_handed_over)(hcd, port1))
4326 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4327 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
4329done:
4330 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304331 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4332 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333}
4334
Sarah Sharp714b07b2012-01-24 13:53:18 -08004335/* Returns 1 if there was a remote wakeup and a connect status change. */
4336static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004337 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004338{
4339 struct usb_device *hdev;
4340 struct usb_device *udev;
4341 int connect_change = 0;
4342 int ret;
4343
4344 hdev = hub->hdev;
Lan Tianyuff823c72012-09-05 13:44:32 +08004345 udev = hub->ports[port - 1]->child;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004346 if (!hub_is_superspeed(hdev)) {
4347 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4348 return 0;
4349 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4350 } else {
4351 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004352 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4353 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004354 return 0;
4355 }
4356
Sarah Sharp714b07b2012-01-24 13:53:18 -08004357 if (udev) {
4358 /* TRSMRCY = 10 msec */
4359 msleep(10);
4360
4361 usb_lock_device(udev);
4362 ret = usb_remote_wakeup(udev);
4363 usb_unlock_device(udev);
4364 if (ret < 0)
4365 connect_change = 1;
4366 } else {
4367 ret = -ENODEV;
4368 hub_port_disable(hub, port, 1);
4369 }
4370 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4371 port, ret);
4372 return connect_change;
4373}
4374
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375static void hub_events(void)
4376{
4377 struct list_head *tmp;
4378 struct usb_device *hdev;
4379 struct usb_interface *intf;
4380 struct usb_hub *hub;
4381 struct device *hub_dev;
4382 u16 hubstatus;
4383 u16 hubchange;
4384 u16 portstatus;
4385 u16 portchange;
4386 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004387 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
4389 /*
4390 * We restart the list every time to avoid a deadlock with
4391 * deleting hubs downstream from this one. This should be
4392 * safe since we delete the hub from the event list.
4393 * Not the most efficient, but avoids deadlocks.
4394 */
4395 while (1) {
4396
4397 /* Grab the first entry at the beginning of the list */
4398 spin_lock_irq(&hub_event_lock);
4399 if (list_empty(&hub_event_list)) {
4400 spin_unlock_irq(&hub_event_lock);
4401 break;
4402 }
4403
4404 tmp = hub_event_list.next;
4405 list_del_init(tmp);
4406
4407 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004408 kref_get(&hub->kref);
4409 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
Alan Sterne8054852007-05-04 11:55:11 -04004411 hdev = hub->hdev;
4412 hub_dev = hub->intfdev;
4413 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004414 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 hdev->state, hub->descriptor
4416 ? hub->descriptor->bNbrPorts
4417 : 0,
4418 /* NOTE: expects max 15 ports... */
4419 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004420 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 /* Lock the device, then check to see if we were
4423 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004424 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004425 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004426 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
4428 /* If the hub has died, clean up after it */
4429 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004430 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004431 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 goto loop;
4433 }
4434
Alan Stern40f122f2006-11-09 14:44:33 -05004435 /* Autoresume */
4436 ret = usb_autopm_get_interface(intf);
4437 if (ret) {
4438 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004440 }
4441
4442 /* If this is an inactive hub, do nothing */
4443 if (hub->quiescing)
4444 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
4446 if (hub->error) {
4447 dev_dbg (hub_dev, "resetting for error %d\n",
4448 hub->error);
4449
Ming Lei742120c2008-06-18 22:00:29 +08004450 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 if (ret) {
4452 dev_dbg (hub_dev,
4453 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004454 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 }
4456
4457 hub->nerrors = 0;
4458 hub->error = 0;
4459 }
4460
4461 /* deal with port status changes */
4462 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4463 if (test_bit(i, hub->busy_bits))
4464 continue;
4465 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004466 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004468 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 continue;
4470
4471 ret = hub_port_status(hub, i,
4472 &portstatus, &portchange);
4473 if (ret < 0)
4474 continue;
4475
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4477 clear_port_feature(hdev, i,
4478 USB_PORT_FEAT_C_CONNECTION);
4479 connect_change = 1;
4480 }
4481
4482 if (portchange & USB_PORT_STAT_C_ENABLE) {
4483 if (!connect_change)
4484 dev_dbg (hub_dev,
4485 "port %d enable change, "
4486 "status %08x\n",
4487 i, portstatus);
4488 clear_port_feature(hdev, i,
4489 USB_PORT_FEAT_C_ENABLE);
4490
4491 /*
4492 * EM interference sometimes causes badly
4493 * shielded USB devices to be shutdown by
4494 * the hub, this hack enables them again.
4495 * Works at least with mouse driver.
4496 */
4497 if (!(portstatus & USB_PORT_STAT_ENABLE)
4498 && !connect_change
Lan Tianyuff823c72012-09-05 13:44:32 +08004499 && hub->ports[i - 1]->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 dev_err (hub_dev,
4501 "port %i "
4502 "disabled by hub (EMI?), "
4503 "re-enabling...\n",
4504 i);
4505 connect_change = 1;
4506 }
4507 }
4508
Sarah Sharp72937e12012-01-24 11:46:50 -08004509 if (hub_handle_remote_wakeup(hub, i,
4510 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004511 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004512
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004514 u16 status = 0;
4515 u16 unused;
4516
4517 dev_dbg(hub_dev, "over-current change on port "
4518 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 clear_port_feature(hdev, i,
4520 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004521 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004522 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004523 hub_port_status(hub, i, &status, &unused);
4524 if (status & USB_PORT_STAT_OVERCURRENT)
4525 dev_err(hub_dev, "over-current "
4526 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 }
4528
4529 if (portchange & USB_PORT_STAT_C_RESET) {
4530 dev_dbg (hub_dev,
4531 "reset change on port %d\n",
4532 i);
4533 clear_port_feature(hdev, i,
4534 USB_PORT_FEAT_C_RESET);
4535 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004536 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4537 hub_is_superspeed(hub->hdev)) {
4538 dev_dbg(hub_dev,
4539 "warm reset change on port %d\n",
4540 i);
4541 clear_port_feature(hdev, i,
4542 USB_PORT_FEAT_C_BH_PORT_RESET);
4543 }
John Youndbe79bb2001-09-17 00:00:00 -07004544 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4545 clear_port_feature(hub->hdev, i,
4546 USB_PORT_FEAT_C_PORT_LINK_STATE);
4547 }
4548 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4549 dev_warn(hub_dev,
4550 "config error on port %d\n",
4551 i);
4552 clear_port_feature(hub->hdev, i,
4553 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Andiry Xu5e467f62011-04-27 18:07:54 +08004556 /* Warm reset a USB3 protocol port if it's in
4557 * SS.Inactive state.
4558 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02004559 if (hub_port_warm_reset_required(hub, portstatus)) {
Andiry Xu5e467f62011-04-27 18:07:54 +08004560 dev_dbg(hub_dev, "warm reset port %d\n", i);
Andiry Xu75d7cf72011-09-13 16:41:11 -07004561 hub_port_reset(hub, i, NULL,
4562 HUB_BH_RESET_TIME, true);
Andiry Xu5e467f62011-04-27 18:07:54 +08004563 }
4564
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 if (connect_change)
4566 hub_port_connect_change(hub, i,
4567 portstatus, portchange);
4568 } /* end for i */
4569
4570 /* deal with hub status changes */
4571 if (test_and_clear_bit(0, hub->event_bits) == 0)
4572 ; /* do nothing */
4573 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4574 dev_err (hub_dev, "get_hub_status failed\n");
4575 else {
4576 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4577 dev_dbg (hub_dev, "power change\n");
4578 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004579 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4580 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004581 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004582 else
4583 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 }
4585 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004586 u16 status = 0;
4587 u16 unused;
4588
4589 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004591 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004592 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004593 hub_hub_status(hub, &status, &unused);
4594 if (status & HUB_STATUS_OVERCURRENT)
4595 dev_err(hub_dev, "over-current "
4596 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 }
4598 }
4599
Alan Stern8e4ceb32009-12-07 13:01:37 -05004600 loop_autopm:
4601 /* Balance the usb_autopm_get_interface() above */
4602 usb_autopm_put_interface_no_suspend(intf);
4603 loop:
4604 /* Balance the usb_autopm_get_interface_no_resume() in
4605 * kick_khubd() and allow autosuspend.
4606 */
4607 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004608 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004610 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
4612 } /* end while (1) */
4613}
4614
4615static int hub_thread(void *__unused)
4616{
Alan Stern3bb1af52008-03-03 15:15:36 -05004617 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4618 * port handover. Otherwise it might see that a full-speed device
4619 * was gone before the EHCI controller had handed its port over to
4620 * the companion full-speed controller.
4621 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004622 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 do {
4625 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004626 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004627 !list_empty(&hub_event_list) ||
4628 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004629 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004631 pr_debug("%s: khubd exiting\n", usbcore_name);
4632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633}
4634
Németh Márton1e927d92010-01-10 15:34:53 +01004635static const struct usb_device_id hub_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4637 .bDeviceClass = USB_CLASS_HUB},
4638 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4639 .bInterfaceClass = USB_CLASS_HUB},
4640 { } /* Terminating entry */
4641};
4642
4643MODULE_DEVICE_TABLE (usb, hub_id_table);
4644
4645static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 .name = "hub",
4647 .probe = hub_probe,
4648 .disconnect = hub_disconnect,
4649 .suspend = hub_suspend,
4650 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004651 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004652 .pre_reset = hub_pre_reset,
4653 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004654 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004656 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657};
4658
4659int usb_hub_init(void)
4660{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 if (usb_register(&hub_driver) < 0) {
4662 printk(KERN_ERR "%s: can't register hub driver\n",
4663 usbcore_name);
4664 return -1;
4665 }
4666
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004667 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4668 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
4671 /* Fall through if kernel_thread failed */
4672 usb_deregister(&hub_driver);
4673 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4674
4675 return -1;
4676}
4677
4678void usb_hub_cleanup(void)
4679{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004680 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
4682 /*
4683 * Hub resources are freed for us by usb_deregister. It calls
4684 * usb_driver_purge on every device which in turn calls that
4685 * devices disconnect function if it is using this driver.
4686 * The hub_disconnect function takes care of releasing the
4687 * individual hub resources. -greg
4688 */
4689 usb_deregister(&hub_driver);
4690} /* usb_hub_cleanup() */
4691
Alan Sterneb764c42008-03-03 15:16:04 -05004692static int descriptors_changed(struct usb_device *udev,
4693 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694{
Alan Sterneb764c42008-03-03 15:16:04 -05004695 int changed = 0;
4696 unsigned index;
4697 unsigned serial_len = 0;
4698 unsigned len;
4699 unsigned old_length;
4700 int length;
4701 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
Alan Sterneb764c42008-03-03 15:16:04 -05004703 if (memcmp(&udev->descriptor, old_device_descriptor,
4704 sizeof(*old_device_descriptor)) != 0)
4705 return 1;
4706
4707 /* Since the idVendor, idProduct, and bcdDevice values in the
4708 * device descriptor haven't changed, we will assume the
4709 * Manufacturer and Product strings haven't changed either.
4710 * But the SerialNumber string could be different (e.g., a
4711 * different flash card of the same brand).
4712 */
4713 if (udev->serial)
4714 serial_len = strlen(udev->serial) + 1;
4715
4716 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004718 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4719 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 }
Alan Sterneb764c42008-03-03 15:16:04 -05004721
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004722 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 if (buf == NULL) {
4724 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4725 /* assume the worst */
4726 return 1;
4727 }
4728 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004729 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4731 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004732 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 dev_dbg(&udev->dev, "config index %d, error %d\n",
4734 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004735 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 break;
4737 }
4738 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4739 != 0) {
4740 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004741 index,
4742 ((struct usb_config_descriptor *) buf)->
4743 bConfigurationValue);
4744 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 break;
4746 }
4747 }
Alan Sterneb764c42008-03-03 15:16:04 -05004748
4749 if (!changed && serial_len) {
4750 length = usb_string(udev, udev->descriptor.iSerialNumber,
4751 buf, serial_len);
4752 if (length + 1 != serial_len) {
4753 dev_dbg(&udev->dev, "serial string error %d\n",
4754 length);
4755 changed = 1;
4756 } else if (memcmp(buf, udev->serial, length) != 0) {
4757 dev_dbg(&udev->dev, "serial string changed\n");
4758 changed = 1;
4759 }
4760 }
4761
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004763 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764}
4765
4766/**
Ming Lei742120c2008-06-18 22:00:29 +08004767 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4769 *
Alan Stern79efa092006-06-01 13:33:42 -04004770 * WARNING - don't use this routine to reset a composite device
4771 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004772 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 *
4774 * Do a port reset, reassign the device's address, and establish its
4775 * former operating configuration. If the reset fails, or the device's
4776 * descriptors change from their values before the reset, or the original
4777 * configuration and altsettings cannot be restored, a flag will be set
4778 * telling khubd to pretend the device has been disconnected and then
4779 * re-connected. All drivers will be unbound, and the device will be
4780 * re-enumerated and probed all over again.
4781 *
4782 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4783 * flagged for logical disconnection, or some other negative error code
4784 * if the reset wasn't even attempted.
4785 *
4786 * The caller must own the device lock. For example, it's safe to use
4787 * this from a driver probe() routine after downloading new firmware.
4788 * For calls that might not occur during probe(), drivers should lock
4789 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004790 *
4791 * Locking exception: This routine may also be called from within an
4792 * autoresume handler. Such usage won't conflict with other tasks
4793 * holding the device lock because these tasks should always call
4794 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 */
Ming Lei742120c2008-06-18 22:00:29 +08004796static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
4798 struct usb_device *parent_hdev = udev->parent;
4799 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004800 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004802 int i, ret = 0;
4803 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
4805 if (udev->state == USB_STATE_NOTATTACHED ||
4806 udev->state == USB_STATE_SUSPENDED) {
4807 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4808 udev->state);
4809 return -EINVAL;
4810 }
4811
4812 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004813 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004814 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 return -EISDIR;
4816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 parent_hub = hdev_to_hub(parent_hdev);
4818
Sarah Sharpf74631e2012-06-25 12:08:08 -07004819 /* Disable LPM and LTM while we reset the device and reinstall the alt
4820 * settings. Device-initiated LPM settings, and system exit latency
4821 * settings are cleared when the device is reset, so we have to set
4822 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004823 */
4824 ret = usb_unlocked_disable_lpm(udev);
4825 if (ret) {
4826 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4827 goto re_enumerate;
4828 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07004829 ret = usb_disable_ltm(udev);
4830 if (ret) {
4831 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4832 __func__);
4833 goto re_enumerate;
4834 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 set_bit(port1, parent_hub->busy_bits);
4837 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4838
4839 /* ep0 maxpacket size may change; let the HCD know about it.
4840 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004841 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004843 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 break;
4845 }
4846 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 if (ret < 0)
4849 goto re_enumerate;
4850
4851 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004852 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 dev_info(&udev->dev, "device firmware changed\n");
4854 udev->descriptor = descriptor; /* for disconnect() calls */
4855 goto re_enumerate;
4856 }
Alan Stern4fe03872009-02-26 10:21:02 -05004857
4858 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 if (!udev->actconfig)
4860 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004861
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004862 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004863 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4864 if (ret < 0) {
4865 dev_warn(&udev->dev,
4866 "Busted HC? Not enough HCD resources for "
4867 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004868 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004869 goto re_enumerate;
4870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4872 USB_REQ_SET_CONFIGURATION, 0,
4873 udev->actconfig->desc.bConfigurationValue, 0,
4874 NULL, 0, USB_CTRL_SET_TIMEOUT);
4875 if (ret < 0) {
4876 dev_err(&udev->dev,
4877 "can't restore configuration #%d (error=%d)\n",
4878 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004879 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 goto re_enumerate;
4881 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004882 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4884
Alan Stern4fe03872009-02-26 10:21:02 -05004885 /* Put interfaces back into the same altsettings as before.
4886 * Don't bother to send the Set-Interface request for interfaces
4887 * that were already in altsetting 0; besides being unnecessary,
4888 * many devices can't handle it. Instead just reset the host-side
4889 * endpoint state.
4890 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004892 struct usb_host_config *config = udev->actconfig;
4893 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 struct usb_interface_descriptor *desc;
4895
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05004897 if (desc->bAlternateSetting == 0) {
4898 usb_disable_interface(udev, intf, true);
4899 usb_enable_interface(udev, intf, true);
4900 ret = 0;
4901 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08004902 /* Let the bandwidth allocation function know that this
4903 * device has been reset, and it will have to use
4904 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004905 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08004906 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05004907 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4908 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08004909 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05004910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 if (ret < 0) {
4912 dev_err(&udev->dev, "failed to restore interface %d "
4913 "altsetting %d (error=%d)\n",
4914 desc->bInterfaceNumber,
4915 desc->bAlternateSetting,
4916 ret);
4917 goto re_enumerate;
4918 }
4919 }
4920
4921done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07004922 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Alan Stern78d9a482008-06-23 16:00:40 -04004923 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07004924 usb_enable_ltm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 return 0;
4926
4927re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004928 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 hub_port_logical_disconnect(parent_hub, port1);
4930 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931}
4932
4933/**
4934 * usb_reset_device - warn interface drivers and perform a USB port reset
4935 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4936 *
Alan Stern79efa092006-06-01 13:33:42 -04004937 * Warns all drivers bound to registered interfaces (using their pre_reset
4938 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08004939 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04004940 *
Alan Stern79efa092006-06-01 13:33:42 -04004941 * Return value is the same as for usb_reset_and_verify_device().
4942 *
4943 * The caller must own the device lock. For example, it's safe to use
4944 * this from a driver probe() routine after downloading new firmware.
4945 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08004946 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04004947 *
4948 * If an interface is currently being probed or disconnected, we assume
4949 * its driver knows how to handle resets. For all other interfaces,
4950 * if the driver doesn't have pre_reset and post_reset methods then
4951 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04004952 */
Ming Lei742120c2008-06-18 22:00:29 +08004953int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04004954{
4955 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05004956 int i;
Alan Stern79efa092006-06-01 13:33:42 -04004957 struct usb_host_config *config = udev->actconfig;
4958
4959 if (udev->state == USB_STATE_NOTATTACHED ||
4960 udev->state == USB_STATE_SUSPENDED) {
4961 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4962 udev->state);
4963 return -EINVAL;
4964 }
4965
Alan Stern645daaa2006-08-30 15:47:02 -04004966 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05004967 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04004968
Alan Stern79efa092006-06-01 13:33:42 -04004969 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004970 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004971 struct usb_interface *cintf = config->interface[i];
4972 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004973 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05004974
4975 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004976 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04004977 if (drv->pre_reset && drv->post_reset)
4978 unbind = (drv->pre_reset)(cintf);
4979 else if (cintf->condition ==
4980 USB_INTERFACE_BOUND)
4981 unbind = 1;
4982 if (unbind)
4983 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004984 }
4985 }
4986 }
4987
Ming Lei742120c2008-06-18 22:00:29 +08004988 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004989
4990 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004991 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004992 struct usb_interface *cintf = config->interface[i];
4993 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004994 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05004995
Alan Stern78d9a482008-06-23 16:00:40 -04004996 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004997 drv = to_usb_driver(cintf->dev.driver);
4998 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04004999 rebind = (drv->post_reset)(cintf);
5000 else if (cintf->condition ==
5001 USB_INTERFACE_BOUND)
5002 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005003 }
Alan Stern6c640942008-10-21 15:40:03 -04005004 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04005005 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005006 }
5007 }
5008
Alan Stern94fcda12006-11-20 11:38:46 -05005009 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005010 return ret;
5011}
Ming Lei742120c2008-06-18 22:00:29 +08005012EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005013
5014
5015/**
5016 * usb_queue_reset_device - Reset a USB device from an atomic context
5017 * @iface: USB interface belonging to the device to reset
5018 *
5019 * This function can be used to reset a USB device from an atomic
5020 * context, where usb_reset_device() won't work (as it blocks).
5021 *
5022 * Doing a reset via this method is functionally equivalent to calling
5023 * usb_reset_device(), except for the fact that it is delayed to a
5024 * workqueue. This means that any drivers bound to other interfaces
5025 * might be unbound, as well as users from usbfs in user space.
5026 *
5027 * Corner cases:
5028 *
5029 * - Scheduling two resets at the same time from two different drivers
5030 * attached to two different interfaces of the same device is
5031 * possible; depending on how the driver attached to each interface
5032 * handles ->pre_reset(), the second reset might happen or not.
5033 *
5034 * - If a driver is unbound and it had a pending reset, the reset will
5035 * be cancelled.
5036 *
5037 * - This function can be called during .probe() or .disconnect()
5038 * times. On return from .disconnect(), any pending resets will be
5039 * cancelled.
5040 *
5041 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5042 * does its own.
5043 *
5044 * NOTE: We don't do any reference count tracking because it is not
5045 * needed. The lifecycle of the work_struct is tied to the
5046 * usb_interface. Before destroying the interface we cancel the
5047 * work_struct, so the fact that work_struct is queued and or
5048 * running means the interface (and thus, the device) exist and
5049 * are referenced.
5050 */
5051void usb_queue_reset_device(struct usb_interface *iface)
5052{
5053 schedule_work(&iface->reset_ws);
5054}
5055EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c72012-09-05 13:44:32 +08005056
5057/**
5058 * usb_hub_find_child - Get the pointer of child device
5059 * attached to the port which is specified by @port1.
5060 * @hdev: USB device belonging to the usb hub
5061 * @port1: port num to indicate which port the child device
5062 * is attached to.
5063 *
5064 * USB drivers call this function to get hub's child device
5065 * pointer.
5066 *
5067 * Return NULL if input param is invalid and
5068 * child's usb_device pointer if non-NULL.
5069 */
5070struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5071 int port1)
5072{
5073 struct usb_hub *hub = hdev_to_hub(hdev);
5074
5075 if (port1 < 1 || port1 > hdev->maxchild)
5076 return NULL;
5077 return hub->ports[port1 - 1]->child;
5078}
5079EXPORT_SYMBOL_GPL(usb_hub_find_child);