blob: 1af04bdeaf0c1884487ed830bcee7dd32ee720b6 [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;
Lan Tianyu05f91682012-09-05 13:44:34 +080046 enum usb_port_connect_type connect_type;
Lan Tianyufa2a9562012-09-05 13:44:31 +080047};
48
Alan Stern1bb5f662006-11-06 11:56:13 -050049struct usb_hub {
50 struct device *intfdev; /* the "interface" device */
51 struct usb_device *hdev;
Alan Sterne8054852007-05-04 11:55:11 -040052 struct kref kref;
Alan Stern1bb5f662006-11-06 11:56:13 -050053 struct urb *urb; /* for interrupt polling pipe */
54
55 /* buffer for urb ... with extra space in case of babble */
56 char (*buffer)[8];
Alan Stern1bb5f662006-11-06 11:56:13 -050057 union {
58 struct usb_hub_status hub;
59 struct usb_port_status port;
60 } *status; /* buffer for status reports */
Alan Sterndb90e7a2007-02-05 09:56:15 -050061 struct mutex status_mutex; /* for the status buffer */
Alan Stern1bb5f662006-11-06 11:56:13 -050062
63 int error; /* last reported error */
64 int nerrors; /* track consecutive errors */
65
66 struct list_head event_list; /* hubs w/data or errs ready */
67 unsigned long event_bits[1]; /* status change bitmask */
68 unsigned long change_bits[1]; /* ports with logical connect
69 status change */
70 unsigned long busy_bits[1]; /* ports being reset or
71 resumed */
Alan Stern253e0572009-10-27 15:20:13 -040072 unsigned long removed_bits[1]; /* ports with a "removed"
73 device present */
Sarah Sharp4ee823b2011-11-14 18:00:01 -080074 unsigned long wakeup_bits[1]; /* ports that have signaled
75 remote wakeup */
Alan Stern1bb5f662006-11-06 11:56:13 -050076#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
77#error event_bits[] is too short!
78#endif
79
80 struct usb_hub_descriptor *descriptor; /* class descriptor */
81 struct usb_tt tt; /* Transaction Translator */
82
83 unsigned mA_per_port; /* current for each child */
84
85 unsigned limited_power:1;
86 unsigned quiescing:1;
Alan Sterne8054852007-05-04 11:55:11 -040087 unsigned disconnected:1;
Alan Stern1bb5f662006-11-06 11:56:13 -050088
89 unsigned has_indicators:1;
90 u8 indicator[USB_MAXCHILDREN];
David Howells6d5aefb2006-12-05 19:36:26 +000091 struct delayed_work leds;
Alan Stern8520f382008-09-22 14:44:26 -040092 struct delayed_work init_work;
Lan Tianyufa2a9562012-09-05 13:44:31 +080093 struct usb_port **ports;
Alan Stern1bb5f662006-11-06 11:56:13 -050094};
95
John Youndbe79bb2001-09-17 00:00:00 -070096static inline int hub_is_superspeed(struct usb_device *hdev)
97{
Aman Deep7bf01182011-12-08 12:05:22 +053098 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
John Youndbe79bb2001-09-17 00:00:00 -070099}
Alan Stern1bb5f662006-11-06 11:56:13 -0500100
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -0700101/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -0500102 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
104static DEFINE_SPINLOCK(device_state_lock);
105
106/* khubd's worklist and its lock */
107static DEFINE_SPINLOCK(hub_event_lock);
108static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
109
110/* Wakes up khubd */
111static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
112
akpm@osdl.org9c8d6172005-06-20 14:29:58 -0700113static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/* cycle leds on hubs that aren't blinking for attention */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030116static bool blinkenlights = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117module_param (blinkenlights, bool, S_IRUGO);
118MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
119
120/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200121 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
122 * 10 seconds to send reply for the initial 64-byte descriptor request.
123 */
124/* define initial 64-byte descriptor request timeout in milliseconds */
125static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
126module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +0800127MODULE_PARM_DESC(initial_descriptor_timeout,
128 "initial 64-byte descriptor request timeout in milliseconds "
129 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200130
131/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * As of 2.6.10 we introduce a new USB device initialization scheme which
133 * closely resembles the way Windows works. Hopefully it will be compatible
134 * with a wider range of devices than the old scheme. However some previously
135 * working devices may start giving rise to "device not accepting address"
136 * errors; if that happens the user can try the old scheme by adjusting the
137 * following module parameters.
138 *
139 * For maximum flexibility there are two boolean parameters to control the
140 * hub driver's behavior. On the first initialization attempt, if the
141 * "old_scheme_first" parameter is set then the old scheme will be used,
142 * otherwise the new scheme is used. If that fails and "use_both_schemes"
143 * is set, then the driver will make another attempt, using the other scheme.
144 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030145static bool old_scheme_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
147MODULE_PARM_DESC(old_scheme_first,
148 "start with the old device initialization scheme");
149
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030150static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
152MODULE_PARM_DESC(use_both_schemes,
153 "try the other device initialization scheme if the "
154 "first one fails");
155
Alan Stern32fe0192007-10-10 16:27:07 -0400156/* Mutual exclusion for EHCI CF initialization. This interferes with
157 * port reset on some companion controllers.
158 */
159DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
160EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
161
Alan Stern948fea32008-04-28 11:07:07 -0400162#define HUB_DEBOUNCE_TIMEOUT 1500
163#define HUB_DEBOUNCE_STEP 25
164#define HUB_DEBOUNCE_STABLE 100
165
Lan Tianyufa2a9562012-09-05 13:44:31 +0800166#define to_usb_port(_dev) \
167 container_of(_dev, struct usb_port, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Ming Lei742120c2008-06-18 22:00:29 +0800169static int usb_reset_and_verify_device(struct usb_device *udev);
170
Sarah Sharp131dec32010-12-06 21:00:19 -0800171static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Sarah Sharp131dec32010-12-06 21:00:19 -0800173 if (hub_is_superspeed(hub->hdev))
174 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500175 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500177 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return "1.5 Mb/s";
179 else
180 return "12 Mb/s";
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183/* Note that hdev or one of its children must be locked! */
Alan Stern251180842009-07-22 14:41:18 -0400184static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Lan Tianyuff823c72012-09-05 13:44:32 +0800186 if (!hdev || !hdev->actconfig || !hdev->maxchild)
Alan Stern251180842009-07-22 14:41:18 -0400187 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return usb_get_intfdata(hdev->actconfig->interface[0]);
189}
190
Sarah Sharpd9b20992012-02-20 08:31:26 -0800191static int usb_device_supports_lpm(struct usb_device *udev)
192{
193 /* USB 2.1 (and greater) devices indicate LPM support through
194 * their USB 2.0 Extended Capabilities BOS descriptor.
195 */
196 if (udev->speed == USB_SPEED_HIGH) {
197 if (udev->bos->ext_cap &&
198 (USB_LPM_SUPPORT &
199 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
200 return 1;
201 return 0;
202 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800203
204 /* All USB 3.0 must support LPM, but we need their max exit latency
205 * information from the SuperSpeed Extended Capabilities BOS descriptor.
206 */
207 if (!udev->bos->ss_cap) {
208 dev_warn(&udev->dev, "No LPM exit latency info found. "
209 "Power management will be impacted.\n");
210 return 0;
211 }
212 if (udev->parent->lpm_capable)
213 return 1;
214
215 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. "
216 "Power management will be impacted.\n");
Sarah Sharpd9b20992012-02-20 08:31:26 -0800217 return 0;
218}
219
Sarah Sharp51e0a012012-02-20 12:02:19 -0800220/*
221 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
222 * either U1 or U2.
223 */
224static void usb_set_lpm_mel(struct usb_device *udev,
225 struct usb3_lpm_parameters *udev_lpm_params,
226 unsigned int udev_exit_latency,
227 struct usb_hub *hub,
228 struct usb3_lpm_parameters *hub_lpm_params,
229 unsigned int hub_exit_latency)
230{
231 unsigned int total_mel;
232 unsigned int device_mel;
233 unsigned int hub_mel;
234
235 /*
236 * Calculate the time it takes to transition all links from the roothub
237 * to the parent hub into U0. The parent hub must then decode the
238 * packet (hub header decode latency) to figure out which port it was
239 * bound for.
240 *
241 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
242 * means 0.1us). Multiply that by 100 to get nanoseconds.
243 */
244 total_mel = hub_lpm_params->mel +
245 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
246
247 /*
248 * How long will it take to transition the downstream hub's port into
249 * U0? The greater of either the hub exit latency or the device exit
250 * latency.
251 *
252 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
253 * Multiply that by 1000 to get nanoseconds.
254 */
255 device_mel = udev_exit_latency * 1000;
256 hub_mel = hub_exit_latency * 1000;
257 if (device_mel > hub_mel)
258 total_mel += device_mel;
259 else
260 total_mel += hub_mel;
261
262 udev_lpm_params->mel = total_mel;
263}
264
265/*
266 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
267 * a transition from either U1 or U2.
268 */
269static void usb_set_lpm_pel(struct usb_device *udev,
270 struct usb3_lpm_parameters *udev_lpm_params,
271 unsigned int udev_exit_latency,
272 struct usb_hub *hub,
273 struct usb3_lpm_parameters *hub_lpm_params,
274 unsigned int hub_exit_latency,
275 unsigned int port_to_port_exit_latency)
276{
277 unsigned int first_link_pel;
278 unsigned int hub_pel;
279
280 /*
281 * First, the device sends an LFPS to transition the link between the
282 * device and the parent hub into U0. The exit latency is the bigger of
283 * the device exit latency or the hub exit latency.
284 */
285 if (udev_exit_latency > hub_exit_latency)
286 first_link_pel = udev_exit_latency * 1000;
287 else
288 first_link_pel = hub_exit_latency * 1000;
289
290 /*
291 * When the hub starts to receive the LFPS, there is a slight delay for
292 * it to figure out that one of the ports is sending an LFPS. Then it
293 * will forward the LFPS to its upstream link. The exit latency is the
294 * delay, plus the PEL that we calculated for this hub.
295 */
296 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
297
298 /*
299 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
300 * is the greater of the two exit latencies.
301 */
302 if (first_link_pel > hub_pel)
303 udev_lpm_params->pel = first_link_pel;
304 else
305 udev_lpm_params->pel = hub_pel;
306}
307
308/*
309 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
310 * when a device initiates a transition to U0, until when it will receive the
311 * first packet from the host controller.
312 *
313 * Section C.1.5.1 describes the four components to this:
314 * - t1: device PEL
315 * - t2: time for the ERDY to make it from the device to the host.
316 * - t3: a host-specific delay to process the ERDY.
317 * - t4: time for the packet to make it from the host to the device.
318 *
319 * t3 is specific to both the xHCI host and the platform the host is integrated
320 * into. The Intel HW folks have said it's negligible, FIXME if a different
321 * vendor says otherwise.
322 */
323static void usb_set_lpm_sel(struct usb_device *udev,
324 struct usb3_lpm_parameters *udev_lpm_params)
325{
326 struct usb_device *parent;
327 unsigned int num_hubs;
328 unsigned int total_sel;
329
330 /* t1 = device PEL */
331 total_sel = udev_lpm_params->pel;
332 /* How many external hubs are in between the device & the root port. */
333 for (parent = udev->parent, num_hubs = 0; parent->parent;
334 parent = parent->parent)
335 num_hubs++;
336 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
337 if (num_hubs > 0)
338 total_sel += 2100 + 250 * (num_hubs - 1);
339
340 /* t4 = 250ns * num_hubs */
341 total_sel += 250 * num_hubs;
342
343 udev_lpm_params->sel = total_sel;
344}
345
346static void usb_set_lpm_parameters(struct usb_device *udev)
347{
348 struct usb_hub *hub;
349 unsigned int port_to_port_delay;
350 unsigned int udev_u1_del;
351 unsigned int udev_u2_del;
352 unsigned int hub_u1_del;
353 unsigned int hub_u2_del;
354
355 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
356 return;
357
358 hub = hdev_to_hub(udev->parent);
359 /* It doesn't take time to transition the roothub into U0, since it
360 * doesn't have an upstream link.
361 */
362 if (!hub)
363 return;
364
365 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
366 udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
367 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
368 hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
369
370 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
371 hub, &udev->parent->u1_params, hub_u1_del);
372
373 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
374 hub, &udev->parent->u2_params, hub_u2_del);
375
376 /*
377 * Appendix C, section C.2.2.2, says that there is a slight delay from
378 * when the parent hub notices the downstream port is trying to
379 * transition to U0 to when the hub initiates a U0 transition on its
380 * upstream port. The section says the delays are tPort2PortU1EL and
381 * tPort2PortU2EL, but it doesn't define what they are.
382 *
383 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
384 * about the same delays. Use the maximum delay calculations from those
385 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
386 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
387 * assume the device exit latencies they are talking about are the hub
388 * exit latencies.
389 *
390 * What do we do if the U2 exit latency is less than the U1 exit
391 * latency? It's possible, although not likely...
392 */
393 port_to_port_delay = 1;
394
395 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
396 hub, &udev->parent->u1_params, hub_u1_del,
397 port_to_port_delay);
398
399 if (hub_u2_del > hub_u1_del)
400 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
401 else
402 port_to_port_delay = 1 + hub_u1_del;
403
404 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
405 hub, &udev->parent->u2_params, hub_u2_del,
406 port_to_port_delay);
407
408 /* Now that we've got PEL, calculate SEL. */
409 usb_set_lpm_sel(udev, &udev->u1_params);
410 usb_set_lpm_sel(udev, &udev->u2_params);
411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/* USB 2.0 spec Section 11.24.4.5 */
John Youndbe79bb2001-09-17 00:00:00 -0700414static int get_hub_descriptor(struct usb_device *hdev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
John Youndbe79bb2001-09-17 00:00:00 -0700416 int i, ret, size;
417 unsigned dtype;
418
419 if (hub_is_superspeed(hdev)) {
420 dtype = USB_DT_SS_HUB;
421 size = USB_DT_SS_HUB_SIZE;
422 } else {
423 dtype = USB_DT_HUB;
424 size = sizeof(struct usb_hub_descriptor);
425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 for (i = 0; i < 3; i++) {
428 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
429 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
John Youndbe79bb2001-09-17 00:00:00 -0700430 dtype << 8, 0, data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 USB_CTRL_GET_TIMEOUT);
432 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
433 return ret;
434 }
435 return -EINVAL;
436}
437
438/*
439 * USB 2.0 spec Section 11.24.2.1
440 */
441static int clear_hub_feature(struct usb_device *hdev, int feature)
442{
443 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
444 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
445}
446
447/*
448 * USB 2.0 spec Section 11.24.2.2
449 */
450static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
451{
452 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
453 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
454 NULL, 0, 1000);
455}
456
457/*
458 * USB 2.0 spec Section 11.24.2.13
459 */
460static int set_port_feature(struct usb_device *hdev, int port1, int feature)
461{
462 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
463 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
464 NULL, 0, 1000);
465}
466
467/*
468 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
469 * for info about using port indicators
470 */
471static void set_port_led(
472 struct usb_hub *hub,
473 int port1,
474 int selector
475)
476{
477 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
478 USB_PORT_FEAT_INDICATOR);
479 if (status < 0)
480 dev_dbg (hub->intfdev,
481 "port %d indicator %s status %d\n",
482 port1,
483 ({ char *s; switch (selector) {
484 case HUB_LED_AMBER: s = "amber"; break;
485 case HUB_LED_GREEN: s = "green"; break;
486 case HUB_LED_OFF: s = "off"; break;
487 case HUB_LED_AUTO: s = "auto"; break;
488 default: s = "??"; break;
489 }; s; }),
490 status);
491}
492
493#define LED_CYCLE_PERIOD ((2*HZ)/3)
494
David Howellsc4028952006-11-22 14:57:56 +0000495static void led_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
David Howellsc4028952006-11-22 14:57:56 +0000497 struct usb_hub *hub =
498 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct usb_device *hdev = hub->hdev;
500 unsigned i;
501 unsigned changed = 0;
502 int cursor = -1;
503
504 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
505 return;
506
507 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
508 unsigned selector, mode;
509
510 /* 30%-50% duty cycle */
511
512 switch (hub->indicator[i]) {
513 /* cycle marker */
514 case INDICATOR_CYCLE:
515 cursor = i;
516 selector = HUB_LED_AUTO;
517 mode = INDICATOR_AUTO;
518 break;
519 /* blinking green = sw attention */
520 case INDICATOR_GREEN_BLINK:
521 selector = HUB_LED_GREEN;
522 mode = INDICATOR_GREEN_BLINK_OFF;
523 break;
524 case INDICATOR_GREEN_BLINK_OFF:
525 selector = HUB_LED_OFF;
526 mode = INDICATOR_GREEN_BLINK;
527 break;
528 /* blinking amber = hw attention */
529 case INDICATOR_AMBER_BLINK:
530 selector = HUB_LED_AMBER;
531 mode = INDICATOR_AMBER_BLINK_OFF;
532 break;
533 case INDICATOR_AMBER_BLINK_OFF:
534 selector = HUB_LED_OFF;
535 mode = INDICATOR_AMBER_BLINK;
536 break;
537 /* blink green/amber = reserved */
538 case INDICATOR_ALT_BLINK:
539 selector = HUB_LED_GREEN;
540 mode = INDICATOR_ALT_BLINK_OFF;
541 break;
542 case INDICATOR_ALT_BLINK_OFF:
543 selector = HUB_LED_AMBER;
544 mode = INDICATOR_ALT_BLINK;
545 break;
546 default:
547 continue;
548 }
549 if (selector != HUB_LED_AUTO)
550 changed = 1;
551 set_port_led(hub, i + 1, selector);
552 hub->indicator[i] = mode;
553 }
554 if (!changed && blinkenlights) {
555 cursor++;
556 cursor %= hub->descriptor->bNbrPorts;
557 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
558 hub->indicator[cursor] = INDICATOR_CYCLE;
559 changed++;
560 }
561 if (changed)
562 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
563}
564
565/* use a short timeout for hub/port status fetches */
566#define USB_STS_TIMEOUT 1000
567#define USB_STS_RETRIES 5
568
569/*
570 * USB 2.0 spec Section 11.24.2.6
571 */
572static int get_hub_status(struct usb_device *hdev,
573 struct usb_hub_status *data)
574{
575 int i, status = -ETIMEDOUT;
576
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200577 for (i = 0; i < USB_STS_RETRIES &&
578 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
580 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
581 data, sizeof(*data), USB_STS_TIMEOUT);
582 }
583 return status;
584}
585
586/*
587 * USB 2.0 spec Section 11.24.2.7
588 */
589static int get_port_status(struct usb_device *hdev, int port1,
590 struct usb_port_status *data)
591{
592 int i, status = -ETIMEDOUT;
593
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200594 for (i = 0; i < USB_STS_RETRIES &&
595 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
597 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
598 data, sizeof(*data), USB_STS_TIMEOUT);
599 }
600 return status;
601}
602
Alan Stern3eb14912008-03-03 15:15:43 -0500603static int hub_port_status(struct usb_hub *hub, int port1,
604 u16 *status, u16 *change)
605{
606 int ret;
607
608 mutex_lock(&hub->status_mutex);
609 ret = get_port_status(hub->hdev, port1, &hub->status->port);
610 if (ret < 4) {
611 dev_err(hub->intfdev,
612 "%s failed (err = %d)\n", __func__, ret);
613 if (ret >= 0)
614 ret = -EIO;
615 } else {
616 *status = le16_to_cpu(hub->status->port.wPortStatus);
617 *change = le16_to_cpu(hub->status->port.wPortChange);
John Youndbe79bb2001-09-17 00:00:00 -0700618
Alan Stern3eb14912008-03-03 15:15:43 -0500619 ret = 0;
620 }
621 mutex_unlock(&hub->status_mutex);
622 return ret;
623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625static void kick_khubd(struct usb_hub *hub)
626{
627 unsigned long flags;
628
629 spin_lock_irqsave(&hub_event_lock, flags);
Roel Kluin2e2c5ee2007-10-26 23:54:35 +0200630 if (!hub->disconnected && list_empty(&hub->event_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 list_add_tail(&hub->event_list, &hub_event_list);
Alan Stern8e4ceb32009-12-07 13:01:37 -0500632
633 /* Suppress autosuspend until khubd runs */
634 usb_autopm_get_interface_no_resume(
635 to_usb_interface(hub->intfdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 wake_up(&khubd_wait);
637 }
638 spin_unlock_irqrestore(&hub_event_lock, flags);
639}
640
641void usb_kick_khubd(struct usb_device *hdev)
642{
Alan Stern251180842009-07-22 14:41:18 -0400643 struct usb_hub *hub = hdev_to_hub(hdev);
644
645 if (hub)
646 kick_khubd(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800649/*
650 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
651 * Notification, which indicates it had initiated remote wakeup.
652 *
653 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
654 * device initiates resume, so the USB core will not receive notice of the
655 * resume through the normal hub interrupt URB.
656 */
657void usb_wakeup_notification(struct usb_device *hdev,
658 unsigned int portnum)
659{
660 struct usb_hub *hub;
661
662 if (!hdev)
663 return;
664
665 hub = hdev_to_hub(hdev);
666 if (hub) {
667 set_bit(portnum, hub->wakeup_bits);
668 kick_khubd(hub);
669 }
670}
671EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100674static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200676 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400677 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100678 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 unsigned long bits;
680
Alan Sterne0152682007-08-24 15:42:52 -0400681 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 case -ENOENT: /* synchronous unlink */
683 case -ECONNRESET: /* async unlink */
684 case -ESHUTDOWN: /* hardware going away */
685 return;
686
687 default: /* presumably an error */
688 /* Cause a hub reset after 10 consecutive errors */
Alan Sterne0152682007-08-24 15:42:52 -0400689 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if ((++hub->nerrors < 10) || hub->error)
691 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400692 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* let khubd handle things */
696 case 0: /* we got data: port status changed */
697 bits = 0;
698 for (i = 0; i < urb->actual_length; ++i)
699 bits |= ((unsigned long) ((*hub->buffer)[i]))
700 << (i*8);
701 hub->event_bits[0] = bits;
702 break;
703 }
704
705 hub->nerrors = 0;
706
707 /* Something happened, let khubd figure it out */
708 kick_khubd(hub);
709
710resubmit:
711 if (hub->quiescing)
712 return;
713
714 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
715 && status != -ENODEV && status != -EPERM)
716 dev_err (hub->intfdev, "resubmit --> %d\n", status);
717}
718
719/* USB 2.0 spec Section 11.24.2.3 */
720static inline int
721hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
722{
Alan Sternc2f65952009-11-18 11:37:15 -0500723 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
725 tt, NULL, 0, 1000);
726}
727
728/*
729 * enumeration blocks khubd for a long time. we use keventd instead, since
730 * long blocking there is the exception, not the rule. accordingly, HCDs
731 * talking to TTs must queue control transfers (not just bulk and iso), so
732 * both can talk to the same hub concurrently.
733 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400734static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
David Howellsc4028952006-11-22 14:57:56 +0000736 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400737 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 unsigned long flags;
Mark Lord55e5fdf2007-05-14 19:48:02 -0400739 int limit = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 spin_lock_irqsave (&hub->tt.lock, flags);
Octavian Purdila3b6054d2012-10-01 22:21:12 +0300742 while (!list_empty(&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400743 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 struct usb_tt_clear *clear;
745 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400746 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 int status;
748
Octavian Purdila3b6054d2012-10-01 22:21:12 +0300749 if (!hub->quiescing && --limit < 0)
750 break;
751
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400752 next = hub->tt.clear_list.next;
753 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 list_del (&clear->clear_list);
755
756 /* drop lock so HCD can concurrently report other TT errors */
757 spin_unlock_irqrestore (&hub->tt.lock, flags);
758 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (status)
760 dev_err (&hdev->dev,
761 "clear tt %d (%04x) error %d\n",
762 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400763
764 /* Tell the HCD, even if the operation failed */
765 drv = clear->hcd->driver;
766 if (drv->clear_tt_buffer_complete)
767 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
768
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700769 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400770 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772 spin_unlock_irqrestore (&hub->tt.lock, flags);
773}
774
775/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400776 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
777 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 *
779 * High speed HCDs use this to tell the hub driver that some split control or
780 * bulk transaction failed in a way that requires clearing internal state of
781 * a transaction translator. This is normally detected (and reported) from
782 * interrupt context.
783 *
784 * It may not be possible for that hub to handle additional full (or low)
785 * speed transactions until that state is fully cleared out.
786 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400787int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400789 struct usb_device *udev = urb->dev;
790 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 struct usb_tt *tt = udev->tt;
792 unsigned long flags;
793 struct usb_tt_clear *clear;
794
795 /* we've got to cope with an arbitrary number of pending TT clears,
796 * since each TT has "at least two" buffers that can need it (and
797 * there can be many TTs per hub). even if they're uncommon.
798 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800799 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
801 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400802 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
805 /* info that CLEAR_TT_BUFFER needs */
806 clear->tt = tt->multi ? udev->ttport : 1;
807 clear->devinfo = usb_pipeendpoint (pipe);
808 clear->devinfo |= udev->devnum << 4;
809 clear->devinfo |= usb_pipecontrol (pipe)
810 ? (USB_ENDPOINT_XFER_CONTROL << 11)
811 : (USB_ENDPOINT_XFER_BULK << 11);
812 if (usb_pipein (pipe))
813 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400814
815 /* info for completion callback */
816 clear->hcd = bus_to_hcd(udev->bus);
817 clear->ep = urb->ep;
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /* tell keventd to clear state for this TT */
820 spin_lock_irqsave (&tt->lock, flags);
821 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400822 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400826EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Alan Stern8520f382008-09-22 14:44:26 -0400828/* If do_delay is false, return the number of milliseconds the caller
829 * needs to delay.
830 */
831static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700834 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern8520f382008-09-22 14:44:26 -0400835 unsigned delay;
Alan Stern4489a572006-04-27 15:54:22 -0400836 u16 wHubCharacteristics =
837 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Alan Stern4489a572006-04-27 15:54:22 -0400839 /* Enable power on each port. Some hubs have reserved values
840 * of LPSM (> 2) in their descriptors, even though they are
841 * USB 2.0 hubs. Some hubs do not implement port-power switching
842 * but only emulate it. In all cases, the ports won't work
843 * unless we send these messages to the hub.
844 */
845 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400847 else
848 dev_dbg(hub->intfdev, "trying to enable port power on "
849 "non-switchable hub\n");
850 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
Greg Kroah-Hartmana0693bd2012-09-24 13:04:02 -0700851 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
David Brownellb7896962005-08-31 10:41:44 -0700853 /* Wait at least 100 msec for power to become stable */
Alan Stern8520f382008-09-22 14:44:26 -0400854 delay = max(pgood_delay, (unsigned) 100);
855 if (do_delay)
856 msleep(delay);
857 return delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860static int hub_hub_status(struct usb_hub *hub,
861 u16 *status, u16 *change)
862{
863 int ret;
864
Alan Sterndb90e7a2007-02-05 09:56:15 -0500865 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 ret = get_hub_status(hub->hdev, &hub->status->hub);
867 if (ret < 0)
868 dev_err (hub->intfdev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800869 "%s failed (err = %d)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 else {
871 *status = le16_to_cpu(hub->status->hub.wHubStatus);
872 *change = le16_to_cpu(hub->status->hub.wHubChange);
873 ret = 0;
874 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500875 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return ret;
877}
878
Alan Stern8b28c752005-08-10 17:04:13 -0400879static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
880{
881 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400882 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400883
Lan Tianyuff823c72012-09-05 13:44:32 +0800884 if (hub->ports[port1 - 1]->child && set_state)
885 usb_set_device_state(hub->ports[port1 - 1]->child,
Alan Stern8b28c752005-08-10 17:04:13 -0400886 USB_STATE_NOTATTACHED);
John Youndbe79bb2001-09-17 00:00:00 -0700887 if (!hub->error && !hub_is_superspeed(hub->hdev))
Alan Stern0458d5b2007-05-04 11:52:20 -0400888 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
Alan Stern8b28c752005-08-10 17:04:13 -0400889 if (ret)
890 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
Alan Stern0458d5b2007-05-04 11:52:20 -0400891 port1, ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400892 return ret;
893}
894
Alan Stern0458d5b2007-05-04 11:52:20 -0400895/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800896 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400897 * time later khubd will disconnect() any existing usb_device on the port
898 * and will re-enumerate if there actually is a device attached.
899 */
900static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
901{
902 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
903 hub_port_disable(hub, port1, 1);
904
905 /* FIXME let caller ask to power down the port:
906 * - some devices won't enumerate without a VBUS power cycle
907 * - SRP saves power that way
908 * - ... new call, TBD ...
909 * That's easy if this hub can switch power per-port, and
910 * khubd reactivates the port later (timer, SRP, etc).
911 * Powerdown must be optional, because of reset/DFU.
912 */
913
914 set_bit(port1, hub->change_bits);
915 kick_khubd(hub);
916}
917
Alan Stern253e0572009-10-27 15:20:13 -0400918/**
919 * usb_remove_device - disable a device's port on its parent hub
920 * @udev: device to be disabled and removed
921 * Context: @udev locked, must be able to sleep.
922 *
923 * After @udev's port has been disabled, khubd is notified and it will
924 * see that the device has been disconnected. When the device is
925 * physically unplugged and something is plugged in, the events will
926 * be received and processed normally.
927 */
928int usb_remove_device(struct usb_device *udev)
929{
930 struct usb_hub *hub;
931 struct usb_interface *intf;
932
933 if (!udev->parent) /* Can't remove a root hub */
934 return -EINVAL;
935 hub = hdev_to_hub(udev->parent);
936 intf = to_usb_interface(hub->intfdev);
937
938 usb_autopm_get_interface(intf);
939 set_bit(udev->portnum, hub->removed_bits);
940 hub_port_logical_disconnect(hub, udev->portnum);
941 usb_autopm_put_interface(intf);
942 return 0;
943}
944
Alan Stern6ee0b272008-04-28 11:06:42 -0400945enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -0500946 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -0400947 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -0400948};
Alan Stern5e6effa2008-03-03 15:15:51 -0500949
Alan Stern8520f382008-09-22 14:44:26 -0400950static void hub_init_func2(struct work_struct *ws);
951static void hub_init_func3(struct work_struct *ws);
952
Alan Sternf2835212008-04-28 11:07:17 -0400953static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500954{
955 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -0800956 struct usb_hcd *hcd;
957 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -0500958 int port1;
Alan Sternf2835212008-04-28 11:07:17 -0400959 int status;
Alan Stern948fea32008-04-28 11:07:07 -0400960 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -0400961 unsigned delay;
962
963 /* Continue a partial initialization */
964 if (type == HUB_INIT2)
965 goto init2;
966 if (type == HUB_INIT3)
967 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -0500968
Elric Fua45aa3b2012-02-18 13:32:27 +0800969 /* The superspeed hub except for root hub has to use Hub Depth
970 * value as an offset into the route string to locate the bits
971 * it uses to determine the downstream port number. So hub driver
972 * should send a set hub depth request to superspeed hub after
973 * the superspeed hub is set configuration in initialization or
974 * reset procedure.
975 *
976 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -0400977 * For any other type of activation, turn it on.
978 */
Alan Stern8520f382008-09-22 14:44:26 -0400979 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +0800980 if (hdev->parent && hub_is_superspeed(hdev)) {
981 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
982 HUB_SET_DEPTH, USB_RT_HUB,
983 hdev->level - 1, 0, NULL, 0,
984 USB_CTRL_SET_TIMEOUT);
985 if (ret < 0)
986 dev_err(hub->intfdev,
987 "set hub depth failed\n");
988 }
Alan Stern8520f382008-09-22 14:44:26 -0400989
990 /* Speed up system boot by using a delayed_work for the
991 * hub's initial power-up delays. This is pretty awkward
992 * and the implementation looks like a home-brewed sort of
993 * setjmp/longjmp, but it saves at least 100 ms for each
994 * root hub (assuming usbcore is compiled into the kernel
995 * rather than as a module). It adds up.
996 *
997 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
998 * because for those activation types the ports have to be
999 * operational when we return. In theory this could be done
1000 * for HUB_POST_RESET, but it's easier not to.
1001 */
1002 if (type == HUB_INIT) {
1003 delay = hub_power_on(hub, false);
1004 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
1005 schedule_delayed_work(&hub->init_work,
1006 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -04001007
1008 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -05001009 usb_autopm_get_interface_no_resume(
1010 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -04001011 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001012 } else if (type == HUB_RESET_RESUME) {
1013 /* The internal host controller state for the hub device
1014 * may be gone after a host power loss on system resume.
1015 * Update the device's info so the HW knows it's a hub.
1016 */
1017 hcd = bus_to_hcd(hdev->bus);
1018 if (hcd->driver->update_hub_device) {
1019 ret = hcd->driver->update_hub_device(hcd, hdev,
1020 &hub->tt, GFP_NOIO);
1021 if (ret < 0) {
1022 dev_err(hub->intfdev, "Host not "
1023 "accepting hub info "
1024 "update.\n");
1025 dev_err(hub->intfdev, "LS/FS devices "
1026 "and hubs may not work "
1027 "under this hub\n.");
1028 }
1029 }
1030 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001031 } else {
1032 hub_power_on(hub, true);
1033 }
1034 }
1035 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001036
Alan Stern6ee0b272008-04-28 11:06:42 -04001037 /* Check each port and set hub->change_bits to let khubd know
1038 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001039 */
1040 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001041 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern5e6effa2008-03-03 15:15:51 -05001042 u16 portstatus, portchange;
1043
Alan Stern6ee0b272008-04-28 11:06:42 -04001044 portstatus = portchange = 0;
1045 status = hub_port_status(hub, port1, &portstatus, &portchange);
1046 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1047 dev_dbg(hub->intfdev,
1048 "port %d: status %04x change %04x\n",
1049 port1, portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001050
Alan Stern6ee0b272008-04-28 11:06:42 -04001051 /* After anything other than HUB_RESUME (i.e., initialization
1052 * or any sort of reset), every port should be disabled.
1053 * Unconnected ports should likewise be disabled (paranoia),
1054 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001055 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001056 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1057 type != HUB_RESUME ||
1058 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1059 !udev ||
1060 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001061 /*
1062 * USB3 protocol ports will automatically transition
1063 * to Enabled state when detect an USB3.0 device attach.
1064 * Do not disable USB3 protocol ports.
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001065 */
Sarah Sharp131dec32010-12-06 21:00:19 -08001066 if (!hub_is_superspeed(hdev)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001067 clear_port_feature(hdev, port1,
1068 USB_PORT_FEAT_ENABLE);
1069 portstatus &= ~USB_PORT_STAT_ENABLE;
Sarah Sharp85f0ff42010-10-14 07:22:54 -07001070 } else {
1071 /* Pretend that power was lost for USB3 devs */
1072 portstatus &= ~USB_PORT_STAT_ENABLE;
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001073 }
Alan Stern6ee0b272008-04-28 11:06:42 -04001074 }
1075
Alan Stern948fea32008-04-28 11:07:07 -04001076 /* Clear status-change flags; we'll debounce later */
1077 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1078 need_debounce_delay = true;
1079 clear_port_feature(hub->hdev, port1,
1080 USB_PORT_FEAT_C_CONNECTION);
1081 }
1082 if (portchange & USB_PORT_STAT_C_ENABLE) {
1083 need_debounce_delay = true;
1084 clear_port_feature(hub->hdev, port1,
1085 USB_PORT_FEAT_C_ENABLE);
1086 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001087 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1088 hub_is_superspeed(hub->hdev)) {
1089 need_debounce_delay = true;
1090 clear_port_feature(hub->hdev, port1,
1091 USB_PORT_FEAT_C_BH_PORT_RESET);
1092 }
Alan Stern253e0572009-10-27 15:20:13 -04001093 /* We can forget about a "removed" device when there's a
1094 * physical disconnect or the connect status changes.
1095 */
1096 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1097 (portchange & USB_PORT_STAT_C_CONNECTION))
1098 clear_bit(port1, hub->removed_bits);
1099
Alan Stern6ee0b272008-04-28 11:06:42 -04001100 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1101 /* Tell khubd to disconnect the device or
1102 * check for a new connection
1103 */
1104 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1105 set_bit(port1, hub->change_bits);
1106
1107 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001108 bool port_resumed = (portstatus &
1109 USB_PORT_STAT_LINK_STATE) ==
1110 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001111 /* The power session apparently survived the resume.
1112 * If there was an overcurrent or suspend change
1113 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -08001114 * take care of it. Look at the port link state
1115 * for USB 3.0 hubs, since they don't have a suspend
1116 * change bit, and they don't set the port link change
1117 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001118 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001119 if (portchange || (hub_is_superspeed(hub->hdev) &&
1120 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001121 set_bit(port1, hub->change_bits);
1122
1123 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001124#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001125 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001126#endif
Alan Stern8808f002008-04-28 11:06:55 -04001127 set_bit(port1, hub->change_bits);
1128
Alan Stern6ee0b272008-04-28 11:06:42 -04001129 } else {
1130 /* The power session is gone; tell khubd */
1131 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1132 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001133 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001134 }
1135
Alan Stern948fea32008-04-28 11:07:07 -04001136 /* If no port-status-change flags were set, we don't need any
1137 * debouncing. If flags were set we can try to debounce the
1138 * ports all at once right now, instead of letting khubd do them
1139 * one at a time later on.
1140 *
1141 * If any port-status changes do occur during this delay, khubd
1142 * will see them later and handle them normally.
1143 */
Alan Stern8520f382008-09-22 14:44:26 -04001144 if (need_debounce_delay) {
1145 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001146
Alan Stern8520f382008-09-22 14:44:26 -04001147 /* Don't do a long sleep inside a workqueue routine */
1148 if (type == HUB_INIT2) {
1149 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1150 schedule_delayed_work(&hub->init_work,
1151 msecs_to_jiffies(delay));
1152 return; /* Continues at init3: below */
1153 } else {
1154 msleep(delay);
1155 }
1156 }
1157 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001158 hub->quiescing = 0;
1159
1160 status = usb_submit_urb(hub->urb, GFP_NOIO);
1161 if (status < 0)
1162 dev_err(hub->intfdev, "activate --> %d\n", status);
1163 if (hub->has_indicators && blinkenlights)
1164 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1165
1166 /* Scan all ports that need attention */
1167 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001168
1169 /* Allow autosuspend if it was suppressed */
1170 if (type <= HUB_INIT3)
1171 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -05001172}
1173
Alan Stern8520f382008-09-22 14:44:26 -04001174/* Implement the continuations for the delays above */
1175static void hub_init_func2(struct work_struct *ws)
1176{
1177 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1178
1179 hub_activate(hub, HUB_INIT2);
1180}
1181
1182static void hub_init_func3(struct work_struct *ws)
1183{
1184 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1185
1186 hub_activate(hub, HUB_INIT3);
1187}
1188
Alan Stern43303542008-04-28 11:07:31 -04001189enum hub_quiescing_type {
1190 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1191};
1192
1193static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1194{
1195 struct usb_device *hdev = hub->hdev;
1196 int i;
1197
Alan Stern8520f382008-09-22 14:44:26 -04001198 cancel_delayed_work_sync(&hub->init_work);
1199
Alan Stern43303542008-04-28 11:07:31 -04001200 /* khubd and related activity won't re-trigger */
1201 hub->quiescing = 1;
1202
1203 if (type != HUB_SUSPEND) {
1204 /* Disconnect all the children */
1205 for (i = 0; i < hdev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001206 if (hub->ports[i]->child)
1207 usb_disconnect(&hub->ports[i]->child);
Alan Stern43303542008-04-28 11:07:31 -04001208 }
1209 }
1210
1211 /* Stop khubd and related activity */
1212 usb_kill_urb(hub->urb);
1213 if (hub->has_indicators)
1214 cancel_delayed_work_sync(&hub->leds);
1215 if (hub->tt.hub)
Octavian Purdila036546b2012-10-23 11:33:12 +03001216 flush_work(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001217}
1218
Alan Stern3eb14912008-03-03 15:15:43 -05001219/* caller has locked the hub device */
1220static int hub_pre_reset(struct usb_interface *intf)
1221{
1222 struct usb_hub *hub = usb_get_intfdata(intf);
1223
Alan Stern43303542008-04-28 11:07:31 -04001224 hub_quiesce(hub, HUB_PRE_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001225 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001226}
1227
1228/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001229static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001230{
Alan Stern7de18d82006-06-01 13:37:24 -04001231 struct usb_hub *hub = usb_get_intfdata(intf);
1232
Alan Sternf2835212008-04-28 11:07:17 -04001233 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001234 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001235}
1236
Lan Tianyufa2a9562012-09-05 13:44:31 +08001237static void usb_port_device_release(struct device *dev)
1238{
1239 struct usb_port *port_dev = to_usb_port(dev);
1240
1241 kfree(port_dev);
1242}
1243
1244static void usb_hub_remove_port_device(struct usb_hub *hub,
1245 int port1)
1246{
1247 device_unregister(&hub->ports[port1 - 1]->dev);
1248}
1249
1250struct device_type usb_port_device_type = {
1251 .name = "usb_port",
1252 .release = usb_port_device_release,
1253};
1254
1255static int usb_hub_create_port_device(struct usb_hub *hub,
1256 int port1)
1257{
1258 struct usb_port *port_dev = NULL;
1259 int retval;
1260
1261 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
1262 if (!port_dev) {
1263 retval = -ENOMEM;
1264 goto exit;
1265 }
1266
1267 hub->ports[port1 - 1] = port_dev;
1268 port_dev->dev.parent = hub->intfdev;
1269 port_dev->dev.type = &usb_port_device_type;
1270 dev_set_name(&port_dev->dev, "port%d", port1);
1271
1272 retval = device_register(&port_dev->dev);
1273 if (retval)
1274 goto error_register;
1275 return 0;
1276
1277error_register:
1278 put_device(&port_dev->dev);
1279exit:
1280 return retval;
1281}
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283static int hub_configure(struct usb_hub *hub,
1284 struct usb_endpoint_descriptor *endpoint)
1285{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001286 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 struct usb_device *hdev = hub->hdev;
1288 struct device *hub_dev = hub->intfdev;
1289 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001290 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 unsigned int pipe;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001292 int maxp, ret, i;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001293 char *message = "out of memory";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Alan Sternd697cdd2009-10-27 15:18:46 -04001295 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 ret = -ENOMEM;
1298 goto fail;
1299 }
1300
1301 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1302 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 ret = -ENOMEM;
1304 goto fail;
1305 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001306 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1309 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 ret = -ENOMEM;
1311 goto fail;
1312 }
1313
1314 /* Request the entire hub descriptor.
1315 * hub->descriptor can handle USB_MAXCHILDREN ports,
1316 * but the hub can/will return fewer bytes here.
1317 */
John Youndbe79bb2001-09-17 00:00:00 -07001318 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (ret < 0) {
1320 message = "can't read hub descriptor";
1321 goto fail;
1322 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1323 message = "hub has too many ports!";
1324 ret = -ENODEV;
1325 goto fail;
1326 }
1327
1328 hdev->maxchild = hub->descriptor->bNbrPorts;
1329 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1330 (hdev->maxchild == 1) ? "" : "s");
1331
Lan Tianyufa2a9562012-09-05 13:44:31 +08001332 hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1333 GFP_KERNEL);
Lan Tianyuff823c72012-09-05 13:44:32 +08001334 if (!hub->ports) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001335 ret = -ENOMEM;
1336 goto fail;
1337 }
1338
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001339 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
John Youndbe79bb2001-09-17 00:00:00 -07001341 /* FIXME for USB 3.0, skip for now */
1342 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1343 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 int i;
1345 char portstr [USB_MAXCHILDREN + 1];
1346
1347 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001348 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1350 ? 'F' : 'R';
1351 portstr[hdev->maxchild] = 0;
1352 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1353 } else
1354 dev_dbg(hub_dev, "standalone hub\n");
1355
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001356 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301357 case HUB_CHAR_COMMON_LPSM:
1358 dev_dbg(hub_dev, "ganged power switching\n");
1359 break;
1360 case HUB_CHAR_INDV_PORT_LPSM:
1361 dev_dbg(hub_dev, "individual port power switching\n");
1362 break;
1363 case HUB_CHAR_NO_LPSM:
1364 case HUB_CHAR_LPSM:
1365 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1366 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001369 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301370 case HUB_CHAR_COMMON_OCPM:
1371 dev_dbg(hub_dev, "global over-current protection\n");
1372 break;
1373 case HUB_CHAR_INDV_PORT_OCPM:
1374 dev_dbg(hub_dev, "individual port over-current protection\n");
1375 break;
1376 case HUB_CHAR_NO_OCPM:
1377 case HUB_CHAR_OCPM:
1378 dev_dbg(hub_dev, "no over-current protection\n");
1379 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381
1382 spin_lock_init (&hub->tt.lock);
1383 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001384 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301386 case USB_HUB_PR_FS:
1387 break;
1388 case USB_HUB_PR_HS_SINGLE_TT:
1389 dev_dbg(hub_dev, "Single TT\n");
1390 hub->tt.hub = hdev;
1391 break;
1392 case USB_HUB_PR_HS_MULTI_TT:
1393 ret = usb_set_interface(hdev, 0, 1);
1394 if (ret == 0) {
1395 dev_dbg(hub_dev, "TT per port\n");
1396 hub->tt.multi = 1;
1397 } else
1398 dev_err(hub_dev, "Using single TT (err %d)\n",
1399 ret);
1400 hub->tt.hub = hdev;
1401 break;
1402 case USB_HUB_PR_SS:
1403 /* USB 3.0 hubs don't have a TT */
1404 break;
1405 default:
1406 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1407 hdev->descriptor.bDeviceProtocol);
1408 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001411 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001412 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001413 case HUB_TTTT_8_BITS:
1414 if (hdev->descriptor.bDeviceProtocol != 0) {
1415 hub->tt.think_time = 666;
1416 dev_dbg(hub_dev, "TT requires at most %d "
1417 "FS bit times (%d ns)\n",
1418 8, hub->tt.think_time);
1419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001421 case HUB_TTTT_16_BITS:
1422 hub->tt.think_time = 666 * 2;
1423 dev_dbg(hub_dev, "TT requires at most %d "
1424 "FS bit times (%d ns)\n",
1425 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001427 case HUB_TTTT_24_BITS:
1428 hub->tt.think_time = 666 * 3;
1429 dev_dbg(hub_dev, "TT requires at most %d "
1430 "FS bit times (%d ns)\n",
1431 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001433 case HUB_TTTT_32_BITS:
1434 hub->tt.think_time = 666 * 4;
1435 dev_dbg(hub_dev, "TT requires at most %d "
1436 "FS bit times (%d ns)\n",
1437 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 break;
1439 }
1440
1441 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001442 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 hub->has_indicators = 1;
1444 dev_dbg(hub_dev, "Port indicators are supported\n");
1445 }
1446
1447 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1448 hub->descriptor->bPwrOn2PwrGood * 2);
1449
1450 /* power budgeting mostly matters with bus-powered hubs,
1451 * and battery-powered root hubs (may provide just 8 mA).
1452 */
1453 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001454 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 message = "can't get hub status";
1456 goto fail;
1457 }
Alan Stern7d35b922005-04-25 11:18:32 -04001458 le16_to_cpus(&hubstatus);
1459 if (hdev == hdev->bus->root_hub) {
Alan Stern55c52712005-11-23 12:03:12 -05001460 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1461 hub->mA_per_port = 500;
1462 else {
1463 hub->mA_per_port = hdev->bus_mA;
1464 hub->limited_power = 1;
1465 }
Alan Stern7d35b922005-04-25 11:18:32 -04001466 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1468 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001469 hub->limited_power = 1;
1470 if (hdev->maxchild > 0) {
1471 int remaining = hdev->bus_mA -
1472 hub->descriptor->bHubContrCurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Alan Stern55c52712005-11-23 12:03:12 -05001474 if (remaining < hdev->maxchild * 100)
1475 dev_warn(hub_dev,
1476 "insufficient power available "
1477 "to use all downstream ports\n");
1478 hub->mA_per_port = 100; /* 7.2.1.1 */
1479 }
1480 } else { /* Self-powered external hub */
1481 /* FIXME: What about battery-powered external hubs that
1482 * provide less current per port? */
1483 hub->mA_per_port = 500;
1484 }
1485 if (hub->mA_per_port < 500)
1486 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1487 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001489 /* Update the HCD's internal representation of this hub before khubd
1490 * starts getting port status changes for devices under the hub.
1491 */
1492 hcd = bus_to_hcd(hdev->bus);
1493 if (hcd->driver->update_hub_device) {
1494 ret = hcd->driver->update_hub_device(hcd, hdev,
1495 &hub->tt, GFP_KERNEL);
1496 if (ret < 0) {
1497 message = "can't update HCD hub info";
1498 goto fail;
1499 }
1500 }
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1503 if (ret < 0) {
1504 message = "can't get hub status";
1505 goto fail;
1506 }
1507
1508 /* local power status reports aren't always correct */
1509 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1510 dev_dbg(hub_dev, "local power source is %s\n",
1511 (hubstatus & HUB_STATUS_LOCAL_POWER)
1512 ? "lost (inactive)" : "good");
1513
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001514 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 dev_dbg(hub_dev, "%sover-current condition exists\n",
1516 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1517
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001518 /* set up the interrupt endpoint
1519 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1520 * bytes as USB2.0[11.12.3] says because some hubs are known
1521 * to send more data (and thus cause overflow). For root hubs,
1522 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1523 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1525 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1526
1527 if (maxp > sizeof(*hub->buffer))
1528 maxp = sizeof(*hub->buffer);
1529
1530 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1531 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 ret = -ENOMEM;
1533 goto fail;
1534 }
1535
1536 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1537 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 /* maybe cycle the hub leds */
1540 if (hub->has_indicators && blinkenlights)
1541 hub->indicator [0] = INDICATOR_CYCLE;
1542
Lan Tianyufa2a9562012-09-05 13:44:31 +08001543 for (i = 0; i < hdev->maxchild; i++)
1544 if (usb_hub_create_port_device(hub, i + 1) < 0)
1545 dev_err(hub->intfdev,
1546 "couldn't create port%d device.\n", i + 1);
1547
Alan Sternf2835212008-04-28 11:07:17 -04001548 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return 0;
1550
1551fail:
1552 dev_err (hub_dev, "config failed, %s (err %d)\n",
1553 message, ret);
1554 /* hub_disconnect() frees urb and descriptor */
1555 return ret;
1556}
1557
Alan Sterne8054852007-05-04 11:55:11 -04001558static void hub_release(struct kref *kref)
1559{
1560 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1561
1562 usb_put_intf(to_usb_interface(hub->intfdev));
1563 kfree(hub);
1564}
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566static unsigned highspeed_hubs;
1567
1568static void hub_disconnect(struct usb_interface *intf)
1569{
Huajun Li88162302012-03-12 21:00:19 +08001570 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001571 struct usb_device *hdev = interface_to_usbdev(intf);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001572 int i;
1573
Alan Sterne8054852007-05-04 11:55:11 -04001574 /* Take the hub off the event list and don't let it be added again */
1575 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001576 if (!list_empty(&hub->event_list)) {
1577 list_del_init(&hub->event_list);
1578 usb_autopm_put_interface_no_suspend(intf);
1579 }
Alan Sterne8054852007-05-04 11:55:11 -04001580 hub->disconnected = 1;
1581 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Alan Stern7de18d82006-06-01 13:37:24 -04001583 /* Disconnect all children and quiesce the hub */
1584 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001585 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001586
Alan Stern8b28c752005-08-10 17:04:13 -04001587 usb_set_intfdata (intf, NULL);
Alexander Shishkin1f2235b2012-09-12 14:48:31 +03001588
1589 for (i = 0; i < hdev->maxchild; i++)
1590 usb_hub_remove_port_device(hub, i + 1);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001591 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Alan Sterne8054852007-05-04 11:55:11 -04001593 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 highspeed_hubs--;
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 usb_free_urb(hub->urb);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001597 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001598 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001599 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001600 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Alan Sterne8054852007-05-04 11:55:11 -04001602 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
1605static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1606{
1607 struct usb_host_interface *desc;
1608 struct usb_endpoint_descriptor *endpoint;
1609 struct usb_device *hdev;
1610 struct usb_hub *hub;
1611
1612 desc = intf->cur_altsetting;
1613 hdev = interface_to_usbdev(intf);
1614
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001615 /* Hubs have proper suspend/resume support. */
1616 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001617
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001618 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001619 dev_err(&intf->dev,
1620 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001621 return -E2BIG;
1622 }
1623
David Brownell89ccbdc2006-04-02 10:18:09 -08001624#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1625 if (hdev->parent) {
1626 dev_warn(&intf->dev, "ignoring external hub\n");
1627 return -ENODEV;
1628 }
1629#endif
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /* Some hubs have a subclass of 1, which AFAICT according to the */
1632 /* specs is not defined, but it works */
1633 if ((desc->desc.bInterfaceSubClass != 0) &&
1634 (desc->desc.bInterfaceSubClass != 1)) {
1635descriptor_error:
1636 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1637 return -EIO;
1638 }
1639
1640 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1641 if (desc->desc.bNumEndpoints != 1)
1642 goto descriptor_error;
1643
1644 endpoint = &desc->endpoint[0].desc;
1645
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001646 /* If it's not an interrupt in endpoint, we'd better punt! */
1647 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 goto descriptor_error;
1649
1650 /* We found a hub */
1651 dev_info (&intf->dev, "USB hub found\n");
1652
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001653 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (!hub) {
1655 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1656 return -ENOMEM;
1657 }
1658
Alan Sterne8054852007-05-04 11:55:11 -04001659 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 INIT_LIST_HEAD(&hub->event_list);
1661 hub->intfdev = &intf->dev;
1662 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001663 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001664 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001665 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001668 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 if (hdev->speed == USB_SPEED_HIGH)
1671 highspeed_hubs++;
1672
1673 if (hub_configure(hub, endpoint) >= 0)
1674 return 0;
1675
1676 hub_disconnect (intf);
1677 return -ENODEV;
1678}
1679
1680static int
1681hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1682{
1683 struct usb_device *hdev = interface_to_usbdev (intf);
Lan Tianyuff823c72012-09-05 13:44:32 +08001684 struct usb_hub *hub = hdev_to_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 /* assert ifno == 0 (part of hub spec) */
1687 switch (code) {
1688 case USBDEVFS_HUB_PORTINFO: {
1689 struct usbdevfs_hub_portinfo *info = user_data;
1690 int i;
1691
1692 spin_lock_irq(&device_state_lock);
1693 if (hdev->devnum <= 0)
1694 info->nports = 0;
1695 else {
1696 info->nports = hdev->maxchild;
1697 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001698 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 info->port[i] = 0;
1700 else
1701 info->port[i] =
Lan Tianyuff823c72012-09-05 13:44:32 +08001702 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704 }
1705 spin_unlock_irq(&device_state_lock);
1706
1707 return info->nports + 1;
1708 }
1709
1710 default:
1711 return -ENOSYS;
1712 }
1713}
1714
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001715/*
1716 * Allow user programs to claim ports on a hub. When a device is attached
1717 * to one of these "claimed" ports, the program will "own" the device.
1718 */
1719static int find_port_owner(struct usb_device *hdev, unsigned port1,
Lan Tianyu336c5c32012-07-06 14:13:52 +08001720 struct dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001721{
1722 if (hdev->state == USB_STATE_NOTATTACHED)
1723 return -ENODEV;
1724 if (port1 == 0 || port1 > hdev->maxchild)
1725 return -EINVAL;
1726
1727 /* This assumes that devices not managed by the hub driver
1728 * will always have maxchild equal to 0.
1729 */
Lan Tianyufa2a9562012-09-05 13:44:31 +08001730 *ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001731 return 0;
1732}
1733
1734/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001735int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1736 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001737{
1738 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001739 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001740
1741 rc = find_port_owner(hdev, port1, &powner);
1742 if (rc)
1743 return rc;
1744 if (*powner)
1745 return -EBUSY;
1746 *powner = owner;
1747 return rc;
1748}
1749
Lan Tianyu336c5c32012-07-06 14:13:52 +08001750int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1751 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001752{
1753 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001754 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001755
1756 rc = find_port_owner(hdev, port1, &powner);
1757 if (rc)
1758 return rc;
1759 if (*powner != owner)
1760 return -ENOENT;
1761 *powner = NULL;
1762 return rc;
1763}
1764
Lan Tianyu336c5c32012-07-06 14:13:52 +08001765void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001766{
Lan Tianyufa2a9562012-09-05 13:44:31 +08001767 struct usb_hub *hub = hdev_to_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001768 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001769
Lan Tianyufa2a9562012-09-05 13:44:31 +08001770 for (n = 0; n < hdev->maxchild; n++) {
1771 if (hub->ports[n]->port_owner == owner)
1772 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001773 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001774
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001775}
1776
1777/* The caller must hold udev's lock */
1778bool usb_device_is_owned(struct usb_device *udev)
1779{
1780 struct usb_hub *hub;
1781
1782 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1783 return false;
1784 hub = hdev_to_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001785 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001786}
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1789{
Lan Tianyuff823c72012-09-05 13:44:32 +08001790 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 int i;
1792
1793 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001794 if (hub->ports[i]->child)
1795 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001797 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001798 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 udev->state = USB_STATE_NOTATTACHED;
1800}
1801
1802/**
1803 * usb_set_device_state - change a device's current state (usbcore, hcds)
1804 * @udev: pointer to device whose state should be changed
1805 * @new_state: new state value to be stored
1806 *
1807 * udev->state is _not_ fully protected by the device lock. Although
1808 * most transitions are made only while holding the lock, the state can
1809 * can change to USB_STATE_NOTATTACHED at almost any time. This
1810 * is so that devices can be marked as disconnected as soon as possible,
1811 * without having to wait for any semaphores to be released. As a result,
1812 * all changes to any device's state must be protected by the
1813 * device_state_lock spinlock.
1814 *
1815 * Once a device has been added to the device tree, all changes to its state
1816 * should be made using this routine. The state should _not_ be set directly.
1817 *
1818 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1819 * Otherwise udev->state is set to new_state, and if new_state is
1820 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1821 * to USB_STATE_NOTATTACHED.
1822 */
1823void usb_set_device_state(struct usb_device *udev,
1824 enum usb_device_state new_state)
1825{
1826 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001827 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 spin_lock_irqsave(&device_state_lock, flags);
1830 if (udev->state == USB_STATE_NOTATTACHED)
1831 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001832 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001833
1834 /* root hub wakeup capabilities are managed out-of-band
1835 * and may involve silicon errata ... ignore them here.
1836 */
1837 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001838 if (udev->state == USB_STATE_SUSPENDED
1839 || new_state == USB_STATE_SUSPENDED)
1840 ; /* No change to wakeup settings */
1841 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001842 wakeup = udev->actconfig->desc.bmAttributes
1843 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001844 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001845 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001846 }
Sarah Sharp15123002007-12-21 16:54:15 -08001847 if (udev->state == USB_STATE_SUSPENDED &&
1848 new_state != USB_STATE_SUSPENDED)
1849 udev->active_duration -= jiffies;
1850 else if (new_state == USB_STATE_SUSPENDED &&
1851 udev->state != USB_STATE_SUSPENDED)
1852 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001853 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001854 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 recursively_mark_NOTATTACHED(udev);
1856 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001857 if (wakeup >= 0)
1858 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
David Vrabel6da9c992009-02-18 14:43:47 +00001860EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001862/*
Alan Stern3b29b682011-02-22 09:53:41 -05001863 * Choose a device number.
1864 *
1865 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1866 * USB-2.0 buses they are also used as device addresses, however on
1867 * USB-3.0 buses the address is assigned by the controller hardware
1868 * and it usually is not the same as the device number.
1869 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001870 * WUSB devices are simple: they have no hubs behind, so the mapping
1871 * device <-> virtual port number becomes 1:1. Why? to simplify the
1872 * life of the device connection logic in
1873 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1874 * handshake we need to assign a temporary address in the unauthorized
1875 * space. For simplicity we use the first virtual port number found to
1876 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1877 * and that becomes it's address [X < 128] or its unauthorized address
1878 * [X | 0x80].
1879 *
1880 * We add 1 as an offset to the one-based USB-stack port number
1881 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1882 * 0 is reserved by USB for default address; (b) Linux's USB stack
1883 * uses always #1 for the root hub of the controller. So USB stack's
1884 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001885 *
1886 * Devices connected under xHCI are not as simple. The host controller
1887 * supports virtualization, so the hardware assigns device addresses and
1888 * the HCD must setup data structures before issuing a set address
1889 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001890 */
Alan Stern3b29b682011-02-22 09:53:41 -05001891static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 int devnum;
1894 struct usb_bus *bus = udev->bus;
1895
1896 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001897 if (udev->wusb) {
1898 devnum = udev->portnum + 1;
1899 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1900 } else {
1901 /* Try to allocate the next devnum beginning at
1902 * bus->devnum_next. */
1903 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1904 bus->devnum_next);
1905 if (devnum >= 128)
1906 devnum = find_next_zero_bit(bus->devmap.devicemap,
1907 128, 1);
1908 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (devnum < 128) {
1911 set_bit(devnum, bus->devmap.devicemap);
1912 udev->devnum = devnum;
1913 }
1914}
1915
Alan Stern3b29b682011-02-22 09:53:41 -05001916static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
1918 if (udev->devnum > 0) {
1919 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1920 udev->devnum = -1;
1921 }
1922}
1923
Alan Stern3b29b682011-02-22 09:53:41 -05001924static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001925{
1926 /* The address for a WUSB device is managed by wusbcore. */
1927 if (!udev->wusb)
1928 udev->devnum = devnum;
1929}
1930
Herbert Xuf7410ce2010-01-10 20:15:03 +11001931static void hub_free_dev(struct usb_device *udev)
1932{
1933 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1934
1935 /* Root hubs aren't real devices, so don't free HCD resources */
1936 if (hcd->driver->free_dev && udev->parent)
1937 hcd->driver->free_dev(hcd, udev);
1938}
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940/**
1941 * usb_disconnect - disconnect a device (usbcore-internal)
1942 * @pdev: pointer to device being disconnected
1943 * Context: !in_interrupt ()
1944 *
1945 * Something got disconnected. Get rid of it and all of its children.
1946 *
1947 * If *pdev is a normal device then the parent hub must already be locked.
1948 * If *pdev is a root hub then this routine will acquire the
1949 * usb_bus_list_lock on behalf of the caller.
1950 *
1951 * Only hub drivers (including virtual root hub drivers for host
1952 * controllers) should ever call this.
1953 *
1954 * This call is synchronous, and may not be used in an interrupt context.
1955 */
1956void usb_disconnect(struct usb_device **pdev)
1957{
1958 struct usb_device *udev = *pdev;
Lan Tianyuff823c72012-09-05 13:44:32 +08001959 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 int i;
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 /* mark the device as inactive, so any further urb submissions for
1963 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001964 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 */
1966 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05001967 dev_info(&udev->dev, "USB disconnect, device number %d\n",
1968 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Alan Stern9ad3d6c2005-11-17 17:10:32 -05001970 usb_lock_device(udev);
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08001973 for (i = 0; i < udev->maxchild; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001974 if (hub->ports[i]->child)
1975 usb_disconnect(&hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
1977
1978 /* deallocate hcd/hardware state ... nuking all pending urbs and
1979 * cleaning up all state associated with the current configuration
1980 * so that the hardware is now fully quiesced.
1981 */
Alan Stern782da722006-07-01 22:09:35 -04001982 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04001984 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Alan Stern3b23dd62008-12-05 14:10:34 -05001986 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04001987 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07001988
Alan Stern782da722006-07-01 22:09:35 -04001989 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05001990 * for de-configuring the device and invoking the remove-device
1991 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04001992 */
1993 device_del(&udev->dev);
1994
1995 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 * (or root_hub) pointer.
1997 */
Alan Stern3b29b682011-02-22 09:53:41 -05001998 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 /* Avoid races with recursively_mark_NOTATTACHED() */
2001 spin_lock_irq(&device_state_lock);
2002 *pdev = NULL;
2003 spin_unlock_irq(&device_state_lock);
2004
Herbert Xuf7410ce2010-01-10 20:15:03 +11002005 hub_free_dev(udev);
2006
Alan Stern782da722006-07-01 22:09:35 -04002007 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002010#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011static void show_string(struct usb_device *udev, char *id, char *string)
2012{
2013 if (!string)
2014 return;
2015 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
2016}
2017
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002018static void announce_device(struct usb_device *udev)
2019{
2020 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2021 le16_to_cpu(udev->descriptor.idVendor),
2022 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002023 dev_info(&udev->dev,
2024 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002025 udev->descriptor.iManufacturer,
2026 udev->descriptor.iProduct,
2027 udev->descriptor.iSerialNumber);
2028 show_string(udev, "Product", udev->product);
2029 show_string(udev, "Manufacturer", udev->manufacturer);
2030 show_string(udev, "SerialNumber", udev->serial);
2031}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002033static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034#endif
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036#ifdef CONFIG_USB_OTG
2037#include "otg_whitelist.h"
2038#endif
2039
Oliver Neukum3ede7602007-01-11 14:35:50 +01002040/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002041 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002042 * @udev: newly addressed device (in ADDRESS state)
2043 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002044 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01002045 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002046static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002048 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050#ifdef CONFIG_USB_OTG
2051 /*
2052 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2053 * to wake us after we've powered off VBUS; and HNP, switching roles
2054 * "host" to "peripheral". The OTG descriptor helps figure this out.
2055 */
2056 if (!udev->bus->is_b_host
2057 && udev->config
2058 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002059 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 struct usb_bus *bus = udev->bus;
2061
2062 /* descriptor may appear anywhere in config */
2063 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2064 le16_to_cpu(udev->config[0].desc.wTotalLength),
2065 USB_DT_OTG, (void **) &desc) == 0) {
2066 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002067 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 dev_info(&udev->dev,
2070 "Dual-Role OTG device on %sHNP port\n",
2071 (port1 == bus->otg_port)
2072 ? "" : "non-");
2073
2074 /* enable HNP before suspend, it's simpler */
2075 if (port1 == bus->otg_port)
2076 bus->b_hnp_enable = 1;
2077 err = usb_control_msg(udev,
2078 usb_sndctrlpipe(udev, 0),
2079 USB_REQ_SET_FEATURE, 0,
2080 bus->b_hnp_enable
2081 ? USB_DEVICE_B_HNP_ENABLE
2082 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2083 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2084 if (err < 0) {
2085 /* OTG MESSAGE: report errors here,
2086 * customize to match your product.
2087 */
2088 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002089 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 err);
2091 bus->b_hnp_enable = 0;
2092 }
2093 }
2094 }
2095 }
2096
2097 if (!is_targeted(udev)) {
2098
2099 /* Maybe it can talk to us, though we can't talk to it.
2100 * (Includes HNP test device.)
2101 */
2102 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002103 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (err < 0)
2105 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2106 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002107 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 goto fail;
2109 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002110fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002112 return err;
2113}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002115
2116/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002117 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002118 * @udev: newly addressed device (in ADDRESS state)
2119 *
2120 * This is only called by usb_new_device() and usb_authorize_device()
2121 * and FIXME -- all comments that apply to them apply here wrt to
2122 * environment.
2123 *
2124 * If the device is WUSB and not authorized, we don't attempt to read
2125 * the string descriptors, as they will be errored out by the device
2126 * until it has been authorized.
2127 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002128static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002129{
2130 int err;
2131
2132 if (udev->config == NULL) {
2133 err = usb_get_configuration(udev);
2134 if (err < 0) {
2135 dev_err(&udev->dev, "can't read configurations, error %d\n",
2136 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002137 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002138 }
2139 }
2140 if (udev->wusb == 1 && udev->authorized == 0) {
2141 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2142 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2143 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2144 }
2145 else {
2146 /* read the standard strings and cache them if present */
2147 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2148 udev->manufacturer = usb_cache_string(udev,
2149 udev->descriptor.iManufacturer);
2150 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2151 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002152 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002153 if (err < 0)
2154 return err;
2155
2156 usb_detect_interface_quirks(udev);
2157
2158 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002159}
2160
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002161static void set_usb_port_removable(struct usb_device *udev)
2162{
2163 struct usb_device *hdev = udev->parent;
2164 struct usb_hub *hub;
2165 u8 port = udev->portnum;
2166 u16 wHubCharacteristics;
2167 bool removable = true;
2168
2169 if (!hdev)
2170 return;
2171
2172 hub = hdev_to_hub(udev->parent);
2173
2174 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2175
2176 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2177 return;
2178
2179 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002180 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2181 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002182 removable = false;
2183 } else {
2184 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2185 removable = false;
2186 }
2187
2188 if (removable)
2189 udev->removable = USB_DEVICE_REMOVABLE;
2190 else
2191 udev->removable = USB_DEVICE_FIXED;
2192}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002193
2194/**
2195 * usb_new_device - perform initial device setup (usbcore-internal)
2196 * @udev: newly addressed device (in ADDRESS state)
2197 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002198 * This is called with devices which have been detected but not fully
2199 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002200 * for any device configuration. The caller must have locked either
2201 * the parent hub (if udev is a normal device) or else the
2202 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2203 * udev has already been installed, but udev is not yet visible through
2204 * sysfs or other filesystem code.
2205 *
2206 * It will return if the device is configured properly or not. Zero if
2207 * the interface was registered with the driver core; else a negative
2208 * errno value.
2209 *
2210 * This call is synchronous, and may not be used in an interrupt context.
2211 *
2212 * Only the hub driver or root-hub registrar should ever call this.
2213 */
2214int usb_new_device(struct usb_device *udev)
2215{
2216 int err;
2217
Dan Streetman16985402010-01-06 09:56:53 -05002218 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002219 /* Initialize non-root-hub device wakeup to disabled;
2220 * device (un)configuration controls wakeup capable
2221 * sysfs power/wakeup controls wakeup enabled/disabled
2222 */
2223 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002224 }
2225
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002226 /* Tell the runtime-PM framework the device is active */
2227 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002228 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002229 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002230 pm_runtime_enable(&udev->dev);
2231
Alan Sternc08512c2010-11-15 15:57:58 -05002232 /* By default, forbid autosuspend for all devices. It will be
2233 * allowed for hubs during binding.
2234 */
2235 usb_disable_autosuspend(udev);
2236
Alan Stern8d8558d2009-12-08 15:50:41 -05002237 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002238 if (err < 0)
2239 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002240 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2241 udev->devnum, udev->bus->busnum,
2242 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002243 /* export the usbdev device-node for libusb */
2244 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2245 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2246
Alan Stern6cd13202008-11-13 15:08:30 -05002247 /* Tell the world! */
2248 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002249
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002250 if (udev->serial)
2251 add_device_randomness(udev->serial, strlen(udev->serial));
2252 if (udev->product)
2253 add_device_randomness(udev->product, strlen(udev->product));
2254 if (udev->manufacturer)
2255 add_device_randomness(udev->manufacturer,
2256 strlen(udev->manufacturer));
2257
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002258 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002259
2260 /*
2261 * check whether the hub marks this port as non-removable. Do it
2262 * now so that platform-specific data can override it in
2263 * device_add()
2264 */
2265 if (udev->parent)
2266 set_usb_port_removable(udev);
2267
Alan Stern782da722006-07-01 22:09:35 -04002268 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002269 * for configuring the device and invoking the add-device
2270 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002271 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002272 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (err) {
2274 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2275 goto fail;
2276 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002277
Alan Stern3b23dd62008-12-05 14:10:34 -05002278 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002279 usb_mark_last_busy(udev);
2280 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002281 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283fail:
2284 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002285 pm_runtime_disable(&udev->dev);
2286 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002287 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002290
2291/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002292 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2293 * @usb_dev: USB device
2294 *
2295 * Move the USB device to a very basic state where interfaces are disabled
2296 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002297 *
2298 * We share a lock (that we have) with device_del(), so we need to
2299 * defer its call.
2300 */
2301int usb_deauthorize_device(struct usb_device *usb_dev)
2302{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002303 usb_lock_device(usb_dev);
2304 if (usb_dev->authorized == 0)
2305 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002306
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002307 usb_dev->authorized = 0;
2308 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002309
2310 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002311 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002312 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002313 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002314 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002315 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002316
2317 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002318 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002319
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002320out_unauthorized:
2321 usb_unlock_device(usb_dev);
2322 return 0;
2323}
2324
2325
2326int usb_authorize_device(struct usb_device *usb_dev)
2327{
2328 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002329
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002330 usb_lock_device(usb_dev);
2331 if (usb_dev->authorized == 1)
2332 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002333
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002334 result = usb_autoresume_device(usb_dev);
2335 if (result < 0) {
2336 dev_err(&usb_dev->dev,
2337 "can't autoresume for authorization: %d\n", result);
2338 goto error_autoresume;
2339 }
2340 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2341 if (result < 0) {
2342 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2343 "authorization: %d\n", result);
2344 goto error_device_descriptor;
2345 }
Alan Sternda307122009-12-08 15:54:44 -05002346
2347 kfree(usb_dev->product);
2348 usb_dev->product = NULL;
2349 kfree(usb_dev->manufacturer);
2350 usb_dev->manufacturer = NULL;
2351 kfree(usb_dev->serial);
2352 usb_dev->serial = NULL;
2353
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002354 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002355 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002356 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002357 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002358 /* Choose and set the configuration. This registers the interfaces
2359 * with the driver core and lets interface drivers bind to them.
2360 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002361 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002362 if (c >= 0) {
2363 result = usb_set_configuration(usb_dev, c);
2364 if (result) {
2365 dev_err(&usb_dev->dev,
2366 "can't set config #%d, error %d\n", c, result);
2367 /* This need not be fatal. The user can try to
2368 * set other configurations. */
2369 }
2370 }
2371 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002372
Alan Stern8d8558d2009-12-08 15:50:41 -05002373error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002374error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002375 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002376error_autoresume:
2377out_authorized:
2378 usb_unlock_device(usb_dev); // complements locktree
2379 return result;
2380}
2381
2382
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002383/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2384static unsigned hub_is_wusb(struct usb_hub *hub)
2385{
2386 struct usb_hcd *hcd;
2387 if (hub->hdev->parent != NULL) /* not a root hub? */
2388 return 0;
2389 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2390 return hcd->wireless;
2391}
2392
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394#define PORT_RESET_TRIES 5
2395#define SET_ADDRESS_TRIES 2
2396#define GET_DESCRIPTOR_TRIES 2
2397#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302398#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
2400#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2401#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002402#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403#define HUB_LONG_RESET_TIME 200
2404#define HUB_RESET_TIMEOUT 500
2405
Sarah Sharp10d674a2011-09-14 14:24:52 -07002406static int hub_port_reset(struct usb_hub *hub, int port1,
2407 struct usb_device *udev, unsigned int delay, bool warm);
2408
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002409/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2410 * Port worm reset is required to recover
2411 */
2412static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002413{
2414 return hub_is_superspeed(hub->hdev) &&
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002415 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2416 USB_SS_PORT_LS_SS_INACTIVE) ||
2417 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2418 USB_SS_PORT_LS_COMP_MOD)) ;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002419}
2420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002422 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
2424 int delay_time, ret;
2425 u16 portstatus;
2426 u16 portchange;
2427
2428 for (delay_time = 0;
2429 delay_time < HUB_RESET_TIMEOUT;
2430 delay_time += delay) {
2431 /* wait to give the device a chance to reset */
2432 msleep(delay);
2433
2434 /* read and decode port status */
2435 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2436 if (ret < 0)
2437 return ret;
2438
Andiry Xu75d7cf72011-09-13 16:41:11 -07002439 /*
2440 * Some buggy devices require a warm reset to be issued even
2441 * when the port appears not to be connected.
2442 */
2443 if (!warm) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002444 /*
2445 * Some buggy devices can cause an NEC host controller
2446 * to transition to the "Error" state after a hot port
2447 * reset. This will show up as the port state in
2448 * "Inactive", and the port may also report a
2449 * disconnect. Forcing a warm port reset seems to make
2450 * the device work.
2451 *
2452 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2453 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002454 if (hub_port_warm_reset_required(hub, portstatus)) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002455 int ret;
2456
2457 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2458 clear_port_feature(hub->hdev, port1,
2459 USB_PORT_FEAT_C_CONNECTION);
2460 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2461 clear_port_feature(hub->hdev, port1,
2462 USB_PORT_FEAT_C_PORT_LINK_STATE);
2463 if (portchange & USB_PORT_STAT_C_RESET)
2464 clear_port_feature(hub->hdev, port1,
2465 USB_PORT_FEAT_C_RESET);
2466 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2467 port1);
2468 ret = hub_port_reset(hub, port1,
2469 udev, HUB_BH_RESET_TIME,
2470 true);
2471 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2472 clear_port_feature(hub->hdev, port1,
2473 USB_PORT_FEAT_C_CONNECTION);
2474 return ret;
2475 }
Andiry Xu75d7cf72011-09-13 16:41:11 -07002476 /* Device went away? */
2477 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2478 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Andiry Xu75d7cf72011-09-13 16:41:11 -07002480 /* bomb out completely if the connection bounced */
2481 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2482 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Andiry Xu75d7cf72011-09-13 16:41:11 -07002484 /* if we`ve finished resetting, then break out of
2485 * the loop
2486 */
2487 if (!(portstatus & USB_PORT_STAT_RESET) &&
2488 (portstatus & USB_PORT_STAT_ENABLE)) {
2489 if (hub_is_wusb(hub))
2490 udev->speed = USB_SPEED_WIRELESS;
2491 else if (hub_is_superspeed(hub->hdev))
2492 udev->speed = USB_SPEED_SUPER;
2493 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2494 udev->speed = USB_SPEED_HIGH;
2495 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2496 udev->speed = USB_SPEED_LOW;
2497 else
2498 udev->speed = USB_SPEED_FULL;
2499 return 0;
2500 }
2501 } else {
2502 if (portchange & USB_PORT_STAT_C_BH_RESET)
2503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 }
2505
2506 /* switch to the long delay after two short delay failures */
2507 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2508 delay = HUB_LONG_RESET_TIME;
2509
2510 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002511 "port %d not %sreset yet, waiting %dms\n",
2512 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 }
2514
2515 return -EBUSY;
2516}
2517
Andiry Xu75d7cf72011-09-13 16:41:11 -07002518static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2519 struct usb_device *udev, int *status, bool warm)
2520{
2521 switch (*status) {
2522 case 0:
2523 if (!warm) {
2524 struct usb_hcd *hcd;
2525 /* TRSTRCY = 10 ms; plus some extra */
2526 msleep(10 + 40);
2527 update_devnum(udev, 0);
2528 hcd = bus_to_hcd(udev->bus);
2529 if (hcd->driver->reset_device) {
2530 *status = hcd->driver->reset_device(hcd, udev);
2531 if (*status < 0) {
2532 dev_err(&udev->dev, "Cannot reset "
2533 "HCD device state\n");
2534 break;
2535 }
2536 }
2537 }
2538 /* FALL THROUGH */
2539 case -ENOTCONN:
2540 case -ENODEV:
2541 clear_port_feature(hub->hdev,
2542 port1, USB_PORT_FEAT_C_RESET);
2543 /* FIXME need disconnect() for NOTATTACHED device */
2544 if (warm) {
2545 clear_port_feature(hub->hdev, port1,
2546 USB_PORT_FEAT_C_BH_PORT_RESET);
2547 clear_port_feature(hub->hdev, port1,
2548 USB_PORT_FEAT_C_PORT_LINK_STATE);
2549 } else {
2550 usb_set_device_state(udev, *status
2551 ? USB_STATE_NOTATTACHED
2552 : USB_STATE_DEFAULT);
2553 }
2554 break;
2555 }
2556}
2557
2558/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002560 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
2562 int i, status;
2563
Andiry Xu75d7cf72011-09-13 16:41:11 -07002564 if (!warm) {
2565 /* Block EHCI CF initialization during the port reset.
2566 * Some companion controllers don't like it when they mix.
2567 */
2568 down_read(&ehci_cf_port_reset_rwsem);
2569 } else {
2570 if (!hub_is_superspeed(hub->hdev)) {
2571 dev_err(hub->intfdev, "only USB3 hub support "
2572 "warm reset\n");
2573 return -EINVAL;
2574 }
2575 }
Alan Stern32fe0192007-10-10 16:27:07 -04002576
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 /* Reset the port */
2578 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002579 status = set_port_feature(hub->hdev, port1, (warm ?
2580 USB_PORT_FEAT_BH_PORT_RESET :
2581 USB_PORT_FEAT_RESET));
2582 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002584 "cannot %sreset port %d (err = %d)\n",
2585 warm ? "warm " : "", port1, status);
2586 } else {
2587 status = hub_port_wait_reset(hub, port1, udev, delay,
2588 warm);
David Brownelldd165252005-08-31 10:45:25 -07002589 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 dev_dbg(hub->intfdev,
2591 "port_wait_reset: err = %d\n",
2592 status);
2593 }
2594
2595 /* return on disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002596 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2597 hub_port_finish_reset(hub, port1, udev, &status, warm);
Alan Stern32fe0192007-10-10 16:27:07 -04002598 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 }
2600
2601 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002602 "port %d not enabled, trying %sreset again...\n",
2603 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 delay = HUB_LONG_RESET_TIME;
2605 }
2606
2607 dev_err (hub->intfdev,
2608 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2609 port1);
2610
Andiry Xu75d7cf72011-09-13 16:41:11 -07002611done:
2612 if (!warm)
2613 up_read(&ehci_cf_port_reset_rwsem);
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return status;
2616}
2617
Andiry Xu0ed9a572011-04-27 18:07:43 +08002618/* Check if a port is power on */
2619static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2620{
2621 int ret = 0;
2622
2623 if (hub_is_superspeed(hub->hdev)) {
2624 if (portstatus & USB_SS_PORT_STAT_POWER)
2625 ret = 1;
2626 } else {
2627 if (portstatus & USB_PORT_STAT_POWER)
2628 ret = 1;
2629 }
2630
2631 return ret;
2632}
2633
Alan Sternd388dab2006-07-01 22:14:24 -04002634#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Andiry Xu0ed9a572011-04-27 18:07:43 +08002636/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2637static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2638{
2639 int ret = 0;
2640
2641 if (hub_is_superspeed(hub->hdev)) {
2642 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2643 == USB_SS_PORT_LS_U3)
2644 ret = 1;
2645 } else {
2646 if (portstatus & USB_PORT_STAT_SUSPEND)
2647 ret = 1;
2648 }
2649
2650 return ret;
2651}
Alan Sternb01b03f2008-04-28 11:06:11 -04002652
2653/* Determine whether the device on a port is ready for a normal resume,
2654 * is ready for a reset-resume, or should be disconnected.
2655 */
2656static int check_port_resume_type(struct usb_device *udev,
2657 struct usb_hub *hub, int port1,
2658 int status, unsigned portchange, unsigned portstatus)
2659{
2660 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002661 if (status || port_is_suspended(hub, portstatus) ||
2662 !port_is_power_on(hub, portstatus) ||
2663 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002664 if (status >= 0)
2665 status = -ENODEV;
2666 }
2667
Alan Stern86c57ed2008-06-30 11:14:43 -04002668 /* Can't do a normal resume if the port isn't enabled,
2669 * so try a reset-resume instead.
2670 */
2671 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2672 if (udev->persist_enabled)
2673 udev->reset_resume = 1;
2674 else
2675 status = -ENODEV;
2676 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002677
2678 if (status) {
2679 dev_dbg(hub->intfdev,
2680 "port %d status %04x.%04x after resume, %d\n",
2681 port1, portchange, portstatus, status);
2682 } else if (udev->reset_resume) {
2683
2684 /* Late port handoff can set status-change bits */
2685 if (portchange & USB_PORT_STAT_C_CONNECTION)
2686 clear_port_feature(hub->hdev, port1,
2687 USB_PORT_FEAT_C_CONNECTION);
2688 if (portchange & USB_PORT_STAT_C_ENABLE)
2689 clear_port_feature(hub->hdev, port1,
2690 USB_PORT_FEAT_C_ENABLE);
2691 }
2692
2693 return status;
2694}
2695
Sarah Sharpf74631e2012-06-25 12:08:08 -07002696int usb_disable_ltm(struct usb_device *udev)
2697{
2698 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2699
2700 /* Check if the roothub and device supports LTM. */
2701 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2702 !usb_device_supports_ltm(udev))
2703 return 0;
2704
2705 /* Clear Feature LTM Enable can only be sent if the device is
2706 * configured.
2707 */
2708 if (!udev->actconfig)
2709 return 0;
2710
2711 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2712 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2713 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2714 USB_CTRL_SET_TIMEOUT);
2715}
2716EXPORT_SYMBOL_GPL(usb_disable_ltm);
2717
2718void usb_enable_ltm(struct usb_device *udev)
2719{
2720 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2721
2722 /* Check if the roothub and device supports LTM. */
2723 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2724 !usb_device_supports_ltm(udev))
2725 return;
2726
2727 /* Set Feature LTM Enable can only be sent if the device is
2728 * configured.
2729 */
2730 if (!udev->actconfig)
2731 return;
2732
2733 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2734 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2735 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2736 USB_CTRL_SET_TIMEOUT);
2737}
2738EXPORT_SYMBOL_GPL(usb_enable_ltm);
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740#ifdef CONFIG_USB_SUSPEND
2741
2742/*
Alan Stern140d8f62006-07-01 22:07:21 -04002743 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002744 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002745 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 *
2747 * Suspends a USB device that isn't in active use, conserving power.
2748 * Devices may wake out of a suspend, if anything important happens,
2749 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002750 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 * to disconnect devices while they are suspended.
2752 *
David Brownell390a8c32005-09-13 19:57:27 -07002753 * This only affects the USB hardware for a device; its interfaces
2754 * (and, for hubs, child devices) must already have been suspended.
2755 *
Alan Stern624d6c02007-05-30 15:35:16 -04002756 * Selective port suspend reduces power; most suspended devices draw
2757 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2758 * All devices below the suspended port are also suspended.
2759 *
2760 * Devices leave suspend state when the host wakes them up. Some devices
2761 * also support "remote wakeup", where the device can activate the USB
2762 * tree above them to deliver data, such as a keypress or packet. In
2763 * some cases, this wakes the USB host.
2764 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 * Suspending OTG devices may trigger HNP, if that's been enabled
2766 * between a pair of dual-role devices. That will change roles, such
2767 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2768 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002769 * Devices on USB hub ports have only one "suspend" state, corresponding
2770 * to ACPI D2, "may cause the device to lose some context".
2771 * State transitions include:
2772 *
2773 * - suspend, resume ... when the VBUS power link stays live
2774 * - suspend, disconnect ... VBUS lost
2775 *
2776 * Once VBUS drop breaks the circuit, the port it's using has to go through
2777 * normal re-enumeration procedures, starting with enabling VBUS power.
2778 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2779 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2780 * timer, no SRP, no requests through sysfs.
2781 *
2782 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2783 * the root hub for their bus goes into global suspend ... so we don't
2784 * (falsely) update the device power state to say it suspended.
2785 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 * Returns 0 on success, else negative errno.
2787 */
Alan Stern65bfd292008-11-25 16:39:18 -05002788int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
Alan Stern624d6c02007-05-30 15:35:16 -04002790 struct usb_hub *hub = hdev_to_hub(udev->parent);
2791 int port1 = udev->portnum;
2792 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002793
Alan Stern624d6c02007-05-30 15:35:16 -04002794 /* enable remote wakeup when appropriate; this lets the device
2795 * wake up the upstream hub (including maybe the root hub).
2796 *
2797 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2798 * we don't explicitly enable it here.
2799 */
2800 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002801 if (!hub_is_superspeed(hub->hdev)) {
2802 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2803 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2804 USB_DEVICE_REMOTE_WAKEUP, 0,
2805 NULL, 0,
2806 USB_CTRL_SET_TIMEOUT);
2807 } else {
2808 /* Assume there's only one function on the USB 3.0
2809 * device and enable remote wake for the first
2810 * interface. FIXME if the interface association
2811 * descriptor shows there's more than one function.
2812 */
2813 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2814 USB_REQ_SET_FEATURE,
2815 USB_RECIP_INTERFACE,
2816 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002817 USB_INTRF_FUNC_SUSPEND_RW |
2818 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002819 NULL, 0,
2820 USB_CTRL_SET_TIMEOUT);
2821 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002822 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002823 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2824 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002825 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002826 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002827 return status;
2828 }
Alan Stern624d6c02007-05-30 15:35:16 -04002829 }
2830
Andiry Xu65580b432011-09-23 14:19:52 -07002831 /* disable USB2 hardware LPM */
2832 if (udev->usb2_hw_lpm_enabled == 1)
2833 usb_set_usb2_hardware_lpm(udev, 0);
2834
Sarah Sharpf74631e2012-06-25 12:08:08 -07002835 if (usb_disable_ltm(udev)) {
2836 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2837 __func__);
2838 return -ENOMEM;
2839 }
Sarah Sharp83060952012-05-02 14:25:52 -07002840 if (usb_unlocked_disable_lpm(udev)) {
2841 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2842 __func__);
2843 return -ENOMEM;
2844 }
2845
Alan Stern624d6c02007-05-30 15:35:16 -04002846 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002847 if (hub_is_superspeed(hub->hdev))
2848 status = set_port_feature(hub->hdev,
2849 port1 | (USB_SS_PORT_LS_U3 << 3),
2850 USB_PORT_FEAT_LINK_STATE);
Andiry Xua8f08d82011-03-31 14:56:50 +08002851 else
2852 status = set_port_feature(hub->hdev, port1,
2853 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002854 if (status) {
2855 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2856 port1, status);
2857 /* paranoia: "should not happen" */
Oliver Neukum0c487202009-10-19 13:19:41 +02002858 if (udev->do_remote_wakeup)
2859 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Alan Stern624d6c02007-05-30 15:35:16 -04002860 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2861 USB_DEVICE_REMOTE_WAKEUP, 0,
2862 NULL, 0,
2863 USB_CTRL_SET_TIMEOUT);
Alan Sterncbb33002011-06-15 16:29:16 -04002864
Andiry Xuc3e751e2012-05-05 00:50:10 +08002865 /* Try to enable USB2 hardware LPM again */
2866 if (udev->usb2_hw_lpm_capable == 1)
2867 usb_set_usb2_hardware_lpm(udev, 1);
2868
Sarah Sharpf74631e2012-06-25 12:08:08 -07002869 /* Try to enable USB3 LTM and LPM again */
2870 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07002871 usb_unlocked_enable_lpm(udev);
2872
Alan Sterncbb33002011-06-15 16:29:16 -04002873 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002874 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002875 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002876 } else {
2877 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002878 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2879 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2880 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002881 usb_set_device_state(udev, USB_STATE_SUSPENDED);
2882 msleep(10);
2883 }
Alan Sternc08512c2010-11-15 15:57:58 -05002884 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04002885 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
David Brownellf3f32532005-09-22 22:37:29 -07002887
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888/*
David Brownell390a8c32005-09-13 19:57:27 -07002889 * If the USB "suspend" state is in use (rather than "global suspend"),
2890 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04002891 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 * hardware resume signaling is finished, either because of selective
2893 * resume (by host) or remote wakeup (by device) ... now see what changed
2894 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04002895 *
2896 * If @udev->reset_resume is set then the device is reset before the
2897 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 */
Alan Stern140d8f62006-07-01 22:07:21 -04002899static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
Alan Stern54515fe2007-05-30 15:38:58 -04002901 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 u16 devstatus;
2903
2904 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002905 dev_dbg(&udev->dev, "%s\n",
2906 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 /* usb ch9 identifies four variants of SUSPENDED, based on what
2909 * state the device resumes to. Linux currently won't see the
2910 * first two on the host side; they'd be inside hub_port_init()
2911 * during many timeouts, but khubd can't suspend until later.
2912 */
2913 usb_set_device_state(udev, udev->actconfig
2914 ? USB_STATE_CONFIGURED
2915 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Alan Stern54515fe2007-05-30 15:38:58 -04002917 /* 10.5.4.5 says not to reset a suspended port if the attached
2918 * device is enabled for remote wakeup. Hence the reset
2919 * operation is carried out here, after the port has been
2920 * resumed.
2921 */
2922 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04002923 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08002924 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 /* 10.5.4.5 says be sure devices in the tree are still there.
2927 * For now let's assume the device didn't go crazy on resume,
2928 * and device drivers will know about any resume quirks.
2929 */
Alan Stern54515fe2007-05-30 15:38:58 -04002930 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04002931 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04002932 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2933 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04002934 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04002935
2936 /* If a normal resume failed, try doing a reset-resume */
2937 if (status && !udev->reset_resume && udev->persist_enabled) {
2938 dev_dbg(&udev->dev, "retry with reset-resume\n");
2939 udev->reset_resume = 1;
2940 goto retry_reset_resume;
2941 }
Alan Stern54515fe2007-05-30 15:38:58 -04002942 }
Alan Sternb40b7a92006-06-19 15:12:38 -04002943
Alan Stern624d6c02007-05-30 15:35:16 -04002944 if (status) {
2945 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2946 status);
2947 } else if (udev->actconfig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04002949 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 status = usb_control_msg(udev,
2951 usb_sndctrlpipe(udev, 0),
2952 USB_REQ_CLEAR_FEATURE,
2953 USB_RECIP_DEVICE,
2954 USB_DEVICE_REMOTE_WAKEUP, 0,
2955 NULL, 0,
2956 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04002957 if (status)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002958 dev_dbg(&udev->dev,
2959 "disable remote wakeup, status %d\n",
2960 status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05002961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 }
2964 return status;
2965}
2966
Alan Stern624d6c02007-05-30 15:35:16 -04002967/*
2968 * usb_port_resume - re-activate a suspended usb device's upstream port
2969 * @udev: device to re-activate, not a root hub
2970 * Context: must be able to sleep; device not locked; pm locks held
2971 *
2972 * This will re-activate the suspended device, increasing power usage
2973 * while letting drivers communicate again with its endpoints.
2974 * USB resume explicitly guarantees that the power session between
2975 * the host and the device is the same as it was when the device
2976 * suspended.
2977 *
Alan Sternfeccc302008-03-03 15:15:59 -05002978 * If @udev->reset_resume is set then this routine won't check that the
2979 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04002980 * reset @udev. The end result is that a broken power session can be
2981 * recovered and @udev will appear to persist across a loss of VBUS power.
2982 *
2983 * For example, if a host controller doesn't maintain VBUS suspend current
2984 * during a system sleep or is reset when the system wakes up, all the USB
2985 * power sessions below it will be broken. This is especially troublesome
2986 * for mass-storage devices containing mounted filesystems, since the
2987 * device will appear to have disconnected and all the memory mappings
2988 * to it will be lost. Using the USB_PERSIST facility, the device can be
2989 * made to appear as if it had not disconnected.
2990 *
Ming Lei742120c2008-06-18 22:00:29 +08002991 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05002992 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04002993 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2994 * quite possible for a device to remain unaltered but its media to be
2995 * changed. If the user replaces a flash memory card while the system is
2996 * asleep, he will have only himself to blame when the filesystem on the
2997 * new card is corrupted and the system crashes.
2998 *
Alan Stern624d6c02007-05-30 15:35:16 -04002999 * Returns 0 on success, else negative errno.
3000 */
Alan Stern65bfd292008-11-25 16:39:18 -05003001int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002{
Alan Stern624d6c02007-05-30 15:35:16 -04003003 struct usb_hub *hub = hdev_to_hub(udev->parent);
3004 int port1 = udev->portnum;
3005 int status;
3006 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003007
3008 /* Skip the initial Clear-Suspend step for a remote wakeup */
3009 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003010 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003011 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
3013 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3014
Alan Sternd5cbad42006-08-11 16:52:39 -04003015 set_bit(port1, hub->busy_bits);
3016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003018 if (hub_is_superspeed(hub->hdev))
3019 status = set_port_feature(hub->hdev,
3020 port1 | (USB_SS_PORT_LS_U0 << 3),
3021 USB_PORT_FEAT_LINK_STATE);
3022 else
3023 status = clear_port_feature(hub->hdev,
3024 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003026 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
3027 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04003030 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003031 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 msleep(25);
3033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3035 * stop resume signaling. Then finish the resume
3036 * sequence.
3037 */
Alan Sternd25450c2006-11-20 11:14:30 -05003038 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003039
Alan Sternb01b03f2008-04-28 11:06:11 -04003040 /* TRSMRCY = 10 msec */
3041 msleep(10);
3042 }
Alan Stern54515fe2007-05-30 15:38:58 -04003043
Alan Sternb01b03f2008-04-28 11:06:11 -04003044 SuspendCleared:
3045 if (status == 0) {
Andiry Xua7114232011-04-27 18:07:50 +08003046 if (hub_is_superspeed(hub->hdev)) {
3047 if (portchange & USB_PORT_STAT_C_LINK_STATE)
3048 clear_port_feature(hub->hdev, port1,
3049 USB_PORT_FEAT_C_PORT_LINK_STATE);
3050 } else {
3051 if (portchange & USB_PORT_STAT_C_SUSPEND)
3052 clear_port_feature(hub->hdev, port1,
3053 USB_PORT_FEAT_C_SUSPEND);
3054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Alan Sternd5cbad42006-08-11 16:52:39 -04003057 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04003058
Alan Sternb01b03f2008-04-28 11:06:11 -04003059 status = check_port_resume_type(udev,
3060 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003061 if (status == 0)
3062 status = finish_port_resume(udev);
3063 if (status < 0) {
3064 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3065 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003066 } else {
3067 /* Try to enable USB2 hardware LPM */
3068 if (udev->usb2_hw_lpm_capable == 1)
3069 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003070
Sarah Sharpf74631e2012-06-25 12:08:08 -07003071 /* Try to enable USB3 LTM and LPM */
3072 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003073 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003074 }
Andiry Xu65580b432011-09-23 14:19:52 -07003075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 return status;
3077}
3078
Alan Stern8808f002008-04-28 11:06:55 -04003079/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05003080int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
3082 int status = 0;
3083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003085 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003086 status = usb_autoresume_device(udev);
3087 if (status == 0) {
3088 /* Let the drivers do their thing, then... */
3089 usb_autosuspend_device(udev);
3090 }
Alan Sternd25450c2006-11-20 11:14:30 -05003091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return status;
3093}
3094
Alan Sternd388dab2006-07-01 22:14:24 -04003095#else /* CONFIG_USB_SUSPEND */
3096
3097/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3098
Alan Stern65bfd292008-11-25 16:39:18 -05003099int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003100{
3101 return 0;
3102}
3103
Alan Sternb01b03f2008-04-28 11:06:11 -04003104/* However we may need to do a reset-resume */
3105
Alan Stern65bfd292008-11-25 16:39:18 -05003106int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003107{
Alan Sternb01b03f2008-04-28 11:06:11 -04003108 struct usb_hub *hub = hdev_to_hub(udev->parent);
3109 int port1 = udev->portnum;
3110 int status;
3111 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04003112
Alan Sternb01b03f2008-04-28 11:06:11 -04003113 status = hub_port_status(hub, port1, &portstatus, &portchange);
3114 status = check_port_resume_type(udev,
3115 hub, port1, status, portchange, portstatus);
3116
3117 if (status) {
3118 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3119 hub_port_logical_disconnect(hub, port1);
3120 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04003121 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08003122 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003123 }
3124 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04003125}
3126
Alan Sternd388dab2006-07-01 22:14:24 -04003127#endif
3128
David Brownelldb690872005-09-13 19:56:33 -07003129static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
3131 struct usb_hub *hub = usb_get_intfdata (intf);
3132 struct usb_device *hdev = hub->hdev;
3133 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003134 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Alan Sterncbb33002011-06-15 16:29:16 -04003136 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3138 struct usb_device *udev;
3139
Lan Tianyuff823c72012-09-05 13:44:32 +08003140 udev = hub->ports[port1 - 1]->child;
Alan Stern6840d252007-09-10 11:34:26 -04003141 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04003142 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003143 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003144 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 }
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003147 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3148 /* Enable hub to send remote wakeup for all ports. */
3149 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3150 status = set_port_feature(hdev,
3151 port1 |
3152 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3153 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3154 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3155 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3156 }
3157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
Harvey Harrison441b62c2008-03-03 16:08:34 -08003159 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003160
David Brownellc9f89fa2005-09-13 19:57:04 -07003161 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003162 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
3166static int hub_resume(struct usb_interface *intf)
3167{
Alan Stern5e6effa2008-03-03 15:15:51 -05003168 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Alan Stern5e6effa2008-03-03 15:15:51 -05003170 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003171 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 return 0;
3173}
3174
Alan Sternb41a60e2007-05-30 15:39:33 -04003175static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003176{
Alan Sternb41a60e2007-05-30 15:39:33 -04003177 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003178
Alan Stern5e6effa2008-03-03 15:15:51 -05003179 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003180 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003181 return 0;
3182}
3183
Alan Stern54515fe2007-05-30 15:38:58 -04003184/**
3185 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3186 * @rhdev: struct usb_device for the root hub
3187 *
3188 * The USB host controller driver calls this function when its root hub
3189 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003190 * has been reset. The routine marks @rhdev as having lost power.
3191 * When the hub driver is resumed it will take notice and carry out
3192 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3193 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003194 */
3195void usb_root_hub_lost_power(struct usb_device *rhdev)
3196{
3197 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3198 rhdev->reset_resume = 1;
3199}
3200EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3201
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003202static const char * const usb3_lpm_names[] = {
3203 "U0",
3204 "U1",
3205 "U2",
3206 "U3",
3207};
3208
3209/*
3210 * Send a Set SEL control transfer to the device, prior to enabling
3211 * device-initiated U1 or U2. This lets the device know the exit latencies from
3212 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3213 * packet from the host.
3214 *
3215 * This function will fail if the SEL or PEL values for udev are greater than
3216 * the maximum allowed values for the link state to be enabled.
3217 */
3218static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3219{
3220 struct usb_set_sel_req *sel_values;
3221 unsigned long long u1_sel;
3222 unsigned long long u1_pel;
3223 unsigned long long u2_sel;
3224 unsigned long long u2_pel;
3225 int ret;
3226
3227 /* Convert SEL and PEL stored in ns to us */
3228 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3229 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3230 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3231 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3232
3233 /*
3234 * Make sure that the calculated SEL and PEL values for the link
3235 * state we're enabling aren't bigger than the max SEL/PEL
3236 * value that will fit in the SET SEL control transfer.
3237 * Otherwise the device would get an incorrect idea of the exit
3238 * latency for the link state, and could start a device-initiated
3239 * U1/U2 when the exit latencies are too high.
3240 */
3241 if ((state == USB3_LPM_U1 &&
3242 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3243 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3244 (state == USB3_LPM_U2 &&
3245 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3246 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003247 dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n",
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003248 usb3_lpm_names[state], u1_sel, u1_pel);
3249 return -EINVAL;
3250 }
3251
3252 /*
3253 * If we're enabling device-initiated LPM for one link state,
3254 * but the other link state has a too high SEL or PEL value,
3255 * just set those values to the max in the Set SEL request.
3256 */
3257 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3258 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3259
3260 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3261 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3262
3263 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3264 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3265
3266 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3267 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3268
3269 /*
3270 * usb_enable_lpm() can be called as part of a failed device reset,
3271 * which may be initiated by an error path of a mass storage driver.
3272 * Therefore, use GFP_NOIO.
3273 */
3274 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3275 if (!sel_values)
3276 return -ENOMEM;
3277
3278 sel_values->u1_sel = u1_sel;
3279 sel_values->u1_pel = u1_pel;
3280 sel_values->u2_sel = cpu_to_le16(u2_sel);
3281 sel_values->u2_pel = cpu_to_le16(u2_pel);
3282
3283 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3284 USB_REQ_SET_SEL,
3285 USB_RECIP_DEVICE,
3286 0, 0,
3287 sel_values, sizeof *(sel_values),
3288 USB_CTRL_SET_TIMEOUT);
3289 kfree(sel_values);
3290 return ret;
3291}
3292
3293/*
3294 * Enable or disable device-initiated U1 or U2 transitions.
3295 */
3296static int usb_set_device_initiated_lpm(struct usb_device *udev,
3297 enum usb3_link_state state, bool enable)
3298{
3299 int ret;
3300 int feature;
3301
3302 switch (state) {
3303 case USB3_LPM_U1:
3304 feature = USB_DEVICE_U1_ENABLE;
3305 break;
3306 case USB3_LPM_U2:
3307 feature = USB_DEVICE_U2_ENABLE;
3308 break;
3309 default:
3310 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3311 __func__, enable ? "enable" : "disable");
3312 return -EINVAL;
3313 }
3314
3315 if (udev->state != USB_STATE_CONFIGURED) {
3316 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3317 "for unconfigured device.\n",
3318 __func__, enable ? "enable" : "disable",
3319 usb3_lpm_names[state]);
3320 return 0;
3321 }
3322
3323 if (enable) {
3324 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003325 * Now send the control transfer to enable device-initiated LPM
3326 * for either U1 or U2.
3327 */
3328 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3329 USB_REQ_SET_FEATURE,
3330 USB_RECIP_DEVICE,
3331 feature,
3332 0, NULL, 0,
3333 USB_CTRL_SET_TIMEOUT);
3334 } else {
3335 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3336 USB_REQ_CLEAR_FEATURE,
3337 USB_RECIP_DEVICE,
3338 feature,
3339 0, NULL, 0,
3340 USB_CTRL_SET_TIMEOUT);
3341 }
3342 if (ret < 0) {
3343 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3344 enable ? "Enable" : "Disable",
3345 usb3_lpm_names[state]);
3346 return -EBUSY;
3347 }
3348 return 0;
3349}
3350
3351static int usb_set_lpm_timeout(struct usb_device *udev,
3352 enum usb3_link_state state, int timeout)
3353{
3354 int ret;
3355 int feature;
3356
3357 switch (state) {
3358 case USB3_LPM_U1:
3359 feature = USB_PORT_FEAT_U1_TIMEOUT;
3360 break;
3361 case USB3_LPM_U2:
3362 feature = USB_PORT_FEAT_U2_TIMEOUT;
3363 break;
3364 default:
3365 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3366 __func__);
3367 return -EINVAL;
3368 }
3369
3370 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3371 timeout != USB3_LPM_DEVICE_INITIATED) {
3372 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3373 "which is a reserved value.\n",
3374 usb3_lpm_names[state], timeout);
3375 return -EINVAL;
3376 }
3377
3378 ret = set_port_feature(udev->parent,
3379 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3380 feature);
3381 if (ret < 0) {
3382 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3383 "error code %i\n", usb3_lpm_names[state],
3384 timeout, ret);
3385 return -EBUSY;
3386 }
3387 if (state == USB3_LPM_U1)
3388 udev->u1_params.timeout = timeout;
3389 else
3390 udev->u2_params.timeout = timeout;
3391 return 0;
3392}
3393
3394/*
3395 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3396 * U1/U2 entry.
3397 *
3398 * We will attempt to enable U1 or U2, but there are no guarantees that the
3399 * control transfers to set the hub timeout or enable device-initiated U1/U2
3400 * will be successful.
3401 *
3402 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3403 * driver know about it. If that call fails, it should be harmless, and just
3404 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3405 */
3406static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3407 enum usb3_link_state state)
3408{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003409 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003410 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3411 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3412
3413 /* If the device says it doesn't have *any* exit latency to come out of
3414 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3415 * state.
3416 */
3417 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3418 (state == USB3_LPM_U2 && u2_mel == 0))
3419 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003420
Sarah Sharp65a95b72012-10-05 10:32:07 -07003421 /*
3422 * First, let the device know about the exit latencies
3423 * associated with the link state we're about to enable.
3424 */
3425 ret = usb_req_set_sel(udev, state);
3426 if (ret < 0) {
3427 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3428 usb3_lpm_names[state]);
3429 return;
3430 }
3431
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003432 /* We allow the host controller to set the U1/U2 timeout internally
3433 * first, so that it can change its schedule to account for the
3434 * additional latency to send data to a device in a lower power
3435 * link state.
3436 */
3437 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3438
3439 /* xHCI host controller doesn't want to enable this LPM state. */
3440 if (timeout == 0)
3441 return;
3442
3443 if (timeout < 0) {
3444 dev_warn(&udev->dev, "Could not enable %s link state, "
3445 "xHCI error %i.\n", usb3_lpm_names[state],
3446 timeout);
3447 return;
3448 }
3449
3450 if (usb_set_lpm_timeout(udev, state, timeout))
3451 /* If we can't set the parent hub U1/U2 timeout,
3452 * device-initiated LPM won't be allowed either, so let the xHCI
3453 * host know that this link state won't be enabled.
3454 */
3455 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3456
3457 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3458 else if (udev->actconfig)
3459 usb_set_device_initiated_lpm(udev, state, true);
3460
3461}
3462
3463/*
3464 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3465 * U1/U2 entry.
3466 *
3467 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3468 * If zero is returned, the parent will not allow the link to go into U1/U2.
3469 *
3470 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3471 * it won't have an effect on the bus link state because the parent hub will
3472 * still disallow device-initiated U1/U2 entry.
3473 *
3474 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3475 * possible. The result will be slightly more bus bandwidth will be taken up
3476 * (to account for U1/U2 exit latency), but it should be harmless.
3477 */
3478static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3479 enum usb3_link_state state)
3480{
3481 int feature;
3482
3483 switch (state) {
3484 case USB3_LPM_U1:
3485 feature = USB_PORT_FEAT_U1_TIMEOUT;
3486 break;
3487 case USB3_LPM_U2:
3488 feature = USB_PORT_FEAT_U2_TIMEOUT;
3489 break;
3490 default:
3491 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3492 __func__);
3493 return -EINVAL;
3494 }
3495
3496 if (usb_set_lpm_timeout(udev, state, 0))
3497 return -EBUSY;
3498
3499 usb_set_device_initiated_lpm(udev, state, false);
3500
3501 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3502 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3503 "bus schedule bandwidth may be impacted.\n",
3504 usb3_lpm_names[state]);
3505 return 0;
3506}
3507
3508/*
3509 * Disable hub-initiated and device-initiated U1 and U2 entry.
3510 * Caller must own the bandwidth_mutex.
3511 *
3512 * This will call usb_enable_lpm() on failure, which will decrement
3513 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3514 */
3515int usb_disable_lpm(struct usb_device *udev)
3516{
3517 struct usb_hcd *hcd;
3518
3519 if (!udev || !udev->parent ||
3520 udev->speed != USB_SPEED_SUPER ||
3521 !udev->lpm_capable)
3522 return 0;
3523
3524 hcd = bus_to_hcd(udev->bus);
3525 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3526 return 0;
3527
3528 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003529 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003530 return 0;
3531
3532 /* If LPM is enabled, attempt to disable it. */
3533 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3534 goto enable_lpm;
3535 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3536 goto enable_lpm;
3537
3538 return 0;
3539
3540enable_lpm:
3541 usb_enable_lpm(udev);
3542 return -EBUSY;
3543}
3544EXPORT_SYMBOL_GPL(usb_disable_lpm);
3545
3546/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3547int usb_unlocked_disable_lpm(struct usb_device *udev)
3548{
3549 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3550 int ret;
3551
3552 if (!hcd)
3553 return -EINVAL;
3554
3555 mutex_lock(hcd->bandwidth_mutex);
3556 ret = usb_disable_lpm(udev);
3557 mutex_unlock(hcd->bandwidth_mutex);
3558
3559 return ret;
3560}
3561EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3562
3563/*
3564 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3565 * xHCI host policy may prevent U1 or U2 from being enabled.
3566 *
3567 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3568 * until the lpm_disable_count drops to zero. Caller must own the
3569 * bandwidth_mutex.
3570 */
3571void usb_enable_lpm(struct usb_device *udev)
3572{
3573 struct usb_hcd *hcd;
3574
3575 if (!udev || !udev->parent ||
3576 udev->speed != USB_SPEED_SUPER ||
3577 !udev->lpm_capable)
3578 return;
3579
3580 udev->lpm_disable_count--;
3581 hcd = bus_to_hcd(udev->bus);
3582 /* Double check that we can both enable and disable LPM.
3583 * Device must be configured to accept set feature U1/U2 timeout.
3584 */
3585 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3586 !hcd->driver->disable_usb3_lpm_timeout)
3587 return;
3588
3589 if (udev->lpm_disable_count > 0)
3590 return;
3591
3592 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3593 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3594}
3595EXPORT_SYMBOL_GPL(usb_enable_lpm);
3596
3597/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3598void usb_unlocked_enable_lpm(struct usb_device *udev)
3599{
3600 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3601
3602 if (!hcd)
3603 return;
3604
3605 mutex_lock(hcd->bandwidth_mutex);
3606 usb_enable_lpm(udev);
3607 mutex_unlock(hcd->bandwidth_mutex);
3608}
3609EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3610
3611
Alan Sternd388dab2006-07-01 22:14:24 -04003612#else /* CONFIG_PM */
3613
Alan Sternf07600c2007-05-30 15:38:16 -04003614#define hub_suspend NULL
3615#define hub_resume NULL
3616#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003617
3618int usb_disable_lpm(struct usb_device *udev)
3619{
3620 return 0;
3621}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003622EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003623
3624void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003625EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003626
3627int usb_unlocked_disable_lpm(struct usb_device *udev)
3628{
3629 return 0;
3630}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003631EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003632
3633void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003634EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07003635
3636int usb_disable_ltm(struct usb_device *udev)
3637{
3638 return 0;
3639}
3640EXPORT_SYMBOL_GPL(usb_disable_ltm);
3641
3642void usb_enable_ltm(struct usb_device *udev) { }
3643EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternd388dab2006-07-01 22:14:24 -04003644#endif
3645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
3647/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3648 *
3649 * Between connect detection and reset signaling there must be a delay
3650 * of 100ms at least for debounce and power-settling. The corresponding
3651 * timer shall restart whenever the downstream port detects a disconnect.
3652 *
3653 * Apparently there are some bluetooth and irda-dongles and a number of
3654 * low-speed devices for which this debounce period may last over a second.
3655 * Not covered by the spec - but easy to deal with.
3656 *
3657 * This implementation uses a 1500ms total debounce timeout; if the
3658 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3659 * every 25ms for transient disconnects. When the port status has been
3660 * unchanged for 100ms it returns the port status.
3661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662static int hub_port_debounce(struct usb_hub *hub, int port1)
3663{
3664 int ret;
3665 int total_time, stable_time = 0;
3666 u16 portchange, portstatus;
3667 unsigned connection = 0xffff;
3668
3669 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3670 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3671 if (ret < 0)
3672 return ret;
3673
3674 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3675 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3676 stable_time += HUB_DEBOUNCE_STEP;
3677 if (stable_time >= HUB_DEBOUNCE_STABLE)
3678 break;
3679 } else {
3680 stable_time = 0;
3681 connection = portstatus & USB_PORT_STAT_CONNECTION;
3682 }
3683
3684 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3685 clear_port_feature(hub->hdev, port1,
3686 USB_PORT_FEAT_C_CONNECTION);
3687 }
3688
3689 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3690 break;
3691 msleep(HUB_DEBOUNCE_STEP);
3692 }
3693
3694 dev_dbg (hub->intfdev,
3695 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3696 port1, total_time, stable_time, portstatus);
3697
3698 if (stable_time < HUB_DEBOUNCE_STABLE)
3699 return -ETIMEDOUT;
3700 return portstatus;
3701}
3702
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003703void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704{
Alan Sternddeac4e2009-01-15 17:03:33 -05003705 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3706 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003707 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003709EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
3711#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3712#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3713
Alan Stern4326ed02007-07-30 17:08:43 -04003714static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715{
3716 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003717 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
Sarah Sharpc6515272009-04-27 19:57:26 -07003719 /*
3720 * The host controller will choose the device address,
3721 * instead of the core having chosen it earlier
3722 */
3723 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 return -EINVAL;
3725 if (udev->state == USB_STATE_ADDRESS)
3726 return 0;
3727 if (udev->state != USB_STATE_DEFAULT)
3728 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003729 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003730 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003731 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003732 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3733 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3734 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003736 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003737 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003739 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 }
3741 return retval;
3742}
3743
3744/* Reset device, (re)assign address, get device descriptor.
3745 * Device connection must be stable, no more debouncing needed.
3746 * Returns device in USB_STATE_ADDRESS, except on error.
3747 *
3748 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003749 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 * newly detected device that is not accessible through any global
3751 * pointers, it's not necessary to lock the device.
3752 */
3753static int
3754hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3755 int retry_counter)
3756{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003757 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
3759 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003760 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 int i, j, retval;
3762 unsigned delay = HUB_SHORT_RESET_TIME;
3763 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003764 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003765 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
3767 /* root hub ports have a slightly longer reset period
3768 * (from USB 2.0 spec, section 7.1.7.5)
3769 */
3770 if (!hdev->parent) {
3771 delay = HUB_ROOT_RESET_TIME;
3772 if (port1 == hdev->bus->otg_port)
3773 hdev->bus->b_hnp_enable = 0;
3774 }
3775
3776 /* Some low speed devices have problems with the quick delay, so */
3777 /* be a bit pessimistic with those devices. RHbug #23670 */
3778 if (oldspeed == USB_SPEED_LOW)
3779 delay = HUB_LONG_RESET_TIME;
3780
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003781 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
Luben Tuikov07194ab2011-02-11 11:33:10 -08003783 /* Reset the device; full speed may morph to high speed */
3784 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003785 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003786 if (retval < 0) /* error or disconnect */
3787 goto fail;
3788 /* success, speed is known */
3789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 retval = -ENODEV;
3791
3792 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3793 dev_dbg(&udev->dev, "device reset changed speed!\n");
3794 goto fail;
3795 }
3796 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3799 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003800 * For Wireless USB devices, ep0 max packet is always 512 (tho
3801 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 */
3803 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003804 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003805 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003806 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003809 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 break;
3811 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3812 /* to determine the ep0 maxpacket size, try to read
3813 * the device descriptor to get bMaxPacketSize0 and
3814 * then correct our initial guess.
3815 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003816 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 break;
3818 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003819 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 break;
3821 default:
3822 goto fail;
3823 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003824
3825 if (udev->speed == USB_SPEED_WIRELESS)
3826 speed = "variable speed Wireless";
3827 else
3828 speed = usb_speed_string(udev->speed);
3829
Sarah Sharpc6515272009-04-27 19:57:26 -07003830 if (udev->speed != USB_SPEED_SUPER)
3831 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003832 "%s %s USB device number %d using %s\n",
3833 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003834 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
3836 /* Set up TT records, if needed */
3837 if (hdev->tt) {
3838 udev->tt = hdev->tt;
3839 udev->ttport = hdev->ttport;
3840 } else if (udev->speed != USB_SPEED_HIGH
3841 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003842 if (!hub->tt.hub) {
3843 dev_err(&udev->dev, "parent hub has no TT\n");
3844 retval = -EINVAL;
3845 goto fail;
3846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 udev->tt = &hub->tt;
3848 udev->ttport = port1;
3849 }
3850
3851 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3852 * Because device hardware and firmware is sometimes buggy in
3853 * this area, and this is how Linux has done it for ages.
3854 * Change it cautiously.
3855 *
3856 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3857 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3858 * so it may help with some non-standards-compliant devices.
3859 * Otherwise we start with SET_ADDRESS and then try to read the
3860 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3861 * value.
3862 */
3863 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07003864 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 struct usb_device_descriptor *buf;
3866 int r = 0;
3867
3868#define GET_DESCRIPTOR_BUFSIZE 64
3869 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3870 if (!buf) {
3871 retval = -ENOMEM;
3872 continue;
3873 }
3874
Alan Sternb89ee192007-05-11 10:19:04 -04003875 /* Retry on all errors; some devices are flakey.
3876 * 255 is for WUSB devices, we actually need to use
3877 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 */
3879 for (j = 0; j < 3; ++j) {
3880 buf->bMaxPacketSize0 = 0;
3881 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3882 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3883 USB_DT_DEVICE << 8, 0,
3884 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02003885 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003887 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 if (buf->bDescriptorType ==
3889 USB_DT_DEVICE) {
3890 r = 0;
3891 break;
3892 }
3893 /* FALL THROUGH */
3894 default:
3895 if (r == 0)
3896 r = -EPROTO;
3897 break;
3898 }
3899 if (r == 0)
3900 break;
3901 }
3902 udev->descriptor.bMaxPacketSize0 =
3903 buf->bMaxPacketSize0;
3904 kfree(buf);
3905
Andiry Xu75d7cf72011-09-13 16:41:11 -07003906 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 if (retval < 0) /* error or disconnect */
3908 goto fail;
3909 if (oldspeed != udev->speed) {
3910 dev_dbg(&udev->dev,
3911 "device reset changed speed!\n");
3912 retval = -ENODEV;
3913 goto fail;
3914 }
3915 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003916 dev_err(&udev->dev,
3917 "device descriptor read/64, error %d\n",
3918 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 retval = -EMSGSIZE;
3920 continue;
3921 }
3922#undef GET_DESCRIPTOR_BUFSIZE
3923 }
3924
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003925 /*
3926 * If device is WUSB, we already assigned an
3927 * unauthorized address in the Connect Ack sequence;
3928 * authorization will assign the final address.
3929 */
Sarah Sharpc6515272009-04-27 19:57:26 -07003930 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003931 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3932 retval = hub_set_address(udev, devnum);
3933 if (retval >= 0)
3934 break;
3935 msleep(200);
3936 }
3937 if (retval < 0) {
3938 dev_err(&udev->dev,
3939 "device not accepting address %d, error %d\n",
3940 devnum, retval);
3941 goto fail;
3942 }
Sarah Sharpc6515272009-04-27 19:57:26 -07003943 if (udev->speed == USB_SPEED_SUPER) {
3944 devnum = udev->devnum;
3945 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05003946 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07003947 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05003948 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07003949 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003950
3951 /* cope with hardware quirkiness:
3952 * - let SET_ADDRESS settle, some device hardware wants it
3953 * - read ep0 maxpacket even for high and low speed,
3954 */
3955 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07003956 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
3960 retval = usb_get_device_descriptor(udev, 8);
3961 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003962 dev_err(&udev->dev,
3963 "device descriptor read/8, error %d\n",
3964 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 if (retval >= 0)
3966 retval = -EMSGSIZE;
3967 } else {
3968 retval = 0;
3969 break;
3970 }
3971 }
3972 if (retval)
3973 goto fail;
3974
Elric Fud8aec3d2012-03-26 21:16:02 +08003975 /*
3976 * Some superspeed devices have finished the link training process
3977 * and attached to a superspeed hub port, but the device descriptor
3978 * got from those devices show they aren't superspeed devices. Warm
3979 * reset the port attached by the devices can fix them.
3980 */
3981 if ((udev->speed == USB_SPEED_SUPER) &&
3982 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
3983 dev_err(&udev->dev, "got a wrong device descriptor, "
3984 "warm reset device\n");
3985 hub_port_reset(hub, port1, udev,
3986 HUB_BH_RESET_TIME, true);
3987 retval = -EINVAL;
3988 goto fail;
3989 }
3990
Sarah Sharp6b403b02009-04-27 19:54:10 -07003991 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
3992 udev->speed == USB_SPEED_SUPER)
3993 i = 512;
3994 else
3995 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003996 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04003997 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04003999 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 retval = -EMSGSIZE;
4001 goto fail;
4002 }
Alan Stern56626a72010-10-14 15:25:21 -04004003 if (udev->speed == USB_SPEED_FULL)
4004 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4005 else
4006 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004008 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 }
4010
4011 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4012 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004013 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4014 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 if (retval >= 0)
4016 retval = -ENOMSG;
4017 goto fail;
4018 }
4019
Andiry Xu1ff4df52011-09-23 14:19:48 -07004020 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4021 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004022 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004023 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004024 usb_set_lpm_parameters(udev);
4025 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004026 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004027
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004029 /* notify HCD that we have a device connected and addressed */
4030 if (hcd->driver->update_device)
4031 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004033 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004035 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004036 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01004037 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 return retval;
4039}
4040
4041static void
4042check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4043{
4044 struct usb_qualifier_descriptor *qual;
4045 int status;
4046
Christoph Lametere94b1762006-12-06 20:33:17 -08004047 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 if (qual == NULL)
4049 return;
4050
4051 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4052 qual, sizeof *qual);
4053 if (status == sizeof *qual) {
4054 dev_info(&udev->dev, "not running at top speed; "
4055 "connect to a high speed hub\n");
4056 /* hub LEDs are probably harder to miss than syslog */
4057 if (hub->has_indicators) {
4058 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004059 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 }
4061 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004062 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063}
4064
4065static unsigned
4066hub_power_remaining (struct usb_hub *hub)
4067{
4068 struct usb_device *hdev = hub->hdev;
4069 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004070 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
Alan Stern55c52712005-11-23 12:03:12 -05004072 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 return 0;
4074
Alan Stern55c52712005-11-23 12:03:12 -05004075 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4076 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c72012-09-05 13:44:32 +08004077 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern55c52712005-11-23 12:03:12 -05004078 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
4080 if (!udev)
4081 continue;
4082
Alan Stern55c52712005-11-23 12:03:12 -05004083 /* Unconfigured devices may not use more than 100mA,
4084 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05004086 delta = udev->actconfig->desc.bMaxPower * 2;
4087 else if (port1 != udev->bus->otg_port || hdev->parent)
4088 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 else
Alan Stern55c52712005-11-23 12:03:12 -05004090 delta = 8;
4091 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004092 dev_warn(&udev->dev,
4093 "%dmA is over %umA budget for port %d!\n",
4094 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 remaining -= delta;
4096 }
4097 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004098 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4099 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 remaining = 0;
4101 }
4102 return remaining;
4103}
4104
4105/* Handle physical or logical connection change events.
4106 * This routine is called when:
4107 * a port connection-change occurs;
4108 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08004109 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 * a firmware download)
4111 * caller already locked the hub
4112 */
4113static void hub_port_connect_change(struct usb_hub *hub, int port1,
4114 u16 portstatus, u16 portchange)
4115{
4116 struct usb_device *hdev = hub->hdev;
4117 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05304118 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04004119 unsigned wHubCharacteristics =
4120 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04004121 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04004123
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 dev_dbg (hub_dev,
4125 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08004126 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
4128 if (hub->has_indicators) {
4129 set_port_led(hub, port1, HUB_LED_AUTO);
4130 hub->indicator[port1-1] = INDICATOR_AUTO;
4131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
4133#ifdef CONFIG_USB_OTG
4134 /* during HNP, don't repeat the debounce */
4135 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04004136 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4137 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138#endif
4139
Alan Stern8808f002008-04-28 11:06:55 -04004140 /* Try to resuscitate an existing device */
Lan Tianyuff823c72012-09-05 13:44:32 +08004141 udev = hub->ports[port1 - 1]->child;
Alan Stern8808f002008-04-28 11:06:55 -04004142 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4143 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04004144 usb_lock_device(udev);
4145 if (portstatus & USB_PORT_STAT_ENABLE) {
4146 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004147
4148#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04004149 } else if (udev->state == USB_STATE_SUSPENDED &&
4150 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004151 /* For a suspended device, treat this as a
4152 * remote wakeup event.
4153 */
Alan Stern0534d462010-01-08 12:56:30 -05004154 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004155#endif
4156
4157 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004158 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004159 }
4160 usb_unlock_device(udev);
4161
4162 if (status == 0) {
4163 clear_bit(port1, hub->change_bits);
4164 return;
4165 }
4166 }
4167
Alan Stern24618b02008-04-28 11:06:28 -04004168 /* Disconnect any existing devices under this port */
Alan Stern8808f002008-04-28 11:06:55 -04004169 if (udev)
Lan Tianyuff823c72012-09-05 13:44:32 +08004170 usb_disconnect(&hub->ports[port1 - 1]->child);
Alan Stern24618b02008-04-28 11:06:28 -04004171 clear_bit(port1, hub->change_bits);
4172
Alan Stern253e0572009-10-27 15:20:13 -04004173 /* We can forget about a "removed" device when there's a physical
4174 * disconnect or the connect status changes.
4175 */
4176 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4177 (portchange & USB_PORT_STAT_C_CONNECTION))
4178 clear_bit(port1, hub->removed_bits);
4179
Alan Stern5257d972008-09-22 14:43:08 -04004180 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4181 USB_PORT_STAT_C_ENABLE)) {
4182 status = hub_port_debounce(hub, port1);
4183 if (status < 0) {
4184 if (printk_ratelimit())
4185 dev_err(hub_dev, "connect-debounce failed, "
4186 "port %d disabled\n", port1);
4187 portstatus &= ~USB_PORT_STAT_CONNECTION;
4188 } else {
4189 portstatus = status;
4190 }
4191 }
4192
Richard Zhao925aa462012-07-11 11:09:28 +08004193 if (hcd->phy && !hdev->parent) {
4194 if (portstatus & USB_PORT_STAT_CONNECTION)
4195 usb_phy_notify_connect(hcd->phy, port1);
4196 else
4197 usb_phy_notify_disconnect(hcd->phy, port1);
4198 }
4199
Alan Stern253e0572009-10-27 15:20:13 -04004200 /* Return now if debouncing failed or nothing is connected or
4201 * the device was "removed".
4202 */
4203 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4204 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
4206 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07004207 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08004208 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004210
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 if (portstatus & USB_PORT_STAT_ENABLE)
4212 goto done;
4213 return;
4214 }
4215
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
4218 /* reallocate for each attempt, since references
4219 * to the previous one can escape in various ways
4220 */
4221 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4222 if (!udev) {
4223 dev_err (hub_dev,
4224 "couldn't allocate port %d usb_device\n",
4225 port1);
4226 goto done;
4227 }
4228
4229 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05004230 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004231 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004232 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004233
Sarah Sharp131dec32010-12-06 21:00:19 -08004234 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4235 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004236 udev->speed = USB_SPEED_SUPER;
4237 else
4238 udev->speed = USB_SPEED_UNKNOWN;
4239
Alan Stern3b29b682011-02-22 09:53:41 -05004240 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004241 if (udev->devnum <= 0) {
4242 status = -ENOTCONN; /* Don't retry */
4243 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004244 }
4245
Sarah Sharpe7b77172009-04-27 19:54:26 -07004246 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 status = hub_port_init(hub, udev, port1, i);
4248 if (status < 0)
4249 goto loop;
4250
Phil Dibowitz93362a82010-07-22 00:05:01 +02004251 usb_detect_quirks(udev);
4252 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4253 msleep(1000);
4254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 /* consecutive bus-powered hubs aren't reliable; they can
4256 * violate the voltage drop budget. if the new child has
4257 * a "powered" LED, users should notice we didn't enable it
4258 * (without reading syslog), even without per-port LEDs
4259 * on the parent.
4260 */
4261 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05004262 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 u16 devstat;
4264
4265 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4266 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05004267 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 dev_dbg(&udev->dev, "get status %d ?\n", status);
4269 goto loop_disable;
4270 }
Alan Stern55c52712005-11-23 12:03:12 -05004271 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4273 dev_err(&udev->dev,
4274 "can't connect bus-powered hub "
4275 "to this port\n");
4276 if (hub->has_indicators) {
4277 hub->indicator[port1-1] =
4278 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004279 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 }
4281 status = -ENOTCONN; /* Don't retry */
4282 goto loop_disable;
4283 }
4284 }
4285
4286 /* check for devices running slower than they could */
4287 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4288 && udev->speed == USB_SPEED_FULL
4289 && highspeed_hubs != 0)
4290 check_highspeed (hub, udev, port1);
4291
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004292 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 * udev becomes globally accessible, although presumably
4294 * no one will look at it until hdev is unlocked.
4295 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 status = 0;
4297
4298 /* We mustn't add new devices if the parent hub has
4299 * been disconnected; we would race with the
4300 * recursively_mark_NOTATTACHED() routine.
4301 */
4302 spin_lock_irq(&device_state_lock);
4303 if (hdev->state == USB_STATE_NOTATTACHED)
4304 status = -ENOTCONN;
4305 else
Lan Tianyuff823c72012-09-05 13:44:32 +08004306 hub->ports[port1 - 1]->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 spin_unlock_irq(&device_state_lock);
4308
4309 /* Run it through the hoops (find a driver, etc) */
4310 if (!status) {
4311 status = usb_new_device(udev);
4312 if (status) {
4313 spin_lock_irq(&device_state_lock);
Lan Tianyuff823c72012-09-05 13:44:32 +08004314 hub->ports[port1 - 1]->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 spin_unlock_irq(&device_state_lock);
4316 }
4317 }
4318
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 if (status)
4320 goto loop_disable;
4321
4322 status = hub_power_remaining(hub);
4323 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05004324 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
4326 return;
4327
4328loop_disable:
4329 hub_port_disable(hub, port1, 1);
4330loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004331 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004332 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004333 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004335 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 break;
4337 }
Alan Stern3a311552008-05-20 16:58:29 -04004338 if (hub->hdev->parent ||
4339 !hcd->driver->port_handed_over ||
4340 !(hcd->driver->port_handed_over)(hcd, port1))
4341 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4342 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
4344done:
4345 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304346 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4347 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348}
4349
Sarah Sharp714b07b2012-01-24 13:53:18 -08004350/* Returns 1 if there was a remote wakeup and a connect status change. */
4351static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004352 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004353{
4354 struct usb_device *hdev;
4355 struct usb_device *udev;
4356 int connect_change = 0;
4357 int ret;
4358
4359 hdev = hub->hdev;
Lan Tianyuff823c72012-09-05 13:44:32 +08004360 udev = hub->ports[port - 1]->child;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004361 if (!hub_is_superspeed(hdev)) {
4362 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4363 return 0;
4364 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4365 } else {
4366 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004367 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4368 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004369 return 0;
4370 }
4371
Sarah Sharp714b07b2012-01-24 13:53:18 -08004372 if (udev) {
4373 /* TRSMRCY = 10 msec */
4374 msleep(10);
4375
4376 usb_lock_device(udev);
4377 ret = usb_remote_wakeup(udev);
4378 usb_unlock_device(udev);
4379 if (ret < 0)
4380 connect_change = 1;
4381 } else {
4382 ret = -ENODEV;
4383 hub_port_disable(hub, port, 1);
4384 }
4385 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4386 port, ret);
4387 return connect_change;
4388}
4389
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390static void hub_events(void)
4391{
4392 struct list_head *tmp;
4393 struct usb_device *hdev;
4394 struct usb_interface *intf;
4395 struct usb_hub *hub;
4396 struct device *hub_dev;
4397 u16 hubstatus;
4398 u16 hubchange;
4399 u16 portstatus;
4400 u16 portchange;
4401 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004402 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
4404 /*
4405 * We restart the list every time to avoid a deadlock with
4406 * deleting hubs downstream from this one. This should be
4407 * safe since we delete the hub from the event list.
4408 * Not the most efficient, but avoids deadlocks.
4409 */
4410 while (1) {
4411
4412 /* Grab the first entry at the beginning of the list */
4413 spin_lock_irq(&hub_event_lock);
4414 if (list_empty(&hub_event_list)) {
4415 spin_unlock_irq(&hub_event_lock);
4416 break;
4417 }
4418
4419 tmp = hub_event_list.next;
4420 list_del_init(tmp);
4421
4422 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004423 kref_get(&hub->kref);
4424 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
Alan Sterne8054852007-05-04 11:55:11 -04004426 hdev = hub->hdev;
4427 hub_dev = hub->intfdev;
4428 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004429 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 hdev->state, hub->descriptor
4431 ? hub->descriptor->bNbrPorts
4432 : 0,
4433 /* NOTE: expects max 15 ports... */
4434 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004435 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 /* Lock the device, then check to see if we were
4438 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004439 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004440 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004441 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
4443 /* If the hub has died, clean up after it */
4444 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004445 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004446 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 goto loop;
4448 }
4449
Alan Stern40f122f2006-11-09 14:44:33 -05004450 /* Autoresume */
4451 ret = usb_autopm_get_interface(intf);
4452 if (ret) {
4453 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004455 }
4456
4457 /* If this is an inactive hub, do nothing */
4458 if (hub->quiescing)
4459 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
4461 if (hub->error) {
4462 dev_dbg (hub_dev, "resetting for error %d\n",
4463 hub->error);
4464
Ming Lei742120c2008-06-18 22:00:29 +08004465 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 if (ret) {
4467 dev_dbg (hub_dev,
4468 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004469 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 }
4471
4472 hub->nerrors = 0;
4473 hub->error = 0;
4474 }
4475
4476 /* deal with port status changes */
4477 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4478 if (test_bit(i, hub->busy_bits))
4479 continue;
4480 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004481 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004483 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 continue;
4485
4486 ret = hub_port_status(hub, i,
4487 &portstatus, &portchange);
4488 if (ret < 0)
4489 continue;
4490
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4492 clear_port_feature(hdev, i,
4493 USB_PORT_FEAT_C_CONNECTION);
4494 connect_change = 1;
4495 }
4496
4497 if (portchange & USB_PORT_STAT_C_ENABLE) {
4498 if (!connect_change)
4499 dev_dbg (hub_dev,
4500 "port %d enable change, "
4501 "status %08x\n",
4502 i, portstatus);
4503 clear_port_feature(hdev, i,
4504 USB_PORT_FEAT_C_ENABLE);
4505
4506 /*
4507 * EM interference sometimes causes badly
4508 * shielded USB devices to be shutdown by
4509 * the hub, this hack enables them again.
4510 * Works at least with mouse driver.
4511 */
4512 if (!(portstatus & USB_PORT_STAT_ENABLE)
4513 && !connect_change
Lan Tianyuff823c72012-09-05 13:44:32 +08004514 && hub->ports[i - 1]->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 dev_err (hub_dev,
4516 "port %i "
4517 "disabled by hub (EMI?), "
4518 "re-enabling...\n",
4519 i);
4520 connect_change = 1;
4521 }
4522 }
4523
Sarah Sharp72937e12012-01-24 11:46:50 -08004524 if (hub_handle_remote_wakeup(hub, i,
4525 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004526 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004529 u16 status = 0;
4530 u16 unused;
4531
4532 dev_dbg(hub_dev, "over-current change on port "
4533 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 clear_port_feature(hdev, i,
4535 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004536 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004537 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004538 hub_port_status(hub, i, &status, &unused);
4539 if (status & USB_PORT_STAT_OVERCURRENT)
4540 dev_err(hub_dev, "over-current "
4541 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 }
4543
4544 if (portchange & USB_PORT_STAT_C_RESET) {
4545 dev_dbg (hub_dev,
4546 "reset change on port %d\n",
4547 i);
4548 clear_port_feature(hdev, i,
4549 USB_PORT_FEAT_C_RESET);
4550 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004551 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4552 hub_is_superspeed(hub->hdev)) {
4553 dev_dbg(hub_dev,
4554 "warm reset change on port %d\n",
4555 i);
4556 clear_port_feature(hdev, i,
4557 USB_PORT_FEAT_C_BH_PORT_RESET);
4558 }
John Youndbe79bb2001-09-17 00:00:00 -07004559 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4560 clear_port_feature(hub->hdev, i,
4561 USB_PORT_FEAT_C_PORT_LINK_STATE);
4562 }
4563 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4564 dev_warn(hub_dev,
4565 "config error on port %d\n",
4566 i);
4567 clear_port_feature(hub->hdev, i,
4568 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Andiry Xu5e467f62011-04-27 18:07:54 +08004571 /* Warm reset a USB3 protocol port if it's in
4572 * SS.Inactive state.
4573 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02004574 if (hub_port_warm_reset_required(hub, portstatus)) {
Andiry Xu5e467f62011-04-27 18:07:54 +08004575 dev_dbg(hub_dev, "warm reset port %d\n", i);
Andiry Xu75d7cf72011-09-13 16:41:11 -07004576 hub_port_reset(hub, i, NULL,
4577 HUB_BH_RESET_TIME, true);
Andiry Xu5e467f62011-04-27 18:07:54 +08004578 }
4579
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 if (connect_change)
4581 hub_port_connect_change(hub, i,
4582 portstatus, portchange);
4583 } /* end for i */
4584
4585 /* deal with hub status changes */
4586 if (test_and_clear_bit(0, hub->event_bits) == 0)
4587 ; /* do nothing */
4588 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4589 dev_err (hub_dev, "get_hub_status failed\n");
4590 else {
4591 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4592 dev_dbg (hub_dev, "power change\n");
4593 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004594 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4595 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004596 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004597 else
4598 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 }
4600 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004601 u16 status = 0;
4602 u16 unused;
4603
4604 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004606 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004607 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004608 hub_hub_status(hub, &status, &unused);
4609 if (status & HUB_STATUS_OVERCURRENT)
4610 dev_err(hub_dev, "over-current "
4611 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 }
4613 }
4614
Alan Stern8e4ceb32009-12-07 13:01:37 -05004615 loop_autopm:
4616 /* Balance the usb_autopm_get_interface() above */
4617 usb_autopm_put_interface_no_suspend(intf);
4618 loop:
4619 /* Balance the usb_autopm_get_interface_no_resume() in
4620 * kick_khubd() and allow autosuspend.
4621 */
4622 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004623 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004625 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
4627 } /* end while (1) */
4628}
4629
4630static int hub_thread(void *__unused)
4631{
Alan Stern3bb1af52008-03-03 15:15:36 -05004632 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4633 * port handover. Otherwise it might see that a full-speed device
4634 * was gone before the EHCI controller had handed its port over to
4635 * the companion full-speed controller.
4636 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004637 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004638
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 do {
4640 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004641 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004642 !list_empty(&hub_event_list) ||
4643 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004644 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004646 pr_debug("%s: khubd exiting\n", usbcore_name);
4647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648}
4649
Németh Márton1e927d92010-01-10 15:34:53 +01004650static const struct usb_device_id hub_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4652 .bDeviceClass = USB_CLASS_HUB},
4653 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4654 .bInterfaceClass = USB_CLASS_HUB},
4655 { } /* Terminating entry */
4656};
4657
4658MODULE_DEVICE_TABLE (usb, hub_id_table);
4659
4660static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 .name = "hub",
4662 .probe = hub_probe,
4663 .disconnect = hub_disconnect,
4664 .suspend = hub_suspend,
4665 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004666 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004667 .pre_reset = hub_pre_reset,
4668 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004669 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004671 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672};
4673
4674int usb_hub_init(void)
4675{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 if (usb_register(&hub_driver) < 0) {
4677 printk(KERN_ERR "%s: can't register hub driver\n",
4678 usbcore_name);
4679 return -1;
4680 }
4681
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004682 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4683 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
4686 /* Fall through if kernel_thread failed */
4687 usb_deregister(&hub_driver);
4688 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4689
4690 return -1;
4691}
4692
4693void usb_hub_cleanup(void)
4694{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004695 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
4697 /*
4698 * Hub resources are freed for us by usb_deregister. It calls
4699 * usb_driver_purge on every device which in turn calls that
4700 * devices disconnect function if it is using this driver.
4701 * The hub_disconnect function takes care of releasing the
4702 * individual hub resources. -greg
4703 */
4704 usb_deregister(&hub_driver);
4705} /* usb_hub_cleanup() */
4706
Alan Sterneb764c42008-03-03 15:16:04 -05004707static int descriptors_changed(struct usb_device *udev,
4708 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709{
Alan Sterneb764c42008-03-03 15:16:04 -05004710 int changed = 0;
4711 unsigned index;
4712 unsigned serial_len = 0;
4713 unsigned len;
4714 unsigned old_length;
4715 int length;
4716 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
Alan Sterneb764c42008-03-03 15:16:04 -05004718 if (memcmp(&udev->descriptor, old_device_descriptor,
4719 sizeof(*old_device_descriptor)) != 0)
4720 return 1;
4721
4722 /* Since the idVendor, idProduct, and bcdDevice values in the
4723 * device descriptor haven't changed, we will assume the
4724 * Manufacturer and Product strings haven't changed either.
4725 * But the SerialNumber string could be different (e.g., a
4726 * different flash card of the same brand).
4727 */
4728 if (udev->serial)
4729 serial_len = strlen(udev->serial) + 1;
4730
4731 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004733 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4734 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 }
Alan Sterneb764c42008-03-03 15:16:04 -05004736
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004737 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 if (buf == NULL) {
4739 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4740 /* assume the worst */
4741 return 1;
4742 }
4743 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004744 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4746 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004747 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 dev_dbg(&udev->dev, "config index %d, error %d\n",
4749 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004750 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 break;
4752 }
4753 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4754 != 0) {
4755 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004756 index,
4757 ((struct usb_config_descriptor *) buf)->
4758 bConfigurationValue);
4759 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 break;
4761 }
4762 }
Alan Sterneb764c42008-03-03 15:16:04 -05004763
4764 if (!changed && serial_len) {
4765 length = usb_string(udev, udev->descriptor.iSerialNumber,
4766 buf, serial_len);
4767 if (length + 1 != serial_len) {
4768 dev_dbg(&udev->dev, "serial string error %d\n",
4769 length);
4770 changed = 1;
4771 } else if (memcmp(buf, udev->serial, length) != 0) {
4772 dev_dbg(&udev->dev, "serial string changed\n");
4773 changed = 1;
4774 }
4775 }
4776
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004778 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779}
4780
4781/**
Ming Lei742120c2008-06-18 22:00:29 +08004782 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4784 *
Alan Stern79efa092006-06-01 13:33:42 -04004785 * WARNING - don't use this routine to reset a composite device
4786 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004787 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 *
4789 * Do a port reset, reassign the device's address, and establish its
4790 * former operating configuration. If the reset fails, or the device's
4791 * descriptors change from their values before the reset, or the original
4792 * configuration and altsettings cannot be restored, a flag will be set
4793 * telling khubd to pretend the device has been disconnected and then
4794 * re-connected. All drivers will be unbound, and the device will be
4795 * re-enumerated and probed all over again.
4796 *
4797 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4798 * flagged for logical disconnection, or some other negative error code
4799 * if the reset wasn't even attempted.
4800 *
4801 * The caller must own the device lock. For example, it's safe to use
4802 * this from a driver probe() routine after downloading new firmware.
4803 * For calls that might not occur during probe(), drivers should lock
4804 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004805 *
4806 * Locking exception: This routine may also be called from within an
4807 * autoresume handler. Such usage won't conflict with other tasks
4808 * holding the device lock because these tasks should always call
4809 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 */
Ming Lei742120c2008-06-18 22:00:29 +08004811static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812{
4813 struct usb_device *parent_hdev = udev->parent;
4814 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004815 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004817 int i, ret = 0;
4818 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
4820 if (udev->state == USB_STATE_NOTATTACHED ||
4821 udev->state == USB_STATE_SUSPENDED) {
4822 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4823 udev->state);
4824 return -EINVAL;
4825 }
4826
4827 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004828 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004829 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 return -EISDIR;
4831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 parent_hub = hdev_to_hub(parent_hdev);
4833
Sarah Sharpf74631e2012-06-25 12:08:08 -07004834 /* Disable LPM and LTM while we reset the device and reinstall the alt
4835 * settings. Device-initiated LPM settings, and system exit latency
4836 * settings are cleared when the device is reset, so we have to set
4837 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004838 */
4839 ret = usb_unlocked_disable_lpm(udev);
4840 if (ret) {
4841 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4842 goto re_enumerate;
4843 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07004844 ret = usb_disable_ltm(udev);
4845 if (ret) {
4846 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4847 __func__);
4848 goto re_enumerate;
4849 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004850
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 set_bit(port1, parent_hub->busy_bits);
4852 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4853
4854 /* ep0 maxpacket size may change; let the HCD know about it.
4855 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004856 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004858 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 break;
4860 }
4861 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004862
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 if (ret < 0)
4864 goto re_enumerate;
4865
4866 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004867 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 dev_info(&udev->dev, "device firmware changed\n");
4869 udev->descriptor = descriptor; /* for disconnect() calls */
4870 goto re_enumerate;
4871 }
Alan Stern4fe03872009-02-26 10:21:02 -05004872
4873 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 if (!udev->actconfig)
4875 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004876
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004877 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004878 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4879 if (ret < 0) {
4880 dev_warn(&udev->dev,
4881 "Busted HC? Not enough HCD resources for "
4882 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004883 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004884 goto re_enumerate;
4885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4887 USB_REQ_SET_CONFIGURATION, 0,
4888 udev->actconfig->desc.bConfigurationValue, 0,
4889 NULL, 0, USB_CTRL_SET_TIMEOUT);
4890 if (ret < 0) {
4891 dev_err(&udev->dev,
4892 "can't restore configuration #%d (error=%d)\n",
4893 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004894 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 goto re_enumerate;
4896 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004897 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4899
Alan Stern4fe03872009-02-26 10:21:02 -05004900 /* Put interfaces back into the same altsettings as before.
4901 * Don't bother to send the Set-Interface request for interfaces
4902 * that were already in altsetting 0; besides being unnecessary,
4903 * many devices can't handle it. Instead just reset the host-side
4904 * endpoint state.
4905 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004907 struct usb_host_config *config = udev->actconfig;
4908 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 struct usb_interface_descriptor *desc;
4910
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05004912 if (desc->bAlternateSetting == 0) {
4913 usb_disable_interface(udev, intf, true);
4914 usb_enable_interface(udev, intf, true);
4915 ret = 0;
4916 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08004917 /* Let the bandwidth allocation function know that this
4918 * device has been reset, and it will have to use
4919 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004920 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08004921 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05004922 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4923 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08004924 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05004925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 if (ret < 0) {
4927 dev_err(&udev->dev, "failed to restore interface %d "
4928 "altsetting %d (error=%d)\n",
4929 desc->bInterfaceNumber,
4930 desc->bAlternateSetting,
4931 ret);
4932 goto re_enumerate;
4933 }
4934 }
4935
4936done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07004937 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Alan Stern78d9a482008-06-23 16:00:40 -04004938 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07004939 usb_enable_ltm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 return 0;
4941
4942re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004943 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 hub_port_logical_disconnect(parent_hub, port1);
4945 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946}
4947
4948/**
4949 * usb_reset_device - warn interface drivers and perform a USB port reset
4950 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4951 *
Alan Stern79efa092006-06-01 13:33:42 -04004952 * Warns all drivers bound to registered interfaces (using their pre_reset
4953 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08004954 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04004955 *
Alan Stern79efa092006-06-01 13:33:42 -04004956 * Return value is the same as for usb_reset_and_verify_device().
4957 *
4958 * The caller must own the device lock. For example, it's safe to use
4959 * this from a driver probe() routine after downloading new firmware.
4960 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08004961 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04004962 *
4963 * If an interface is currently being probed or disconnected, we assume
4964 * its driver knows how to handle resets. For all other interfaces,
4965 * if the driver doesn't have pre_reset and post_reset methods then
4966 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04004967 */
Ming Lei742120c2008-06-18 22:00:29 +08004968int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04004969{
4970 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05004971 int i;
Alan Stern79efa092006-06-01 13:33:42 -04004972 struct usb_host_config *config = udev->actconfig;
4973
4974 if (udev->state == USB_STATE_NOTATTACHED ||
4975 udev->state == USB_STATE_SUSPENDED) {
4976 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4977 udev->state);
4978 return -EINVAL;
4979 }
4980
Alan Stern645daaa2006-08-30 15:47:02 -04004981 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05004982 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04004983
Alan Stern79efa092006-06-01 13:33:42 -04004984 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004985 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004986 struct usb_interface *cintf = config->interface[i];
4987 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004988 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05004989
4990 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004991 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04004992 if (drv->pre_reset && drv->post_reset)
4993 unbind = (drv->pre_reset)(cintf);
4994 else if (cintf->condition ==
4995 USB_INTERFACE_BOUND)
4996 unbind = 1;
4997 if (unbind)
4998 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004999 }
5000 }
5001 }
5002
Ming Lei742120c2008-06-18 22:00:29 +08005003 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005004
5005 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005006 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005007 struct usb_interface *cintf = config->interface[i];
5008 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005009 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05005010
Alan Stern78d9a482008-06-23 16:00:40 -04005011 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005012 drv = to_usb_driver(cintf->dev.driver);
5013 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04005014 rebind = (drv->post_reset)(cintf);
5015 else if (cintf->condition ==
5016 USB_INTERFACE_BOUND)
5017 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005018 }
Alan Stern6c640942008-10-21 15:40:03 -04005019 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04005020 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005021 }
5022 }
5023
Alan Stern94fcda12006-11-20 11:38:46 -05005024 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005025 return ret;
5026}
Ming Lei742120c2008-06-18 22:00:29 +08005027EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005028
5029
5030/**
5031 * usb_queue_reset_device - Reset a USB device from an atomic context
5032 * @iface: USB interface belonging to the device to reset
5033 *
5034 * This function can be used to reset a USB device from an atomic
5035 * context, where usb_reset_device() won't work (as it blocks).
5036 *
5037 * Doing a reset via this method is functionally equivalent to calling
5038 * usb_reset_device(), except for the fact that it is delayed to a
5039 * workqueue. This means that any drivers bound to other interfaces
5040 * might be unbound, as well as users from usbfs in user space.
5041 *
5042 * Corner cases:
5043 *
5044 * - Scheduling two resets at the same time from two different drivers
5045 * attached to two different interfaces of the same device is
5046 * possible; depending on how the driver attached to each interface
5047 * handles ->pre_reset(), the second reset might happen or not.
5048 *
5049 * - If a driver is unbound and it had a pending reset, the reset will
5050 * be cancelled.
5051 *
5052 * - This function can be called during .probe() or .disconnect()
5053 * times. On return from .disconnect(), any pending resets will be
5054 * cancelled.
5055 *
5056 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5057 * does its own.
5058 *
5059 * NOTE: We don't do any reference count tracking because it is not
5060 * needed. The lifecycle of the work_struct is tied to the
5061 * usb_interface. Before destroying the interface we cancel the
5062 * work_struct, so the fact that work_struct is queued and or
5063 * running means the interface (and thus, the device) exist and
5064 * are referenced.
5065 */
5066void usb_queue_reset_device(struct usb_interface *iface)
5067{
5068 schedule_work(&iface->reset_ws);
5069}
5070EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c72012-09-05 13:44:32 +08005071
5072/**
5073 * usb_hub_find_child - Get the pointer of child device
5074 * attached to the port which is specified by @port1.
5075 * @hdev: USB device belonging to the usb hub
5076 * @port1: port num to indicate which port the child device
5077 * is attached to.
5078 *
5079 * USB drivers call this function to get hub's child device
5080 * pointer.
5081 *
5082 * Return NULL if input param is invalid and
5083 * child's usb_device pointer if non-NULL.
5084 */
5085struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5086 int port1)
5087{
5088 struct usb_hub *hub = hdev_to_hub(hdev);
5089
5090 if (port1 < 1 || port1 > hdev->maxchild)
5091 return NULL;
5092 return hub->ports[port1 - 1]->child;
5093}
5094EXPORT_SYMBOL_GPL(usb_hub_find_child);
Lan Tianyud5575422012-09-05 13:44:33 +08005095
Lan Tianyu05f91682012-09-05 13:44:34 +08005096/**
5097 * usb_set_hub_port_connect_type - set hub port connect type.
5098 * @hdev: USB device belonging to the usb hub
5099 * @port1: port num of the port
5100 * @type: connect type of the port
5101 */
5102void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
5103 enum usb_port_connect_type type)
5104{
5105 struct usb_hub *hub = hdev_to_hub(hdev);
5106
5107 hub->ports[port1 - 1]->connect_type = type;
5108}
5109
5110/**
5111 * usb_get_hub_port_connect_type - Get the port's connect type
5112 * @hdev: USB device belonging to the usb hub
5113 * @port1: port num of the port
5114 *
5115 * Return connect type of the port and if input params are
5116 * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
5117 */
5118enum usb_port_connect_type
5119usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
5120{
5121 struct usb_hub *hub = hdev_to_hub(hdev);
5122
5123 return hub->ports[port1 - 1]->connect_type;
5124}
5125
Lan Tianyud5575422012-09-05 13:44:33 +08005126#ifdef CONFIG_ACPI
5127/**
5128 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5129 * @hdev: USB device belonging to the usb hub
5130 * @port1: port num of the port
5131 *
5132 * Return port's acpi handle if successful, NULL if params are
5133 * invaild.
5134 */
5135acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5136 int port1)
5137{
5138 struct usb_hub *hub = hdev_to_hub(hdev);
5139
5140 return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
5141}
5142#endif